-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
177 lines (154 loc) · 6.15 KB
/
Copy pathapp.py
File metadata and controls
177 lines (154 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import streamlit as st
import time
import os
from web_game_rl import SnakeGameRL
import numpy as np
from PIL import Image
# Set page configuration
st.set_page_config(
page_title="Snake AI Reinforcement Learning",
page_icon="🐍",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for styling
st.markdown("""
<style>
.main-header {
font-size: 3rem;
color: #4CAF50;
text-align: center;
margin-bottom: 2rem;
font-weight: bold;
}
.sub-header {
font-size: 1.5rem;
color: #2E7D32;
margin-bottom: 1rem;
}
.info-text {
font-size: 1.1rem;
line-height: 1.6;
}
.stButton>button {
background-color: #4CAF50;
color: white;
font-size: 1.2rem;
padding: 0.5rem 2rem;
border-radius: 8px;
border: none;
transition: all 0.3s;
}
.stButton>button:hover {
background-color: #2E7D32;
transform: scale(1.05);
}
</style>
""", unsafe_allow_html=True)
# Initialize session state variables
if 'initialized' not in st.session_state:
st.session_state['initialized'] = True
st.session_state['game'] = None
st.session_state['game_active'] = False
st.session_state['score'] = 0
st.session_state['game_over'] = False
st.session_state['auto_refresh'] = True
st.session_state['games_played'] = 0
st.session_state['record'] = 0
# Main app layout
st.markdown("<h1 class='main-header'>🐍 Snake AI Reinforcement Learning</h1>", unsafe_allow_html=True)
# Create a two-column layout
left_col, right_col = st.columns([2, 3])
with left_col:
# Control section
st.markdown("<h2 class='sub-header'>Snake Game Controls</h2>", unsafe_allow_html=True)
# Start/Stop button
if not st.session_state['game_active']:
if st.button("Start Snake AI"):
# Initialize the game with reinforcement learning
st.session_state['game'] = SnakeGameRL(480, 360)
st.session_state['game_active'] = True
st.session_state['score'] = 0
st.session_state['game_over'] = False
else:
if st.button("Stop Snake AI"):
st.session_state['game_active'] = False
# Reset button
if st.button("Reset Game"):
if st.session_state['game'] is not None:
st.session_state['game'].reset()
st.session_state['score'] = 0
st.session_state['game_over'] = False
# Train button
if st.button("Train Network"):
if st.session_state['game'] is not None:
st.session_state['game'].train_long_memory()
st.info("Training completed on current memory batch")
# Auto-refresh toggle
auto_refresh = st.checkbox("Auto-refresh Game", value=st.session_state['auto_refresh'])
st.session_state['auto_refresh'] = auto_refresh
# Speed slider
speed = st.slider("Game Speed", min_value=10, max_value=60, value=20, step=5)
if st.session_state['game'] is not None:
# This won't actually change the speed in the current game instance
# But we'll use it as a reference for the next game
st.session_state['game_speed'] = speed
else:
st.session_state['game_speed'] = speed
# Game metrics
st.markdown("<h2 class='sub-header'>Game Stats</h2>", unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
st.metric("Current Score", st.session_state['score'])
st.metric("Games Played", st.session_state['games_played'])
with col2:
status = "Game Over" if st.session_state['game_over'] else "Playing" if st.session_state['game_active'] else "Not Started"
st.metric("Game Status", status)
st.metric("Record Score", st.session_state['record'])
# Project information
st.markdown("<h2 class='sub-header'>Project Information</h2>", unsafe_allow_html=True)
st.markdown("""
<p class='info-text'>
This Snake AI project uses reinforcement learning to train an agent to play the Snake game.
The agent learns through experience, improving its performance over time.
</p>
<p class='info-text'>
<b>How Reinforcement Learning Works:</b>
<ul>
<li><b>State:</b> The AI sees the game state (danger directions, food location, etc.)</li>
<li><b>Action:</b> Based on the state, the AI chooses an action (move straight, turn left/right)</li>
<li><b>Reward:</b> The AI gets rewards for eating food (+10) and penalties for crashing (-10)</li>
<li><b>Learning:</b> The AI updates its neural network based on these experiences</li>
<li><b>Memory:</b> The AI remembers past experiences and learns from them in batches</li>
</ul>
</p>
""", unsafe_allow_html=True)
with right_col:
# Game visualization
st.markdown("<h2 class='sub-header'>Snake Game</h2>", unsafe_allow_html=True)
# Create a placeholder for the game
game_placeholder = st.empty()
# Update game state and display
if st.session_state['game_active']:
# Play one step of the game
game_over, score = st.session_state['game'].play_step()
# Update session state
st.session_state['score'] = score
st.session_state['game_over'] = game_over
# Update games played and record
st.session_state['games_played'] = st.session_state['game'].agent.n_games
st.session_state['record'] = st.session_state['game'].record
# Get the current frame and display it
frame = st.session_state['game'].get_frame()
game_placeholder.image(frame, caption="Snake AI Game", use_column_width=False, width=480)
elif st.session_state['game'] is not None:
# Display the last frame if game is paused
frame = st.session_state['game'].get_frame()
game_placeholder.image(frame, caption="Snake AI Game", use_column_width=False, width=480)
else:
# Display a placeholder if game hasn't started
game_placeholder.info("Click 'Start Snake AI' to begin playing!")
# Auto-refresh mechanism
if st.session_state['auto_refresh'] and st.session_state['game_active']:
time.sleep(0.05) # Reduced wait time for faster refresh
st.rerun()