-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathno_walls.py
More file actions
173 lines (151 loc) · 5.41 KB
/
no_walls.py
File metadata and controls
173 lines (151 loc) · 5.41 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
import numpy as np
import random
from copy import deepcopy
import cv2
import scipy.ndimage
import pickle
# Height and width of maze
H, W = 50, 50
# Pixels per square of maze
PPS = 20
# Parameter affecting branching tendency of maze (higher <=> more branching)
ALT = .15
'''
Maze is a 2D list with each index containing another list
of form [0,0,1,0] or [up?, down?, left?, right?]
indicating whether or not one can move up, down, left or right.
[0,1,1,0] ==> down and left are moves without walls
Functions:
generateMaze(W, H) ==> 2D list of list [up?, down?, left?, right?]
step(state, action, maze) ==> reward, new_state
updateV(V, T, gamma) ==> Takes in current value function (table corresponding to value of cells),
maze transitions (stored in T) and updates it with optimal value iteration.
show(maze, PPS) ==> Renders maze
'''
def generateMaze(W=6, H=6, alt=False):
# Returns maze to specs above
def adjacent(maze, coord, visited=[]):
# Return list of unvisited cells adjacent to coord
H = len(maze)
W = len(maze[0])
considered = [(coord[0]-1, coord[1]),
(coord[0]+1, coord[1]),
(coord[0], coord[1]-1),
(coord[0], coord[1]+1)]
result = []
for adjacentCell in considered:
if adjacentCell in visited:
continue
if adjacentCell[0] == -1 or adjacentCell[0] == H:
continue
if adjacentCell[1] == -1 or adjacentCell[1] == W:
continue
result.append(adjacentCell)
return result
maze = []
for i in range(H):
maze.append([])
for j in range(W):
maze[-1].append([0,0,0,0,0])
lastCoord = (np.random.choice(H), np.random.choice(W))
C = [lastCoord]
visited = []
while C:
if alt is not False:
if random.random() < alt:
coord = random.choice(C)
C.remove(coord)
C.append(coord)
next_cells = adjacent(maze=maze, coord=C[-1], visited=visited)
if not next_cells:
C.pop()
else:
next_cell = random.choice(next_cells)
connection = (next_cell[0]-C[-1][0], next_cell[1]-C[-1][1])
if connection == (-1,0):
maze[C[-1][0]][C[-1][1]][0] = 1
maze[next_cell[0]][next_cell[1]][1] = 1
if connection == (1,0):
maze[C[-1][0]][C[-1][1]][1] = 1
maze[next_cell[0]][next_cell[1]][0] = 1
if connection == (0,-1):
maze[C[-1][0]][C[-1][1]][2] = 1
maze[next_cell[0]][next_cell[1]][3] = 1
if connection == (0,1):
maze[C[-1][0]][C[-1][1]][3] = 1
maze[next_cell[0]][next_cell[1]][2] = 1
C.append(next_cell)
visited.append(next_cell)
return np.array(maze)
def step(SA, maze, H, W):
# step(SA, maze) ==> reward, new_state
# SA is state-action tuple
if SA[2] == 4:
new_state = (SA[0], SA[1])
if new_state == (H-1,W-1):
reward = 10000
else:
reward = -1
if maze[SA[0:4]] == 1: # opening in direction of action
if SA[2] == 0:
new_state = (SA[0]-1, SA[1])
if SA[2] == 1:
new_state = (SA[0]+1, SA[1])
if SA[2] == 2:
new_state = (SA[0], SA[1]-1)
if SA[2] == 3:
new_state = (SA[0], SA[1]+1)
if new_state == (H-1,W-1):
reward = 10000
else:
reward = -1
else: # bounce off wall
new_state = SA[0:2]
reward = -100
return reward, new_state
def updateV(V, T, gamma=.999,):
# Update value function (V) with transitions (T)
Vnew = np.zeros(V.shape)
H, W = V.shape[0:2]
for i in range(H):
for j in range(W):
Q_est = []
for a in range(5): # possible action
next_state = T[i,j,a,0:2]
# reward + Q(next_state) for each possible action
Q_est.append(T[i,j,a,2] + gamma*V[int(next_state[0]),int(next_state[1])][0])
Vnew[i,j] = max(Q_est)
return Vnew
def show(maze, PPS):
# Render maze
H, W = maze.shape[0:2]
transitions = np.zeros((*maze.shape, 3))
# For each possible maze coordinate, action,
# transitions stores [next_state[0], next_state[1], reward]
for i in range(H):
for j in range(W):
for k in range(5):
r, ns = step((i,j,k), maze, H=H, W=W) # reward, next_state
transitions[i,j,k] = [*ns, r]
T = transitions
V = np.zeros((H, W, 1)) # Optimal Policy Value
# Options to pickle frames commented out below. video.py won't necessarily work, but will with a few adjustments.
# frames = []
for i in range(10000):
im = cv2.normalize(V, None, alpha=.1, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
im = np.kron(im, np.ones((PPS, PPS)))
cv2.imshow('Maze',im)
# if i % 3 == 0:
# frames.append(V)
V = updateV(V, T)
if cv2.waitKey(1)==27:
break
# item = [frames, maze]
# pickle_out = open("frames.pickle", "wb")
# pickle.dump(item, pickle_out)
# pickle_out.close()
def main(H, W, PPS, ALT=.15):
maze = np.array(generateMaze(H=H, W=W, alt=ALT))
show(maze, PPS=PPS)
if __name__ == '__main__':
main(H, W, PPS, ALT)