February 21, 2020

PLANNING IN THE REAL WORLD


PROBLEM

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. The Problem class includes everything that the PlanningProblem class includes. Additionally, it also includes the following attributes essential to define a real-world planning problem:

  • a list of jobs to be done
  • a dictionary of resources

It also overloads the act method to call the do_action method of the HLA class, and also includes a new method refinements that finds refinements or primitive actions for high level actions.
hierarchical_search and angelic_search are also built into the Problem class to solve such planning problems.

In [1]:
from planningv2 import *
from notebook import psource
In [5]:
psource(Problem)

class Problem(PlanningProblem):
    """
    Define real-world problems by aggregating resources as numerical quantities instead of
    named entities.

    This class is identical to PDLL, except that it overloads the act function to handle
    resource and ordering conditions imposed by HLA as opposed to Action.
    """
    def __init__(self, init, goals, actions, jobs=None, resources=None):
        super().__init__(init, goals, actions)
        self.jobs = jobs
        self.resources = resources or {}

    def act(self, action):
        """
        Performs the HLA given as argument.

        Note that this is different from the superclass action - where the parameter was an
        Expression. For real world problems, an Expr object isn't enough to capture all the
        detail required for executing the action - resources, preconditions, etc need to be
        checked for too.
        """
        args = action.args
        list_action = first(a for a in self.actions if a.name == action.name)
        if list_action is None:
            raise Exception("Action '{}' not found".format(action.name))
        self.init = list_action.do_action(self.jobs, self.resources, self.init, args).clauses

    def refinements(hla, state, library):  # refinements may be (multiple) HLA themselves ...
        """
        state is a Problem, containing the current state kb
        library is a dictionary containing details for every possible refinement. eg:
        {
        'HLA': [
            'Go(Home, SFO)',
            'Go(Home, SFO)',
            'Drive(Home, SFOLongTermParking)',
            'Shuttle(SFOLongTermParking, SFO)',
            'Taxi(Home, SFO)'
            ],
        'steps': [
            ['Drive(Home, SFOLongTermParking)', 'Shuttle(SFOLongTermParking, SFO)'],
            ['Taxi(Home, SFO)'],
            [],
            [],
            []
            ],
        # empty refinements indicate a primitive action
        'precond': [
            ['At(Home) & Have(Car)'],
            ['At(Home)'],
            ['At(Home) & Have(Car)'],
            ['At(SFOLongTermParking)'],
            ['At(Home)']
            ],
        'effect': [
            ['At(SFO) & ~At(Home)'],
            ['At(SFO) & ~At(Home)'],
            ['At(SFOLongTermParking) & ~At(Home)'],
            ['At(SFO) & ~At(SFOLongTermParking)'],
            ['At(SFO) & ~At(Home)']
            ]
        }
        """
        e = Expr(hla.name, hla.args)
        indices = [i for i, x in enumerate(library['HLA']) if expr(x).op == hla.name]
        for i in indices:
            actions = []
            for j in range(len(library['steps'][i])):
                # find the index of the step [j]  of the HLA 
                index_step = [k for k,x in enumerate(library['HLA']) if x == library['steps'][i][j]][0]
                precond = library['precond'][index_step][0] # preconditions of step [j]
                effect = library['effect'][index_step][0] # effect of step [j]
                actions.append(HLA(library['steps'][i][j], precond, effect))
            yield actions

    def hierarchical_search(problem, hierarchy):
        """
        [Figure 11.5] 'Hierarchical Search, a Breadth First Search implementation of Hierarchical
        Forward Planning Search'
        The problem is a real-world problem defined by the problem class, and the hierarchy is
        a dictionary of HLA - refinements (see refinements generator for details)
        """
        act = Node(problem.init, None, [problem.actions[0]])
        frontier = deque()
        frontier.append(act)
        while True:
            if not frontier:
                return None
            plan = frontier.popleft()
            (hla, index) = Problem.find_hla(plan, hierarchy) # finds the first non primitive hla in plan actions
            prefix = plan.action[:index]
            outcome = Problem(Problem.result(problem.init, prefix), problem.goals , problem.actions )
            suffix = plan.action[index+1:]
            if not hla: # hla is None and plan is primitive
                if outcome.goal_test():
                    return plan.action
            else:
                for sequence in Problem.refinements(hla, outcome, hierarchy): # find refinements
                    frontier.append(Node(outcome.init, plan, prefix + sequence+ suffix))

    def result(state, actions):
        """The outcome of applying an action to the current problem"""
        for a in actions: 
            if a.check_precond(state, a.args):
                state = a(state, a.args).clauses
        return state
    

    def angelic_search(problem, hierarchy, initialPlan):
        """
	[Figure 11.8] A hierarchical planning algorithm that uses angelic semantics to identify and
	commit to high-level plans that work while avoiding high-level plans that don’t. 
	The predicate MAKING-PROGRESS checks to make sure that we aren’t stuck in an infinite regression
	of refinements. 
	At top level, call ANGELIC -SEARCH with [Act ] as the initialPlan .

        initialPlan contains a sequence of HLA's with angelic semantics 

        The possible effects of an angelic HLA in initialPlan are : 
        ~ : effect remove
        $+: effect possibly add
        $-: effect possibly remove
        $$: possibly add or remove
	"""
        frontier = deque(initialPlan)
        while True: 
            if not frontier:
                return None
            plan = frontier.popleft() # sequence of HLA/Angelic HLA's 
            opt_reachable_set = Problem.reach_opt(problem.init, plan)
            pes_reachable_set = Problem.reach_pes(problem.init, plan)
            if problem.intersects_goal(opt_reachable_set): 
                if Problem.is_primitive( plan, hierarchy ): 
                    return ([x for x in plan.action])
                guaranteed = problem.intersects_goal(pes_reachable_set) 
                if guaranteed and Problem.making_progress(plan, initialPlan):
                    final_state = guaranteed[0] # any element of guaranteed 
                    return Problem.decompose(hierarchy, problem, plan, final_state, pes_reachable_set)
                hla, index = Problem.find_hla(plan, hierarchy) # there should be at least one HLA/Angelic_HLA, otherwise plan would be primitive.
                prefix = plan.action[:index]
                suffix = plan.action[index+1:]
                outcome = Problem(Problem.result(problem.init, prefix), problem.goals , problem.actions )
                for sequence in Problem.refinements(hla, outcome, hierarchy): # find refinements
                    frontier.append(Angelic_Node(outcome.init, plan, prefix + sequence+ suffix, prefix+sequence+suffix))


    def intersects_goal(problem, reachable_set):
        """
        Find the intersection of the reachable states and the goal
        """
        return [y for x in list(reachable_set.keys()) for y in reachable_set[x] if all(goal in y for goal in problem.goals)] 


    def is_primitive(plan,  library):
        """
        checks if the hla is primitive action 
        """
        for hla in plan.action: 
            indices = [i for i, x in enumerate(library['HLA']) if expr(x).op == hla.name]
            for i in indices:
                if library["steps"][i]: 
                    return False
        return True
             


    def reach_opt(init, plan): 
        """
        Finds the optimistic reachable set of the sequence of actions in plan 
        """
        reachable_set = {0: [init]}
        optimistic_description = plan.action #list of angelic actions with optimistic description
        return Problem.find_reachable_set(reachable_set, optimistic_description)
 

    def reach_pes(init, plan): 
        """ 
        Finds the pessimistic reachable set of the sequence of actions in plan
        """
        reachable_set = {0: [init]}
        pessimistic_description = plan.action_pes # list of angelic actions with pessimistic description
        return Problem.find_reachable_set(reachable_set, pessimistic_description)

    def find_reachable_set(reachable_set, action_description):
        """
	Finds the reachable states of the action_description when applied in each state of reachable set.
	"""
        for i in range(len(action_description)):
            reachable_set[i+1]=[]
            if type(action_description[i]) is Angelic_HLA:
                possible_actions = action_description[i].angelic_action()
            else: 
                possible_actions = action_description
            for action in possible_actions:
                for state in reachable_set[i]:
                    if action.check_precond(state , action.args) :
                        if action.effect[0] :
                            new_state = action(state, action.args).clauses
                            reachable_set[i+1].append(new_state)
                        else: 
                            reachable_set[i+1].append(state)
        return reachable_set

    def find_hla(plan, hierarchy):
        """
        Finds the the first HLA action in plan.action, which is not primitive
        and its corresponding index in plan.action
        """
        hla = None
        index = len(plan.action)
        for i in range(len(plan.action)): # find the first HLA in plan, that is not primitive
            if not Problem.is_primitive(Node(plan.state, plan.parent, [plan.action[i]]), hierarchy):
                hla = plan.action[i] 
                index = i
                break
        return hla, index

    def making_progress(plan, initialPlan):
        """ 
        Prevents from infinite regression of refinements  

        (infinite regression of refinements happens when the algorithm finds a plan that 
        its pessimistic reachable set intersects the goal inside a call to decompose on the same plan, in the same circumstances)  
        """
        for i in range(len(initialPlan)):
            if (plan == initialPlan[i]):
                return False
        return True 

    def decompose(hierarchy, s_0, plan, s_f, reachable_set):
        solution = [] 
        i = max(reachable_set.keys())
        while plan.action_pes: 
            action = plan.action_pes.pop()
            if (i==0): 
                return solution
            s_i = Problem.find_previous_state(s_f, reachable_set,i, action) 
            problem = Problem(s_i, s_f , plan.action)
            angelic_call = Problem.angelic_search(problem, hierarchy, [Angelic_Node(s_i, Node(None), [action],[action])])
            if angelic_call:
                for x in angelic_call: 
                    solution.insert(0,x)
            else: 
                return None
            s_f = s_i
            i-=1
        return solution


    def find_previous_state(s_f, reachable_set, i, action):
        """
        Given a final state s_f and an action finds a state s_i in reachable_set 
        such that when action is applied to state s_i returns s_f.  
        """
        s_i = reachable_set[i-1][0]
        for state in reachable_set[i-1]:
            if s_f in [x for x in Problem.reach_pes(state, Angelic_Node(state, None, [action],[action]))[1]]:
                s_i =state
                break
        return s_i

