forked from TikhonJelvis/RL-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov_decision_process.py
307 lines (232 loc) · 9.03 KB
/
markov_decision_process.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
from __future__ import annotations
from abc import ABC, abstractmethod
from collections import defaultdict
from dataclasses import dataclass
from typing import (DefaultDict, Dict, Iterable, Generic, Mapping,
Tuple, Sequence, TypeVar, Optional)
from rl.distribution import (Bernoulli, Constant, Categorical, Choose,
Distribution, FiniteDistribution)
from rl.function_approx import (FunctionApprox)
from rl.markov_process import (
FiniteMarkovRewardProcess, MarkovRewardProcess, StateReward
)
A = TypeVar('A')
S = TypeVar('S')
class Policy(ABC, Generic[S, A]):
'''A policy is a function that specifies what we should do (the
action) at a given state of our MDP.
'''
@abstractmethod
def act(self, state: S) -> Optional[Distribution[A]]:
pass
class Always(Policy[S, A]):
action: A
def __init__(self, action: A):
self.action = action
def act(self, _: S) -> Optional[Distribution[A]]:
return Constant(self.action)
class FinitePolicy(Policy[S, A]):
''' A policy where the state and action spaces are finite.
'''
policy_map: Mapping[S, Optional[FiniteDistribution[A]]]
def __init__(
self,
policy_map: Mapping[S, Optional[FiniteDistribution[A]]]
):
self.policy_map = policy_map
def __repr__(self) -> str:
display = ""
for s, d in self.policy_map.items():
if d is None:
display += f"{s} is a Terminal State\n"
else:
display += f"For State {s}:\n"
for a, p in d:
display += f" Do Action {a} with Probability {p:.3f}\n"
return display
def act(self, state: S) -> Optional[FiniteDistribution[A]]:
return self.policy_map[state]
def states(self) -> Iterable[S]:
return self.policy_map.keys()
@dataclass(frozen=True)
class TransitionStep(Generic[S, A]):
'''A single step in the simulation of an MDP, containing:
state -- the state we start from
action -- the action we took at that state
next_state -- the state we ended up in after the action
reward -- the instantaneous reward we got for this transition
'''
state: S
action: A
next_state: S
reward: float
def add_return(self, γ: float, return_: float) -> ReturnStep[S, A]:
'''Given a γ and the return from 'next_state', this annotates the
transition with a return for 'state'.
'''
return ReturnStep(
self.state,
self.action,
self.next_state,
self.reward,
return_=self.reward + γ * return_
)
@dataclass(frozen=True)
class ReturnStep(TransitionStep[S, A]):
'''A Transition that also contains the total *return* for its starting
state.
'''
return_: float
class MarkovDecisionProcess(ABC, Generic[S, A]):
def apply_policy(self, policy: Policy[S, A]) -> MarkovRewardProcess[S]:
mdp = self
class RewardProcess(MarkovRewardProcess[S]):
def transition_reward(
self,
state: S
) -> Optional[Distribution[Tuple[S, float]]]:
actions: Optional[Distribution[A]] = policy.act(state)
if actions is None:
return None
# TODO: Handle the case where mdp.step(state, a)
# returns None
#
# Idea: use an exception for termination instead of
# return None?
return actions.apply(lambda a: mdp.step(state, a))
return RewardProcess()
@abstractmethod
def actions(self, state: S) -> Iterable[A]:
pass
def is_terminal(self, state: S) -> bool:
'''Is the given state a terminal state?
We cannot take any actions from a terminal state. This means
that a state is terminal iff `self.actions(s)` is empty.
'''
try:
next(iter(self.actions(state)))
return False
except StopIteration:
return True
@abstractmethod
def step(
self,
state: S,
action: A
) -> Optional[Distribution[Tuple[S, float]]]:
pass
def simulate_actions(
self,
start_states: Distribution[S],
policy: Policy[S, A]
) -> Iterable[TransitionStep[S, A]]:
'''Simulate this MDP with the given policy, yielding the
sequence of (states, action, next state, reward) 4-tuples
encountered in the simulation trace.
'''
state: S = start_states.sample()
reward: float = 0
while True:
action_distribution = policy.act(state)
if action_distribution is None:
return
action = action_distribution.sample()
next_distribution = self.step(state, action)
if next_distribution is None:
return
next_state, reward = next_distribution.sample()
yield TransitionStep(state, action, next_state, reward)
state = next_state
def action_traces(
self,
start_states: Distribution[S],
policy: Policy[S, A]
) -> Iterable[Iterable[TransitionStep[S, A]]]:
'''Yield an infinite number of traces as returned by
simulate_actions.
'''
while True:
yield self.simulate_actions(start_states, policy)
def policy_from_q(
q: FunctionApprox[Tuple[S, A]],
mdp: MarkovDecisionProcess[S, A],
ϵ: float = 0.0
) -> Policy[S, A]:
'''Return a policy that chooses the action that maximizes the reward
for each state in the given Q function.
Arguments:
q -- approximation of the Q function for the MDP
mdp -- the process for which we're generating a policy
ϵ -- the fraction of the actions where we explore rather
than following the optimal policy
Returns a policy based on the given Q function.
'''
explore = Bernoulli(ϵ)
class QPolicy(Policy[S, A]):
def act(self, s: S) -> Optional[Distribution[A]]:
if mdp.is_terminal(s):
return None
if explore.sample():
return Choose(set(mdp.actions(s)))
_, action = q.argmax((s, a) for a in mdp.actions(s))
return Constant(action)
return QPolicy()
ActionMapping = Mapping[A, StateReward[S]]
StateActionMapping = Mapping[S, Optional[ActionMapping[A, S]]]
class FiniteMarkovDecisionProcess(MarkovDecisionProcess[S, A]):
'''A Markov Decision Process with finite state and action spaces.
'''
mapping: StateActionMapping[S, A]
non_terminal_states: Sequence[S]
def __init__(self, mapping: StateActionMapping[S, A]):
self.mapping = mapping
self.non_terminal_states = [s for s, v in mapping.items()
if v is not None]
def __repr__(self) -> str:
display = ""
for s, d in self.mapping.items():
if d is None:
display += f"{s} is a Terminal State\n"
else:
display += f"From State {s}:\n"
for a, d1 in d.items():
display += f" With Action {a}:\n"
for (s1, r), p in d1:
display += f" To [State {s1} and "\
+ f"Reward {r:.3f}] with Probability {p:.3f}\n"
return display
def step(self, state: S, action: A) -> Optional[StateReward]:
action_map: Optional[ActionMapping[A, S]] = self.mapping[state]
if action_map is None:
return None
return action_map[action]
def apply_finite_policy(self, policy: FinitePolicy[S, A])\
-> FiniteMarkovRewardProcess[S]:
transition_mapping: Dict[S, Optional[StateReward[S]]] = {}
for state in self.mapping:
action_map: Optional[ActionMapping[A, S]] = self.mapping[state]
if action_map is None:
transition_mapping[state] = None
else:
outcomes: DefaultDict[Tuple[S, float], float]\
= defaultdict(float)
actions = policy.act(state)
if actions is not None:
for action, p_action in actions:
for outcome, p_state_reward in action_map[action]:
outcomes[outcome] += p_action * p_state_reward
transition_mapping[state] = Categorical(outcomes)
return FiniteMarkovRewardProcess(transition_mapping)
def action_mapping(self, state: S) -> Optional[ActionMapping[A, S]]:
return self.mapping[state]
def actions(self, state: S) -> Iterable[A]:
'''All the actions allowed for the given state.
This will be empty for terminal states.
'''
actions = self.mapping[state]
return iter([]) if actions is None else actions.keys()
def states(self) -> Iterable[S]:
'''Iterate over all the states in this process—terminal *and*
non-terminal.
'''
return self.mapping.keys()