{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Planning\n", "#### Chapters 10-11\n", "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook serves as supporting material for topics covered in **Chapter 10 - Classical Planning** and **Chapter 11 - Planning and Acting in the Real World** from the book *[Artificial Intelligence: A Modern Approach](http://aima.cs.berkeley.edu)*. \n", "This notebook uses implementations from the [planning.py](https://github.com/aimacode/aima-python/blob/master/planning.py) module. \n", "See the [intro notebook](https://github.com/aimacode/aima-python/blob/master/intro.ipynb) for instructions.\n", "\n", "We'll start by looking at `PlanningProblem` and `Action` data types for defining problems and actions. \n", "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) \n", "followed by some common planning problems and methods of solving them.\n", "\n", "Let's start by importing everything from the planning module." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from planning import *\n", "from notebook import psource" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CONTENTS\n", "\n", "**Classical Planning**\n", "- PlanningProblem\n", "- Action\n", "- Planning Problems\n", " * Air cargo problem\n", " * Spare tire problem\n", " * Three block tower problem\n", " * Shopping Problem\n", " * Socks and shoes problem\n", " * Cake problem\n", "- Solving Planning Problems\n", " * GraphPlan\n", " * Linearize\n", " * PartialOrderPlanner\n", "
\n", "\n", "**Planning in the real world**\n", "- Problem\n", "- HLA\n", "- Planning Problems\n", " * Job shop problem\n", " * Double tennis problem\n", "- Solving Planning Problems\n", " * Hierarchical Search\n", " * Angelic Search" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PlanningProblem\n", "\n", "PDDL stands for Planning Domain Definition Language.\n", "The `PlanningProblem` class is used to represent planning problems in this module. The following attributes are essential to be able to define a problem:\n", "* an initial state\n", "* a set of goals\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": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class PlanningProblem:\n",
       "    """\n",
       "    Planning Domain Definition Language (PlanningProblem) used to define a search problem.\n",
       "    It stores states in a knowledge base consisting of first order logic statements.\n",
       "    The conjunction of these logical statements completely defines a state.\n",
       "    """\n",
       "\n",
       "    def __init__(self, init, goals, actions):\n",
       "        self.init = self.convert(init)\n",
       "        self.goals = self.convert(goals)\n",
       "        self.actions = actions\n",
       "\n",
       "    def convert(self, clauses):\n",
       "        """Converts strings into exprs"""\n",
       "        if not isinstance(clauses, Expr):\n",
       "            if len(clauses) > 0:\n",
       "                clauses = expr(clauses)\n",
       "            else:\n",
       "                clauses = []\n",
       "        try:\n",
       "            clauses = conjuncts(clauses)\n",
       "        except AttributeError:\n",
       "            clauses = clauses\n",
       "\n",
       "        new_clauses = []\n",
       "        for clause in clauses:\n",
       "            if clause.op == '~':\n",
       "                new_clauses.append(expr('Not' + str(clause.args[0])))\n",
       "            else:\n",
       "                new_clauses.append(clause)\n",
       "        return new_clauses\n",
       "\n",
       "    def goal_test(self):\n",
       "        """Checks if the goals have been reached"""\n",
       "        return all(goal in self.init for goal in self.goals)\n",
       "\n",
       "    def act(self, action):\n",
       "        """\n",
       "        Performs the action given as argument.\n",
       "        Note that action is an Expr like expr('Remove(Glass, Table)') or expr('Eat(Sandwich)')\n",
       "        """       \n",
       "        action_name = action.op\n",
       "        args = action.args\n",
       "        list_action = first(a for a in self.actions if a.name == action_name)\n",
       "        if list_action is None:\n",
       "            raise Exception("Action '{}' not found".format(action_name))\n",
       "        if not list_action.check_precond(self.init, args):\n",
       "            raise Exception("Action '{}' pre-conditions not satisfied".format(action))\n",
       "        self.init = list_action(self.init, args).clauses\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(PlanningProblem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `init` attribute is an expression that forms the initial knowledge base for the problem.\n", "
\n", "The `goals` attribute is an expression that indicates the goals to be reached by the problem.\n", "
\n", "Lastly, `actions` contains a list of `Action` objects that may be executed in the search space of the problem.\n", "
\n", "The `goal_test` method checks if the goal has been reached.\n", "
\n", "The `act` method acts out the given action and updates the current state.\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ACTION\n", "\n", "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": [ "The module models actions using the `Action` class" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class Action:\n",
       "    """\n",
       "    Defines an action schema using preconditions and effects.\n",
       "    Use this to describe actions in PlanningProblem.\n",
       "    action is an Expr where variables are given as arguments(args).\n",
       "    Precondition and effect are both lists with positive and negative literals.\n",
       "    Negative preconditions and effects are defined by adding a 'Not' before the name of the clause\n",
       "    Example:\n",
       "    precond = [expr("Human(person)"), expr("Hungry(Person)"), expr("NotEaten(food)")]\n",
       "    effect = [expr("Eaten(food)"), expr("Hungry(person)")]\n",
       "    eat = Action(expr("Eat(person, food)"), precond, effect)\n",
       "    """\n",
       "\n",
       "    def __init__(self, action, precond, effect):\n",
       "        if isinstance(action, str):\n",
       "            action = expr(action)\n",
       "        self.name = action.op\n",
       "        self.args = action.args\n",
       "        self.precond = self.convert(precond)\n",
       "        self.effect = self.convert(effect)\n",
       "\n",
       "    def __call__(self, kb, args):\n",
       "        return self.act(kb, args)\n",
       "\n",
       "    def __repr__(self):\n",
       "        return '{}({})'.format(self.__class__.__name__, Expr(self.name, *self.args))\n",
       "\n",
       "    def convert(self, clauses):\n",
       "        """Converts strings into Exprs"""\n",
       "        if isinstance(clauses, Expr):\n",
       "            clauses = conjuncts(clauses)\n",
       "            for i in range(len(clauses)):\n",
       "                if clauses[i].op == '~':\n",
       "                    clauses[i] = expr('Not' + str(clauses[i].args[0]))\n",
       "\n",
       "        elif isinstance(clauses, str):\n",
       "            clauses = clauses.replace('~', 'Not')\n",
       "            if len(clauses) > 0:\n",
       "                clauses = expr(clauses)\n",
       "\n",
       "            try:\n",
       "                clauses = conjuncts(clauses)\n",
       "            except AttributeError:\n",
       "                pass\n",
       "\n",
       "        return clauses\n",
       "\n",
       "    def substitute(self, e, args):\n",
       "        """Replaces variables in expression with their respective Propositional symbol"""\n",
       "\n",
       "        new_args = list(e.args)\n",
       "        for num, x in enumerate(e.args):\n",
       "            for i, _ in enumerate(self.args):\n",
       "                if self.args[i] == x:\n",
       "                    new_args[num] = args[i]\n",
       "        return Expr(e.op, *new_args)\n",
       "\n",
       "    def check_precond(self, kb, args):\n",
       "        """Checks if the precondition is satisfied in the current state"""\n",
       "\n",
       "        if isinstance(kb, list):\n",
       "            kb = FolKB(kb)\n",
       "        for clause in self.precond:\n",
       "            if self.substitute(clause, args) not in kb.clauses:\n",
       "                return False\n",
       "        return True\n",
       "\n",
       "    def act(self, kb, args):\n",
       "        """Executes the action on the state's knowledge base"""\n",
       "\n",
       "        if isinstance(kb, list):\n",
       "            kb = FolKB(kb)\n",
       "\n",
       "        if not self.check_precond(kb, args):\n",
       "            raise Exception('Action pre-conditions not satisfied')\n",
       "        for clause in self.effect:\n",
       "            kb.tell(self.substitute(clause, args))\n",
       "            if clause.op[:3] == 'Not':\n",
       "                new_clause = Expr(clause.op[3:], *clause.args)\n",
       "\n",
       "                if kb.ask(self.substitute(new_clause, args)) is not False:\n",
       "                    kb.retract(self.substitute(new_clause, args))\n",
       "            else:\n",
       "                new_clause = Expr('Not' + clause.op, *clause.args)\n",
       "\n",
       "                if kb.ask(self.substitute(new_clause, args)) is not False:    \n",
       "                    kb.retract(self.substitute(new_clause, args))\n",
       "\n",
       "        return kb\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(Action)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This class represents an action given the expression, the preconditions and its effects. \n", "A list `precond` stores the preconditions of the action and a list `effect` stores its effects.\n", "Negative preconditions and effects are input using a `~` symbol before the clause, which are internally prefixed with a `Not` to make it easier to work with.\n", "For example, the negation of `At(obj, loc)` will be input as `~At(obj, loc)` and internally represented as `NotAt(obj, loc)`. \n", "This equivalently creates a new clause for each negative literal, removing the hassle of maintaining two separate knowledge bases.\n", "This greatly simplifies algorithms like `GraphPlan` as we will see later.\n", "The `convert` method takes an input string, parses it, removes conjunctions if any and returns a list of `Expr` objects.\n", "The `check_precond` method checks if the preconditions for that action are valid, given a `kb`.\n", "The `act` method carries out the action on the given knowledge base." ] }, { "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": { "collapsed": true }, "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": { "collapsed": true }, "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": { "collapsed": true }, "outputs": [], "source": [ "#Sibiu to Bucharest\n", "precond = 'At(Sibiu)'\n", "effect = 'At(Bucharest) & ~At(Sibiu)'\n", "fly_s_b = Action('Fly(Sibiu, Bucharest)', precond, effect)\n", "\n", "#Bucharest to Sibiu\n", "precond = 'At(Bucharest)'\n", "effect = 'At(Sibiu) & ~At(Bucharest)'\n", "fly_b_s = Action('Fly(Bucharest, Sibiu)', precond, effect)\n", "\n", "#Sibiu to Craiova\n", "precond = 'At(Sibiu)'\n", "effect = 'At(Craiova) & ~At(Sibiu)'\n", "fly_s_c = Action('Fly(Sibiu, Craiova)', precond, effect)\n", "\n", "#Craiova to Sibiu\n", "precond = 'At(Craiova)'\n", "effect = 'At(Sibiu) & ~At(Craiova)'\n", "fly_c_s = Action('Fly(Craiova, Sibiu)', precond, effect)\n", "\n", "#Bucharest to Craiova\n", "precond = 'At(Bucharest)'\n", "effect = 'At(Craiova) & ~At(Bucharest)'\n", "fly_b_c = Action('Fly(Bucharest, Craiova)', precond, effect)\n", "\n", "#Craiova to Bucharest\n", "precond = 'At(Craiova)'\n", "effect = 'At(Bucharest) & ~At(Craiova)'\n", "fly_c_b = Action('Fly(Craiova, Bucharest)', precond, effect)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the drive actions like this." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Drive\n", "precond = 'At(x)'\n", "effect = 'At(y) & ~At(x)'\n", "drive = Action('Drive(x, y)', precond, effect)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our goal is defined as" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "goals = 'At(Bucharest)'" ] }, { "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": 10, "metadata": { "collapsed": true }, "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": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "prob = PlanningProblem(knowledge_base, goals, [fly_s_b, fly_b_s, fly_s_c, fly_c_s, fly_b_c, fly_c_b, drive])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PLANNING PROBLEMS\n", "---\n", "\n", "## Air Cargo Problem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the Air Cargo problem, we start with cargo at two airports, SFO and JFK. Our goal is to send each cargo to the other airport. We have two airplanes to help us accomplish the task. \n", "The problem can be defined with three actions: Load, Unload and Fly. \n", "Let us look how the `air_cargo` problem has been defined in the module. " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def air_cargo():\n",
       "    """\n",
       "    [Figure 10.1] AIR-CARGO-PROBLEM\n",
       "\n",
       "    An air-cargo shipment problem for delivering cargo to different locations,\n",
       "    given the starting location and airplanes.\n",
       "\n",
       "    Example:\n",
       "    >>> from planning import *\n",
       "    >>> ac = air_cargo()\n",
       "    >>> ac.goal_test()\n",
       "    False\n",
       "    >>> ac.act(expr('Load(C2, P2, JFK)'))\n",
       "    >>> ac.act(expr('Load(C1, P1, SFO)'))\n",
       "    >>> ac.act(expr('Fly(P1, SFO, JFK)'))\n",
       "    >>> ac.act(expr('Fly(P2, JFK, SFO)'))\n",
       "    >>> ac.act(expr('Unload(C2, P2, SFO)'))\n",
       "    >>> ac.goal_test()\n",
       "    False\n",
       "    >>> ac.act(expr('Unload(C1, P1, JFK)'))\n",
       "    >>> ac.goal_test()\n",
       "    True\n",
       "    >>>\n",
       "    """\n",
       "\n",
       "    return PlanningProblem(init='At(C1, SFO) & At(C2, JFK) & At(P1, SFO) & At(P2, JFK) & Cargo(C1) & Cargo(C2) & Plane(P1) & Plane(P2) & Airport(SFO) & Airport(JFK)', \n",
       "                goals='At(C1, JFK) & At(C2, SFO)',\n",
       "                actions=[Action('Load(c, p, a)', \n",
       "                                precond='At(c, a) & At(p, a) & Cargo(c) & Plane(p) & Airport(a)',\n",
       "                                effect='In(c, p) & ~At(c, a)'),\n",
       "                         Action('Unload(c, p, a)',\n",
       "                                precond='In(c, p) & At(p, a) & Cargo(c) & Plane(p) & Airport(a)',\n",
       "                                effect='At(c, a) & ~In(c, p)'),\n",
       "                         Action('Fly(p, f, to)',\n",
       "                                precond='At(p, f) & Plane(p) & Airport(f) & Airport(to)',\n",
       "                                effect='At(p, to) & ~At(p, f)')])\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(air_cargo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**At(c, a):** The cargo **'c'** is at airport **'a'**.\n", "\n", "**~At(c, a):** The cargo **'c'** is _not_ at airport **'a'**.\n", "\n", "**In(c, p):** Cargo **'c'** is in plane **'p'**.\n", "\n", "**~In(c, p):** Cargo **'c'** is _not_ in plane **'p'**.\n", "\n", "**Cargo(c):** Declare **'c'** as cargo.\n", "\n", "**Plane(p):** Declare **'p'** as plane.\n", "\n", "**Airport(a):** Declare **'a'** 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. \n", "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": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "airCargo = air_cargo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before taking any actions, we will check if `airCargo` has reached its goal:" ] }, { "cell_type": "code", "execution_count": 14, "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.\n", "The actions are then carried out on the `airCargo` PlanningProblem.\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, a):** Unload cargo **'c'** from plane **'p'** to airport **'a'**.\n", "\n", "This problem can have multiple valid solutions.\n", "One such solution is shown below." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "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": 16, "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. \n", "The goal is to mount a spare tire onto the car's axle, given that we have a flat tire on the axle and a spare tire in the trunk. " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def spare_tire():\n",
       "    """[Figure 10.2] SPARE-TIRE-PROBLEM\n",
       "\n",
       "    A problem involving changing the flat tire of a car\n",
       "    with a spare tire from the trunk.\n",
       "\n",
       "    Example:\n",
       "    >>> from planning import *\n",
       "    >>> st = spare_tire()\n",
       "    >>> st.goal_test()\n",
       "    False\n",
       "    >>> st.act(expr('Remove(Spare, Trunk)'))\n",
       "    >>> st.act(expr('Remove(Flat, Axle)'))\n",
       "    >>> st.goal_test()\n",
       "    False\n",
       "    >>> st.act(expr('PutOn(Spare, Axle)'))\n",
       "    >>> st.goal_test()\n",
       "    True\n",
       "    >>>\n",
       "    """\n",
       "\n",
       "    return PlanningProblem(init='Tire(Flat) & Tire(Spare) & At(Flat, Axle) & At(Spare, Trunk)',\n",
       "                goals='At(Spare, Axle) & At(Flat, Ground)',\n",
       "                actions=[Action('Remove(obj, loc)',\n",
       "                                precond='At(obj, loc)',\n",
       "                                effect='At(obj, Ground) & ~At(obj, loc)'),\n",
       "                         Action('PutOn(t, Axle)',\n",
       "                                precond='Tire(t) & At(t, Ground) & ~At(Flat, Axle)',\n",
       "                                effect='At(t, Axle) & ~At(t, Ground)'),\n",
       "                         Action('LeaveOvernight',\n",
       "                                precond='',\n",
       "                                effect='~At(Spare, Ground) & ~At(Spare, Axle) & ~At(Spare, Trunk) & \\\n",
       "                                        ~At(Flat, Ground) & ~At(Flat, Axle) & ~At(Flat, Trunk)')])\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "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` PlanningProblem.\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", "metadata": {}, "source": [ "This is a valid solution.\n", "
\n", "Another possible solution is" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "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": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def three_block_tower():\n",
       "    """\n",
       "    [Figure 10.3] THREE-BLOCK-TOWER\n",
       "\n",
       "    A blocks-world problem of stacking three blocks in a certain configuration,\n",
       "    also known as the Sussman Anomaly.\n",
       "\n",
       "    Example:\n",
       "    >>> from planning import *\n",
       "    >>> tbt = three_block_tower()\n",
       "    >>> tbt.goal_test()\n",
       "    False\n",
       "    >>> tbt.act(expr('MoveToTable(C, A)'))\n",
       "    >>> tbt.act(expr('Move(B, Table, C)'))\n",
       "    >>> tbt.goal_test()\n",
       "    False\n",
       "    >>> tbt.act(expr('Move(A, Table, B)'))\n",
       "    >>> tbt.goal_test()\n",
       "    True\n",
       "    >>>\n",
       "    """\n",
       "\n",
       "    return PlanningProblem(init='On(A, Table) & On(B, Table) & On(C, A) & Block(A) & Block(B) & Block(C) & Clear(B) & Clear(C)',\n",
       "                goals='On(A, B) & On(B, C)',\n",
       "                actions=[Action('Move(b, x, y)',\n",
       "                                precond='On(b, x) & Clear(b) & Clear(y) & Block(b) & Block(y)',\n",
       "                                effect='On(b, y) & Clear(x) & ~On(b, x) & ~Clear(y)'),\n",
       "                         Action('MoveToTable(b, x)',\n",
       "                                precond='On(b, x) & Clear(b) & Block(b)',\n",
       "                                effect='On(b, Table) & Clear(x) & ~On(b, x)')])\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", "**~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": [], "source": [ "threeBlockTower = three_block_tower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before taking any actions, we will check if `threeBlockTower` has reached its goal:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "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` PlanningProblem.\n", "\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" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "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", " 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." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "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": [ "The `three_block_tower` problem can also be defined in simpler terms using just two actions `ToTable(x, y)` and `FromTable(x, y)`.\n", "The underlying problem remains the same however, stacking up three blocks in a certain configuration given a particular starting state.\n", "Let's have a look at the alternative definition." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def simple_blocks_world():\n",
       "    """\n",
       "    SIMPLE-BLOCKS-WORLD\n",
       "\n",
       "    A simplified definition of the Sussman Anomaly problem.\n",
       "\n",
       "    Example:\n",
       "    >>> from planning import *\n",
       "    >>> sbw = simple_blocks_world()\n",
       "    >>> sbw.goal_test()\n",
       "    False\n",
       "    >>> sbw.act(expr('ToTable(A, B)'))\n",
       "    >>> sbw.act(expr('FromTable(B, A)'))\n",
       "    >>> sbw.goal_test()\n",
       "    False\n",
       "    >>> sbw.act(expr('FromTable(C, B)'))\n",
       "    >>> sbw.goal_test()\n",
       "    True\n",
       "    >>>\n",
       "    """\n",
       "\n",
       "    return PlanningProblem(init='On(A, B) & Clear(A) & OnTable(B) & OnTable(C) & Clear(C)',\n",
       "                goals='On(B, A) & On(C, B)',\n",
       "                actions=[Action('ToTable(x, y)',\n",
       "                                precond='On(x, y) & Clear(x)',\n",
       "                                effect='~On(x, y) & Clear(y) & OnTable(x)'),\n",
       "                         Action('FromTable(y, x)',\n",
       "                                precond='OnTable(y) & Clear(y) & Clear(x)',\n",
       "                                effect='~OnTable(y) & ~Clear(x) & On(y, x)')])\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(simple_blocks_world)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**On(x, y):** The block **'x'** is on **'y'**. Both **'x'** and **'y'** have to be blocks.\n", "\n", "**~On(x, y):** The block **'x'** is _not_ on **'y'**. Both **'x'** and **'y'** have to be blocks.\n", "\n", "**OnTable(x):** The block **'x'** is on the table.\n", "\n", "**~OnTable(x):** The block **'x'** is _not_ on the table.\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's now define a `simple_blocks_world` prolem." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "simpleBlocksWorld = simple_blocks_world()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before taking any actions, we will see if `simple_bw` has reached its goal." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simpleBlocksWorld.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 `simple_bw` PlanningProblem.\n", "\n", "The actions available to us are the following: MoveToTable, Move\n", "\n", "**ToTable(x, y): ** Move box **'x'** stacked on **'y'** to the table, given that box **'y'** is clear.\n", "\n", "**FromTable(x, y): ** Move box **'x'** from wherever it is, to the top of **'y'**, given that both **'x'** and **'y'** are clear.\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "solution = [expr('ToTable(A, B)'),\n", " expr('FromTable(B, A)'),\n", " expr('FromTable(C, B)')]\n", "\n", "for action in solution:\n", " simpleBlocksWorld.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": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(simpleBlocksWorld.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": [ "## Shopping Problem" ] }, { "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." ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def shopping_problem():\n",
       "    """\n",
       "    SHOPPING-PROBLEM\n",
       "\n",
       "    A problem of acquiring some items given their availability at certain stores.\n",
       "\n",
       "    Example:\n",
       "    >>> from planning import *\n",
       "    >>> sp = shopping_problem()\n",
       "    >>> sp.goal_test()\n",
       "    False\n",
       "    >>> sp.act(expr('Go(Home, HW)'))\n",
       "    >>> sp.act(expr('Buy(Drill, HW)'))\n",
       "    >>> sp.act(expr('Go(HW, SM)'))\n",
       "    >>> sp.act(expr('Buy(Banana, SM)'))\n",
       "    >>> sp.goal_test()\n",
       "    False\n",
       "    >>> sp.act(expr('Buy(Milk, SM)'))\n",
       "    >>> sp.goal_test()\n",
       "    True\n",
       "    >>>\n",
       "    """\n",
       "\n",
       "    return PlanningProblem(init='At(Home) & Sells(SM, Milk) & Sells(SM, Banana) & Sells(HW, Drill)',\n",
       "                goals='Have(Milk) & Have(Banana) & Have(Drill)', \n",
       "                actions=[Action('Buy(x, store)',\n",
       "                                precond='At(store) & Sells(store, x)',\n",
       "                                effect='Have(x)'),\n",
       "                         Action('Go(x, y)',\n",
       "                                precond='At(x)',\n",
       "                                effect='At(y) & ~At(x)')])\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(shopping_problem)" ] }, { "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", "\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'**." ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "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": 36, "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", "\n", "**Buy(x, store):** Buy an item **'x'** from a **'store'** given that the **'store'** sells **'x'**.\n", "\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` PlanningProblem." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "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." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shoppingProblem.goal_test()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has now successfully achieved the goal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Socks and Shoes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a simple problem of putting on a pair of socks and shoes.\n", "The problem is defined in the module as given below." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def socks_and_shoes():\n",
       "    """\n",
       "    SOCKS-AND-SHOES-PROBLEM\n",
       "\n",
       "    A task of wearing socks and shoes on both feet\n",
       "\n",
       "    Example:\n",
       "    >>> from planning import *\n",
       "    >>> ss = socks_and_shoes()\n",
       "    >>> ss.goal_test()\n",
       "    False\n",
       "    >>> ss.act(expr('RightSock'))\n",
       "    >>> ss.act(expr('RightShoe'))\n",
       "    >>> ss.act(expr('LeftSock'))\n",
       "    >>> ss.goal_test()\n",
       "    False\n",
       "    >>> ss.act(expr('LeftShoe'))\n",
       "    >>> ss.goal_test()\n",
       "    True\n",
       "    >>>\n",
       "    """\n",
       "\n",
       "    return PlanningProblem(init='',\n",
       "                goals='RightShoeOn & LeftShoeOn',\n",
       "                actions=[Action('RightShoe',\n",
       "                                precond='RightSockOn',\n",
       "                                effect='RightShoeOn'),\n",
       "                        Action('RightSock',\n",
       "                                precond='',\n",
       "                                effect='RightSockOn'),\n",
       "                        Action('LeftShoe',\n",
       "                                precond='LeftSockOn',\n",
       "                                effect='LeftShoeOn'),\n",
       "                        Action('LeftSock',\n",
       "                                precond='',\n",
       "                                effect='LeftSockOn')])\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(socks_and_shoes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**LeftSockOn:** Indicates that we have already put on the left sock.\n", "\n", "**RightSockOn:** Indicates that we have already put on the right sock.\n", "\n", "**LeftShoeOn:** Indicates that we have already put on the left shoe.\n", "\n", "**RightShoeOn:** Indicates that we have already put on the right shoe.\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": true }, "outputs": [], "source": [ "socksShoes = socks_and_shoes()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's first check whether the goal state is reached or not." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "socksShoes.goal_test()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As the goal state isn't reached, we will define a sequence of actions that might help us achieve the goal.\n", "These actions will then be acted upon the `socksShoes` PlanningProblem to check if the goal state is reached." ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": true }, "outputs": [], "source": [ "solution = [expr('RightSock'),\n", " expr('RightShoe'),\n", " expr('LeftSock'),\n", " expr('LeftShoe')]" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for action in solution:\n", " socksShoes.act(action)\n", " \n", "socksShoes.goal_test()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have reached our goal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cake Problem" ] }, { "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." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def have_cake_and_eat_cake_too():\n",
       "    """\n",
       "    [Figure 10.7] CAKE-PROBLEM\n",
       "\n",
       "    A problem where we begin with a cake and want to \n",
       "    reach the state of having a cake and having eaten a cake.\n",
       "    The possible actions include baking a cake and eating a cake.\n",
       "\n",
       "    Example:\n",
       "    >>> from planning import *\n",
       "    >>> cp = have_cake_and_eat_cake_too()\n",
       "    >>> cp.goal_test()\n",
       "    False\n",
       "    >>> cp.act(expr('Eat(Cake)'))\n",
       "    >>> cp.goal_test()\n",
       "    False\n",
       "    >>> cp.act(expr('Bake(Cake)'))\n",
       "    >>> cp.goal_test()\n",
       "    True\n",
       "    >>>\n",
       "    """\n",
       "\n",
       "    return PlanningProblem(init='Have(Cake)',\n",
       "                goals='Have(Cake) & Eaten(Cake)',\n",
       "                actions=[Action('Eat(Cake)',\n",
       "                                precond='Have(Cake)',\n",
       "                                effect='Eaten(Cake) & ~Have(Cake)'),\n",
       "                         Action('Bake(Cake)',\n",
       "                                precond='~Have(Cake)',\n",
       "                                effect='Have(Cake)')])\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "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", "\n", "**Have(Cake):** Declares that we have a **'Cake'**.\n", "\n", "**~Have(Cake):** Declares that we _don't_ have a **'Cake'**." ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": true }, "outputs": [], "source": [ "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", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(cakeProblem.goal_test())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us look at the possible actions.\n", "\n", "**Bake(x):** To bake **' x '**.\n", "\n", "**Eat(x):** To eat **' x '**." ] }, { "cell_type": "markdown", "metadata": {}, "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` PlanningProblem." ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, "outputs": [], "source": [ "solution = [expr(\"Eat(Cake)\"),\n", " expr(\"Bake(Cake)\")]\n", "\n", "for action in solution:\n", " cakeProblem.act(action)" ] }, { "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", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(cakeProblem.goal_test())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has now successfully achieved its goal i.e, to have and eat the cake." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One might wonder if the order of the actions matters for this problem.\n", "Let's see for ourselves." ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "ename": "Exception", "evalue": "Action 'Bake(Cake)' pre-conditions not satisfied", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mException\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0maction\u001b[0m \u001b[1;32min\u001b[0m \u001b[0msolution\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mcakeProblem\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mact\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maction\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m~\\Documents\\Python\\Data Science\\Machine Learning\\Aima\\planning.py\u001b[0m in \u001b[0;36mact\u001b[1;34m(self, action)\u001b[0m\n\u001b[0;32m 58\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Action '{}' not found\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maction_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 59\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mlist_action\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_precond\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minit\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 60\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Action '{}' pre-conditions not satisfied\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maction\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 61\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist_action\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minit\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclauses\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 62\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mException\u001b[0m: Action 'Bake(Cake)' pre-conditions not satisfied" ] } ], "source": [ "cakeProblem = have_cake_and_eat_cake_too()\n", "\n", "solution = [expr('Bake(Cake)'),\n", " expr('Eat(Cake)')]\n", "\n", "for action in solution:\n", " cakeProblem.act(action)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It raises an exception.\n", "Indeed, according to the problem, we cannot bake a cake if we already have one.\n", "In planning terms, '~Have(Cake)' is a precondition to the action 'Bake(Cake)'.\n", "Hence, this solution is invalid." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SOLVING PLANNING PROBLEMS\n", "----\n", "### GRAPHPLAN\n", "
\n", "The GraphPlan algorithm is a popular method of solving classical planning problems.\n", "Before we get into the details of the algorithm, let's look at a special data structure called **planning graph**, used to give better heuristic estimates and plays a key role in the GraphPlan algorithm." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Planning Graph\n", "A planning graph is a directed graph organized into levels. \n", "Each level contains information about the current state of the knowledge base and the possible state-action links to and from that level.\n", "The first level contains the initial state with nodes representing each fluent that holds in that level.\n", "This level has state-action links linking each state to valid actions in that state.\n", "Each action is linked to all its preconditions and its effect states.\n", "Based on these effects, the next level is constructed.\n", "The next level contains similarly structured information about the next state.\n", "In this way, the graph is expanded using state-action links till we reach a state where all the required goals hold true simultaneously.\n", "We can say that we have reached our goal if none of the goal states in the current level are mutually exclusive.\n", "This will be explained in detail later.\n", "
\n", "Planning graphs only work for propositional planning problems, hence we need to eliminate all variables by generating all possible substitutions.\n", "
\n", "For example, the planning graph of the `have_cake_and_eat_cake_too` problem might look like this\n", "![title](images/cake_graph.jpg)\n", "
\n", "The black lines indicate links between states and actions.\n", "
\n", "In every planning problem, we are allowed to carry out the `no-op` action, ie, we can choose no action for a particular state.\n", "These are called 'Persistence' actions and are represented in the graph by the small square boxes.\n", "In technical terms, a persistence action has effects same as its preconditions.\n", "This enables us to carry a state to the next level.\n", "
\n", "
\n", "The gray lines indicate mutual exclusivity.\n", "This means that the actions connected bya gray line cannot be taken together.\n", "Mutual exclusivity (mutex) occurs in the following cases:\n", "1. **Inconsistent effects**: One action negates the effect of the other. For example, _Eat(Cake)_ and the persistence of _Have(Cake)_ have inconsistent effects because they disagree on the effect _Have(Cake)_\n", "2. **Interference**: One of the effects of an action is the negation of a precondition of the other. For example, _Eat(Cake)_ interferes with the persistence of _Have(Cake)_ by negating its precondition.\n", "3. **Competing needs**: One of the preconditions of one action is mutually exclusive with a precondition of the other. For example, _Bake(Cake)_ and _Eat(Cake)_ are mutex because they compete on the value of the _Have(Cake)_ precondition." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the module, planning graphs have been implemented using two classes, `Level` which stores data for a particular level and `Graph` which connects multiple levels together.\n", "Let's look at the `Level` class." ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class Level:\n",
       "    """\n",
       "    Contains the state of the planning problem\n",
       "    and exhaustive list of actions which use the\n",
       "    states as pre-condition.\n",
       "    """\n",
       "\n",
       "    def __init__(self, kb):\n",
       "        """Initializes variables to hold state and action details of a level"""\n",
       "\n",
       "        self.kb = kb\n",
       "        # current state\n",
       "        self.current_state = kb.clauses\n",
       "        # current action to state link\n",
       "        self.current_action_links = {}\n",
       "        # current state to action link\n",
       "        self.current_state_links = {}\n",
       "        # current action to next state link\n",
       "        self.next_action_links = {}\n",
       "        # next state to current action link\n",
       "        self.next_state_links = {}\n",
       "        # mutually exclusive actions\n",
       "        self.mutex = []\n",
       "\n",
       "    def __call__(self, actions, objects):\n",
       "        self.build(actions, objects)\n",
       "        self.find_mutex()\n",
       "\n",
       "    def separate(self, e):\n",
       "        """Separates an iterable of elements into positive and negative parts"""\n",
       "\n",
       "        positive = []\n",
       "        negative = []\n",
       "        for clause in e:\n",
       "            if clause.op[:3] == 'Not':\n",
       "                negative.append(clause)\n",
       "            else:\n",
       "                positive.append(clause)\n",
       "        return positive, negative\n",
       "\n",
       "    def find_mutex(self):\n",
       "        """Finds mutually exclusive actions"""\n",
       "\n",
       "        # Inconsistent effects\n",
       "        pos_nsl, neg_nsl = self.separate(self.next_state_links)\n",
       "\n",
       "        for negeff in neg_nsl:\n",
       "            new_negeff = Expr(negeff.op[3:], *negeff.args)\n",
       "            for poseff in pos_nsl:\n",
       "                if new_negeff == poseff:\n",
       "                    for a in self.next_state_links[poseff]:\n",
       "                        for b in self.next_state_links[negeff]:\n",
       "                            if {a, b} not in self.mutex:\n",
       "                                self.mutex.append({a, b})\n",
       "\n",
       "        # Interference will be calculated with the last step\n",
       "        pos_csl, neg_csl = self.separate(self.current_state_links)\n",
       "\n",
       "        # Competing needs\n",
       "        for posprecond in pos_csl:\n",
       "            for negprecond in neg_csl:\n",
       "                new_negprecond = Expr(negprecond.op[3:], *negprecond.args)\n",
       "                if new_negprecond == posprecond:\n",
       "                    for a in self.current_state_links[posprecond]:\n",
       "                        for b in self.current_state_links[negprecond]:\n",
       "                            if {a, b} not in self.mutex:\n",
       "                                self.mutex.append({a, b})\n",
       "\n",
       "        # Inconsistent support\n",
       "        state_mutex = []\n",
       "        for pair in self.mutex:\n",
       "            next_state_0 = self.next_action_links[list(pair)[0]]\n",
       "            if len(pair) == 2:\n",
       "                next_state_1 = self.next_action_links[list(pair)[1]]\n",
       "            else:\n",
       "                next_state_1 = self.next_action_links[list(pair)[0]]\n",
       "            if (len(next_state_0) == 1) and (len(next_state_1) == 1):\n",
       "                state_mutex.append({next_state_0[0], next_state_1[0]})\n",
       "        \n",
       "        self.mutex = self.mutex + state_mutex\n",
       "\n",
       "    def build(self, actions, objects):\n",
       "        """Populates the lists and dictionaries containing the state action dependencies"""\n",
       "\n",
       "        for clause in self.current_state:\n",
       "            p_expr = Expr('P' + clause.op, *clause.args)\n",
       "            self.current_action_links[p_expr] = [clause]\n",
       "            self.next_action_links[p_expr] = [clause]\n",
       "            self.current_state_links[clause] = [p_expr]\n",
       "            self.next_state_links[clause] = [p_expr]\n",
       "\n",
       "        for a in actions:\n",
       "            num_args = len(a.args)\n",
       "            possible_args = tuple(itertools.permutations(objects, num_args))\n",
       "\n",
       "            for arg in possible_args:\n",
       "                if a.check_precond(self.kb, arg):\n",
       "                    for num, symbol in enumerate(a.args):\n",
       "                        if not symbol.op.islower():\n",
       "                            arg = list(arg)\n",
       "                            arg[num] = symbol\n",
       "                            arg = tuple(arg)\n",
       "\n",
       "                    new_action = a.substitute(Expr(a.name, *a.args), arg)\n",
       "                    self.current_action_links[new_action] = []\n",
       "\n",
       "                    for clause in a.precond:\n",
       "                        new_clause = a.substitute(clause, arg)\n",
       "                        self.current_action_links[new_action].append(new_clause)\n",
       "                        if new_clause in self.current_state_links:\n",
       "                            self.current_state_links[new_clause].append(new_action)\n",
       "                        else:\n",
       "                            self.current_state_links[new_clause] = [new_action]\n",
       "                   \n",
       "                    self.next_action_links[new_action] = []\n",
       "                    for clause in a.effect:\n",
       "                        new_clause = a.substitute(clause, arg)\n",
       "\n",
       "                        self.next_action_links[new_action].append(new_clause)\n",
       "                        if new_clause in self.next_state_links:\n",
       "                            self.next_state_links[new_clause].append(new_action)\n",
       "                        else:\n",
       "                            self.next_state_links[new_clause] = [new_action]\n",
       "\n",
       "    def perform_actions(self):\n",
       "        """Performs the necessary actions and returns a new Level"""\n",
       "\n",
       "        new_kb = FolKB(list(set(self.next_state_links.keys())))\n",
       "        return Level(new_kb)\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(Level)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each level stores the following data\n", "1. The current state of the level in `current_state`\n", "2. Links from an action to its preconditions in `current_action_links`\n", "3. Links from a state to the possible actions in that state in `current_state_links`\n", "4. Links from each action to its effects in `next_action_links`\n", "5. Links from each possible next state from each action in `next_state_links`. This stores the same information as the `current_action_links` of the next level.\n", "6. Mutex links in `mutex`.\n", "
\n", "
\n", "The `find_mutex` method finds the mutex links according to the points given above.\n", "
\n", "The `build` method populates the data structures storing the state and action information.\n", "Persistence actions for each clause in the current state are also defined here. \n", "The newly created persistence action has the same name as its state, prefixed with a 'P'." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's now look at the `Graph` class." ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class Graph:\n",
       "    """\n",
       "    Contains levels of state and actions\n",
       "    Used in graph planning algorithm to extract a solution\n",
       "    """\n",
       "\n",
       "    def __init__(self, pddl):\n",
       "        self.pddl = pddl\n",
       "        self.kb = FolKB(pddl.init)\n",
       "        self.levels = [Level(self.kb)]\n",
       "        self.objects = set(arg for clause in self.kb.clauses for arg in clause.args)\n",
       "\n",
       "    def __call__(self):\n",
       "        self.expand_graph()\n",
       "\n",
       "    def expand_graph(self):\n",
       "        """Expands the graph by a level"""\n",
       "\n",
       "        last_level = self.levels[-1]\n",
       "        last_level(self.pddl.actions, self.objects)\n",
       "        self.levels.append(last_level.perform_actions())\n",
       "\n",
       "    def non_mutex_goals(self, goals, index):\n",
       "        """Checks whether the goals are mutually exclusive"""\n",
       "\n",
       "        goal_perm = itertools.combinations(goals, 2)\n",
       "        for g in goal_perm:\n",
       "            if set(g) in self.levels[index].mutex:\n",
       "                return False\n",
       "        return True\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(Graph)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The class stores a problem definition in `pddl`, \n", "a knowledge base in `kb`, \n", "a list of `Level` objects in `levels` and \n", "all the possible arguments found in the initial state of the problem in `objects`.\n", "
\n", "The `expand_graph` method generates a new level of the graph.\n", "This method is invoked when the goal conditions haven't been met in the current level or the actions that lead to it are mutually exclusive.\n", "The `non_mutex_goals` method checks whether the goals in the current state are mutually exclusive.\n", "
\n", "
\n", "Using these two classes, we can define a planning graph which can either be used to provide reliable heuristics for planning problems or used in the `GraphPlan` algorithm.\n", "
\n", "Let's have a look at the `GraphPlan` class." ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class GraphPlan:\n",
       "    """\n",
       "    Class for formulation GraphPlan algorithm\n",
       "    Constructs a graph of state and action space\n",
       "    Returns solution for the planning problem\n",
       "    """\n",
       "\n",
       "    def __init__(self, pddl):\n",
       "        self.graph = Graph(pddl)\n",
       "        self.nogoods = []\n",
       "        self.solution = []\n",
       "\n",
       "    def check_leveloff(self):\n",
       "        """Checks if the graph has levelled off"""\n",
       "\n",
       "        check = (set(self.graph.levels[-1].current_state) == set(self.graph.levels[-2].current_state))\n",
       "\n",
       "        if check:\n",
       "            return True\n",
       "\n",
       "    def extract_solution(self, goals, index):\n",
       "        """Extracts the solution"""\n",
       "\n",
       "        level = self.graph.levels[index]    \n",
       "        if not self.graph.non_mutex_goals(goals, index):\n",
       "            self.nogoods.append((level, goals))\n",
       "            return\n",
       "\n",
       "        level = self.graph.levels[index - 1]    \n",
       "\n",
       "        # Create all combinations of actions that satisfy the goal    \n",
       "        actions = []\n",
       "        for goal in goals:\n",
       "            actions.append(level.next_state_links[goal])    \n",
       "\n",
       "        all_actions = list(itertools.product(*actions))    \n",
       "\n",
       "        # Filter out non-mutex actions\n",
       "        non_mutex_actions = []    \n",
       "        for action_tuple in all_actions:\n",
       "            action_pairs = itertools.combinations(list(set(action_tuple)), 2)        \n",
       "            non_mutex_actions.append(list(set(action_tuple)))        \n",
       "            for pair in action_pairs:            \n",
       "                if set(pair) in level.mutex:\n",
       "                    non_mutex_actions.pop(-1)\n",
       "                    break\n",
       "    \n",
       "\n",
       "        # Recursion\n",
       "        for action_list in non_mutex_actions:        \n",
       "            if [action_list, index] not in self.solution:\n",
       "                self.solution.append([action_list, index])\n",
       "\n",
       "                new_goals = []\n",
       "                for act in set(action_list):                \n",
       "                    if act in level.current_action_links:\n",
       "                        new_goals = new_goals + level.current_action_links[act]\n",
       "\n",
       "                if abs(index) + 1 == len(self.graph.levels):\n",
       "                    return\n",
       "                elif (level, new_goals) in self.nogoods:\n",
       "                    return\n",
       "                else:\n",
       "                    self.extract_solution(new_goals, index - 1)\n",
       "\n",
       "        # Level-Order multiple solutions\n",
       "        solution = []\n",
       "        for item in self.solution:\n",
       "            if item[1] == -1:\n",
       "                solution.append([])\n",
       "                solution[-1].append(item[0])\n",
       "            else:\n",
       "                solution[-1].append(item[0])\n",
       "\n",
       "        for num, item in enumerate(solution):\n",
       "            item.reverse()\n",
       "            solution[num] = item\n",
       "\n",
       "        return solution\n",
       "\n",
       "    def goal_test(self, kb):\n",
       "        return all(kb.ask(q) is not False for q in self.graph.pddl.goals)\n",
       "\n",
       "    def execute(self):\n",
       "        """Executes the GraphPlan algorithm for the given problem"""\n",
       "\n",
       "        while True:\n",
       "            self.graph.expand_graph()\n",
       "            if (self.goal_test(self.graph.levels[-1].kb) and self.graph.non_mutex_goals(self.graph.pddl.goals, -1)):\n",
       "                solution = self.extract_solution(self.graph.pddl.goals, -1)\n",
       "                if solution:\n",
       "                    return solution\n",
       "            \n",
       "            if len(self.graph.levels) >= 2 and self.check_leveloff():\n",
       "                return None\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(GraphPlan)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given a planning problem defined as a PlanningProblem, `GraphPlan` creates a planning graph stored in `graph` and expands it till it reaches a state where all its required goals are present simultaneously without mutual exclusivity.\n", "
\n", "Once a goal is found, `extract_solution` is called.\n", "This method recursively finds the path to a solution given a planning graph.\n", "In the case where `extract_solution` fails to find a solution for a set of goals as a given level, we record the `(level, goals)` pair as a **no-good**.\n", "Whenever `extract_solution` is called again with the same level and goals, we can find the recorded no-good and immediately return failure rather than searching again. \n", "No-goods are also used in the termination test.\n", "
\n", "The `check_leveloff` method checks if the planning graph for the problem has **levelled-off**, ie, it has the same states, actions and mutex pairs as the previous level.\n", "If the graph has already levelled off and we haven't found a solution, there is no point expanding the graph, as it won't lead to anything new.\n", "In such a case, we can declare that the planning problem is unsolvable with the given constraints.\n", "
\n", "
\n", "To summarize, the `GraphPlan` algorithm calls `expand_graph` and tests whether it has reached the goal and if the goals are non-mutex.\n", "
\n", "If so, `extract_solution` is invoked which recursively reconstructs the solution from the planning graph.\n", "
\n", "If not, then we check if our graph has levelled off and continue if it hasn't." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's solve a few planning problems that we had defined earlier." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Air cargo problem\n", "In accordance with the summary above, we have defined a helper function to carry out `GraphPlan` on the `air_cargo` problem.\n", "The function is pretty straightforward.\n", "Let's have a look." ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def air_cargo_graphplan():\n",
       "    """Solves the air cargo problem using GraphPlan"""\n",
       "    return GraphPlan(air_cargo()).execute()\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(air_cargo_graphplan)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's instantiate the problem and find a solution using this helper function." ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[[Load(C2, P2, JFK),\n", " Fly(P2, JFK, SFO),\n", " Load(C1, P1, SFO),\n", " Fly(P1, SFO, JFK),\n", " PCargo(C1),\n", " PAirport(JFK),\n", " PPlane(P2),\n", " PAirport(SFO),\n", " PPlane(P1),\n", " PCargo(C2)],\n", " [Unload(C2, P2, SFO), Unload(C1, P1, JFK)]]]" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "airCargoG = air_cargo_graphplan()\n", "airCargoG" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each element in the solution is a valid action.\n", "The solution is separated into lists for each level.\n", "The actions prefixed with a 'P' are persistence actions and can be ignored.\n", "They simply carry certain states forward.\n", "We have another helper function `linearize` that presents the solution in a more readable format, much like a total-order planner, but it is _not_ a total-order planner." ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Load(C2, P2, JFK),\n", " Fly(P2, JFK, SFO),\n", " Load(C1, P1, SFO),\n", " Fly(P1, SFO, JFK),\n", " Unload(C2, P2, SFO),\n", " Unload(C1, P1, JFK)]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linearize(airCargoG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indeed, this is a correct solution.\n", "
\n", "There are similar helper functions for some other planning problems.\n", "
\n", "Lets' try solving the spare tire problem." ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Remove(Flat, Axle), Remove(Spare, Trunk), PutOn(Spare, Axle)]" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spareTireG = spare_tire_graphplan()\n", "linearize(spareTireG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solution for the cake problem" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Eat(Cake), Bake(Cake)]" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cakeProblemG = have_cake_and_eat_cake_too_graphplan()\n", "linearize(cakeProblemG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solution for the Sussman's Anomaly configuration of three blocks." ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[MoveToTable(C, A), Move(B, Table, C), Move(A, Table, B)]" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sussmanAnomalyG = three_block_tower_graphplan()\n", "linearize(sussmanAnomalyG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solution of the socks and shoes problem" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[LeftSock, RightSock, LeftShoe, RightShoe]" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "socksShoesG = socks_and_shoes_graphplan()\n", "linearize(socksShoesG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### TOTAL ORDER PLANNER\n", "\n", "In mathematical terminology, **total order**, **linear order** or **simple order** refers to a set *X* which is said to be totally ordered under ≤ if the following statements hold for all *a*, *b* and *c* in *X*:\n", "
\n", "If *a* ≤ *b* and *b* ≤ *a*, then *a* = *b* (antisymmetry).\n", "
\n", "If *a* ≤ *b* and *b* ≤ *c*, then *a* ≤ *c* (transitivity).\n", "
\n", "*a* ≤ *b* or *b* ≤ *a* (connex relation).\n", "\n", "
\n", "In simpler terms, a total order plan is a linear ordering of actions to be taken to reach the goal state.\n", "There may be several different total-order plans for a particular goal depending on the problem.\n", "
\n", "
\n", "In the module, the `Linearize` class solves problems using this paradigm.\n", "At its core, the `Linearize` uses a solved planning graph from `GraphPlan` and finds a valid total-order solution for it.\n", "Let's have a look at the class." ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class Linearize:\n",
       "\n",
       "    def __init__(self, pddl):\n",
       "        self.pddl = pddl\n",
       "\n",
       "    def filter(self, solution):\n",
       "        """Filter out persistence actions from a solution"""\n",
       "\n",
       "        new_solution = []\n",
       "        for section in solution[0]:\n",
       "            new_section = []\n",
       "            for operation in section:\n",
       "                if not (operation.op[0] == 'P' and operation.op[1].isupper()):\n",
       "                    new_section.append(operation)\n",
       "            new_solution.append(new_section)\n",
       "        return new_solution\n",
       "\n",
       "    def orderlevel(self, level, pddl):\n",
       "        """Return valid linear order of actions for a given level"""\n",
       "\n",
       "        for permutation in itertools.permutations(level):\n",
       "            temp = copy.deepcopy(pddl)\n",
       "            count = 0\n",
       "            for action in permutation:\n",
       "                try:\n",
       "                    temp.act(action)\n",
       "                    count += 1\n",
       "                except:\n",
       "                    count = 0\n",
       "                    temp = copy.deepcopy(pddl)\n",
       "                    break\n",
       "            if count == len(permutation):\n",
       "                return list(permutation), temp\n",
       "        return None\n",
       "\n",
       "    def execute(self):\n",
       "        """Finds total-order solution for a planning graph"""\n",
       "\n",
       "        graphplan_solution = GraphPlan(self.pddl).execute()\n",
       "        filtered_solution = self.filter(graphplan_solution)\n",
       "        ordered_solution = []\n",
       "        pddl = self.pddl\n",
       "        for level in filtered_solution:\n",
       "            level_solution, pddl = self.orderlevel(level, pddl)\n",
       "            for element in level_solution:\n",
       "                ordered_solution.append(element)\n",
       "\n",
       "        return ordered_solution\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(Linearize)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `filter` method removes the persistence actions (if any) from the planning graph representation.\n", "
\n", "The `orderlevel` method finds a valid total-ordering of a specified level of the planning-graph, given the state of the graph after the previous level.\n", "
\n", "The `execute` method sequentially calls `orderlevel` for all the levels in the planning-graph and returns the final total-order solution.\n", "
\n", "
\n", "Let's look at some examples." ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Load(C2, P2, JFK),\n", " Fly(P2, JFK, SFO),\n", " Load(C1, P1, SFO),\n", " Fly(P1, SFO, JFK),\n", " Unload(C2, P2, SFO),\n", " Unload(C1, P1, JFK)]" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# total-order solution for air_cargo problem\n", "Linearize(air_cargo()).execute()" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Remove(Flat, Axle), Remove(Spare, Trunk), PutOn(Spare, Axle)]" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# total-order solution for spare_tire problem\n", "Linearize(spare_tire()).execute()" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[MoveToTable(C, A), Move(B, Table, C), Move(A, Table, B)]" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# total-order solution for three_block_tower problem\n", "Linearize(three_block_tower()).execute()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[ToTable(A, B), FromTable(B, A), FromTable(C, B)]" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# total-order solution for simple_blocks_world problem\n", "Linearize(simple_blocks_world()).execute()" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[LeftSock, RightSock, LeftShoe, RightShoe]" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# total-order solution for socks_and_shoes problem\n", "Linearize(socks_and_shoes()).execute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### PARTIAL ORDER PLANNER\n", "A partial-order planning algorithm is significantly different from a total-order planner.\n", "The way a partial-order plan works enables it to take advantage of _problem decomposition_ and work on each subproblem separately.\n", "It works on several subgoals independently, solves them with several subplans, and then combines the plan.\n", "
\n", "A partial-order planner also follows the **least commitment** strategy, where it delays making choices for as long as possible.\n", "Variables are not bound unless it is absolutely necessary and new actions are chosen only if the existing actions cannot fulfil the required precondition.\n", "
\n", "Any planning algorithm that can place two actions into a plan without specifying which comes first is called a **partial-order planner**.\n", "A partial-order planner searches through the space of plans rather than the space of states, which makes it perform better for certain problems.\n", "
\n", "
\n", "Let's have a look at the `PartialOrderPlanner` class." ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class PartialOrderPlanner:\n",
       "\n",
       "    def __init__(self, pddl):\n",
       "        self.pddl = pddl\n",
       "        self.initialize()\n",
       "\n",
       "    def initialize(self):\n",
       "        """Initialize all variables"""\n",
       "        self.causal_links = []\n",
       "        self.start = Action('Start', [], self.pddl.init)\n",
       "        self.finish = Action('Finish', self.pddl.goals, [])\n",
       "        self.actions = set()\n",
       "        self.actions.add(self.start)\n",
       "        self.actions.add(self.finish)\n",
       "        self.constraints = set()\n",
       "        self.constraints.add((self.start, self.finish))\n",
       "        self.agenda = set()\n",
       "        for precond in self.finish.precond:\n",
       "            self.agenda.add((precond, self.finish))\n",
       "        self.expanded_actions = self.expand_actions()\n",
       "\n",
       "    def expand_actions(self, name=None):\n",
       "        """Generate all possible actions with variable bindings for precondition selection heuristic"""\n",
       "\n",
       "        objects = set(arg for clause in self.pddl.init for arg in clause.args)\n",
       "        expansions = []\n",
       "        action_list = []\n",
       "        if name is not None:\n",
       "            for action in self.pddl.actions:\n",
       "                if str(action.name) == name:\n",
       "                    action_list.append(action)\n",
       "        else:\n",
       "            action_list = self.pddl.actions\n",
       "\n",
       "        for action in action_list:\n",
       "            for permutation in itertools.permutations(objects, len(action.args)):\n",
       "                bindings = unify(Expr(action.name, *action.args), Expr(action.name, *permutation))\n",
       "                if bindings is not None:\n",
       "                    new_args = []\n",
       "                    for arg in action.args:\n",
       "                        if arg in bindings:\n",
       "                            new_args.append(bindings[arg])\n",
       "                        else:\n",
       "                            new_args.append(arg)\n",
       "                    new_expr = Expr(str(action.name), *new_args)\n",
       "                    new_preconds = []\n",
       "                    for precond in action.precond:\n",
       "                        new_precond_args = []\n",
       "                        for arg in precond.args:\n",
       "                            if arg in bindings:\n",
       "                                new_precond_args.append(bindings[arg])\n",
       "                            else:\n",
       "                                new_precond_args.append(arg)\n",
       "                        new_precond = Expr(str(precond.op), *new_precond_args)\n",
       "                        new_preconds.append(new_precond)\n",
       "                    new_effects = []\n",
       "                    for effect in action.effect:\n",
       "                        new_effect_args = []\n",
       "                        for arg in effect.args:\n",
       "                            if arg in bindings:\n",
       "                                new_effect_args.append(bindings[arg])\n",
       "                            else:\n",
       "                                new_effect_args.append(arg)\n",
       "                        new_effect = Expr(str(effect.op), *new_effect_args)\n",
       "                        new_effects.append(new_effect)\n",
       "                    expansions.append(Action(new_expr, new_preconds, new_effects))\n",
       "\n",
       "        return expansions\n",
       "\n",
       "    def find_open_precondition(self):\n",
       "        """Find open precondition with the least number of possible actions"""\n",
       "\n",
       "        number_of_ways = dict()\n",
       "        actions_for_precondition = dict()\n",
       "        for element in self.agenda:\n",
       "            open_precondition = element[0]\n",
       "            possible_actions = list(self.actions) + self.expanded_actions\n",
       "            for action in possible_actions:\n",
       "                for effect in action.effect:\n",
       "                    if effect == open_precondition:\n",
       "                        if open_precondition in number_of_ways:\n",
       "                            number_of_ways[open_precondition] += 1\n",
       "                            actions_for_precondition[open_precondition].append(action)\n",
       "                        else:\n",
       "                            number_of_ways[open_precondition] = 1\n",
       "                            actions_for_precondition[open_precondition] = [action]\n",
       "\n",
       "        number = sorted(number_of_ways, key=number_of_ways.__getitem__)\n",
       "        \n",
       "        for k, v in number_of_ways.items():\n",
       "            if v == 0:\n",
       "                return None, None, None\n",
       "\n",
       "        act1 = None\n",
       "        for element in self.agenda:\n",
       "            if element[0] == number[0]:\n",
       "                act1 = element[1]\n",
       "                break\n",
       "\n",
       "        if number[0] in self.expanded_actions:\n",
       "            self.expanded_actions.remove(number[0])\n",
       "\n",
       "        return number[0], act1, actions_for_precondition[number[0]]\n",
       "\n",
       "    def find_action_for_precondition(self, oprec):\n",
       "        """Find action for a given precondition"""\n",
       "\n",
       "        # either\n",
       "        #   choose act0 E Actions such that act0 achieves G\n",
       "        for action in self.actions:\n",
       "            for effect in action.effect:\n",
       "                if effect == oprec:\n",
       "                    return action, 0\n",
       "\n",
       "        # or\n",
       "        #   choose act0 E Actions such that act0 achieves G\n",
       "        for action in self.pddl.actions:\n",
       "            for effect in action.effect:\n",
       "                if effect.op == oprec.op:\n",
       "                    bindings = unify(effect, oprec)\n",
       "                    if bindings is None:\n",
       "                        break\n",
       "                    return action, bindings\n",
       "\n",
       "    def generate_expr(self, clause, bindings):\n",
       "        """Generate atomic expression from generic expression given variable bindings"""\n",
       "\n",
       "        new_args = []\n",
       "        for arg in clause.args:\n",
       "            if arg in bindings:\n",
       "                new_args.append(bindings[arg])\n",
       "            else:\n",
       "                new_args.append(arg)\n",
       "\n",
       "        try:\n",
       "            return Expr(str(clause.name), *new_args)\n",
       "        except:\n",
       "            return Expr(str(clause.op), *new_args)\n",
       "        \n",
       "    def generate_action_object(self, action, bindings):\n",
       "        """Generate action object given a generic action andvariable bindings"""\n",
       "\n",
       "        # if bindings is 0, it means the action already exists in self.actions\n",
       "        if bindings == 0:\n",
       "            return action\n",
       "\n",
       "        # bindings cannot be None\n",
       "        else:\n",
       "            new_expr = self.generate_expr(action, bindings)\n",
       "            new_preconds = []\n",
       "            for precond in action.precond:\n",
       "                new_precond = self.generate_expr(precond, bindings)\n",
       "                new_preconds.append(new_precond)\n",
       "            new_effects = []\n",
       "            for effect in action.effect:\n",
       "                new_effect = self.generate_expr(effect, bindings)\n",
       "                new_effects.append(new_effect)\n",
       "            return Action(new_expr, new_preconds, new_effects)\n",
       "\n",
       "    def cyclic(self, graph):\n",
       "        """Check cyclicity of a directed graph"""\n",
       "\n",
       "        new_graph = dict()\n",
       "        for element in graph:\n",
       "            if element[0] in new_graph:\n",
       "                new_graph[element[0]].append(element[1])\n",
       "            else:\n",
       "                new_graph[element[0]] = [element[1]]\n",
       "\n",
       "        path = set()\n",
       "\n",
       "        def visit(vertex):\n",
       "            path.add(vertex)\n",
       "            for neighbor in new_graph.get(vertex, ()):\n",
       "                if neighbor in path or visit(neighbor):\n",
       "                    return True\n",
       "            path.remove(vertex)\n",
       "            return False\n",
       "\n",
       "        value = any(visit(v) for v in new_graph)\n",
       "        return value\n",
       "\n",
       "    def add_const(self, constraint, constraints):\n",
       "        """Add the constraint to constraints if the resulting graph is acyclic"""\n",
       "\n",
       "        if constraint[0] == self.finish or constraint[1] == self.start:\n",
       "            return constraints\n",
       "\n",
       "        new_constraints = set(constraints)\n",
       "        new_constraints.add(constraint)\n",
       "\n",
       "        if self.cyclic(new_constraints):\n",
       "            return constraints\n",
       "        return new_constraints\n",
       "\n",
       "    def is_a_threat(self, precondition, effect):\n",
       "        """Check if effect is a threat to precondition"""\n",
       "\n",
       "        if (str(effect.op) == 'Not' + str(precondition.op)) or ('Not' + str(effect.op) == str(precondition.op)):\n",
       "            if effect.args == precondition.args:\n",
       "                return True\n",
       "        return False\n",
       "\n",
       "    def protect(self, causal_link, action, constraints):\n",
       "        """Check and resolve threats by promotion or demotion"""\n",
       "\n",
       "        threat = False\n",
       "        for effect in action.effect:\n",
       "            if self.is_a_threat(causal_link[1], effect):\n",
       "                threat = True\n",
       "                break\n",
       "\n",
       "        if action != causal_link[0] and action != causal_link[2] and threat:\n",
       "            # try promotion\n",
       "            new_constraints = set(constraints)\n",
       "            new_constraints.add((action, causal_link[0]))\n",
       "            if not self.cyclic(new_constraints):\n",
       "                constraints = self.add_const((action, causal_link[0]), constraints)\n",
       "            else:\n",
       "                # try demotion\n",
       "                new_constraints = set(constraints)\n",
       "                new_constraints.add((causal_link[2], action))\n",
       "                if not self.cyclic(new_constraints):\n",
       "                    constraints = self.add_const((causal_link[2], action), constraints)\n",
       "                else:\n",
       "                    # both promotion and demotion fail\n",
       "                    print('Unable to resolve a threat caused by', action, 'onto', causal_link)\n",
       "                    return\n",
       "        return constraints\n",
       "\n",
       "    def convert(self, constraints):\n",
       "        """Convert constraints into a dict of Action to set orderings"""\n",
       "\n",
       "        graph = dict()\n",
       "        for constraint in constraints:\n",
       "            if constraint[0] in graph:\n",
       "                graph[constraint[0]].add(constraint[1])\n",
       "            else:\n",
       "                graph[constraint[0]] = set()\n",
       "                graph[constraint[0]].add(constraint[1])\n",
       "        return graph\n",
       "\n",
       "    def toposort(self, graph):\n",
       "        """Generate topological ordering of constraints"""\n",
       "\n",
       "        if len(graph) == 0:\n",
       "            return\n",
       "\n",
       "        graph = graph.copy()\n",
       "\n",
       "        for k, v in graph.items():\n",
       "            v.discard(k)\n",
       "\n",
       "        extra_elements_in_dependencies = _reduce(set.union, graph.values()) - set(graph.keys())\n",
       "\n",
       "        graph.update({element:set() for element in extra_elements_in_dependencies})\n",
       "        while True:\n",
       "            ordered = set(element for element, dependency in graph.items() if len(dependency) == 0)\n",
       "            if not ordered:\n",
       "                break\n",
       "            yield ordered\n",
       "            graph = {element: (dependency - ordered) for element, dependency in graph.items() if element not in ordered}\n",
       "        if len(graph) != 0:\n",
       "            raise ValueError('The graph is not acyclic and cannot be linearly ordered')\n",
       "\n",
       "    def display_plan(self):\n",
       "        """Display causal links, constraints and the plan"""\n",
       "\n",
       "        print('Causal Links')\n",
       "        for causal_link in self.causal_links:\n",
       "            print(causal_link)\n",
       "\n",
       "        print('\\nConstraints')\n",
       "        for constraint in self.constraints:\n",
       "            print(constraint[0], '<', constraint[1])\n",
       "\n",
       "        print('\\nPartial Order Plan')\n",
       "        print(list(reversed(list(self.toposort(self.convert(self.constraints))))))\n",
       "\n",
       "    def execute(self, display=True):\n",
       "        """Execute the algorithm"""\n",
       "\n",
       "        step = 1\n",
       "        self.tries = 1\n",
       "        while len(self.agenda) > 0:\n",
       "            step += 1\n",
       "            # select <G, act1> from Agenda\n",
       "            try:\n",
       "                G, act1, possible_actions = self.find_open_precondition()\n",
       "            except IndexError:\n",
       "                print('Probably Wrong')\n",
       "                break\n",
       "\n",
       "            act0 = possible_actions[0]\n",
       "            # remove <G, act1> from Agenda\n",
       "            self.agenda.remove((G, act1))\n",
       "\n",
       "            # For actions with variable number of arguments, use least commitment principle\n",
       "            # act0_temp, bindings = self.find_action_for_precondition(G)\n",
       "            # act0 = self.generate_action_object(act0_temp, bindings)\n",
       "\n",
       "            # Actions = Actions U {act0}\n",
       "            self.actions.add(act0)\n",
       "\n",
       "            # Constraints = add_const(start < act0, Constraints)\n",
       "            self.constraints = self.add_const((self.start, act0), self.constraints)\n",
       "\n",
       "            # for each CL E CausalLinks do\n",
       "            #   Constraints = protect(CL, act0, Constraints)\n",
       "            for causal_link in self.causal_links:\n",
       "                self.constraints = self.protect(causal_link, act0, self.constraints)\n",
       "\n",
       "            # Agenda = Agenda U {<P, act0>: P is a precondition of act0}\n",
       "            for precondition in act0.precond:\n",
       "                self.agenda.add((precondition, act0))\n",
       "\n",
       "            # Constraints = add_const(act0 < act1, Constraints)\n",
       "            self.constraints = self.add_const((act0, act1), self.constraints)\n",
       "\n",
       "            # CausalLinks U {<act0, G, act1>}\n",
       "            if (act0, G, act1) not in self.causal_links:\n",
       "                self.causal_links.append((act0, G, act1))\n",
       "\n",
       "            # for each A E Actions do\n",
       "            #   Constraints = protect(<act0, G, act1>, A, Constraints)\n",
       "            for action in self.actions:\n",
       "                self.constraints = self.protect((act0, G, act1), action, self.constraints)\n",
       "\n",
       "            if step > 200:\n",
       "                print('Couldn\\'t find a solution')\n",
       "                return None, None\n",
       "\n",
       "        if display:\n",
       "            self.display_plan()\n",
       "        else:\n",
       "            return self.constraints, self.causal_links                \n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(PartialOrderPlanner)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will first describe the data-structures and helper methods used, followed by the algorithm used to find a partial-order plan." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each plan has the following four components:\n", "\n", "1. **`actions`**: a set of actions that make up the steps of the plan.\n", "`actions` is always a subset of `pddl.actions` the set of possible actions for the given planning problem. \n", "The `start` and `finish` actions are dummy actions defined to bring uniformity to the problem. The `start` action has no preconditions and its effects constitute the initial state of the planning problem. \n", "The `finish` action has no effects and its preconditions constitute the goal state of the planning problem.\n", "The empty plan consists of just these two dummy actions.\n", "2. **`constraints`**: a set of temporal constraints that define the order of performing the actions relative to each other.\n", "`constraints` does not define a linear ordering, rather it usually represents a directed graph which is also acyclic if the plan is consistent.\n", "Each ordering is of the form A < B, which reads as \"A before B\" and means that action A _must_ be executed sometime before action B, but not necessarily immediately before.\n", "`constraints` stores these as a set of tuples `(Action(A), Action(B))` which is interpreted as given above.\n", "A constraint cannot be added to `constraints` if it breaks the acyclicity of the existing graph.\n", "3. **`causal_links`**: a set of causal-links. \n", "A causal link between two actions _A_ and _B_ in the plan is written as _A_ --_p_--> _B_ and is read as \"A achieves p for B\".\n", "This imples that _p_ is an effect of _A_ and a precondition of _B_.\n", "It also asserts that _p_ must remain true from the time of action _A_ to the time of action _B_.\n", "Any violation of this rule is called a threat and must be resolved immediately by adding suitable ordering constraints.\n", "`causal_links` stores this information as tuples `(Action(A), precondition(p), Action(B))` which is interpreted as given above.\n", "Causal-links can also be called **protection-intervals**, because the link _A_ --_p_--> _B_ protects _p_ from being negated over the interval from _A_ to _B_.\n", "4. **`agenda`**: a set of open-preconditions.\n", "A precondition is open if it is not achieved by some action in the plan.\n", "Planners will work to reduce the set of open preconditions to the empty set, without introducing a contradiction.\n", "`agenda` stored this information as tuples `(precondition(p), Action(A))` where p is a precondition of the action A.\n", "\n", "A **consistent plan** is a plan in which there are no cycles in the ordering constraints and no conflicts with the causal-links.\n", "A consistent plan with no open preconditions is a **solution**.\n", "
\n", "
\n", "Let's briefly glance over the helper functions before going into the actual algorithm.\n", "
\n", "**`expand_actions`**: generates all possible actions with variable bindings for use as a heuristic of selection of an open precondition.\n", "
\n", "**`find_open_precondition`**: finds a precondition from the agenda with the least number of actions that fulfil that precondition.\n", "This heuristic helps form mandatory ordering constraints and causal-links to further simplify the problem and reduce the probability of encountering a threat.\n", "
\n", "**`find_action_for_precondition`**: finds an action that fulfils the given precondition along with the absolutely necessary variable bindings in accordance with the principle of _least commitment_.\n", "In case of multiple possible actions, the action with the least number of effects is chosen to minimize the chances of encountering a threat.\n", "
\n", "**`cyclic`**: checks if a directed graph is cyclic.\n", "
\n", "**`add_const`**: adds `constraint` to `constraints` if the newly formed graph is acyclic and returns `constraints` otherwise.\n", "
\n", "**`is_a_threat`**: checks if the given `effect` negates the given `precondition`.\n", "
\n", "**`protect`**: checks if the given `action` poses a threat to the given `causal_link`.\n", "If so, the threat is resolved by either promotion or demotion, whichever generates acyclic temporal constraints.\n", "If neither promotion or demotion work, the chosen action is not the correct fit or the planning problem cannot be solved altogether.\n", "
\n", "**`convert`**: converts a graph from a list of edges to an `Action` : `set` mapping, for use in topological sorting.\n", "
\n", "**`toposort`**: a generator function that generates a topological ordering of a given graph as a list of sets.\n", "Each set contains an action or several actions.\n", "If a set has more that one action in it, it means that permutations between those actions also produce a valid plan.\n", "
\n", "**`display_plan`**: displays the `causal_links`, `constraints` and the partial order plan generated from `toposort`.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **`execute`** method executes the algorithm, which is summarized below:\n", "
\n", "1. An open precondition is selected (a sub-goal that we want to achieve).\n", "2. An action that fulfils the open precondition is chosen.\n", "3. Temporal constraints are updated.\n", "4. Existing causal links are protected. Protection is a method that checks if the causal links conflict\n", " and if they do, temporal constraints are added to fix the threats.\n", "5. The set of open preconditions is updated.\n", "6. Temporal constraints of the selected action and the next action are established.\n", "7. A new causal link is added between the selected action and the owner of the open precondition.\n", "8. The set of new causal links is checked for threats and if found, the threat is removed by either promotion or demotion.\n", " If promotion or demotion is unable to solve the problem, the planning problem cannot be solved with the current sequence of actions\n", " or it may not be solvable at all.\n", "9. These steps are repeated until the set of open preconditions is empty." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A partial-order plan can be used to generate different valid total-order plans.\n", "This step is called **linearization** of the partial-order plan.\n", "All possible linearizations of a partial-order plan for `socks_and_shoes` looks like this.\n", "
\n", "![title](images/pop.jpg)\n", "
\n", "Linearization can be carried out in many ways, but the most efficient way is to represent the set of temporal constraints as a directed graph.\n", "We can easily realize that the graph should also be acyclic as cycles in constraints means that the constraints are inconsistent.\n", "This acyclicity is enforced by the `add_const` method, which adds a new constraint only if the acyclicity of the existing graph is not violated.\n", "The `protect` method also checks for acyclicity of the newly-added temporal constraints to make a decision between promotion and demotion in case of a threat.\n", "This property of a graph created from the temporal constraints of a valid partial-order plan allows us to use topological sort to order the constraints linearly.\n", "A topological sort may produce several different valid solutions for a given directed acyclic graph." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we know how `PartialOrderPlanner` works, let's solve a few problems using it." ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Causal Links\n", "(Action(PutOn(Spare, Axle)), At(Spare, Axle), Action(Finish))\n", "(Action(Start), Tire(Spare), Action(PutOn(Spare, Axle)))\n", "(Action(Remove(Flat, Axle)), NotAt(Flat, Axle), Action(PutOn(Spare, Axle)))\n", "(Action(Start), At(Flat, Axle), Action(Remove(Flat, Axle)))\n", "(Action(Remove(Spare, Trunk)), At(Spare, Ground), Action(PutOn(Spare, Axle)))\n", "(Action(Start), At(Spare, Trunk), Action(Remove(Spare, Trunk)))\n", "(Action(Remove(Flat, Axle)), At(Flat, Ground), Action(Finish))\n", "\n", "Constraints\n", "Action(Start) < Action(Finish)\n", "Action(Start) < Action(Remove(Spare, Trunk))\n", "Action(Remove(Flat, Axle)) < Action(PutOn(Spare, Axle))\n", "Action(Remove(Flat, Axle)) < Action(Finish)\n", "Action(Remove(Spare, Trunk)) < Action(PutOn(Spare, Axle))\n", "Action(Start) < Action(PutOn(Spare, Axle))\n", "Action(Start) < Action(Remove(Flat, Axle))\n", "Action(PutOn(Spare, Axle)) < Action(Finish)\n", "\n", "Partial Order Plan\n", "[{Action(Start)}, {Action(Remove(Flat, Axle)), Action(Remove(Spare, Trunk))}, {Action(PutOn(Spare, Axle))}, {Action(Finish)}]\n" ] } ], "source": [ "st = spare_tire()\n", "pop = PartialOrderPlanner(st)\n", "pop.execute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We observe that in the given partial order plan, Remove(Flat, Axle) and Remove(Spare, Trunk) are in the same set.\n", "This means that the order of performing these actions does not affect the final outcome.\n", "That aside, we also see that the PutOn(Spare, Axle) action has to be performed after both the Remove actions are complete, which seems logically consistent." ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Causal Links\n", "(Action(FromTable(B, A)), On(B, A), Action(Finish))\n", "(Action(FromTable(C, B)), On(C, B), Action(Finish))\n", "(Action(Start), Clear(C), Action(FromTable(C, B)))\n", "(Action(Start), Clear(A), Action(FromTable(B, A)))\n", "(Action(Start), OnTable(C), Action(FromTable(C, B)))\n", "(Action(Start), OnTable(B), Action(FromTable(B, A)))\n", "(Action(ToTable(A, B)), Clear(B), Action(FromTable(C, B)))\n", "(Action(Start), On(A, B), Action(ToTable(A, B)))\n", "(Action(ToTable(A, B)), Clear(B), Action(FromTable(B, A)))\n", "(Action(Start), Clear(A), Action(ToTable(A, B)))\n", "\n", "Constraints\n", "Action(Start) < Action(FromTable(B, A))\n", "Action(Start) < Action(FromTable(C, B))\n", "Action(Start) < Action(ToTable(A, B))\n", "Action(ToTable(A, B)) < Action(FromTable(C, B))\n", "Action(Start) < Action(Finish)\n", "Action(ToTable(A, B)) < Action(FromTable(B, A))\n", "Action(FromTable(C, B)) < Action(Finish)\n", "Action(FromTable(B, A)) < Action(Finish)\n", "Action(FromTable(B, A)) < Action(FromTable(C, B))\n", "\n", "Partial Order Plan\n", "[{Action(Start)}, {Action(ToTable(A, B))}, {Action(FromTable(B, A))}, {Action(FromTable(C, B))}, {Action(Finish)}]\n" ] } ], "source": [ "sbw = simple_blocks_world()\n", "pop = PartialOrderPlanner(sbw)\n", "pop.execute()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "We see that this plan does not have flexibility in selecting actions, ie, actions should be performed in this order and this order only, to successfully reach the goal state." ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Causal Links\n", "(Action(RightShoe), RightShoeOn, Action(Finish))\n", "(Action(LeftShoe), LeftShoeOn, Action(Finish))\n", "(Action(LeftSock), LeftSockOn, Action(LeftShoe))\n", "(Action(RightSock), RightSockOn, Action(RightShoe))\n", "\n", "Constraints\n", "Action(Start) < Action(RightSock)\n", "Action(Start) < Action(LeftSock)\n", "Action(RightSock) < Action(RightShoe)\n", "Action(RightShoe) < Action(Finish)\n", "Action(Start) < Action(LeftShoe)\n", "Action(LeftSock) < Action(LeftShoe)\n", "Action(Start) < Action(RightShoe)\n", "Action(Start) < Action(Finish)\n", "Action(LeftShoe) < Action(Finish)\n", "\n", "Partial Order Plan\n", "[{Action(Start)}, {Action(LeftSock), Action(RightSock)}, {Action(RightShoe), Action(LeftShoe)}, {Action(Finish)}]\n" ] } ], "source": [ "ss = socks_and_shoes()\n", "pop = PartialOrderPlanner(ss)\n", "pop.execute()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "This plan again doesn't have constraints in selecting socks or shoes.\n", "As long as both socks are worn before both shoes, we are fine.\n", "Notice however, there is one valid solution,\n", "
\n", "LeftSock -> LeftShoe -> RightSock -> RightShoe\n", "
\n", "that the algorithm could not find as it cannot be represented as a general partially-ordered plan but is a specific total-order solution." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Runtime differences\n", "Let's briefly take a look at the running time of all the three algorithms on the `socks_and_shoes` problem." ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ss = socks_and_shoes()" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "333 µs ± 8.86 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%%timeit\n", "GraphPlan(ss).execute()" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.29 ms ± 43.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%%timeit\n", "Linearize(ss).execute()" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "425 µs ± 17 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%%timeit\n", "PartialOrderPlanner(ss).execute(display=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We observe that `GraphPlan` is about 4 times faster than `Linearize` because `Linearize` essentially runs a `GraphPlan` subroutine under the hood and then carries out some transformations on the solved planning-graph.\n", "
\n", "We also find that `GraphPlan` is slightly faster than `PartialOrderPlanner`, but this is mainly due to the `expand_actions` method in `PartialOrderPlanner` that slows it down as it generates all possible permutations of actions and variable bindings.\n", "
\n", "Without heuristic functions, `PartialOrderPlanner` will be atleast as fast as `GraphPlan`, if not faster, but will have a higher tendency to encounter threats and conflicts which might take additional time to resolve.\n", "
\n", "Different planning algorithms work differently for different problems." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PLANNING IN THE REAL WORLD\n", "---\n", "## PROBLEM\n", "The `Problem` class is a wrapper for `PlanningProblem` with some additional functionality and data-structures to handle real-world planning problems that involve time and resource constraints.\n", "The `Problem` class includes everything that the `PlanningProblem` class includes.\n", "Additionally, it also includes the following attributes essential to define a real-world planning problem:\n", "- a list of `jobs` to be done\n", "- a dictionary of `resources`\n", "\n", "It also overloads the `act` method to call the `do_action` method of the `HLA` class, \n", "and also includes a new method `refinements` that finds refinements or primitive actions for high level actions.\n", "
\n", "`hierarchical_search` and `angelic_search` are also built into the `Problem` class to solve such planning problems." ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class Problem(PlanningProblem):\n",
       "    """\n",
       "    Define real-world problems by aggregating resources as numerical quantities instead of\n",
       "    named entities.\n",
       "\n",
       "    This class is identical to PDLL, except that it overloads the act function to handle\n",
       "    resource and ordering conditions imposed by HLA as opposed to Action.\n",
       "    """\n",
       "    def __init__(self, init, goals, actions, jobs=None, resources=None):\n",
       "        super().__init__(init, goals, actions)\n",
       "        self.jobs = jobs\n",
       "        self.resources = resources or {}\n",
       "\n",
       "    def act(self, action):\n",
       "        """\n",
       "        Performs the HLA given as argument.\n",
       "\n",
       "        Note that this is different from the superclass action - where the parameter was an\n",
       "        Expression. For real world problems, an Expr object isn't enough to capture all the\n",
       "        detail required for executing the action - resources, preconditions, etc need to be\n",
       "        checked for too.\n",
       "        """\n",
       "        args = action.args\n",
       "        list_action = first(a for a in self.actions if a.name == action.name)\n",
       "        if list_action is None:\n",
       "            raise Exception("Action '{}' not found".format(action.name))\n",
       "        self.init = list_action.do_action(self.jobs, self.resources, self.init, args).clauses\n",
       "\n",
       "    def refinements(hla, state, library):  # TODO - refinements may be (multiple) HLA themselves ...\n",
       "        """\n",
       "        state is a Problem, containing the current state kb\n",
       "        library is a dictionary containing details for every possible refinement. eg:\n",
       "        {\n",
       "        'HLA': ['Go(Home,SFO)', 'Go(Home,SFO)', 'Drive(Home, SFOLongTermParking)', 'Shuttle(SFOLongTermParking, SFO)', 'Taxi(Home, SFO)'],\n",
       "        'steps': [['Drive(Home, SFOLongTermParking)', 'Shuttle(SFOLongTermParking, SFO)'], ['Taxi(Home, SFO)'], [], [], []],\n",
       "        # empty refinements ie primitive action\n",
       "        'precond': [['At(Home), Have(Car)'], ['At(Home)'], ['At(Home)', 'Have(Car)'], ['At(SFOLongTermParking)'], ['At(Home)']],\n",
       "        'effect': [['At(SFO)'], ['At(SFO)'], ['At(SFOLongTermParking)'], ['At(SFO)'], ['At(SFO)'], ['~At(Home)'], ['~At(Home)'], ['~At(Home)'], ['~At(SFOLongTermParking)'], ['~At(Home)']]\n",
       "        }\n",
       "        """\n",
       "        e = Expr(hla.name, hla.args)\n",
       "        indices = [i for i, x in enumerate(library['HLA']) if expr(x).op == hla.name]\n",
       "        for i in indices:\n",
       "            # TODO multiple refinements\n",
       "            precond = []\n",
       "            for p in library['precond'][i]:\n",
       "                if p[0] == '~':\n",
       "                    precond.append(expr('Not' + p[1:]))\n",
       "                else:\n",
       "                    precond.append(expr(p))\n",
       "            effect = []\n",
       "            for e in library['effect'][i]:\n",
       "                if e[0] == '~':\n",
       "                    effect.append(expr('Not' + e[1:]))\n",
       "                else:\n",
       "                    effect.append(expr(e))\n",
       "            action = HLA(library['steps'][i][0], precond, effect)\n",
       "            if action.check_precond(state.init, action.args):\n",
       "                yield action\n",
       "\n",
       "    def hierarchical_search(problem, hierarchy):\n",
       "        """\n",
       "        [Figure 11.5] 'Hierarchical Search, a Breadth First Search implementation of Hierarchical\n",
       "        Forward Planning Search'\n",
       "        The problem is a real-world problem defined by the problem class, and the hierarchy is\n",
       "        a dictionary of HLA - refinements (see refinements generator for details)\n",
       "        """\n",
       "        act = Node(problem.actions[0])\n",
       "        frontier = deque()\n",
       "        frontier.append(act)\n",
       "        while True:\n",
       "            if not frontier:\n",
       "                return None\n",
       "            plan = frontier.popleft()\n",
       "            print(plan.state.name)\n",
       "            hla = plan.state  # first_or_null(plan)\n",
       "            prefix = None\n",
       "            if plan.parent:\n",
       "                prefix = plan.parent.state.action  # prefix, suffix = subseq(plan.state, hla)\n",
       "            outcome = Problem.result(problem, prefix)\n",
       "            if hla is None:\n",
       "                if outcome.goal_test():\n",
       "                    return plan.path()\n",
       "            else:\n",
       "                print("else")\n",
       "                for sequence in Problem.refinements(hla, outcome, hierarchy):\n",
       "                    print("...")\n",
       "                    frontier.append(Node(plan.state, plan.parent, sequence))\n",
       "\n",
       "    def result(problem, action):\n",
       "        """The outcome of applying an action to the current problem"""\n",
       "        if action is not None:\n",
       "            problem.act(action)\n",
       "            return problem\n",
       "        else:\n",
       "            return problem\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(Problem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## HLA\n", "To be able to model a real-world planning problem properly, it is essential to be able to represent a _high-level action (HLA)_ that can be hierarchically reduced to primitive actions." ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class HLA(Action):\n",
       "    """\n",
       "    Define Actions for the real-world (that may be refined further), and satisfy resource\n",
       "    constraints.\n",
       "    """\n",
       "    unique_group = 1\n",
       "\n",
       "    def __init__(self, action, precond=None, effect=None, duration=0,\n",
       "                 consume=None, use=None):\n",
       "        """\n",
       "        As opposed to actions, to define HLA, we have added constraints.\n",
       "        duration holds the amount of time required to execute the task\n",
       "        consumes holds a dictionary representing the resources the task consumes\n",
       "        uses holds a dictionary representing the resources the task uses\n",
       "        """\n",
       "        precond = precond or [None]\n",
       "        effect = effect or [None]\n",
       "        super().__init__(action, precond, effect)\n",
       "        self.duration = duration\n",
       "        self.consumes = consume or {}\n",
       "        self.uses = use or {}\n",
       "        self.completed = False\n",
       "        # self.priority = -1 #  must be assigned in relation to other HLAs\n",
       "        # self.job_group = -1 #  must be assigned in relation to other HLAs\n",
       "\n",
       "    def do_action(self, job_order, available_resources, kb, args):\n",
       "        """\n",
       "        An HLA based version of act - along with knowledge base updation, it handles\n",
       "        resource checks, and ensures the actions are executed in the correct order.\n",
       "        """\n",
       "        # print(self.name)\n",
       "        if not self.has_usable_resource(available_resources):\n",
       "            raise Exception('Not enough usable resources to execute {}'.format(self.name))\n",
       "        if not self.has_consumable_resource(available_resources):\n",
       "            raise Exception('Not enough consumable resources to execute {}'.format(self.name))\n",
       "        if not self.inorder(job_order):\n",
       "            raise Exception("Can't execute {} - execute prerequisite actions first".\n",
       "                            format(self.name))\n",
       "        kb = super().act(kb, args)  # update knowledge base\n",
       "        for resource in self.consumes:  # remove consumed resources\n",
       "            available_resources[resource] -= self.consumes[resource]\n",
       "        self.completed = True  # set the task status to complete\n",
       "        return kb\n",
       "\n",
       "    def has_consumable_resource(self, available_resources):\n",
       "        """\n",
       "        Ensure there are enough consumable resources for this action to execute.\n",
       "        """\n",
       "        for resource in self.consumes:\n",
       "            if available_resources.get(resource) is None:\n",
       "                return False\n",
       "            if available_resources[resource] < self.consumes[resource]:\n",
       "                return False\n",
       "        return True\n",
       "\n",
       "    def has_usable_resource(self, available_resources):\n",
       "        """\n",
       "        Ensure there are enough usable resources for this action to execute.\n",
       "        """\n",
       "        for resource in self.uses:\n",
       "            if available_resources.get(resource) is None:\n",
       "                return False\n",
       "            if available_resources[resource] < self.uses[resource]:\n",
       "                return False\n",
       "        return True\n",
       "\n",
       "    def inorder(self, job_order):\n",
       "        """\n",
       "        Ensure that all the jobs that had to be executed before the current one have been\n",
       "        successfully executed.\n",
       "        """\n",
       "        for jobs in job_order:\n",
       "            if self in jobs:\n",
       "                for job in jobs:\n",
       "                    if job is self:\n",
       "                        return True\n",
       "                    if not job.completed:\n",
       "                        return False\n",
       "        return True\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(HLA)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition to preconditions and effects, an object of the `HLA` class also stores:\n", "- the `duration` of the HLA\n", "- the quantity of consumption of _consumable_ resources\n", "- the quantity of _reusable_ resources used\n", "- a bool `completed` denoting if the `HLA` has been completed\n", "\n", "The class also has some useful helper methods:\n", "- `do_action`: checks if required consumable and reusable resources are available and if so, executes the action.\n", "- `has_consumable_resource`: checks if there exists sufficient quantity of the required consumable resource.\n", "- `has_usable_resource`: checks if reusable resources are available and not already engaged.\n", "- `inorder`: ensures that all the jobs that had to be executed before the current one have been successfully executed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PLANNING PROBLEMS\n", "---\n", "## Job-shop Problem\n", "This is a simple problem involving the assembly of two cars simultaneously.\n", "The problem consists of two jobs, each of the form [`AddEngine`, `AddWheels`, `Inspect`] to be performed on two cars with different requirements and availability of resources.\n", "
\n", "Let's look at how the `job_shop_problem` has been defined on the module." ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def job_shop_problem():\n",
       "    """\n",
       "    [Figure 11.1] JOB-SHOP-PROBLEM\n",
       "\n",
       "    A job-shop scheduling problem for assembling two cars,\n",
       "    with resource and ordering constraints.\n",
       "\n",
       "    Example:\n",
       "    >>> from planning import *\n",
       "    >>> p = job_shop_problem()\n",
       "    >>> p.goal_test()\n",
       "    False\n",
       "    >>> p.act(p.jobs[1][0])\n",
       "    >>> p.act(p.jobs[1][1])\n",
       "    >>> p.act(p.jobs[1][2])\n",
       "    >>> p.act(p.jobs[0][0])\n",
       "    >>> p.act(p.jobs[0][1])\n",
       "    >>> p.goal_test()\n",
       "    False\n",
       "    >>> p.act(p.jobs[0][2])\n",
       "    >>> p.goal_test()\n",
       "    True\n",
       "    >>>\n",
       "    """\n",
       "    resources = {'EngineHoists': 1, 'WheelStations': 2, 'Inspectors': 2, 'LugNuts': 500}\n",
       "\n",
       "    add_engine1 = HLA('AddEngine1', precond='~Has(C1, E1)', effect='Has(C1, E1)', duration=30, use={'EngineHoists': 1})\n",
       "    add_engine2 = HLA('AddEngine2', precond='~Has(C2, E2)', effect='Has(C2, E2)', duration=60, use={'EngineHoists': 1})\n",
       "    add_wheels1 = HLA('AddWheels1', precond='~Has(C1, W1)', effect='Has(C1, W1)', duration=30, use={'WheelStations': 1}, consume={'LugNuts': 20})\n",
       "    add_wheels2 = HLA('AddWheels2', precond='~Has(C2, W2)', effect='Has(C2, W2)', duration=15, use={'WheelStations': 1}, consume={'LugNuts': 20})\n",
       "    inspect1 = HLA('Inspect1', precond='~Inspected(C1)', effect='Inspected(C1)', duration=10, use={'Inspectors': 1})\n",
       "    inspect2 = HLA('Inspect2', precond='~Inspected(C2)', effect='Inspected(C2)', duration=10, use={'Inspectors': 1})\n",
       "\n",
       "    actions = [add_engine1, add_engine2, add_wheels1, add_wheels2, inspect1, inspect2]\n",
       "\n",
       "    job_group1 = [add_engine1, add_wheels1, inspect1]\n",
       "    job_group2 = [add_engine2, add_wheels2, inspect2]\n",
       "\n",
       "    return Problem(init='Car(C1) & Car(C2) & Wheels(W1) & Wheels(W2) & Engine(E2) & Engine(E2) & ~Has(C1, E1) & ~Has(C2, E2) & ~Has(C1, W1) & ~Has(C2, W2) & ~Inspected(C1) & ~Inspected(C2)',\n",
       "                   goals='Has(C1, W1) & Has(C1, E1) & Inspected(C1) & Has(C2, W2) & Has(C2, E2) & Inspected(C2)',\n",
       "                   actions=actions,\n",
       "                   jobs=[job_group1, job_group2],\n",
       "                   resources=resources)\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(job_shop_problem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The states of this problem are:\n", "
\n", "
\n", "**Has(x, y)**: Car **'x'** _has_ **'y'** where **'y'** can be an Engine or a Wheel.\n", "\n", "**~Has(x, y)**: Car **'x'** does _not have_ **'y'** where **'y'** can be an Engine or a Wheel.\n", "\n", "**Inspected(c)**: Car **'c'** has been _inspected_.\n", "\n", "**~Inspected(c)**: Car **'c'** has _not_ been inspected.\n", "\n", "In the initial state, `C1` and `C2` are cars and neither have an engine or wheels and haven't been inspected.\n", "`E1` and `E2` are engines.\n", "`W1` and `W2` are wheels.\n", "
\n", "Our goal is to have engines and wheels on both cars and to get them inspected. We will discuss how to achieve this.\n", "
\n", "Let's define an object of the `job_shop_problem`." ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": true }, "outputs": [], "source": [ "jobShopProblem = job_shop_problem()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before taking any actions, we will check if `jobShopProblem` has reached its goal." ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(jobShopProblem.goal_test())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now define a possible solution that can help us reach the goal. \n", "The actions are then carried out on the `jobShopProblem` object." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following actions are available to us:\n", "\n", "**AddEngine1**: Adds an engine to the car C1. Takes 30 minutes to complete and uses an engine hoist.\n", " \n", "**AddEngine2**: Adds an engine to the car C2. Takes 60 minutes to complete and uses an engine hoist.\n", "\n", "**AddWheels1**: Adds wheels to car C1. Takes 30 minutes to complete. Uses a wheel station and consumes 20 lug nuts.\n", "\n", "**AddWheels2**: Adds wheels to car C2. Takes 15 minutes to complete. Uses a wheel station and consumes 20 lug nuts as well.\n", "\n", "**Inspect1**: Gets car C1 inspected. Requires 10 minutes of inspection by one inspector.\n", "\n", "**Inspect2**: Gets car C2 inspected. Requires 10 minutes of inspection by one inspector." ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": true }, "outputs": [], "source": [ "solution = [jobShopProblem.jobs[1][0],\n", " jobShopProblem.jobs[1][1],\n", " jobShopProblem.jobs[1][2],\n", " jobShopProblem.jobs[0][0],\n", " jobShopProblem.jobs[0][1],\n", " jobShopProblem.jobs[0][2]]\n", "\n", "for action in solution:\n", " jobShopProblem.act(action)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(jobShopProblem.goal_test())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a valid solution and one of many correct ways to solve this problem." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Double tennis problem\n", "This problem is a simple case of a multiactor planning problem, where two agents act at once and can simultaneously change the current state of the problem. \n", "A correct plan is one that, if executed by the actors, achieves the goal.\n", "In the true multiagent setting, of course, the agents may not agree to execute any particular plan, but atleast they will know what plans _would_ work if they _did_ agree to execute them.\n", "
\n", "In the double tennis problem, two actors A and B are playing together and can be in one of four locations: `LeftBaseLine`, `RightBaseLine`, `LeftNet` and `RightNet`.\n", "The ball can be returned only if a player is in the right place.\n", "Each action must include the actor as an argument.\n", "
\n", "Let's first look at the definition of the `double_tennis_problem` in the module." ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
def double_tennis_problem():\n",
       "    """\n",
       "    [Figure 11.10] DOUBLE-TENNIS-PROBLEM\n",
       "\n",
       "    A multiagent planning problem involving two partner tennis players\n",
       "    trying to return an approaching ball and repositioning around in the court.\n",
       "\n",
       "    Example:\n",
       "    >>> from planning import *\n",
       "    >>> dtp = double_tennis_problem()\n",
       "    >>> goal_test(dtp.goals, dtp.init)\n",
       "    False\n",
       "    >>> dtp.act(expr('Go(A, RightBaseLine, LeftBaseLine)'))\n",
       "    >>> dtp.act(expr('Hit(A, Ball, RightBaseLine)'))\n",
       "    >>> goal_test(dtp.goals, dtp.init)\n",
       "    False\n",
       "    >>> dtp.act(expr('Go(A, LeftNet, RightBaseLine)'))\n",
       "    >>> goal_test(dtp.goals, dtp.init)\n",
       "    True\n",
       "    >>>\n",
       "    """\n",
       "\n",
       "    return PlanningProblem(init='At(A, LeftBaseLine) & At(B, RightNet) & Approaching(Ball, RightBaseLine) & Partner(A, B) & Partner(B, A)',\n",
       "                             goals='Returned(Ball) & At(x, LeftNet) & At(y, RightNet)',\n",
       "                             actions=[Action('Hit(actor, Ball, loc)',\n",
       "                                             precond='Approaching(Ball, loc) & At(actor, loc)',\n",
       "                                             effect='Returned(Ball)'),\n",
       "                                      Action('Go(actor, to, loc)', \n",
       "                                             precond='At(actor, loc)',\n",
       "                                             effect='At(actor, to) & ~At(actor, loc)')])\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(double_tennis_problem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The states of this problem are:\n", "\n", "**Approaching(Ball, loc)**: The `Ball` is approaching the location `loc`.\n", "\n", "**Returned(Ball)**: One of the actors successfully hit the approaching ball from the correct location which caused it to return to the other side.\n", "\n", "**At(actor, loc)**: `actor` is at location `loc`.\n", "\n", "**~At(actor, loc)**: `actor` is _not_ at location `loc`.\n", "\n", "Let's now define an object of `double_tennis_problem`.\n" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": true }, "outputs": [], "source": [ "doubleTennisProblem = double_tennis_problem()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before taking any actions, we will check if `doubleTennisProblem` has reached the goal." ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(goal_test(doubleTennisProblem.goals, doubleTennisProblem.init))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we can see, the goal hasn't been reached. \n", "We now define a possible solution that can help us reach the goal of having the ball returned.\n", "The actions will then be carried out on the `doubleTennisProblem` object." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The actions available to us are the following:\n", "\n", "**Hit(actor, ball, loc)**: returns an approaching ball if `actor` is present at the `loc` that the ball is approaching.\n", "\n", "**Go(actor, to, loc)**: moves an `actor` from location `loc` to location `to`.\n", "\n", "We notice something different in this problem though, \n", "which is quite unlike any other problem we have seen so far. \n", "The goal state of the problem contains a variable `a`.\n", "This happens sometimes in multiagent planning problems \n", "and it means that it doesn't matter _which_ actor is at the `LeftNet` or the `RightNet`, as long as there is atleast one actor at either `LeftNet` or `RightNet`." ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": true }, "outputs": [], "source": [ "solution = [expr('Go(A, RightBaseLine, LeftBaseLine)'),\n", " expr('Hit(A, Ball, RightBaseLine)'),\n", " expr('Go(A, LeftNet, RightBaseLine)')]\n", "\n", "for action in solution:\n", " doubleTennisProblem.act(action)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "goal_test(doubleTennisProblem.goals, doubleTennisProblem.init)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has now successfully reached its goal, ie, to return the approaching ball." ] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }