-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridWorld.py
504 lines (404 loc) · 16.2 KB
/
GridWorld.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import numpy as np
from curriculum import curriculum
from stack import Stack
from new_curriculum import new_curriculum
class GridWorld:
def __init__(self, row, col, kl, kp, kd, kg, num_agents):
self.row = row
self.col = col
self.kl = kl
self.kp = kp
self.kd = kd
self.kg = kg
self.kgback = kg
self.num_agents = num_agents
self.actions = np.array([0, 1, 2, 3, 4])
self.num_flags = 3
self.flag_dynamic = 16
self.floors = 2
self.stage = 3
self.state, self.action, self.reward, self.state_, self.done = (0, 0, 0, 0, 0)
self.elevator = [74, 78, 83, 87]
self.agents = dict()
self.stack_states = Stack()
for agent in range(num_agents):
self.agents[agent] = [] # lista com state, action, reward, next_state, done
def set_obstacles(self, obstacles: np.array):
self.obstacles = obstacles
def set_pick_up(self, pick_up : np.array):
self.pick_up = pick_up
def set_drop_off(self, drop_off : np.array):
self.drop_off = drop_off
def set_stage(self, stage : int):
self.stage = stage
def set_progressive_curriculum(self, value:int):
self.progressive = value
def possible_states(self):
all_grid_position = self.row * self.col * self.floors # floors é o numero de andares
all_pick_up = len(self.pick_up)
all_drop_off = len(self.drop_off)
all_flag = self.num_flags
all_agents = self.num_agents
flag_dynamic = self.flag_dynamic
self.all_states = np.arange(all_grid_position**all_agents * all_pick_up\
* all_drop_off * all_flag * flag_dynamic)
shape = [flag_dynamic, all_flag, all_drop_off, all_pick_up, all_grid_position]
for i in range(1, all_agents):
shape.insert(0, all_grid_position)
shape = tuple(shape)
self.all_states = self.all_states.reshape(shape)
self.initial_states()
self.crr = curriculum(self.all_states, self.obstacles, self.elevator, self.col, self.row)
self.ncrr = new_curriculum(self.all_states, self.obstacles, self.elevator, self.col, self.row, self.pick_up, self.drop_off)
def initial_states(self):
shape = self.all_states.shape
n_axis = len(shape)
states = np.copy(self.all_states)
if n_axis > 5:
del_axis = [i for i in range(n_axis) if n_axis-i > 5]
del_axis.append(n_axis - 1)
del_axis = tuple(del_axis)
del_pick_up = tuple([i for i in range(shape[-1]) if i not in self.pick_up])
states = np.delete(states, del_pick_up, axis=n_axis - 1)
states = np.delete(states, (1, 2), axis=n_axis - 4)
self.start_state = states.flatten()
def reset(self):
#if len(self.stack_states) == 0:
# for item in self.ncrr.get_stage(self.stage)[self.progressive]:
# self.stack_states.push(item)
if self.stage >= 3:
self.state = np.random.choice(self.ncrr.get_stage(3))
else:
self.state = np.random.choice(self.ncrr.get_stage(self.stage)[self.progressive]) # falta arrumar esse
self.current_dynamic, self.current_flag, self.current_drop_off, self.current_pick_up, \
self.grid_position = np.array(np.where(self.state == self.all_states)).squeeze()
return self.state
# Antigo Curriculum
#if self.stage == 5:
# self.state = np.random.choice(self.crr.get_stage(0))
#else:
# self.state = np.random.choice(self.crr.get_stage(self.stage))
#self.current_dynamic, self.current_flag, self.current_drop_off, self.current_pick_up, \
#self.grid_position = np.array(np.where(self.state == self.all_states)).squeeze()
#return self.state
def move(self, action):
grid_position_ = self.grid_position
if action == 0: #baixo
grid_position_ += self.col
elif action == 1: # cima
grid_position_ -= self.col
elif action == 2 and grid_position_ % self.col != self.col - 1: #direita
grid_position_ += 1
elif action == 3 and grid_position_ % self.col != 0: # esquerda
grid_position_ -= 1
elif action == 4: # parado
pass
if not self.on_map(grid_position_):
return self.grid_position
if self.on_obstacle(grid_position_):
return self.grid_position
return grid_position_
def on_map(self, grid_position):
if grid_position < 0 or grid_position >= self.row * self.col * self.floors:
return False
return True
def on_obstacle(self, grid_position):
if grid_position in self.obstacles:
return True
return False
def on_goal(self, grid_position):
if self.stage == 0:
if self.current_flag == 1 or \
(self.current_flag == 0 and grid_position == self.pick_up[self.current_pick_up]):
self.kg = self.kp
return True
return False
#if self.stage == 1:
# return self.on_elevator2(grid_position)
# if self.stage == 1:
# if self.current_flag == 2 or grid_position == self.drop_off[self.current_drop_off]:
# self.kg = self.kd
# return True
# return False
if self.current_flag == 2 and grid_position in self.pick_up:
self.kg = self.kgback
return True
return False
# Curriculum antigo
#if self.stage == 0:
# if self.current_flag == 1 or \
# self.current_flag == 0 and grid_position == self.pick_up[self.current_pick_up] :
# self.kg = self.kp
# return True
# return False
#
# elif self.stage == 1:
# if grid_position in self.elevator[0:2]:
# self.kg = self.kp
# return True
# return False
#
# elif self.stage == 2:
# if self.current_flag == 2:# and grid_position == self.drop_off[self.current_drop_off]:
# self.kg = self.kd
# return True
# return False
#
# elif self.stage == 3:
# if self.current_flag == 2 and grid_position in self.elevator[2:]:
# self.kg = self.kd
# return True
# return False
#
# if self.current_flag == 2 and grid_position in self.pick_up:
# self.kg = self.kgback
# return True
# return False
def on_elevator2(self, grid_position):
if grid_position in self.elevator[2:]:
return True
return False
def on_drop_off(self, grid_position):
if self.current_flag == 1 and grid_position == self.drop_off[self.current_drop_off]:
return True
return False
def on_pick_up(self, grid_position):
if self.current_flag == 0 and grid_position == self.pick_up[self.current_pick_up]:
return True
return False
def on_done(self, grid_position):
if self.on_goal(grid_position):
return True
#if self.on_dynamic(self.action):
# return True
return False
def on_elevator(self, grid_position):
if grid_position in self.elevator:
return True
return False
def att_flag(self, grid_position):
if self.current_flag == 0 and grid_position == self.pick_up[self.current_pick_up]:
self.current_flag = 1
if self.current_flag == 1 and grid_position == self.drop_off[self.current_drop_off]:
self.current_flag = 2
def att_dynamic(self, state):
if state >= self.col * self.row * 2:
state = self.what_position(state)
self.current_dynamic = np.random.choice(self.state_dynamic_flag[state])
return self.att_state(self.grid_position_)
def step(self, action):
self.action = action
self.grid_position_ = self.move(action=action)
self.reward = self.get_reward(self.grid_position, self.grid_position_)
self.done = self.on_done(self.grid_position_)
self.att_flag(self.grid_position_)
self.state_ = self.att_state(self.grid_position_)
self.grid_position = self.grid_position_
self.state = self.state_
return self.state_, self.reward, self.done
def action_space(self):
return self.actions
def state_space(self):
return self.all_states.flatten()
def att_state(self, grid_position):
return self.all_states[self.current_dynamic, self.current_flag, \
self.current_drop_off, self.current_pick_up, grid_position]
def on_dynamic(self, action):
binary_flag_dynamic = self.decimal2binary(self.current_dynamic)
if action == 0 and binary_flag_dynamic[0] == '1':
return True
if action == 1 and binary_flag_dynamic[1] == '1':
return True
if action == 2 and binary_flag_dynamic[2] == '1':
return True
if action == 3 and binary_flag_dynamic[3] == '1':
return True
return False
def set_state(self, observation):
self.current_dynamic, self.current_flag, \
self.current_drop_off, self.current_pick_up, self.grid_position = self.get_states(observation)
def get_position(self):
return self.grid_position
def get_states(self, observation):
return np.array(np.where(observation == self.all_states)).squeeze()
def get_possibles_grid_positions(self):
return np.delete(np.arange(self.col * self.row * 2), self.obstacles)
def get_observation(self, states : tuple):
return self.all_states[states]
def what_position(self, state):
dynamic, flag, drop, pick, gp = np.array(np.where(state == self.all_states)).squeeze()
return gp
def get_reward(self, state, state_):
reward = self.kl
if self.on_dynamic(self.action):
reward -= self.kg
if self.current_flag == 2:
reward -= self.kg // 2
if self.on_drop_off(state_):
reward += self.kd
if self.on_pick_up(state_):
reward += self.kp
if self.on_goal(state_):
reward += self.kg
if self.action == 4:
reward += self.kg // 4
if self.action == 4:
reward -= 6
# if self.action == 2 and self.current_flag == 0:
# reward -= 1
# if self.action == 3 and self.current_flag == 2:
# reward -= 1
#if self.action == 0 and (self.current_flag == 2 or self.current_flag == 0):
# reward -= 1
if self.current_flag == 0 :
position_goal = self.pick_up[self.current_pick_up]
xg, yg = divmod(position_goal, self.row)
position_agent = self.what_position(state_)
xa, ya = divmod(position_agent, self.row)
distance = np.abs(xg - xa) + np.abs(yg - ya)
reward -= distance**2 / (2 * self.row + self.row - 2)**2
elif self.current_flag == 1:
position_goal = self.drop_off[self.current_drop_off]
xg, yg = divmod(position_goal, self.row)
position_agent = self.what_position(state_)
xa, ya = divmod(position_agent, self.row)
distance = np.abs(xg - xa) + np.abs(yg - ya)
reward -= distance**2 / (2 * self.row + self.row - 2)**2
else:
position_agent = self.what_position(state_)
min_dist = 100
for pick_up in self.pick_up:
position_goal = pick_up
xg, yg = divmod(position_goal, self.row)
position_agent = self.what_position(state_)
xa, ya = divmod(position_agent, self.row)
distance = np.abs(xg - xa) + np.abs(yg - ya)
if distance < min_dist:
min_dist = distance
distance = min_dist
#position_goal = self.pick_up[2]
#xg, yg = divmod(position_goal, self.row)
#position_agent = self.what_position(state_)
#xa, ya = divmod(position_agent, self.row)
#distance = np.abs(xg - xa) + np.abs(yg - ya)
reward -= distance**2 / (2 * self.row + self.row - 2)**2
#if self.action == 1 and self.current_flag == 1:
# reward -= 2
return reward
def decimal2binary(self, decimal):
binary = bin(decimal).replace("0b", "")
while len(binary) < 4:
binary = '0' + binary
return binary
def binary2decimal(self, binary):
return int(binary, 2)
def load_available_flag_dynamic(self):
self.state_dynamic_flag = dict()
states = np.delete(self.all_states, self.obstacles, axis=4)
for state in states.flatten():
possible_actions = self.available_action(state)
possible_actions = possible_actions[:-1]
aux_possibles = []
for action in self.actions[:-1]:
if action in possible_actions:
aux_possibles.append(2)
else:
aux_possibles.append(1)
flags_dynamic = []
for down in range(aux_possibles[0]):
for up in range(aux_possibles[1]):
for right in range(aux_possibles[2]):
for left in range(aux_possibles[3]):
decimal = self.binary2decimal(str(down) + str(up) + str(right) + str(left))
flags_dynamic.append(decimal)
self.state_dynamic_flag[state] = np.array(flags_dynamic)
def load_available_action(self):
self.state_action = dict()
states = np.delete(self.all_states, self.obstacles, axis=4)
for state in states.flatten():
data = np.array(np.where(state == self.all_states)).squeeze()
grid_position = data[-1]
aux = []
if (grid_position < self.col*self.row):
if (((grid_position + self.col) < self.col*self.row) or grid_position == 74 or grid_position == 78) and \
(grid_position + self.col) not in self.obstacles:
aux.append(0)
if ((grid_position - self.col) >= 0) and \
(grid_position - self.col) not in self.obstacles:
aux.append(1)
else:
if ((grid_position + self.col) < self.col*self.row*self.floors) and \
(grid_position + self.col) not in self.obstacles:
aux.append(0)
if (((grid_position - self.col) >= self.row*self.col) or grid_position == 83 or grid_position == 87) and \
(grid_position - self.col) not in self.obstacles:
aux.append(1)
if ((grid_position % self.col != self.col - 1)) and \
(grid_position + 1) not in self.obstacles:
aux.append(2)
if ((grid_position % self.col != 0)) and \
(grid_position - 1) not in self.obstacles:
aux.append(3)
aux.append(4)
self.state_action[state] = np.array(aux)
def available_action(self, state):
state = self.what_position(state)
return self.state_action[state]
def available_action2(self, state):
state = self.what_position(state)
return self.state_action[state]
def load_available_flag_dynamic2(self):
self.state_dynamic_flag = dict()
states = np.arange(self.col * self.row * 2)
for state in states.flatten():
possible_actions = self.available_action2(state)
possible_actions = possible_actions[:-1]
aux_possibles = []
for action in self.actions[:-1]:
if action in possible_actions:
aux_possibles.append(2)
else:
aux_possibles.append(1)
flags_dynamic = []
for down in range(aux_possibles[0]):
for up in range(aux_possibles[1]):
for right in range(aux_possibles[2]):
for left in range(aux_possibles[3]):
decimal = self.binary2decimal(str(down) + str(up) + str(right) + str(left))
flags_dynamic.append(decimal)
self.state_dynamic_flag[state] = np.array(flags_dynamic)
def load_available_action2(self):
self.state_action = dict()
states = np.arange(self.row * self.col * 2)
for state in states:
grid_position = state
aux = []
if (grid_position < self.col*self.row):
if (((grid_position + self.col) < self.col*self.row) or grid_position == 74 or grid_position == 78) and \
(grid_position + self.col) not in self.obstacles:
aux.append(0)
if ((grid_position - self.col) >= 0) and \
(grid_position - self.col) not in self.obstacles:
aux.append(1)
else:
if ((grid_position + self.col) < self.col*self.row*self.floors) and \
(grid_position + self.col) not in self.obstacles:
aux.append(0)
if (((grid_position - self.col) >= self.row*self.col) or grid_position == 83 or grid_position == 87) and \
(grid_position - self.col) not in self.obstacles:
aux.append(1)
if ((grid_position % self.col != self.col - 1)) and \
(grid_position + 1) not in self.obstacles:
aux.append(2)
if ((grid_position % self.col != 0)) and \
(grid_position - 1) not in self.obstacles:
aux.append(3)
aux.append(4)
self.state_action[state] = np.array(aux)
def generate_demand(self, n):
stack_books = Stack()
for i in range(n):
pick_up = np.random.choice(np.arange(len(self.pick_up)))
drop_off = np.random.choice(np.arange(len(self.drop_off)))
stack_books.push([drop_off, pick_up])
return stack_books