{ "cells": [ { "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:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "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, "metadata": {}, "outputs": [], "source": [ "%psource Action" ] }, { "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, "metadata": {}, "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, "metadata": {}, "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, "metadata": {}, "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, "metadata": {}, "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, "metadata": {}, "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, "metadata": {}, "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, "metadata": {}, "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, "metadata": {}, "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`. " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def air_cargo():\n",
       "    init = [expr('At(C1, SFO)'),\n",
       "            expr('At(C2, JFK)'),\n",
       "            expr('At(P1, SFO)'),\n",
       "            expr('At(P2, JFK)'),\n",
       "            expr('Cargo(C1)'),\n",
       "            expr('Cargo(C2)'),\n",
       "            expr('Plane(P1)'),\n",
       "            expr('Plane(P2)'),\n",
       "            expr('Airport(JFK)'),\n",
       "            expr('Airport(SFO)')]\n",
       "\n",
       "    def goal_test(kb):\n",
       "        required = [expr('At(C1 , JFK)'), expr('At(C2 ,SFO)')]\n",
       "        return all([kb.ask(q) is not False for q in required])\n",
       "\n",
       "    # Actions\n",
       "\n",
       "    #  Load\n",
       "    precond_pos = [expr("At(c, a)"), expr("At(p, a)"), expr("Cargo(c)"), expr("Plane(p)"),\n",
       "                   expr("Airport(a)")]\n",
       "    precond_neg = []\n",
       "    effect_add = [expr("In(c, p)")]\n",
       "    effect_rem = [expr("At(c, a)")]\n",
       "    load = Action(expr("Load(c, p, a)"), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
       "\n",
       "    #  Unload\n",
       "    precond_pos = [expr("In(c, p)"), expr("At(p, a)"), expr("Cargo(c)"), expr("Plane(p)"),\n",
       "                   expr("Airport(a)")]\n",
       "    precond_neg = []\n",
       "    effect_add = [expr("At(c, a)")]\n",
       "    effect_rem = [expr("In(c, p)")]\n",
       "    unload = Action(expr("Unload(c, p, a)"), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
       "\n",
       "    #  Fly\n",
       "    #  Used 'f' instead of 'from' because 'from' is a python keyword and expr uses eval() function\n",
       "    precond_pos = [expr("At(p, f)"), expr("Plane(p)"), expr("Airport(f)"), expr("Airport(to)")]\n",
       "    precond_neg = []\n",
       "    effect_add = [expr("At(p, to)")]\n",
       "    effect_rem = [expr("At(p, f)")]\n",
       "    fly = Action(expr("Fly(p, f, to)"), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
       "\n",
       "    return PDDL(init, [load, unload, fly], goal_test)\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "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": {}, "outputs": [], "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", "execution_count": 13, "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", "execution_count": 14, "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", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(airCargo.goal_test())" ] }, { "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", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def spare_tire():\n",
       "    init = [expr('Tire(Flat)'),\n",
       "            expr('Tire(Spare)'),\n",
       "            expr('At(Flat, Axle)'),\n",
       "            expr('At(Spare, Trunk)')]\n",
       "\n",
       "    def goal_test(kb):\n",
       "        required = [expr('At(Spare, Axle)')]\n",
       "        return all(kb.ask(q) is not False for q in required)\n",
       "\n",
       "    # Actions\n",
       "\n",
       "    # Remove\n",
       "    precond_pos = [expr("At(obj, loc)")]\n",
       "    precond_neg = []\n",
       "    effect_add = [expr("At(obj, Ground)")]\n",
       "    effect_rem = [expr("At(obj, loc)")]\n",
       "    remove = Action(expr("Remove(obj, loc)"), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
       "\n",
       "    # PutOn\n",
       "    precond_pos = [expr("Tire(t)"), expr("At(t, Ground)")]\n",
       "    precond_neg = [expr("At(Flat, Axle)")]\n",
       "    effect_add = [expr("At(t, Axle)")]\n",
       "    effect_rem = [expr("At(t, Ground)")]\n",
       "    put_on = Action(expr("PutOn(t, Axle)"), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
       "\n",
       "    # LeaveOvernight\n",
       "    precond_pos = []\n",
       "    precond_neg = []\n",
       "    effect_add = []\n",
       "    effect_rem = [expr("At(Spare, Ground)"), expr("At(Spare, Axle)"), expr("At(Spare, Trunk)"),\n",
       "                  expr("At(Flat, Ground)"), expr("At(Flat, Axle)"), expr("At(Flat, Trunk)")]\n",
       "    leave_overnight = Action(expr("LeaveOvernight"), [precond_pos, precond_neg],\n",
       "                             [effect_add, effect_rem])\n",
       "\n",
       "    return PDDL(init, [remove, put_on, leave_overnight], goal_test)\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "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, "metadata": {}, "outputs": [], "source": [ "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", "execution_count": 18, "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", "execution_count": 19, "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", "execution_count": 20, "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", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def three_block_tower():\n",
       "    init = [expr('On(A, Table)'),\n",
       "            expr('On(B, Table)'),\n",
       "            expr('On(C, A)'),\n",
       "            expr('Block(A)'),\n",
       "            expr('Block(B)'),\n",
       "            expr('Block(C)'),\n",
       "            expr('Clear(B)'),\n",
       "            expr('Clear(C)')]\n",
       "\n",
       "    def goal_test(kb):\n",
       "        required = [expr('On(A, B)'), expr('On(B, C)')]\n",
       "        return all(kb.ask(q) is not False for q in required)\n",
       "\n",
       "    # Actions\n",
       "\n",
       "    #  Move\n",
       "    precond_pos = [expr('On(b, x)'), expr('Clear(b)'), expr('Clear(y)'), expr('Block(b)'),\n",
       "                   expr('Block(y)')]\n",
       "    precond_neg = []\n",
       "    effect_add = [expr('On(b, y)'), expr('Clear(x)')]\n",
       "    effect_rem = [expr('On(b, x)'), expr('Clear(y)')]\n",
       "    move = Action(expr('Move(b, x, y)'), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
       "\n",
       "    #  MoveToTable\n",
       "    precond_pos = [expr('On(b, x)'), expr('Clear(b)'), expr('Block(b)')]\n",
       "    precond_neg = []\n",
       "    effect_add = [expr('On(b, Table)'), expr('Clear(x)')]\n",
       "    effect_rem = [expr('On(b, x)')]\n",
       "    moveToTable = Action(expr('MoveToTable(b, x)'), [precond_pos, precond_neg],\n",
       "                         [effect_add, effect_rem])\n",
       "\n",
       "    return PDDL(init, [move, moveToTable], goal_test)\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "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", "**Block(x):** Declares **'x'** as a block.\n", "\n", "**Clear(x):** To tell that there is nothing on **'x'**.\n", " \n", " Let us now define an object of `three_block_tower` problem:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "three_block_tower = three_block_tower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, before taking any actions, we will check `three_tower_block` if it has completed the goal it is required to do" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(three_block_tower.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 build a stack of three blocks. Then the `three_block_tower` acts on each of them.\n", "\n", "The actions available to us are the following: MoveToTable, Move\n", "\n", "**MoveToTable(b, x):** Move the box **'b'** which is on top of box **'x'** to the table.\n", "\n", "**Move(b, x, y):** Move box **'b'** from top of **'x'** to the top of **'y'**.\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "solution = [expr(\"MoveToTable(C, A)\"),\n", " expr(\"Move(B, Table, C)\"),\n", " expr(\"Move(A, Table, B)\")]\n", "\n", "for action in solution:\n", " three_block_tower.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" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(three_block_tower.goal_test())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has now successfully achieved its goal i.e, to build a stack of three blocks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Have Cake and Eat Cake Too" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This problem involves the task of eating a cake with an initial condition of having a cake. First, let us take a look at `have_cake_and_eat_cake_too`" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def have_cake_and_eat_cake_too():\n",
       "    init = [expr('Have(Cake)')]\n",
       "\n",
       "    def goal_test(kb):\n",
       "        required = [expr('Have(Cake)'), expr('Eaten(Cake)')]\n",
       "        return all(kb.ask(q) is not False for q in required)\n",
       "\n",
       "    # Actions\n",
       "\n",
       "    # Eat cake\n",
       "    precond_pos = [expr('Have(Cake)')]\n",
       "    precond_neg = []\n",
       "    effect_add = [expr('Eaten(Cake)')]\n",
       "    effect_rem = [expr('Have(Cake)')]\n",
       "    eat_cake = Action(expr('Eat(Cake)'), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
       "\n",
       "    # Bake Cake\n",
       "    precond_pos = []\n",
       "    precond_neg = [expr('Have(Cake)')]\n",
       "    effect_add = [expr('Have(Cake)')]\n",
       "    effect_rem = []\n",
       "    bake_cake = Action(expr('Bake(Cake)'), [precond_pos, precond_neg], [effect_add, effect_rem])\n",
       "\n",
       "    return PDDL(init, [eat_cake, bake_cake], goal_test)\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(have_cake_and_eat_cake_too)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Have(x):** Declares that we have **' x '**." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "have_cake_and_eat_cake_too = have_cake_and_eat_cake_too()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First let us check wether the goal state (have cake and eat cake) is reached or not." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(have_cake_and_eat_cake_too.goal_test())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As the goal state is not reached we will make some actions and we will let `have_cake_and_eat_cake_too` act on them. To eat the cake we need to bake it. Let us look at the actions that we can do.\n", "\n", "**Bake(x):** To bake **' x '**.\n", "\n", "**Eat(x):** To eat **' x '**." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "solution = [expr(\"Bake(cake)\"),\n", " expr(\"Eat(cake)\")]\n", "\n", "for action in solution:\n", " have_cake_and_eat_cake_too.act(action)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have made actions to bake the cake and eat the cake. The goal state is **having and eating the cake**. Let us check if it is reached or not." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(have_cake_and_eat_cake_too.goal_test())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has now successfully achieved its goal i.e, to have and eat the cake." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }