{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Angelic Search \n", "\n", "Search using angelic semantics (is a hierarchical search), where the agent chooses the implementation of the HLA's.
\n", "The algorithms input is: problem, hierarchy and initialPlan\n", "- problem is of type Problem \n", "- hierarchy is a dictionary consisting of all the actions. \n", "- initialPlan is an approximate description(optimistic and pessimistic) of the agents choices for the implementation.
\n", " initialPlan contains a sequence of HLA's with angelic semantics" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from planning import * \n", "from notebook import psource" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Angelic search algorithm consists of three parts. \n", "- Search using angelic semantics\n", "- Decompose\n", "- a search in the space of refinements, in a similar way with hierarchical search\n", "\n", "### Searching using angelic semantics\n", "- Find the reachable set (optimistic and pessimistic) of the sequence of angelic HLA in initialPlan\n", " - If the optimistic reachable set doesn't intersect the goal, then there is no solution\n", " - If the pessimistic reachable set intersects the goal, then we call decompose, in order to find the sequence of actions that lead us to the goal. \n", " - If the optimistic reachable set intersects the goal, but the pessimistic doesn't we do some further refinements, in order to see if there is a sequence of actions that achieves the goal. \n", " \n", "### Search in space of refinements\n", "- Create a search tree, that has root the action and children it's refinements\n", "- Extend frontier by adding each refinement, so that we keep looping till we find all primitive actions\n", "- If we achieve that we return the path of the solution (search tree), else there is no solution and we return None.\n", "\n", " \n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
    def angelic_search(problem, hierarchy, initialPlan):\n",
       "        """\n",
       "\t[Figure 11.8] A hierarchical planning algorithm that uses angelic semantics to identify and\n",
       "\tcommit to high-level plans that work while avoiding high-level plans that don’t. \n",
       "\tThe predicate MAKING-PROGRESS checks to make sure that we aren’t stuck in an infinite regression\n",
       "\tof refinements. \n",
       "\tAt top level, call ANGELIC -SEARCH with [Act ] as the initialPlan .\n",
       "\n",
       "        initialPlan contains a sequence of HLA's with angelic semantics \n",
       "\n",
       "        The possible effects of an angelic HLA in initialPlan are : \n",
       "        ~ : effect remove\n",
       "        $+: effect possibly add\n",
       "        $-: effect possibly remove\n",
       "        $$: possibly add or remove\n",
       "\t"""\n",
       "        frontier = deque(initialPlan)\n",
       "        while True: \n",
       "            if not frontier:\n",
       "                return None\n",
       "            plan = frontier.popleft() # sequence of HLA/Angelic HLA's \n",
       "            opt_reachable_set = Problem.reach_opt(problem.init, plan)\n",
       "            pes_reachable_set = Problem.reach_pes(problem.init, plan)\n",
       "            if problem.intersects_goal(opt_reachable_set): \n",
       "                if Problem.is_primitive( plan, hierarchy ): \n",
       "                    return ([x for x in plan.action])\n",
       "                guaranteed = problem.intersects_goal(pes_reachable_set) \n",
       "                if guaranteed and Problem.making_progress(plan, initialPlan):\n",
       "                    final_state = guaranteed[0] # any element of guaranteed \n",
       "                    #print('decompose')\n",
       "                    return Problem.decompose(hierarchy, problem, plan, final_state, pes_reachable_set)\n",
       "                (hla, index) = Problem.find_hla(plan, hierarchy) # there should be at least one HLA/Angelic_HLA, otherwise plan would be primitive.\n",
       "                prefix = plan.action[:index]\n",
       "                suffix = plan.action[index+1:]\n",
       "                outcome = Problem(Problem.result(problem.init, prefix), problem.goals , problem.actions )\n",
       "                for sequence in Problem.refinements(hla, outcome, hierarchy): # find refinements\n",
       "                    frontier.append(Angelic_Node(outcome.init, plan, prefix + sequence+ suffix, prefix+sequence+suffix))\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(Problem.angelic_search)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### Decompose \n", "- Finds recursively the sequence of states and actions that lead us from initial state to goal.\n", "- For each of the above actions we find their refinements,if they are not primitive, by calling the angelic_search function. \n", " If there are not refinements return None\n", " \n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
    def decompose(hierarchy, s_0, plan, s_f, reachable_set):\n",
       "        solution = [] \n",
       "        i = max(reachable_set.keys())\n",
       "        while plan.action_pes: \n",
       "            action = plan.action_pes.pop()\n",
       "            if (i==0): \n",
       "                return solution\n",
       "            s_i = Problem.find_previous_state(s_f, reachable_set,i, action) \n",
       "            problem = Problem(s_i, s_f , plan.action)\n",
       "            angelic_call = Problem.angelic_search(problem, hierarchy, [Angelic_Node(s_i, Node(None), [action],[action])])\n",
       "            if angelic_call:\n",
       "                for x in angelic_call: \n",
       "                    solution.insert(0,x)\n",
       "            else: \n",
       "                return None\n",
       "            s_f = s_i\n",
       "            i-=1\n",
       "        return solution\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(Problem.decompose)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example\n", "\n", "Suppose that somebody wants to get to the airport. \n", "The possible ways to do so is either get a taxi, or drive to the airport.
\n", "Those two actions have some preconditions and some effects. \n", "If you get the taxi, you need to have cash, whereas if you drive you need to have a car.
\n", "Thus we define the following hierarchy of possible actions.\n", "\n", "##### hierarchy" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "library = {\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", " 'precond': [['At(Home) & Have(Car)'], ['At(Home)'], ['At(Home) & Have(Car)'], ['At(SFOLongTermParking)'], ['At(Home)']],\n", " 'effect': [['At(SFO) & ~At(Home)'], ['At(SFO) & ~At(Home) & ~Have(Cash)'], ['At(SFOLongTermParking) & ~At(Home)'], ['At(SFO) & ~At(LongTermParking)'], ['At(SFO) & ~At(Home) & ~Have(Cash)']] }\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "the possible actions are the following:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "go_SFO = HLA('Go(Home,SFO)', precond='At(Home)', effect='At(SFO) & ~At(Home)')\n", "taxi_SFO = HLA('Taxi(Home,SFO)', precond='At(Home)', effect='At(SFO) & ~At(Home) & ~Have(Cash)')\n", "drive_SFOLongTermParking = HLA('Drive(Home, SFOLongTermParking)', 'At(Home) & Have(Car)','At(SFOLongTermParking) & ~At(Home)' )\n", "shuttle_SFO = HLA('Shuttle(SFOLongTermParking, SFO)', 'At(SFOLongTermParking)', 'At(SFO) & ~At(LongTermParking)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose that (our preconditionds are that) we are Home and we have cash and car and our goal is to get to SFO and maintain our cash, and our possible actions are the above.
\n", "##### Then our problem is: " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "prob = Problem('At(Home) & Have(Cash) & Have(Car)', 'At(SFO) & Have(Cash)', [go_SFO, taxi_SFO, drive_SFOLongTermParking,shuttle_SFO])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An agent gives us some approximate information about the plan we will follow:
\n", "(initialPlan is an Angelic Node, where: \n", "- state is the initial state of the problem, \n", "- parent is None \n", "- action: is a list of actions (Angelic HLA's) with the optimistic estimators of effects and \n", "- action_pes: is a list of actions (Angelic HLA's) with the pessimistic approximations of the effects\n", "##### InitialPlan" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "angelic_opt_description = Angelic_HLA('Go(Home, SFO)', precond = 'At(Home)', effect ='$+At(SFO) & $-At(Home)' ) \n", "angelic_pes_description = Angelic_HLA('Go(Home, SFO)', precond = 'At(Home)', effect ='$+At(SFO) & ~At(Home)' )\n", "\n", "initialPlan = [Angelic_Node(prob.init, None, [angelic_opt_description], [angelic_pes_description])] \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to find the optimistic and pessimistic reachable set of initialPlan when applied to the problem:\n", "##### Optimistic/Pessimistic reachable set" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[At(Home), Have(Cash), Have(Car)], [Have(Cash), Have(Car), At(SFO), NotAt(Home)], [Have(Cash), Have(Car), NotAt(Home)], [At(Home), Have(Cash), Have(Car), At(SFO)], [At(Home), Have(Cash), Have(Car)]] \n", "\n", "[[At(Home), Have(Cash), Have(Car)], [Have(Cash), Have(Car), At(SFO), NotAt(Home)], [Have(Cash), Have(Car), NotAt(Home)]]\n" ] } ], "source": [ "opt_reachable_set = Problem.reach_opt(prob.init, initialPlan[0])\n", "pes_reachable_set = Problem.reach_pes(prob.init, initialPlan[0])\n", "print([x for y in opt_reachable_set.keys() for x in opt_reachable_set[y]], '\\n')\n", "print([x for y in pes_reachable_set.keys() for x in pes_reachable_set[y]])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Refinements" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[HLA(Drive(Home, SFOLongTermParking)), HLA(Shuttle(SFOLongTermParking, SFO))]\n", "[{'duration': 0, 'effect': [At(SFOLongTermParking), NotAt(Home)], 'args': (Home, SFOLongTermParking), 'uses': {}, 'consumes': {}, 'name': 'Drive', 'completed': False, 'precond': [At(Home), Have(Car)]}, {'duration': 0, 'effect': [At(SFO), NotAt(LongTermParking)], 'args': (SFOLongTermParking, SFO), 'uses': {}, 'consumes': {}, 'name': 'Shuttle', 'completed': False, 'precond': [At(SFOLongTermParking)]}] \n", "\n", "[HLA(Taxi(Home, SFO))]\n", "[{'duration': 0, 'effect': [At(SFO), NotAt(Home), NotHave(Cash)], 'args': (Home, SFO), 'uses': {}, 'consumes': {}, 'name': 'Taxi', 'completed': False, 'precond': [At(Home)]}] \n", "\n" ] } ], "source": [ "for sequence in Problem.refinements(go_SFO, prob, library):\n", " print (sequence)\n", " print([x.__dict__ for x in sequence ], '\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the angelic search\n", "##### Top level call" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[HLA(Drive(Home, SFOLongTermParking)), HLA(Shuttle(SFOLongTermParking, SFO))] \n", "\n", "[{'duration': 0, 'effect': [At(SFOLongTermParking), NotAt(Home)], 'args': (Home, SFOLongTermParking), 'uses': {}, 'consumes': {}, 'name': 'Drive', 'completed': False, 'precond': [At(Home), Have(Car)]}, {'duration': 0, 'effect': [At(SFO), NotAt(LongTermParking)], 'args': (SFOLongTermParking, SFO), 'uses': {}, 'consumes': {}, 'name': 'Shuttle', 'completed': False, 'precond': [At(SFOLongTermParking)]}]\n" ] } ], "source": [ "plan= Problem.angelic_search(prob, library, initialPlan)\n", "print (plan, '\\n')\n", "print ([x.__dict__ for x in plan])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 2" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "library_2 = {\n", " 'HLA': ['Go(Home,SFO)', 'Go(Home,SFO)', 'Bus(Home, MetroStop)', 'Metro(MetroStop, SFO)' , 'Metro(MetroStop, SFO)', 'Metro1(MetroStop, SFO)', 'Metro2(MetroStop, SFO)' ,'Taxi(Home, SFO)'],\n", " 'steps': [['Bus(Home, MetroStop)', 'Metro(MetroStop, SFO)'], ['Taxi(Home, SFO)'], [], ['Metro1(MetroStop, SFO)'], ['Metro2(MetroStop, SFO)'],[],[],[]],\n", " 'precond': [['At(Home)'], ['At(Home)'], ['At(Home)'], ['At(MetroStop)'], ['At(MetroStop)'],['At(MetroStop)'], ['At(MetroStop)'] ,['At(Home) & Have(Cash)']],\n", " 'effect': [['At(SFO) & ~At(Home)'], ['At(SFO) & ~At(Home) & ~Have(Cash)'], ['At(MetroStop) & ~At(Home)'], ['At(SFO) & ~At(MetroStop)'], ['At(SFO) & ~At(MetroStop)'], ['At(SFO) & ~At(MetroStop)'] , ['At(SFO) & ~At(MetroStop)'] ,['At(SFO) & ~At(Home) & ~Have(Cash)']] \n", " }" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[HLA(Bus(Home, MetroStop)), HLA(Metro1(MetroStop, SFO))] \n", "\n", "[{'duration': 0, 'effect': [At(MetroStop), NotAt(Home)], 'args': (Home, MetroStop), 'uses': {}, 'consumes': {}, 'name': 'Bus', 'completed': False, 'precond': [At(Home)]}, {'duration': 0, 'effect': [At(SFO), NotAt(MetroStop)], 'args': (MetroStop, SFO), 'uses': {}, 'consumes': {}, 'name': 'Metro1', 'completed': False, 'precond': [At(MetroStop)]}]\n" ] } ], "source": [ "plan_2 = Problem.angelic_search(prob, library_2, initialPlan)\n", "print(plan_2, '\\n')\n", "print([x.__dict__ for x in plan_2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 3 \n", "\n", "Sometimes there is no plan that achieves the goal!" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "library_3 = {\n", " 'HLA': ['Shuttle(SFOLongTermParking, SFO)', 'Go(Home, SFOLongTermParking)', 'Taxi(Home, SFOLongTermParking)', 'Drive(Home, SFOLongTermParking)', 'Drive(SFOLongTermParking, Home)', 'Get(Cash)', 'Go(Home, ATM)'],\n", " 'steps': [['Get(Cash)', 'Go(Home, SFOLongTermParking)'], ['Taxi(Home, SFOLongTermParking)'], [], [], [], ['Drive(SFOLongTermParking, Home)', 'Go(Home, ATM)'], []],\n", " 'precond': [['At(SFOLongTermParking)'], ['At(Home)'], ['At(Home) & Have(Cash)'], ['At(Home)'], ['At(SFOLongTermParking)'], ['At(SFOLongTermParking)'], ['At(Home)']],\n", " 'effect': [['At(SFO)'], ['At(SFO)'], ['At(SFOLongTermParking) & ~Have(Cash)'], ['At(SFOLongTermParking)'] ,['At(Home) & ~At(SFOLongTermParking)'], ['At(Home) & Have(Cash)'], ['Have(Cash)'] ]\n", " }\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "shuttle_SFO = HLA('Shuttle(SFOLongTermParking, SFO)', 'Have(Cash) & At(SFOLongTermParking)', 'At(SFO)')\n", "prob_3 = Problem('At(SFOLongTermParking) & Have(Cash)', 'At(SFO) & Have(Cash)', [shuttle_SFO])\n", "# optimistic/pessimistic descriptions\n", "angelic_opt_description = Angelic_HLA('Shuttle(SFOLongTermParking, SFO)', precond = 'At(SFOLongTermParking)', effect ='$+At(SFO) & $-At(SFOLongTermParking)' ) \n", "angelic_pes_description = Angelic_HLA('Shuttle(SFOLongTermParking, SFO)', precond = 'At(SFOLongTermParking)', effect ='$+At(SFO) & ~At(SFOLongTermParking)' ) \n", "# initial Plan\n", "initialPlan_3 = [Angelic_Node(prob.init, None, [angelic_opt_description], [angelic_pes_description])] " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "plan_3 = prob_3.angelic_search(library_3, initialPlan_3)\n", "print(plan_3)" ] } ], "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 }