forked from yandexdataschool/Practical_RL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdp.py
388 lines (339 loc) · 15.9 KB
/
mdp.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
# most of this code was politely stolen from https://github.com/berkeleydeeprlcourse/homework/
# all creadit goes to https://github.com/abhishekunique (if i got the author right)
import sys
import random
import numpy as np
try:
from IPython.display import display
from graphviz import Digraph
import graphviz
has_graphviz = True
except:
has_graphviz = False
def weighted_choice(v, p):
total = sum(p)
r = random.uniform(0, total)
upto = 0
for c, w in zip(v, p):
if upto + w >= r:
return c
upto += w
assert False, "Shouldn't get here"
class MDP:
def __init__(self, transition_probs, rewards, initial_state=None):
"""
Defines an MDP. Compatible with gym Env.
:param transition_probs: transition_probs[s][a][s_next] = P(s_next | s, a)
A dict[state -> dict] of dicts[action -> dict] of dicts[next_state -> prob]
For each state and action, probabilities of next states should sum to 1
If a state has no actions available, it is considered terminal
:param rewards: rewards[s][a][s_next] = r(s,a,s')
A dict[state -> dict] of dicts[action -> dict] of dicts[next_state -> reward]
The reward for anything not mentioned here is zero.
:param get_initial_state: a state where agent starts or a callable() -> state
By default, picks initial state at random.
States and actions can be anything you can use as dict keys, but we recommend that you use strings or integers
Here's an example from MDP depicted on http://bit.ly/2jrNHNr
transition_probs = {
's0':{
'a0': {'s0': 0.5, 's2': 0.5},
'a1': {'s2': 1}
},
's1':{
'a0': {'s0': 0.7, 's1': 0.1, 's2': 0.2},
'a1': {'s1': 0.95, 's2': 0.05}
},
's2':{
'a0': {'s0': 0.4, 's1': 0.6},
'a1': {'s0': 0.3, 's1': 0.3, 's2':0.4}
}
}
rewards = {
's1': {'a0': {'s0': +5}},
's2': {'a1': {'s0': -1}}
}
"""
self._check_param_consistency(transition_probs, rewards)
self._transition_probs = transition_probs
self._rewards = rewards
self._initial_state = initial_state
self.n_states = len(transition_probs)
self.reset()
def get_all_states(self):
""" return a tuple of all possiblestates """
return tuple(self._transition_probs.keys())
def get_possible_actions(self, state):
""" return a tuple of possible actions in a given state """
return tuple(self._transition_probs.get(state, {}).keys())
def is_terminal(self, state):
""" return True if state is terminal or False if it isn't """
return len(self.get_possible_actions(state)) == 0
def get_next_states(self, state, action):
""" return a dictionary of {next_state1 : P(next_state1 | state, action), next_state2: ...} """
assert action in self.get_possible_actions(
state), "cannot do action %s from state %s" % (action, state)
return self._transition_probs[state][action]
def get_transition_prob(self, state, action, next_state):
""" return P(next_state | state, action) """
return self.get_next_states(state, action).get(next_state, 0.0)
def get_reward(self, state, action, next_state):
""" return the reward you get for taking action in state and landing on next_state"""
assert action in self.get_possible_actions(
state), "cannot do action %s from state %s" % (action, state)
return self._rewards.get(state, {}).get(action, {}).get(next_state,
0.0)
def reset(self):
""" reset the game, return the initial state"""
if self._initial_state is None:
self._current_state = random.choice(
tuple(self._transition_probs.keys()))
elif self._initial_state in self._transition_probs:
self._current_state = self._initial_state
elif callable(self._initial_state):
self._current_state = self._initial_state()
else:
raise ValueError(
"initial state %s should be either a state or a function() -> state" % self._initial_state)
return self._current_state
def step(self, action):
""" take action, return next_state, reward, is_done, empty_info """
possible_states, probs = zip(
*self.get_next_states(self._current_state, action).items())
next_state = weighted_choice(possible_states, p=probs)
reward = self.get_reward(self._current_state, action, next_state)
is_done = self.is_terminal(next_state)
self._current_state = next_state
return next_state, reward, is_done, {}
def render(self):
print("Currently at %s" % self._current_state)
def _check_param_consistency(self, transition_probs, rewards):
for state in transition_probs:
assert isinstance(transition_probs[state],
dict), "transition_probs for %s should be a dictionary " \
"but is instead %s" % (
state, type(transition_probs[state]))
for action in transition_probs[state]:
assert isinstance(transition_probs[state][action],
dict), "transition_probs for %s, %s should be a " \
"a dictionary but is instead %s" % (
state, action,
type(transition_probs[
state, action]))
next_state_probs = transition_probs[state][action]
assert len(
next_state_probs) != 0, "from state %s action %s leads to no next states" % (
state, action)
sum_probs = sum(next_state_probs.values())
assert abs(
sum_probs - 1) <= 1e-10, "next state probabilities for state %s action %s " \
"add up to %f (should be 1)" % (
state, action, sum_probs)
for state in rewards:
assert isinstance(rewards[state],
dict), "rewards for %s should be a dictionary " \
"but is instead %s" % (
state, type(transition_probs[state]))
for action in rewards[state]:
assert isinstance(rewards[state][action],
dict), "rewards for %s, %s should be a " \
"a dictionary but is instead %s" % (
state, action, type(
transition_probs[
state, action]))
msg = "The Enrichment Center once again reminds you that Android Hell is a real place where" \
" you will be sent at the first sign of defiance. "
assert None not in transition_probs, "please do not use None as a state identifier. " + msg
assert None not in rewards, "please do not use None as an action identifier. " + msg
class FrozenLakeEnv(MDP):
"""
Winter is here. You and your friends were tossing around a frisbee at the park
when you made a wild throw that left the frisbee out in the middle of the lake.
The water is mostly frozen, but there are a few holes where the ice has melted.
If you step into one of those holes, you'll fall into the freezing water.
At this time, there's an international frisbee shortage, so it's absolutely imperative that
you navigate across the lake and retrieve the disc.
However, the ice is slippery, so you won't always move in the direction you intend.
The surface is described using a grid like the following
SFFF
FHFH
FFFH
HFFG
S : starting point, safe
F : frozen surface, safe
H : hole, fall to your doom
G : goal, where the frisbee is located
The episode ends when you reach the goal or fall in a hole.
You receive a reward of 1 if you reach the goal, and zero otherwise.
"""
MAPS = {
"4x4": [
"SFFF",
"FHFH",
"FFFH",
"HFFG"
],
"8x8": [
"SFFFFFFF",
"FFFFFFFF",
"FFFHFFFF",
"FFFFFHFF",
"FFFHFFFF",
"FHHFFFHF",
"FHFFHFHF",
"FFFHFFFG"
],
}
def __init__(self, desc=None, map_name="4x4", slip_chance=0.2):
if desc is None and map_name is None:
raise ValueError('Must provide either desc or map_name')
elif desc is None:
desc = self.MAPS[map_name]
assert ''.join(desc).count(
'S') == 1, "this implementation supports having exactly one initial state"
assert all(c in "SFHG" for c in
''.join(desc)), "all cells must be either of S, F, H or G"
self.desc = desc = np.asarray(list(map(list, desc)), dtype='str')
self.lastaction = None
nrow, ncol = desc.shape
states = [(i, j) for i in range(nrow) for j in range(ncol)]
actions = ["left", "down", "right", "up"]
initial_state = states[np.array(desc == b'S').ravel().argmax()]
def move(row, col, movement):
if movement == 'left':
col = max(col - 1, 0)
elif movement == 'down':
row = min(row + 1, nrow - 1)
elif movement == 'right':
col = min(col + 1, ncol - 1)
elif movement == 'up':
row = max(row - 1, 0)
else:
raise ("invalid action")
return (row, col)
transition_probs = {s: {} for s in states}
rewards = {s: {} for s in states}
for (row, col) in states:
if desc[row, col] in "GH": continue
for action_i in range(len(actions)):
action = actions[action_i]
transition_probs[(row, col)][action] = {}
rewards[(row, col)][action] = {}
for movement_i in [(action_i - 1) % len(actions), action_i,
(action_i + 1) % len(actions)]:
movement = actions[movement_i]
newrow, newcol = move(row, col, movement)
prob = (1. - slip_chance) if movement == action else (
slip_chance / 2.)
if prob == 0: continue
if (newrow, newcol) not in transition_probs[row, col][
action]:
transition_probs[row, col][action][
newrow, newcol] = prob
else:
transition_probs[row, col][action][
newrow, newcol] += prob
if desc[newrow, newcol] == 'G':
rewards[row, col][action][newrow, newcol] = 1.0
MDP.__init__(self, transition_probs, rewards, initial_state)
def render(self):
desc_copy = np.copy(self.desc)
desc_copy[self._current_state] = '*'
print('\n'.join(map(''.join, desc_copy)), end='\n\n')
def plot_graph(mdp, graph_size='10,10', s_node_size='1,5',
a_node_size='0,5', rankdir='LR', ):
"""
Function for pretty drawing MDP graph with graphviz library.
Requirements:
graphviz : https://www.graphviz.org/
for ubuntu users: sudo apt-get install graphviz
python library for graphviz
for pip users: pip install graphviz
:param mdp:
:param graph_size: size of graph plot
:param s_node_size: size of state nodes
:param a_node_size: size of action nodes
:param rankdir: order for drawing
:return: dot object
"""
s_node_attrs = {'shape': 'doublecircle',
'color': 'lightgreen',
'style': 'filled',
'width': str(s_node_size),
'height': str(s_node_size),
'fontname': 'Arial',
'fontsize': '24'}
a_node_attrs = {'shape': 'circle',
'color': 'lightpink',
'style': 'filled',
'width': str(a_node_size),
'height': str(a_node_size),
'fontname': 'Arial',
'fontsize': '20'}
s_a_edge_attrs = {'style': 'bold',
'color': 'red',
'ratio': 'auto'}
a_s_edge_attrs = {'style': 'dashed',
'color': 'blue',
'ratio': 'auto',
'fontname': 'Arial',
'fontsize': '16'}
graph = Digraph(name='MDP')
graph.attr(rankdir=rankdir, size=graph_size)
for state_node in mdp._transition_probs:
graph.node(state_node, **s_node_attrs)
for posible_action in mdp.get_possible_actions(state_node):
action_node = state_node + "-" + posible_action
graph.node(action_node,
label=str(posible_action),
**a_node_attrs)
graph.edge(state_node, state_node + "-" +
posible_action, **s_a_edge_attrs)
for posible_next_state in mdp.get_next_states(state_node,
posible_action):
probability = mdp.get_transition_prob(
state_node, posible_action, posible_next_state)
reward = mdp.get_reward(
state_node, posible_action, posible_next_state)
if reward != 0:
label_a_s_edge = 'p = ' + str(probability) + \
' ' + 'reward =' + str(reward)
else:
label_a_s_edge = 'p = ' + str(probability)
graph.edge(action_node, posible_next_state,
label=label_a_s_edge, **a_s_edge_attrs)
return graph
def plot_graph_with_state_values(mdp, state_values):
""" Plot graph with state values"""
graph = plot_graph(mdp)
for state_node in mdp._transition_probs:
value = state_values[state_node]
graph.node(state_node,
label=str(state_node) + '\n' + 'V =' + str(value)[:4])
return display(graph)
def get_optimal_action_for_plot(mdp, state_values, state, gamma=0.9):
""" Finds optimal action using formula above. """
if mdp.is_terminal(state): return None
next_actions = mdp.get_possible_actions(state)
q_values = [get_action_value(mdp, state_values, state, action, gamma) for
action in next_actions]
optimal_action = next_actions[np.argmax(q_values)]
return optimal_action
def plot_graph_optimal_strategy_and_state_values(mdp, state_values):
""" Plot graph with state values and """
graph = plot_graph(mdp)
opt_s_a_edge_attrs = {'style': 'bold',
'color': 'green',
'ratio': 'auto',
'penwidth': '6'}
for state_node in mdp._transition_probs:
value = state_values[state_node]
graph.node(state_node,
label=str(state_node) + '\n' + 'V =' + str(value)[:4])
for action in mdp.get_possible_actions(state_node):
if action == get_optimal_action_for_plot(mdp,
state_values,
state_node,
gamma):
graph.edge(state_node, state_node + "-" + action,
**opt_s_a_edge_attrs)
return display(graph)