-
Notifications
You must be signed in to change notification settings - Fork 0
/
approximation.py
422 lines (338 loc) · 16.3 KB
/
approximation.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""Implementation of different methods using function approximation"""
import argparse
import numpy as np
import plot as plt
import random
from env import Action, Easy21, State
from mc import MonteCarloControl
from policy import EpsilonGreedyApproximationPolicy
from tqdm import tqdm
# For reproducibility
random.seed(0)
np.random.seed(0)
def encode(s: State, a: Action) -> np.ndarray:
"""
Encodes the given state-action pair using coarse coding as specified in the Easy21 assignment:
A binary feature vector rho(s, a) with 3 ∗ 6 ∗ 2 = 36 features. Each binary feature
has a value of 1 iff (s, a) lies within the cuboid of state-space corresponding to
that feature, and the action corresponding to that feature. The cuboids have the
following overlapping intervals:
- dealer(s) = {[1, 4], [4, 7], [7, 10]}
- player(s) = {[1, 6], [4, 9], [7, 12], [10, 15], [13, 18], [16, 21]}
- a = {hit, stick}
:param State s: The state to encode
:param Action a: The action to encode
:return: A binary feature vector representing the encoded state-action pair
:rtype: np.ndarray
"""
# `range` is end-exclusive so we add a 1 to make sure we capture the intervals inclusive ends
dealer = [range(1, 5), range(4, 8), range(7, 11)]
player = [range(1, 7), range(4, 10), range(7, 13), range(10, 16), range(13, 19), range(16, 22)]
encoded = np.zeros((3, 6, 2))
for i, d in enumerate(dealer):
for j, p in enumerate(player):
for k, action in enumerate([Action.hit, Action.stick]):
if s.dealer_first_card in d and s.player_sum in p and a == action:
encoded[i, j, k] = 1
return encoded.flatten()
class OnPolicyGradientMonteCarlo(MonteCarloControl):
"""On-policy gradient Monte Carlo with epsilon-soft policy"""
def learn(self, epochs=200, alpha=0.01, l=1.0, verbose=False, **kwargs) -> np.ndarray:
"""
Learns the optimal value function.
:param int epochs: The number of epochs to take to learn the value function
:param float alpha: The learning rate
:param float l: The discount factor lambda
:param bool verbose: Whether to use verbose mode or not
:return: The optimal value function
:rtype: np.ndarray
"""
w = np.random.rand(36)
approximator = lambda s: [np.dot(w, encode(s, a)) for a in [Action.hit, Action.stick]]
# Constant exploration as in the Easy21 assignment
pi = EpsilonGreedyApproximationPolicy(epsilon=0.05, approximator=approximator, seed=24)
for _ in tqdm(range(epochs), disable=not verbose):
trajectories = self._sample_episode(pi)
# Reverse the list so we start backpropagating the return from the last episode
trajectories.reverse()
# Learn from the episode
g = 0
for t in trajectories:
g = t.reward + l * g
# SGD update
x = encode(t.state, t.action)
w += alpha * (g - np.dot(w, x)) * x
# Compute the optimal value function which is simply the value of the best action in each state
values = np.zeros(self._env.state_space)
for d in range(self._env.state_space[0]):
for p in range(self._env.state_space[1]):
values[d, p] = np.max(approximator(State(d, p)))
return values
class SemiGradientTDZero:
"""Semi-gradient TD(0) with epsilon-soft policy"""
def __init__(self):
self._env = Easy21(seed=24)
def learn(self, epochs=200, alpha=0.01, gamma=0.9, verbose=False, **kwargs) -> np.ndarray:
"""
Learns the optimal value function.
:param int epochs: The number of epochs to take to learn the value function
:param float alpha: The learning rate
:param float gamma: The discount factor
:param bool verbose: Whether to use verbose mode or not
:return: The optimal value function
:rtype: np.ndarray
"""
w = np.random.rand(36)
approximator = lambda s: [np.dot(w, encode(s, a)) for a in [Action.hit, Action.stick]]
# Constant exploration as in the Easy21 assignment
pi = EpsilonGreedyApproximationPolicy(epsilon=0.05, approximator=approximator, seed=24)
for _ in tqdm(range(epochs), disable=not verbose):
s = self._env.reset()
done = False
while not done:
a = pi[s]
s_prime, r, done = self._env.step(a)
# Compute the TD target
if done:
td_target = r
else:
td_target = r + gamma * np.max(approximator(s_prime))
# SGD update
x = encode(s, a)
w += alpha * (td_target - np.dot(w, x)) * x
s = s_prime
# Compute the optimal value function which is simply the value of the best action in each state
values = np.zeros(self._env.state_space)
for d in range(self._env.state_space[0]):
for p in range(self._env.state_space[1]):
values[d, p] = np.max(approximator(State(d, p)))
return values
class SemiGradientNStepTD:
"""Semi-gradient n-step TD with epsilon-soft policy"""
def __init__(self):
self._env = Easy21(seed=24)
def learn(self, epochs=200, n=10, alpha=0.01, gamma=0.9, verbose=False, **kwargs) -> np.ndarray:
"""
Learns the optimal value function.
:param int epochs: The number of epochs to take to learn the value function
:param int n: The n-steps to use
:param float alpha: The learning rate
:param float gamma: The discount factor
:param bool verbose: Whether to use verbose mode or not
:return: The optimal value function
:rtype: np.ndarray
"""
w = np.random.rand(36)
approximator = lambda s: [np.dot(w, encode(s, a)) for a in [Action.hit, Action.stick]]
# Constant exploration as in the Easy21 assignment
pi = EpsilonGreedyApproximationPolicy(epsilon=0.05, approximator=approximator, seed=24)
for _ in tqdm(range(epochs), disable=not verbose):
states = []
rewards = []
actions = []
s = self._env.reset()
states.append(s)
# T controls the end of the episode
T = np.inf
# t is the current time step
t = 0
while True:
if t < T:
a = pi[s]
s_prime, r, done = self._env.step(a)
states.append(s_prime)
actions.append(a)
rewards.append(r)
if done:
# Stop in the next step
T = t + 1
# tau is the step whose estimate is being updated
tau = t - n + 1
if tau >= 0:
# Compute approximate reward from the current step to n-steps later or the end of the episode (if tau + n goes beyond)
# Note that in the pseudocode presented by Sutton and Barto, they use (i - tau - 1) and (tau + 1) because they index the
# current reward as R_t+1; in this implementation, the reward is considered to be part of the current step R_t and hence
# we used tau instead of tau + 1
G = sum([gamma ** (i - tau) * rewards[i] for i in range(tau, min(tau + n, T))])
# Bootstrap the missing values if the we're not at the end of the episode using the approximator
if tau + n < T:
s = states[tau + n]
G += gamma ** n * np.max(approximator(s))
# SGD update
x = encode(states[tau], actions[tau])
w += alpha * (G - np.dot(w, x)) * x
# Stop when we have reached the end of the episode
if tau == T - 1:
break
t += 1
# Compute the optimal value function which is simply the value of the best action in each state
values = np.zeros(self._env.state_space)
for d in range(self._env.state_space[0]):
for p in range(self._env.state_space[1]):
values[d, p] = np.max(approximator(State(d, p)))
return values
class SemiGradientSarsa:
"""On-policy semi-gradient SARSA with epsilon-soft policy"""
def __init__(self):
self._env = Easy21(seed=24)
def learn(self, epochs=200, alpha=0.01, gamma=0.9, verbose=False, **kwargs) -> np.ndarray:
"""
Learns the optimal value function.
:param int epochs: The number of epochs to take to learn the value function
:param float alpha: The learning rate
:param float gamma: The discount factor
:param bool verbose: Whether to use verbose mode or not
:return: The optimal value function
:rtype: np.ndarray
"""
w = np.random.rand(36)
approximator = lambda s: [np.dot(w, encode(s, a)) for a in [Action.hit, Action.stick]]
# Constant exploration as in the Easy21 assignment
pi = EpsilonGreedyApproximationPolicy(epsilon=0.05, approximator=approximator, seed=24)
for _ in tqdm(range(epochs), disable=not verbose):
s = self._env.reset()
a = pi[s]
done = False
while not done:
# Generate S,A,R,S',A' trajectory
s_prime, r, done = self._env.step(a)
# Compute the TD target
if done:
a_prime = None
td_target = r
else:
a_prime = pi[s_prime]
td_target = r + gamma * np.dot(w, encode(s_prime, a_prime))
# SGD update
x = encode(s, a)
w += alpha * (td_target - np.dot(w, x)) * x
s = s_prime
a = a_prime
# Compute the optimal value function which is simply the value of the best action in each state
values = np.zeros(self._env.state_space)
for d in range(self._env.state_space[0]):
for p in range(self._env.state_space[1]):
values[d, p] = np.max(approximator(State(d, p)))
return values
class SemiGradientNStepSarsa:
"""On-policy semi-gradient n-step SARSA with epsilon-soft policy"""
def __init__(self):
self._env = Easy21(seed=24)
def learn(self, epochs=200, n=10, alpha=0.01, gamma=0.9, verbose=False, **kwargs) -> np.ndarray:
"""
Learns the optimal value function.
:param int epochs: The number of epochs to take to learn the value function
:param int n: The n-steps to use
:param float alpha: The learning rate
:param float gamma: The discount factor
:param bool verbose: Whether to use verbose mode or not
:return: The optimal value function
:rtype: np.ndarray
"""
w = np.random.rand(36)
approximator = lambda s: [np.dot(w, encode(s, a)) for a in [Action.hit, Action.stick]]
# Constant exploration as in the Easy21 assignment
pi = EpsilonGreedyApproximationPolicy(epsilon=0.05, approximator=approximator, seed=24)
for _ in tqdm(range(epochs), disable=not verbose):
states = []
actions = []
rewards = []
s = self._env.reset()
states.append(s)
a = pi[s]
actions.append(a)
# T controls the end of the episode
T = np.inf
# t is the current time step
t = 0
while True:
if t < T:
s_prime, r, done = self._env.step(actions[t])
states.append(s_prime)
rewards.append(r)
if done:
# Stop in the next step
T = t + 1
else:
a_prime = pi[s_prime]
actions.append(a_prime)
# tau is the step whose estimate is being updated
tau = t - n + 1
if tau >= 0:
# Compute approximate reward from the current step to n-steps later or the end of the episode (if tau + n goes beyond)
# Note that in the pseudocode presented by Sutton and Barto, they use (i - tau - 1) and (tau + 1) because they index the
# current reward as R_t+1; in this implementation, the reward is considered to be part of the current step R_t and hence
# we used tau instead of tau + 1
G = sum([gamma ** (i - tau) * rewards[i] for i in range(tau, min(tau + n, T))])
# Bootstrap the missing values if the we're not at the end of the episode
if tau + n < T:
s = states[tau + n]
a = actions[tau + n]
G += gamma ** n * np.dot(w, encode(s, a))
# SGD update of the *current* time step
s = states[tau]
a = actions[tau]
x = encode(s, a)
w += alpha * (G - np.dot(w, x)) * x
# Stop when we have reached the end of the episode
if tau == T - 1:
break
t += 1
# Compute the optimal value function which is simply the value of the best action in each state
values = np.zeros(self._env.state_space)
for d in range(self._env.state_space[0]):
for p in range(self._env.state_space[1]):
values[d, p] = np.max(approximator(State(d, p)))
return values
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run approximation methods")
parser.add_argument(
"--on-policy-mc", action="store_true", help="Execute On-policy gradient Monte Carlo with epsilon-soft policy"
)
parser.add_argument("--td-zero", action="store_true", help="Execute Semi-gradient TD(0) with epsilon-soft policy")
parser.add_argument(
"--nstep-td", action="store_true", help="Execute Semi-gradient n-step TD with epsilon-soft policy"
)
parser.add_argument("--sarsa", action="store_true", help="Execute Semi-gradient SARSA with epsilon-soft policy")
parser.add_argument(
"--nstep-sarsa", action="store_true", help="Executes Semi-gradient n-step SARSA with epsilon-soft policy"
)
parser.add_argument("--epochs", type=int, default=200, help="Epochs to train")
parser.add_argument("--alpha", type=float, default=0.01, help="Learning rate to use")
parser.add_argument("--gamma", type=float, default=0.9, help="Discount factor")
parser.add_argument("-n", type=int, default=10, help="n-steps to use")
parser.add_argument("--verbose", action="store_true", help="Run in verbose mode")
args = parser.parse_args()
# The optimal value function obtained
V = None
# The algorithm to run
approx = None
# The title of the plot
title = None
if args.on_policy_mc:
print("Running On-policy Gradient Monte Carlo")
approx = OnPolicyGradientMonteCarlo()
title = "grad_on_policy_monte_carlo"
elif args.td_zero:
print("Running Semi-gradient TD(0)")
approx = SemiGradientTDZero()
title = "semi_grad_td_zero"
elif args.nstep_td:
print("Running Semi-gradient n-step TD")
approx = SemiGradientNStepTD()
title = "semi_grad_nstep_td"
elif args.sarsa:
print("Running Semi-gradient SARSA")
approx = SemiGradientSarsa()
title = "semi_grad_sarsa"
elif args.nstep_sarsa:
print("Running Semi-gradient n-step SARSA")
approx = SemiGradientNStepSarsa()
title = "semi_grad_nstep_sarsa"
if approx is not None:
V = approx.learn(epochs=args.epochs, alpha=args.alpha, gamma=args.gamma, n=args.n, verbose=args.verbose)
if V is not None:
# Plot the value function as a surface
# Remove the state where the dealer's first card is 0 and the player's sum is 0 because these are not possible
# They were kept in the value function to avoid having to deal with 0-index vs 1-index
plt.plot_value_function(range(1, Easy21.state_space[0]), range(1, Easy21.state_space[1]), V[1:, 1:], title)