-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtd_lambda.py
188 lines (127 loc) · 6.67 KB
/
td_lambda.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
import os,sys
import numpy as np
class Temporal_Difference_lambda:
def __init__(self,state_list,action_list):
self.states = state_list
self.actions = action_list
self.state_num = len(self.states)
self.action_num = len(self.actions)
self.Q = dict()
for s in self.states:
self.Q[s] = np.random.random(self.action_num)
#self.Q[s] = np.zeros(self.action_num)
self.Z = dict()
for s in self.states:
self.Z[s] = np.zeros(self.action_num)
def reset_Z(self):
for s in self.states:
self.Z[s] = np.zeros(self.action_num)
def set_policy(self,learning_type):
self.pi = dict()
self.mu = dict()
if learning_type == 'sarsa_lambda':
for s in self.states:
self.pi[s] = np.random.random(self.action_num)
self.pi[s] = self.pi[s] / np.sum(self.pi[s])
elif learning_type == 'naive_Q_lambda':
for s in self.states:
idx = np.random.randint(0, self.action_num, size=1)[0]
self.pi[s] = np.zeros(self.action_num)
self.pi[s][idx] = 1.0
self.mu[s] = np.random.random(self.action_num)
self.mu[s] = self.mu[s] / np.sum(self.mu[s])
def sarsa_lambda(self,env,episode_num,epsilon,alpha,gamma,Lambda,max_timestep,eval_interval):
ep_idx = 0
avg_ep_return_list = []
while ep_idx < episode_num:
if ep_idx % eval_interval == 0:
eval_ep = env.episode_generator(self.pi, max_timestep, True)
print("eval episode length:%d" % (len(eval_ep) / 3))
c_avg_return = env.avg_return_per_episode(eval_ep)
avg_ep_return_list.append(c_avg_return)
print("assessing return:%f" % c_avg_return)
print "avg return list length:", len(avg_ep_return_list)
ep_idx += 1
print "ep_idx:",ep_idx
self.reset_Z()
env.c_state = env.getInitState()
env.next_state = env.c_state
c_action_idx = np.random.choice(self.action_num, 1, p=self.pi[env.c_state])[0]
#env.c_action = self.actions[c_action_idx]
n = 0
while not (env.isTerminated() or n >= max_timestep):
env.c_state = env.next_state
env.c_action = self.actions[c_action_idx]
env.c_state, env.c_action, env.c_reward, env.next_state = env.oneStep_generator()
next_action_idx = np.random.choice(self.action_num, 1, p=self.pi[env.next_state])[0]
delta = env.c_reward + gamma * self.Q[env.next_state][next_action_idx] - self.Q[env.c_state][c_action_idx]
self.Z[env.c_state][c_action_idx] += 1
for s in self.states:
for i in range(self.action_num):
self.Q[s][i] += alpha * delta * self.Z[s][i]
self.Z[s][i] *= gamma * Lambda
# --------policy update at same time---------#
c_best_action_idx = np.argmax(self.Q[env.c_state])
for action_idx in range(self.action_num):
if action_idx == c_best_action_idx:
self.pi[env.c_state][action_idx] = 1 - epsilon + epsilon / self.action_num
else:
self.pi[env.c_state][action_idx] = epsilon / self.action_num
c_action_idx = next_action_idx
n += 1
print "n:",n
return avg_ep_return_list
# to improve the performance of Watkin's Q(lambda) and reduce the complexity of Peng's Q(lambda),we introduce naive Q(lambda)
def naive_Q_lambda(self,env,episode_num,epsilon,alpha,gamma,Lambda,max_timestep,eval_interval):
ep_idx = 0
avg_ep_return_list = []
while ep_idx < episode_num:
if ep_idx % eval_interval == 0:
eval_ep = env.episode_generator(self.pi, max_timestep, True)
print("eval episode length:%d" % (len(eval_ep) / 3))
c_avg_return = env.avg_return_per_episode(eval_ep)
avg_ep_return_list.append(c_avg_return)
print("assessing return:%f" % c_avg_return)
print "avg return list length:", len(avg_ep_return_list)
ep_idx += 1
print "ep_idx:", ep_idx
self.reset_Z()
env.c_state = env.getInitState()
env.next_state = env.c_state
c_action_idx = np.random.choice(self.action_num, 1, p=self.mu[env.c_state])[0]
# env.c_action = self.actions[c_action_idx]
n = 0
while not (env.isTerminated() or n >= max_timestep):
env.c_state = env.next_state
env.c_action = self.actions[c_action_idx]
env.c_state, env.c_action, env.c_reward, env.next_state = env.oneStep_generator()
next_action_idx = np.random.choice(self.action_num, 1, p=self.mu[env.next_state])[0]
next_best_action_idx = np.argmax(self.Q[env.next_state])
delta = env.c_reward + gamma * self.Q[env.next_state][next_best_action_idx] - self.Q[env.c_state][next_action_idx]
self.Z[env.c_state][env.c_action] += 1
if next_action_idx == next_best_action_idx:
for s in self.states:
for i in range(self.action_num):
self.Q[s][i] += alpha * delta * self.Z[s][i]
self.Z[s][i] *= Lambda * gamma
else:
for s in self.states:
for i in range(self.action_num):
self.Q[s][i] += alpha * delta * self.Z[s][i]
self.Z[s][i] = 0.5
c_best_action_idx = np.argmax(self.Q[env.c_state])
# ------- update behavior policy --------- #
for action_idx in range(self.action_num):
if action_idx == c_best_action_idx:
self.mu[env.c_state][action_idx] = 1 - epsilon + epsilon / self.action_num
else:
self.mu[env.c_state][action_idx] = epsilon / self.action_num
# --------target policy update at same time---------#
for action_idx in range(self.action_num):
if action_idx == c_best_action_idx:
self.pi[env.c_state][action_idx] = 1.0
else:
self.pi[env.c_state][action_idx] = 0.0
c_action_idx = next_action_idx
n += 1
return avg_ep_return_list