-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
414 lines (352 loc) · 14.9 KB
/
main.py
File metadata and controls
414 lines (352 loc) · 14.9 KB
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
# standard libraries
from time import time
# custom libraries
from classes import *
# third-party libraries
from tqdm import tqdm
import matplotlib.pyplot as plt
RUNS = 25 # 25, 50
EPISODES = 250 # 150, 250
MAX_RESOLUTION = 8
OPTIMAL_PATH = 78 # 78, 85
PRINT_INTERVAL = 4 # for printing Q-table
def figure_7(runs, episodes):
# instantiate maze
maze = Maze()
# modify maze to article specifcations
maze.ROWS = 24
maze.COLS = 36
maze.START = [8, 0]
maze.GOAL = []
for i in range(0, 4):
for j in range(32, 36):
maze.GOAL.append([i, j])
maze.OBSTACLES = []
for i in range(4, 16):
for j in range(8, 12):
maze.OBSTACLES.append([i, j])
for i in range(16, 20):
for j in range(20, 24):
maze.OBSTACLES.append([i, j])
for i in range(0, 12):
for j in range(28, 32):
maze.OBSTACLES.append([i, j])
# the size of q value
maze.q_size = (maze.ROWS, maze.COLS, len(maze.actions))
# instantiate parameters
params = DynaParams()
# set up models for planning
models = [Dyna, QueueDyna, QueueDyna]
methods = [dyna, dyna_lf, dyna_f]
method_names = ['Dyna', 'largest-first Dyna', 'focused Dyna']
# track the # of steps and backups
steps = np.zeros((len(method_names), episodes, runs))
backups = np.zeros((len(method_names), episodes, runs))
for run in tqdm(range(runs)):
for nameIndex in range(len(method_names)):
print('run %d, %s' % (run, method_names[nameIndex]))
# instantiate a Q-table
q_value = np.zeros(maze.q_size)
# instantiate model
model = models[nameIndex]()
# play for an episode
for ep in range(episodes):
if ep > 0:
ep_ = ep-1
else:
ep_ = 0
s, b = methods[nameIndex](
q_value, model, maze, params, maze.START)
steps[nameIndex, ep, run] += s
# print(str(steps))
backups[nameIndex, ep, run] += backups[nameIndex, ep_, run] + b
# print(str(backups))
print('steps: \n', steps[:, :, run])
print('backups: \n', backups[:, :, run])
# average the number of steps and backups for each episode
steps_mean = steps.mean(axis=2)
backups_mean = backups.mean(axis=2)
print('average steps: \n', steps_mean)
print('average backups: \n', backups_mean)
# compute the standard deviation over all runs
steps_std = steps.std(axis=2)
# backups_std = backups.std(axis=2)
print('steps std dev: \n', steps_std)
# print('backups std dev \n', backups_std)
# convert NumPy arrays of means and std devs into separate lists
mean_list = []
for methodIndex in range(len(method_names)):
mean_list.append(sorted(tuple(zip(backups_mean[methodIndex, :].tolist(), steps_mean[methodIndex, :].tolist())),
key=lambda tup: tup[0]))
std_list = steps_std[methodIndex, :].tolist()
# plot the three methods
for nameIndex in range(len(method_names)):
# backups, steps = zip(*mean_list[nameIndex])
# print('average over %i runs - backups: %s, steps: %s' %
# (RUNS, backups, steps))
# zip output
plt.plot(*zip(*mean_list[nameIndex]), label=method_names[nameIndex])
plt.xlabel('no. backups')
plt.ylabel('steps to goal')
plt.xscale('log')
plt.legend()
plt.title('Runs: %d, Episodes: %d' % (runs, episodes))
plt.savefig('images/figure_7.png')
# plt.show()
plt.close()
# plot the three methods with error bars
for nameIndex in range(len(method_names)):
backups, steps = zip(*mean_list[nameIndex])
# print('average over %i runs - backups: %s, steps: %s' %
# (RUNS, backups, steps))
# print('std dev over %i runs - steps: %s' % (RUNS, std_list))
# error bar output
plt.errorbar(backups, steps, yerr=std_list, fmt='o', markersize=4,
capsize=4, label=method_names[nameIndex])
plt.xlabel('no. backups')
plt.ylabel('steps to goal')
plt.xscale('log')
plt.legend()
plt.title('Runs: %d, Episodes: %d' % (runs, episodes))
plt.savefig('images/figure_7_error_bars.png')
# plt.show()
plt.close()
def figure_8(max_resolution, runs):
# get the original maze
original_maze = Maze()
# instantiate parameters
params = DynaParams()
# configure each of the maze resolutions
mazes = [original_maze.extend_maze(i)
for i in range(1, MAX_RESOLUTION + 1)]
# set up models for planning
models = [Dyna, QueueDyna, QueueDyna]
methods = [dyna,dyna_lf, dyna_f]
method_names = ['Dyna', 'largest-first Dyna', 'focused Dyna']
# track the # of backups
backups = np.zeros((len(method_names), MAX_RESOLUTION, RUNS))
for run in range(0, runs):
for nameIndex, method in enumerate(method_names):
for mazeIndex, maze in zip(range(len(mazes)), mazes):
print('run %d, %s, maze size %d' % (
run, method_names[nameIndex], maze.ROWS * maze.COLS))
# instantiate a Q-table
q_value = np.zeros(maze.q_size)
# instantiate model
model = models[nameIndex]()
# play for an episode
zero_counter = 0
while True: # try for 250 episodes
s, b = methods[nameIndex](
q_value, model, maze, params, maze.START)
if b == 0:
zero_counter += 1
if zero_counter >= 10:
print('A backup value of zero was returned 10 times. Run for %s has been stopped.' % (method))
break
backups[nameIndex, mazeIndex,
run] += b
print(str(backups))
# check whether the (relaxed) optimal path is found
if s <= 14 * maze.resolution * 1.5:
break
print('backups: \n', backups[:, :, run])
# compute the mean over all the runs
backups_mean = backups.mean(axis=2)
print('average backups: \n', backups_mean)
# compute the standard deviation over all runs
backups_std = backups.std(axis=2)
print('backups std dev: \n', backups_std)
# convert NumPy array of methods to seperate lists
mean_list = []
for methodIndex in range(len(method_names)):
mean_list.append(sorted(tuple(zip(
[i for i in range(1, MAX_RESOLUTION + 1)],
backups_mean[methodIndex, :].tolist())),
key=lambda tup: tup[0]))
std_list = backups_std[methodIndex, :].tolist()
# plot the three methods
for nameIndex in range(len(method_names)):
# resolution, backups = zip(*mean_list[nameIndex])
# print('average over %i runs - resolution: %s, backups: %s' % (RUNS, resolution, backups))
# print('std dev over %i runs - backups: %s' % (RUNS, std_list))
plt.plot(*zip(*mean_list[nameIndex]), label=method_names[nameIndex])
plt.xlabel('resolution')
plt.ylabel('no. backups until optimal solution')
plt.yscale('log')
plt.legend()
plt.title('Runs: %d, Episodes: %i Max. resolution: %i' %
(runs, EPISODES, MAX_RESOLUTION))
plt.savefig('images/figure_8.png')
# plt.show()
plt.close()
# plot the three methods with error bars
for nameIndex in range(len(method_names)):
resolution, backups = zip(*mean_list[nameIndex])
# print('average over %i runs - resolution: %s, backups: %s' % (RUNS, resolution, backups))
# print('std dev over %i runs - backups: %s' % (RUNS, std_list))
# error bar output
plt.errorbar(resolution, backups, yerr=std_list, fmt='o', markersize=4,
capsize=4, label=method_names[nameIndex])
plt.xlabel('resolution')
plt.ylabel('no. backups until optimal solution')
plt.yscale('log')
plt.legend()
plt.title('Runs: %d, Episodes: %i Max. resolution: %i' %
(runs, EPISODES, MAX_RESOLUTION))
plt.savefig('images/figure_8_error_bars.png')
# plt.show()
plt.close()
def figure_10(runs, episodes):
# instantiate maze
maze = Maze()
# modify maze to article specifications
maze.ROWS = 30
maze.COLS = 30
maze.START = [16, 0]
maze.GOAL = [[0, 28], [0, 29]]
maze.OLD_OBSTACLES = [[2, 3], [2, 4], [3, 3],
[5, 11], [5, 12], [5, 13], [26, 15]]
for row in range(12, 14):
for col in range(12, 21):
maze.OLD_OBSTACLES.append([row, col])
for row in range(14, 17):
for col in range(8, 24):
maze.OLD_OBSTACLES.append([row, col])
for row in range(18, 26):
for col in range(7, 25):
maze.OLD_OBSTACLES.append([row, col])
for row in range(0, 17):
for col in range(25, 28):
maze.OLD_OBSTACLES.append([row, col])
maze.OLD_OBSTACLES.append([17, 25])
maze.NEW_OBSTACLES = deepcopy(maze.OLD_OBSTACLES)
maze.NEW_OBSTACLES.remove([17, 25])
# the size of q value
maze.q_size = (maze.ROWS, maze.COLS, len(maze.actions))
# max steps per algorithm
maze.max_steps = 5000
# instantiate parameters
params = DynaParams()
# set up models for planning
models = [Dyna, QueueDyna, QueueDyna]
methods = [dyna, dyna_lf, dyna_f]
method_names = ['Dyna', 'largest-first Dyna', 'focused Dyna']
# track the # of steps and backups
steps = np.zeros((len(method_names), episodes, runs))
backups = np.zeros((len(method_names), episodes, runs))
# new states and actions for priority queue after removing obstacle
states = [[17, 24], [18, 25], [17, 26], [17, 25], [17, 25], [17, 25]]
actions = [maze.ACTION_RIGHT, maze.ACTION_UP,
maze.ACTION_LEFT, maze.ACTION_LEFT, maze.ACTION_DOWN, maze.ACTION_RIGHT]
for run in tqdm(range(runs)):
for nameIndex in range(len(method_names)):
print('run %d, %s' % (run, method_names[nameIndex]))
# set original maze
maze.obstacles = maze.OLD_OBSTACLES
# instantiate a Q-table
q_value = np.zeros(maze.q_size)
# instantiate model
model = models[nameIndex]()
print_counter = 0
while True: # loop until optimal path (episodes)
s, _ = methods[nameIndex](
q_value, model, maze, params, maze.START)
path = s
print('The last path took %i steps.' % (path))
print_counter += 1
if print_counter % PRINT_INTERVAL == 0:
print('Q-table: \n', str(q_value))
if path <= OPTIMAL_PATH:
break
# change the obstacles
maze.obstacles = maze.NEW_OBSTACLES
# feed new state-action pairs into the model
update_model(maze, model)
# insert new state-action pairs into priority queue
if methods[nameIndex] == dyna_lf:
for state, action in zip(states, actions):
next_state, reward = maze.step(state, action)
priority = np.abs(reward + params.gamma *
np.max(q_value[next_state[0],
next_state[1], :]) -
q_value[state[0], state[1], action])
model.insert(priority, state, action)
if methods[nameIndex] == dyna_f:
for state, action in zip(states, actions):
next_state, reward = maze.step(state, action)
priority = (params.gamma**dist_from_start(maze.START, state)) * \
(reward + params.gamma *
np.max(q_value[next_state[0], next_state[1], :]) -
q_value[state[0], state[1], action])
model.insert(priority, state, action)
# play for an episode
for ep in range(episodes):
if ep > 0:
ep_ = ep-1
else:
ep_ = 0
s, b = methods[nameIndex](
q_value, model, maze, params, maze.START)
steps[nameIndex, ep, run] += s
# print(str(steps))
backups[nameIndex, ep, run] += backups[nameIndex, ep_, run] + b
# print(str(backups))
print('steps \n', steps[:, :, run])
print('backups \n', backups[:, :, run])
# average the number of steps and backups for each episode
steps_mean = steps.mean(axis=2)
backups_mean = backups.mean(axis=2)
print('average steps \n', steps_mean)
print('average backups \n', backups_mean)
# compute the standard deviation over all runs
steps_std = steps.std(axis=2)
# backups_std = backups.std(axis=2)
print('steps std dev \n', steps_std)
# print('backups std dev \n', backups_std)
# convert NumPy arrays of means and std devs into separate lists
mean_list = []
for methodIndex in range(len(method_names)):
mean_list.append(sorted(tuple(zip(backups_mean[methodIndex, :].tolist(), steps_mean[methodIndex, :].tolist())),
key=lambda tup: tup[0]))
std_list = steps_std[methodIndex, :].tolist()
# plot the three methods
for nameIndex in range(len(method_names)):
# backups, steps = zip(*mean_list[nameIndex])
# print('average over %i runs - backups: %s, steps: %s' %
# (RUNS, backups, steps))
# zip output
plt.plot(*zip(*mean_list[nameIndex]), label=method_names[nameIndex])
plt.xlabel('no. backups')
plt.ylabel('steps to goal')
plt.xscale('log')
plt.legend()
plt.title('Runs: %i, Episodes: %i, Optimal Path: <= %i' % (runs, episodes, OPTIMAL_PATH))
plt.savefig('images/figure_10_path_78.png')
# plt.show()
plt.close()
# plot the three methods with error bars
for nameIndex in range(len(method_names)):
backups, steps = zip(*mean_list[nameIndex])
# print('average over %i runs - backups: %s, steps: %s' %
# (RUNS, backups, steps))
# print('std dev over %i runs - steps: %s' % (RUNS, std_list))
# error bar output
plt.errorbar(backups, steps, yerr=std_list, fmt='o', markersize=4,
capsize=4, label=method_names[nameIndex])
plt.xlabel('no. backups')
plt.ylabel('steps to goal')
plt.xscale('log')
plt.legend()
plt.title('Runs: %i, Episodes: %i, Optimal Path: <= %i' %
(runs, episodes, OPTIMAL_PATH))
plt.savefig('images/figure_10_path_78_error_bars.png')
# plt.show()
plt.close()
if __name__ == '__main__':
start = time()
# figure_7(RUNS, EPISODES)
# figure_8(MAX_RESOLUTION, RUNS)
figure_10(RUNS, EPISODES)
end = time()
print("Execution time: " + str((end-start)/60) + " minutes")