forked from TikhonJelvis/RL-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapproximate_dynamic_programming.py
323 lines (245 loc) · 10.4 KB
/
approximate_dynamic_programming.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
'''Approximate dynamic programming algorithms are variations on
dynamic programming algorithms that can work with function
approximations rather than exact representations of the process's
state space.
'''
from typing import Iterator, Tuple, TypeVar, Sequence, List
from operator import itemgetter
import numpy as np
from rl.distribution import Distribution
from rl.function_approx import FunctionApprox
from rl.iterate import iterate
from rl.markov_process import (FiniteMarkovRewardProcess, MarkovRewardProcess,
RewardTransition, NonTerminal, State)
from rl.markov_decision_process import (FiniteMarkovDecisionProcess,
MarkovDecisionProcess,
StateActionMapping)
from rl.policy import DeterministicPolicy
S = TypeVar('S')
A = TypeVar('A')
# A representation of a value function for a finite MDP with states of
# type S
ValueFunctionApprox = FunctionApprox[NonTerminal[S]]
QValueFunctionApprox = FunctionApprox[Tuple[NonTerminal[S], A]]
NTStateDistribution = Distribution[NonTerminal[S]]
def extended_vf(vf: ValueFunctionApprox[S], s: State[S]) -> float:
return s.on_non_terminal(vf, 0.0)
def evaluate_finite_mrp(
mrp: FiniteMarkovRewardProcess[S],
γ: float,
approx_0: ValueFunctionApprox[S]
) -> Iterator[ValueFunctionApprox[S]]:
'''Iteratively calculate the value function for the give finite Markov
Reward Process, using the given FunctionApprox to approximate the
value function at each step.
'''
def update(v: ValueFunctionApprox[S]) -> ValueFunctionApprox[S]:
vs: np.ndarray = v.evaluate(mrp.non_terminal_states)
updated: np.ndarray = mrp.reward_function_vec + γ * \
mrp.get_transition_matrix().dot(vs)
return v.update(zip(mrp.non_terminal_states, updated))
return iterate(update, approx_0)
def evaluate_mrp(
mrp: MarkovRewardProcess[S],
γ: float,
approx_0: ValueFunctionApprox[S],
non_terminal_states_distribution: NTStateDistribution[S],
num_state_samples: int
) -> Iterator[ValueFunctionApprox[S]]:
'''Iteratively calculate the value function for the given Markov Reward
Process, using the given FunctionApprox to approximate the value function
at each step for a random sample of the process' non-terminal states.
'''
def update(v: ValueFunctionApprox[S]) -> ValueFunctionApprox[S]:
nt_states: Sequence[NonTerminal[S]] = \
non_terminal_states_distribution.sample_n(num_state_samples)
def return_(s_r: Tuple[State[S], float]) -> float:
s1, r = s_r
return r + γ * extended_vf(v, s1)
return v.update(
[(s, mrp.transition_reward(s).expectation(return_))
for s in nt_states]
)
return iterate(update, approx_0)
def value_iteration_finite(
mdp: FiniteMarkovDecisionProcess[S, A],
γ: float,
approx_0: ValueFunctionApprox[S]
) -> Iterator[ValueFunctionApprox[S]]:
'''Iteratively calculate the Optimal Value function for the given finite
Markov Decision Process, using the given FunctionApprox to approximate the
Optimal Value function at each step
'''
def update(v: ValueFunctionApprox[S]) -> ValueFunctionApprox[S]:
def return_(s_r: Tuple[State[S], float]) -> float:
s1, r = s_r
return r + γ * extended_vf(v, s1)
return v.update(
[(
s,
max(mdp.mapping[s][a].expectation(return_)
for a in mdp.actions(s))
) for s in mdp.non_terminal_states]
)
return iterate(update, approx_0)
def value_iteration(
mdp: MarkovDecisionProcess[S, A],
γ: float,
approx_0: ValueFunctionApprox[S],
non_terminal_states_distribution: NTStateDistribution[S],
num_state_samples: int
) -> Iterator[ValueFunctionApprox[S]]:
'''Iteratively calculate the Optimal Value function for the given
Markov Decision Process, using the given FunctionApprox to approximate the
Optimal Value function at each step for a random sample of the process'
non-terminal states.
'''
def update(v: ValueFunctionApprox[S]) -> ValueFunctionApprox[S]:
nt_states: Sequence[NonTerminal[S]] = \
non_terminal_states_distribution.sample_n(num_state_samples)
def return_(s_r: Tuple[State[S], float]) -> float:
s1, r = s_r
return r + γ * extended_vf(v, s1)
return v.update(
[(s, max(mdp.step(s, a).expectation(return_)
for a in mdp.actions(s)))
for s in nt_states]
)
return iterate(update, approx_0)
def backward_evaluate_finite(
step_f0_pairs: Sequence[Tuple[RewardTransition[S],
ValueFunctionApprox[S]]],
γ: float
) -> Iterator[ValueFunctionApprox[S]]:
'''Evaluate the given finite Markov Reward Process using backwards
induction, given that the process stops after limit time steps.
'''
v: List[ValueFunctionApprox[S]] = []
for i, (step, approx0) in enumerate(reversed(step_f0_pairs)):
def return_(s_r: Tuple[State[S], float], i=i) -> float:
s1, r = s_r
return r + γ * (extended_vf(v[i-1], s1) if i > 0 else 0.)
v.append(
approx0.solve([(s, res.expectation(return_))
for s, res in step.items()])
)
return reversed(v)
MRP_FuncApprox_Distribution = Tuple[MarkovRewardProcess[S],
ValueFunctionApprox[S],
NTStateDistribution[S]]
def backward_evaluate(
mrp_f0_mu_triples: Sequence[MRP_FuncApprox_Distribution[S]],
γ: float,
num_state_samples: int,
error_tolerance: float
) -> Iterator[ValueFunctionApprox[S]]:
'''Evaluate the given finite Markov Reward Process using backwards
induction, given that the process stops after limit time steps, using
the given FunctionApprox for each time step for a random sample of the
time step's states.
'''
v: List[ValueFunctionApprox[S]] = []
for i, (mrp, approx0, mu) in enumerate(reversed(mrp_f0_mu_triples)):
def return_(s_r: Tuple[State[S], float], i=i) -> float:
s1, r = s_r
return r + γ * (extended_vf(v[i-1], s1) if i > 0 else 0.)
v.append(
approx0.solve(
[(s, mrp.transition_reward(s).expectation(return_))
for s in mu.sample_n(num_state_samples)],
error_tolerance
)
)
return reversed(v)
def back_opt_vf_and_policy_finite(
step_f0s: Sequence[Tuple[StateActionMapping[S, A],
ValueFunctionApprox[S]]],
γ: float,
) -> Iterator[Tuple[ValueFunctionApprox[S], DeterministicPolicy[S, A]]]:
'''Use backwards induction to find the optimal value function and optimal
policy at each time step
'''
vp: List[Tuple[ValueFunctionApprox[S], DeterministicPolicy[S, A]]] = []
for i, (step, approx0) in enumerate(reversed(step_f0s)):
def return_(s_r: Tuple[State[S], float], i=i) -> float:
s1, r = s_r
return r + γ * (extended_vf(vp[i-1][0], s1) if i > 0 else 0.)
this_v = approx0.solve(
[(s, max(res.expectation(return_)
for a, res in actions_map.items()))
for s, actions_map in step.items()]
)
def deter_policy(state: S) -> A:
return max(
((res.expectation(return_), a) for a, res in
step[NonTerminal(state)].items()),
key=itemgetter(0)
)[1]
vp.append((this_v, DeterministicPolicy(deter_policy)))
return reversed(vp)
MDP_FuncApproxV_Distribution = Tuple[
MarkovDecisionProcess[S, A],
ValueFunctionApprox[S],
NTStateDistribution[S]
]
def back_opt_vf_and_policy(
mdp_f0_mu_triples: Sequence[MDP_FuncApproxV_Distribution[S, A]],
γ: float,
num_state_samples: int,
error_tolerance: float
) -> Iterator[Tuple[ValueFunctionApprox[S], DeterministicPolicy[S, A]]]:
'''Use backwards induction to find the optimal value function and optimal
policy at each time step, using the given FunctionApprox for each time step
for a random sample of the time step's states.
'''
vp: List[Tuple[ValueFunctionApprox[S], DeterministicPolicy[S, A]]] = []
for i, (mdp, approx0, mu) in enumerate(reversed(mdp_f0_mu_triples)):
def return_(s_r: Tuple[State[S], float], i=i) -> float:
s1, r = s_r
return r + γ * (extended_vf(vp[i-1][0], s1) if i > 0 else 0.)
this_v = approx0.solve(
[(s, max(mdp.step(s, a).expectation(return_)
for a in mdp.actions(s)))
for s in mu.sample_n(num_state_samples)],
error_tolerance
)
def deter_policy(state: S) -> A:
return max(
((mdp.step(NonTerminal(state), a).expectation(return_), a)
for a in mdp.actions(NonTerminal(state))),
key=itemgetter(0)
)[1]
vp.append((this_v, DeterministicPolicy(deter_policy)))
return reversed(vp)
MDP_FuncApproxQ_Distribution = Tuple[
MarkovDecisionProcess[S, A],
QValueFunctionApprox[S, A],
NTStateDistribution[S]
]
def back_opt_qvf(
mdp_f0_mu_triples: Sequence[MDP_FuncApproxQ_Distribution[S, A]],
γ: float,
num_state_samples: int,
error_tolerance: float
) -> Iterator[QValueFunctionApprox[S, A]]:
'''Use backwards induction to find the optimal q-value function policy at
each time step, using the given FunctionApprox (for Q-Value) for each time
step for a random sample of the time step's states.
'''
horizon: int = len(mdp_f0_mu_triples)
qvf: List[QValueFunctionApprox[S, A]] = []
for i, (mdp, approx0, mu) in enumerate(reversed(mdp_f0_mu_triples)):
def return_(s_r: Tuple[State[S], float], i=i) -> float:
s1, r = s_r
next_return: float = max(
qvf[i-1]((s1, a)) for a in
mdp_f0_mu_triples[horizon - i][0].actions(s1)
) if i > 0 and isinstance(s1, NonTerminal) else 0.
return r + γ * next_return
this_qvf = approx0.solve(
[((s, a), mdp.step(s, a).expectation(return_))
for s in mu.sample_n(num_state_samples) for a in mdp.actions(s)],
error_tolerance
)
qvf.append(this_qvf)
return reversed(qvf)