-
Notifications
You must be signed in to change notification settings - Fork 0
/
automated_planning.html
590 lines (562 loc) · 26.5 KB
/
automated_planning.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
<!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.340">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Carmen Gómez Valenzuela">
<meta name="dcterms.date" content="2023-05-13">
<title>CGoDataScience - Automated planning</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;
}
</style>
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "navbar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "end",
"type": "overlay",
"limit": 20,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit"
}
}</script>
<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>
<link rel="stylesheet" href="styles.css">
</head>
<body class="nav-fixed">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="navbar navbar-expand-lg navbar-dark ">
<div class="navbar-container container-fluid">
<div class="navbar-brand-container">
<a class="navbar-brand" href="./index.html">
<span class="navbar-title">CGoDataScience</span>
</a>
</div>
<div id="quarto-search" class="" title="Search"></div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav navbar-nav-scroll me-auto">
<li class="nav-item">
<a class="nav-link" href="./index.html" rel="" target="">
<span class="menu-text">Home</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./portfolio.html" rel="" target="">
<span class="menu-text">Portfolio</span></a>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="nav-menu-statistics" role="button" data-bs-toggle="dropdown" aria-expanded="false" rel="" target="">
<span class="menu-text">Statistics</span>
</a>
<ul class="dropdown-menu" aria-labelledby="nav-menu-statistics">
<li>
<a class="dropdown-item" href="./statistical_inference.html" rel="" target="">
<span class="dropdown-text">Statistical Inference</span></a>
</li>
<li>
<a class="dropdown-item" href="./regression.html" rel="" target="">
<span class="dropdown-text">Regression: Models and Methods</span></a>
</li>
<li>
<a class="dropdown-item" href="./multivariate_analysis.html" rel="" target="">
<span class="dropdown-text">Multivariate Analysis</span></a>
</li>
<li>
<a class="dropdown-item" href="./longitudinal_analysis.html" rel="" target="">
<span class="dropdown-text">Longitudinal data analysis</span></a>
</li>
</ul>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="nav-menu-artificial-intelligence" role="button" data-bs-toggle="dropdown" aria-expanded="false" rel="" target="">
<span class="menu-text">Artificial Intelligence</span>
</a>
<ul class="dropdown-menu" aria-labelledby="nav-menu-artificial-intelligence">
<li>
<a class="dropdown-item" href="./machine_learning.html" rel="" target="">
<span class="dropdown-text">Machine Learning</span></a>
</li>
<li>
<a class="dropdown-item" href="./deep_learning.html" rel="" target="">
<span class="dropdown-text">Deep Learning</span></a>
</li>
<li>
<a class="dropdown-item" href="./automated_planning.html" rel="" target="">
<span class="dropdown-text">Automated planning</span></a>
</li>
</ul>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" href="#" id="nav-menu-bioinformatics" role="button" data-bs-toggle="dropdown" aria-expanded="false" rel="" target="">
<span class="menu-text">Bioinformatics</span>
</a>
<ul class="dropdown-menu" aria-labelledby="nav-menu-bioinformatics">
<li>
<a class="dropdown-item" href="./molecular_biology.html" rel="" target="">
<span class="dropdown-text">Molecular biology</span></a>
</li>
<li>
<a class="dropdown-item" href="./analysis_of_omics_data.html" rel="" target="">
<span class="dropdown-text">Analysis of omics data</span></a>
</li>
<li>
<a class="dropdown-item" href="./computational_genomics.html" rel="" target="">
<span class="dropdown-text">Computational genomics</span></a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="./blog.html" rel="" target="">
<span class="menu-text">Blog</span></a>
</li>
</ul>
<div class="quarto-navbar-tools ms-auto">
<a href="https://www.linkedin.com/in/cgomezvalenzuela/" title="" class="quarto-navigation-tool px-1" aria-label=""><i class="bi bi-linkedin"></i></a>
<div class="dropdown">
<a href="" title="" id="quarto-navigation-tool-dropdown-0" class="quarto-navigation-tool dropdown-toggle px-1" data-bs-toggle="dropdown" aria-expanded="false" aria-label=""><i class="bi bi-github"></i></a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="quarto-navigation-tool-dropdown-0">
<li>
<a class="dropdown-item quarto-navbar-tools ms-auto-item" href="https://code.com">
Source Code
</a>
</li>
<li>
<a class="dropdown-item quarto-navbar-tools ms-auto-item" href="https://bugs.com">
Report a Bug
</a>
</li>
</ul>
</div>
</div>
</div> <!-- /navcollapse -->
</div> <!-- /container-fluid -->
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
<!-- sidebar -->
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">On this page</h2>
<ul>
<li><a href="#introduction" id="toc-introduction" class="nav-link active" data-scroll-target="#introduction">Introduction</a></li>
<li><a href="#classical-planning-model" id="toc-classical-planning-model" class="nav-link" data-scroll-target="#classical-planning-model">Classical Planning Model</a></li>
<li><a href="#language-for-classical-planning-strips" id="toc-language-for-classical-planning-strips" class="nav-link" data-scroll-target="#language-for-classical-planning-strips">Language for Classical Planning: Strips</a></li>
<li><a href="#from-language-to-models" id="toc-from-language-to-models" class="nav-link" data-scroll-target="#from-language-to-models">From Language to Models</a></li>
<li><a href="#planning-and-model-based-reinforcement-learning" id="toc-planning-and-model-based-reinforcement-learning" class="nav-link" data-scroll-target="#planning-and-model-based-reinforcement-learning">Planning and Model-based Reinforcement Learning</a></li>
<li><a href="#computational-problems-of-classical-planning" id="toc-computational-problems-of-classical-planning" class="nav-link" data-scroll-target="#computational-problems-of-classical-planning">Computational problems of Classical Planning</a></li>
<li><a href="#references" id="toc-references" class="nav-link" data-scroll-target="#references">References</a></li>
</ul>
</nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Automated planning</h1>
<div class="quarto-categories">
<div class="quarto-category">Multivariate Analysis</div>
</div>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Carmen Gómez Valenzuela </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">May 13, 2023</p>
</div>
</div>
</div>
</header>
<p><em>“Planning is the art and practice of thinking before acting: of reviewing the courses of action one has available and predicting their expected (and unexpected) results to be able to choose the course of action most beneficial with respect to one’s goals.”</em> <br>–Patrik Haslum–</p>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p><strong>Basic Planning Problem</strong>: given descriptions of possible initial states of the world, desired goals and a set of possible actions, synthesize a plan that is guaranteed to generate a state which contains the desired goals.</p>
<p>In artificial intelligence, planning refers to the process of generating a sequence of actions to achieve a specific goal or set of goals. Planning involves finding an optimal sequence of actions that can transform an initial state of the world into a desired goal state while satisfying any relevant constraints and optimizing some objective function.</p>
<p>In summary, AI Planning is the model-based approach to action selection: prouduces the behaviour from the model (solves the model)</p>
</section>
<section id="classical-planning-model" class="level1">
<h1>Classical Planning Model</h1>
Classical planning model is a tuple of six elements: <span class="math display">\[\mathcal{S} = \langle S, s_0, S_G, A, f, c \rangle \]</span> where:
<ul>
<li>
the first is the finite set of states <span class="math inline">\(S\)</span>
</li>
<li>
one of this states is called the initial state <span class="math inline">\(s_0∈S\)</span>
</li>
<li>
some known non empty subset of these are called goal states: <span class="math inline">\(S_G⊆S\)</span>
</li>
<li>
a final set of actions <span class="math inline">\(A(s) ⊆ A\)</span> that if a subset of applicable actions defined for each state <span class="math inline">\(s∈S\)</span>
</li>
<li>
A deterministic transition function maps a state s and an applicable action a into a resulting state s’ <span class="math display">\[s' = f(a, s)~for~a∈A(s)\]</span>
</li>
<li>
finally each transition has associated a non-negative action cost <span class="math inline">\(c(a, s)\)</span>
</li>
</ul>
<p>So solutions are sequences of applicable actions that start at the initial state <span class="math inline">\(s_0\)</span> and add some goal states <span class="math inline">\(S_G\)</span></p>
</section>
<section id="language-for-classical-planning-strips" class="level1">
<h1>Language for Classical Planning: Strips</h1>
<p>In order to be able to completely represent those models we use the planning languages. The simplest one and the most common is Strips, which stands for Stanford Research Institute of Problem Solver.</p>
In Strips the Planning task is given by a 5-tuple of elements: <span class="math display">\[\Pi = \langle F, O, c, I, G \rangle\]</span>
<ul>
<li>
F: finite set of atoms or boolean variables
</li>
<li>
O: finite set of operators or actions of form <span class="math inline">\(\langle Add, Del, Pre \rangle\)</span> (Add/Delete/Preconditions), each of these are a subset of atoms
</li>
<li>
<span class="math inline">\(c: O \mapsto \mathbb{R}\)</span>: a non negative operator cost or action cost that gives a non negative value for each action
</li>
<li>
I: initial state (a subset of atoms)
</li>
<li>
G: goal description (also a subset of atoms)
</li>
</ul>
<p>A plan is a sequence of applicable actions that maps I into a state consistent with G</p>
</section>
<section id="from-language-to-models" class="level1">
<h1>From Language to Models</h1>
Given a Strips planning task we can define the model as follows: a Strips Planning task <span class="math inline">\(\Pi = \langle F, O, c, I, G \rangle\)</span> determines state model <span class="math inline">\(\mathcal{S}(\Pi)\)</span> where:
<ul>
<li>
the states <span class="math inline">\(s∈S\)</span> are collections of atoms from F (all possible subsets of F)
</li>
<li>
the initial state <span class="math inline">\(s_0\)</span> is I
</li>
<li>
the goal states <span class="math inline">\(s\)</span> are such that <span class="math inline">\(G⊆s\)</span> (all states that are super sets of G)
</li>
<li>
the actions a in <span class="math inline">\(A(s)\)</span> are ops in O s.t. <span class="math inline">\(Pre(a)⊆s\)</span> (the actions in A(s) are operators in O such as the precondition is included in the state)
</li>
<li>
and the next state is <span class="math inline">\(s'=s− Del(a) + Add(a)\)</span> is generated by removing the set of delete effects and adding the set of additions
</li>
<li>
action costs <span class="math inline">\(c(a, s)=c(a)\)</span>: all transitions that belongs to the same action will get the same cost
</li>
</ul>
<p>Solutions to the plannig model are exactly the solutions for the plannig task: Solutions of <span class="math inline">\(S(\Pi)\)</span> are plans of <span class="math inline">\(\Pi\)</span></p>
</section>
<section id="planning-and-model-based-reinforcement-learning" class="level1">
<h1>Planning and Model-based Reinforcement Learning</h1>
<p>Let’s see how Strips planning tasks can be used to define the commonly used models in the realm. For the forward model we need to define the next state, that is done given the current state and an action: you take a state, remove the delete effects and give up the add effects. The only difference is that is done only for applicable actions, for non applicable actions is just a cell flow. <span class="math display">\[(a, s_i) \rightarrow s_{(i+1)} = s_i−Del(a)+Add(a)~if~Pre(a)⊆s_i, (a,s_i) \mapsto s_i~ otherwise\]</span></p>
<p>For the backwards/reverse model, we regress an action in a state by removing the add effects and adding preconditions. This is only valid for actions whose delete effects don’t appear in the state. <span class="math display">\[s_{(i+1)} \rightarrow (a, s_i)~where~Del(a)∩s_{(i+1)}=∅~and~s_i = s_{(i+1)} − Add(a) + Pre(a)\]</span></p>
<p>For inverse model we can find an action whose preconditions are included in the <span class="math inline">\(s_i\)</span> and a plan which would result in <span class="math inline">\(s_{(i+1)\)</span>. That’s can be done just by going over all applicable actions. <span class="math display">\[(s_{(i+1)}, s_i) \rightarrow a,~where~ Pre(a)⊆s_i~and~s_{(i+1)} = s_i − Del(a) + Add(a)\]</span></p>
<p>The rewards approximate the negative of the true optimal cost <span class="math inline">\(-c^*(s_{(i+1)})\)</span> of reaching the goal (reward obtanaible) from the state <span class="math inline">\(s_{(i+1)}:(s_i, a, s_{(i+1)}) \rightarrow h(s_{(i+1)}) − h(s_i)\)</span></p>
</section>
<section id="computational-problems-of-classical-planning" class="level1">
<h1>Computational problems of Classical Planning</h1>
<ul>
<li>
Cost of ptimal planning: the cost of find a plan (sequence of actions) that minimizes the summed action cost
</li>
<li>
In satisficing planning: we want to find any plan improving the plan cost as much as possible (the cheaper plan)
</li>
<li>
In agile planning: we care only about how quickly a plan can found ignoring completely its quantity
</li>
<li>
For top-k planning: find k plans such that no cheaper plans exist (basically genrealizes the cost optimal planning)
</li>
<li>
In top-quality planning: we search for all possible plans up to a certain cost
</li>
<li>
<p>Diverse planning: deals with both plan quality and plan diversity (variety of problems, aiming at obtaining diverse set of plans considering plan quality as well)</p>
</li></ul></section>
<section id="references" class="level1">
<h1>References</h1>
<p>AAAI 2022 Tutorial on AI Planning: Theory and Practice <br>https://aiplanning-tutorial.github.io/ <br>https://www.youtube.com/watch?v=q-mShBwHkc4 <br>https://www.youtube.com/watch?v=PKISCipS9Og <br>https://planning.wiki/ <br>https://www.youtube.com/watch?v=XW0z8Oik6G8&list=PL1Q0jeuU6XppflOPFx1qQVuWbXTcjxevU <br>https://www.youtube.com/watch?v=_NOVa4i7Us8&list=PL1Q0jeuU6XppflOPFx1qQVuWbXTcjxevU&index=7</p>
</section>
</main> <!-- /main -->
<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>