-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrogger_cli.py
More file actions
executable file
·380 lines (317 loc) · 12.6 KB
/
Copy pathfrogger_cli.py
File metadata and controls
executable file
·380 lines (317 loc) · 12.6 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import os
import sys
import time
import torch
import select
from frogger_env import FroggerEnv
from frogger_policy import PolicyNet
from render_utils import render_game, clear_screen, print_title, print_raw
# Platform-specific imports for real-time key capture
if os.name == 'nt': # Windows
import msvcrt
else: # Linux
import tty
import termios
def getch():
"""Get single char. from stdin without echo"""
if os.name == 'nt':
return msvcrt.getch().decode('utf-8', errors='ignore')
else:
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def kbhit():
"""Check if key pressed (non-blocking)"""
if os.name == 'nt':
return msvcrt.kbhit()
else:
dr, dw, de = select.select([sys.stdin], [], [], 0)
return dr != []
def getch_nonblocking():
"""Get char. if available, o/w return None (non-blocking)"""
if os.name == 'nt':
if msvcrt.kbhit():
return msvcrt.getch().decode('utf-8', errors='ignore')
return None
else:
if kbhit():
ch = sys.stdin.read(1)
return ch
return None
class RawTerminal:
"""Context manager for raw terminal mode (Unix/Linux/MacOS)"""
def __init__(self):
if os.name != 'nt':
self.fd = sys.stdin.fileno()
self.old_settings = None
def __enter__(self):
if os.name != 'nt':
self.old_settings = termios.tcgetattr(self.fd)
tty.setraw(self.fd)
return self
def __exit__(self, type, value, traceback):
if os.name != 'nt' and self.old_settings is not None:
termios.tcsetattr(self.fd, termios.TCSADRAIN, self.old_settings)
def show_menu():
"""Display main menu + return user choices"""
clear_screen()
print_title()
# Rendering mode
print("Choose rendering mode:")
print(" [1] ASCII (simple characters)")
print(" [2] Emoji (🐸🚗🏆)")
print()
while True:
choice = input("Select mode (1 or 2): ").strip()
if choice == '1':
use_ascii = True
break
elif choice == '2':
use_ascii = False
break
else:
print("Invalid choice. Please enter 1 or 2.")
# Play mode (i.e., human or agent)
clear_screen()
print_title()
mode_str = "ASCII" if use_ascii else "Emoji"
print(f"Mode: {mode_str}\n")
print("Choose game mode:")
print(" [1] Play yourself")
print(" [2] Watch RL agent")
print(" [q] Quit")
print()
game_speed = None
while True:
choice = input("Select mode (1, 2, or q): ").strip().lower()
if choice == '1':
mode = 'human'
# Game speed for human play
clear_screen()
print_title()
print(f"Mode: {mode_str}\n")
print("Choose game speed:")
print(" [1] Fast (0.75s per step)")
print(" [2] Medium (1s per step)")
print(" [3] Slow (1.25s per step) *Recommended*")
print()
while True:
speed_choice = input("Select speed (1, 2, or 3): ").strip()
if speed_choice == '1':
game_speed = 0.75
break
elif speed_choice == '2':
game_speed = 1
break
elif speed_choice == '3':
game_speed = 1.25
break
else:
print("Invalid choice. Please enter 1, 2, or 3.")
return mode, use_ascii, game_speed
elif choice == '2':
return 'agent', use_ascii, None
elif choice == 'q':
return None, None, None
else:
print("Invalid choice. Please enter 1, 2, or q.")
def human_play(use_ascii=True, game_speed=0.3):
"""Human player mode with real-time game updates"""
env = FroggerEnv()
episode = 0
# Track last action for continuous input
current_action = 4 # Default to STAY
quit_flag = False
# Use raw terminal mode for entire play session
with RawTerminal():
while True:
state = env.reset()
episode += 1
done = False
total_reward = 0.0
step_count = 0
while not done:
# Display curr. state
clear_screen()
print_raw("╔═══════════════════════════════════╗")
print_raw("║ F R O G G E R ║")
print_raw("║ Reinforcement Learning ║")
print_raw("╚═══════════════════════════════════╝")
print_raw()
print_raw(f"Episode {episode} | Step {step_count} | Reward: {total_reward:.2f}")
print_raw("─" * 35)
print_raw()
# Render game grid w/ raw terminal
render_game(env, use_ascii, use_carriage_returns=True)
print_raw()
print_raw("Controls: W=up, S=down, A=left, D=right, Q=quit")
print_raw(f"Game Speed: {game_speed}s/step")
print_raw()
# Wait for game_speed duration while checking for input
start_time = time.time()
current_action = 4 # Default to STAY
while (time.time() - start_time) < game_speed:
# Check for input during the wait period
key = getch_nonblocking()
if key is not None:
key_lower = key.lower()
if key_lower == 'q':
quit_flag = True
break
# Map keys to actions (WASD only)
action_map = {
'w': 0, # UP
's': 1, # DOWN
'a': 2, # LEFT
'd': 3, # RIGHT
' ': 4, # STAY
}
current_action = action_map.get(key_lower, 4)
# Small sleep to prevent busy-waiting
time.sleep(0.01)
if quit_flag:
break
if quit_flag:
break
# Take action (user input or STAY)
_, reward, done, _ = env.step(current_action)
total_reward += reward
step_count += 1
if quit_flag:
return
# Episode ended
clear_screen()
print_raw("╔═══════════════════════════════════╗")
print_raw("║ F R O G G E R ║")
print_raw("║ Reinforcement Learning ║")
print_raw("╚═══════════════════════════════════╝")
print_raw()
print_raw(f"Episode {episode} Complete!")
print_raw("─" * 35)
print_raw()
# Render final state
render_game(env, use_ascii, use_carriage_returns=True)
print_raw()
if total_reward >= 4.0: # reached goal
print_raw("🎉 SUCCESS! You reached the goal!")
elif total_reward <= -10.0:
print_raw("⏱️ Time's up!")
else:
print_raw("💥 CRASH! You got hit by a car.")
print_raw(f"\r\nFinal Reward: {total_reward:.2f}")
print_raw()
print_raw("Press any key to play again, or 'q' to quit...")
key = getch_nonblocking()
while key is None:
time.sleep(0.01)
key = getch_nonblocking()
if key.lower() == 'q':
return
def watch_agent(use_ascii=True, episodes=5):
"""Watch RL agent play"""
checkpoint_path = "checkpoints/frogger_policy_0.98.pt"
if not os.path.exists(checkpoint_path):
print(f"Error: Could not find policy at {checkpoint_path}")
print("Press any key to return to menu...")
getch()
return
env = FroggerEnv()
obs_dim = env._get_obs().shape[0]
n_actions = 5
policy = PolicyNet(input_dim=obs_dim, hidden_dim=128, n_actions=n_actions)
state_dict = torch.load(checkpoint_path, map_location=torch.device("cpu"))
policy.load_state_dict(state_dict)
policy.eval()
cumulative_reward = 0.0
# Action names for display
action_names = {0: "UP", 1: "DOWN", 2: "LEFT", 3: "RIGHT", 4: "STAY"}
for ep in range(episodes):
state = env.reset()
done = False
total_reward = 0.0
step_count = 0
actions_taken = [] # Track all actions in this episode
current_action = None
while not done and step_count < 200:
clear_screen()
print_title()
print(f"Episode {ep+1}/{episodes} | Step {step_count} | Cumulative: {cumulative_reward:.2f}")
print("─" * 35)
print()
render_game(env, use_ascii)
print()
# Display episode reward with last move
if current_action is not None:
print(f"Episode Reward: {total_reward:.2f} | {action_names[current_action]}")
else:
print(f"Episode Reward: {total_reward:.2f}")
print()
print("(Press Ctrl+C to stop)")
time.sleep(0.5) # Slower pace for watching
# Get action from policy
state_tensor = torch.from_numpy(state).float().unsqueeze(0)
with torch.no_grad():
logits = policy(state_tensor)
action = logits.argmax(dim=-1).item()
current_action = action
actions_taken.append(action_names[action])
state, reward, done, _ = env.step(action)
total_reward += reward
step_count += 1
cumulative_reward += total_reward
# Final frame
clear_screen()
print_title()
print(f"Episode {ep+1}/{episodes} Complete!")
print("─" * 35)
print()
render_game(env, use_ascii)
print()
if total_reward >= 4.0:
print("✓ Agent reached the goal!")
elif total_reward < -0.5:
print("✗ Agent failed to reach the goal.")
# Display episode reward with last move
if current_action is not None:
print(f"\nEpisode Reward: {total_reward:.2f} | {action_names[current_action]}")
else:
print(f"\nEpisode Reward: {total_reward:.2f}")
print(f"Cumulative Reward: {cumulative_reward:.2f}")
print(f"Moves: {actions_taken}")
print()
if ep < episodes - 1:
input("Press Enter to continue to next episode...")
else:
# Last episode
print(f"All {episodes} episodes complete!")
print(f"Average reward: {cumulative_reward/episodes:.2f}")
print()
input("Press Enter to return to menu...")
return
def main():
"""Main entry point"""
try:
while True:
mode, use_ascii, game_speed = show_menu()
if mode is None:
clear_screen()
print("Thanks for playing! 🐸")
break
elif mode == 'human':
human_play(use_ascii=use_ascii, game_speed=game_speed)
elif mode == 'agent':
watch_agent(use_ascii=use_ascii, episodes=5)
except KeyboardInterrupt:
clear_screen()
print("\nThanks for playing! 🐸")
except Exception as e:
print(f"\nError: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()