HLA = High-Level Action

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.

Note: I have modified planningv2.py to include the debug decorator for HLA do_action

In [4]:
psource(debug)

def debug(f):
    def g(*args):
        print ("Calling: {} with args: {}".format(f.__name__, args))
        val = f(*args)
        print ("Exiting: {} with value: {}".format(f.__name__, val))
        print()
        return val
    return g 
In [2]:
psource(HLA)

class HLA(Action):
    """
    Define Actions for the real-world (that may be refined further), and satisfy resource
    constraints.
    """
    unique_group = 1

    def __init__(self, action, precond=None, effect=None, duration=0,
                 consume=None, use=None):
        """
        As opposed to actions, to define HLA, we have added constraints.
        duration holds the amount of time required to execute the task
        consumes holds a dictionary representing the resources the task consumes
        uses holds a dictionary representing the resources the task uses
        """
        precond = precond or [None]
        effect = effect or [None]
        super().__init__(action, precond, effect)
        self.duration = duration
        self.consumes = consume or {}
        self.uses = use or {}
        self.completed = False
        # self.priority = -1 #  must be assigned in relation to other HLAs
        # self.job_group = -1 #  must be assigned in relation to other HLAs

    @debug
    def do_action(self, job_order, available_resources, kb, args):
        """
        An HLA based version of act - along with knowledge base updation, it handles
        resource checks, and ensures the actions are executed in the correct order.
        """
        # print(self.name)
        if not self.has_usable_resource(available_resources):
            raise Exception('Not enough usable resources to execute {}'.format(self.name))
        if not self.has_consumable_resource(available_resources):
            raise Exception('Not enough consumable resources to execute {}'.format(self.name))
        if not self.inorder(job_order):
            raise Exception("Can't execute {} - execute prerequisite actions first".
                            format(self.name))
        kb = super().act(kb, args)  # update knowledge base
        for resource in self.consumes:  # remove consumed resources
            available_resources[resource] -= self.consumes[resource]
        self.completed = True  # set the task status to complete
        return kb

    def has_consumable_resource(self, available_resources):
        """
        Ensure there are enough consumable resources for this action to execute.
        """
        for resource in self.consumes:
            if available_resources.get(resource) is None:
                return False
            if available_resources[resource] < self.consumes[resource]:
                return False
        return True

    def has_usable_resource(self, available_resources):
        """
        Ensure there are enough usable resources for this action to execute.
        """
        for resource in self.uses:
            if available_resources.get(resource) is None:
                return False
            if available_resources[resource] < self.uses[resource]:
                return False
        return True

    def inorder(self, job_order):
        """
        Ensure that all the jobs that had to be executed before the current one have been
        successfully executed.
        """
        for jobs in job_order:
            if self in jobs:
                for job in jobs:
                    if job is self:
                        return True
                    if not job.completed:
                        return False
        return True

