forked from TikhonJelvis/RL-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_td.py
125 lines (102 loc) · 4.01 KB
/
test_td.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
import unittest
import itertools
import random
from typing import cast, Iterable, Iterator, Optional, Tuple
from rl.distribution import Categorical, Choose
from rl.function_approx import Tabular
import rl.iterate as iterate
from rl.markov_process import FiniteMarkovRewardProcess, NonTerminal
import rl.markov_process as mp
from rl.markov_decision_process import FiniteMarkovDecisionProcess
import rl.markov_decision_process as mdp
import rl.td as td
from rl.policy import FinitePolicy
class FlipFlop(FiniteMarkovRewardProcess[bool]):
'''A version of FlipFlop implemented with the FiniteMarkovProcess
machinery.
'''
def __init__(self, p: float):
transition_reward_map = {
b: Categorical({(not b, 2.0): p, (b, 1.0): 1 - p})
for b in (True, False)
}
super().__init__(transition_reward_map)
class TestEvaluate(unittest.TestCase):
def setUp(self):
random.seed(42)
self.finite_flip_flop = FlipFlop(0.7)
self.finite_mdp = FiniteMarkovDecisionProcess({
True: {
True: Categorical({(True, 1.0): 0.7, (False, 2.0): 0.3}),
False: Categorical({(True, 1.0): 0.3, (False, 2.0): 0.7}),
},
False: {
True: Categorical({(False, 1.0): 0.7, (True, 2.0): 0.3}),
False: Categorical({(False, 1.0): 0.3, (True, 2.0): 0.7}),
}
})
def test_evaluate_finite_mrp(self) -> None:
start = Tabular(
{s: 0.0 for s in self.finite_flip_flop.non_terminal_states},
count_to_weight_func=lambda _: 0.1
)
episode_length = 20
episodes: Iterable[Iterable[mp.TransitionStep[bool]]] =\
self.finite_flip_flop.reward_traces(Choose({
NonTerminal(True),
NonTerminal(False)
}))
transitions: Iterable[mp.TransitionStep[bool]] =\
itertools.chain.from_iterable(
itertools.islice(episode, episode_length)
for episode in episodes
)
vs = td.td_prediction(transitions, γ=0.99, approx_0=start)
v: Optional[Tabular[NonTerminal[bool]]] = iterate.last(
itertools.islice(
cast(Iterator[Tabular[NonTerminal[bool]]], vs),
10000)
)
if v is not None:
self.assertEqual(len(v.values_map), 2)
for s in v.values_map:
# Intentionally loose bound—otherwise test is too slow.
# Takes >1s on my machine otherwise.
self.assertLess(abs(v(s) - 170), 3.0)
else:
assert False
def test_evaluate_finite_mdp(self) -> None:
q_0: Tabular[Tuple[NonTerminal[bool], bool]] = Tabular(
{(s, a): 0.0
for s in self.finite_mdp.non_terminal_states
for a in self.finite_mdp.actions(s)},
count_to_weight_func=lambda _: 0.1
)
uniform_policy: FinitePolicy[bool, bool] =\
FinitePolicy({
s.state: Choose(self.finite_mdp.actions(s))
for s in self.finite_mdp.non_terminal_states
})
transitions: Iterable[mdp.TransitionStep[bool, bool]] =\
self.finite_mdp.simulate_actions(
Choose(self.finite_mdp.non_terminal_states),
uniform_policy
)
qs = td.q_learning_external_transitions(
transitions,
self.finite_mdp.actions,
q_0,
γ=0.99
)
q: Optional[Tabular[Tuple[NonTerminal[bool], bool]]] =\
iterate.last(
cast(Iterator[Tabular[Tuple[NonTerminal[bool], bool]]],
itertools.islice(qs, 20000))
)
if q is not None:
self.assertEqual(len(q.values_map), 4)
for s in [NonTerminal(True), NonTerminal(False)]:
self.assertLess(abs(q((s, False)) - 170.0), 2)
self.assertGreater(q((s, False)), q((s, True)))
else:
assert False