Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
"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=\"sd\">"""Spare tire problem"""</span>\n",
"\n",
" <span class=\"k\">return</span> <span class=\"n\">PDDL</span><span class=\"p\">(</span><span class=\"n\">init</span><span class=\"o\">=</span><span class=\"s1\">'Tire(Flat) & Tire(Spare) & At(Flat, Axle) & At(Spare, Trunk)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">goals</span><span class=\"o\">=</span><span class=\"s1\">'At(Spare, Axle) & At(Flat, Ground)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">actions</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"s1\">'Remove(obj, loc)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">precond</span><span class=\"o\">=</span><span class=\"s1\">'At(obj, loc)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">effect</span><span class=\"o\">=</span><span class=\"s1\">'At(obj, Ground) & ~At(obj, loc)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"s1\">'PutOn(t, Axle)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">precond</span><span class=\"o\">=</span><span class=\"s1\">'Tire(t) & At(t, Ground) & ~At(Flat, Axle)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">effect</span><span class=\"o\">=</span><span class=\"s1\">'At(t, Axle) & ~At(t, Ground)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"s1\">'LeaveOvernight'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">precond</span><span class=\"o\">=</span><span class=\"s1\">''</span><span class=\"p\">,</span>\n",
" <span class=\"n\">effect</span><span class=\"o\">=</span><span class=\"s1\">'~At(Spare, Ground) & ~At(Spare, Axle) & ~At(Spare, Trunk) & </span><span class=\"se\">\\</span>\n",
"<span class=\"s1\"> ~At(Flat, Ground) & ~At(Flat, Axle) & ~At(Flat, Trunk)'</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(obj, loc):** object **'obj'** is at location **'loc'**.\n",
"\n",
"**~At(obj, loc):** object **'obj'** is _not_ at location **'loc'**.\n",
"\n",
"**Tire(t):** Declare a tire of type **'t'**.\n",
"\n",
"Let us now define an object of `spare_tire` problem:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"spareTire = spare_tire()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before taking any actions, we will check if `spare_tire` has reached its goal:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"print(spareTire.goal_test())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we can see, it hasn't completed the goal. \n",
"We now define a possible solution that can help us reach the goal of having a spare tire mounted onto the car's axle. \n",
"The actions are then carried out on the `spareTire` PDDL.\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",
"**LeaveOvernight():** We live in a particularly bad neighborhood and all tires, flat or not, are stolen if we leave them overnight.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"solution = [expr(\"Remove(Flat, Axle)\"),\n",
" expr(\"Remove(Spare, Trunk)\"),\n",
" expr(\"PutOn(Spare, Axle)\")]\n",
"\n",
"for action in solution:\n",
" spareTire.act(action)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(spareTire.goal_test())"
]
},
{
"cell_type": "markdown",
"source": [
"This is a valid solution.\n",
"<br>\n",
"Another possible solution is"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true
},
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
"spareTire = spare_tire()\n",
"\n",
"solution = [expr('Remove(Spare, Trunk)'),\n",
" expr('Remove(Flat, Axle)'),\n",
" expr('PutOn(Spare, Axle)')]\n",
"\n",
"for action in solution:\n",
" spareTire.act(action)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(spareTire.goal_test())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that both solutions work, which means that the problem can be solved irrespective of the order in which the `Remove` actions take place, as long as both `Remove` actions take place before the `PutOn` action."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have successfully mounted a spare tire onto the 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. \n",
"The blocks can be stacked, but only one block can fit directly on top of another.\n",
"A robot arm can pick up a block and move it to another position, either on the table or on top of another block. \n",
"The arm can pick up only one block at a time, so it cannot pick up a block that has another one on it. \n",
"The goal will always be to build one or more stacks of blocks. \n",
"In our case, we consider only three blocks.\n",
"The particular configuration we will use is called the Sussman anomaly after Prof. Gerry Sussman."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's take a look at the definition of `three_block_tower()` in the module."
]
},
{
"cell_type": "code",
"execution_count": 24,
"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=\"sd\">"""Sussman Anomaly problem"""</span>\n",
"\n",
" <span class=\"k\">return</span> <span class=\"n\">PDDL</span><span class=\"p\">(</span><span class=\"n\">init</span><span class=\"o\">=</span><span class=\"s1\">'On(A, Table) & On(B, Table) & On(C, A) & Block(A) & Block(B) & Block(C) & Clear(B) & Clear(C)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">goals</span><span class=\"o\">=</span><span class=\"s1\">'On(A, B) & On(B, C)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">actions</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"s1\">'Move(b, x, y)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">precond</span><span class=\"o\">=</span><span class=\"s1\">'On(b, x) & Clear(b) & Clear(y) & Block(b) & Block(y)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">effect</span><span class=\"o\">=</span><span class=\"s1\">'On(b, y) & Clear(x) & ~On(b, x) & ~Clear(y)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"s1\">'MoveToTable(b, x)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">precond</span><span class=\"o\">=</span><span class=\"s1\">'On(b, x) & Clear(b) & Block(b)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">effect</span><span class=\"o\">=</span><span class=\"s1\">'On(b, Table) & Clear(x) & ~On(b, x)'</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(three_block_tower)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**On(b, x):** The block **'b'** is on **'x'**. **'x'** can be a table or a block.\n",
"\n",
"**~On(b, x):** The block **'b'** is _not_ on **'x'**. **'x'** can be a table or a block.\n",
"\n",
"**Block(b):** Declares **'b'** as a block.\n",
"\n",
"**Clear(x):** To indicate that there is nothing on **'x'** and it is free to be moved around.\n",
"\n",
"**~Clear(x):** To indicate that there is something on **'x'** and it cannot be moved.\n",
" \n",
" Let us now define an object of `three_block_tower` problem:"
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"outputs": [],
"threeBlockTower = three_block_tower()"
"Before taking any actions, we will check if `threeBlockTower` has reached its goal:"
"cell_type": "code",
"execution_count": 26,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"print(threeBlockTower.goal_test())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we can see, it hasn't completed the goal. \n",
"We now define a sequence of actions that can stack three blocks in the required order. \n",
"The actions are then carried out on the `threeBlockTower` PDDL.\n",
"The actions available to us are the following: MoveToTable, Move\n",
"\n",
"**MoveToTable(b, x): ** Move box **'b'** stacked on **'x'** to the table, given that box **'b'** is clear.\n",
"\n",
"**Move(b, x, y): ** Move box **'b'** stacked on **'x'** to the top of **'y'**, given that both **'b'** and **'y'** are clear.\n"
"execution_count": 27,
"metadata": {
"collapsed": true
},
"solution = [expr(\"MoveToTable(C, A)\"),\n",
" expr(\"Move(B, Table, C)\"),\n",
" expr(\"Move(A, Table, B)\")]\n",
"\n",
"for action in solution:\n",
" threeBlockTower.act(action)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As the `three_block_tower` has taken all the steps it needed in order to achieve the goal, we can now check if it has acheived its goal."
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"print(threeBlockTower.goal_test())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It has now successfully achieved its goal i.e, to build a stack of three blocks in the specified order."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This problem requires us to acquire a carton of milk, a banana and a drill.\n",
"Initially, we start from home and it is known to us that milk and bananas are available in the supermarket and the hardware store sells drills.\n",
"Let's take a look at the definition of the `shopping_problem` in the module."
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
"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\">shopping_problem</span><span class=\"p\">():</span>\n",
" <span class=\"sd\">"""Shopping problem"""</span>\n",
"\n",
" <span class=\"k\">return</span> <span class=\"n\">PDDL</span><span class=\"p\">(</span><span class=\"n\">init</span><span class=\"o\">=</span><span class=\"s1\">'At(Home) & Sells(SM, Milk) & Sells(SM, Banana) & Sells(HW, Drill)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">goals</span><span class=\"o\">=</span><span class=\"s1\">'Have(Milk) & Have(Banana) & Have(Drill)'</span><span class=\"p\">,</span> \n",
" <span class=\"n\">actions</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"s1\">'Buy(x, store)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">precond</span><span class=\"o\">=</span><span class=\"s1\">'At(store) & Sells(store, x)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">effect</span><span class=\"o\">=</span><span class=\"s1\">'Have(x)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"s1\">'Go(x, y)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">precond</span><span class=\"o\">=</span><span class=\"s1\">'At(x)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">effect</span><span class=\"o\">=</span><span class=\"s1\">'At(y) & ~At(x)'</span><span class=\"p\">)])</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"<IPython.core.display.HTML object>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**At(x):** Indicates that we are currently at **'x'** where **'x'** can be Home, SM (supermarket) or HW (Hardware store).\n",
"**~At(x):** Indicates that we are currently _not_ at **'x'**.\n",
"\n",
"**Sells(s, x):** Indicates that item **'x'** can be bought from store **'s'**.\n",
"\n",
"**Have(x):** Indicates that we possess the item **'x'**."
"execution_count": 30,
"metadata": {
"collapsed": true
},
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
"shoppingProblem = shopping_problem()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's first check whether the goal state Have(Milk), Have(Banana), Have(Drill) is reached or not."
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"print(shoppingProblem.goal_test())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's look at the possible actions\n",
"**Buy(x, store):** Buy an item **'x'** from a **'store'** given that the **'store'** sells **'x'**.\n",
"**Go(x, y):** Go to destination **'y'** starting from source **'x'**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now define a valid solution that will help us reach the goal.\n",
"The sequence of actions will then be carried out onto the `shoppingProblem` PDDL."
"execution_count": 32,
"metadata": {
"collapsed": true
},
"solution = [expr('Go(Home, SM)'),\n",
" expr('Buy(Milk, SM)'),\n",
" expr('Buy(Banana, SM)'),\n",
" expr('Go(SM, HW)'),\n",
" expr('Buy(Drill, HW)')]\n",
"\n",
"for action in solution:\n",
" shoppingProblem.act(action)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have taken the steps required to acquire all the stuff we need. \n",
"Let's see if we have reached our goal."
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It has now successfully achieved the goal."
{
"cell_type": "markdown",
"metadata": {},
"source": [
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This problem requires us to reach the state of having a cake and having eaten a cake simlutaneously, given a single cake.\n",
"Let's first take a look at the definition of the `have_cake_and_eat_cake_too` problem in the module."
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
"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\">have_cake_and_eat_cake_too</span><span class=\"p\">():</span>\n",
" <span class=\"sd\">"""Cake problem"""</span>\n",
"\n",
" <span class=\"k\">return</span> <span class=\"n\">PDDL</span><span class=\"p\">(</span><span class=\"n\">init</span><span class=\"o\">=</span><span class=\"s1\">'Have(Cake)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">goals</span><span class=\"o\">=</span><span class=\"s1\">'Have(Cake) & Eaten(Cake)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">actions</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"s1\">'Eat(Cake)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">precond</span><span class=\"o\">=</span><span class=\"s1\">'Have(Cake)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">effect</span><span class=\"o\">=</span><span class=\"s1\">'Eaten(Cake) & ~Have(Cake)'</span><span class=\"p\">),</span>\n",
" <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"s1\">'Bake(Cake)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">precond</span><span class=\"o\">=</span><span class=\"s1\">'~Have(Cake)'</span><span class=\"p\">,</span>\n",
" <span class=\"n\">effect</span><span class=\"o\">=</span><span class=\"s1\">'Have(Cake)'</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(have_cake_and_eat_cake_too)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since this problem doesn't involve variables, states can be considered similar to symbols in propositional logic.\n",
"**Have(Cake):** Declares that we have a **'Cake'**.\n",
"**~Have(Cake):** Declares that we _don't_ have a **'Cake'**."
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": true
},
"cakeProblem = have_cake_and_eat_cake_too()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First let us check whether the goal state 'Have(Cake)' and 'Eaten(Cake)' are reached or not."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us look at the possible actions.\n",
"**Bake(x):** To bake **' x '**.\n",
"source": [
"We now define a valid solution that can help us reach the goal.\n",
"The sequence of actions will then be acted upon the `cakeProblem` PDDL."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": true
},
"solution = [expr(\"Eat(Cake)\"),\n",
" expr(\"Bake(Cake)\")]\n",
"\n",
"for action in solution:\n",
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we have made actions to bake the cake and eat the cake. Let us check if we have reached the goal."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [