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": [
SnShine
a validé
"from mdp import MDP, GridMDP, sequential_decision_environment, value_iteration"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Review\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."
]
},
{
"cell_type": "code",
SnShine
a validé
"execution_count": 2,
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
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%psource MDP"
]
},
{
"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",
SnShine
a validé
"execution_count": 3,
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
"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",
SnShine
a validé
"execution_count": 4,
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
"metadata": {
"collapsed": false
},
"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.keys())\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",
" return [(new_state, prob) 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",
SnShine
a validé
"execution_count": 5,
"our_mdp = CustomMDP(t, rewards, terminals, init, gamma=.9)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With this we have sucessfully represented our MDP. Later we will look at ways to solve this MDP."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Grid MDP\n",
"\n",
"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. The code should be easy to understand if you have gone through the CustomMDP example.\n",
"\n"
]
},
{
"cell_type": "code",
SnShine
a validé
"execution_count": 6,
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
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%psource GridMDP"
]
},
{
"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",
SnShine
a validé
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<mdp.GridMDP at 0x7f256c52c390>"
SnShine
a validé
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sequential_decision_environment"
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Value Iteration\n",
"\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 algorithm Value Iteration (**Fig. 17.4** in the book) relies on finding solutions of the Bellman's Equation. The intuition Value Iteration works is because values propagate. 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",
SnShine
a validé
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%psource value_iteration"
]
},
{
"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. Let us solve the **sequencial_decision_enviornment** GridMDP.\n"
]
},
{
"cell_type": "code",
SnShine
a validé
"execution_count": 9,
"metadata": {
"collapsed": false
},
"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}"
]
},
SnShine
a validé
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"value_iteration(sequential_decision_environment)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualization for Value Iteration\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",
SnShine
a validé
"execution_count": 10,
"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",
SnShine
a validé
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"from collections import defaultdict\n",
"\n",
"def make_plot_grid_step_function(columns, row, U_over_time):\n",
" '''ipywidgets interactive function supports\n",
" single parameter as input. This function\n",
" creates and return such a function by taking\n",
" in input other parameters\n",
" '''\n",
" def plot_grid_step(iteration):\n",
" data = U_over_time[iteration]\n",
" data = defaultdict(lambda: 0, data)\n",
" grid = []\n",
" for row in range(rows):\n",
" current_row = []\n",
" for column in range(columns):\n",
" current_row.append(data[(column, row)])\n",
" grid.append(current_row)\n",
" grid.reverse() # output like book\n",
" fig = plt.imshow(grid, cmap=plt.cm.bwr, interpolation='nearest')\n",
"\n",
" plt.axis('off')\n",
" fig.axes.get_xaxis().set_visible(False)\n",
" fig.axes.get_yaxis().set_visible(False)\n",
"\n",
" for col in range(len(grid)):\n",
" for row in range(len(grid[0])):\n",
" magic = grid[col][row]\n",
" fig.axes.text(row, col, \"{0:.2f}\".format(magic), va='center', ha='center')\n",
"\n",
" plt.show()\n",
" \n",
" return plot_grid_step\n",
"\n",
"def make_visualize(slider):\n",
" ''' Takes an input a slider and returns \n",
" callback function for timer and animation\n",
" '''\n",
" \n",
" def visualize_callback(Visualize, time_step):\n",
" if Visualize is True:\n",
" for i in range(slider.min, slider.max + 1):\n",
" slider.value = i\n",
" time.sleep(float(time_step))\n",
" \n",
" return visualize_callback\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 12,
},
"outputs": [],
"source": [
"columns = 4\n",
"rows = 3\n",
"U_over_time = value_iteration_instru(sequential_decision_environment)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plot_grid_step = make_plot_grid_step_function(columns, rows, U_over_time)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAATgAAADtCAYAAAAr+2lCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADM5JREFUeJzt2lFolGe+gPFn0ggH1pKEHPWQ0a2Cya7scpz1ECxyEETY\ngANGUKgNbEqoopbdhFKkXikKB9obRXSDVsqxWch2KdQG9cRVKAgKktYajAtdrWldndhIUxs3vRGZ\nOReJaULSONvqzPjv87txJu/7hTd/Ph8+JyZyuRySFFFZsQ8gSU+KgZMUloGTFJaBkxSWgZMUloGT\nFFb5TIsjI/h/SKQim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KS5f7p12z40bX7Bq\n1fOkUnW0tLzIgwcPJq1fvPgRVVWz6Op6vxBHLgrnlD9nlZ+IcyqpwJ0+3U1//3V6e6+xf/9h2tq2\nTrtv587XaW19jd7eq1RUVNLR8fb4WjabZdeuHaxe3VCoYxecc8qfs8pP1DmVVOBOnuyiqakZgPr6\n5dy7N8ydO4NT9p09+yGNjesBaGp6iePHj42vHTp0gHXrNjBnztzCHLoInFP+nFV+os6ppAI3MJAh\nmVww/r6mJsnAQGbSnqGhISorqygrGz16Mjmf27cHxq8/ceIDNm3aRi6XK9zBC8w55c9Z5SfqnEoq\ncD/Wjh2vsmfPmxO+UjqDLiXOKX/OKj+lOqfyYh/gyJF2jh49QiKRYNmyejKZm+NrmcwtamqSk/ZX\nV1czPPwN2WyWsrKySXsuXfqYlpaN5HI5hoa+4syZbsrLZ5FOry3oz/QkOKf8Oav8/BTmVPQnuM2b\nX+H8+UucO/cJ6XQjnZ0dAPT0XKCiopK5c+dNuWblylUcO/YeAJ2d75BONwLQ19dPX18/V658TmPj\nBvbubS/6gB8X55Q/Z5Wfn8Kcih64iRoa1rBw4SKWLl1MW9sW9u1rH19bvz7N4OCXAOze/QYHD+4l\nlarj7t2vaW5+ecr3SiQSBTt3oTmn/Dmr/ESdU2KmDwRHRkrkH9KSNIPZs5m2qiX1BCdJj5OBkxSW\ngZMUloGTFJaBkxSWgZMUloGTFJaBkxSWgZMUloGTFJaBkxSWgZMUloGTFJaBkxSWgZMUloGTFJaB\nkxSWgZMUloGTFJaBkxSWgZMUloGTFJaBkxSWgZMUloGTFJaBkxSWgZMUloGTFJaBkxSWgZMUloGT\nFJaBkxSWgZMUViKXyxX7DJL0RPgEJyksAycpLAMnKSwDJyksAycpLAMnKaz/B9v3wubCyTXSAAAA\nAElFTkSuQmCC\n",
"<matplotlib.figure.Figure at 0x7f256c48a9b0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import ipywidgets as widgets\n",
"from IPython.display import display\n",
"\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(desctiption = \"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": [
"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."
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
},
"widgets": {
"state": {
492
493
494
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
"001e6c8ed3fc4eeeb6ab7901992314dd": {
"views": []
},
"00f29880456846a8854ab515146ec55b": {
"views": []
},
"010f52f7cde545cba25593839002049b": {
"views": []
},
"01473ad99aa94acbaca856a7d980f2b9": {
"views": []
},
"021a4a4f35da484db5c37c5c8d0dbcc2": {
"views": []
},
"02229be5d3bc401fad55a0378977324a": {
"views": []
},
"022a5fdfc8e44fb09b21c4bd5b67a0db": {
"views": [
{
"cell_index": 27
}
]
},
"025c3b0250b94d4c8d9b33adfdba4c15": {
"views": []
},
"028f96abfed644b8b042be1e4b16014d": {
"views": []
},
"0303bad44d404a1b9ad2cc167e42fcb7": {
"views": []
},
"031d2d17f32347ec83c43798e05418fe": {
"views": []
},
"03de64f0c2fd43f1b3b5d84aa265aeb7": {
"views": []
},
"03fdd484675b42ad84448f64c459b0e0": {
"views": []
},
"044cf74f03fd44fd840e450e5ee0c161": {
"views": []
},
"054ae5ba0a014a758de446f1980f1ba5": {
"views": []
},
"0675230fb92f4539bc257b768fb4cd10": {
"views": [
{
"cell_index": 27
}
]
},
"06c93b34e1f4424aba9a0b172c428260": {
"views": []
},
"077a5ea324be46c3ad0110671a0c6a12": {
"views": []
},
"0781138d150142a08775861a69beaec9": {
"views": []
},
"0783e74a8c2b40cc9b0f5706271192f4": {
"views": [
{
"cell_index": 27
}
]
},
"07c7678b73634e728085f19d7b5b84f7": {
"views": []
},
"07febf1d15a140d8adb708847dd478ec": {
"views": []
},
"08299b681cd9477f9b19a125e186ce44": {
"views": []
},
"083af89d82e445aab4abddfece61d700": {
"views": []
},
"08a1129a8bd8486bbfe2c9e49226f618": {
"views": []
},
"08a2f800c0d540fdb24015156c7ffc15": {
"views": []
},
"097d8d0feccc4c76b87bbcb3f1ecece7": {
"views": []
},
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
"098f12158d844cdf89b29a4cd568fda0": {
"views": [
{
"cell_index": 27
}
]
},
"09e96f9d5d32453290af60fbd29ca155": {
"views": []
},
"0a2ec7c49dcd4f768194483c4f2e8813": {
"views": []
},
"0b1d6ed8fe4144b8a24228e1befe2084": {
"views": []
},
"0b299f8157d24fa9830653a394ef806a": {
"views": []
},
"0b2a4ac81a244ff1a7b313290465f8f4": {
"views": []
},
"0b52cfc02d604bc2ae42f4ba8c7bca4f": {
"views": []
},
"0b65fb781274495ab498ad518bc274d4": {
"views": [
{
"cell_index": 27
}
]
},
"0b865813de0841c49b41f6ad5fb85c6a": {
"views": []
},
"0c2070d20fb04864aeb2008a6f2b8b30": {
"views": []
},
"0cf5319bcde84f65a1a91c5f9be3aa28": {
"views": []
},
"0d721b5be85f4f8aafe26b3597242d60": {
"views": []
},
"0d9f29e197ad45d6a04bbb6864d3be6d": {
"views": []
},
"0e03c7e2c0414936b206ed055e19acba": {
"views": []
},
"0e2265aa506a4778bfc480d5e48c388b": {
"views": []
},
"0e4e3d0b6afc413e86970ec4250df678": {
"views": []
},
"0e6a5fe6423542e6a13e30f8929a8b02": {
"views": []
},
"0e7b2f39c94343c3b0d3b6611351886e": {
"views": []
},
"0eb5005fa34440988bcf3be231d31511": {
"views": []
},
"104703ad808e41bc9106829bb0396ece": {
"views": []
},
"109c376b28774a78bf90d3da4587d834": {
"views": []
},
"10b24041718843da976ac616e77ea522": {
"views": []
},
"11516bb6db8b45ef866bd9be8bb59312": {
"views": []
},
"1203903354fa467a8f38dbbad79cbc81": {
"views": []
},
"124ecbe68ada40f68d6a1807ad6bcdf9": {
"views": []
},
"1264becdbb63455183aa75f236a3413e": {
"views": []
},
"13061cc21693480a8380346277c1b877": {
"views": []
},
"130dd4d2c9f04ad28d9a6ac40045a329": {
"views": []
},
"1350a087b5a9422386c3c5f04dd5d1c9": {
"views": []
},
"139bd19be4a4427a9e08f0be6080188e": {
"views": []
},
"13f9f589d36c477f9b597dda459efd16": {
"views": []
},
"140917b5c77348ec82ea45da139a3045": {
"views": []
},
"145419657bb1401ba934e6cea43d5fd1": {
"views": []
},
"15d748f1629d4da1982cd62cfbcb1725": {
"views": []
},
"17ad015dbc744ac6952d2a6da89f0289": {
"views": []
},
"17b6508f32e4425e9f43e5407eb55ed3": {
"views": []
},
"185598d8e5fc4dffae293f270a6e7328": {
"views": []
},
"196473b25f384f3895ee245e8b7874e9": {
"views": []
},
"19c0f87663a0431285a62d4ad6748046": {
"views": []
},
"1a00a7b7446d4ad8b08c9a2a9ea9c852": {
"views": []
},
"1a97f5b88cdc4ae0871578c06bbb9965": {
"views": []
},
"1a9a07777b0c4a45b33e25a70ebdc290": {
"views": []
},
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
"1af711fe8e4f43f084cef6c89eec40ae": {
"views": [
{
"cell_index": 27
}
]
},
"1aff6a6e15b34bb89d7579d445071230": {
"views": []
},
"1b1ea7e915d846aea9efeae4381b2c48": {
"views": []
},
"1ba02ae1967740b0a69e07dbe95635cb": {
"views": []
},
"1c5c913acbde4e87a163abb2e24e6e38": {
"views": [
{
"cell_index": 27
}
]
},
"1cfca0b7ef754c459e1ad97c1f0ceb3b": {
"views": []
},
"1d8f6a4910e649589863b781aab4c4d4": {
"views": []
},
"1e64b8f5a1554a22992693c194f7b971": {
"views": []
},
"1e8f0a2bf7614443a380e53ed27b48c0": {
"views": []
},
"1f4e6fa4bacc479e8cd997b26a5af733": {
"views": []
},
"1fdf09158eb44415a946f07c6aaba620": {
"views": []
},
"200e3ebead3d4858a47e2f6d345ca395": {
"views": [
{
"cell_index": 27
}
]
},
"2050d4b462474a059f9e6493ba06ac58": {
"views": []
},
"20b5c21a6e6a427ba3b9b55a0214f75e": {
"views": []
},
"20b99631feba4a9c98c9d5f74c620273": {
"views": []
},
"20bcff5082854ab89a7977ae56983e30": {
"views": []
},
"20d708bf9b7845fa946f5f37c7733fee": {
"views": []
},
"210b36ea9edf4ee49ae1ae3fe5005282": {
"views": []
},
"21415393cb2d4f72b5c3f5c058aeaf66": {
"views": []
},
"2186a18b6ed8405a8a720bae59de2ace": {
"views": []
},
"220dc13e9b6942a7b9ed9e37d5ede7ba": {
"views": []
},
"221a735fa6014a288543e6f8c7e4e2ef": {
"views": []
},
"2288929cec4d4c8faad411029f5e21fa": {
"views": []
},
"22b86e207ea6469d85d8333870851a86": {
"views": []
},
"23283ad662a140e3b5e8677499e91d64": {
"views": []
},
"23a7cc820b63454ca6be3dcfd2538ac1": {
"views": []
},
"240ed02d576546028af3edfab9ea8558": {
"views": []
},
"24678e52a0334cb9a9a56f92c29750be": {
"views": []
},
"247820f6d83f4dd9b68f5df77dbda4b7": {
"views": []
},
"24b6a837fbd942c9a68218fb8910dcd5": {
"views": []
},
"24ee3204f26348bca5e6a264973e5b56": {
"views": []
},
"262c7bb5bd7447f791509571fe74ae44": {
"views": []
},
"263595f22d0d45e2a850854bcefe4731": {
"views": []
},
"2640720aa6684c5da6d7870abcbc950b": {
"views": []
},
"265ca1ec7ad742f096bb8104d0cf1550": {
"views": []
},
"26bf66fba453464fac2f5cd362655083": {
"views": []
},
"29769879478f49e8b4afd5c0b4662e87": {
"views": []
},
"29a13bd6bc8d486ca648bf30c9e4c2a6": {
"views": []
},
"29c5df6267584654b76205fc5559c553": {
"views": []
},
"29ce25045e7248e5892e8aafc635c416": {
"views": []
},
"2a17207c43c9424394299a7b52461794": {
"views": []
},
"2a777941580945bc83ddb0c817ed4122": {
"views": []
},
"2ae1844e2afe416183658d7a602e5963": {
"views": []
},
"2afa2938b41944cf8c14e41a431e3969": {
"views": []
},
"2bdc5f9b161548e3aab8ea392b5af1a1": {
"views": []
},
"2c26b2bcfc96473584930a4b622d268e": {
"views": []
},
"2ca2a914a5f940b18df0b5cde2b79e4b": {
"views": []
},
"2ca2c532840548a9968d1c6b2f0acdd8": {
"views": []
},
"2d17c32bfea143babe2b114d8777b15d": {
"views": []
},
"2d3acd8872c342eab3484302cac2cb05": {
"views": [
{
"cell_index": 27
}
]
},
"2dc514cc2f5547aeb97059a5070dc9e3": {
"views": []
},
"2e1351ad05384d058c90e594bc6143c1": {
"views": [
{
"cell_index": 27
}
]
},
"2e9b80fa18984615933e41c1c1db2171": {
"views": []
},
"2ef17ee6b7c74a4bbbbbe9b1a93e4fb6": {
"views": []
},
"2f5438f1b34046a597a467effd43df11": {
"views": [
{
"cell_index": 27
}
]
},
"2f8d22417f3e421f96027fca40e1554f": {
"views": []
},
"2fb0409cfb49469d89a32597dc3edba9": {
"views": []
},
"303ccef837984c97b7e71f2988c737a4": {
"views": []
},
"3058b0808dca48a0bba9a93682260491": {
"views": []
},
"306b65493c28411eb10ad786bbf85dc5": {
"views": []
},
"30f5d30cf2d84530b3199015c5ff00eb": {
"views": []
},
"310b1ac518bd4079bdb7ecaf523a6809": {
"views": []
},
"313eca81d9d24664bcc837db54d59618": {
"views": []
},
"31413caf78c14548baa61e3e3c9edc55": {
"views": []
},
"317fbd3cb6324b2fbdfd6aa46a8d1192": {
"views": []
},
"319425ba805346f5ba366c42e220f9c6": {
"views": [
{
"cell_index": 27
}
]
},
"31fc8165275e473f8f75c6215b5184ff": {
"views": []
},
"329f12edaa0c44d2a619450f188e8777": {
"views": []
},
"32edf057582f4a6ca30ce3cb685bf971": {
"views": []
},
"330e74773ba148e18674cfa3e63cd6cc": {
"views": []
},
"332a89c03bfb49c2bb291051d172b735": {
"views": [
{
"cell_index": 27
}
]
},
"3347dfda0aca450f89dd9b39ca1bec7d": {
"views": []
},
"336e8bcfd7cc4a85956674b0c7bffff2": {
"views": []
},
"3376228b3b614d4ab2a10b2fd0f484fd": {
"views": []
},
"3380a22bc67c4be99c61050800f93395": {
"views": []
},
"34b5c16cbea448809c2ccbce56f8d5a5": {
"views": []
},
"34bb050223504afc8053ce931103f52c": {
"views": []
},
"34c28187175d49198b536a1ab13668c4": {
"views": []
},
"3521f32644514ecf9a96ddfa5d80fb9b": {
"views": []
},
"36511bd77ed74f668053df749cc735d4": {
"views": []
},
"36541c3490bd4268b64daf20d8c24124": {
"views": []
},
"37aa1dd4d76a4bac98857b519b7b523a": {
"views": []
},
"37aa3cfa3f8f48989091ec46ac17ae48": {
"views": []
},
"386991b0b1424a9c816dac6a29e1206b": {