jatinror commited on
Commit
f9f2735
·
verified ·
1 Parent(s): d2b5471

Upload test_pixelcopter.py

Browse files
Files changed (1) hide show
  1. test_pixelcopter.py +127 -0
test_pixelcopter.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pygame
2
+ import time
3
+ import numpy as np
4
+ from stable_baselines3 import PPO
5
+ from gymnasium import spaces
6
+ import gymnasium as gym
7
+
8
+ # -------------------------
9
+ # Side-Scrolling PixelCopter Environment (Medium/Certification Friendly)
10
+ # -------------------------
11
+ class PixelCopterCertEnv(gym.Env):
12
+ def __init__(self, screen_width=50, screen_height=10, gap_size=6):
13
+ super().__init__()
14
+ self.screen_width = screen_width
15
+ self.screen_height = screen_height
16
+ self.copter_y = self.screen_height // 2
17
+ self.copter_velocity = 0
18
+ self.gravity = 0.25
19
+ self.lift = -0.9
20
+ self.done = False
21
+ self.timestep = 0
22
+ self.max_timesteps = 500
23
+ self.gap_size = gap_size
24
+ self.wall_gap_positions = [np.random.randint(1, self.screen_height - self.gap_size -1)
25
+ for _ in range(screen_width)]
26
+ self.action_space = spaces.Discrete(2)
27
+ self.observation_space = spaces.Box(
28
+ low=0, high=self.screen_height, shape=(self.screen_width + 1,), dtype=np.float32
29
+ )
30
+
31
+ def reset(self, seed=None, options=None):
32
+ self.copter_y = self.screen_height // 2
33
+ self.copter_velocity = 0
34
+ self.done = False
35
+ self.timestep = 0
36
+ self.wall_gap_positions = [np.random.randint(1, self.screen_height - self.gap_size -1)
37
+ for _ in range(self.screen_width)]
38
+ obs = np.array([self.copter_y] + self.wall_gap_positions, dtype=np.float32)
39
+ return obs, {}
40
+
41
+ def step(self, action):
42
+ if action == 1:
43
+ self.copter_velocity += self.lift
44
+ self.copter_velocity += self.gravity
45
+ self.copter_y += self.copter_velocity
46
+ self.copter_y = np.clip(self.copter_y, 0, self.screen_height)
47
+
48
+ self.wall_gap_positions = self.wall_gap_positions[1:]
49
+ last_gap = self.wall_gap_positions[-1]
50
+ new_gap = last_gap + np.random.choice([-1,0,1])
51
+ new_gap = np.clip(new_gap, 1, self.screen_height - self.gap_size -1)
52
+ self.wall_gap_positions.append(new_gap)
53
+
54
+ gap_top = self.wall_gap_positions[0]
55
+ gap_bottom = gap_top + self.gap_size
56
+ if self.copter_y <= gap_top or self.copter_y >= gap_bottom:
57
+ self.done = True
58
+ reward = -5
59
+ else:
60
+ reward = 1
61
+
62
+ self.timestep += 1
63
+ if self.timestep >= self.max_timesteps:
64
+ self.done = True
65
+
66
+ obs = np.array([self.copter_y] + self.wall_gap_positions, dtype=np.float32)
67
+ return obs, reward, self.done, False, {}
68
+
69
+ # -------------------------
70
+ # Pygame Setup
71
+ # -------------------------
72
+ pygame.init()
73
+ WINDOW_WIDTH = 800
74
+ WINDOW_HEIGHT = 400
75
+ WIN = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
76
+ pygame.display.set_caption("PixelCopter Certification Test")
77
+ WHITE = (255,255,255)
78
+ BLUE = (0,102,255)
79
+ BLACK = (0,0,0)
80
+
81
+ def draw_screen(obs, env):
82
+ WIN.fill(WHITE)
83
+ scale_y = WINDOW_HEIGHT / env.screen_height
84
+ scale_x = WINDOW_WIDTH / env.screen_width
85
+ copter_y = int(obs[0])
86
+ wall_gaps = [int(gap) for gap in obs[1:]]
87
+
88
+ # Draw walls
89
+ for i, gap_top in enumerate(wall_gaps):
90
+ gap_bottom = gap_top + env.gap_size
91
+ pygame.draw.rect(WIN, BLACK, pygame.Rect(i*scale_x, 0, scale_x, gap_top*scale_y))
92
+ pygame.draw.rect(WIN, BLACK, pygame.Rect(i*scale_x, gap_bottom*scale_y, scale_x, WINDOW_HEIGHT - gap_bottom*scale_y))
93
+
94
+ # Draw copter
95
+ pygame.draw.rect(WIN, BLUE, pygame.Rect(50, copter_y*scale_y - 10, 20, 20))
96
+ pygame.display.update()
97
+
98
+ # -------------------------
99
+ # Load Model & Test
100
+ # -------------------------
101
+ env = PixelCopterCertEnv(screen_width=80, screen_height=10, gap_size=6)
102
+ model = PPO.load("ppo_pixelcopter_cert")
103
+
104
+ episodes = 5
105
+ for ep in range(episodes):
106
+ obs, _ = env.reset()
107
+ done = False
108
+ total_reward = 0
109
+ while not done:
110
+ for event in pygame.event.get():
111
+ if event.type==pygame.QUIT:
112
+ done=True
113
+ pygame.quit()
114
+ action, _ = model.predict(obs)
115
+ obs, reward, done, truncated, info = env.step(action)
116
+ total_reward += reward
117
+ draw_screen(obs, env)
118
+ time.sleep(0.03)
119
+ print(f"Episode {ep+1} reward: {total_reward}")
120
+
121
+ pygame.quit()
122
+
123
+
124
+
125
+
126
+
127
+