-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom_walk.py
809 lines (653 loc) · 21.8 KB
/
random_walk.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
import sys
from os.path import dirname, join, realpath
dir_path = dirname(dirname(realpath(__file__)))
sys.path.insert(1, join(dir_path, 'utils'))
from abc import ABC, abstractmethod
from typing import List, Callable
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm, trange
from env import RandomWalk
def get_true_value(env: RandomWalk) -> np.ndarray:
'''
Calculate true values of @env by Dynamic programming
Params
------
env: RandomWalk env
Return
------
true_value: true values of @env's states
'''
# With this random walk, it makes sense to initialize the values
# in the closed interval [-1, 1] and increasing
n_states = env.n_states
true_value = np.arange(-(n_states + 1), n_states + 3, 2) / (n_states + 1)
theta = 1e-2
while True:
old_value = true_value.copy()
for state in env.state_space:
true_value[state] = 0
trajectory = []
for action in env.action_space:
state_transition = env.get_state_transition(state, action)
for next_state in state_transition:
state_trans_prob = state_transition[next_state]
true_value[state] += env.transition_probs[action] \
* state_trans_prob * true_value[next_state]
delta = np.sum(np.abs(old_value - true_value))
if delta < theta:
break
true_value[0] = true_value[-1] = 0
return true_value
class ValueFunction(ABC):
def __init__(self):
pass
@abstractmethod
def get_value(self, state: int, terminated: bool=None) -> float:
'''
Get value of the state @state
Params
------
state: state of the agent
terminated: whether @state is a terminal state
'''
pass
@abstractmethod
def update(self, state: int, error: float) -> None:
'''
Update weight vector
Params
------
state: state of the agent
error: update amount
'''
pass
class StateAggregationValueFunction(ValueFunction):
'''
Value Function using state aggreagation as feature mapping
'''
def __init__(self, n_groups: int, n_states: int) -> None:
'''
Params
------
n_groups: number of groups
n_states: number of states
'''
self.n_groups = n_groups
self.group_size = n_states // n_groups
self.weights = np.zeros(n_groups)
def _get_group(self, state: int) -> int:
'''
Get group index
Params
------
state: state of the agent
Return
------
group_idx: group index
'''
group_idx = (state - 1) // self.group_size
return group_idx
def _get_grad(self, state: int) -> np.ndarray:
'''
Compute the gradient w.r.t @self.weights at state @state
Params
------
state: state of the agent
Return
------
grad: gradient w.r.t @self.weights at the state @state
'''
group_idx = self._get_group(state)
grad = np.zeros(self.n_groups)
grad[group_idx] = 1
return grad
def get_value(self, state: int, terminated: bool=None) -> float:
'''
Get value of state @state
States within a group share the same value function,
which is a component of @self.weights
Params
------
state: state of the agent
terminated: whether state @state is terminal
Return
------
value: value of state @state
'''
if terminated is not None and terminated:
value = 0
else:
group_idx = self._get_group(state)
value = self.weights[group_idx]
return value
def update(self, state: int, error: float) -> None:
'''
Update weight vector
Params
------
state: state of the agent
error: update amount
'''
grad = self._get_grad(state)
self.weights += error * grad
Feature_mapping = Callable[[int, int], float]
class BasesValueFunction(ValueFunction):
'''
Value function using polynomial/Fourier basis as feature mapping
'''
def __init__(self, order: int, basis_type: str,
n_states: int) -> None:
'''
Params
------
order: order
basis_type: basis type
n_states: number of states
'''
self.order = order
self.basis_type = basis_type
self.basis_types = ['Polynomial', 'Fourier']
self.n_states = n_states
# additional basis for bias
self.weights = np.zeros(order + 1)
self.features = self._get_features()
def _get_features(self) -> List[Feature_mapping]:
'''
Get feature vector functions with 1-dim state
Return
------
features: list of feature mapping functions
'''
features = []
if self.basis_type == self.basis_types[0]:
for i in range(self.order + 1):
features.append(lambda s, i=i: pow(s, i))
elif self.basis_type == self.basis_types[1]:
for i in range(self.order + 1):
features.append(lambda s, i=i: np.cos(i * np.pi * s))
return features
def _get_feature_vector(self, state: int) -> np.ndarray:
'''
Get feature vector of state @state
Params
------
state: state of the agent
Return
------
feature_vector: feature vector of the state @state
'''
feature_vector = np.asarray([x_i(state) for x_i in self.features])
return feature_vector
def _get_grad(self, state: int) -> np.ndarray:
'''
Compute the gradient w.r.t @self.w at state @state
Since value function is approximated by a linear function,
its gradient w.r.t the weight @self.weights is equal to
the feature vector @self.features
Params
------
state: state of the agent
Return
------
grad: gradient w.r.t @self.weights at the state @state
'''
state /= float(self.n_states)
feature_vector = self._get_feature_vector(state)
grad = feature_vector
return grad
def get_value(self, state: int, terminated: bool=None) -> float:
'''
Get value of the state @state
value function is equal to dot product of its feature
vector and weight corresponding
Params
------
state: state of the agent
terminated: whether @state is terminal
Return
------
value: value of the state @state
'''
if terminated is not None and terminated:
value = 0
else:
state /= float(self.n_states)
feature_vector = self._get_feature_vector(state)
value = np.dot(self.weights, feature_vector)
return value
def update(self, state: int, error: int) -> None:
'''
Update weight vector
Params
------
state: state of the agent
error: update amount
'''
grad = self._get_grad(state)
self.weights += error * grad
class TilingValueFunction(ValueFunction):
def __init__(self, n_tilings: int, tile_width: int,
tiling_offset: int, n_states: int) -> None:
'''
Params:
------
n_tilings: number of tilings
tile_width: tile width
tiling_offset: tiling offset
n_state: number of states
'''
self.n_tilings = n_tilings
self.tile_width = tile_width
self.tiling_offset = tiling_offset
# we need 1 more tile for each tiling to make sure that
# each state is covered by the same number of tiles
# within an interval with length = @self.tiling_size, all
# states activate the same tiles, have the same feature
# representation, and therefore the same value function.
self.tiling_size = n_states // tile_width + 1
self.weights = np.zeros((n_tilings, self.tiling_size))
def _get_active_tiles(self, state: int) -> List[int]:
'''
Get list of (indices of) active tiles
Params
------
state: state of the agent
Return
------
active_tiles: list of (indices of) active tiles
'''
active_tiles = []
for tiling_idx in range(self.n_tilings):
tile_idx = (state - self.tiling_offset * tiling_idx - 1) \
// self.tile_width + 1
active_tiles.append(tile_idx)
return active_tiles
def get_value(self, state: int) -> float:
'''
Get value of the state @state
Params
------
state: state of the agent
Return
------
value: value of the state @state
'''
value = 0
active_tiles = self._get_active_tiles(state)
for tiling_idx, tile_idx in enumerate(active_tiles):
value += self.weights[tiling_idx, tile_idx]
return value
def update(self, state: int, error: float) -> None:
'''
Update weight vector
Params
------
state: state of the agent
error: update amount
'''
active_tiles = self._get_active_tiles(state)
error /= self.n_tilings
for tiling_idx in range(self.n_tilings):
self.weights[tiling_idx, active_tiles[tiling_idx]] += error
class Agent(ABC):
'''
Agent abstract class
'''
def __init__(self, env: RandomWalk,
value_function: ValueFunction,
alpha: float, gamma: float) -> None:
'''
Params
------
env: RandomWalk env
value_function: value function
alpha: step size param
gamma : discount factor
'''
self.env = env
self.value_function = value_function
self.alpha = alpha
self.gamma = gamma
def reset(self) -> None:
'''
Reset agent
'''
self.env.reset()
def random_policy(self) -> int:
'''
Policy choosing actions randomly
Return
------
action: chosen action
'''
action = np.random.choice(self.env.action_space)
return action
@abstractmethod
def learn(self) -> None:
'''
Update weights vector by SGD method
'''
pass
@abstractmethod
def run(self) -> None:
'''
Perform an episode
'''
pass
class GradientMonteCarlo(Agent):
'''
Gradient Monte Carlo agent
'''
def __init__(self, env: RandomWalk,
value_function: ValueFunction,
alpha: float, gamma: float,
mu: np.ndarray=None) -> None:
'''
Params
------
env: RandomWalk env
value_function: value function
alpha: step size param
gamma : discount factor
mu: state distribution
'''
super().__init__(env, value_function, alpha, gamma)
self.mu = mu
def learn(self, state: int, target: float, estimate: float) -> None:
'''
Update weight vector by SGD method
Params
------
state: state of the agent
target: target of the update
estimate: estimate of the update
'''
error = target - estimate
error *= self.alpha
self.value_function.update(state, error)
def run(self) -> None:
'''
Perform an episode
'''
self.reset()
trajectory = []
while True:
action = self.random_policy()
state = self.env.state
next_state, reward, terminated = self.env.step(action)
for t in range(len(trajectory)):
trajectory[t][1] += np.power(self.gamma, len(trajectory) - t) * reward
trajectory.append([state, reward])
if terminated:
break
for state, return_ in trajectory:
self.learn(state, return_, self.value_function.get_value(state))
if self.mu is not None:
self.mu[state] += 1
class NStepSemiGradientTD(Agent):
'''
n-step semi-gradient TD agent
'''
def __init__(self, env: RandomWalk,
value_function: ValueFunction,
n: int, alpha: float, gamma: float) -> None:
'''
Params
------
env: RandomWalk env
value_function: value function
n: number of steps
alpha: step size param
gamma : discount factor
'''
super().__init__(env, value_function, alpha, gamma)
self.n = n
def learn(self, state: int, target: float, estimate: float) -> None:
'''
Update weight vector by SGD method
Params
------
state: state of the agent
target: target of the update
estimate: estimate of the update
'''
error = target - estimate
error *= self.alpha
self.value_function.update(state, error)
def run(self) -> None:
'''
Perform an episode
'''
self.reset()
states = [self.env.state]
rewards = [0] # dummy reward to save the next reward as R_{t+1}
terminates = [False] # flag list to indicate whether S_t is terminal
T = float('inf')
t = 0
while True:
if t < T:
action = self.random_policy()
next_state, reward, terminated = self.env.step(action)
states.append(next_state)
rewards.append(reward)
terminates.append(terminated)
if terminated:
T = t + 1
tau = t - self.n + 1
if tau >= 0:
G = 0
for i in range(tau + 1, min(tau + self.n, T) + 1):
G += np.power(self.gamma, i - tau - 1) * rewards[i]
if tau + self.n < T:
G += np.power(self.gamma, self.n) * self.value_function.get_value(
states[tau + self.n], terminates[tau + self.n])
if not terminates[tau]:
self.learn(states[tau], G,
self.value_function.get_value(states[tau]))
t += 1
if tau == T - 1:
break
def gradient_mc_state_aggregation_plot(env: RandomWalk,
true_value: np.ndarray) -> None:
'''
Plot gradient MC w/ state aggregation
Params
------
env: RandomWalk env
true_value: true values
'''
alpha = 2e-5
gamma = 1
n_groups = 10
n_eps = 100000
mu = np.zeros(env.n_states + 2)
value_function = StateAggregationValueFunction(n_groups, env.n_states)
gradient_mc = GradientMonteCarlo(env, value_function, alpha, gamma, mu)
for _ in trange(n_eps):
gradient_mc.run()
mu /= np.sum(mu)
values = [value_function.get_value(state) for state in env.state_space]
fig, ax1 = plt.subplots()
value_func_plot = ax1.plot(env.state_space, values,
label=r'Approximate MC value $\hat{v}$', color='blue')
true_value_plot = ax1.plot(env.state_space, true_value[1: -1],
label=r'True value $v_\pi$', color='red')
ax1.set_xlabel('State')
ax1.set_ylabel('Value scale')
ax2 = ax1.twinx()
state_dist_plot = ax2.plot(env.state_space, mu[1: -1],
label=r'State distribution $\mu$', color='gray')
ax2.set_ylabel('Distribution scale')
plots = value_func_plot + true_value_plot + state_dist_plot
labels = [l.get_label() for l in plots]
plt.legend(plots, labels, loc=0)
plt.savefig('./gradient_mc_state_agg.png')
plt.close()
def semi_gradient_td_0_plot(env: RandomWalk,
true_value: np.ndarray) -> None:
'''
Plot semi-gradient TD(0)
Params
------
env: RandomWalk env
true_value: true values
'''
alpha = 2e-4
n_groups = 10
n_eps = 100000
gamma = 1
value_function = StateAggregationValueFunction(n_groups, env.n_states)
semi_grad_td_0 = NStepSemiGradientTD(env, value_function, 1, alpha, gamma)
for _ in trange(n_eps):
semi_grad_td_0.run()
values = [value_function.get_value(state) for state in env.state_space]
plt.plot(env.state_space, values,
label=r'Approximate TD value $\hat{v}$', color='blue')
plt.plot(env.state_space, true_value[1: -1],
label=r'True value $v_\pi$', color='red')
plt.xlabel('State')
plt.ylabel('Value scale')
plt.legend()
def n_step_semi_gradient_td_plot(env: RandomWalk,
true_value: np.ndarray) -> None:
'''
Plot n-step semi-gradient TD
Params
------
env: RandomWalk env
true_value: true values
'''
n_eps = 10
n_runs = 100
n_groups = 20
gamma = 1
ns = np.power(2, np.arange(0, 10))
alphas = np.arange(0, 1.1, 0.1)
errors = np.zeros((len(ns), len(alphas)))
for n_i, n in enumerate(ns):
for alpha_i, alpha in enumerate(alphas):
print(f'n={n}, alpha={alpha}')
for _ in trange(n_runs):
value_function = StateAggregationValueFunction(n_groups, env.n_states)
n_step_semi_grad_td = NStepSemiGradientTD(env, value_function, n, alpha, gamma)
for _ in range(n_eps):
n_step_semi_grad_td.run()
values = np.array([value_function.get_value(state)
for state in env.state_space])
rmse = np.sqrt(np.sum(np.power(values - true_value[1: -1], 2)
/ env.n_states))
errors[n_i, alpha_i] += rmse
errors /= n_eps * n_runs
for i in range(0, len(ns)):
plt.plot(alphas, errors[i, :], label='n = %d' % (ns[i]))
plt.xlabel(r'$\alpha$')
plt.ylabel('Average RMS error')
plt.ylim([0.25, 0.55])
plt.legend()
def semi_gradient_td_plot(env: RandomWalk,
true_value: np.ndarray) -> None:
'''
Plot Semi-gradient TD methods
Params
------
env: RandomWalk env
true_value: true values
'''
plt.figure(figsize=(20, 10))
plt.subplot(121)
semi_gradient_td_0_plot(env, true_value)
plt.subplot(122)
n_step_semi_gradient_td_plot(env, true_value)
plt.savefig('./semi_gradient_td.png')
plt.close()
def gradient_mc_tilings_plot(env: RandomWalk,
true_value: np.ndarray) -> None:
'''
Plot gradient Monte Carlo w/ single and multiple tilings
The single tiling method is basically state aggregation.
Params
------
env: RandomWalk env
true_value: true values
'''
n_runs = 1
n_eps = 5000
n_tilings = 50
tile_width = 200
tiling_offset = 4
gamma = 1
plot_labels = ['state aggregation (one tiling)', 'tile coding (50 tilings)']
errors = np.zeros((len(plot_labels), n_eps))
for _ in range(n_runs):
value_functions = [
StateAggregationValueFunction(env.n_states // tile_width, env.n_states),
TilingValueFunction(n_tilings, tile_width, tiling_offset, env.n_states)
]
for i in range(len(value_functions)):
for ep in trange(n_eps):
alpha = 1.0 / (ep + 1)
gradient_mc = GradientMonteCarlo(env, value_functions[i], alpha, gamma)
gradient_mc.run()
values = [value_functions[i].get_value(state) for state in env.state_space]
errors[i][ep] += np.sqrt(np.mean(np.power(true_value[1: -1] - values, 2)))
errors /= n_runs
for i in range(len(plot_labels)):
plt.plot(errors[i], label=plot_labels[i])
plt.xlabel('Episodes')
plt.ylabel('RMSE')
plt.legend()
plt.savefig('./gradient_mc_tile_coding.png')
plt.close()
def gradient_mc_bases_plot(env: RandomWalk,
true_value: np.ndarray) -> None:
'''
Plot gradient Monte Carlo w/ Fourier and polynomial bases
Params
------
env: RandomWalk env
true_value: true values
'''
orders = [5, 10, 20]
n_runs = 1
n_eps = 5000
gamma = 1
bases = [
{'method': 'Polynomial', 'alpha': 1e-4},
{'method': 'Fourier', 'alpha': 5e-5}
]
errors = np.zeros((len(bases), len(orders), n_eps))
for i_basis, basis in enumerate(bases):
for i_order, order in enumerate(orders):
print(f'{basis["method"]} basis, order={order}')
for _ in range(n_runs):
value_function = BasesValueFunction(order, basis['method'], env.n_states)
gradient_mc = GradientMonteCarlo(env, value_function, basis['alpha'], gamma)
for ep in trange(n_eps):
gradient_mc.run()
values = np.array([value_function.get_value(state)
for state in env.state_space])
rmse = np.sqrt(np.mean(np.power(values - true_value[1: -1], 2)))
errors[i_basis, i_order, ep] += rmse
errors /= n_runs
for i_basis, basis in enumerate(bases):
for i_order, order in enumerate(orders):
plt.plot(errors[i_basis, i_order, :], label='%s basis, order = %d' \
% (basis['method'], order))
plt.xlabel('Episodes')
plt.ylabel('RMSE')
plt.legend()
plt.savefig('./gradient_mc_bases.png')
plt.close()
if __name__ == '__main__':
n_states = 1000
start_state = 500
terminal_states = [0, n_states + 1]
transition_radius = 100
env = RandomWalk(n_states, start_state, terminal_states,
transition_radius=transition_radius)
true_value = get_true_value(env)
gradient_mc_state_aggregation_plot(env, true_value)
semi_gradient_td_plot(env, true_value)
gradient_mc_tilings_plot(env, true_value)
gradient_mc_bases_plot(env, true_value)