-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_en.py
executable file
·248 lines (207 loc) · 8.49 KB
/
maze_en.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
import numpy as np
import time
import sys
if sys.version_info.major == 2:
import Tkinter as tk
else:
import tkinter as tk
UNIT = 40
MAZE_H = 10
MAZE_W = 10
class Maze(tk.Tk, object):
def __init__(self):
super(Maze, self).__init__()
self.action_space = ['u', 'd', 'l', 'r']
self.n_actions = len(self.action_space)
self.title('environment')
self.geometry('{0}x{1}'.format(MAZE_H * UNIT, MAZE_H * UNIT))
self._build_maze()
def _build_maze(self):
self.canvas = tk.Canvas(self, bg='white',
height=MAZE_H * UNIT,
width=MAZE_W * UNIT)
# grids
for c in range(0, MAZE_W * UNIT, UNIT):
x0, y0, x1, y1 = c, 0, c, MAZE_H * UNIT
self.canvas.create_line(x0, y0, x1, y1)
for r in range(0, MAZE_H * UNIT, UNIT):
x0, y0, x1, y1 = 0, r, MAZE_H * UNIT, r
self.canvas.create_line(x0, y0, x1, y1)
# origin
origin = np.array([20, 20])
# hell
hell1_center = origin + np.array([UNIT * 2, UNIT*2])
self.hell1 = self.canvas.create_rectangle(
hell1_center[0] - 15, hell1_center[1] - 15,
hell1_center[0] + 15, hell1_center[1] + 15,
fill='black')
# hell
hell2_center = origin + np.array([UNIT*3, UNIT * 2])
self.hell2 = self.canvas.create_rectangle(
hell2_center[0] - 15, hell2_center[1] - 15,
hell2_center[0] + 15, hell2_center[1] + 15,
fill='black')
# hell
hell3_center = origin + np.array([UNIT * 4, UNIT*2])
self.hell3 = self.canvas.create_rectangle(
hell3_center[0] - 15, hell3_center[1] - 15,
hell3_center[0] + 15, hell3_center[1] + 15,
fill='black')
# hell
hell4_center = origin + np.array([UNIT * 5, UNIT*2])
self.hell4 = self.canvas.create_rectangle(
hell4_center[0] - 15, hell4_center[1] - 15,
hell4_center[0] + 15, hell4_center[1] + 15,
fill='black')
# hell
hell5_center = origin + np.array([UNIT*5, UNIT * 3])
self.hell5 = self.canvas.create_rectangle(
hell5_center[0] - 15, hell5_center[1] - 15,
hell5_center[0] + 15, hell5_center[1] + 15,
fill='black')
# hell
hell6_center = origin + np.array([UNIT * 5, UNIT*4])
self.hell6 = self.canvas.create_rectangle(
hell6_center[0] - 15, hell6_center[1] - 15,
hell6_center[0] + 15, hell6_center[1] + 15,
fill='black')
# hell
hell7_center = origin + np.array([UNIT * 8, 0])
self.hell7 = self.canvas.create_rectangle(
hell7_center[0] - 15, hell7_center[1] - 15,
hell7_center[0] + 15, hell7_center[1] + 15,
fill='black')
# hell
hell8_center = origin + np.array([UNIT * 8, UNIT * 1])
self.hell8 = self.canvas.create_rectangle(
hell8_center[0] - 15, hell8_center[1] - 15,
hell8_center[0] + 15, hell8_center[1] + 15,
fill='black')
# hell
hell9_center = origin + np.array([UNIT * 8, UNIT * 2])
self.hell9 = self.canvas.create_rectangle(
hell9_center[0] - 15, hell9_center[1] - 15,
hell9_center[0] + 15, hell9_center[1] + 15,
fill='black')
# hell
hell10_center = origin + np.array([UNIT * 8, UNIT * 3])
self.hell10 = self.canvas.create_rectangle(
hell10_center[0] - 15, hell10_center[1] - 15,
hell10_center[0] + 15, hell10_center[1] + 15,
fill='black')
# hell
hell11_center = origin + np.array([UNIT * 0, UNIT * 3])
self.hell11 = self.canvas.create_rectangle(
hell11_center[0] - 15, hell11_center[1] - 15,
hell11_center[0] + 15, hell11_center[1] + 15,
fill='black')
# hell
hell12_center = origin + np.array([UNIT * 1, UNIT * 3])
self.hell12 = self.canvas.create_rectangle(
hell12_center[0] - 15, hell12_center[1] - 15,
hell12_center[0] + 15, hell12_center[1] + 15,
fill='black')
# hell
hell13_center = origin + np.array([UNIT * 3, UNIT * 5])
self.hell13 = self.canvas.create_rectangle(
hell13_center[0] - 15, hell13_center[1] - 15,
hell13_center[0] + 15, hell13_center[1] + 15,
fill='black')
# hell
hell14_center = origin + np.array([UNIT * 3, UNIT * 6])
self.hell14 = self.canvas.create_rectangle(
hell14_center[0] - 15, hell14_center[1] - 15,
hell14_center[0] + 15, hell14_center[1] + 15,
fill='black')
# hell
hell15_center = origin + np.array([UNIT * 3, UNIT * 7])
self.hell15 = self.canvas.create_rectangle(
hell15_center[0] - 15, hell15_center[1] - 15,
hell15_center[0] + 15, hell15_center[1] + 15,
fill='black')
# hell
hell16_center = origin + np.array([UNIT * 6, UNIT * 8])
self.hell16 = self.canvas.create_rectangle(
hell16_center[0] - 15, hell16_center[1] - 15,
hell16_center[0] + 15, hell16_center[1] + 15,
fill='black')
# hell
hell17_center = origin + np.array([UNIT * 6, UNIT * 9])
self.hell17 = self.canvas.create_rectangle(
hell17_center[0] - 15, hell17_center[1] - 15,
hell17_center[0] + 15, hell17_center[1] + 15,
fill='black')
# hell
hell18_center = origin + np.array([UNIT * 7, UNIT * 8])
self.hell18 = self.canvas.create_rectangle(
hell18_center[0] - 15, hell18_center[1] - 15,
hell18_center[0] + 15, hell18_center[1] + 15,
fill='black')
# create oval
oval_center = origin + np.array([UNIT * 7, UNIT * 4])
self.oval = self.canvas.create_oval(
oval_center[0] - 15, oval_center[1] - 15,
oval_center[0] + 15, oval_center[1] + 15,
fill='yellow')
# create red rect
self.rect = self.canvas.create_rectangle(
origin[0] - 15, origin[1] - 15,
origin[0] + 15, origin[1] + 15,
fill='red')
# pack all
self.canvas.pack()
def reset(self):
self.update()
time.sleep(0.001)
self.canvas.delete(self.rect)
origin = np.array([20, 20])
self.rect = self.canvas.create_rectangle(
origin[0] - 15, origin[1] - 15,
origin[0] + 15, origin[1] + 15,
fill='red')
# return observation
return self.canvas.coords(self.rect)
def step(self, action):
s = self.canvas.coords(self.rect)
base_action = np.array([0, 0])
if action == 0: # up
if s[1] > UNIT:
base_action[1] -= UNIT
elif action == 1: # down
if s[1] < (MAZE_H - 1) * UNIT:
base_action[1] += UNIT
elif action == 2: # right
if s[0] < (MAZE_W - 1) * UNIT:
base_action[0] += UNIT
elif action == 3: # left
if s[0] > UNIT:
base_action[0] -= UNIT
self.canvas.move(self.rect, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.rect) # next state
# reward function
if s_ == self.canvas.coords(self.oval):
reward = 1
done = True
elif s_ in [self.canvas.coords(self.hell1), self.canvas.coords(self.hell2), self.canvas.coords(self.hell3),self.canvas.coords(self.hell4), self.canvas.coords(self.hell5), self.canvas.coords(self.hell6),self.canvas.coords(self.hell7), self.canvas.coords(self.hell8), self.canvas.coords(self.hell9),self.canvas.coords(self.hell10), self.canvas.coords(self.hell11), self.canvas.coords(self.hell12),self.canvas.coords(self.hell13), self.canvas.coords(self.hell14), self.canvas.coords(self.hell15),self.canvas.coords(self.hell16), self.canvas.coords(self.hell17), self.canvas.coords(self.hell18)]:
reward = -1
done = True
else:
reward = 0
done = False
return s_, reward, done
def render(self):
time.sleep(0.1)
self.update()
def update():
for t in range(10):
s = env.reset()
while True:
env.render()
a = 1
s, r, done = env.step(a)
if done:
break
if __name__ == '__main__':
env = Maze()
env.after(100, update)
env.mainloop()