def get_automaton_formula(self, vars_mapping, predicates_name, action): """Get the automaton formula.""" temp = [] i = 0 # print(predicates_name) # print(vars_mapping) for char in action: if char == "1": # if predicates_name[i] in [x.name for x in list(vars_mapping)]: temp.append( Literal.positive( Predicate( predicates_name[i], [x[0] for x in vars_mapping[list(vars_mapping)[i]]], ) ) ) # else: # temp.append(Literal.positive(Predicate(predicates_name[i]))) elif char == "0": # if predicates_name[i] in vars_mapping.keys(): temp.append( Literal.negative( Predicate( predicates_name[i], [x[0] for x in vars_mapping[list(vars_mapping)[i]]], ) ) ) # else: # temp.append(Literal.negative(Predicate(predicates_name[i]))) else: pass i += 1 return temp
def p_literal(self, p): """literal : LPAREN NOT_KEY predicate RPAREN | predicate""" if len(p) == 2: p[0] = Literal.positive(p[1]) elif len(p) == 5: p[0] = Literal.negative(p[3])
def compute_preconditions( self, source_action, vars_mapping, predicates_name, variables ): """Compute new preconditions.""" if len(source_action) == 1: if ( self.get_automaton_formula( vars_mapping, predicates_name, source_action[0][1] ) == [] ): formula = Literal.positive( Predicate("q" + str(source_action[0][0]), variables) ) else: automaton_state = [ Literal.positive( Predicate("q" + str(source_action[0][0]), variables) ) ] formula = FormulaAnd( automaton_state + self.get_automaton_formula( vars_mapping, predicates_name, source_action[0][1] ) + [Literal.negative(Predicate("turnDomain"))] ) else: formula = FormulaOr( self.get_or_conditions( vars_mapping, predicates_name, variables, source_action ) ) return formula
def add_to_effect(self): """Modify action effects.""" if isinstance(self.effects, FormulaAnd): self.effects.complete_domain_turn(False) else: old_formula = self.effects effect_to_be_added = Literal.negative(Predicate("turnDomain")) self.effects = FormulaAnd([old_formula, effect_to_be_added])
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 compute_effects(self, destination, variables): """Compute new effects.""" negated_states = [] for state in sorted(self.states): if state != destination: negated_states.append( Literal.negative(Predicate("q" + str(state), variables)) ) else: pass automaton_destination = [ Literal.positive(Predicate("q" + str(destination), variables)) ] turnDomain = [Literal.positive(Predicate("turnDomain"))] formula = FormulaAnd(automaton_destination + negated_states + turnDomain) return formula
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 complete_domain_turn(self, flag): """Add domain turn predicate.""" if flag: self.andList.append(Literal.positive(Predicate("turnDomain"))) else: self.andList.append(Literal.negative(Predicate("turnDomain")))
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]