-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathnew_small_maze.py
404 lines (351 loc) · 13.6 KB
/
new_small_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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 31 20:00:07 2020
@author: jingci
"""
import numpy as np
import time
import sys
if sys.version_info.major == 2:
import Tkinter as tk
else:
import tkinter as tk
UNIT = 20 # pixels
MAZE_H = 21 # grid height
MAZE_W = 21 # grid width
#shelf coordinates
X_Block_pic = [1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19]
X_Block = [element * UNIT for element in X_Block_pic]
Y_Block_pic = [5,6,8,9,11,12,14,15,17,18]
Y_Block = [element * UNIT for element in Y_Block_pic]
origin1 = np.array([70, 50])
origin2 = np.array([210,50])
origin3 = np.array([350,50])
class Maze(tk.Tk, object):
def __init__(self):
super(Maze, self).__init__()
self.action_space = ['u', 'd', 'l', 'r','w'] #up, down, left, right, wait
self.n_actions = len(self.action_space)
self.title('Warehouse')
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='oldlace',
height=MAZE_H * UNIT,
width=MAZE_W * UNIT)
# create 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_W * UNIT, r
self.canvas.create_line(x0, y0, x1, y1)
# create operation desks
for i in range (1,16,7):
self.canvas.create_rectangle(i*UNIT, 0, (i+5)*UNIT, 2*UNIT, fill = 'sandybrown')
# create shelves
for k in range (1,17,5):
for i in range (5,18,3):
self.canvas.create_rectangle(k*UNIT, i*UNIT, (k+4)*UNIT,(i+2)*UNIT,fill='bisque4')
# create human being
self.human1 = self.canvas.create_rectangle(5*UNIT, 5*UNIT, 6*UNIT, 6*UNIT, fill='green')
self.human2 = self.canvas.create_rectangle(13*UNIT, 0, 14*UNIT, 1*UNIT, fill='green')
# create targets
self.target1 = self.canvas.create_rectangle(
19*UNIT,10*UNIT,20*UNIT,11*UNIT,
fill='light salmon')
self.target2 = self.canvas.create_rectangle(
2*UNIT,10*UNIT,3*UNIT,11*UNIT,
fill='tomato')
self.target3 = self.canvas.create_rectangle(
8*UNIT,10*UNIT,9*UNIT,11*UNIT,
fill='orangered')
# define starting points
self.org1 = self.canvas.create_rectangle(
origin1[0] - 10, origin1[1] - 10,
origin1[0] + 10, origin1[1] + 10)
self.org2 = self.canvas.create_rectangle(
origin2[0] - 10, origin2[1] - 10,
origin2[0] + 10, origin2[1] + 10)
self.org3 = self.canvas.create_rectangle(
origin3[0] - 10, origin3[1] - 10,
origin3[0] + 10, origin3[1] + 10)
#create robot1
self.rect1 = self.canvas.create_rectangle(
origin1[0] - 10, origin1[1] - 10,
origin1[0] + 10, origin1[1] + 10,
fill='SkyBlue1')
#create robot2
self.rect2 = self.canvas.create_rectangle(
origin2[0] - 10, origin2[1] - 10,
origin2[0] + 10, origin2[1] + 10,
fill='SteelBlue2')
#create robot3
self.rect3 = self.canvas.create_rectangle(
origin3[0] - 10, origin3[1] - 10,
origin3[0] + 10, origin3[1] + 10,
fill='RoyalBlue1' )
# pack all
self.canvas.pack()
def resetRobot(self):
self.update()
time.sleep(0.01)
self.canvas.delete(self.rect1)
self.canvas.delete(self.rect2)
self.canvas.delete(self.rect3)
self.rect1 = self.canvas.create_rectangle(
origin1[0] - 10, origin1[1] - 10,
origin1[0] + 10, origin1[1] + 10,
fill='SkyBlue1')
self.rect2 = self.canvas.create_rectangle(
origin2[0] - 10, origin2[1] - 10,
origin2[0] + 10, origin2[1] + 10,
fill='SteelBlue2')
self.rect3 = self.canvas.create_rectangle(
origin3[0] - 10, origin3[1] - 10,
origin3[0] + 10, origin3[1] + 10,
fill='RoyalBlue1' )
return self.canvas.coords(self.rect1), self.canvas.coords(self.rect2), self.canvas.coords(self.rect3)
def resetHuman(self):
self.update()
time.sleep(0.2)
self.canvas.delete(self.human1)
self.canvas.delete(self.human2)
self.human1 = self.canvas.create_rectangle(5*UNIT, 5*UNIT, 6*UNIT, 6*UNIT, fill='green')
self.human2 = self.canvas.create_rectangle(13*UNIT, 0, 14*UNIT, 1*UNIT, fill='green')
return self.canvas.coords(self.human1), self.canvas.coords(self.human2)
def humanStep1(self, action):
s = self.canvas.coords(self.human1)
base_action = moveAgent(s, action)
self.canvas.move(self.human1, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.human1) # next state
return s_
def humanStep2(self, action):
s = self.canvas.coords(self.human2)
base_action = moveAgent(s, action)
self.canvas.move(self.human2, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.human2) # next state
return s_
def returnStep1(self, action):
s = self.canvas.coords(self.rect1)
base_action = moveAgent(s, action)
self.canvas.move(self.rect1, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.rect1) # next state
# reward function
if s_ == self.canvas.coords(self.org1):
reward = 50
done = 'arrive'
s_ = 'terminal'
elif s_ == self.canvas.coords(self.target2) or s_ == self.canvas.coords(self.target3):
reward = -50
done = 'hit'
s_ = 'terminal'
elif s_ == self.canvas.coords(self.org2) or s_ == self.canvas.coords(self.org3):
reward = -50
done = 'hit'
s_ = 'terminal'
elif s_[0] == 0 or s_[1] < 40 or s_[2] >= MAZE_H * UNIT or s_[3] >= MAZE_W * UNIT:
reward = -50
done = 'hit'
s_ = 'terminal'
elif int(s_[0]) in X_Block and int(s_[1]) in Y_Block:
reward = -50
done = 'hit'
s_ = 'terminal'
else:
reward = 0
done = 'nothing'
return s_, reward, done
def returnStep2(self, action):
s = self.canvas.coords(self.rect2)
base_action = moveAgent(s, action)
self.canvas.move(self.rect2, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.rect2) # next state
# reward function
if s_ == self.canvas.coords(self.org2):
reward = 50
done = 'arrive'
s_ = 'terminal'
elif s_ == self.canvas.coords(self.target1) or s_ == self.canvas.coords(self.target3):
reward = -50
done = 'hit'
s_ = 'terminal'
elif s_ == self.canvas.coords(self.org1) or s_ == self.canvas.coords(self.org3):
reward = -50
done = 'hit'
s_ = 'terminal'
elif s_[0] == 0 or s_[1] < 40 or s_[2] >= MAZE_H * UNIT or s_[3] >= MAZE_W * UNIT:
reward = -50
done = 'hit'
s_ = 'terminal'
elif int(s_[0]) in X_Block and int(s_[1]) in Y_Block:
reward = -50
done = 'hit'
s_ = 'terminal'
else:
reward = 0
done = 'nothing'
return s_, reward, done
def returnStep3(self, action):
s = self.canvas.coords(self.rect3)
base_action = moveAgent(s, action)
self.canvas.move(self.rect3, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.rect3) # next state
# reward function
if s_ == self.canvas.coords(self.org3):
reward = 50
done = 'arrive'
s_ = 'terminal'
elif s_ == self.canvas.coords(self.target1) or s_ == self.canvas.coords(self.target2):
reward = -50
done = 'hit'
s_ = 'terminal'
elif s_ == self.canvas.coords(self.org1) or s_ == self.canvas.coords(self.org2):
reward = -50
done = 'hit'
s_ = 'terminal'
elif s_[0] == 0 or s_[1] < 40 or s_[2] >= MAZE_H * UNIT or s_[3] >= MAZE_W * UNIT:
reward = -50
done = 'hit'
s_ = 'terminal'
elif int(s_[0]) in X_Block and int(s_[1]) in Y_Block:
reward = -50
done = 'hit'
s_ = 'terminal'
else:
reward = 0
done = 'nothing'
return s_, reward, done
def step1(self, action, obstacle=None):
s = self.canvas.coords(self.rect1)
base_action = moveAgent(s, action)
self.canvas.move(self.rect1, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.rect1) # next state
# reward function
if s_ == self.canvas.coords(self.target1):
reward = 50
done = 'arrive'
s_ = 'terminal'
elif s_ == self.canvas.coords(self.target2) or s_ == self.canvas.coords(self.target3):
reward = -50
done = 'hit'
s_ = 'terminal'
elif s_[0] == 0 or s_[1] < 40 or s_[2] >= MAZE_H * UNIT or s_[3] >= MAZE_W * UNIT:
reward = -50
done = 'hit'
s_ = 'terminal'
elif int(s_[0]) in X_Block and int(s_[1]) in Y_Block:
reward = -50
done = 'hit'
s_ = 'terminal'
else:
reward = 0
done = 'nothing'
if obstacle != None:
if s_ == obstacle:
reward = -50
done = 'hit'
s_ = 'terminal'
return s_, reward, done
def step2(self, action, obstacle=None):
s = self.canvas.coords(self.rect2)
base_action = moveAgent(s, action)
self.canvas.move(self.rect2, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.rect2) # next state
# reward function
if s_ == self.canvas.coords(self.target2):
reward = 50
done = 'arrive'
s_ = 'terminal'
elif s_ == self.canvas.coords(self.target1) or s_ == self.canvas.coords(self.target3):
reward = -50
done = 'hit'
s_ = 'terminal'
elif s_[0] == 0 or int(s_[1]) < 40 or s_[2] >= MAZE_H * UNIT or s_[3] >= MAZE_W * UNIT:
reward = -50
done = 'hit'
s_ = 'terminal'
elif int(s_[0]) in X_Block and int(s_[1]) in Y_Block:
reward = -50
done = 'hit'
s_ = 'terminal'
else:
reward = 0
done = 'nothing'
if obstacle != None:
if s_ == obstacle:
reward = -50
done = 'hit'
s_ = 'terminal'
return s_, reward, done
def step3(self, action, obstacle=None):
s = self.canvas.coords(self.rect3)
base_action = moveAgent(s, action)
self.canvas.move(self.rect3, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.rect3) # next state
# reward function
if s_ == self.canvas.coords(self.target3):
reward = 50
done = 'arrive'
s_ = 'terminal'
elif s_ == self.canvas.coords(self.target1) or s_ == self.canvas.coords(self.target2):
reward = -50
done = 'hit'
s_ = 'terminal'
elif s_[0] == 0 or s_[1] < 40 or s_[2] >= MAZE_H * UNIT or s_[3] >= MAZE_W * UNIT:
reward = -50
done = 'hit'
s_ = 'terminal'
elif int(s_[0]) in X_Block and int(s_[1]) in Y_Block:
reward = -50
done = 'hit'
s_ = 'terminal'
else:
reward = 0
done = 'nothing'
if obstacle != None:
if s_ == obstacle:
reward = -50
done = 'hit'
s_ = 'terminal'
return s_, reward, done
def render(self):
time.sleep(0.01)
self.update()
def moveAgent(s, action):
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
elif action == 4: # wait
base_action = np.array([0, 0])
return base_action
def update():
for t in range(10):
s1, s2 = env.resetRobot()
while True:
env.render()
s1,r1, done1 = env.step1(2)
s2,r2,done2 = env.step2(1)
if done1 == 'hit' and done2 == 'hit':
break
elif done1 == 'hit' and done2 == 'nothing':
break
elif done1 == 'arrive' and done2 == 'arrive':
break
elif s1 == s2:
break
if __name__ == '__main__':
env = Maze()
env.after(2000, update)
env.mainloop()