Newer
Older
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Markov decision processes (MDPs)\n",
"\n",
"This IPy notebook acts as supporting material for topics covered in **Chapter 17 Making Complex Decisions** of the book* Artificial Intelligence: A Modern Approach*. We makes use of the implementations in mdp.py module. This notebook also includes a brief summary of the main topics as a review. Let us import everything from the mdp module to get started."
]
},
SnShine
a validé
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from mdp import *\n",
"from notebook import psource, pseudocode"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CONTENTS\n",
"\n",
"* Overview\n",
"* MDP\n",
"* Grid MDP\n",
"* Value Iteration\n",
" * Value Iteration Visualization\n",
"* Policy Iteration"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## OVERVIEW\n",
SnShine
a validé
"\n",
"Before we start playing with the actual implementations let us review a couple of things about MDPs.\n",
"\n",
"- A stochastic process has the **Markov property** if the conditional probability distribution of future states of the process (conditional on both past and present states) depends only upon the present state, not on the sequence of events that preceded it.\n",
"\n",
" -- Source: [Wikipedia](https://en.wikipedia.org/wiki/Markov_property)\n",
"\n",
"Often it is possible to model many different phenomena as a Markov process by being flexible with our definition of state.\n",
" \n",
"\n",
"- MDPs help us deal with fully-observable and non-deterministic/stochastic environments. For dealing with partially-observable and stochastic cases we make use of generalization of MDPs named POMDPs (partially observable Markov decision process).\n",
"\n",
"Our overall goal to solve a MDP is to come up with a policy which guides us to select the best action in each state so as to maximize the expected sum of future rewards."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## MDP\n",
"\n",
"To begin with let us look at the implementation of MDP class defined in mdp.py The docstring tells us what all is required to define a MDP namely - set of states, actions, initial state, transition model, and a reward function. Each of these are implemented as methods. Do not close the popup so that you can follow along the description of code below."
SnShine
a validé
"execution_count": 2,
Aman Deep Singh
a validé
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
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">class</span> <span class=\"nc\">MDP</span><span class=\"p\">:</span>\n",
"\n",
" <span class=\"sd\">"""A Markov Decision Process, defined by an initial state, transition model,</span>\n",
"<span class=\"sd\"> and reward function. We also keep track of a gamma value, for use by</span>\n",
"<span class=\"sd\"> algorithms. The transition model is represented somewhat differently from</span>\n",
"<span class=\"sd\"> the text. Instead of P(s' | s, a) being a probability number for each</span>\n",
"<span class=\"sd\"> state/state/action triplet, we instead have T(s, a) return a</span>\n",
"<span class=\"sd\"> list of (p, s') pairs. We also keep track of the possible states,</span>\n",
"<span class=\"sd\"> terminal states, and actions for each state. [page 646]"""</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">init</span><span class=\"p\">,</span> <span class=\"n\">actlist</span><span class=\"p\">,</span> <span class=\"n\">terminals</span><span class=\"p\">,</span> <span class=\"n\">transitions</span><span class=\"o\">=</span><span class=\"p\">{},</span> <span class=\"n\">states</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"n\">gamma</span><span class=\"o\">=.</span><span class=\"mi\">9</span><span class=\"p\">):</span>\n",
" <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"p\">(</span><span class=\"mi\">0</span> <span class=\"o\"><</span> <span class=\"n\">gamma</span> <span class=\"o\"><=</span> <span class=\"mi\">1</span><span class=\"p\">):</span>\n",
" <span class=\"k\">raise</span> <span class=\"ne\">ValueError</span><span class=\"p\">(</span><span class=\"s2\">"An MDP must have 0 < gamma <= 1"</span><span class=\"p\">)</span>\n",
"\n",
" <span class=\"k\">if</span> <span class=\"n\">states</span><span class=\"p\">:</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">states</span> <span class=\"o\">=</span> <span class=\"n\">states</span>\n",
" <span class=\"k\">else</span><span class=\"p\">:</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">states</span> <span class=\"o\">=</span> <span class=\"nb\">set</span><span class=\"p\">()</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">init</span> <span class=\"o\">=</span> <span class=\"n\">init</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">actlist</span> <span class=\"o\">=</span> <span class=\"n\">actlist</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">terminals</span> <span class=\"o\">=</span> <span class=\"n\">terminals</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">transitions</span> <span class=\"o\">=</span> <span class=\"n\">transitions</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">gamma</span> <span class=\"o\">=</span> <span class=\"n\">gamma</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">reward</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">R</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">state</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">"""Return a numeric reward for this state."""</span>\n",
" <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">reward</span><span class=\"p\">[</span><span class=\"n\">state</span><span class=\"p\">]</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">T</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">state</span><span class=\"p\">,</span> <span class=\"n\">action</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">"""Transition model. From a state and an action, return a list</span>\n",
"<span class=\"sd\"> of (probability, result-state) pairs."""</span>\n",
" <span class=\"k\">if</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">transitions</span> <span class=\"o\">==</span> <span class=\"p\">{}):</span>\n",
" <span class=\"k\">raise</span> <span class=\"ne\">ValueError</span><span class=\"p\">(</span><span class=\"s2\">"Transition model is missing"</span><span class=\"p\">)</span>\n",
" <span class=\"k\">else</span><span class=\"p\">:</span>\n",
" <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">transitions</span><span class=\"p\">[</span><span class=\"n\">state</span><span class=\"p\">][</span><span class=\"n\">action</span><span class=\"p\">]</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">actions</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">state</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">"""Set of actions that can be performed in this state. By default, a</span>\n",
"<span class=\"sd\"> fixed list of actions, except for terminal states. Override this</span>\n",
"<span class=\"sd\"> method if you need to specialize by state."""</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">state</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">terminals</span><span class=\"p\">:</span>\n",
" <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"bp\">None</span><span class=\"p\">]</span>\n",
" <span class=\"k\">else</span><span class=\"p\">:</span>\n",
" <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">actlist</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
Aman Deep Singh
a validé
"psource(MDP)"
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
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The **_ _init_ _** method takes in the following parameters:\n",
"\n",
"- init: the initial state.\n",
"- actlist: List of actions possible in each state.\n",
"- terminals: List of terminal states where only possible action is exit\n",
"- gamma: Discounting factor. This makes sure that delayed rewards have less value compared to immediate ones.\n",
"\n",
"**R** method returns the reward for each state by using the self.reward dict.\n",
"\n",
"**T** method is not implemented and is somewhat different from the text. Here we return (probability, s') pairs where s' belongs to list of possible state by taking action a in state s.\n",
"\n",
"**actions** method returns list of actions possible in each state. By default it returns all actions for states other than terminal states.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let us implement the simple MDP in the image below. States A, B have actions X, Y available in them. Their probabilities are shown just above the arrows. We start with using MDP as base class for our CustomMDP. Obviously we need to make a few changes to suit our case. We make use of a transition matrix as our transitions are not very simple.\n",
"<img src=\"files/images/mdp-a.png\">"
]
},
{
"cell_type": "code",
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
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Transition Matrix as nested dict. State -> Actions in state -> States by each action -> Probabilty\n",
"t = {\n",
" \"A\": {\n",
" \"X\": {\"A\":0.3, \"B\":0.7},\n",
" \"Y\": {\"A\":1.0}\n",
" },\n",
" \"B\": {\n",
" \"X\": {\"End\":0.8, \"B\":0.2},\n",
" \"Y\": {\"A\":1.0}\n",
" },\n",
" \"End\": {}\n",
"}\n",
"\n",
"init = \"A\"\n",
"\n",
"terminals = [\"End\"]\n",
"\n",
"rewards = {\n",
" \"A\": 5,\n",
" \"B\": -10,\n",
" \"End\": 100\n",
"}"
]
},
{
"cell_type": "code",
},
"outputs": [],
"source": [
"class CustomMDP(MDP):\n",
"\n",
" def __init__(self, transition_matrix, rewards, terminals, init, gamma=.9):\n",
" # All possible actions.\n",
" actlist = []\n",
" for state in transition_matrix.keys():\n",
" actlist.extend(transition_matrix[state])\n",
" actlist = list(set(actlist))\n",
"\n",
" MDP.__init__(self, init, actlist, terminals=terminals, gamma=gamma)\n",
" self.t = transition_matrix\n",
" self.reward = rewards\n",
" for state in self.t:\n",
" self.states.add(state)\n",
"\n",
" def T(self, state, action):\n",
" if action is None:\n",
" return [(0.0, state)]\n",
" else: \n",
" return [(prob, new_state) for new_state, prob in self.t[state][action].items()]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally we instantize the class with the parameters for our MDP in the picture."
]
},
{
"cell_type": "code",
"our_mdp = CustomMDP(t, rewards, terminals, init, gamma=.9)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With this we have successfully represented our MDP. Later we will look at ways to solve this MDP."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we look at a concrete implementation that makes use of the MDP as base class. The GridMDP class in the mdp module is used to represent a grid world MDP like the one shown in in **Fig 17.1** of the AIMA Book. We assume for now that the environment is _fully observable_, so that the agent always knows where it is. The code should be easy to understand if you have gone through the CustomMDP example."
SnShine
a validé
"execution_count": 6,
Aman Deep Singh
a validé
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
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">class</span> <span class=\"nc\">GridMDP</span><span class=\"p\">(</span><span class=\"n\">MDP</span><span class=\"p\">):</span>\n",
"\n",
" <span class=\"sd\">"""A two-dimensional grid MDP, as in [Figure 17.1]. All you have to do is</span>\n",
"<span class=\"sd\"> specify the grid as a list of lists of rewards; use None for an obstacle</span>\n",
"<span class=\"sd\"> (unreachable state). Also, you should specify the terminal states.</span>\n",
"<span class=\"sd\"> An action is an (x, y) unit vector; e.g. (1, 0) means move east."""</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">grid</span><span class=\"p\">,</span> <span class=\"n\">terminals</span><span class=\"p\">,</span> <span class=\"n\">init</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"mi\">0</span><span class=\"p\">),</span> <span class=\"n\">gamma</span><span class=\"o\">=.</span><span class=\"mi\">9</span><span class=\"p\">):</span>\n",
" <span class=\"n\">grid</span><span class=\"o\">.</span><span class=\"n\">reverse</span><span class=\"p\">()</span> <span class=\"c1\"># because we want row 0 on bottom, not on top</span>\n",
" <span class=\"n\">MDP</span><span class=\"o\">.</span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">init</span><span class=\"p\">,</span> <span class=\"n\">actlist</span><span class=\"o\">=</span><span class=\"n\">orientations</span><span class=\"p\">,</span>\n",
" <span class=\"n\">terminals</span><span class=\"o\">=</span><span class=\"n\">terminals</span><span class=\"p\">,</span> <span class=\"n\">gamma</span><span class=\"o\">=</span><span class=\"n\">gamma</span><span class=\"p\">)</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">grid</span> <span class=\"o\">=</span> <span class=\"n\">grid</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">rows</span> <span class=\"o\">=</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">grid</span><span class=\"p\">)</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">cols</span> <span class=\"o\">=</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">grid</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">])</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">x</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">cols</span><span class=\"p\">):</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">y</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">rows</span><span class=\"p\">):</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">reward</span><span class=\"p\">[</span><span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">y</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">grid</span><span class=\"p\">[</span><span class=\"n\">y</span><span class=\"p\">][</span><span class=\"n\">x</span><span class=\"p\">]</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">grid</span><span class=\"p\">[</span><span class=\"n\">y</span><span class=\"p\">][</span><span class=\"n\">x</span><span class=\"p\">]</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">states</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">((</span><span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">y</span><span class=\"p\">))</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">T</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">state</span><span class=\"p\">,</span> <span class=\"n\">action</span><span class=\"p\">):</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">action</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>\n",
" <span class=\"k\">return</span> <span class=\"p\">[(</span><span class=\"mf\">0.0</span><span class=\"p\">,</span> <span class=\"n\">state</span><span class=\"p\">)]</span>\n",
" <span class=\"k\">else</span><span class=\"p\">:</span>\n",
" <span class=\"k\">return</span> <span class=\"p\">[(</span><span class=\"mf\">0.8</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">go</span><span class=\"p\">(</span><span class=\"n\">state</span><span class=\"p\">,</span> <span class=\"n\">action</span><span class=\"p\">)),</span>\n",
" <span class=\"p\">(</span><span class=\"mf\">0.1</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">go</span><span class=\"p\">(</span><span class=\"n\">state</span><span class=\"p\">,</span> <span class=\"n\">turn_right</span><span class=\"p\">(</span><span class=\"n\">action</span><span class=\"p\">))),</span>\n",
" <span class=\"p\">(</span><span class=\"mf\">0.1</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">go</span><span class=\"p\">(</span><span class=\"n\">state</span><span class=\"p\">,</span> <span class=\"n\">turn_left</span><span class=\"p\">(</span><span class=\"n\">action</span><span class=\"p\">)))]</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">go</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">state</span><span class=\"p\">,</span> <span class=\"n\">direction</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">"""Return the state that results from going in this direction."""</span>\n",
" <span class=\"n\">state1</span> <span class=\"o\">=</span> <span class=\"n\">vector_add</span><span class=\"p\">(</span><span class=\"n\">state</span><span class=\"p\">,</span> <span class=\"n\">direction</span><span class=\"p\">)</span>\n",
" <span class=\"k\">return</span> <span class=\"n\">state1</span> <span class=\"k\">if</span> <span class=\"n\">state1</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">states</span> <span class=\"k\">else</span> <span class=\"n\">state</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">to_grid</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">mapping</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">"""Convert a mapping from (x, y) to v into a [[..., v, ...]] grid."""</span>\n",
" <span class=\"k\">return</span> <span class=\"nb\">list</span><span class=\"p\">(</span><span class=\"nb\">reversed</span><span class=\"p\">([[</span><span class=\"n\">mapping</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">((</span><span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">y</span><span class=\"p\">),</span> <span class=\"bp\">None</span><span class=\"p\">)</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">x</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">cols</span><span class=\"p\">)]</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">y</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">rows</span><span class=\"p\">)]))</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">to_arrows</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">policy</span><span class=\"p\">):</span>\n",
" <span class=\"n\">chars</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n",
" <span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"mi\">0</span><span class=\"p\">):</span> <span class=\"s1\">'>'</span><span class=\"p\">,</span> <span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"mi\">1</span><span class=\"p\">):</span> <span class=\"s1\">'^'</span><span class=\"p\">,</span> <span class=\"p\">(</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"mi\">0</span><span class=\"p\">):</span> <span class=\"s1\">'<'</span><span class=\"p\">,</span> <span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">):</span> <span class=\"s1\">'v'</span><span class=\"p\">,</span> <span class=\"bp\">None</span><span class=\"p\">:</span> <span class=\"s1\">'.'</span><span class=\"p\">}</span>\n",
" <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">to_grid</span><span class=\"p\">({</span><span class=\"n\">s</span><span class=\"p\">:</span> <span class=\"n\">chars</span><span class=\"p\">[</span><span class=\"n\">a</span><span class=\"p\">]</span> <span class=\"k\">for</span> <span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">,</span> <span class=\"n\">a</span><span class=\"p\">)</span> <span class=\"ow\">in</span> <span class=\"n\">policy</span><span class=\"o\">.</span><span class=\"n\">items</span><span class=\"p\">()})</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
Aman Deep Singh
a validé
"psource(GridMDP)"
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
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The **_ _init_ _** method takes **grid** as an extra parameter compared to the MDP class. The grid is a nested list of rewards in states.\n",
"\n",
"**go** method returns the state by going in particular direction by using vector_add.\n",
"\n",
"**T** method is not implemented and is somewhat different from the text. Here we return (probability, s') pairs where s' belongs to list of possible state by taking action a in state s.\n",
"\n",
"**actions** method returns list of actions possible in each state. By default it returns all actions for states other than terminal states.\n",
"\n",
"**to_arrows** are used for representing the policy in a grid like format."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can create a GridMDP like the one in **Fig 17.1** as follows: \n",
"\n",
" GridMDP([[-0.04, -0.04, -0.04, +1],\n",
" [-0.04, None, -0.04, -1],\n",
" [-0.04, -0.04, -0.04, -0.04]],\n",
" terminals=[(3, 2), (3, 1)])\n",
" \n",
"In fact the **sequential_decision_environment** in mdp module has been instantized using the exact same code."
]
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sequential_decision_environment"
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"\n",
"Now that we have looked how to represent MDPs. Let's aim at solving them. Our ultimate goal is to obtain an optimal policy. We start with looking at Value Iteration and a visualisation that should help us understanding it better.\n",
"\n",
"We start by calculating Value/Utility for each of the states. The Value of each state is the expected sum of discounted future rewards given we start in that state and follow a particular policy _pi_. The value or the utility of a state is given by\n",
"\n",
"$$U(s)=R(s)+\\gamma\\max_{a\\epsilon A(s)}\\sum_{s'} P(s'\\ |\\ s,a)U(s')$$\n",
"\n",
"This is called the Bellman equation. The algorithm Value Iteration (**Fig. 17.4** in the book) relies on finding solutions of this Equation. The intuition Value Iteration works is because values propagate through the state space by means of local updates. This point will we more clear after we encounter the visualisation. For more information you can refer to **Section 17.2** of the book. \n"
]
},
{
"cell_type": "code",
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
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">value_iteration</span><span class=\"p\">(</span><span class=\"n\">mdp</span><span class=\"p\">,</span> <span class=\"n\">epsilon</span><span class=\"o\">=</span><span class=\"mf\">0.001</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">"""Solving an MDP by value iteration. [Figure 17.4]"""</span>\n",
" <span class=\"n\">U1</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"n\">s</span><span class=\"p\">:</span> <span class=\"mi\">0</span> <span class=\"k\">for</span> <span class=\"n\">s</span> <span class=\"ow\">in</span> <span class=\"n\">mdp</span><span class=\"o\">.</span><span class=\"n\">states</span><span class=\"p\">}</span>\n",
" <span class=\"n\">R</span><span class=\"p\">,</span> <span class=\"n\">T</span><span class=\"p\">,</span> <span class=\"n\">gamma</span> <span class=\"o\">=</span> <span class=\"n\">mdp</span><span class=\"o\">.</span><span class=\"n\">R</span><span class=\"p\">,</span> <span class=\"n\">mdp</span><span class=\"o\">.</span><span class=\"n\">T</span><span class=\"p\">,</span> <span class=\"n\">mdp</span><span class=\"o\">.</span><span class=\"n\">gamma</span>\n",
" <span class=\"k\">while</span> <span class=\"bp\">True</span><span class=\"p\">:</span>\n",
" <span class=\"n\">U</span> <span class=\"o\">=</span> <span class=\"n\">U1</span><span class=\"o\">.</span><span class=\"n\">copy</span><span class=\"p\">()</span>\n",
" <span class=\"n\">delta</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">s</span> <span class=\"ow\">in</span> <span class=\"n\">mdp</span><span class=\"o\">.</span><span class=\"n\">states</span><span class=\"p\">:</span>\n",
" <span class=\"n\">U1</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">R</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">)</span> <span class=\"o\">+</span> <span class=\"n\">gamma</span> <span class=\"o\">*</span> <span class=\"nb\">max</span><span class=\"p\">([</span><span class=\"nb\">sum</span><span class=\"p\">([</span><span class=\"n\">p</span> <span class=\"o\">*</span> <span class=\"n\">U</span><span class=\"p\">[</span><span class=\"n\">s1</span><span class=\"p\">]</span> <span class=\"k\">for</span> <span class=\"p\">(</span><span class=\"n\">p</span><span class=\"p\">,</span> <span class=\"n\">s1</span><span class=\"p\">)</span> <span class=\"ow\">in</span> <span class=\"n\">T</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">,</span> <span class=\"n\">a</span><span class=\"p\">)])</span>\n",
" <span class=\"k\">for</span> <span class=\"n\">a</span> <span class=\"ow\">in</span> <span class=\"n\">mdp</span><span class=\"o\">.</span><span class=\"n\">actions</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">)])</span>\n",
" <span class=\"n\">delta</span> <span class=\"o\">=</span> <span class=\"nb\">max</span><span class=\"p\">(</span><span class=\"n\">delta</span><span class=\"p\">,</span> <span class=\"nb\">abs</span><span class=\"p\">(</span><span class=\"n\">U1</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span> <span class=\"o\">-</span> <span class=\"n\">U</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]))</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">delta</span> <span class=\"o\"><</span> <span class=\"n\">epsilon</span> <span class=\"o\">*</span> <span class=\"p\">(</span><span class=\"mi\">1</span> <span class=\"o\">-</span> <span class=\"n\">gamma</span><span class=\"p\">)</span> <span class=\"o\">/</span> <span class=\"n\">gamma</span><span class=\"p\">:</span>\n",
" <span class=\"k\">return</span> <span class=\"n\">U</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It takes as inputs two parameters, an MDP to solve and epsilon the maximum error allowed in the utility of any state. It returns a dictionary containing utilities where the keys are the states and values represent utilities. <br> Value Iteration starts with arbitrary initial values for the utilities, calculates the right side of the Bellman equation and plugs it into the left hand side, thereby updating the utility of each state from the utilities of its neighbors. \n",
"This is repeated until equilibrium is reached. \n",
"It works on the principle of _Dynamic Programming_. \n",
"If U_i(s) is the utility value for state _s_ at the _i_ th iteration, the iteration step, called Bellman update, looks like this:\n",
"\n",
"$$ U_{i+1}(s) \\leftarrow R(s) + \\gamma \\max_{a \\epsilon A(s)} \\sum_{s'} P(s'\\ |\\ s,a)U_{i}(s') $$\n",
"\n",
"As you might have noticed, `value_iteration` has an infinite loop. How do we decide when to stop iterating? \n",
"The concept of _contraction_ successfully explains the convergence of value iteration. \n",
"Refer to **Section 17.2.3** of the book for a detailed explanation. \n",
"In the algorithm, we calculate a value _delta_ that measures the difference in the utilities of the current time step and the previous time step. \n",
"\n",
"$$\\delta = \\max{(\\delta, \\begin{vmatrix}U_{i + 1}(s) - U_i(s)\\end{vmatrix})}$$\n",
"\n",
"This value of delta decreases over time.\n",
"We terminate the algorithm if the delta value is less than a threshold value determined by the hyperparameter _epsilon_.\n",
"\n",
"$$\\delta \\lt \\epsilon \\frac{(1 - \\gamma)}{\\gamma}$$\n",
"\n",
"To summarize, the Bellman update is a _contraction_ by a factor of `gamma` on the space of utility vectors. \n",
"Hence, from the properties of contractions in general, it follows that `value_iteration` always converges to a unique solution of the Bellman equations whenever gamma is less than 1.\n",
"We then terminate the algorithm when a reasonable approximation is achieved.\n",
"In practice, it often occurs that the policy _pi_ becomes optimal long before the utility function converges. For the given 4 x 3 environment with _gamma = 0.9_, the policy _pi_ is optimal when _i = 4_, even though the maximum error in the utility function is stil 0.46.This can be clarified from **figure 17.6** in the book. Hence, to increase computational efficiency, we often use another method to solve MDPs called Policy Iteration which we will see in the later part of this notebook. \n",
"<br>For now, let us solve the **sequential_decision_environment** GridMDP using `value_iteration`."
{
"cell_type": "code",
"outputs": [
{
"data": {
"text/plain": [
"{(0, 0): 0.2962883154554812,\n",
" (0, 1): 0.3984432178350045,\n",
" (0, 2): 0.5093943765842497,\n",
" (1, 0): 0.25386699846479516,\n",
" (1, 2): 0.649585681261095,\n",
" (2, 0): 0.3447542300124158,\n",
" (2, 1): 0.48644001739269643,\n",
" (2, 2): 0.7953620878466678,\n",
" (3, 0): 0.12987274656746342,\n",
" (3, 1): -1.0,\n",
" (3, 2): 1.0}"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"value_iteration(sequential_decision_environment)"
]
},
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
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The pseudocode for the algorithm:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"### AIMA3e\n",
"__function__ VALUE-ITERATION(_mdp_, _ε_) __returns__ a utility function \n",
" __inputs__: _mdp_, an MDP with states _S_, actions _A_(_s_), transition model _P_(_s′_ | _s_, _a_), \n",
"      rewards _R_(_s_), discount _γ_ \n",
"   _ε_, the maximum error allowed in the utility of any state \n",
" __local variables__: _U_, _U′_, vectors of utilities for states in _S_, initially zero \n",
"        _δ_, the maximum change in the utility of any state in an iteration \n",
"\n",
" __repeat__ \n",
"   _U_ ← _U′_; _δ_ ← 0 \n",
"   __for each__ state _s_ in _S_ __do__ \n",
"     _U′_\\[_s_\\] ← _R_(_s_) + _γ_ max<sub>_a_ ∈ _A_(_s_)</sub> Σ _P_(_s′_ | _s_, _a_) _U_\\[_s′_\\] \n",
"     __if__ | _U′_\\[_s_\\] − _U_\\[_s_\\] | > _δ_ __then__ _δ_ ← | _U′_\\[_s_\\] − _U_\\[_s_\\] | \n",
" __until__ _δ_ < _ε_(1 − _γ_)/_γ_ \n",
" __return__ _U_ \n",
"\n",
"---\n",
"__Figure ??__ The value iteration algorithm for calculating utilities of states. The termination condition is from Equation (__??__)."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pseudocode(\"Value-Iteration\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### AIMA3e\n",
"__function__ VALUE-ITERATION(_mdp_, _ε_) __returns__ a utility function \n",
" __inputs__: _mdp_, an MDP with states _S_, actions _A_(_s_), transition model _P_(_s′_ | _s_, _a_), \n",
"      rewards _R_(_s_), discount _γ_ \n",
"   _ε_, the maximum error allowed in the utility of any state \n",
" __local variables__: _U_, _U′_, vectors of utilities for states in _S_, initially zero \n",
"        _δ_, the maximum change in the utility of any state in an iteration \n",
"\n",
" __repeat__ \n",
"   _U_ ← _U′_; _δ_ ← 0 \n",
"   __for each__ state _s_ in _S_ __do__ \n",
"     _U′_\\[_s_\\] ← _R_(_s_) + _γ_ max<sub>_a_ ∈ _A_(_s_)</sub> Σ _P_(_s′_ | _s_, _a_) _U_\\[_s′_\\] \n",
"     __if__ | _U′_\\[_s_\\] − _U_\\[_s_\\] | > _δ_ __then__ _δ_ ← | _U′_\\[_s_\\] − _U_\\[_s_\\] | \n",
" __until__ _δ_ < _ε_(1 − _γ_)/_γ_ \n",
" __return__ _U_ \n",
"\n",
"---\n",
"__Figure ??__ The value iteration algorithm for calculating utilities of states. The termination condition is from Equation (__??__)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## VALUE ITERATION VISUALIZATION\n",
"\n",
"To illustrate that values propagate out of states let us create a simple visualisation. We will be using a modified version of the value_iteration function which will store U over time. We will also remove the parameter epsilon and instead add the number of iterations we want."
]
},
{
"cell_type": "code",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def value_iteration_instru(mdp, iterations=20):\n",
" U_over_time = []\n",
" U1 = {s: 0 for s in mdp.states}\n",
" R, T, gamma = mdp.R, mdp.T, mdp.gamma\n",
" for _ in range(iterations):\n",
" U = U1.copy()\n",
" for s in mdp.states:\n",
" U1[s] = R(s) + gamma * max([sum([p * U[s1] for (p, s1) in T(s, a)])\n",
" for a in mdp.actions(s)])\n",
" U_over_time.append(U)\n",
" return U_over_time"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we define a function to create the visualisation from the utilities returned by **value_iteration_instru**. The reader need not concern himself with the code that immediately follows as it is the usage of Matplotib with IPython Widgets. If you are interested in reading more about these visit [ipywidgets.readthedocs.io](http://ipywidgets.readthedocs.io)"
]
},
{
"cell_type": "code",
},
"outputs": [],
"source": [
"columns = 4\n",
"rows = 3\n",
"U_over_time = value_iteration_instru(sequential_decision_environment)"
]
},
{
"cell_type": "code",
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from notebook import make_plot_grid_step_function\n",
"\n",
"plot_grid_step = make_plot_grid_step_function(columns, rows, U_over_time)"
]
},
{
"cell_type": "code",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAATcAAADuCAYAAABcZEBhAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADVdJREFUeJzt239o2/edx/HX9+prSRfbbQqLrK9d2iKzcporX2kcnyAH\nV0i8/JjbP7pL/MfcboGQXEaYYab5Y1cYgbZXzuFwmgbcCyX5xwn0D3s4P6rQMAiInKCJ/pjDgWpk\nsL6KU9zN9Vw36WK++8OKUjeO5XWW9M17zwcY/NXnY/h834hnpUh1fN8XAFjzD9U+AACUA3EDYBJx\nA2AScQNgEnEDYBJxA2AScQNgEnEDYBJxA2BSzV+zeXZW/O8MQBmtrXWqfYTg8/0VDYlXbgBMIm4A\nTCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBM\nIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwi\nbgBMCmzcfN9Xb+8BxWIRtbc/p3T6ypL7rl79RBs3tigWi6i394B831+03t/fp9paR1NTU5U4dsUw\nn9KY0f39XNL3Jf3wPuu+pAOSIpKek/TNyZ2Q1Fz4OVHGM/6tAhu3ROKcxsYySqcz6u8fUE/PviX3\n9fTs05Ej7yudzmhsLKMLF84X13K5CV28mFBT05OVOnbFMJ/SmNH9vSbp/DLr5yRlCj8Dku5M7g+S\nfiPp/ySlCr//sWyn/NsENm5nzgyrq6tbjuOora1d09PTmpy8vmjP5OR1zczMqK2tXY7jqKurWyMj\nQ8X1gwd7dOjQO3Icp9LHLzvmUxozur9/lbRumfVhSd2SHEntkqYlXZf0kaTNhb99vPD7cpGspsDG\nLZ/35LpNxWvXbVQ+7y2xp7F4HQ7f3TMyMqxw2FVLS6wyB64w5lMaM/ruPElN37huLDx2v8eDqKba\nByiHubk59fW9qaGhRLWPEkjMpzRm9OAL1Cu3gYGjisdbFY+3KhRqkOdNFNc8L6dw2F20Pxx25Xm5\n4nU+v7Anmx3T+HhW8XhM0ehT8rycNm16XjduTFbsXsqB+ZTGjFaHK2niG9e5wmP3ezyIAhW3PXv2\nK5lMK5lMa8eOlzU4eFK+7yuVuqz6+nqFQg2L9odCDaqrq1MqdVm+72tw8KS2b39J0WiLstnPNDo6\nrtHRcbluoy5duqL160NVurPVwXxKY0aro1PSSS18anpZUr2kBkkdkhJa+BDhj4XfO6p0xlIC+7a0\no2ObEomzisUiWrPmUR079kFxLR5vVTKZliQdPvye9u59TTdvfqXNm7dqy5at1TpyRTGf0pjR/XVJ\n+p2kKS38u9lvJP25sLZX0jZJZ7XwVZBHJd2Z3DpJ/ylpQ+H6DS3/wUQ1Od/+Ts9yZme18s0A/mpr\na219KlsWvr+iIQXqbSkArBbiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk\n4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTi\nBsAk4gbAJOIGwCTiBsAk4gbAJOIGwKSaah/AkrXf86t9hMCb/dKp9hECzRHPoVJWOiFeuQEwibgB\nMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEw\nibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJ\nuAEwKbBx831fvb0HFItF1N7+nNLpK0vuu3r1E23c2KJYLKLe3gPyfX/Ren9/n2prHU1NTVXi2BVz\n/vx5/eDZZxVpbtbbb799z/qtW7e0c9cuRZqbtbG9XePj48W1t956S5HmZv3g2Wf10UcfVfDUlcVz\nqJT/l/Qvkh6R9N/L7MtK2igpImmnpK8Lj98qXEcK6+PlOuh3Eti4JRLnNDaWUTqdUX//gHp69i25\nr6dnn44ceV/pdEZjYxlduHC+uJbLTejixYSamp6s1LErYn5+Xvt/8QudO3tW10ZHNXjqlK5du7Zo\nz/Hjx/X4Y4/p00xGPb/8pV4/eFCSdO3aNZ06fVqjv/+9zp87p//Yv1/z8/PVuI2y4zlUyjpJ/ZJ+\nVWLf65J6JH0q6XFJxwuPHy9cf1pYf708x/yOAhu3M2eG1dXVLcdx1NbWrunpaU1OXl+0Z3LyumZm\nZtTW1i7HcdTV1a2RkaHi+sGDPTp06B05jlPp45dVKpVSJBLRM888o4cffli7du7U8PDwoj3Dv/2t\nXn31VUnSK6+8oo8//li+72t4eFi7du7UI488oqefflqRSESpVKoat1F2PIdK+b6kDZL+cZk9vqSL\nkl4pXL8q6c58hgvXKqx/XNgfDIGNWz7vyXWbiteu26h83ltiT2PxOhy+u2dkZFjhsKuWllhlDlxB\nnuepqfHufTc2NsrzvHv3NC3Mr6amRvX19fr8888XPS5Jja57z99awXNoNXwu6TFJNYXrRkl3ZuhJ\nujPfGkn1hf3BUFN6y4Nnbm5OfX1vamgoUe2j4AHFc+jBF6hXbgMDRxWPtyoeb1Uo1CDPmyiueV5O\n4bC7aH847MrzcsXrfH5hTzY7pvHxrOLxmKLRp+R5OW3a9Lxu3Jis2L2Uk+u6msjdve9cLifXde/d\nM7Ewv9u3b+uLL77QE088sehxScp53j1/+yDjOVTKUUmthZ/8CvY/IWla0u3CdU7SnRm6ku7M97ak\nLwr7gyFQcduzZ7+SybSSybR27HhZg4Mn5fu+UqnLqq+vVyjUsGh/KNSguro6pVKX5fu+BgdPavv2\nlxSNtiib/Uyjo+MaHR2X6zbq0qUrWr8+VKU7W10bNmxQJpNRNpvV119/rVOnT6uzs3PRns4f/1gn\nTpyQJH344Yd68cUX5TiOOjs7der0ad26dUvZbFaZTEZtbW3VuI2y4DlUyn5J6cJPeAX7HUn/JunD\nwvUJSS8Vfu8sXKuw/mJhfzAE9m1pR8c2JRJnFYtFtGbNozp27IPiWjzeqmQyLUk6fPg97d37mm7e\n/EqbN2/Vli1bq3XkiqmpqdG7R46o40c/0vz8vH7+s58pGo3qjTfe0AsvvKDOzk7t3r1bP+3uVqS5\nWevWrdOpwUFJUjQa1b//5Cf6p2hUNTU1Ovruu3rooYeqfEflwXOolElJL0ia0cLrnP+RdE1SnaRt\nkv5XCwH8L0m7JP1a0j9L2l34+92SfqqFr4Ksk3Sqgmcvzfn2d3qWMzsboI9CAmjt9xhPKbNfBue/\n7EFUW1vtEwSf76/s5WGg3pYCwGohbgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwi\nbgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJu\nAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEyqqfYBLJn90qn2EfCA+9Ofqn0CO3jlBsAk4gbAJOIG\nwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbA\nJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk4gbAJOIGwCTiBsAk\n4gbApMDGzfd99fYeUCwWUXv7c0qnryy57+rVT7RxY4tisYh6ew/I9/1F6/39faqtdTQ1NVWJY1cM\n8ymNGS3P+nwCG7dE4pzGxjJKpzPq7x9QT8++Jff19OzTkSPvK53OaGwsowsXzhfXcrkJXbyYUFPT\nk5U6dsUwn9KY0fKszyewcTtzZlhdXd1yHEdtbe2anp7W5OT1RXsmJ69rZmZGbW3tchxHXV3dGhkZ\nKq4fPNijQ4fekeM4lT5+2TGf0pjR8qzPJ7Bxy+c9uW5T8dp1G5XPe0vsaSxeh8N394yMDCscdtXS\nEqvMgSuM+ZTGjJZnfT411T5AOczNzamv700NDSWqfZRAYj6lMaPlPQjzCdQrt4GBo4rHWxWPtyoU\napDnTRTXPC+ncNhdtD8cduV5ueJ1Pr+wJ5sd0/h4VvF4TNHoU/K8nDZtel43bkxW7F7KgfmUxoyW\n9/c0n0DFbc+e/Uom00om09qx42UNDp6U7/tKpS6rvr5eoVDDov2hUIPq6uqUSl2W7/saHDyp7dtf\nUjTaomz2M42Ojmt0dFyu26hLl65o/fpQle5sdTCf0pjR8v6e5hPYt6UdHduUSJxVLBbRmjWP6tix\nD4pr8Xirksm0JOnw4fe0d+9runnzK23evFVbtmyt1pErivmUxoyWZ30+zre/s7Kc2VmtfDMAlMHa\ntVrRR7OBelsKAKuFuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4\nATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgBMIm4ATCJuAEwibgB\nMIm4ATCJuAEwibgBMIm4ATDJ8X2/2mcAgFXHKzcAJhE3ACYRNwAmETcAJhE3ACYRNwAmETcAJhE3\nACYRNwAmETcAJv0F9s8EDYqi1wAAAAAASUVORK5CYII=\n",
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Widget Javascript not detected. It may not be installed or enabled properly.\n"
]
},
{
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import ipywidgets as widgets\n",
"from IPython.display import display\n",
"iteration_slider = widgets.IntSlider(min=1, max=15, step=1, value=0)\n",
"w=widgets.interactive(plot_grid_step,iteration=iteration_slider)\n",
"\n",
"visualize_callback = make_visualize(iteration_slider)\n",
"\n",
"visualize_button = widgets.ToggleButton(description = \"Visualize\", value = False)\n",
"time_select = widgets.ToggleButtons(description='Extra Delay:',options=['0', '0.1', '0.2', '0.5', '0.7', '1.0'])\n",
"a = widgets.interactive(visualize_callback, Visualize = visualize_button, time_step=time_select)\n",
"display(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Aman Deep Singh
a validé
"Move the slider above to observe how the utility changes across iterations. It is also possible to move the slider using arrow keys or to jump to the value by directly editing the number with a double click. The **Visualize Button** will automatically animate the slider for you. The **Extra Delay Box** allows you to set time delay in seconds upto one second for each time step. There is also an interactive editor for grid-world problems `grid_mdp.py` in the gui folder for you to play around with."
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# POLICY ITERATION\n",
"\n",
"We have already seen that value iteration converges to the optimal policy long before it accurately estimates the utility function. \n",
"If one action is clearly better than all the others, then the exact magnitude of the utilities in the states involved need not be precise. \n",
"The policy iteration algorithm works on this insight. \n",
"The algorithm executes two fundamental steps:\n",
"* **Policy evaluation**: Given a policy _πᵢ_, calculate _Uᵢ = U(πᵢ)_, the utility of each state if _πᵢ_ were to be executed.\n",
"* **Policy improvement**: Calculate a new policy _πᵢ₊₁_ using one-step look-ahead based on the utility values calculated.\n",
"\n",
"The algorithm terminates when the policy improvement step yields no change in the utilities. \n",
"Refer to **Figure 17.6** in the book to see how this is an improvement over value iteration.\n",
"We now have a simplified version of the Bellman equation\n",
"\n",
"$$U_i(s) = R(s) + \\gamma \\sum_{s'}P(s'\\ |\\ s, \\pi_i(s))U_i(s')$$\n",
"\n",
"An important observation in this equation is that this equation doesn't have the `max` operator, which makes it linear.\n",
"For _n_ states, we have _n_ linear equations with _n_ unknowns, which can be solved exactly in time _**O(n³)**_.\n",
"For more implementational details, have a look at **Section 17.3**.\n",
"Let us now look at how the expected utility is found and how `policy_iteration` is implemented."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",