-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrendering.py
More file actions
221 lines (196 loc) · 7.59 KB
/
Copy pathrendering.py
File metadata and controls
221 lines (196 loc) · 7.59 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
import pygame
import numpy as np
import sys
class Renderer:
"""
Renders the grid world env with agent and targets.
"""
metadata = {"render_modes": ["human", "rgb_array"], "render_fps": 4}
def __init__(
self, grid_size: int, meta_data, window_size: int = 512, render_mode=None
):
"""
:param grid_size: The size of the gird in cells (e.g. 5)
:param meta_data: Meta data such as rendering modes and fps
:param window_size: The displayed window size in pixels
:param render_mode: "human", "rgb_array" or None
"""
pygame.init()
name = pygame.font.get_default_font()
self._font = pygame.font.SysFont(name=name, size=20)
self.window_size = window_size # pyGame window size
assert render_mode is None or render_mode in self.metadata["render_modes"]
self.render_mode = render_mode
self.grid_size = grid_size
self.metadata = meta_data
self.window = None
self.clock = None
def render(
self,
agent_location,
new_episode=False,
targets=None,
visited_cells_count=None,
):
"""
Renders in rgb_mode
:param agent_location: The location of the agent in the grid world
:param new_episode: Flag if a new episode has started. Renders a black screen between episodes
:param targets: The targets of the grid world
:param visited_cells_count: Array / Matrix that counts how often a cell (position in grid) was visited by the agent (Optional parameter.)
"""
if self.render_mode == "rgb_array":
return self.render_frame(
agent_location=agent_location,
new_episode=new_episode,
targets=targets,
visited_cells_count=visited_cells_count
)
def render_frame_for_humans_if_needed(
self,
agent_location,
new_episode=False,
targets=None,
visited_cells_count=None
):
"""
Renders the frame if rendering_mode == "human"
:param agent_location: The location of the agent in the grid world
:param new_episode: Flag if a new episode has started. Renders a black screen between episodes
:param targets: The targets that should be rendered.
:param visited_cells_count: / Matrix that counts how often a cell (position in grid) was visited by the agent (Optional parameter.)
"""
if self.render_mode == "human":
return self.render_frame(
agent_location,
new_episode=new_episode,
targets=targets,
visited_cells_count=visited_cells_count,
)
def render_frame(
self,
agent_location,
new_episode=False,
targets=None,
visited_cells_count=None,
):
"""
The actual rendering function
:param agent_location: The location of the agent in the grid world
:param new_episode: Flag if a new episode has started. Renders a black screen between episodes
:param targets: The targets that should be rendered
:param visited_cells_count: Array / Matrix that counts how often a cell (position in grid) was visited by the agent (Optional parameter.)
"""
space_top = 0
window_length = window_height = self.window_size
if self.window is None and self.render_mode == "human":
pygame.init()
pygame.display.init()
self.window = pygame.display.set_mode(
(window_length, self.window_size + space_top)
)
if self.clock is None and self.render_mode == "human":
self.clock = pygame.time.Clock()
canvas = pygame.Surface(self.window.get_size())
canvas.fill((255, 255, 255))
if new_episode:
# render a black screen when a new episode begins
canvas.fill((0, 0, 0))
else:
# the size of a single cell in pixels
pix_square_size = self.window_size / self.grid_size
env_grid = self.draw_environment(
size=self.grid_size,
pix_square_size=pix_square_size,
agent_location=agent_location,
targets=targets,
visited_cells_count=visited_cells_count,
)
canvas.blit(env_grid, (0, space_top))
if self.render_mode == "human":
# copy content from canvas to visible window
self.window.blit(canvas, canvas.get_rect())
pygame.event.pump()
pygame.display.update()
# makes pygame window closable
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Ensure that rendering occurs at the predefined framerate
# This code adds delay to keep framerate stable
self.clock.tick(self.metadata["render_fps"])
else: # rgb array
return np.transpose(
np.array(pygame.surfarray.pixels3d(canvas)), axes=(1, 0, 2)
)
def draw_environment(
self,
size: int,
pix_square_size,
agent_location,
targets=None,
visited_cells_count=None,
) -> pygame.surface:
canvas = pygame.Surface((self.window_size, self.window_size))
canvas.fill((255, 255, 255))
visited_cells_count = {} if visited_cells_count is None else visited_cells_count
for position, times_visited in visited_cells_count.items():
# color visited cell according to times visited
# yellow for the first visit, then darken it linearly, darkest color is (255, 25, 0)
red = max(255 - 2 * (times_visited - 1), 225)
green = max(255 - 45 * (times_visited - 1), 25)
visited_cell_color = (red, green, 0) # yellow
pygame.draw.rect(
canvas,
visited_cell_color,
pygame.Rect(
pix_square_size * np.asarray(position),
(pix_square_size, pix_square_size),
),
)
if targets is None:
targets = []
# draw the targets
for target in targets:
target_color_base = (
target.color
if not (
np.array_equal(target.position, agent_location)
)
else (0, 255, 0)
)
pygame.draw.rect(
canvas,
target_color_base,
pygame.Rect(
pix_square_size * target.position,
(pix_square_size, pix_square_size),
),
)
# draw the agent
pygame.draw.circle(
canvas,
(0, 0, 255),
(agent_location + 0.5) * pix_square_size,
pix_square_size / 3,
)
# draw the grid
self.draw_grid_lines(size=size, pix_square_size=pix_square_size, canvas=canvas)
return canvas
def draw_grid_lines(self, size: int, pix_square_size, canvas: pygame.surface):
for x in range(size + 1):
pygame.draw.line(
canvas,
0,
(0, pix_square_size * x),
(self.window_size, pix_square_size * x),
width=3,
)
pygame.draw.line(
canvas,
0,
(pix_square_size * x, 0),
(pix_square_size * x, self.window_size),
width=3,
)