In addition to preconditions and effects, an object of the HLA class also stores:

  • the duration of the HLA
  • the quantity of consumption of consumable resources
  • the quantity of reusable resources used
  • a bool completed denoting if the HLA has been completed

The class also has some useful helper methods:

  • do_action: checks if required consumable and reusable resources are available and if so, executes the action.
  • has_consumable_resource: checks if there exists sufficient quantity of the required consumable resource.
  • has_usable_resource: checks if reusable resources are available and not already engaged.
  • inorder: ensures that all the jobs that had to be executed before the current one have been successfully executed.

PLANNING PROBLEMS


Job-shop Problem

This is a simple problem involving the assembly of two cars simultaneously. 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.
Let's look at how the job_shop_problem has been defined on the module.

In [6]:
psource(job_shop_problem)

def job_shop_problem():
    """
    [Figure 11.1] JOB-SHOP-PROBLEM

    A job-shop scheduling problem for assembling two cars,
    with resource and ordering constraints.

    Example:
    >>> from planning import *
    >>> p = job_shop_problem()
    >>> p.goal_test()
    False
    >>> p.act(p.jobs[1][0])
    >>> p.act(p.jobs[1][1])
    >>> p.act(p.jobs[1][2])
    >>> p.act(p.jobs[0][0])
    >>> p.act(p.jobs[0][1])
    >>> p.goal_test()
    False
    >>> p.act(p.jobs[0][2])
    >>> p.goal_test()
    True
    >>>
    """
    resources = {'EngineHoists': 1, 'WheelStations': 2, 'Inspectors': 2, 'LugNuts': 500}

    add_engine1 = HLA('AddEngine1', precond='~Has(C1, E1)', effect='Has(C1, E1)', duration=30, use={'EngineHoists': 1})
    add_engine2 = HLA('AddEngine2', precond='~Has(C2, E2)', effect='Has(C2, E2)', duration=60, use={'EngineHoists': 1})
    add_wheels1 = HLA('AddWheels1', precond='~Has(C1, W1)', effect='Has(C1, W1)', duration=30, use={'WheelStations': 1}, consume={'LugNuts': 20})
    add_wheels2 = HLA('AddWheels2', precond='~Has(C2, W2)', effect='Has(C2, W2)', duration=15, use={'WheelStations': 1}, consume={'LugNuts': 20})
    inspect1 = HLA('Inspect1', precond='~Inspected(C1)', effect='Inspected(C1)', duration=10, use={'Inspectors': 1})
    inspect2 = HLA('Inspect2', precond='~Inspected(C2)', effect='Inspected(C2)', duration=10, use={'Inspectors': 1})

    actions = [add_engine1, add_engine2, add_wheels1, add_wheels2, inspect1, inspect2]

    job_group1 = [add_engine1, add_wheels1, inspect1]
    job_group2 = [add_engine2, add_wheels2, inspect2]

    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)',
                   goals='Has(C1, W1) & Has(C1, E1) & Inspected(C1) & Has(C2, W2) & Has(C2, E2) & Inspected(C2)',
                   actions=actions,
                   jobs=[job_group1, job_group2],
                   resources=resources)

