-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.py
More file actions
49 lines (38 loc) · 1.19 KB
/
testing.py
File metadata and controls
49 lines (38 loc) · 1.19 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
import gymnasium as gym
from stable_baselines3 import PPO
from square_lunar_lander import SquareLunarLander
import pygame
import time
def run_trained_agent():
# Initialize environment
env = SquareLunarLander(render_mode='human')
# Load trained model
try:
model = PPO.load("square_lander_basic")
print("Model loaded successfully")
except Exception as e:
print(f"Error loading model: {e}")
return
# Run the agent
obs, _ = env.reset()
clock = pygame.time.Clock()
running = True
while running:
# Handle Pygame events (critical!)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Get action from trained agent
action, _ = model.predict(obs, deterministic=True)
# Step environment
obs, _, done, _, _ = env.step(action)
# Render
env.render()
# Control speed
clock.tick(60) # 60 FPS
if done:
print("Episode completed, resetting...")
obs, _ = env.reset()
env.close()
if __name__ == "__main__":
run_trained_agent()