-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliff_qlearning.py
196 lines (165 loc) · 3.79 KB
/
cliff_qlearning.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
import numpy as np
import sys
import matplotlib.pyplot as plt
nrows = 3
ncols = 12
nact = 4
nepisodes = 100000
epsilon = 0.1
alpha = 0.1
gamma = 0.95
reward_normal = -1
reward_cliff = -100
reward_destination = -1
#---------------------------------------------------
Q = np.zeros((nrows,ncols,nact),dtype=np.float)
def go_to_start():
# start coordinates
y = nrows
x = 0
return x, y
def random_action():
# a = 0 : top/north
# a = 1 : right/east
# a = 2 : bottom/south
# a = 3 : left/west
a = np.random.randint(nact)
return a
def move(x,y,a):
# state = 0: OK
# state = 1: reached destination
# state = 2: fell into cliff
state = 0
if (x == 0 and y == nrows and a == 0):
# start location
x1 = x
y1 = y - 1
return x1, y1, state
elif (x == ncols-1 and y == nrows-1 and a == 2):
# reached destination
x1 = x
y1 = y + 1
state = 1
return x1, y1, state
else:
if (a == 0):
x1 = x
y1 = y - 1
elif (a == 1):
x1 = x + 1
y1 = y
elif (a == 2):
x1 = x
y1 = y + 1
elif (a == 3):
x1 = x - 1
y1 = y
if (x1 < 0):
x1 = 0
if (x1 > ncols-1):
x1 = ncols-1
if (y1 < 0):
y1 = 0
if (y1 > nrows-1):
state = 2
return x1, y1, state
def exploit(x,y,Q):
# start location
if (x == 0 and y == nrows):
a = 0
return a
# destination location
if (x == ncols-1 and y == nrows-1):
a = 2
return a
if (x == ncols-1 and y == nrows):
print("exploit at destination not possible ")
sys.exit()
# interior location
if (x < 0 or x > ncols-1 or y < 0 or y > nrows-1):
print("error ", x, y)
sys.exit()
a = np.argmax(Q[y,x,:])
return a
def bellman(x,y,a,reward,Qs1a1,Q):
if (y == nrows and x == 0):
# at start location; no Bellman update possible
return Q
if (y == nrows and x == ncols-1):
# at destination location; no Bellman update possible
return Q
Q[y,x,a] = Q[y,x,a] + alpha*(reward + gamma*Qs1a1 - Q[y,x,a])
return Q
def max_Q(x,y,Q):
a = np.argmax(Q[y,x,:])
return Q[y,x,a]
def explore_exploit(x,y,Q):
r = np.random.uniform()
if (r < epsilon):
# explore
a = random_action()
else:
# exploit
a = exploit(x,y,Q)
return a
#---------------------------------------------------
for n in range(nepisodes+1):
if (n % 1000 == 0):
print("episode #: ", n)
x, y = go_to_start()
while(True):
a = explore_exploit(x,y,Q)
x1, y1, state = move(x,y,a)
if (state == 1):
reward = reward_destination
Qs1a1 = 0.0
Q = bellman(x,y,a,reward,Qs1a1,Q)
break
elif (state == 2):
reward = reward_cliff
Qs1a1 = 0.0
Q = bellman(x,y,a,reward,Qs1a1,Q)
break
elif (state == 0):
reward = reward_normal
# Q-learning
Qs1a1 = max_Q(x1,y1,Q)
Q = bellman(x,y,a,reward,Qs1a1,Q)
x = x1
y = y1
#---------------------------------------------------
for i in range(nact):
plt.subplot(nact,1,i+1)
plt.imshow(Q[:,:,i])
plt.axis('off')
plt.colorbar()
if (i == 0):
plt.title('Q-north')
elif (i == 1):
plt.title('Q-east')
elif (i == 2):
plt.title('Q-south')
elif (i == 3):
plt.title('Q-west')
plt.savefig('Q_qlearning.png')
plt.clf()
plt.close()
#---------------------------------------------------
# path planning
path = np.zeros((nrows,ncols,nact),dtype=np.float)
x, y = go_to_start()
while(True):
a = exploit(x,y,Q)
print(x,y,a)
x1, y1, state = move(x,y,a)
if (state == 1 or state == 2):
print("breaking ", state)
break
elif (state == 0):
x = x1
y = y1
if (x >= 0 and x <= ncols-1 and y >= 0 and y <= nrows-1):
path[y,x] = 100.0
plt.imshow(path)
plt.savefig('path_qlearning.png')
#---------------------------------------------------