{ "cells": [ { "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": 14, "metadata": {}, "outputs": [], "source": [ "from planning import *\n", "from notebook import psource" ] }, { "cell_type": "code", "execution_count": 15, "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": 16, "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, planningproblem):\n",
       "        self.planningproblem = planningproblem\n",
       "        self.kb = FolKB(planningproblem.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.planningproblem.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": 17, "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, planningproblem):\n",
       "        self.graph = Graph(planningproblem)\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.planningproblem.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.planningproblem.goals, -1)):\n",
       "                solution = self.extract_solution(self.graph.planningproblem.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": 18, "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": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[[Load(C2, P2, JFK),\n", " PAirport(SFO),\n", " PAirport(JFK),\n", " PPlane(P2),\n", " PPlane(P1),\n", " Fly(P2, JFK, SFO),\n", " PCargo(C2),\n", " Load(C1, P1, SFO),\n", " Fly(P1, SFO, JFK),\n", " PCargo(C1)],\n", " [Unload(C2, P2, SFO), Unload(C1, P1, JFK)]]]" ] }, "execution_count": 19, "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": 20, "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": 20, "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": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Remove(Spare, Trunk), Remove(Flat, Axle), PutOn(Spare, Axle)]" ] }, "execution_count": 21, "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": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Eat(Cake), Bake(Cake)]" ] }, "execution_count": 22, "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": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[MoveToTable(C, A), Move(B, Table, C), Move(A, Table, B)]" ] }, "execution_count": 23, "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": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[RightSock, LeftSock, RightShoe, LeftShoe]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "socksShoesG = socks_and_shoes_graphplan()\n", "linearize(socksShoesG)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.3" } }, "nbformat": 4, "nbformat_minor": 1 }