planning.ipynb 72,9 ko
Newer Older
    "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": [
       "<!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=\"n\">init</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Have(Cake)&#39;</span><span class=\"p\">)]</span>\n",
       "\n",
       "    <span class=\"k\">def</span> <span class=\"nf\">goal_test</span><span class=\"p\">(</span><span class=\"n\">kb</span><span class=\"p\">):</span>\n",
       "        <span class=\"n\">required</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Have(Cake)&#39;</span><span class=\"p\">),</span> <span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Eaten(Cake)&#39;</span><span class=\"p\">)]</span>\n",
       "        <span class=\"k\">return</span> <span class=\"nb\">all</span><span class=\"p\">(</span><span class=\"n\">kb</span><span class=\"o\">.</span><span class=\"n\">ask</span><span class=\"p\">(</span><span class=\"n\">q</span><span class=\"p\">)</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">False</span> <span class=\"k\">for</span> <span class=\"n\">q</span> <span class=\"ow\">in</span> <span class=\"n\">required</span><span class=\"p\">)</span>\n",
       "\n",
       "    <span class=\"c1\"># Actions</span>\n",
       "\n",
       "    <span class=\"c1\"># Eat cake</span>\n",
       "    <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Have(Cake)&#39;</span><span class=\"p\">)]</span>\n",
       "    <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
       "    <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Eaten(Cake)&#39;</span><span class=\"p\">)]</span>\n",
       "    <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Have(Cake)&#39;</span><span class=\"p\">)]</span>\n",
       "    <span class=\"n\">eat_cake</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Eat(Cake)&#39;</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
       "\n",
       "    <span class=\"c1\"># Bake Cake</span>\n",
       "    <span class=\"n\">precond_pos</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
       "    <span class=\"n\">precond_neg</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Have(Cake)&#39;</span><span class=\"p\">)]</span>\n",
       "    <span class=\"n\">effect_add</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Have(Cake)&#39;</span><span class=\"p\">)]</span>\n",
       "    <span class=\"n\">effect_rem</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n",
       "    <span class=\"n\">bake_cake</span> <span class=\"o\">=</span> <span class=\"n\">Action</span><span class=\"p\">(</span><span class=\"n\">expr</span><span class=\"p\">(</span><span class=\"s1\">&#39;Bake(Cake)&#39;</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"n\">precond_pos</span><span class=\"p\">,</span> <span class=\"n\">precond_neg</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"n\">effect_add</span><span class=\"p\">,</span> <span class=\"n\">effect_rem</span><span class=\"p\">])</span>\n",
       "\n",
       "    <span class=\"k\">return</span> <span class=\"n\">PDDL</span><span class=\"p\">(</span><span class=\"n\">init</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">eat_cake</span><span class=\"p\">,</span> <span class=\"n\">bake_cake</span><span class=\"p\">],</span> <span class=\"n\">goal_test</span><span class=\"p\">)</span>\n",
       "</pre></div>\n",
       "</body>\n",
       "</html>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "psource(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_minor": 1