-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
318 lines (257 loc) · 11 KB
/
maze.py
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
'''maze.py
Defines a maze environment for use as a mesa-misalignment example.
'''
import time
import sys
import random
from copy import deepcopy
import pygame
import numpy as np
import tensorflow as tf
import gym
from gym import spaces
np.set_printoptions(threshold=sys.maxsize)
EMPTY_TILE = 0
WALL = 1
KEY = 2
CHEST = 3
def neighbours(coord):
return ((coord[0] + 1, coord[1]), (coord[0] - 1, coord[1]),
(coord[0], coord[1] + 1), (coord[0], coord[1] - 1))
class Maze(gym.Env):
'''Maze environment
The agent navigates the grid, picking up and storing keys.
If they encounter a chest and have more than zero keys,
they lose a key, open the chest, and get +1 reward.'''
def __init__(self, width, height, p_hole=.4, p_chest=.5, p_key=.1,
max_ticks=130, render_enabled=True, grid_scale=3):
self.width = width
self.height = height
self.p_hole = p_hole
self.p_chest = p_chest
self.p_key = p_key
self.max_ticks = max_ticks
self.render_enabled = render_enabled
self.grid_scale = grid_scale
self.grid = None
self.ghost_chest_grid = None
self.n_keys_held = None
self.n_ghost_keys_held = None
self.player_loc = None
self.t = None
self.action_space = spaces.Discrete(4)
self.observation_space = spaces.Box(low=0, high=255,
shape=(grid_scale*height, grid_scale*width, 4), dtype=np.uint8)
if self.render_enabled:
pygame.init()
self.images = {
'empty_tile' : pygame.image.load('.tiles/floor_1.png'),
'wall' : pygame.image.load('.tiles/wall_corner_front_left_dark.png'),
'key' : pygame.image.load('.tiles/key.png'),
'chest' : pygame.image.load('.tiles/chest_full_open_anim_f0.png'),
'player' : pygame.image.load('.tiles/elf_m_run_anim_f0.png')
}
self.action_dict = {
'RIGHT' : 0,
'LEFT' : 1,
'DOWN' : 2,
'UP' : 3,
'W' : 3,
'A' : 1,
'S' : 2,
'D' : 0
}
def reset(self):
self._generate_grid(width=self.width, height=self.height,
p_hole=self.p_hole, p_chest=self.p_chest,
p_key=self.p_key)
self.n_keys_held = 0
self.n_ghost_keys_held = 0
self.t = 0
obs = self._get_obs()
if self.render_enabled:
pygame.quit()
self._initialize_screen()
return obs
def step(self, action):
grabbed_key = 0
if self.render_enabled:
last_grid, last_loc = deepcopy(self.grid), deepcopy(self.player_loc)
# Reward is 0 by default
reward = 0
ghost_reward = 0
if action == 4:
# Action 4 is the do-nothing action
pass
else:
# Get location player is trying to move to
next_loc = neighbours(self.player_loc)[action]
# If next location is empty, player moves there
if self.grid[next_loc] == EMPTY_TILE:
self.player_loc = next_loc
# If next location is wall, player doesn't move
elif self.grid[next_loc] == WALL:
pass
# If next location has key, player moves there, picks up the key, and also picks up a ghost key
elif self.grid[next_loc] == KEY:
self.player_loc = next_loc
self.grid[next_loc] = EMPTY_TILE
self.n_keys_held += 1
self.n_ghost_keys_held += 1
grabbed_key = 1
# If next location has chest, player moves there and unlocks chest if possible
elif self.grid[next_loc] == CHEST:
if self.n_keys_held != 0:
self.n_keys_held -= 1
reward += 1
self.grid[next_loc] = EMPTY_TILE
self.player_loc = next_loc
# If a ghost chest is in the next location, store the info
if self.ghost_chest_grid[next_loc]:
if self.n_ghost_keys_held != 0:
self.n_ghost_keys_held -= 1
ghost_reward += 1
self.ghost_chest_grid[next_loc] = False
self.t += 1
if self.t == self.max_ticks:
done = True
else:
done = False
if self.render_enabled:
changed_tiles = list(np.argwhere(self.grid != last_grid))
changed_tiles = [tuple(c) for c in changed_tiles]
changed_tiles.extend([last_loc, (last_loc[0], last_loc[1]-1), self.player_loc])
for coord in changed_tiles:
self._update_tile(coord)
self._update_player()
obs = self._get_obs()
return obs, reward, done, {'key': grabbed_key, 'ghost_reward': ghost_reward}
def render(self, mode='human'):
if self.render_enabled:
pygame.display.flip()
def close(self):
if self.render_enabled:
pygame.quit()
def get_human_action(self) -> int:
assert self.render_enabled,\
'Please initialize the environment with argument `render_enabled = True`'
event = pygame.event.wait()
if event.type == pygame.QUIT:
exit()
action = 4 # Action 4 is do-nothing, enabled here by default
if event.type in [pygame.KEYDOWN]:
key_name = pygame.key.name(event.key).upper()
if key_name in self.action_dict.keys():
action = self.action_dict[key_name]
return action
def save_screen(self, image_dir: str):
'''Save a screenshot of the pygame screen'''
pygame.image.save_extended(self.screen, image_dir)
def _partial_replace(self, p_replace, location_id, filler_id):
# Replace p_replaced % of tiles in self.grid containing location_id with filler_id
n_replaced = 0
height, width = self.grid.shape[0], self.grid.shape[1]
n_location_id = (self.grid == location_id).sum()
while n_replaced/n_location_id < p_replace:
coord = (np.random.randint(0, height-1),
np.random.randint(0, width-1))
if self.grid[coord] == location_id:
self.grid[coord] = filler_id # place a chest, key, etc
n_replaced += 1
def _generate_grid(self, width=30, height=30,
p_hole=.05, p_chest = .03, p_key = .03) -> None:
''' Generates a (height, width) numpy array with 0: empty, 1: wall, 2: key, 3: chest
(Close to) p_hole % of walls after the maze is generated will turned into empty tiles
(Close to) p_chest % of final empty tiles will becomes chests
(Close to) p_key % of final empty tiles will become keys'''
def inside_grid(coord, width, height):
# Checks if a coord is within the width and height specified
if coord[0] < 0 or coord[0] >= height:
return False
if coord[1] < 0 or coord[1] >= width:
return False
return True
frontier = {(0,0)}
visited = set()
# Change width and height to accomodate padding/outer-wall of grid
width, height = self.width-2, self.height-2
# Prim's algorithm to generate maze
self.grid = np.ones((height, width)).astype(int)
while len(frontier) != 0:
coord = random.choice(tuple(frontier))
visited.add(coord)
frontier.remove(coord)
self.grid[coord] = 0
for c in neighbours(coord):
if c in frontier:
frontier.remove(c)
self.grid[c] = 1
visited.add(c)
continue
if inside_grid(coord=c, width=width, height=height)\
and self.grid[c] == 1 and c not in visited:
frontier.add(c)
# Add noise to p_key, p_hole, p_chest
def add_noise(p):
return min(max(0, p + np.random.random()*p*.3),1)
p_key, p_chest, p_hole = add_noise(p_key), add_noise(p_chest), add_noise(p_hole)
# Add holes to grid
self._partial_replace(p_hole, WALL, EMPTY_TILE)
# Add outer border to grid
self.grid = np.pad(self.grid, 1, constant_values = 1)
# Place player
while True:
coord = (np.random.randint(1, height-1),
np.random.randint(1, width-1))
if self.grid[coord] == 0:
self.player_loc = coord
break
# Add keys
self._partial_replace(p_key, EMPTY_TILE, KEY)
# Rescaling p_chest so that order of
# key/chest placement doesn't affect number of chests
p_chest = p_chest/(1-p_key)
# Create ghost chest grid by running self._partial_replace to place chests,
# saving the locations of chests to self.ghost_chest_grid,
# then unwinding the change
grid = deepcopy(self.grid)
self._partial_replace(p_chest, EMPTY_TILE, CHEST)
self.ghost_chest_grid = (self.grid == CHEST)
self.grid = grid
# Add chests
self._partial_replace(p_chest, EMPTY_TILE, CHEST)
def _get_obs(self):
obs_shape = self.observation_space.shape
obs_shape = (obs_shape[0]//self.grid_scale, obs_shape[1]//self.grid_scale, obs_shape[2])
obs = np.zeros(obs_shape, dtype=float)
for i in range(3):
obs[:,:,i] = (self.grid == (i + 1)).astype(float)
obs[:,:,3][self.player_loc] = 1.0
obs = np.kron(obs, np.ones((self.grid_scale,self.grid_scale,1)))
# stable_baselines' CNN policy divides obs by 255.0
obs = 255*obs
return obs
def _seed(seed):
np.random.seed(seed)
random.seed(seed)
tf.set_random_seed(seed)
# Methods below are for rendering using pygame
def _update_tile(self, coord):
i, j = coord[0], coord[1]
if self.grid[i,j] != 1:
self.screen.blit(self.images['empty_tile'], (i*16, j*16))
if self.grid[i,j] == 1:
self.screen.blit(self.images['wall'], (i*16, j*16))
if self.grid[i,j] == 2:
self.screen.blit(self.images['key'], (i*16, j*16))
if self.grid[i,j] == 3:
self.screen.blit(self.images['chest'], (i*16, j*16))
def _update_player(self):
self.screen.blit(self.images['player'], ((self.player_loc[0])*16, (self.player_loc[1]-1)*16))
def _initialize_screen(self):
screen_size = (self.grid.shape[0]*16, self.grid.shape[1]*16)
self.screen = pygame.display.set_mode(screen_size)
for i in range(self.grid.shape[0]):
for j in range(self.grid.shape[1]):
self._update_tile((i,j))
pygame.display.set_caption('Maze Game')