-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrl_maze_policy_grad.py
410 lines (326 loc) · 13.1 KB
/
rl_maze_policy_grad.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
# -*- coding: utf-8 -*-
"""RL_maze_Policy_Grad.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1TCqY1mzgdqXEpHkJV-fpN9Bo1czYrIIT
"""
# Commented out IPython magic to ensure Python compatibility.
# Modification from "https://github.com/YutaroOgawa/Deep-Reinforcement-Learning-Book/blob/master/program/2_2_maze_random.ipynb"
'''
Add Using Policy Gradient !!!
'''
import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import animation, rc
matplotlib.rcParams['animation.embed_limit'] = 2**128
import matplotlib.pyplot as plt
# %matplotlib inline
# Initialize State of Maze
# Set the size of the Graph
fig = plt.figure( figsize=(10, 10) )
ax = plt.gca()
# Draw the Red Wall
# [x1, y1] ---> [x2, y2]
#plt.plot([x1, x2], [y1, y2], color='red', linewidth=2)
lwd=12
plt.plot([ 0, 0 ], [ 0, 10 ], color='red', linewidth=lwd)
plt.plot([ 0,10 ], [ 10, 10 ], color='red', linewidth=lwd)
plt.plot([ 10,10 ], [ 10, 0 ], color='red', linewidth=lwd)
plt.plot([ 10, 0 ], [ 0, 0 ], color='red', linewidth=lwd)
plt.plot([1, 1 ], [ 1, 1 + 7], color='red', linewidth=lwd)
plt.plot([1, 2 ], [ 2 + 7, 2 + 7], color='red', linewidth=lwd)
plt.plot([2, 2 ], [ 2 + 7, 1 + 7], color='red', linewidth=lwd)
plt.plot([2, 3 + 7], [ 1 + 7, 1 + 7], color='red', linewidth=lwd)
plt.plot([1, 3 ], [-1 + 7,-1 + 7], color='red', linewidth=lwd)
plt.plot([4, 9 ], [-1 + 7,-1 + 7], color='red', linewidth=lwd)
plt.plot([1, 9 ], [ 0 + 7, 0 + 7], color='red', linewidth=lwd)
plt.plot([2, 2 ], [ 0, -2 + 7], color='red', linewidth=lwd)
plt.plot([3, 3 ], [ 1, -1 + 7], color='red', linewidth=lwd)
plt.plot([4, 4 ], [ 0, -2 + 7], color='red', linewidth=lwd)
plt.plot([5, 5 ], [ 1, -1 + 7], color='red', linewidth=lwd)
plt.plot([6, 6 ], [ 0, -2 + 7], color='red', linewidth=lwd)
plt.plot([7, 7 ], [ 1, -1 + 7], color='red', linewidth=lwd)
plt.plot([8, 8 ], [ 0, -2 + 7], color='red', linewidth=lwd)
plt.plot([9, 9 ], [ 1, -1 + 7], color='red', linewidth=lwd)
plt.plot([3, 4 ], [ 5, 5 ], color='red', linewidth=lwd)
plt.plot([9, 10 ], [ 1, 1 ], color='red', linewidth=lwd)
# Draw the text to represent state of agent in the maze
def plot_fun(i,j):
plt.text(i+0.5, (9-j)+0.5, 'S_{}'.format(i + j*10), size=8, ha='center')
[[plot_fun(i,j) for i in range(10)] for j in range(10)]
# Set the range of the graph
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
plt.tick_params(axis='both',
which='both',
bottom='off',
top='off',
labelbottom='off',
right='off',
left='off',
labelleft='off',
)
# Draw it!
start_x, start_y = [1 - 0.5, 10 - 0.5]
plt.text(start_x, start_y - 0.25, 'START', ha='center')
plt.text(start_y, start_x - 0.25, 'GOAL', ha='center')
line, =ax.plot([start_x], [start_y], marker="o", color='g', markersize=35)
line, =ax.plot([start_y], [start_x], marker="o", color='y', markersize=35)
# Set the Strategy parameters theta_0 (The Strategy Table)
N = np.nan
# up, right, down, left
theta_0_row9 = np.array([
[ N, 1, 1, N],# S_0
[ N, 1, N, 1],# S_1
[ N, 1, 1, 1],# S_2
[ N, 1, 1, 1],# S_3
[ N, 1, 1, 1],# S_4
[ N, 1, 1, 1],# S_5
[ N, 1, 1, 1],# S_6
[ N, 1, 1, 1],# S_7
[ N, 1, 1, 1],# S_8
[ N, N, 1, 1],# S_9
])
theta_0_row8 = np.array([
[ 1, 1, 1, N],# S_10
[ N, N, 1, 1],# S_11
[ 1, 1, N, N],# S_12
[ 1, 1, N, 1],# S_13
[ 1, 1, N, 1],# S_14
[ 1, 1, N, 1],# S_15
[ 1, 1, N, 1],# S_16
[ 1, 1, N, 1],# S_17
[ 1, 1, N, 1],# S_18
[ 1, N, N, 1],# S_19
])
theta_0_row7 = np.array([
[ 1, N, 1, N],# S_20
[ 1, 1, N, N],# S_21
[ N, 1, N, 1],# S_22
[ N, 1, N, 1],# S_23
[ N, 1, N, 1],# S_24
[ N, 1, N, 1],# S_25
[ N, 1, N, 1],# S_26
[ N, 1, N, 1],# S_27
[ N, 1, N, 1],# S_28
[ N, N, 1, 1],# S_29
])
theta_0_row6 = np.array([
[ 1, N, 1, N],# S_30
[ N, 1, N, N],# S_31
[ N, 1, N, 1],# S_32
[ N, 1, 1, 1],# S_33
[ N, 1, N, 1],# S_34
[ N, 1, N, 1],# S_35
[ N, 1, N, 1],# S_36
[ N, 1, N, 1],# S_37
[ N, 1, N, 1],# S_38
[ 1, N, 1, 1],# S_39
])
theta_0_row5 = np.array([
[ 1, N, 1, N],# S_40
[ N, 1, 1, N],# S_41
[ N, N, 1, 1],# S_42
[ 1, 1, N, N],# S_43
[ N, N, 1, 1],# S_44
[ N, 1, 1, N],# S_45
[ N, N, 1, 1],# S_46
[ N, 1, 1, N],# S_47
[ N, N, 1, 1],# S_48
[ 1, N, 1, N],# S_49
])
theta_0_row4 = np.array([
[ 1, N, 1, N],# S_50
[ 1, N, 1, N],# S_51
[ 1, N, 1, N],# S_52
[ N, N, 1, N],# S_53
[ 1, N, 1, N],# S_54
[ 1, N, 1, N],# S_55
[ 1, N, 1, N],# S_56
[ 1, N, 1, N],# S_57
[ 1, N, 1, N],# S_58
[ 1, N, 1, N],# S_59
])
theta_0_row3 = np.array([
[ 1, N, 1, N],# S_60
[ 1, N, 1, N],# S_61
[ 1, N, 1, N],# S_62
[ 1, N, 1, N],# S_63
[ 1, N, 1, N],# S_64
[ 1, N, 1, N],# S_65
[ 1, N, 1, N],# S_66
[ 1, N, 1, N],# S_67
[ 1, N, 1, N],# S_68
[ 1, N, 1, N],# S_69
])
theta_0_row2 = np.array([
[ 1, N, 1, N],# S_70
[ 1, N, 1, N],# S_71
[ 1, N, 1, N],# S_72
[ 1, N, 1, N],# S_73
[ 1, N, 1, N],# S_74
[ 1, N, 1, N],# S_75
[ 1, N, 1, N],# S_76
[ 1, N, 1, N],# S_77
[ 1, N, 1, N],# S_78
[ 1, N, 1, N],# S_79
])
theta_0_row1 = np.array([
[ 1, N, 1, N],# S_80
[ 1, N, 1, N],# S_81
[ 1, N, 1, N],# S_82
[ 1, N, 1, N],# S_83
[ 1, N, 1, N],# S_84
[ 1, N, 1, N],# S_85
[ 1, N, 1, N],# S_86
[ 1, N, 1, N],# S_87
[ 1, N, 1, N],# S_88
[ 1, N, N, N],# S_89
])
theta_0_row0 = np.array([
[ 1, 1, N, N],# S_90
[ 1, N, N, 1],# S_91
[ 1, 1, N, N],# S_92
[ 1, N, N, 1],# S_93
[ 1, 1, N, N],# S_94
[ 1, N, N, 1],# S_95
[ 1, 1, N, N],# S_96
[ 1, N, N, 1],# S_97
[ 1, 1, N, N],# S_98
[ 1, N, N, 1],# S_99
])
theta_0 = np.concatenate([
theta_0_row9,
theta_0_row8,
theta_0_row7,
theta_0_row6,
theta_0_row5,
theta_0_row4,
theta_0_row3,
theta_0_row2,
theta_0_row1,
theta_0_row0,
])
#theta_0
def softmax_convert_into_pi_from_theta(theta):
# Using Softmax function to calculate the percentage
beta = 1.0
[m, n] = theta.shape
pi = np.zeros((m, n))
exp_theta = np.exp(beta * theta)
for i in range(0, m):
# The straight-forward way of calculating percentage
#pi[i, :] = theta[i, :] / np.nansum(theta[i, :])
# The softmax way of calculating percentage
pi[i, :] = exp_theta[i, :] / np.nansum(exp_theta[i, :])
pi = np.nan_to_num(pi)
return pi
#pi_0 = simple_convert_into_pi_from_theta(theta_0)
pi_0 = softmax_convert_into_pi_from_theta(theta_0)
#pi_0
# Get Next Stage
def get_action_and_next_s(pi, s):
direction = ["up", "right", "down", "left"]
# Choose the direction according to the probability of pi[s,:]
next_direction = np.random.choice(direction, p=pi[s,:])
if next_direction == "up":
action = 0
s_next = s - 10
elif next_direction == "right":
action = 1
s_next = s + 1
elif next_direction == "down":
action = 2
s_next = s + 10
elif next_direction == "left":
action = 3
s_next = s - 1
return [action, s_next]
def goal_maze_return_state_and_action(pi):
s = 0 # At the starting point
state_and_action_history = [[0, np.nan]]
while True:
[action, next_s] = get_action_and_next_s(pi, s)
state_and_action_history[-1][1] = action
state_and_action_history.append([next_s, np.nan])
if next_s == 99:
break
else:
s = next_s
return state_and_action_history
state_and_action_history = goal_maze_return_state_and_action(pi_0)
#print("The total steps used to finish the maze is " + str(len(state_and_action_history)-1))
state_and_action_history_cutted = state_and_action_history.copy()
#print(state_and_action_history_cutted)
# Define the Update function of theta parameter
def update_theta(theta, pi, s_a_history):
eta = 0.1 # learning rate defined
T = len(state_and_action_history) - 1
# the total number of steps to reach the goal
[m, n] = theta.shape
delta_theta = theta.copy()
# Calculate delta_theta for each elements
for i in range(0, m):
for j in range(0, n):
if not(np.isnan(theta[i, j])):
SA_i = [ SA for SA in state_and_action_history if SA[0] == i ]
SA_ij = [ SA for SA in state_and_action_history if SA == [i, j]]
N_i = len(SA_i)
N_ij = len(SA_ij)
delta_theta[i, j] = (N_ij - pi[i, j] * N_i) / T
new_theta = theta + eta * delta_theta
return new_theta
new_theta = update_theta(theta_0, pi_0, state_and_action_history)
pi = softmax_convert_into_pi_from_theta(new_theta)
#print(pi)
# 方策勾配法で迷路を解く
# 初版で、def update_thetaに間違いがあった関係で、終了条件を変更します(修正日:180703)
#stop_epsilon = 10**-8 # 10^-8よりも方策に変化が少なくなったら学習終了とする
stop_epsilon = 10**-4 # 10^-4よりも方策に変化が少なくなったら学習終了とする
theta = theta_0
pi = pi_0
is_continue = True
count = 1
while is_continue: # is_continueがFalseになるまで繰り返す
state_and_action_history = goal_maze_return_state_and_action(pi) # 方策πで迷路内を探索した履歴を求める
new_theta = update_theta(theta, pi, state_and_action_history) # パラメータΘを更新
new_pi = softmax_convert_into_pi_from_theta(new_theta) # 方策πの更新
print(np.sum(np.abs(new_pi - pi))) # 方策の変化を出力
print(str(count)+" : The total steps used to finish the maze is " + str(len(state_and_action_history) - 1))
count += 1
if np.sum(np.abs(new_pi - pi)) < stop_epsilon:
is_continue = False
else:
theta = new_theta
pi = new_pi
state_and_action_history_cutted = state_and_action_history.copy()
print(state_and_action_history_cutted)
# 最終的な方策を確認
np.set_printoptions(precision=3, suppress=True) # 有効桁数3、指数表示しないという設定
print(pi)
# エージェントの移動の様子を可視化します
# 参考URL http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-notebooks/
'''
import matplotlib
from matplotlib import animation, rc
matplotlib.rcParams['animation.embed_limit'] = 2**128
'''
#from IPython.display import HTML
def init():
'''背景画像の初期化'''
line.set_data([], [])
return (line,)
def animate(i):
'''フレームごとの描画内容'''
state = state_and_action_history_cutted[i][0] # 現在の場所を描く
x = (state % 10) + 0.5 # 状態のx座標は、3で割った余り+0.5
y = 9.5 - int(state / 10) # y座標は3で割った商を9.5から引く
line.set_data(x, y)
return (line,)
# 初期化関数とフレームごとの描画関数を用いて動画を作成する
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len( state_and_action_history_cutted), interval=200, repeat=False)
#HTML(anim.to_jshtml())
#rc('animation', html='jshtml')
#anim
anim.save("maze_RL_Policy_Gradient.mp4")