-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaze.py
707 lines (569 loc) · 19.8 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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
import sys
from os.path import dirname, join, realpath
dir_path = dirname(dirname(realpath(__file__)))
sys.path.insert(1, join(dir_path, 'utils'))
from typing import Tuple, List
from abc import ABC, abstractmethod
import numpy as np
import matplotlib.pyplot as plt
from tqdm import trange
import time
import heapq
import itertools
from env import GridWorld
class PriorityQueue:
'''
This class is taken and modified from
https://docs.python.org/3/library/heapq.html#priority-queue-implementation-notes
'''
def __init__(self) -> None:
self.pqueue = []
self.entry_finder = {}
self.REMOVED = '<removed-item>'
self.counter = itertools.count()
def push(self, item: object, priority: float=0) -> None:
if item in self.entry_finder:
self.remove(item)
count = next(self.counter)
entry = [priority, count, item]
self.entry_finder[item] = entry
heapq.heappush(self.pqueue, entry)
def remove(self, item: object) -> None:
entry = self.entry_finder.pop(item)
entry[-1] = self.REMOVED
def pop(self) -> object:
while self.pqueue:
priority, count, item = heapq.heappop(self.pqueue)
if item is not self.REMOVED:
del self.entry_finder[item]
return item
raise KeyError('pop from an empty priority queue')
def is_empty(self) -> bool:
return not self.entry_finder
class DynaAgent(ABC):
'''
Dyna agent abstract class
'''
def __init__(self, env: GridWorld,
epsilon: float,
alpha: float,
gamma: float,
planning_step: int) -> None:
'''
env: GridWorld env
epsilon: exploration param
alpha: step size param
gamma: discount factor
planning_step: number of planning steps
'''
self.env = env
self.epsilon = epsilon
self.alpha = alpha
self.gamma = gamma
self.planning_step = planning_step
self.value_function = np.zeros((env.height,
env.width, len(env.action_space)))
self.model = dict()
@abstractmethod
def __call__(self, env: GridWorld,
epsilon: float,
alpha: float,
gamma: float,
planning_step: int) -> object:
pass
def reset(self):
return self.env.reset()
def _epsilon_greedy(self, state: np.ndarray) -> int:
'''
Choose action according to epsilon-greedy
Params
------
state: state of the agent
Return
------
action: chosen action
'''
if np.random.binomial(1, self.epsilon) == 1:
action = np.random.choice(self.env.action_space)
else:
max_value = self.value_function[state[0], state[1], :].max()
action = np.random.choice(np.flatnonzero(
self.value_function[state[0], state[1], :] == max_value))
return action
def _update_Q(self, state: np.ndarray,
action: int, next_state: np.ndarray,
reward: float) -> None:
'''
Update state-action value function
Params
------
state: state of the agent
action: action taken at @state
next_state: next state according to @state
reward: reward taken at @next_state
'''
target = reward + self.gamma * np.max(
self.value_function[next_state[0], next_state[1], :])
error = target - self.value_function[state[0], state[1], action]
self.value_function[state[0], state[1], action] += self.alpha * error
@abstractmethod
def _model_learning(self) -> None:
pass
@abstractmethod
def _search_control(self) -> None:
pass
@abstractmethod
def run(self) -> None:
pass
class DynaQ(DynaAgent):
'''
Dyna-Q agent
'''
def __init__(self, env: GridWorld,
epsilon: float,
alpha: float,
gamma: float,
planning_step: int) -> None:
'''
env: GridWorld env
epsilon: exploration param
alpha: step size param
gamma: discount factor
planning_step:
'''
super().__init__(env, epsilon, alpha, gamma, planning_step)
def __call__(self, env: GridWorld,
epsilon: float,
alpha: float,
gamma: float,
planning_step: int) -> object:
return DynaQ(env, epsilon, alpha, gamma, planning_step)
def _model_learning(self, state: np.ndarray,
action: int, next_state: np.ndarray,
reward: float) -> None:
'''
Model learning
Params
------
state: state of the agent
action: action taken at @state
next_state: next state according to @state
reward: reward taken at @next_state
'''
state_ = (state[0], state[1])
if state_ not in self.model.keys():
self.model[state_] = dict()
self.model[state_][action] = next_state, reward
def _search_control(self) -> Tuple[Tuple[int, int], int, np.ndarray, float]:
'''
Search control
Return
------
'''
states = list(self.model.keys())
state = states[np.random.choice(len(states))]
actions = list(self.model[state].keys())
action = actions[np.random.choice(len(actions))]
next_state, reward = self.model[state][action]
return state, action, next_state, reward
def run(self) -> int:
'''
Perform an episode
Return
------
n_steps: number of steps of the episode
'''
state = self.reset()
n_steps = 0
while True:
action = self._epsilon_greedy(state)
next_state, reward, terminated = self.env.step(action)
self._update_Q(state, action, next_state, reward)
self._model_learning(state, action, next_state, reward)
for _ in range(self.planning_step):
state_, action_, next_state_, reward_ = self._search_control()
self._update_Q(state_, action_, next_state_, reward_)
state = next_state
n_steps += 1
if terminated:
break
return n_steps
class DynaQPlus(DynaQ):
'''
Dyna-Q+ agent
'''
def __init__(self, env: GridWorld,
epsilon: float,
alpha: float,
gamma: float,
planning_step: int,
kappa: float) -> None:
'''
env: GridWorld env
epsilon: exploration param
alpha: step size param
gamma: discount factor
planning_step: number of planning steps
kappa: exploration bonus param
'''
super().__init__(env, epsilon, alpha, gamma, planning_step)
self.kappa = kappa
self.current_step = 0
def __call__(self, env: GridWorld,
epsilon: float,
alpha: float,
gamma: float,
planning_step: int,
kappa: float) -> object:
return DynaQPlus(env, epsilon, alpha, gamma, planning_step, kappa)
def _model_learning(self, state: np.ndarray,
action: int, next_state: np.ndarray,
reward: float) -> None:
'''
Model learning
Params
------
state: state of the agent
action: action taken at @state
next_state: next state according to @state
reward: reward taken at @next_state
'''
self.current_step += 1
state_ = (state[0], state[1])
if state_ not in self.model.keys():
self.model[state_] = dict()
for action_ in self.env.action_space:
if action_ != action:
# lead back to the same state with a reward of zero
self.model[state_][action_] = state, 0, 1
self.model[state_][action] = next_state, reward, self.current_step
def _search_control(self) -> Tuple[Tuple[int, int], int, np.ndarray, float]:
'''
Search control
Return
------
'''
states = list(self.model.keys())
state = states[np.random.choice(len(states))]
actions = list(self.model[state].keys())
action = actions[np.random.choice(len(actions))]
next_state, reward, last_tried = self.model[state][action]
reward += self.kappa * np.sqrt(self.current_step - last_tried)
return state, action, next_state, reward
class PrioritizedSweeping(DynaQ):
'''
Prioritized sweeping agent
'''
def __init__(self, env: GridWorld,
epsilon: float,
alpha: float,
gamma: float,
planning_step: int,
theta: float) -> None:
'''
env: GridWorld env
epsilon: exploration param
alpha: step size param
gamma: discount factor
planning_step: number of planning steps
theta:
'''
super().__init__(env, epsilon, alpha,
gamma, planning_step)
self.theta = theta
self.pqueue = PriorityQueue()
self.predecessor_pairs = dict()
def __call__(self, env: GridWorld,
epsilon: float,
alpha: float,
gamma: float,
planning_step: int,
theta: float) -> object:
return PrioritizedSweeping(env, epsilon, alpha,
gamma, planning_step, theta)
def _model_learning(self, state: np.ndarray,
action: int, next_state: np.ndarray,
reward: float) -> None:
'''
Model learning
Params
------
state: state of the agent
action: action taken at @state
next_state: next state according to @state
reward: reward taken at @next_state
'''
super()._model_learning(state, action, next_state, reward)
next_state_ = (next_state[0], next_state[1])
if next_state_ not in self.predecessor_pairs.keys():
self.predecessor_pairs[next_state_] = []
self.predecessor_pairs[next_state_].append((state, action))
def _search_control(self) -> Tuple[Tuple[int, int], int, np.ndarray, float]:
state, action = self.pqueue.pop()
next_state, reward = self.model[state][action]
return state, action, next_state, reward
# return state, action, reward predicted to lead to a state
def _get_predecessor_pairs(self, state: np.ndarray):
'''
'''
predecessors_ = []
state_ = (state[0], state[1])
if state_ in self.predecessor_pairs.keys():
for pre_state, pre_action in self.predecessor_pairs[state_]:
pre_state_ = (pre_state[0], pre_state[1])
predecessors_.append([pre_state, pre_action,
self.model[pre_state_][pre_action][1]])
return predecessors_
def _cal_priority(self, state: np.ndarray,
action: int,
next_state: np.ndarray,
reward: float) -> float:
'''
Compute the priority
Params
------
state: state of the agent
action: action taken at @state
next_state: next state according to @state
reward: reward taken at @next_state
'''
return np.abs(reward + self.gamma * np.max(self.value_function[next_state[0], next_state[1], :])
- self.value_function[state[0], state[1], action])
def run(self) -> int:
'''
Perform an episode
Return
------
n_updates: number of updates of the episode
'''
state = self.reset()
n_steps = 0
n_updates = 0
while True:
action = self._epsilon_greedy(state)
next_state, reward, terminated = self.env.step(action)
self._model_learning(state, action, next_state, reward)
priority = self._cal_priority(state, action, next_state, reward)
if priority > self.theta:
# since the heap in heapq is a min-heap
state_ = (state[0], state[1])
self.pqueue.push((state_, action), -priority)
planning_step_count = 0
while planning_step_count < self.planning_step and not self.pqueue.is_empty():
state_, action_, next_state_, reward_ = self._search_control()
self._update_Q(state_, action_, next_state_, reward_)
for pre_state_, pre_action_, pre_reward_ in self._get_predecessor_pairs(state_):
priority_ = self._cal_priority(pre_state_, pre_action_, state_, pre_reward_)
if priority_ > self.theta:
pre_state__ = (pre_state_[0], pre_state_[1])
self.pqueue.push((pre_state__, pre_action_), -priority_)
planning_step_count += 1
state = next_state
n_steps += 1
n_updates += planning_step_count + 1
if terminated:
break
return n_updates
def dyna_maze():
height = 6
width = 9
start_state = (2, 0)
terminal_states = [(0, 8)]
obstacles = [(0, 7), (1, 7), (2, 7), (1, 2), (2, 2), (3, 2), (4, 5)]
maze = GridWorld(height, width, start_state,
terminal_states, obstacles=obstacles)
n_runs = 30
n_episodes = 50
alpha = 0.1
epsilon = 0.1
gamma = 0.95
planning_steps = [0, 5, 50]
n_steps = np.zeros((len(planning_steps), n_episodes))
for _ in trange(n_runs):
for i, planning_step in enumerate(planning_steps):
agent = DynaQ(maze, epsilon, alpha, gamma, planning_step)
for ep in range(n_episodes):
n_steps[i, ep] += agent.run()
n_steps /= n_runs
for i in range(len(planning_steps)):
plt.plot(n_steps[i, :], label=f'{planning_steps[i]} planning steps')
plt.xlabel('Episodes')
plt.ylabel('Steps per episode')
plt.legend()
plt.savefig('./dyna_maze.png')
plt.close()
def blocking_maze():
height = 6
width = 9
start_state = (5, 3)
terminal_states = [(0, 8)]
obstacles = [(3, i) for i in range(8)]
new_obstacles = [(3, i) for i in range(1, 9)]
n_runs = 20
alpha = 1
epsilon = 0.1
gamma = 0.95
planning_step = 10
obstacles_change_step = 1000
max_step = 3000
kappa = 1e-4
maze = GridWorld(height, width, start_state, terminal_states)
methods = [
{
'name' :'Dyna-Q',
'agent': DynaQ,
'params': [maze, epsilon, alpha, gamma, planning_step]
},
{
'name': 'Dyna-Q+',
'agent': DynaQPlus,
'params': [maze, epsilon, alpha, gamma, planning_step, kappa]
}
]
rewards = np.zeros((n_runs, len(methods), max_step))
for i, method in enumerate(methods):
print(method['name'])
for run in trange(n_runs):
maze.set_obstacles(obstacles)
agent = method['agent'](*method['params'])
step = 0
last_step = 0
while step < max_step:
step += agent.run()
rewards[run, i, last_step: step] = rewards[run, i, last_step]
rewards[run, i, min(step, max_step - 1)] = rewards[run, i, last_step] + 1
last_step = step
if step > obstacles_change_step:
maze.set_obstacles(new_obstacles)
time.sleep(0.1)
rewards = np.mean(rewards, axis=0)
for i, method in enumerate(methods):
plt.plot(rewards[i, :], label=method['name'])
plt.xlabel('Time steps')
plt.ylabel('Cumulative reward')
plt.legend()
plt.savefig('./blocking_maze.png')
plt.close()
def shortcut_maze():
height = 6
width = 9
start_state = (5, 3)
terminal_states = [(0, 8)]
obstacles = [(3, i) for i in range(1, 9)]
new_obstacles = [(3, i) for i in range(1, 8)]
n_runs = 5
alpha = 1
epsilon = 0.1
gamma = 0.95
planning_step = 50
obstacles_change_step = 3000
max_step = 6000
kappa = 1e-3
maze = GridWorld(height, width, start_state, terminal_states)
methods = [
{
'name' :'Dyna-Q',
'agent': DynaQ,
'params': [maze, epsilon, alpha, gamma, planning_step]
},
{
'name': 'Dyna-Q+',
'agent': DynaQPlus,
'params': [maze, epsilon, alpha, gamma, planning_step, kappa]
}
]
rewards = np.zeros((n_runs, len(methods), max_step))
for i, method in enumerate(methods):
print(method['name'])
for run in trange(n_runs):
maze.set_obstacles(obstacles)
agent = method['agent'](*method['params'])
step = 0
last_step = 0
while step < max_step:
step += agent.run()
rewards[run, i, last_step: step] = rewards[run, i, last_step]
rewards[run, i, min(step, max_step - 1)] = \
rewards[run, i, last_step] + 1
last_step = step
if step > obstacles_change_step:
maze.set_obstacles(new_obstacles)
time.sleep(0.1)
rewards = np.mean(rewards, axis=0)
for i, method in enumerate(methods):
plt.plot(rewards[i, :], label=method['name'])
plt.xlabel('Time steps')
plt.ylabel('Cumulative reward')
plt.legend()
plt.savefig('./shortcut_maze.png')
plt.close()
def is_optimal_solution(agent: DynaAgent, resolution: int) -> bool:
max_steps = 14 * resolution * 1.2
state = agent.reset()
n_steps = 0
while True:
action = np.argmax(agent.value_function[state[0], state[1], :])
state, _, terminated = agent.env.step(action)
n_steps += 1
if terminated:
break
if n_steps > max_steps:
return False
return True
def mazes():
height = 6
width = 9
start_state = (2, 0)
terminal_states = [(0, 8)]
obstacles = [(0, 7), (1, 7), (2, 7), (1, 2), (2, 2), (3, 2), (4, 5)]
maze = GridWorld(height, width, start_state,
terminal_states, obstacles=obstacles)
resolutions = range(1, 6)
n_runs = 5
alpha = 0.5
epsilon = 0.1
gamma = 0.95
planning_step = 5
theta = 0.0001
methods = [
{
'name': 'Dyna-Q',
'agent': DynaQ,
'params': [epsilon, alpha, gamma, planning_step]
},
{
'name': 'Prioritized Sweeping',
'agent': PrioritizedSweeping,
'params': [epsilon, alpha, gamma, planning_step, theta]
}
]
updates = np.zeros((n_runs, len(methods), len(resolutions)))
for i, method in enumerate(methods):
print(method['name'])
for run in range(n_runs):
for res in resolutions:
maze_ = maze.extend(res)
agent = method['agent'](maze_, *method['params'])
print(f'run = {run}, maze size = {maze_.height * maze_.width}')
steps = []
while True:
steps.append(agent.run())
if is_optimal_solution(agent, res):
break
updates[run, i, res - 1] = np.sum(steps)
time.sleep(0.1)
updates = np.mean(updates, axis=0)
updates[0, :] *= planning_step + 1
for i, method in enumerate(methods):
plt.plot(np.arange(1, len(resolutions) + 1), updates[i, :], label=method['name'])
plt.xlabel('maze resolution factor')
plt.ylabel('updates until optimal solution')
plt.yscale('log')
plt.legend()
plt.savefig('./prioritized_sweeping.png')
plt.close()
if __name__ == '__main__':
dyna_maze()
blocking_maze()
shortcut_maze()
mazes()