The states of this problem are:

Has(x, y): Car 'x' has 'y' where 'y' can be an Engine or a Wheel.

~Has(x, y): Car 'x' does not have 'y' where 'y' can be an Engine or a Wheel.

Inspected(c): Car 'c' has been inspected.

~Inspected(c): Car 'c' has not been inspected.

In the initial state, C1 and C2 are cars and neither have an engine or wheels and haven't been inspected. E1 and E2 are engines. W1 and W2 are wheels.
Our goal is to have engines and wheels on both cars and to get them inspected. We will discuss how to achieve this.
Let's define an object of the job_shop_problem.

In [7]:
jobShopProblem = job_shop_problem()

Before taking any actions, we will check if jobShopProblem has reached its goal.

In [8]:
print(jobShopProblem.goal_test())
False

We now define a possible solution that can help us reach the goal. The actions are then carried out on the jobShopProblem object.

The following actions are available to us:

AddEngine1: Adds an engine to the car C1. Takes 30 minutes to complete and uses an engine hoist.

AddEngine2: Adds an engine to the car C2. Takes 60 minutes to complete and uses an engine hoist.

AddWheels1: Adds wheels to car C1. Takes 30 minutes to complete. Uses a wheel station and consumes 20 lug nuts.

AddWheels2: Adds wheels to car C2. Takes 15 minutes to complete. Uses a wheel station and consumes 20 lug nuts as well.

