Newer
Older
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Planning: planning.py; chapters 10-11"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook describes the [planning.py](https://github.com/aimacode/aima-python/blob/master/planning.py) module, which covers Chapters 10 (Classical Planning) and 11 (Planning and Acting in the Real World) of *[Artificial Intelligence: A Modern Approach](http://aima.cs.berkeley.edu)*. See the [intro notebook](https://github.com/aimacode/aima-python/blob/master/intro.ipynb) for instructions.\n",
"\n",
"We'll start by looking at `PDDL` and `Action` data types for defining problems and actions. Then, we will see how to use them by trying to plan a trip from *Sibiu* to *Bucharest* across the familiar map of Romania, from [search.ipynb](https://github.com/aimacode/aima-python/blob/master/search.ipynb). Finally, we will look at the implementation of the GraphPlan algorithm.\n",
"\n",
"The first step is to load the code:"
]
},
"from planning import *\n",
"from notebook import psource"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To be able to model a planning problem properly, it is essential to be able to represent an Action. Each action we model requires at least three things:\n",
"* preconditions that the action must meet\n",
"* the effects of executing the action\n",
"* some expression that represents the action"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Planning actions have been modelled using the `Action` class. Let's look at the source to see how the internal details of an action are implemented in Python."
]
},
{
"cell_type": "code",
"execution_count": 2,
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is interesting to see the way preconditions and effects are represented here. Instead of just being a list of expressions each, they consist of two lists - `precond_pos` and `precond_neg`. This is to work around the fact that PDDL doesn't allow for negations. Thus, for each precondition, we maintain a separate list of those preconditions that must hold true, and those whose negations must hold true. Similarly, instead of having a single list of expressions that are the result of executing an action, we have two. The first (`effect_add`) contains all the expressions that will evaluate to true if the action is executed, and the the second (`effect_neg`) contains all those expressions that would be false if the action is executed (ie. their negations would be true).\n",
"\n",
"The constructor parameters, however combine the two precondition lists into a single `precond` parameter, and the effect lists into a single `effect` parameter."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `PDDL` class is used to represent planning problems in this module. The following attributes are essential to be able to define a problem:\n",
"* a goal test\n",
"* an initial state\n",
"* a set of viable actions that can be executed in the search space of the problem\n",
"\n",
"View the source to see how the Python code tries to realise these."
]
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [],
"source": [
"%psource PDDL"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `initial_state` attribute is a list of `Expr` expressions that forms the initial knowledge base for the problem. Next, `actions` contains a list of `Action` objects that may be executed in the search space of the problem. Lastly, we pass a `goal_test` function as a parameter - this typically takes a knowledge base as a parameter, and returns whether or not the goal has been reached."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now lets try to define a planning problem using these tools. Since we already know about the map of Romania, lets see if we can plan a trip across a simplified map of Romania.\n",
"\n",
"Here is our simplified map definition:"
]
},
{
"cell_type": "code",
"execution_count": 4,
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
"outputs": [],
"source": [
"from utils import *\n",
"# this imports the required expr so we can create our knowledge base\n",
"\n",
"knowledge_base = [\n",
" expr(\"Connected(Bucharest,Pitesti)\"),\n",
" expr(\"Connected(Pitesti,Rimnicu)\"),\n",
" expr(\"Connected(Rimnicu,Sibiu)\"),\n",
" expr(\"Connected(Sibiu,Fagaras)\"),\n",
" expr(\"Connected(Fagaras,Bucharest)\"),\n",
" expr(\"Connected(Pitesti,Craiova)\"),\n",
" expr(\"Connected(Craiova,Rimnicu)\")\n",
" ]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us add some logic propositions to complete our knowledge about travelling around the map. These are the typical symmetry and transitivity properties of connections on a map. We can now be sure that our `knowledge_base` understands what it truly means for two locations to be connected in the sense usually meant by humans when we use the term.\n",
"\n",
"Let's also add our starting location - *Sibiu* to the map."
]
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [],
"source": [
"knowledge_base.extend([\n",
" expr(\"Connected(x,y) ==> Connected(y,x)\"),\n",
" expr(\"Connected(x,y) & Connected(y,z) ==> Connected(x,z)\"),\n",
" expr(\"At(Sibiu)\")\n",
" ])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now have a complete knowledge base, which can be seen like this:"
]
},
{
"cell_type": "code",
"execution_count": 6,
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
"outputs": [
{
"data": {
"text/plain": [
"[Connected(Bucharest, Pitesti),\n",
" Connected(Pitesti, Rimnicu),\n",
" Connected(Rimnicu, Sibiu),\n",
" Connected(Sibiu, Fagaras),\n",
" Connected(Fagaras, Bucharest),\n",
" Connected(Pitesti, Craiova),\n",
" Connected(Craiova, Rimnicu),\n",
" (Connected(x, y) ==> Connected(y, x)),\n",
" ((Connected(x, y) & Connected(y, z)) ==> Connected(x, z)),\n",
" At(Sibiu)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"knowledge_base"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now define possible actions to our problem. We know that we can drive between any connected places. But, as is evident from [this](https://en.wikipedia.org/wiki/List_of_airports_in_Romania) list of Romanian airports, we can also fly directly between Sibiu, Bucharest, and Craiova.\n",
"\n",
"We can define these flight actions like this:"
]
},
{
"cell_type": "code",
"execution_count": 7,
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
"outputs": [],
"source": [
"#Sibiu to Bucharest\n",
"precond_pos = [expr('At(Sibiu)')]\n",
"precond_neg = []\n",
"effect_add = [expr('At(Bucharest)')]\n",
"effect_rem = [expr('At(Sibiu)')]\n",
"fly_s_b = Action(expr('Fly(Sibiu, Bucharest)'), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
"\n",
"#Bucharest to Sibiu\n",
"precond_pos = [expr('At(Bucharest)')]\n",
"precond_neg = []\n",
"effect_add = [expr('At(Sibiu)')]\n",
"effect_rem = [expr('At(Bucharest)')]\n",
"fly_b_s = Action(expr('Fly(Bucharest, Sibiu)'), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
"\n",
"#Sibiu to Craiova\n",
"precond_pos = [expr('At(Sibiu)')]\n",
"precond_neg = []\n",
"effect_add = [expr('At(Craiova)')]\n",
"effect_rem = [expr('At(Sibiu)')]\n",
"fly_s_c = Action(expr('Fly(Sibiu, Craiova)'), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
"\n",
"#Craiova to Sibiu\n",
"precond_pos = [expr('At(Craiova)')]\n",
"precond_neg = []\n",
"effect_add = [expr('At(Sibiu)')]\n",
"effect_rem = [expr('At(Craiova)')]\n",
"fly_c_s = Action(expr('Fly(Craiova, Sibiu)'), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
"\n",
"#Bucharest to Craiova\n",
"precond_pos = [expr('At(Bucharest)')]\n",
"precond_neg = []\n",
"effect_add = [expr('At(Craiova)')]\n",
"effect_rem = [expr('At(Bucharest)')]\n",
"fly_b_c = Action(expr('Fly(Bucharest, Craiova)'), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
"\n",
"#Craiova to Bucharest\n",
"precond_pos = [expr('At(Craiova)')]\n",
"precond_neg = []\n",
"effect_add = [expr('At(Bucharest)')]\n",
"effect_rem = [expr('At(Craiova)')]\n",
"fly_c_b = Action(expr('Fly(Craiova, Bucharest)'), [precond_pos, precond_neg], [effect_add, effect_rem])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And the drive actions like this."
]
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [],
"source": [
"#Drive\n",
"precond_pos = [expr('At(x)')]\n",
"precond_neg = []\n",
"effect_add = [expr('At(y)')]\n",
"effect_rem = [expr('At(x)')]\n",
"drive = Action(expr('Drive(x, y)'), [precond_pos, precond_neg], [effect_add, effect_rem])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we can define a a function that will tell us when we have reached our destination, Bucharest."
]
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [],
"source": [
"def goal_test(kb):\n",
" return kb.ask(expr(\"At(Bucharest)\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Thus, with all the components in place, we can define the planning problem."
]
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [],
"source": [
"prob = PDDL(knowledge_base, [fly_s_b, fly_b_s, fly_s_c, fly_c_s, fly_b_c, fly_c_b, drive], goal_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Air Cargo Problem:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Air Cargo problem involves loading and unloading of cargo and flying it from place to place. The problem can be defined with three actions: Load, Unload and Fly. Let us look at `air_cargo`. "
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
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">air_cargo</span><span class=\"p\">():</span>\n",
" <span class=\"n\">init</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'At(C1, SFO)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'At(C2, JFK)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'At(P1, SFO)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'At(P2, JFK)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Cargo(C1)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Cargo(C2)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Plane(P1)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Plane(P2)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Airport(JFK)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Airport(SFO)'</span><span class=\"p\">)]</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">goal_test</span><span class=\"p\">(</span><span class=\"n\">kb</span><span class=\"p\">):</span>\n",
" <span class=\"n\">required</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'At(C1 , JFK)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'At(C2 ,SFO)'</span><span class=\"p\">)]</span>\n",
" <span class=\"k\">return</span> <span class=\"nb\">all</span><span class=\"p\">([</span><span class=\"n\">kb</span><span class=\"o\">.</span><span class=\"n\">ask</span><span class=\"p\">(</span><span class=\"n\">q</span><span class=\"p\">)</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">False</span> <span class=\"k\">for</span> <span class=\"n\">q</span> <span class=\"ow\">in</span> <span class=\"n\">required</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"c1\"># Actions</span>\n",
"\n",
" <span class=\"c1\"># Load</span>\n",
" <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(c, a)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(p, a)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Cargo(c)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Plane(p)"</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Airport(a)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"In(c, p)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(c, a)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">load</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Load(c, p, a)"</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"c1\"># Unload</span>\n",
" <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"In(c, p)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(p, a)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Cargo(c)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Plane(p)"</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Airport(a)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(c, a)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"In(c, p)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">unload</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Unload(c, p, a)"</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"c1\"># Fly</span>\n",
" <span class=\"c1\"># Used 'f' instead of 'from' because 'from' is a python keyword and expr uses eval() function</span>\n",
" <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(p, f)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Plane(p)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Airport(f)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Airport(to)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(p, to)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(p, f)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">fly</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Fly(p, f, to)"</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"k\">return</span> <span class=\"n\">PDDL</span><span class=\"p\">(</span><span class=\"n\">init</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">load</span><span class=\"p\">,</span> <span class=\"n\">unload</span><span class=\"p\">,</span> <span class=\"n\">fly</span><span class=\"p\">],</span> <span class=\"n\">goal_test</span><span class=\"p\">)</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"psource(air_cargo)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**At(x, a):** The cargo or plane **'x'** is at airport **'a'**.\n",
"\n",
"**In(c, p):** Cargo **'c'** is in palne **'p'**.\n",
"\n",
"**Cargo(x):** Declare **'x'** as cargo.\n",
"\n",
"**Plane(x):** Declare **'x'** as plane.\n",
"\n",
"**Airport(x):** Declare **'x'** as airport.\n",
"\n",
"\n",
"\n",
"In the `initial_state`, we have cargo C1, plane P1 at airport SFO and cargo C2, plane P2 at airport JFK. Our goal state is to have cargo C1 at airport JFK and cargo C2 at airport SFO. We will discuss on how to achieve this. Let us now define an object of the `air_cargo` problem:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"source": [
"airCargo = air_cargo()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, before taking any actions, we will check the `airCargo` if it has completed the goal it is required to do:"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"print(airCargo.goal_test())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It returns False because the goal state is not yet reached. Now, we define the sequence of actions that it should take in order to achieve the goal. Then the `airCargo` acts on each of them.\n",
"\n",
"The actions available to us are the following: Load, Unload, Fly\n",
"\n",
"**Load(c, p, a):** Load cargo **'c'** into plane **'p'** from airport **'a'**.\n",
"\n",
"**Fly(p, f, t):** Fly the plane **'p'** from airport **'f'** to airport **'t'**.\n",
"\n",
"**Unload(c, p, c):** Unload cargo **'c'** from plane **'p'** to airport **'a'**.\n"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"solution = [expr(\"Load(C1 , P1, SFO)\"),\n",
" expr(\"Fly(P1, SFO, JFK)\"),\n",
" expr(\"Unload(C1, P1, JFK)\"),\n",
" expr(\"Load(C2, P2, JFK)\"),\n",
" expr(\"Fly(P2, JFK, SFO)\"),\n",
" expr(\"Unload (C2, P2, SFO)\")] \n",
"\n",
"for action in solution:\n",
" airCargo.act(action)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As the `airCargo` has taken all the steps it needed in order to achieve the goal, we can now check if it has acheived its goal:"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It has now achieved its goal."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The Spare Tire Problem"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's consider the problem of changing a flat tire of a car. The goal is to have a good spare tire properly mounted onto the car's axle, where the initial state has a flat tire on the axle and a good spare tire in the trunk. "
{
"cell_type": "code",
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
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">spare_tire</span><span class=\"p\">():</span>\n",
" <span class=\"n\">init</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Tire(Flat)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Tire(Spare)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'At(Flat, Axle)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'At(Spare, Trunk)'</span><span class=\"p\">)]</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">goal_test</span><span class=\"p\">(</span><span class=\"n\">kb</span><span class=\"p\">):</span>\n",
" <span class=\"n\">required</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'At(Spare, Axle)'</span><span class=\"p\">)]</span>\n",
" <span class=\"k\">return</span> <span class=\"nb\">all</span><span class=\"p\">(</span><span class=\"n\">kb</span><span class=\"o\">.</span><span class=\"n\">ask</span><span class=\"p\">(</span><span class=\"n\">q</span><span class=\"p\">)</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">False</span> <span class=\"k\">for</span> <span class=\"n\">q</span> <span class=\"ow\">in</span> <span class=\"n\">required</span><span class=\"p\">)</span>\n",
"\n",
" <span class=\"c1\"># Actions</span>\n",
"\n",
" <span class=\"c1\"># Remove</span>\n",
" <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(obj, loc)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(obj, Ground)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(obj, loc)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">remove</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Remove(obj, loc)"</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"c1\"># PutOn</span>\n",
" <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"Tire(t)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(t, Ground)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(Flat, Axle)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(t, Axle)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(t, Ground)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">put_on</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"PutOn(t, Axle)"</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"c1\"># LeaveOvernight</span>\n",
" <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(Spare, Ground)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(Spare, Axle)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(Spare, Trunk)"</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(Flat, Ground)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(Flat, Axle)"</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"At(Flat, Trunk)"</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">leave_overnight</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s2\">"LeaveOvernight"</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span>\n",
" <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"k\">return</span> <span class=\"n\">PDDL</span><span class=\"p\">(</span><span class=\"n\">init</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">remove</span><span class=\"p\">,</span> <span class=\"n\">put_on</span><span class=\"p\">,</span> <span class=\"n\">leave_overnight</span><span class=\"p\">],</span> <span class=\"n\">goal_test</span><span class=\"p\">)</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"psource(spare_tire)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**At(x, l):** object **'x'** is at location **'l'**.\n",
"\n",
"**Tire(x):** Declare a tire of type **'x'**.\n",
"\n",
"Let us now define an object of `spare_tire` problem:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"spare_tire = spare_tire()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, before taking any actions, we will check `spare_tire` if it has completed the goal it is required to do"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"print(spare_tire.goal_test())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we can see, it hasn't completed the goal. Now, we define the sequence of actions that it should take in order to have a good spare tire properly mounted onto the car's axle. Then the `spare_tire` acts on each of them.\n",
"\n",
"The actions available to us are the following: Remove, PutOn\n",
"\n",
"**Remove(obj, loc):** Remove the tire **'obj'** from the location **'loc'**.\n",
"\n",
"**PutOn(t, Axle):** Attach the tire **'t'** on the Axle.\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"solution = [expr(\"Remove(Flat, Axle)\"),\n",
" expr(\"Remove(Spare, Trunk)\"),\n",
" expr(\"PutOn(Spare, Axle)\")]\n",
"\n",
"for action in solution:\n",
" spare_tire.act(action)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As the `spare_tire` has taken all the steps it needed in order to achieve the goal, we can now check if it has acheived its goal"
]
},
{
"cell_type": "code",
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(spare_tire.goal_test())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It has now successfully achieved its goal i.e, to have a good spare tire properly mounted onto the car's axle."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Three Block Tower Problem"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This problem's domain consists of a set of cube-shaped blocks sitting on a table. The blocks can be stacked, but only one block can fit directly on top of another. A robot arm can pick up a block and move it to another position, either on the table or on top of another block. The arm can pick up only one block at a time, so it cannot pick up a block that has another one on it. The goal will always be to build one or more stacks of blocks. In our case, we consider only three blocks."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"let us take a look at the `three_block_tower()` code."
]
},
{
"cell_type": "code",
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">three_block_tower</span><span class=\"p\">():</span>\n",
" <span class=\"n\">init</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(A, Table)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(B, Table)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(C, A)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Block(A)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Block(B)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Block(C)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Clear(B)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Clear(C)'</span><span class=\"p\">)]</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">goal_test</span><span class=\"p\">(</span><span class=\"n\">kb</span><span class=\"p\">):</span>\n",
" <span class=\"n\">required</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(A, B)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(B, C)'</span><span class=\"p\">)]</span>\n",
" <span class=\"k\">return</span> <span class=\"nb\">all</span><span class=\"p\">(</span><span class=\"n\">kb</span><span class=\"o\">.</span><span class=\"n\">ask</span><span class=\"p\">(</span><span class=\"n\">q</span><span class=\"p\">)</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">False</span> <span class=\"k\">for</span> <span class=\"n\">q</span> <span class=\"ow\">in</span> <span class=\"n\">required</span><span class=\"p\">)</span>\n",
"\n",
" <span class=\"c1\"># Actions</span>\n",
"\n",
" <span class=\"c1\"># Move</span>\n",
" <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(b, x)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Clear(b)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Clear(y)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Block(b)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Block(y)'</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(b, y)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Clear(x)'</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(b, x)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Clear(y)'</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">move</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Move(b, x, y)'</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"c1\"># MoveToTable</span>\n",
" <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(b, x)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Clear(b)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Block(b)'</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
" <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(b, Table)'</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'Clear(x)'</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'On(b, x)'</span><span class=\"p\">)]</span>\n",
" <span class=\"n\">moveToTable</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">'MoveToTable(b, x)'</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span>\n",
" <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"k\">return</span> <span class=\"n\">PDDL</span><span class=\"p\">(</span><span class=\"n\">init</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">move</span><span class=\"p\">,</span> <span class=\"n\">moveToTable</span><span class=\"p\">],</span> <span class=\"n\">goal_test</span><span class=\"p\">)</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [