-
Notifications
You must be signed in to change notification settings - Fork 152
/
_AIMA-4x3-gridworld.html
810 lines (781 loc) · 63.5 KB
/
_AIMA-4x3-gridworld.html
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
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
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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
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
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.433">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>aima-4x3-gridworld</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="_AIMA-4x3-gridworld_files/libs/clipboard/clipboard.min.js"></script>
<script src="_AIMA-4x3-gridworld_files/libs/quarto-html/quarto.js"></script>
<script src="_AIMA-4x3-gridworld_files/libs/quarto-html/popper.min.js"></script>
<script src="_AIMA-4x3-gridworld_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="_AIMA-4x3-gridworld_files/libs/quarto-html/anchor.min.js"></script>
<link href="_AIMA-4x3-gridworld_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="_AIMA-4x3-gridworld_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="_AIMA-4x3-gridworld_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="_AIMA-4x3-gridworld_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="_AIMA-4x3-gridworld_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<section id="the-4x3-grid-world-example" class="level1">
<h1>The 4x3 Grid World Example</h1>
<p>The used example is the simple 4x3 grid world described in AIMA Figure 12.1 and used again in 22.1 as:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="figures/AIMA_Figure_17_1.png" class="img-fluid figure-img"></p>
<figcaption class="figure-caption">AIMA Figure 17.1: (a) A simple, stochastic <span class="math inline">\(4 \times 3\)</span> environment that presents the agent with a sequential decision problem. (b) Illustration of the transition model of the environment: the “intended” outcome occurs with probability 0.8, but with probability 0.2 the agent moves at right angles to the intended direction. A collision with a wall results in no movement. Transitions into the two terminal states have reward +1 and -1, respectively, and all other transitions have a reward of -0.04.</figcaption>
</figure>
</div>
<p>In the following, we implement states, actions, the reward function,and the transition model. We also show how to represent a policy and how to estimate the expected utility using simulation.</p>
<section id="states" class="level2">
<h2 class="anchored" data-anchor-id="states">States</h2>
<p>We define the atomic state space <span class="math inline">\(S\)</span> by labeling the states <span class="math inline">\(1, 2, ...\)</span>. We convert coordinates <code>(rows, columns)</code> to the state label.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># I use capitalized variables as global constants</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>COLS <span class="ot"><-</span> <span class="dv">4</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>ROWS <span class="ot"><-</span> <span class="dv">3</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>S <span class="ot">=</span> <span class="fu">seq_len</span>(ROWS <span class="sc">*</span> COLS)</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a>LAYOUT <span class="ot"><-</span> <span class="fu">matrix</span>(S, <span class="at">nrow =</span> ROWS , <span class="at">ncol =</span> COLS)</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>LAYOUT</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12</code></pre>
</div>
</div>
<p>Note that the rows are displayed upside-down compared to the text book, so we use a function to display them in reverse order.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>show_layout <span class="ot"><-</span> <span class="cf">function</span>(x) {</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> x <span class="ot"><-</span> <span class="fu">matrix</span>(x, <span class="at">ncol =</span> COLS, <span class="at">nrow =</span> ROWS, </span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="at">dimnames =</span> <span class="fu">list</span>(<span class="at">row =</span> <span class="fu">seq_len</span>(ROWS), <span class="at">col =</span> <span class="fu">seq_len</span>(COLS)))</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> x[<span class="fu">rev</span>(<span class="fu">seq_len</span>(ROWS)), ]</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="fu">show_layout</span>(LAYOUT)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> col
row 1 2 3 4
3 3 6 9 12
2 2 5 8 11
1 1 4 7 10</code></pre>
</div>
</div>
<p>Convert between coordinates and state labels.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>rc_to_s <span class="ot"><-</span> <span class="cf">function</span>(rc)</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> LAYOUT[<span class="fu">rbind</span>(rc)]</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>s_to_rc <span class="ot"><-</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">function</span>(s)</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">drop</span>(<span class="fu">which</span>(LAYOUT <span class="sc">==</span> s, <span class="at">arr.ind =</span> <span class="cn">TRUE</span>, <span class="at">useNames =</span> <span class="cn">FALSE</span>))</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a><span class="fu">rc_to_s</span>(<span class="fu">c</span>(<span class="dv">3</span>, <span class="dv">4</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 12</code></pre>
</div>
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">s_to_rc</span>(<span class="dv">12</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3 4</code></pre>
</div>
</div>
<p>Start state</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>start <span class="ot"><-</span> 1L</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Define terminal states.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>is_terminal <span class="ot"><-</span> <span class="cf">function</span>(s) s <span class="sc">%in%</span> <span class="fu">c</span>(<span class="dv">5</span>, <span class="dv">11</span>, <span class="dv">12</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="actions" class="level2">
<h2 class="anchored" data-anchor-id="actions">Actions</h2>
<p>The complete set of actions is <span class="math inline">\(A = \{\mathrm{'Up', 'Right', 'Down', 'Left', 'None'}\}\)</span>. Not all actions are available in every state. Also, action <code>None</code> is added as the only possible action in an absorbing state.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>A <span class="ot">=</span> <span class="fu">c</span>(<span class="st">'Up'</span>, <span class="st">'Right'</span>, <span class="st">'Down'</span>, <span class="st">'Left'</span>, <span class="st">'None'</span>)</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>actions <span class="ot"><-</span> <span class="cf">function</span>(s) { </span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># absorbing states</span></span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(s <span class="sc">==</span> <span class="dv">11</span> <span class="sc">||</span> s <span class="sc">==</span> <span class="dv">12</span>) <span class="fu">return</span>(<span class="st">'None'</span>)</span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># illegal state</span></span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(s <span class="sc">==</span> <span class="dv">5</span>) <span class="fu">return</span>(<span class="st">'None'</span>)</span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="st">'Up'</span>, <span class="st">'Right'</span>, <span class="st">'Down'</span>, <span class="st">'Left'</span>)</span>
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb11-14"><a href="#cb11-14" aria-hidden="true" tabindex="-1"></a><span class="fu">lapply</span>(S, actions)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] "Up" "Right" "Down" "Left"
[[2]]
[1] "Up" "Right" "Down" "Left"
[[3]]
[1] "Up" "Right" "Down" "Left"
[[4]]
[1] "Up" "Right" "Down" "Left"
[[5]]
[1] "None"
[[6]]
[1] "Up" "Right" "Down" "Left"
[[7]]
[1] "Up" "Right" "Down" "Left"
[[8]]
[1] "Up" "Right" "Down" "Left"
[[9]]
[1] "Up" "Right" "Down" "Left"
[[10]]
[1] "Up" "Right" "Down" "Left"
[[11]]
[1] "None"
[[12]]
[1] "None"</code></pre>
</div>
</div>
</section>
<section id="rewards" class="level2">
<h2 class="anchored" data-anchor-id="rewards">Rewards</h2>
<p><span class="math inline">\(R(s, a, s')\)</span> define the reward for the transition from <span class="math inline">\(s\)</span> to <span class="math inline">\(s'\)</span> with action <span class="math inline">\(a\)</span>.</p>
<p>For the textbook example we have:</p>
<ul>
<li>Any move costs utility (a reward of -0.04).</li>
<li>Going to state 12 has a reward of +1</li>
<li>Going to state 11 has a reward of -1.</li>
</ul>
<p>Note that once you are in an absorbing state (11 or 12), then the problem is over and there is no more reward!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>R <span class="ot"><-</span> <span class="cf">function</span>(s, a, s_prime) {</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="do">## no more reward when we in 11 or 12.</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(a <span class="sc">==</span> <span class="st">'None'</span> <span class="sc">||</span> s <span class="sc">==</span> <span class="dv">11</span> <span class="sc">||</span> s <span class="sc">==</span> <span class="dv">12</span>) <span class="fu">return</span>(<span class="dv">0</span>)</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> <span class="do">## transition to the absorbing states.</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(s_prime <span class="sc">==</span> <span class="dv">12</span>) <span class="fu">return</span>(<span class="sc">+</span><span class="dv">1</span>)</span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(s_prime <span class="sc">==</span> <span class="dv">11</span>) <span class="fu">return</span>(<span class="sc">-</span><span class="dv">1</span>)</span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a> <span class="do">## cost for each move</span></span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(<span class="sc">-</span><span class="fl">0.04</span>)</span>
<span id="cb13-11"><a href="#cb13-11" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb13-12"><a href="#cb13-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-13"><a href="#cb13-13" aria-hidden="true" tabindex="-1"></a><span class="fu">R</span>(<span class="dv">1</span>, <span class="st">'Up'</span>, <span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.04</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">R</span>(<span class="dv">9</span>, <span class="st">'Right'</span>, <span class="dv">12</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">R</span>(<span class="dv">12</span>, <span class="st">'None'</span>, <span class="dv">12</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
</div>
</section>
<section id="transition-model" class="level2">
<h2 class="anchored" data-anchor-id="transition-model">Transition Model</h2>
<p><span class="math inline">\(P(s' | s, a)\)</span> is the probability of going from state <span class="math inline">\(s\)</span> to <span class="math inline">\(s'\)</span> by when taking action <span class="math inline">\(a\)</span>. We will create a matrix <span class="math inline">\(P_a(s' | s)\)</span> for each action.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>calc_transition <span class="ot"><-</span> <span class="cf">function</span>(s, action) {</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> action <span class="ot"><-</span> <span class="fu">match.arg</span>(action, <span class="at">choices =</span> A)</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">length</span>(s) <span class="sc">></span> <span class="dv">1</span>) <span class="fu">return</span>(<span class="fu">t</span>(<span class="fu">sapply</span>(s, calc_transition, <span class="at">action =</span> action)))</span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># deal with absorbing and illegal state</span></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(s <span class="sc">==</span> <span class="dv">11</span> <span class="sc">||</span> s <span class="sc">==</span> <span class="dv">12</span> <span class="sc">||</span> s <span class="sc">==</span> <span class="dv">5</span> <span class="sc">||</span> action <span class="sc">==</span> <span class="st">'None'</span>) {</span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a> P <span class="ot"><-</span> <span class="fu">rep</span>(<span class="dv">0</span>, <span class="fu">length</span>(S))</span>
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true" tabindex="-1"></a> P[s] <span class="ot"><-</span> <span class="dv">1</span></span>
<span id="cb19-10"><a href="#cb19-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(P) </span>
<span id="cb19-11"><a href="#cb19-11" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb19-12"><a href="#cb19-12" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-13"><a href="#cb19-13" aria-hidden="true" tabindex="-1"></a> action_to_delta <span class="ot"><-</span> <span class="fu">list</span>(</span>
<span id="cb19-14"><a href="#cb19-14" aria-hidden="true" tabindex="-1"></a> <span class="st">'Up'</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="sc">+</span><span class="dv">1</span>, <span class="dv">0</span>),</span>
<span id="cb19-15"><a href="#cb19-15" aria-hidden="true" tabindex="-1"></a> <span class="st">'Down'</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>, <span class="dv">0</span>),</span>
<span id="cb19-16"><a href="#cb19-16" aria-hidden="true" tabindex="-1"></a> <span class="st">'Right'</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="sc">+</span><span class="dv">1</span>),</span>
<span id="cb19-17"><a href="#cb19-17" aria-hidden="true" tabindex="-1"></a> <span class="st">'Left'</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="sc">-</span><span class="dv">1</span>)</span>
<span id="cb19-18"><a href="#cb19-18" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb19-19"><a href="#cb19-19" aria-hidden="true" tabindex="-1"></a> delta <span class="ot"><-</span> action_to_delta[[action]]</span>
<span id="cb19-20"><a href="#cb19-20" aria-hidden="true" tabindex="-1"></a> dr <span class="ot"><-</span> delta[<span class="dv">1</span>]</span>
<span id="cb19-21"><a href="#cb19-21" aria-hidden="true" tabindex="-1"></a> dc <span class="ot"><-</span> delta[<span class="dv">2</span>]</span>
<span id="cb19-22"><a href="#cb19-22" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-23"><a href="#cb19-23" aria-hidden="true" tabindex="-1"></a> rc <span class="ot"><-</span> <span class="fu">s_to_rc</span>(s)</span>
<span id="cb19-24"><a href="#cb19-24" aria-hidden="true" tabindex="-1"></a> r <span class="ot"><-</span> rc[<span class="dv">1</span>]</span>
<span id="cb19-25"><a href="#cb19-25" aria-hidden="true" tabindex="-1"></a> c <span class="ot"><-</span> rc[<span class="dv">2</span>]</span>
<span id="cb19-26"><a href="#cb19-26" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-27"><a href="#cb19-27" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(dr <span class="sc">!=</span> <span class="dv">0</span> <span class="sc">&&</span> dc <span class="sc">!=</span> <span class="dv">0</span>) </span>
<span id="cb19-28"><a href="#cb19-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"You can only go up/down or right/left!"</span>)</span>
<span id="cb19-29"><a href="#cb19-29" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-30"><a href="#cb19-30" aria-hidden="true" tabindex="-1"></a> P <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">0</span>, <span class="at">nrow =</span> ROWS, <span class="at">ncol =</span> COLS)</span>
<span id="cb19-31"><a href="#cb19-31" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-32"><a href="#cb19-32" aria-hidden="true" tabindex="-1"></a> <span class="co"># UP/DOWN</span></span>
<span id="cb19-33"><a href="#cb19-33" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(dr <span class="sc">!=</span> <span class="dv">0</span>) {</span>
<span id="cb19-34"><a href="#cb19-34" aria-hidden="true" tabindex="-1"></a> new_r <span class="ot"><-</span> r <span class="sc">+</span> dr</span>
<span id="cb19-35"><a href="#cb19-35" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(new_r <span class="sc">></span> ROWS <span class="sc">||</span> new_r <span class="sc"><</span> <span class="dv">1</span>) new_r <span class="ot"><-</span> r</span>
<span id="cb19-36"><a href="#cb19-36" aria-hidden="true" tabindex="-1"></a> <span class="do">## can't got to (2, 2)</span></span>
<span id="cb19-37"><a href="#cb19-37" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(new_r <span class="sc">==</span> <span class="dv">2</span> <span class="sc">&&</span> c <span class="sc">==</span> <span class="dv">2</span>) new_r <span class="ot"><-</span> r</span>
<span id="cb19-38"><a href="#cb19-38" aria-hidden="true" tabindex="-1"></a> P[new_r, c] <span class="ot"><-</span> .<span class="dv">8</span></span>
<span id="cb19-39"><a href="#cb19-39" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-40"><a href="#cb19-40" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(c <span class="sc"><</span> COLS <span class="sc">&</span> <span class="sc">!</span>(r <span class="sc">==</span> <span class="dv">2</span> <span class="sc">&</span> (c <span class="sc">+</span> <span class="dv">1</span>) <span class="sc">==</span> <span class="dv">2</span>)) </span>
<span id="cb19-41"><a href="#cb19-41" aria-hidden="true" tabindex="-1"></a> P[r, c <span class="sc">+</span> <span class="dv">1</span>] <span class="ot"><-</span> .<span class="dv">1</span> <span class="cf">else</span> P[r, c] <span class="ot"><-</span> P[r, c] <span class="sc">+</span> .<span class="dv">1</span> </span>
<span id="cb19-42"><a href="#cb19-42" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(c <span class="sc">></span> <span class="dv">1</span> <span class="sc">&</span> <span class="sc">!</span>(r <span class="sc">==</span> <span class="dv">2</span> <span class="sc">&</span> (c <span class="sc">-</span> <span class="dv">1</span>) <span class="sc">==</span> <span class="dv">2</span>)) </span>
<span id="cb19-43"><a href="#cb19-43" aria-hidden="true" tabindex="-1"></a> P[r, c <span class="sc">-</span> <span class="dv">1</span>] <span class="ot"><-</span> .<span class="dv">1</span> <span class="cf">else</span> P[r, c] <span class="ot"><-</span> P[r, c] <span class="sc">+</span> .<span class="dv">1</span> </span>
<span id="cb19-44"><a href="#cb19-44" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb19-45"><a href="#cb19-45" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-46"><a href="#cb19-46" aria-hidden="true" tabindex="-1"></a> <span class="co"># RIGHT/LEFT</span></span>
<span id="cb19-47"><a href="#cb19-47" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(dc <span class="sc">!=</span> <span class="dv">0</span>) {</span>
<span id="cb19-48"><a href="#cb19-48" aria-hidden="true" tabindex="-1"></a> new_c <span class="ot"><-</span> c <span class="sc">+</span> dc</span>
<span id="cb19-49"><a href="#cb19-49" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(new_c <span class="sc">></span> COLS <span class="sc">||</span> new_c <span class="sc"><</span> <span class="dv">1</span>) new_c <span class="ot"><-</span> c</span>
<span id="cb19-50"><a href="#cb19-50" aria-hidden="true" tabindex="-1"></a> <span class="do">## can't got to (2, 2)</span></span>
<span id="cb19-51"><a href="#cb19-51" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(r <span class="sc">==</span> <span class="dv">2</span> <span class="sc">&&</span> new_c <span class="sc">==</span> <span class="dv">2</span>) new_c <span class="ot"><-</span> c</span>
<span id="cb19-52"><a href="#cb19-52" aria-hidden="true" tabindex="-1"></a> P[r, new_c] <span class="ot"><-</span> .<span class="dv">8</span></span>
<span id="cb19-53"><a href="#cb19-53" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-54"><a href="#cb19-54" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(r <span class="sc"><</span> ROWS <span class="sc">&</span> <span class="sc">!</span>((r <span class="sc">+</span> <span class="dv">1</span>) <span class="sc">==</span> <span class="dv">2</span> <span class="sc">&</span> c <span class="sc">==</span> <span class="dv">2</span>)) </span>
<span id="cb19-55"><a href="#cb19-55" aria-hidden="true" tabindex="-1"></a> P[r <span class="sc">+</span> <span class="dv">1</span>, c] <span class="ot"><-</span> .<span class="dv">1</span> <span class="cf">else</span> P[r, c] <span class="ot"><-</span> P[r, c] <span class="sc">+</span> .<span class="dv">1</span> </span>
<span id="cb19-56"><a href="#cb19-56" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(r <span class="sc">></span> <span class="dv">1</span> <span class="sc">&</span> <span class="sc">!</span>((r <span class="sc">-</span> <span class="dv">1</span>) <span class="sc">==</span> <span class="dv">2</span> <span class="sc">&</span> c <span class="sc">==</span> <span class="dv">2</span>)) </span>
<span id="cb19-57"><a href="#cb19-57" aria-hidden="true" tabindex="-1"></a> P[r <span class="sc">-</span> <span class="dv">1</span>, c] <span class="ot"><-</span> .<span class="dv">1</span> <span class="cf">else</span> P[r, c] <span class="ot"><-</span> P[r, c] <span class="sc">+</span> .<span class="dv">1</span> </span>
<span id="cb19-58"><a href="#cb19-58" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb19-59"><a href="#cb19-59" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb19-60"><a href="#cb19-60" aria-hidden="true" tabindex="-1"></a> <span class="fu">as.vector</span>(P)</span>
<span id="cb19-61"><a href="#cb19-61" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Try to go up from state 1 (this is (1,1), the bottom left corner). Note: we cannot go left so there is a .1 chance to stay in place.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">calc_transition</span>(<span class="dv">1</span>, <span class="st">'Up'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0.1 0.8 0.0 0.1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0</code></pre>
</div>
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">show_layout</span>(<span class="fu">calc_transition</span>(<span class="dv">1</span>, <span class="st">'Up'</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> col
row 1 2 3 4
3 0.0 0.0 0 0
2 0.8 0.0 0 0
1 0.1 0.1 0 0</code></pre>
</div>
</div>
<p>Try to go right from (2,1). Since right is blocked, there is a .8 probability of staying in place.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">show_layout</span>(<span class="fu">calc_transition</span>(<span class="dv">2</span>, <span class="st">'Right'</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> col
row 1 2 3 4
3 0.1 0 0 0
2 0.8 0 0 0
1 0.1 0 0 0</code></pre>
</div>
</div>
<p>Calculate transitions for each state to each other state. Each row represents a state <span class="math inline">\(s\)</span> and each column a state <span class="math inline">\(s'\)</span> so we get a complete definition for <span class="math inline">\(P_a(s' | s)\)</span>. Note that the matrix is stochastic (all rows add up to 1).</p>
<p>Create a matrix for each action.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>P_matrices <span class="ot"><-</span> <span class="fu">lapply</span>(A, <span class="at">FUN =</span> <span class="cf">function</span>(a) <span class="fu">calc_transition</span>(S, a))</span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(P_matrices) <span class="ot"><-</span> A</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(P_matrices)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>List of 5
$ Up : num [1:12, 1:12] 0.1 0 0 0.1 0 0 0 0 0 0 ...
$ Right: num [1:12, 1:12] 0.1 0.1 0 0 0 0 0 0 0 0 ...
$ Down : num [1:12, 1:12] 0.9 0.8 0 0.1 0 0 0 0 0 0 ...
$ Left : num [1:12, 1:12] 0.9 0.1 0 0.8 0 0 0 0 0 0 ...
$ None : num [1:12, 1:12] 1 0 0 0 0 0 0 0 0 0 ...</code></pre>
</div>
</div>
<p>Create a function interface for <span class="math inline">\(P(s' | s, a)\)</span>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>P <span class="ot"><-</span> <span class="cf">function</span>(sp, s, a) P_matrices[[a]][s, sp]</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a><span class="fu">P</span>(<span class="dv">2</span>, <span class="dv">1</span>, <span class="st">'Up'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.8</code></pre>
</div>
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="fu">P</span>(<span class="dv">5</span>, <span class="dv">4</span>, <span class="st">'Up'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
</div>
</section>
<section id="policy" class="level2">
<h2 class="anchored" data-anchor-id="policy">Policy</h2>
<p>The solution to an MDP is a policy <span class="math inline">\(\pi\)</span> which defines which action to take in each state. We represent deterministic policies as a vector of actions. I make up a policy that always goes up and then to the right once the agent hits the top.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a>pi_manual <span class="ot"><-</span> <span class="fu">rep</span>(<span class="st">'Up'</span>, <span class="at">times =</span> <span class="fu">length</span>(S))</span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a>pi_manual[<span class="fu">c</span>(<span class="dv">3</span>, <span class="dv">6</span>, <span class="dv">9</span>)] <span class="ot"><-</span> <span class="st">'Right'</span></span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a>pi_manual</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "Up" "Up" "Right" "Up" "Up" "Right" "Up" "Up" "Right"
[10] "Up" "Up" "Up" </code></pre>
</div>
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="fu">show_layout</span>(pi_manual)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> col
row 1 2 3 4
3 "Right" "Right" "Right" "Up"
2 "Up" "Up" "Up" "Up"
1 "Up" "Up" "Up" "Up"</code></pre>
</div>
</div>
<p>We can also create a random policy by randomly choosing from the available actions for each state.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1234</span>)</span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a>create_random_policy <span class="ot"><-</span></span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">function</span>()</span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">sapply</span>(</span>
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a> S,</span>
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a> <span class="at">FUN =</span> <span class="cf">function</span>(s)</span>
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">sample</span>(<span class="fu">actions</span>(s), <span class="dv">1</span>)</span>
<span id="cb36-8"><a href="#cb36-8" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb36-9"><a href="#cb36-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-10"><a href="#cb36-10" aria-hidden="true" tabindex="-1"></a>pi_random <span class="ot"><-</span> <span class="fu">create_random_policy</span>()</span>
<span id="cb36-11"><a href="#cb36-11" aria-hidden="true" tabindex="-1"></a><span class="fu">show_layout</span>(pi_random)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> col
row 1 2 3 4
3 "Right" "Left" "Up" "None"
2 "Left" "None" "Up" "None"
1 "Left" "Right" "Down" "Right"</code></pre>
</div>
</div>
<p>Stochastic policies use probabilities of actions in each state. We use as simple table with probabilities where each row is a state and the columns are the actions.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1234</span>)</span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a>create_random_soft_policy <span class="ot"><-</span></span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">function</span>() {</span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a> p <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">0</span>, <span class="at">nrow =</span> <span class="fu">length</span>(S), <span class="at">ncol =</span> <span class="fu">length</span>(A), <span class="at">dimnames =</span> <span class="fu">list</span>(S, A)) </span>
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> (s <span class="cf">in</span> S)</span>
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a> p[s, <span class="fu">actions</span>(s)] <span class="ot"><-</span> <span class="fu">runif</span>(<span class="at">n =</span> <span class="fu">length</span>(<span class="fu">actions</span>(s)))</span>
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># make stochastic (rows sum up to 1)</span></span>
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a>p <span class="ot"><-</span> <span class="fu">sweep</span>(p, <span class="at">MARGIN =</span> <span class="dv">1</span>, <span class="at">STATS =</span> <span class="fu">rowSums</span>(p), <span class="at">FUN =</span> <span class="st">'/'</span>)</span>
<span id="cb38-10"><a href="#cb38-10" aria-hidden="true" tabindex="-1"></a>p</span>
<span id="cb38-11"><a href="#cb38-11" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb38-12"><a href="#cb38-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-13"><a href="#cb38-13" aria-hidden="true" tabindex="-1"></a>pi_random_soft <span class="ot"><-</span> <span class="fu">create_random_soft_policy</span>()</span>
<span id="cb38-14"><a href="#cb38-14" aria-hidden="true" tabindex="-1"></a>pi_random_soft</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Up Right Down Left None
1 0.05775684 0.3161035 0.309487501 0.3166521 0
2 0.49385022 0.3673038 0.005447087 0.1333988 0
3 0.27536627 0.2125970 0.286738186 0.2252985 0
4 0.12104469 0.3953429 0.125147070 0.3584653 0
5 0.00000000 0.0000000 0.000000000 0.0000000 1
6 0.26618674 0.1862791 0.231674078 0.3158601 0
7 0.42009540 0.2207333 0.055508653 0.3036627 0
8 0.26298502 0.1705537 0.296745408 0.2697159 0
9 0.04270727 0.4255694 0.247440113 0.2842832 0
10 0.30758515 0.1098004 0.460595778 0.1220187 0
11 0.00000000 0.0000000 0.000000000 0.0000000 1
12 0.00000000 0.0000000 0.000000000 0.0000000 1</code></pre>
</div>
</div>
</section>
<section id="expected-utility" class="level2">
<h2 class="anchored" data-anchor-id="expected-utility">Expected Utility</h2>
<p>The expected utility can be calculated by</p>
<p><span class="math inline">\(U^\pi = E\left[\sum_{t=0}^\infty \gamma^t R(s_t, \pi(s_t), s_{t+1})\right]\)</span></p>
<p>We need to define the discount factor.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>GAMMA <span class="ot"><-</span> <span class="dv">1</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We can evaluate the utility of a policy using simulation. For the stochastic transition model, we need to be able to sample the state <span class="math inline">\(s'\)</span> the system transitions to when using action <span class="math inline">\(a\)</span> in state <span class="math inline">\(s\)</span>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb41"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>sample_transition <span class="ot"><-</span> <span class="cf">function</span>(s, a)</span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">sample</span>(S, <span class="at">size =</span> <span class="dv">1</span>, <span class="at">prob =</span> P_matrices[[a]][s,])</span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a><span class="fu">sample_transition</span>(<span class="dv">1</span>, <span class="st">'Up'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="fu">table</span>(<span class="fu">replicate</span>(<span class="at">n =</span> <span class="dv">100</span>, <span class="fu">sample_transition</span>(<span class="dv">1</span>, <span class="st">'Up'</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
1 2 4
11 83 6 </code></pre>
</div>
</div>
<p>We can now simulate the utility for one episode.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>simulate_utility <span class="ot"><-</span> <span class="cf">function</span>(pi, <span class="at">s0 =</span> <span class="dv">1</span>, <span class="at">max_t =</span> <span class="dv">100</span>) {</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a> s <span class="ot"><-</span> s0</span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a> U <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a> t <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb45-6"><a href="#cb45-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span> (<span class="cn">TRUE</span>) {</span>
<span id="cb45-7"><a href="#cb45-7" aria-hidden="true" tabindex="-1"></a> <span class="do">## get action from policy (matrix means it is a stochastic policy)</span></span>
<span id="cb45-8"><a href="#cb45-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">is.matrix</span>(pi))</span>
<span id="cb45-9"><a href="#cb45-9" aria-hidden="true" tabindex="-1"></a> a <span class="ot"><-</span> pi[s]</span>
<span id="cb45-10"><a href="#cb45-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span></span>
<span id="cb45-11"><a href="#cb45-11" aria-hidden="true" tabindex="-1"></a> a <span class="ot"><-</span> <span class="fu">sample</span>(A, <span class="at">size =</span> <span class="dv">1</span>, <span class="at">prob =</span> pi[s, ])</span>
<span id="cb45-12"><a href="#cb45-12" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb45-13"><a href="#cb45-13" aria-hidden="true" tabindex="-1"></a> <span class="do">## sample a transition given the action from the policy</span></span>
<span id="cb45-14"><a href="#cb45-14" aria-hidden="true" tabindex="-1"></a> s_prime <span class="ot"><-</span> <span class="fu">sample_transition</span>(s, a)</span>
<span id="cb45-15"><a href="#cb45-15" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb45-16"><a href="#cb45-16" aria-hidden="true" tabindex="-1"></a> <span class="do">##</span></span>
<span id="cb45-17"><a href="#cb45-17" aria-hidden="true" tabindex="-1"></a> U <span class="ot"><-</span> U <span class="sc">+</span> GAMMA <span class="sc">^</span> t <span class="sc">*</span> <span class="fu">R</span>(s, a, s_prime)</span>
<span id="cb45-18"><a href="#cb45-18" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb45-19"><a href="#cb45-19" aria-hidden="true" tabindex="-1"></a> s <span class="ot"><-</span> s_prime</span>
<span id="cb45-20"><a href="#cb45-20" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb45-21"><a href="#cb45-21" aria-hidden="true" tabindex="-1"></a> <span class="do">## reached an absorbing state?</span></span>
<span id="cb45-22"><a href="#cb45-22" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (s <span class="sc">==</span> <span class="dv">11</span> <span class="sc">||</span> s <span class="sc">==</span> <span class="dv">12</span> <span class="sc">||</span> s <span class="sc">==</span> <span class="dv">5</span>)</span>
<span id="cb45-23"><a href="#cb45-23" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span></span>
<span id="cb45-24"><a href="#cb45-24" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb45-25"><a href="#cb45-25" aria-hidden="true" tabindex="-1"></a> t <span class="ot"><-</span> t <span class="sc">+</span> <span class="dv">1</span></span>
<span id="cb45-26"><a href="#cb45-26" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (t <span class="sc">>=</span> max_t)</span>
<span id="cb45-27"><a href="#cb45-27" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span></span>
<span id="cb45-28"><a href="#cb45-28" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb45-29"><a href="#cb45-29" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb45-30"><a href="#cb45-30" aria-hidden="true" tabindex="-1"></a> U</span>
<span id="cb45-31"><a href="#cb45-31" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Simulate the <code>N</code> episodes.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb46"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a>simulate_utilities <span class="ot"><-</span> <span class="cf">function</span>(pi, <span class="at">s0 =</span> <span class="dv">1</span>, <span class="at">N =</span> <span class="dv">1000</span>, <span class="at">max_t =</span> <span class="dv">100</span>)</span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">replicate</span>(N, <span class="fu">simulate_utility</span>(pi, s0, max_t))</span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a>utility_manual <span class="ot"><-</span> <span class="fu">simulate_utilities</span>(pi_manual)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The expected utility for starting from state <span class="math inline">\(s_0 = 1\)</span> is.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(utility_manual)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.69924</code></pre>
</div>
<div class="sourceCode cell-code" id="cb49"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hist</span>(utility_manual, <span class="at">xlim =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>, <span class="dv">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="_AIMA-4x3-gridworld_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Compare with the utility of the random policy.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb50"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>utility_random <span class="ot"><-</span> <span class="fu">simulate_utilities</span>(pi_random, <span class="at">max_t =</span> <span class="dv">100</span>)</span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a><span class="fu">table</span>(utility_random)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>utility_random
-4
1000 </code></pre>
</div>
</div>
<p>The random policy performs really bad and most likely always stumbles around for <code>max_t</code> moves at a cost of .04 each. The manually created policy is obviously better.</p>
<p>We can use simulation to estimate the expected utility for starting from each state following the policy.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb52"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a>U_manual <span class="ot"><-</span></span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">sapply</span>(</span>
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a> S,</span>
<span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a> <span class="at">FUN =</span> <span class="cf">function</span>(s)</span>
<span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mean</span>(<span class="fu">simulate_utilities</span>(pi_manual, <span class="at">s0 =</span> s))</span>
<span id="cb52-6"><a href="#cb52-6" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb52-7"><a href="#cb52-7" aria-hidden="true" tabindex="-1"></a><span class="fu">show_layout</span>(U_manual)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> col
row 1 2 3 4
3 0.86044 0.89644 0.95420 0.00000
2 0.79396 -0.04000 0.71252 0.00000
1 0.71736 0.35884 0.51804 -0.86708</code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb54"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>U_random <span class="ot"><-</span></span>
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">sapply</span>(</span>
<span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a> S,</span>
<span id="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a> <span class="at">FUN =</span> <span class="cf">function</span>(s)</span>
<span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mean</span>(<span class="fu">simulate_utilities</span>(pi_random, <span class="at">s0 =</span> s))</span>
<span id="cb54-6"><a href="#cb54-6" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb54-7"><a href="#cb54-7" aria-hidden="true" tabindex="-1"></a><span class="fu">show_layout</span>(U_random)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> col
row 1 2 3 4
3 -4 -4.00000 -1.60504 0.00000
2 -4 0.00000 -1.44744 0.00000
1 -4 -1.84944 -1.79828 -1.37408</code></pre>
</div>
</div>
</section>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const clipboard = new window.ClipboardJS('.code-copy-button', {
text: function(trigger) {
const codeEl = trigger.previousElementSibling.cloneNode(true);
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
let selectedAnnoteEl;
const selectorForAnnotation = ( cell, annotation) => {
let cellAttr = 'data-code-cell="' + cell + '"';
let lineAttr = 'data-code-annotation="' + annotation + '"';
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
return selector;
}
const selectCodeLines = (annoteEl) => {
const doc = window.document;
const targetCell = annoteEl.getAttribute("data-target-cell");
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
const lineIds = lines.map((line) => {
return targetCell + "-" + line;
})
let top = null;
let height = null;
let parent = null;
if (lineIds.length > 0) {
//compute the position of the single el (top and bottom and make a div)
const el = window.document.getElementById(lineIds[0]);
top = el.offsetTop;
height = el.offsetHeight;
parent = el.parentElement.parentElement;
if (lineIds.length > 1) {
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
height = bottom - top;
}
if (top !== null && height !== null && parent !== null) {
// cook up a div (if necessary) and position it
let div = window.document.getElementById("code-annotation-line-highlight");
if (div === null) {
div = window.document.createElement("div");
div.setAttribute("id", "code-annotation-line-highlight");
div.style.position = 'absolute';
parent.appendChild(div);
}
div.style.top = top - 2 + "px";
div.style.height = height + 4 + "px";
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
if (gutterDiv === null) {
gutterDiv = window.document.createElement("div");
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
gutterDiv.style.position = 'absolute';
const codeCell = window.document.getElementById(targetCell);
const gutter = codeCell.querySelector('.code-annotation-gutter');
gutter.appendChild(gutterDiv);
}
gutterDiv.style.top = top - 2 + "px";
gutterDiv.style.height = height + 4 + "px";
}
selectedAnnoteEl = annoteEl;
}
};
const unselectCodeLines = () => {
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
elementsIds.forEach((elId) => {
const div = window.document.getElementById(elId);
if (div) {
div.remove();
}
});
selectedAnnoteEl = undefined;
};
// Attach click handler to the DT
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
for (const annoteDlNode of annoteDls) {
annoteDlNode.addEventListener('click', (event) => {
const clickedEl = event.target;
if (clickedEl !== selectedAnnoteEl) {
unselectCodeLines();
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
if (activeEl) {
activeEl.classList.remove('code-annotation-active');
}
selectCodeLines(clickedEl);
clickedEl.classList.add('code-annotation-active');
} else {
// Unselect the line
unselectCodeLines();
clickedEl.classList.remove('code-annotation-active');
}
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>