Inspect1: Gets car C1 inspected. Requires 10 minutes of inspection by one inspector.

Inspect2: Gets car C2 inspected. Requires 10 minutes of inspection by one inspector.

In [9]:
solution = [jobShopProblem.jobs[1][0],
            jobShopProblem.jobs[1][1],
            jobShopProblem.jobs[1][2],
            jobShopProblem.jobs[0][0],
            jobShopProblem.jobs[0][1],
            jobShopProblem.jobs[0][2]]

for action in solution:
    jobShopProblem.act(action)
Calling: do_action with args: (HLA(AddEngine2), [[HLA(AddEngine1), HLA(AddWheels1), HLA(Inspect1)], [HLA(AddEngine2), HLA(AddWheels2), HLA(Inspect2)]], {'EngineHoists': 1, 'WheelStations': 2, 'Inspectors': 2, 'LugNuts': 500}, [Car(C1), Car(C2), Wheels(W1), Wheels(W2), Engine(E2), Engine(E2), NotHas(C1, E1), NotHas(C2, E2), NotHas(C1, W1), NotHas(C2, W2), NotInspected(C1), NotInspected(C2)], ())
Exiting: do_action with value: <logic.FolKB object at 0x7f68904508d0>

Calling: do_action with args: (HLA(AddWheels2), [[HLA(AddEngine1), HLA(AddWheels1), HLA(Inspect1)], [HLA(AddEngine2), HLA(AddWheels2), HLA(Inspect2)]], {'EngineHoists': 1, 'WheelStations': 2, 'Inspectors': 2, 'LugNuts': 500}, [Car(C1), Car(C2), Wheels(W1), Wheels(W2), Engine(E2), Engine(E2), NotHas(C1, E1), NotHas(C1, W1), NotHas(C2, W2), NotInspected(C1), NotInspected(C2), Has(C2, E2)], ())
Exiting: do_action with value: <logic.FolKB object at 0x7f6890450ad0>

Calling: do_action with args: (HLA(Inspect2), [[HLA(AddEngine1), HLA(AddWheels1), HLA(Inspect1)], [HLA(AddEngine2), HLA(AddWheels2), HLA(Inspect2)]], {'EngineHoists': 1, 'WheelStations': 2, 'Inspectors': 2, 'LugNuts': 480}, [Car(C1), Car(C2), Wheels(W1), Wheels(W2), Engine(E2), Engine(E2), NotHas(C1, E1), NotHas(C1, W1), NotInspected(C1), NotInspected(C2), Has(C2, E2), Has(C2, W2)], ())
Exiting: do_action with value: <logic.FolKB object at 0x7f689048ecd0>

Calling: do_action with args: (HLA(AddEngine1), [[HLA(AddEngine1), HLA(AddWheels1), HLA(Inspect1)], [HLA(AddEngine2), HLA(AddWheels2), HLA(Inspect2)]], {'EngineHoists': 1, 'WheelStations': 2, 'Inspectors': 2, 'LugNuts': 480}, [Car(C1), Car(C2), Wheels(W1), Wheels(W2), Engine(E2), Engine(E2), NotHas(C1, E1), NotHas(C1, W1), NotInspected(C1), Has(C2, E2), Has(C2, W2), Inspected(C2)], ())
Exiting: do_action with value: <logic.FolKB object at 0x7f6894286210>

Calling: do_action with args: (HLA(AddWheels1), [[HLA(AddEngine1), HLA(AddWheels1), HLA(Inspect1)], [HLA(AddEngine2), HLA(AddWheels2), HLA(Inspect2)]], {'EngineHoists': 1, 'WheelStations': 2, 'Inspectors': 2, 'LugNuts': 480}, [Car(C1), Car(C2), Wheels(W1), Wheels(W2), Engine(E2), Engine(E2), NotHas(C1, W1), NotInspected(C1), Has(C2, E2), Has(C2, W2), Inspected(C2), Has(C1, E1)], ())
Exiting: do_action with value: <logic.FolKB object at 0x7f68e062e310>

