From a83a309c62412a3e1f8cb1ef7159b231d185d100 Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Mon, 2 Apr 2018 11:50:46 +0530 Subject: [PATCH 01/11] added have_cake_and_eat_cake_too --- planning.ipynb | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/planning.ipynb b/planning.ipynb index 5c26e5b5e..e353c7663 100644 --- a/planning.ipynb +++ b/planning.ipynb @@ -594,6 +594,104 @@ "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. " + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "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": 24, + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "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": 26, + "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": { From 7e32b9eb6a4f2e8fc036fc6103848c549564650a Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Mon, 2 Apr 2018 12:06:09 +0530 Subject: [PATCH 02/11] renamed effect_neg to effect_rem --- planning.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planning.ipynb b/planning.ipynb index e353c7663..441e8d83c 100644 --- a/planning.ipynb +++ b/planning.ipynb @@ -59,7 +59,7 @@ "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", + "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_rem`) 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." ] From 4e8c42f1c1d5eee605698128f6b4271ede367dac Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Mon, 2 Apr 2018 12:43:19 +0530 Subject: [PATCH 03/11] added have_cake_and_eat_cake_too to readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e3aa1f9e4..579f63b62 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and | 10.1 | Air-Cargo-problem | `air_cargo` | [`planning.py`][planning] | Done | Included | | 10.2 | Spare-Tire-Problem | `spare_tire` | [`planning.py`][planning] | Done | Included | | 10.3 | Three-Block-Tower | `three_block_tower` | [`planning.py`][planning] | Done | Included | -| 10.7 | Cake-Problem | `have_cake_and_eat_cake_too` | [`planning.py`][planning] | Done | | +| 10.7 | Cake-Problem | `have_cake_and_eat_cake_too` | [`planning.py`][planning] | Done | Included | | 10.9 | Graphplan | `GraphPlan` | [`planning.py`][planning] | | | | 10.13 | Partial-Order-Planner | | | | | | 11.1 | Job-Shop-Problem-With-Resources | `job_shop_problem` | [`planning.py`][planning] | Done | | @@ -186,4 +186,4 @@ Many thanks for contributions over the years. I got bug reports, corrected code, [rl]:../master/rl.py [search]:../master/search.py [utils]:../master/utils.py -[text]:../master/text.py \ No newline at end of file +[text]:../master/text.py From 22e7e91254c829f8de2a869786fb876760feebcd Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Wed, 4 Apr 2018 18:15:28 +0530 Subject: [PATCH 04/11] added details to Problems in planning.ipynb Added more information for the problems Air Cargo, Spare Tire, Three Block Tower, Have Cake and Eat Cake Too. --- planning.ipynb | 737 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 696 insertions(+), 41 deletions(-) diff --git a/planning.ipynb b/planning.ipynb index 441e8d83c..afdb44e2c 100644 --- a/planning.ipynb +++ b/planning.ipynb @@ -26,7 +26,8 @@ "metadata": {}, "outputs": [], "source": [ - "from planning import *" + "from planning import *\n", + "from notebook import psource" ] }, { @@ -59,7 +60,7 @@ "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_rem`) contains all those expressions that would be false if the action is executed (ie. their negations would be true).\n", + "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." ] @@ -302,13 +303,185 @@ "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 with defined with three actions: Load, Unload and Fly. Let us now define an object of `air_cargo` problem:" + "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", + "we have an `initial_state` of having 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 `air_cargo` problem:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, "outputs": [], "source": [ "airCargo = air_cargo()" @@ -323,7 +496,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -342,22 +515,29 @@ "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 achieve\n", - "the goal. Then the `airCargo` acts on each of them." + "It will give us 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", + "Let us have an idea of the actions available to us i.e, 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": 13, + "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", + " 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)" @@ -372,22 +552,19 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 15, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] } ], "source": [ - "airCargo.goal_test()" + "print (airCargo.goal_test())" ] }, { @@ -408,12 +585,169 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let's consider the problem of changing a flat tire. 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. Let us now define an object of `spare_tire` problem:" + "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": 15, + "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": [ @@ -429,7 +763,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -448,12 +782,19 @@ "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." + "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", + "Let us have an idea of the actions available to us i.e, 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": 17, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -474,7 +815,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -507,12 +848,175 @@ "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. Let us now define an object of `three_block_tower` problem:" + "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": 19, + "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": [ @@ -528,7 +1032,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -547,12 +1051,18 @@ "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." + "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", + "Let us have an idea of the actions available to us i.e, 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": 22, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ @@ -573,7 +1083,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 25, "metadata": {}, "outputs": [ { @@ -606,12 +1116,153 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This problem involves the task of eating a cake with an initial condition of having a cake. " + "This problem involves the task of eating a cake with an initial condition of having a cake. First let us look at take a look at `have_cake_and_eat_cake_too`" ] }, { "cell_type": "code", - "execution_count": 23, + "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):** Declraes that we have **' x '**." + ] + }, + { + "cell_type": "code", + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -627,7 +1278,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -646,12 +1297,16 @@ "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." + "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": 25, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ @@ -671,7 +1326,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -710,7 +1365,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.5.2" } }, "nbformat": 4, From c2f2bfa692e0b3eca6af5e60f5924aff478d3f48 Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Wed, 4 Apr 2018 18:53:42 +0530 Subject: [PATCH 05/11] removed a test from planning.py A test for three block tower problem is written here. I have removed it. --- planning.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/planning.py b/planning.py index bb54f2027..b7c1c021d 100644 --- a/planning.py +++ b/planning.py @@ -867,15 +867,3 @@ def goal_test(kb): goal_test, [job_group1, job_group2], resources) -def test_three_block_tower(): - p = three_block_tower() - assert p.goal_test() is False - solution = [expr("MoveToTable(C, A)"), - expr("Move(B, Table, C)"), - expr("Move(A, Table, B)")] - - for action in solution: - p.act(action) - - assert p.goal_test() - From e2e304c808d9dd9ef44cccb099135ee966ce0ef7 Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Wed, 4 Apr 2018 19:53:45 +0530 Subject: [PATCH 06/11] Style fixes --- planning.ipynb | 52 +++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/planning.ipynb b/planning.ipynb index afdb44e2c..1776dba91 100644 --- a/planning.ipynb +++ b/planning.ipynb @@ -463,19 +463,19 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**At(x, a):** The cargo or plane **' x '** is at airport **' a '**.\n", + "**At(x, a):** The cargo or plane **'x'** is at airport **'a'**.\n", "\n", - "**In(c, p):** Cargo **' c '** is in palne **' p '**.\n", + "**In(c, p):** Cargo **'c'** is in palne **'p'**.\n", "\n", - "**Cargo(x):** Declare **' x '** as cargo.\n", + "**Cargo(x):** Declare **'x'** as cargo.\n", "\n", - "**Plane(x):** Declare **' x '** as plane.\n", + "**Plane(x):** Declare **'x'** as plane.\n", "\n", - "**Airport(x):** Declare **' x '** as airport.\n", + "**Airport(x):** Declare **'x'** as airport.\n", "\n", "\n", "\n", - "we have an `initial_state` of having 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 `air_cargo` problem:" + "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 `air_cargo` problem:" ] }, { @@ -515,15 +515,15 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "It will give us 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", + "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", - "Let us have an idea of the actions available to us i.e, Load, Unload, Fly\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", + "**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", + "**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" + "**Unload(c, p, c):** Unload cargo **'c'** from plane **'p'** to airport **'a'**.\n" ] }, { @@ -564,7 +564,7 @@ } ], "source": [ - "print (airCargo.goal_test())" + "print(airCargo.goal_test())" ] }, { @@ -731,16 +731,16 @@ } ], "source": [ - "psource (spare_tire)" + "psource(spare_tire)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**At(x, l):** object **' x '** is at location **' l '**.\n", + "**At(x, l):** object **'x'** is at location **'l'**.\n", "\n", - "**Tire(x):** Declare a tire of type **' x '**.\n", + "**Tire(x):** Declare a tire of type **'x'**.\n", "\n", "Let us now define an object of `spare_tire` problem:" ] @@ -784,11 +784,11 @@ "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", - "Let us have an idea of the actions available to us i.e, Remove, PutOn\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", + "**Remove(obj, loc):** Remove the tire **'obj'** from the location **'loc'**.\n", "\n", - "**PutOn(t, Axle):** Attach the tire **' t '** on the Axle.\n", + "**PutOn(t, Axle):** Attach the tire **'t'** on the Axle.\n", "\n" ] }, @@ -998,18 +998,18 @@ } ], "source": [ - "psource (three_block_tower)" + "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", + "**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", + "**Block(x):** Declares **'x'** as a block.\n", "\n", - "**Clear(x):** To tell that there is nothing on **' x '**.\n", + "**Clear(x):** To tell that there is nothing on **'x'**.\n", " \n", " Let us now define an object of `three_block_tower` problem:" ] @@ -1053,11 +1053,11 @@ "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", - "Let us have an idea of the actions available to us i.e, MoveToTable, Move\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", + "**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" + "**Move(b, x, y):** Move box **'b'** from top of **'x'** to the top of **'y'**.\n" ] }, { @@ -1250,7 +1250,7 @@ } ], "source": [ - "psource (have_cake_and_eat_cake_too)" + "psource(have_cake_and_eat_cake_too)" ] }, { From 3e5247fba0042697d8d8ada187e08435dc063145 Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Wed, 4 Apr 2018 19:55:26 +0530 Subject: [PATCH 07/11] minor style fix --- planning.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planning.ipynb b/planning.ipynb index 1776dba91..92fb2d2a1 100644 --- a/planning.ipynb +++ b/planning.ipynb @@ -456,7 +456,7 @@ } ], "source": [ - "psource (air_cargo)" + "psource(air_cargo)" ] }, { From 50eeba6e1fa18ff3f05936019faee6e54febf345 Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Wed, 4 Apr 2018 20:45:56 +0530 Subject: [PATCH 08/11] fixed a typo --- planning.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planning.ipynb b/planning.ipynb index 92fb2d2a1..4bc75751f 100644 --- a/planning.ipynb +++ b/planning.ipynb @@ -1257,7 +1257,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Have(x):** Declraes that we have **' x '**." + "**Have(x):** Declares that we have **' x '**." ] }, { From 7c801e88eb86e7b07b8a2557f77c40b654c3393b Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Fri, 6 Apr 2018 17:41:35 +0530 Subject: [PATCH 09/11] minor fixes some sentence issues --- planning.ipynb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/planning.ipynb b/planning.ipynb index 4bc75751f..d91be0d4e 100644 --- a/planning.ipynb +++ b/planning.ipynb @@ -475,7 +475,7 @@ "\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 `air_cargo` problem:" + "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:" ] }, { @@ -848,7 +848,7 @@ "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." + "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." ] }, { @@ -1116,7 +1116,7 @@ "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 look at take a look at `have_cake_and_eat_cake_too`" + "This problem involves the task of eating a cake with an initial condition of having a cake. First, let us look take a look at `have_cake_and_eat_cake_too`" ] }, { From aee54545a06fad079b2644809f62e994208efa96 Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Fri, 6 Apr 2018 20:09:24 +0530 Subject: [PATCH 10/11] minor changes --- planning.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planning.ipynb b/planning.ipynb index d91be0d4e..ce4d5ad99 100644 --- a/planning.ipynb +++ b/planning.ipynb @@ -1116,7 +1116,7 @@ "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 look take a look at `have_cake_and_eat_cake_too`" + "This problem involves the task of eating a cake with an initial condition of having a cake. First, let us take a look at the `have_cake_and_eat_cake_too`" ] }, { From f0c31e25d1be2b0bb3d42807774ceb660f90229d Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Fri, 6 Apr 2018 20:11:15 +0530 Subject: [PATCH 11/11] minor fixes --- planning.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planning.ipynb b/planning.ipynb index ce4d5ad99..6a79a3100 100644 --- a/planning.ipynb +++ b/planning.ipynb @@ -1116,7 +1116,7 @@ "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 the `have_cake_and_eat_cake_too`" + "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`" ] }, {