def split_operator(self, op, number): """Split an ADL operator into a list of operators.""" new_op_list = [] pair_precond_effect = [] additionals = [] formula = op.effects for item in formula.andList: if isinstance(item, FormulaWhen): formula_condition = item.condition formula_statement = item.formula pair_precond_effect.append([ FormulaAnd([op.preconditions, formula_condition]), formula_statement, ]) else: additionals.append(item) k = 1 for j in range(len(pair_precond_effect)): pair_precond_effect[j][k] = FormulaAnd( [pair_precond_effect[j][k]] + additionals) for u in range(number): new_op = Action( op.name + "-" + str(u), op.parameters, pair_precond_effect[u][0], pair_precond_effect[u][1], ) new_op_list.append(new_op) return new_op_list
def test_domain_operators(self): """Test that the domain operators are correct.""" op1 = Action( name="move-car", parameters=[ Term.variable("?from", "location"), Term.variable("?to", "location"), ], preconditions=FormulaAnd([ Literal.positive(Predicate("vehicleat", ["?from"])), Literal.positive(Predicate("road", ["?from", "?to"])), Literal.positive(Predicate("not-flattire")), ]), effects=FormulaAnd([ FormulaOneOf([ FormulaAnd([ Literal.positive(Predicate("vehicleat", ["?to"])), Literal.negative(Predicate("vehicleat", ["?from"])), ]), FormulaAnd([ Literal.positive(Predicate("vehicleat", ["?to"])), Literal.negative(Predicate("vehicleat", ["?from"])), Literal.negative(Predicate("not-flattire")), ]), ]), ]), ) op2 = Action( name="changetire", parameters=[ Term.variable("?loc", "location"), ], preconditions=FormulaAnd([ Literal.positive(Predicate("spare-in", ["?loc"])), Literal.positive(Predicate("vehicleat", ["?loc"])), ]), effects=FormulaAnd([ Literal.negative(Predicate("spare-in", ["?loc"])), Literal.positive(Predicate("not-flattire")), ]), ) assert self.pddl_operators == [ op1, op2, ]
def modify_operator(self, op_copy, condition, formula): """Modify operator.""" if isinstance(op_copy.preconditions, FormulaAnd): op_copy.preconditions.andList.append(condition) new_preconditions = op_copy.preconditions.andList new_effects = formula else: new_preconditions = FormulaAnd([op_copy.preconditions, condition]) new_effects = formula new_op = Action(op_copy.name, op_copy.parameters, new_preconditions, new_effects) return new_op
def create_operators_trans(self, domain_predicates, grounded_symbols): """Create operators corresponding to the automaton.""" new_operators = [] my_predicates = [symbol.name for symbol in grounded_symbols] (parameters, obj_mapping) = self.compute_parameters( domain_predicates, grounded_symbols ) vars_mapping = self.compute_varsMapping(grounded_symbols, obj_mapping) my_variables = [param.name for param in parameters] counter = 0 for destination, source_action in self.trans_by_dest.items(): if source_action: fluents_list_precond = self.compute_preconditions( source_action, vars_mapping, my_predicates, my_variables ) if isinstance(fluents_list_precond, FormulaAnd): new_precondition = fluents_list_precond else: new_precondition = FormulaAnd( [fluents_list_precond] + [Literal.negative(Predicate("turnDomain"))] ) new_effects = self.compute_effects(destination, my_variables) new_operators.append( Action( "trans-" + str(counter), parameters, new_precondition, new_effects, ) ) counter += 1 else: pass return new_operators, parameters
def p_action_def(self, p): """action_def : LPAREN ACTION_KEY NAME parameters_def precond_def effects_def RPAREN""" p[0] = Action(p[3], p[4], p[5], p[6])
def test_automaton_create_trans_op_2(self): """Test that the created trans operator is correct.""" pddl_domain = self.parser( open( str( Path( TEST_ROOT_DIR, "data", "pddl-domains", "blocksworld-ipc08", "domain.pddl", ))).read()) grounded_symbols = [ Symbol("emptyhand"), Symbol("on", ["b", "e"]), Symbol("ontable", ["e"]), ] actual_trans_ops, actual_params = self.aut_obj.create_operators_trans( pddl_domain.predicates, grounded_symbols) expected_trans_ops = [ Action( name="trans-0", parameters=[ Term.variable("?b1-00", "block"), Term.variable("?b2-01", "block"), ], preconditions=FormulaAnd([ FormulaOr([ FormulaAnd([ Literal.positive( Predicate( "q1", ["?b1-00", "?b2-01"], )), Literal.negative(Predicate("emptyhand")), ]), FormulaAnd([ Literal.positive( Predicate( "q1", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate("emptyhand")), Literal.negative( Predicate( "on", ["?b1-00", "?b2-01"], )), ]), FormulaAnd([ Literal.positive( Predicate( "q1", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate("emptyhand")), Literal.positive( Predicate( "on", ["?b1-00", "?b2-01"], )), Literal.negative(Predicate( "ontable", ["?b2-01"], )), ]), FormulaAnd([ Literal.positive( Predicate( "q2", ["?b1-00", "?b2-01"], )), Literal.negative(Predicate("emptyhand")), ]), FormulaAnd([ Literal.positive( Predicate( "q2", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate("emptyhand")), Literal.negative( Predicate( "on", ["?b1-00", "?b2-01"], )), ]), FormulaAnd([ Literal.positive( Predicate( "q2", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate("emptyhand")), Literal.positive( Predicate( "on", ["?b1-00", "?b2-01"], )), Literal.negative(Predicate( "ontable", ["?b2-01"], )), ]), ]), Literal.negative(Predicate("turnDomain")), ]), effects=FormulaAnd([ Literal.positive(Predicate( "q2", ["?b1-00", "?b2-01"], )), Literal.negative(Predicate( "q1", ["?b1-00", "?b2-01"], )), Literal.negative(Predicate( "q3", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate("turnDomain")), ]), ), Action( name="trans-1", parameters=[ Term.variable("?b1-00", "block"), Term.variable("?b2-01", "block"), ], preconditions=FormulaAnd([ FormulaOr([ FormulaAnd([ Literal.positive( Predicate( "q1", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate("emptyhand")), Literal.positive( Predicate( "on", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate( "ontable", ["?b2-01"], )), ]), FormulaAnd([ Literal.positive( Predicate( "q2", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate("emptyhand")), Literal.positive( Predicate( "on", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate( "ontable", ["?b2-01"], )), ]), Literal.positive( Predicate( "q3", ["?b1-00", "?b2-01"], )), ]), Literal.negative(Predicate("turnDomain")), ]), effects=FormulaAnd([ Literal.positive(Predicate( "q3", ["?b1-00", "?b2-01"], )), Literal.negative(Predicate( "q1", ["?b1-00", "?b2-01"], )), Literal.negative(Predicate( "q2", ["?b1-00", "?b2-01"], )), Literal.positive(Predicate("turnDomain")), ]), ), ] expected_params = [ Term.variable("?b1-00", "block"), Term.variable("?b2-01", "block"), ] assert actual_trans_ops == expected_trans_ops assert actual_params == expected_params
def test_automaton_create_trans_op(self): """Test that the created trans operator is correct.""" pddl_domain = self.parser( open( str( Path( TEST_ROOT_DIR, "data", "pddl-domains", "triangle-tireworld", "domain.pddl", ))).read()) grounded_symbols = [Symbol("vehicleat", ["l31"])] actual_trans_ops, actual_params = self.aut_obj.create_operators_trans( pddl_domain.predicates, grounded_symbols) expected_trans_ops = [ Action( name="trans-0", parameters=[Term.variable("?loc-00", "location")], preconditions=FormulaAnd([ FormulaOr([ FormulaAnd([ Literal.positive(Predicate("q1", ["?loc-00"])), Literal.negative( Predicate("vehicleat", ["?loc-00"])), ]), FormulaAnd([ Literal.positive(Predicate("q2", ["?loc-00"])), Literal.negative( Predicate("vehicleat", ["?loc-00"])), ]), ]), Literal.negative(Predicate("turnDomain")), ]), effects=FormulaAnd([ Literal.positive(Predicate("q2", ["?loc-00"])), Literal.negative(Predicate("q1", ["?loc-00"])), Literal.negative(Predicate("q3", ["?loc-00"])), Literal.positive(Predicate("turnDomain")), ]), ), Action( name="trans-1", parameters=[Term.variable("?loc-00", "location")], preconditions=FormulaAnd([ FormulaOr([ FormulaAnd([ Literal.positive(Predicate("q1", ["?loc-00"])), Literal.positive( Predicate("vehicleat", ["?loc-00"])), ]), FormulaAnd([ Literal.positive(Predicate("q2", ["?loc-00"])), Literal.positive( Predicate("vehicleat", ["?loc-00"])), ]), Literal.positive(Predicate("q3", ["?loc-00"])), ]), Literal.negative(Predicate("turnDomain")), ]), effects=FormulaAnd([ Literal.positive(Predicate("q3", ["?loc-00"])), Literal.negative(Predicate("q1", ["?loc-00"])), Literal.negative(Predicate("q2", ["?loc-00"])), Literal.positive(Predicate("turnDomain")), ]), ), ] expected_params = [Term.variable("?loc-00", "location")] assert actual_trans_ops == expected_trans_ops assert actual_params == expected_params
def test_domain_operators(self): """Test that the domain operators are correct.""" op1 = Action( name="walk-on-beam", parameters=[ Term.variable("?from", "location"), Term.variable("?to", "location"), ], preconditions=FormulaAnd([ Literal.negative(Predicate("broken-leg")), Literal.positive(Predicate("up")), Literal.positive(Predicate("position", ["?from"])), Literal.positive(Predicate("next-fwd", ["?from", "?to"])), ]), effects=FormulaOneOf([ FormulaAnd([ Literal.positive(Predicate("position", ["?to"])), Literal.negative(Predicate("position", ["?from"])), ]), FormulaAnd([ Literal.negative(Predicate("up")), Literal.positive(Predicate("position", ["?to"])), Literal.negative(Predicate("position", ["?from"])), ]), ]), ) op2 = Action( name="walk-left", parameters=[ Term.variable("?from", "location"), Term.variable("?to", "location"), ], preconditions=FormulaAnd([ Literal.negative(Predicate("broken-leg")), Literal.negative(Predicate("up")), Literal.positive(Predicate("position", ["?from"])), Literal.positive(Predicate("next-bwd", ["?from", "?to"])), ]), effects=FormulaAnd([ Literal.positive(Predicate("position", ["?to"])), Literal.negative(Predicate("position", ["?from"])), ]), ) op3 = Action( name="walk-right", parameters=[ Term.variable("?from", "location"), Term.variable("?to", "location"), ], preconditions=FormulaAnd([ Literal.negative(Predicate("broken-leg")), Literal.negative(Predicate("up")), Literal.positive(Predicate("position", ["?from"])), Literal.positive(Predicate("next-fwd", ["?from", "?to"])), ]), effects=FormulaAnd([ Literal.positive(Predicate("position", ["?to"])), Literal.negative(Predicate("position", ["?from"])), ]), ) op4 = Action( name="climb", parameters=[Term.variable("?p", "location")], preconditions=FormulaAnd([ Literal.negative(Predicate("broken-leg")), Literal.negative(Predicate("up")), Literal.positive(Predicate("position", ["?p"])), Literal.positive(Predicate("ladder-at", ["?p"])), ]), effects=FormulaAnd([Literal.positive(Predicate("up"))]), ) op5 = Action( name="climb-down", parameters=[], preconditions=FormulaAnd([ Literal.negative(Predicate("broken-leg")), Literal.positive(Predicate("up")), ]), effects=FormulaAnd([Literal.negative(Predicate("up"))]), ) op6 = Action( name="jump-over", parameters=[ Term.variable("?from", "location"), Term.variable("?middle", "location"), Term.variable("?to", "location"), ], preconditions=FormulaAnd([ Literal.negative(Predicate("broken-leg")), Literal.positive(Predicate("up")), Literal.positive(Predicate("position", ["?from"])), Literal.positive(Predicate( "next-fwd", ["?from", "?middle"], )), Literal.positive(Predicate("next-fwd", ["?middle", "?to"])), ]), effects=FormulaOneOf([ FormulaAnd([ Literal.negative(Predicate("up")), Literal.positive(Predicate("broken-leg")), ]), FormulaAnd([ Literal.negative(Predicate("up")), Literal.positive(Predicate("broken-leg")), Literal.positive(Predicate("position", ["?middle"])), Literal.negative(Predicate("position", ["?from"])), ]), FormulaAnd([ Literal.negative(Predicate("up")), Literal.positive(Predicate("position", ["?middle"])), Literal.negative(Predicate("position", ["?from"])), ]), FormulaAnd([ Literal.negative(Predicate("up")), Literal.positive(Predicate("broken-leg")), Literal.positive(Predicate("position", ["?to"])), Literal.negative(Predicate("position", ["?from"])), ]), FormulaAnd([ Literal.negative(Predicate("up")), Literal.positive(Predicate("position", ["?to"])), Literal.negative(Predicate("position", ["?from"])), ]), FormulaAnd([ Literal.positive(Predicate("position", ["?to"])), Literal.negative(Predicate("position", ["?from"])), ]), ]), ) assert self.pddl_operators == [op1, op2, op3, op4, op5, op6]