Calling: do_action with args: (HLA(Inspect1), [[HLA(AddEngine1), HLA(AddWheels1), HLA(Inspect1)], [HLA(AddEngine2), HLA(AddWheels2), HLA(Inspect2)]], {'EngineHoists': 1, 'WheelStations': 2, 'Inspectors': 2, 'LugNuts': 460}, [Car(C1), Car(C2), Wheels(W1), Wheels(W2), Engine(E2), Engine(E2), NotInspected(C1), Has(C2, E2), Has(C2, W2), Inspected(C2), Has(C1, E1), Has(C1, W1)], ())
Exiting: do_action with value: <logic.FolKB object at 0x7f68e062e350>

The above output is due to the debug decorator. If you remove debug, there is no output.

In [17]:
print(jobShopProblem.goal_test())
True

This is a valid solution and one of many correct ways to solve this problem.

Double tennis problem

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. A correct plan is one that, if executed by the actors, achieves the goal. 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.
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. The ball can be returned only if a player is in the right place. Each action must include the actor as an argument.
Let's first look at the definition of the double_tennis_problem in the module.

In [2]:
psource(double_tennis_problem)

def double_tennis_problem():
    """
    [Figure 11.10] DOUBLE-TENNIS-PROBLEM

    A multiagent planning problem involving two partner tennis players
    trying to return an approaching ball and repositioning around in the court.

    Example:
    >>> from planning import *
    >>> dtp = double_tennis_problem()
    >>> goal_test(dtp.goals, dtp.init)
    False
    >>> dtp.act(expr('Go(A, RightBaseLine, LeftBaseLine)'))
    >>> dtp.act(expr('Hit(A, Ball, RightBaseLine)'))
    >>> goal_test(dtp.goals, dtp.init)
    False
    >>> dtp.act(expr('Go(A, LeftNet, RightBaseLine)'))
    >>> goal_test(dtp.goals, dtp.init)
    True
    >>>
    """

    return PlanningProblem(init='At(A, LeftBaseLine) & At(B, RightNet) & Approaching(Ball, RightBaseLine) & Partner(A, B) & Partner(B, A)',
                             goals='Returned(Ball) & At(A, LeftNet) & At(A, RightNet)',
                             actions=[Action('Hit(actor, Ball, loc)',
                                             precond='Approaching(Ball, loc) & At(actor, loc)',
                                             effect='Returned(Ball)'),
                                      Action('Go(actor, to, loc)', 
                                             precond='At(actor, loc)',
                                             effect='At(actor, to) & ~At(actor, loc)')])

The states of this problem are:

Approaching(Ball, loc): The Ball is approaching the location loc.

Returned(Ball): One of the actors successfully hit the approaching ball from the correct location which caused it to return to the other side.

At(actor, loc): actor is at location loc.

~At(actor, loc): actor is not at location loc.

Let's now define an object of double_tennis_problem.

In [11]:
doubleTennisProblem = double_tennis_problem()

Before taking any actions, we will check if doubleTennisProblem has reached the goal.

In [12]:
print(doubleTennisProblem.goal_test())
False

As we can see, the goal hasn't been reached. We now define a possible solution that can help us reach the goal of having the ball returned. The actions will then be carried out on the doubleTennisProblem object.

The actions available to us are the following:

Hit(actor, ball, loc): returns an approaching ball if actor is present at the loc that the ball is approaching.

Go(actor, to, loc): moves an actor from location loc to location to.

We notice something different in this problem though, which is quite unlike any other problem we have seen so far. The goal state of the problem contains a variable a. This happens sometimes in multiagent planning problems 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.

In [13]:
solution = [expr('Go(A, RightBaseLine, LeftBaseLine)'),
            expr('Hit(A, Ball, RightBaseLine)'),
            expr('Go(A, LeftNet, RightBaseLine)')]

for action in solution:
    doubleTennisProblem.act(action)
In [14]:
doubleTennisProblem.goal_test()
Out[14]:
False

Oops! It did not work! There is a bug in the code. Find the bug and report it in polleverywhere.

In [8]:
dir(doubleTennisProblem)
Out[8]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'act',
 'actions',
 'convert',
 'goal_test',
 'goals',
 'init']
In [5]:
doubleTennisProblem.goals
Out[5]:
[Returned(Ball), At(A, LeftNet), At(A, RightNet)]
In [9]:
doubleTennisProblem.init
Out[9]:
[At(B, RightNet),
 Approaching(Ball, RightBaseLine),
 Partner(A, B),
 Partner(B, A),
 NotAt(A, LeftBaseLine),
 Returned(Ball),
 At(A, LeftNet),
 NotAt(A, RightBaseLine)]
In [ ]: