-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmountainCar.py
217 lines (175 loc) · 5.51 KB
/
mountainCar.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
import gym
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time
def eGreedy(epsilon):
n = np.random.randint(100)
ep = epsilon*100
if (ep>n):
e = 1
else:
e = 0
return e
def findState(P,s):
minpos = minDelta(s,P,0)
minvel = minDelta(s,P,1)
posPos = np.where(P[:,0] == minpos)[0]
posVel = np.where(P[:,1] == minvel)[0]
for i in range(len(posPos)):
for j in range(len(posVel)):
if (posPos[i] == posVel[j]):
pos = posPos[i]
return pos
def minDelta(p,P,state):
delta = []
for i in range(len(P[:,state])):
delta.append(abs(p[state]-P[i,state]))
pos = delta.index(min(delta))
min_dist = P[pos,state]
return min_dist
def rearangeQ(z):
Z = np.zeros(d*d).reshape(d,d)
row = 0
column = 0
for i in range(len(z)):
if (i%d == 0 and i>0):
row +=1
column = 0
Z[row,column] = abs(z[i])
column += 1
return Z
def plotQ(Q,k):
z=[]
for i in range(len(Q)):
m=max(Q[i,:])
z.append(m)
fig = plt.figure(k)
ax = fig.gca(projection='3d')
X = np.linspace(-1.2,0.5, d)
Y = np.linspace(-0.07,0.07,d)
X,Y = np.meshgrid(X,Y)
Z = rearangeQ(z)
ax.plot_surface(X,Y,Z)
ax.set_xlabel('position')
ax.set_ylabel('velocity')
ax.set_zlabel('max Q value')
# plt.show()
# Q = np.arange(1200).reshape(400,3)
# k = 1
# plotQ(Q,k)
# plt.show()
def TabularQ(Q,P,episode,steps,epsilon,gamma,alpha,lamb):
step_til_end = []
for ep in range(episode):
print('#episode:',ep)
print('___________________________________________________________________')
E = np.zeros(d*d*env.action_space.n).reshape(d*d,env.action_space.n)
s = env._reset()
s_d = findState(P,s)
e = eGreedy(epsilon)
l = list(Q[s_d,:])
a = e*np.random.randint(env.action_space.n) + (1-e)*l.index(max(l))
for st in range(steps):
# if (st%100==0):
# print('oldpos:', s[0],'oldvel:',s[1],'action:',a)
snew,rnew,done,_ = env._step(a)
# if (st%100==0):
# print('newpos:', snew[0],'newvel:',snew[1],'done?:',done)
env._render()
s_d_new = findState(P, snew)
e = eGreedy(epsilon)
l = list(Q[s_d_new,:])
anew = e*np.random.randint(env.action_space.n) + (1-e)*l.index(max(l))
astar = l.index(max(l))
sigma = rnew + gamma*Q[s_d_new,astar] - Q[s_d,a]
E[s_d,a] = E[s_d,a]+1
for i in range(len(Q[:,0])):
Q[s_d,a] = Q[s_d,a] + alpha * sigma *E[s_d,a]
if (astar == anew):
E[s_d,a] = gamma*lamb *E[s_d,a]
else:
E[s_d,a] = 0
if (snew[0]>= 0.5):
# if (ep%10 == 0 and ep > 0):
# plotQ(Q,ep)
step_til_end.append(st)
print('geschafft in', st,'steps')
break
s = snew
s_d = s_d_new
a = anew
return step_til_end,Q
tstart = []
tend = []
for m in range(1):
tstart.append(time.time())
env=gym.make('MountainCar-v0')
intervals = [20,80,120]
d = intervals[m]
p_d = np.linspace(-1.2,0.5, d)
p_dot_d = np.linspace(-0.07,0.07,d)
P = np.zeros(d*d*2).reshape(d*d,2)
Q = np.zeros(d*d*env.action_space.n).reshape(d*d,env.action_space.n)
count = 0
for i in range(len(p_dot_d)):
for j in range(len(p_d)):
P[count,:] = np.array([p_d[j],p_dot_d[i]])
count +=1
episode = 10
steps = int(1e5)
epsilon = 0.0
gamma = 0.99
alpha = 0.1
lamb = 0.9
step_til_end,Qnew=TabularQ(Q,P,episode, steps, epsilon, gamma, alpha, lamb)
np.save(r'/home/jack/Documents/LiClipse Workspace/RL/step_til_end_' + str(m)+ '.npy',step_til_end)
# plt.figure(1)
# plt.subplot(211)
# plt.plot(range(episode),step_til_end,label = str(m))
tend.append(time.time())
deltaT = []
for m in range(len(tstart)):
deltaT.append((tend[m]-tstart[m])/float(60))
np.save(r'/home/jack/Documents/LiClipse Workspace/RL/deltaT.npy',deltaT)
#
# plt.xlabel('#steps unit goal is reached')
# plt.ylabel('#episodes')
# plt.legend()
# plt.grid()
# plt.subplot(212)
# plt.plot(intervals,deltaT)
# plt.xlabel('intervals')
# plt.ylabel('time in s')
# step_til_end = np.zeros(episode*10).reshape(10,episode)
# cumulative = []
# sum_steps = 0
# for n in range(10):
# Q = np.zeros(d*d*env.action_space.n).reshape(d*d,env.action_space.n)
# st,Qnew = TabularQ(Q,P,episode, steps, epsilon, gamma, alpha, lamb)
# step_til_end[n,:]=st
# sum_steps += 1/float(episode) * sum(step_til_end[n,:])
# cumulative.append(sum_steps)
# # Q = Qnew
#
#
# np.save(r'/home/jack/Documents/LiClipse Workspace/RL/step_til_end.npy',step_til_end)
#
# sum_episode = []
# for n in range(len(step_til_end[0,:])):
# sum_episode.append(sum(step_til_end[:,n]))
#
#
# plt.figure(1)
# plt.subplot(211)
# plt.plot(range(10),cumulative)
# plt.grid()
# plt.ylabel('averaged cumulative steps')
#
# plt.subplot(212)
# plt.plot(sum_episode,range(episode))
# plt.xlabel('averaged number of steps per episode')
# plt.ylabel('number of episodes')
# plt.grid()
#
# plt.show()