-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.py
190 lines (152 loc) · 4.94 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
import numpy as np
import matplotlib.pyplot as plt
#Reward can be give as a function than as an array
class Maze():
def __init__(self,maze,rat=(0,0),cheese=None):
'''
Maze Coding:
1 - blocked
0 - Free
.5 - rat
'''
self.maze=maze
self.nrow=maze.shape[0]
self.ncol=maze.shape[1]
self.actions=[0,1,2,3]
self.rat=rat
if cheese is None:
self.cheese=(self.nrow-1,self.ncol-1)
else:
self.cheese=cheese
#Validation if both rat and cheese are in free cells
if maze[self.cheese] == 1:
raise Exception("Cheese shouldn't be on a blocked cell")
if maze[self.rat] == 1:
raise Exception("Rat shouldn't be on a blocked cell")
#For saving current state
self.state=(rat[0],rat[1],"start")
self.visited_states=set()
def get_possible_actions(self,cell=None):
'''
Get all the actions that's possible from a particular
cell in the maze.
'''
if cell is None:
row,col,mode=self.state
else:
row,col=cell
last_row,last_col=self.nrow-1,self.ncol-1
cell_actions=self.actions.copy()
#Removing invalid actions from the cells on the border
if col==0:
cell_actions.remove(3)
if row==0:
cell_actions.remove(0)
if col==last_col:
cell_actions.remove(1)
if row==last_row:
cell_actions.remove(2)
#Removing invalid actions into blocked cells
if row>0 and self.maze[row-1,col]==1:
cell_actions.remove(0)
if row<last_row and self.maze[row+1,col]==1:
cell_actions.remove(2)
if col>0 and self.maze[row,col-1]==1:
cell_actions.remove(3)
if col<last_col and self.maze[row,col+1]==1:
cell_actions.remove(1)
return cell_actions
def update_state(self,action):
valid_actions=self.get_possible_actions()
row,col,mode=self.state
#Add the current state to visited states
self.visited_states.add((row,col))
if action in valid_actions:
mode="valid"
if action==0:
row-=1
if action==1:
col+=1
if action==2:
row+=1
if action==3:
col-=1
else:
mode="invalid"
self.state=row,col,mode
def get_reward(self):
row,col,mode=self.state
#If an invalid move is attempted
if mode=="invalid":
return -10
#If found cheese - 100 points
if (row,col)==self.cheese:
return 100
#Return -1 for all other states
return -1
def get_status(self):
#Return True if found Cheese
row,col,mode=self.state
if (row,col)==self.cheese:
return True
else:
return False
def step(self,action):
self.update_state(action)
reward=self.get_reward()
done=self.get_status()
return self.state,reward,done
def reset(self):
row,col=self.rat
self.state=(row,col,"start")
self.visited_states=set()
def show(self):
canvas=np.copy(self.maze)
canvas[self.maze==0]=1
canvas[self.maze==1]=0
#Coloring Visited cells
for row,col in self.visited_states:
canvas[row,col]=0.2
#Coloring Rat cell
rat_row,rat_col,_ = self.state
canvas[rat_row,rat_col]=0.5
#Coloring Cheese cell
cheese_row,cheese_col=self.cheese
canvas[cheese_row,cheese_col]=.8
nrow,ncol=canvas.shape
plt.grid('on')
ax=plt.gca()
ax.set_xticks(np.arange(0.5,ncol,1))
ax.set_yticks(np.arange(0.5,nrow,1))
ax.set_xticklabels([])
ax.set_yticklabels([])
img=plt.imshow(canvas, interpolation='none', cmap='gray')
print("Hidh")
return img
def get_canvas(self):
canvas=np.copy(self.maze)
canvas[self.maze==0]=1
canvas[self.maze==1]=0
#Coloring Cheese cell
cheese_row,cheese_col=self.cheese
canvas[cheese_row,cheese_col]=.8
#Coloring Visited cells
for row,col in self.visited_states:
canvas[row,col]=0.2
#Coloring Rat cell
rat_row,rat_col,_ = self.state
canvas[rat_row,rat_col]=0.5
return canvas
def show_maze(self):
canvas=np.copy(self.maze)
canvas[self.maze==0]=1
canvas[self.maze==1]=0
nrow,ncol=canvas.shape
plt.grid('on')
ax=plt.gca()
ax.set_xticks(np.arange(0.5,ncol,1))
ax.set_yticks(np.arange(0.5,nrow,1))
ax.set_xticklabels([])
ax.set_yticklabels([])
img=plt.imshow(canvas, interpolation='none', cmap='gray')
return img