-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtcar.py
139 lines (103 loc) · 2.59 KB
/
mtcar.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
import gym
import numpy as np
import matplotlib.pyplot as plt
env=gym.make('MountainCar-v0')
outdir='experimentj/'
env=gym.wrappers.Monitor(env,outdir,video_callable=False,force=True)
na=env.action_space.n
nx=env.observation_space.high.size
nrbf=16*np.ones(nx).astype(int)
width=1./(nrbf-1.)
sigma=width[0]/2.
den=2*sigma**2
episodes=500
steps=2000
stpcount=0
epsilon0=0.1
epsilonf=0.01
epsilon_all=np.linspace(epsilon0,epsilonf,num=episodes)
#epsilonk=(epsilon-epsilonf)**(1./episodes)
lambd=0.5
alpha=0.1
gamma=0.99
xrang=np.zeros((2,nx))
xrang[0,:]=env.observation_space.low
xrang[1,:]=env.observation_space.high
n=np.prod(nrbf)#length of rbf
fs=np.zeros(n)
fs_new=np.zeros(n)
theta=np.zeros((n,na))
#rbf centers
c=np.zeros((n,nx))
for i in range(nrbf[0]):
for j in range(nrbf[1]):
c[i*nrbf[1]+j,:]=(i*width[1],j*width[0])
def normalize(s):
y=np.zeros(len(s))
for i in range(len(s)):
y[i]=(s[i]-xrang[0,i])/(xrang[1,i]-xrang[0,i])
return y
def phi(s):
fs=np.zeros(n)
for i in range(n):
fs[i]=np.exp(-np.linalg.norm(s-c[i,:])**2/den)
return fs
def egreedy(e,Q):
rand=np.random.random()
if rand<1.-e:
a=Q.argmax()
else:
a=env.action_space.sample()
return int(a)
def getQ(fs,theta):
Q=np.dot(theta.T,fs)
return Q
def getQact(fs,a,theta):
Q=np.dot(theta[:,a],fs)
#data storage
qv_all=[]
delta_all=[]
r_all=[]
epsilon=epsilon0
for ep in range(episodes):
#done=False
r_sum=0
epsilon=epsilon_all[ep]
e=np.zeros((n,na))
s=normalize(env.reset())
fs=phi(s)
Q_old=getQ(fs,theta)
a=egreedy(epsilon,Q_old)
for t in range(steps):
s_new,r,done,info=env.step(a)
s_new=normalize(s_new)
fs_new=phi(s_new)
Q=getQ(fs_new,theta)
qv_all.append(Q)
a_new=egreedy(epsilon,Q)
if done:
delta=r-Q_old[a]
else:
delta=r+gamma*Q[a_new]-Q_old[a]
delta_all.append(delta)
e[:,a]=fs #replace traces
for a in range(n):
for b in range(na):
theta[a,b]+=alpha*delta*e[a,b]
e*=gamma*lambd
s=s_new
fs=fs_new
a=a_new
Q_old=Q
r_sum+=r
if done:
break
stpcount+=1
print("Ep:"+str(ep)+" t:"+str(t)+" t_all:"+str(stpcount)+" r:"+str(r_sum)+" epsilon:"+str(epsilon)+" Q:"+str(Q))
#epsilon*=epsilonk
r_all.append(r_sum)
np.save('reward_j.npy',r_all)
np.save('qvalue.npy',qv_all)
np.save('delta.npy',delta_all)
np.save('theta.npy',theta)
env.close()