-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
437 lines (348 loc) · 16.2 KB
/
main.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
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 5 19:46:07 2020
@author: mahsa
"""
import env1
import env2
import env_ref
import Qlearning
import SARSA
import matplotlib.pyplot as plt
import time
# ****************************** Environment 1 *********************************
[env1 , x_start_env1, x_goal_env1] = env1.environment1()
opt_path_reward_env1 = 10 - (16-1) # for environment 1
##**************** Q_learning vs. SARSA - Environment 1 ********************
gamma1000 = 0.5 # discount factor
alpha1000 = 0.1 # learning rate
epsilon1000 = 0.1
max_Iter1000 = 500
wall_penalty1000 = -2
goal_state_reward1000 = 10
max_step1000 = 2000
t0 = time.time()
[num_Iter1001, num_step1001, path_reward1001, cumulative_steps1001] = Qlearning.Q_learning(env1 , x_start_env1, x_goal_env1, gamma1000, alpha1000, epsilon1000, max_Iter1000, max_step1000, wall_penalty1000, goal_state_reward1000)
t1 = time.time()
alpha1000 = 0.1 # learning rate for SARSA
[num_Iter1002, num_step1002, path_reward1002, cumulative_steps1002] = SARSA.SARSA_alg(env1 , x_start_env1, x_goal_env1, gamma1000, alpha1000, epsilon1000, max_Iter1000, max_step1000, wall_penalty1000, goal_state_reward1000)
t2 = time.time()
fig1001 = plt.figure()
ax = plt.subplot(111)
ax.plot(num_Iter1001, num_step1001, '-b', label='Q_learning')
ax.plot(num_Iter1002, num_step1002, ':k', label='SARSA')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("number of steps")
plt.show()
fig1001.savefig('number-of-steps-1000.png')
fig1001.savefig('number-of-steps-1000.pdf')
fig1002 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter1001, path_reward1001, '-b', label='Q_learning')
ax.plot(num_Iter1002, path_reward1002, ':k', label='SARSA')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("sum of rewards")
plt.show()
fig1002.savefig('sum-of-rewards-1000.png')
fig1002.savefig('sum-of-rewards-1000.pdf')
fig1003 = plt.figure()
ax = plt.subplot()
ax.plot(cumulative_steps1001, num_Iter1001,'-b', label='Q_learning')
ax.plot(cumulative_steps1002, num_Iter1002,':k', label='SARSA')
ax.legend()
plt.xlabel("cumulative steps")
plt.ylabel("Iteration")
plt.show()
fig1003.savefig('Iteration-steps-1000.png')
fig1003.savefig('Iteration-steps-1000.pdf')
Loss1001 = path_reward1001
Loss1001[:] = [abs(number-opt_path_reward_env1)/max_Iter1000 for number in Loss1001]
Loss1002 = path_reward1002
Loss1002[:] = [abs(number-opt_path_reward_env1)/max_Iter1000 for number in Loss1002]
fig1004 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter1001, Loss1001, '-b', label='Q_learning')
ax.plot(num_Iter1002, Loss1002, ':k', label='SARSA')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("Loss")
plt.show()
fig1004.savefig('Loss-1000.png')
fig1004.savefig('Loss-1000.pdf')
print("Time elapsed for Q_learning (env1): ", t1-t0)
print("Time elapsed for SARSA (env2): ", t2-t1)
# *************** Q_learning - Environment 1 - Comparing parameter values *********************
# # ---------- different Learning rates -------------
gamma2000 = 0.5 # discount factor
epsilon2000 = 0.1
max_Iter2000 = 500
wall_penalty2000 = -2
goal_state_reward2000 = 10
max_step2000 = 2000
alpha2001 = 0.5 # learning rate
[num_Iter2001, num_step2001, path_reward2001, cumulative_steps2001] = Qlearning.Q_learning(env1 , x_start_env1, x_goal_env1, gamma2000, alpha2001, epsilon2000, max_Iter2000, max_step2000, wall_penalty2000, goal_state_reward2000)
alpha2002 = 0.2
[num_Iter2002, num_step2002, path_reward2002, cumulative_steps2002] = Qlearning.Q_learning(env1 , x_start_env1, x_goal_env1, gamma2000, alpha2002, epsilon2000, max_Iter2000, max_step2000, wall_penalty2000, goal_state_reward2000)
alpha2003 = 0.1
[num_Iter2003, num_step2003, path_reward2003, cumulative_steps2003] = Qlearning.Q_learning(env1 , x_start_env1, x_goal_env1, gamma2000, alpha2003, epsilon2000, max_Iter2000, max_step2000, wall_penalty2000, goal_state_reward2000)
fig2001 = plt.figure()
ax = plt.subplot(111)
ax.plot(num_Iter2001, num_step2001, '-b', label='learning rate = 0.5')
ax.plot(num_Iter2002, num_step2002, '--r', label='learning rate = 0.2')
ax.plot(num_Iter2003, num_step2003, ':k', label='learning rate = 0.1')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("number of steps")
plt.show()
fig2001.savefig('number-of-steps-2000.png')
fig2001.savefig('number-of-steps2000.pdf')
fig2002 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter2001, path_reward2001, '-b', label='learning rate = 0.5')
ax.plot(num_Iter2002, path_reward2002, '--r', label='learning rate = 0.2')
ax.plot(num_Iter2003, path_reward2003, ':k', label='learning rate = 0.1')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("sum of rewards")
plt.show()
fig2002.savefig('sum-of-rewards-2000.png')
fig2002.savefig('sum-of-rewards-2000.pdf')
Loss2001 = path_reward2001
Loss2001[:] = [abs(number-opt_path_reward_env1)/max_Iter2000 for number in Loss2001]
Loss2002 = path_reward2002
Loss2002[:] = [abs(number-opt_path_reward_env1)/max_Iter2000 for number in Loss2002]
Loss2003 = path_reward2003
Loss2003[:] = [abs(number-opt_path_reward_env1)/max_Iter2000 for number in Loss2003]
fig2003 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter2001, Loss2001, '-b', label='learning rate = 0.5')
ax.plot(num_Iter2002, Loss2002, '--r', label='learning rate = 0.2')
ax.plot(num_Iter2003, Loss2003, ':k', label='learning rate = 0.1')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("Loss")
plt.show()
fig2003.savefig('Loss-2000.png')
fig2003.savefig('Loss-2000.pdf')
##---------- different discount factor -------------
alpha3000 = 0.2 # learning rate
epsilon3000 = 0.1
max_Iter3000 = 500
wall_penalty3000 = -2
goal_state_reward3000 = 10
max_step3000 = 2000
gamma3001 = 0.1 # discount factor
[num_Iter3001, num_step3001, path_reward3001, cumulative_steps3001] = Qlearning.Q_learning(env1 , x_start_env1, x_goal_env1, gamma3001, alpha3000, epsilon3000, max_Iter3000, max_step3000, wall_penalty3000, goal_state_reward3000)
gamma3002 = 0.3
[num_Iter3002, num_step3002, path_reward3002, cumulative_steps3002] = Qlearning.Q_learning(env1 , x_start_env1, x_goal_env1, gamma3002, alpha3000, epsilon3000, max_Iter3000, max_step3000, wall_penalty3000, goal_state_reward3000)
gamma3003 = 0.5
[num_Iter3003, num_step3003, path_reward3003, cumulative_steps3003] = Qlearning.Q_learning(env1 , x_start_env1, x_goal_env1, gamma3003, alpha3000, epsilon3000, max_Iter3000, max_step3000, wall_penalty3000, goal_state_reward3000)
fig3001 = plt.figure()
ax = plt.subplot(111)
ax.plot(num_Iter3001, num_step3001, '-b', label='discount factor = 0.1')
ax.plot(num_Iter3002, num_step3002, '--r', label='discount factor = 0.3')
ax.plot(num_Iter3003, num_step3003, ':k', label='discount factor = 0.5')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("number of steps")
plt.show()
fig3001.savefig('number-of-steps-3000.png')
fig3001.savefig('number-of-steps-3000.pdf')
fig3002 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter3001, path_reward3001, '-b', label='discount factor = 0.1')
ax.plot(num_Iter3002, path_reward3002, '--r', label='discount factor = 0.3')
ax.plot(num_Iter3003, path_reward3003, ':k', label='discount factor = 0.5')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("sum of rewards")
plt.show()
fig3002.savefig('sum-of-rewards-3000.png')
fig3002.savefig('sum-of-rewards-3000.pdf')
Loss3001 = path_reward3001
Loss3001[:] = [abs(number-opt_path_reward_env1)/max_Iter3000 for number in Loss3001]
Loss3002 = path_reward3002
Loss3002[:] = [abs(number-opt_path_reward_env1)/max_Iter3000 for number in Loss3002]
Loss3003 = path_reward3003
Loss3003[:] = [abs(number-opt_path_reward_env1)/max_Iter3000 for number in Loss3003]
fig3003 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter3001, Loss3001, '-b', label='discount factor = 0.1')
ax.plot(num_Iter3002, Loss3002, '--r', label='discount factor = 0.3')
ax.plot(num_Iter3003, Loss3003, ':k', label='discount factor = 0.5')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("Loss")
plt.show()
fig3003.savefig('Loss-3000.png')
fig3003.savefig('Loss-3000.pdf')
# # _____________________________________________________________________________
##******************** Environment 2 *********************
[env2 , x_start_env2, x_goal_env2] = env2.environment2()
opt_path_reward_env2 = 10 - (35-1) # 100 - (35-1) # for environment 1
##******************** Q_learning vs SARSA - Environment 2 *********************
gamma4000 = 0.5 # discount factor
epsilon4000 = 0.1
max_Iter4000 = 3500
alpha4000 = 0.1 # learning rate for Q_learning
wall_penalty4000 = -2 #
goal_state_reward4000 = 10
max_step4000 = 4000
t3 = time.time()
[num_Iter4001, num_step4001, path_reward4001, cumulative_steps4001] = Qlearning.Q_learning(env2 , x_start_env2, x_goal_env2, gamma4000, alpha4000, epsilon4000, max_Iter4000, max_step4000, wall_penalty4000, goal_state_reward4000)
t4 = time.time()
alpha4000 = 0.1 # learning rate for SARSA
[num_Iter4002, num_step4002, path_reward4002, cumulative_steps4002] = SARSA.SARSA_alg(env2 , x_start_env2, x_goal_env2, gamma4000, alpha4000, epsilon4000, max_Iter4000, max_step4000, wall_penalty4000, goal_state_reward4000)
t5 = time.time()
fig4001 = plt.figure()
ax = plt.subplot(111)
ax.plot(num_Iter4001, num_step4001, '-b', label='Q_learning')
ax.plot(num_Iter4002, num_step4002, ':k', label='SARSA')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("number of steps")
plt.show()
fig4001.savefig('number-of-steps-4000.png')
fig4001.savefig('number-of-steps-4000.pdf')
fig4002 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter4001, path_reward4001, '-b', label='Q_learning')
ax.plot(num_Iter4002, path_reward4002, ':k', label='SARSA')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("sum of rewards")
plt.show()
fig4002.savefig('sum-of-rewards-4000.png')
fig4002.savefig('sum-of-rewards-4000.pdf')
fig4003 = plt.figure()
ax = plt.subplot()
ax.plot(cumulative_steps4001, num_Iter4001,'-b', label='Q_learning')
ax.plot(cumulative_steps4002, num_Iter4002,':k', label='SARSA')
ax.legend()
plt.xlabel("cumulative steps")
plt.ylabel("Iteration")
plt.show()
fig4003.savefig('Iteration-steps-4000.png')
fig4003.savefig('Iteration-steps-4000.pdf')
Loss4001 = path_reward4001
Loss4001[:] = [abs(number-opt_path_reward_env2)/max_Iter4000 for number in Loss4001]
Loss4002 = path_reward4002
Loss4002[:] = [abs(number-opt_path_reward_env2)/max_Iter4000 for number in Loss4002]
fig4004 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter4001, Loss4001, '-b', label='Q_learning')
ax.plot(num_Iter4002, Loss4002, ':k', label='SARSA')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("Loss")
plt.show()
fig4004.savefig('Loss-env2-4000.png')
fig4004.savefig('Loss-env2-4000.pdf')
print("Time elapsed for Q_earning (env2): ", t4-t3)
print("Time elapsed for SARSA (env2): ", t5-t4)
# # # # ---------- different Learning rates - Q_learning - Environment 2 -------------
gamma5000 = 0.5 #discount factor
epsilon5000 = 0.1
max_Iter5000 = 3000
wall_penalty5000 = -2
goal_state_reward5000 = 10
max_step5000 = 2000
alpha5001 = 0.5 # learning rate
[num_Iter5001, num_step5001, path_reward5001, cumulative_steps5001] = Qlearning.Q_learning(env2 , x_start_env2, x_goal_env2, gamma5000, alpha5001, epsilon5000, max_Iter5000, max_step5000, wall_penalty5000, goal_state_reward5000)
alpha5002 = 0.2
[num_Iter5002, num_step5002, path_reward5002, cumulative_steps5002] = Qlearning.Q_learning(env2 , x_start_env2, x_goal_env2, gamma5000, alpha5002, epsilon5000, max_Iter5000, max_step5000, wall_penalty5000, goal_state_reward5000)
alpha5003 = 0.1
[num_Iter5003, num_step5003, path_reward5003, cumulative_steps5003] = Qlearning.Q_learning(env2 , x_start_env2, x_goal_env2, gamma5000, alpha5003, epsilon5000, max_Iter5000, max_step5000, wall_penalty5000, goal_state_reward5000)
fig5001 = plt.figure()
ax = plt.subplot(111)
ax.plot(num_Iter5001, num_step5001, '-b', label='learning rate = 0.5')
ax.plot(num_Iter5002, num_step5002, '--r', label='learning rate = 0.2')
ax.plot(num_Iter5003, num_step5003, ':k', label='learning rate = 0.1')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("number of steps")
plt.show()
fig5001.savefig('number-of-steps_5000.png')
fig5001.savefig('number-of-steps_5000.pdf')
fig5002 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter5001, path_reward5001, '-b', label='learning rate = 0.5')
ax.plot(num_Iter5002, path_reward5002, '--r', label='learning rate = 0.2')
ax.plot(num_Iter5003, path_reward5003, ':k', label='learning rate = 0.1')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("sum of rewards")
plt.show()
fig5002.savefig('sum-of-rewards_5000.png')
fig5002.savefig('sum-of-rewards_5000.pdf')
Loss5001 = path_reward5001
Loss5001[:] = [abs(number-opt_path_reward_env2)/max_Iter5000 for number in Loss5001]
Loss5002 = path_reward5002
Loss5002[:] = [abs(number-opt_path_reward_env2)/max_Iter5000 for number in Loss5002]
Loss5003 = path_reward5003
Loss5003[:] = [abs(number-opt_path_reward_env2)/max_Iter5000 for number in Loss5003]
fig5003 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter5001, Loss5001, '-b', label='learning rate = 0.5')
ax.plot(num_Iter5002, Loss5002, '--r', label='learning rate = 0.2')
ax.plot(num_Iter5003, Loss5003, ':k', label='learning rate = 0.1')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("Loss")
plt.show()
fig5003.savefig('Loss_5000.png')
fig5003.savefig('Loss_5000.pdf')
# # # ---------- different Discount factor - Q_learning - Environment 2 -------------
alpha6000 = 0.2 # learning rate
epsilon6000 = 0.1
max_Iter6000 = 3000
wall_penalty6000 = -2
goal_state_reward6000 = 10
max_step6000 = 2000
gamma6001 = 0.5 # discount factor
[num_Iter6001, num_step6001, path_reward6001, cumulative_steps6001] = Qlearning.Q_learning(env2 , x_start_env2, x_goal_env2, gamma6001, alpha6000, epsilon6000, max_Iter6000, max_step6000, wall_penalty6000, goal_state_reward6000)
gamma6002 = 0.45 # discount factor
[num_Iter6002, num_step6002, path_reward6002, cumulative_steps6002] = Qlearning.Q_learning(env2 , x_start_env2, x_goal_env2, gamma6002, alpha6000, epsilon6000, max_Iter6000, max_step6000, wall_penalty6000, goal_state_reward6000)
gamma6003 = 0.35 # discount factor
[num_Iter6003, num_step6003, path_reward6003, cumulative_steps6003] = Qlearning.Q_learning(env2 , x_start_env2, x_goal_env2, gamma6003, alpha6000, epsilon6000, max_Iter6000, max_step6000, wall_penalty6000, goal_state_reward6000)
fig6001 = plt.figure()
ax = plt.subplot(111)
ax.plot(num_Iter6001, num_step6001, '-b', label='discount factor = 0.5')
ax.plot(num_Iter6002, num_step6002, '--r', label='discount factor = 0.45')
ax.plot(num_Iter6003, num_step6003, ':k', label='discount factor = 0.35')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("number of steps")
plt.show()
fig6001.savefig('number-of-steps_6000.png')
fig6001.savefig('number-of-steps_6000.pdf')
fig6002 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter6001, path_reward6001, '-b', label='discount factor = 0.5')
ax.plot(num_Iter6002, path_reward6002, '--r', label='discount factor = 0.45')
ax.plot(num_Iter6003, path_reward6003, ':k', label='discount factor = 0.35')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("sum of rewards")
plt.show()
fig6002.savefig('sum-of-rewards_6000.png')
fig6002.savefig('sum-of-rewards_6000.pdf')
Loss6001 = path_reward6001
Loss6001[:] = [abs(number-opt_path_reward_env2)/max_Iter6000 for number in Loss6001]
Loss6002 = path_reward6002
Loss6002[:] = [abs(number-opt_path_reward_env2)/max_Iter6000 for number in Loss6002]
Loss6003 = path_reward6003
Loss6003[:] = [abs(number-opt_path_reward_env2)/max_Iter6000 for number in Loss6003]
fig6003 = plt.figure()
ax = plt.subplot()
ax.plot(num_Iter6001, Loss6001, '-b', label='discount factor = 0.5')
ax.plot(num_Iter6002, Loss6002, '--r', label='discount factor = 0.45')
ax.plot(num_Iter6003, Loss6003, ':k', label='discount factor = 0.35')
ax.legend()
plt.xlabel("number of iteration")
plt.ylabel("Loss")
plt.show()
fig6003.savefig('Loss_6000.png')
fig6003.savefig('Loss_6000.pdf')