Ejemplo n.º 1
0
    def test_robot_locations_connected(self):
        problem = self.problems['robot_locations_connected'].problem

        gro = TransformersGrounder(problem)
        grounded_problem = gro.get_rewritten_problem()
        self.assertEqual(len(grounded_problem.actions), 28)
        for a in grounded_problem.actions:
            self.assertEqual(len(a.parameters), 0)
        for i, a in enumerate(problem.actions):
            if i == 0:
                self.assertEqual(len(gro.get_transformed_actions(a)), 8)
            elif i == 1:
                self.assertEqual(len(gro.get_transformed_actions(a)), 20)
            else:
                self.assertTrue(False)

        with OneshotPlanner(problem_kind=grounded_problem.kind) as planner:
            self.assertNotEqual(planner, None)
            grounded_plan = planner.solve(grounded_problem).plan
            plan = gro.rewrite_back_plan(grounded_plan)
            for ai in plan.actions:
                a = ai.action
                self.assertEqual(a, problem.action(a.name))
            with PlanValidator(problem_kind=problem.kind) as pv:
                self.assertTrue(pv.validate(problem, plan))
Ejemplo n.º 2
0
    def test_timed_connected_locations(self):
        problem = self.problems['timed_connected_locations'].problem

        gro = TransformersGrounder(problem)
        grounded_problem = gro.get_rewritten_problem()
        self.assertEqual(len(grounded_problem.actions), 20)
        for a in grounded_problem.actions:
            self.assertEqual(len(a.parameters), 0)
        for a in problem.actions:
            self.assertEqual(len(gro.get_transformed_actions(a)), 20)
Ejemplo n.º 3
0
    def test_basic(self):
        problem = self.problems['basic'].problem

        gro = TransformersGrounder(problem)
        with self.assertRaises(UPUsageError) as e:
            gro.get_rewrite_back_map()
        self.assertIn('The get_rewrite_back_map method must be called after the function get_rewritten_problem!', str(e.exception))

        grounded_problem = gro.get_rewritten_problem()
        grounded_problem_2 = gro.get_rewritten_problem()
        self.assertEqual(grounded_problem, grounded_problem_2)
        grounded_problem.name = problem.name
        self.assertEqual(grounded_problem, problem)
Ejemplo n.º 4
0
    def test_robot(self):
        problem = self.problems['robot'].problem

        gro = TransformersGrounder(problem)
        grounded_problem = gro.get_rewritten_problem()
        self.assertEqual(len(grounded_problem.actions), 2)
        for a in grounded_problem.actions:
            self.assertEqual(len(a.parameters), 0)

        with OneshotPlanner(problem_kind=grounded_problem.kind) as planner:
            self.assertNotEqual(planner, None)
            grounded_plan = planner.solve(grounded_problem).plan
            plan = gro.rewrite_back_plan(grounded_plan)
            for ai in plan.actions:
                a = ai.action
                self.assertEqual(a, problem.action(a.name))
            with PlanValidator(problem_kind=problem.kind) as pv:
                self.assertTrue(pv.validate(problem, plan))
Ejemplo n.º 5
0
 def ground(self,
            problem: 'unified_planning.model.Problem') -> GroundingResult:
     tarski_problem = unified_planning.interop.convert_problem_to_tarski(
         problem)
     actions = None
     try:
         lpgs = LPGroundingStrategy(tarski_problem)
         actions = lpgs.ground_actions()
     except tarski.grounding.errors.ReachabilityLPUnsolvable:
         raise unified_planning.exceptions.UPUsageError(
             'tarski grounder can not find a solvable grounding.')
     grounded_actions_map: Dict[Action, List[Tuple[FNode, ...]]] = {}
     fluents = {fluent.name: fluent for fluent in problem.fluents}
     objects = {object.name: object for object in problem.all_objects}
     types: Dict[str, Optional['unified_planning.model.Type']] = {}
     if not problem.has_type('object'):
         types[
             'object'] = None  # we set object as None, so when it is the father of a type in tarski, in UP it will be None.
     for action_name, list_of_tuple_of_parameters in actions.items():
         action = problem.action(action_name)
         parameters = {
             parameter.name: parameter
             for parameter in action.parameters
         }
         grounded_actions_map[action] = []
         for tuple_of_parameters in list_of_tuple_of_parameters:
             temp_list_of_converted_parameters = []
             for p in tuple_of_parameters:
                 if isinstance(p, str):
                     temp_list_of_converted_parameters.append(
                         problem.env.expression_manager.ObjectExp(
                             problem.object(p)))
                 else:
                     temp_list_of_converted_parameters.append(convert_tarski_formula(problem.env, fluents, \
                         objects, parameters, types, p))
             grounded_actions_map[action].append(
                 tuple(temp_list_of_converted_parameters))
     unified_planning_grounder = Grounder(
         problem, grounding_actions_map=grounded_actions_map)
     grounded_problem = unified_planning_grounder.get_rewritten_problem()
     trace_back_map = unified_planning_grounder.get_rewrite_back_map()
     return GroundingResult(
         grounded_problem, partial(lift_action_instance,
                                   map=trace_back_map), self.name, [])
Ejemplo n.º 6
0
    def test_matchcellar_grounder_from_factory(self):
        problem = self.problems['matchcellar'].problem

        gro = TransformersGrounder(problem)
        grounded_problem_test = gro.get_rewritten_problem()
        with Grounder(name='up_grounder') as grounder:
            self.assertTrue(grounder.supports(problem.kind))
            ground_result = grounder.ground(problem)
            grounded_problem_try, rewrite_back_plan_function = ground_result.problem, ground_result.lift_action_instance
            self.assertEqual(grounded_problem_test, grounded_problem_try)
            with OneshotPlanner(problem_kind=grounded_problem_try.kind) as planner:
                self.assertNotEqual(planner, None)
                grounded_plan = planner.solve(grounded_problem_try).plan
                plan = grounded_plan.replace_action_instances(rewrite_back_plan_function)
                for _, ai, _ in plan.actions:
                    a = ai.action
                    self.assertEqual(a, problem.action(a.name))
                with PlanValidator(problem_kind=problem.kind) as pv:
                    self.assertTrue(pv.validate(problem, plan))
Ejemplo n.º 7
0
 def test_ad_hoc_1(self):
     problem = Problem('ad_hoc')
     Location = UserType('Location')
     visited = Fluent('at', BoolType(), position=Location)
     l1 = Object('l1', Location)
     visit = InstantaneousAction('visit', l_to=Location)
     l_to = visit.parameter('l_to')
     visit.add_effect(visited(l_to), True)
     visit_l1 = InstantaneousAction('visit_l1')
     visit_l1.add_effect(visited(l1), True)
     problem.add_fluent(visited)
     problem.set_initial_value(visited(l1), True)
     problem.add_object(l1)
     problem.add_action(visit)
     problem.add_action(visit_l1)
     gro = TransformersGrounder(problem)
     grounded_problem = gro.get_rewritten_problem()
     self.assertEqual(len(grounded_problem.actions), 2)
     for a in grounded_problem.actions:
         self.assertEqual(len(a.parameters), 0)
     for a in problem.actions:
         self.assertEqual(len(gro.get_transformed_actions(a)), 1)
Ejemplo n.º 8
0
 def test_ad_hoc_2(self):
     problem = Problem('ad_hoc')
     gro = TransformersGrounder(problem)
     gro.get_rewritten_problem()
     with self.assertRaises(AttributeError):
         gro.rewrite_back_plan(problem)