-
Notifications
You must be signed in to change notification settings - Fork 1
/
zhang_code.py
265 lines (231 loc) · 9.68 KB
/
zhang_code.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
#######################################################################
# Copyright (C) #
# 2016-2018 Shangtong Zhang(zhangshangtong.cpp@gmail.com) #
# 2016 Kenta Shimada(hyperkentakun@gmail.com) #
# Permission given to modify the code as long as you keep this #
# declaration at the top #
#######################################################################
import os
import numpy as np
import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt
from tqdm import tqdm
# Seed for random number generator
SEED = 1
# all states
N_STATES = 19
# all states but terminal states
STATES = np.arange(1, N_STATES + 1)
# start from the middle state
START_STATE = 10
# two terminal states
# an action leading to the left terminal state has reward -1
# an action leading to the right terminal state has reward 1
END_STATES = [0, N_STATES + 1]
# true state values from Bellman equation
TRUE_VALUE = np.arange(-20, 22, 2) / 20.0
TRUE_VALUE[0] = TRUE_VALUE[N_STATES + 1] = 0.0
# base class for lambda-based algorithms in this chapter
# In this example, we use the simplest linear feature function, state aggregation.
# And we use exact 19 groups, so the weights for each group is exact the value for that state
class ValueFunction:
# @rate: lambda, as it's a keyword in python, so I call it rate
# @stepSize: alpha, step size for update
def __init__(self, rate, step_size):
self.rate = rate
self.step_size = step_size
self.weights = np.zeros(N_STATES + 2)
# the state value is just the weight
def value(self, state):
return self.weights[state]
# feed the algorithm with new observation
# derived class should override this function
def learn(self, state, reward):
return
# initialize some variables at the beginning of each episode
# must be called at the very beginning of each episode
# derived class should override this function
def new_episode(self):
return
# Off-line lambda-return algorithm
class OffLineLambdaReturn(ValueFunction):
def __init__(self, rate, step_size):
ValueFunction.__init__(self, rate, step_size)
# To accelerate learning, set a truncate value for power of lambda
self.rate_truncate = 1e-3
def new_episode(self):
# initialize the trajectory
self.trajectory = [START_STATE]
# only need to track the last reward in one episode, as all others are 0
self.reward = 0.0
def learn(self, state, reward):
# add the new state to the trajectory
self.trajectory.append(state)
if state in END_STATES:
# start off-line learning once the episode ends
self.reward = reward
self.T = len(self.trajectory) - 1
self.off_line_learn()
# get the n-step return from the given time
def n_step_return_from_time(self, n, time):
# gamma is always 1 and rewards are zero except for the last reward
# the formula can be simplified
end_time = min(time + n, self.T)
returns = self.value(self.trajectory[end_time])
if end_time == self.T:
returns += self.reward
return returns
# get the lambda-return from the given time
def lambda_return_from_time(self, time):
returns = 0.0
lambda_power = 1
for n in range(1, self.T - time):
returns += lambda_power * self.n_step_return_from_time(n, time)
lambda_power *= self.rate
if lambda_power < self.rate_truncate:
# If the power of lambda has been too small, discard all the following sequences
break
returns *= 1 - self.rate
if lambda_power >= self.rate_truncate:
returns += lambda_power * self.reward
return returns
# perform off-line learning at the end of an episode
def off_line_learn(self):
for time in range(self.T):
# update for each state in the trajectory
state = self.trajectory[time]
delta = self.lambda_return_from_time(time) - self.value(state)
delta *= self.step_size
self.weights[state] += delta
# TD(lambda) algorithm
class TemporalDifferenceLambda(ValueFunction):
def __init__(self, rate, step_size):
ValueFunction.__init__(self, rate, step_size)
self.new_episode()
def new_episode(self):
# initialize the eligibility trace
self.eligibility = np.zeros(N_STATES + 2)
# initialize the beginning state
self.last_state = START_STATE
def learn(self, state, reward):
# update the eligibility trace and weights
self.eligibility *= self.rate
self.eligibility[self.last_state] += 1
delta = reward + self.value(state) - self.value(self.last_state)
delta *= self.step_size
self.weights += delta * self.eligibility
self.last_state = state
# True online TD(lambda) algorithm
class TrueOnlineTemporalDifferenceLambda(ValueFunction):
def __init__(self, rate, step_size):
ValueFunction.__init__(self, rate, step_size)
def new_episode(self):
# initialize the eligibility trace
self.eligibility = np.zeros(N_STATES + 2)
# initialize the beginning state
self.last_state = START_STATE
# initialize the old state value
self.old_state_value = 0.0
def learn(self, state, reward):
# update the eligibility trace and weights
last_state_value = self.value(self.last_state)
state_value = self.value(state)
dutch = 1 - self.step_size * self.rate * self.eligibility[self.last_state]
self.eligibility *= self.rate
self.eligibility[self.last_state] += dutch
delta = reward + state_value - last_state_value
self.weights += self.step_size * (delta + last_state_value - self.old_state_value) * self.eligibility
self.weights[self.last_state] -= self.step_size * (last_state_value - self.old_state_value)
self.old_state_value = state_value
self.last_state = state
# Random walk
def random_walk(value_function, rng=None):
if rng is None:
rng = np.random.RandomState()
value_function.new_episode()
state = START_STATE
while state not in END_STATES:
next_state = state + rng.choice([-1, 1])
if next_state == 0:
reward = -1
elif next_state == N_STATES + 1:
reward = 1
else:
reward = 0
value_function.learn(next_state, reward)
state = next_state
# general plot framework
# @valueFunctionGenerator: generate an instance of value function
# @runs: specify the number of independent runs
# @lambdas: a series of different lambda values
# @alphas: sequences of step size for each lambda
def parameter_sweep(value_function_generator, runs, lambdas, alphas):
# play for 10 episodes for each run
episodes = 10
# track the rms errors
errors = [np.zeros(len(alphas_)) for alphas_ in alphas]
for run in tqdm(range(runs)):
for lambdaIndex, rate in enumerate(lambdas):
for alphaIndex, alpha in enumerate(alphas[lambdaIndex]):
valueFunction = value_function_generator(rate, alpha)
rng = np.random.RandomState(SEED)
for episode in range(episodes):
random_walk(valueFunction, rng)
stateValues = [valueFunction.value(state) for state in STATES]
errors[lambdaIndex][alphaIndex] += np.sqrt(np.mean(np.power(stateValues - TRUE_VALUE[1: -1], 2)))
# average over runs and episodes
for error in errors:
error /= episodes * runs
for i in range(len(lambdas)):
plt.plot(alphas[i], errors[i], label='lambda = ' + str(lambdas[i]))
plt.xlabel('alpha')
plt.ylabel('RMS error')
plt.legend()
# Figure 12.3: Off-line lambda-return algorithm
def figure_12_3(plot_dir):
lambdas = [0.0, 0.4, 0.8, 0.9, 0.95, 0.975, 0.99, 1]
alphas = [np.arange(0, 1.1, 0.1),
np.arange(0, 1.1, 0.1),
np.arange(0, 1.1, 0.1),
np.arange(0, 1.1, 0.1),
np.arange(0, 1.1, 0.1),
np.arange(0, 0.55, 0.05),
np.arange(0, 0.22, 0.02),
np.arange(0, 0.11, 0.01)]
parameter_sweep(OffLineLambdaReturn, 50, lambdas, alphas)
plt.savefig(os.path.join(plot_dir, 'figure_12_3.png'))
plt.close()
# Figure 12.6: TD(lambda) algorithm
def figure_12_6(plot_dir):
lambdas = [0.0, 0.4, 0.8, 0.9, 0.95, 0.975, 0.99, 1]
alphas = [np.arange(0, 1.1, 0.1),
np.arange(0, 1.1, 0.1),
np.arange(0, 0.99, 0.09),
np.arange(0, 0.55, 0.05),
np.arange(0, 0.33, 0.03),
np.arange(0, 0.22, 0.02),
np.arange(0, 0.11, 0.01),
np.arange(0, 0.044, 0.004)]
parameter_sweep(TemporalDifferenceLambda, 50, lambdas, alphas)
plt.savefig(os.path.join(plot_dir, 'figure_12_6.png'))
plt.close()
# Figure 12.7: True online TD(lambda) algorithm
def figure_12_8(plot_dir):
lambdas = [0.0, 0.4, 0.8, 0.9, 0.95, 0.975, 0.99, 1]
alphas = [np.arange(0, 1.1, 0.1),
np.arange(0, 1.1, 0.1),
np.arange(0, 1.1, 0.1),
np.arange(0, 1.1, 0.1),
np.arange(0, 1.1, 0.1),
np.arange(0, 0.88, 0.08),
np.arange(0, 0.44, 0.04),
np.arange(0, 0.11, 0.01)]
parameter_sweep(TrueOnlineTemporalDifferenceLambda, 50, lambdas, alphas)
plt.savefig(os.path.join(plot_dir, 'figure_12_8.png'))
plt.close()
if __name__ == '__main__':
plot_dir = 'plots'
#figure_12_3(plot_dir)
#figure_12_6(plot_dir)
figure_12_8(plot_dir)