def __init__(self, raw_string): if raw_string is not None: super().__init__(raw_string) for child in self.children: tokens = re.split(r'\s', child) identifier = tokens[0] if identifier == "problem": self.name = tokens[1] elif identifier == ":domain": self.dom_name = tokens[1] elif identifier == ":objects": self.objects = child elif identifier == ":init": self.init_state = [ fluenttree.FluentTree(c) for c in PDDLPart.PDDLPart(child).children ] elif identifier == ":goal": self.goal = fluenttree.FluentTree( Utils.find_child(child)[0]) else: super().__init__('') self.name = '' self.dom_name = '' self.objects = '' self.init_state = [] self.goal = None
def get_compiled_actions_of_action(self, action): # Get relevant effects from within this action # possible_and_relevant_effects = self.possible_effects_of_action(action).intersection(self.relevant_effects) possible_and_relevant_effects = [ x for x in self.possible_effects_of_action(action) if x in self.relevant_effects ] possible_intentions = self.possible_intentions combos = itertools.product(possible_and_relevant_effects, possible_intentions) actions = [] for effect, intent in list(combos): effect_name = f"{'not-' if effect.is_not else ''}{'intends-' if effect.is_intention else ''}{'eq' if effect.identifier=='=' else effect.identifier}" act_name = f"{action.name}-for-{effect_name}-because-intends-{intent.identifier}" # parameters = action.parameters + [f"intent-param-{i}" for i in range(intent.arity)] parameters = copy.deepcopy(action.parameters) parameters.parameters += [ f"{i}-for-intent" for i in intent.parameters ] # [f"?intent-param-{i}" for i in range(intent.arity)] + parameters.types += intent.types preconditions = fluenttree.FluentTree("and ") preconditions.is_leaf = False preconditions.child_trees.append(action.precondition) # TODO: What about multi-agent actions??? preconditions.child_trees.append( fluenttree.FluentTree( f"(intends-{intent.identifier} {action.agents[0]} {' '.join([f'{i}-for-intent' for i in intent.parameters])})" )) # TODO: Delegate preconditions are complex effects = fluenttree.FluentTree("and ") effects.is_leaf = False effects.child_trees.append( action.effect ) # TODO: Modify effect to flatten (intends ?a (pred ?x ?y)) -> (intends-pred ?a ?x ?y) # TODO: (justified-prec-intends-intent ?prec-params ?actor ?intent-params) for all preconditions. # Tricky if preconditions not simple list, or are not-ed new_action = Operator.Operator(None) new_action.name = act_name new_action.parameters = parameters new_action.precondition = preconditions new_action.effect = effects actions.append(new_action) return actions
def generate_belief_action(original_action, suffix): """ Creates a new Operator object with all preconditions converted into belief preconditions. :param original_action: :param suffix: :return: """ dupe = Operator(None) dupe.name = original_action.name + "_" + suffix dupe.agents = original_action.agents dupe.parameters = original_action.parameters belief_sets = [] for agent in original_action.agents: copied_preconditions = deepcopy(original_action.precondition) copied_preconditions.child_trees = [ x for x in copied_preconditions.child_trees if not x.is_belief ] make_beleaves(copied_preconditions, agent) belief_sets.append(copied_preconditions) # dupe.precondition = convert_to_belief_string(original_action.precondition) dupe.precondition = fluenttree.FluentTree("and ") dupe.precondition.is_leaf = False if suffix == "success": dupe.precondition.child_trees.append( deepcopy(original_action.precondition)) dupe.effect = convert_effects_minimally(original_action.effect, original_action.agents) elif suffix == "fail": prec_not_met_tree = fluenttree.FluentTree("not ") prec_not_met_tree.is_leaf = False prec_not_met_tree.child_trees.append( deepcopy(original_action.precondition)) dupe.precondition.child_trees.append(prec_not_met_tree) dupe.effect = convert_effects_minimally(original_action.fail, original_action.agents) # THIS HAS TO COME AFTER THE REAL EFFECTS OR ELSE GLAIVE BREAKS dupe.precondition.child_trees += belief_sets dupe.precondition = super_simplify_formula(dupe.precondition) flatten_beliefs_with_not(dupe.precondition) dupe.precondition = super_simplify_formula(dupe.precondition) dupe.effect = super_simplify_formula(dupe.effect) flatten_beliefs_with_not(dupe.effect) dupe.effect = super_simplify_formula(dupe.effect) # simplify_formula(dupe.precondition) return dupe
def __init__(self, to_parse): if to_parse is not None: super().__init__(to_parse) self.base_string = to_parse self.name = re.split(r'\s', self.base_string)[1] tokens = Utils.get_colon_sections(self.base_string) self.agents = [] self.parameters = [] self.precondition = None self.effect = None self.fail = None for token in tokens: title = token.split()[0] if title == ":action": self.name = token.split()[1] # self.name = fluenttree.first_word(token) elif title == ":parameters": # self.parameters = Parameter.parameter_list(Utils.find_child(token)[0]) self.parameters = fluenttree.AbstractPredicate( "X " + Utils.find_child(token)[0]) self.parameters.identifier = "" elif title == ":precondition": self.precondition = fluenttree.FluentTree( Utils.find_child(token)[0]) elif title == ":effect": self.effect = fluenttree.FluentTree( Utils.find_child(token)[0]) elif title == ":fail": self.fail = fluenttree.FluentTree( Utils.find_child(token)[0]) elif title == ":agents": self.agents = Utils.get_question_mark_sections( Utils.find_child(token)[0]) else: super().__init__("") self.base_string = "blank" self.name = "blank" self.agents = [] self.parameters = fluenttree.AbstractPredicate("") self.precondition = None self.effect = None self.fail = None
def flatten_beliefs_with_not(ft): if ft.is_belief: if len(ft.child_trees) > 0 and ft.child_trees[0].identifier == "not": leaf = fluenttree.AbstractPredicate( ft.child_trees[0].child_trees[0]) upper = fluenttree.AbstractPredicate(ft) ft.identifier = 'believes_not_' + leaf.identifier ft.words = [ft.identifier] + [upper.parameters[0] ] + leaf.parameters else: ft_pred = fluenttree.AbstractPredicate(ft) ft.identifier = "believes_" + ft_pred.identifier ft.words = [ft.identifier] + ft_pred.parameters ft.is_belief = False ft.child_trees = [] elif ft.is_leaf: return else: new_children = [] for c in ft.child_trees: if not c.is_belief: new_children.append(c) else: if len(c.child_trees ) > 0 and c.child_trees[0].identifier == "not": leaf = fluenttree.AbstractPredicate( c.child_trees[0].child_trees[0]) upper = fluenttree.AbstractPredicate(c) new_child = fluenttree.FluentTree( "believes_not_" + leaf.identifier + ' ' + ' '.join([upper.parameters[0]] + leaf.parameters), depth=c.depth) else: c_predicate_form = fluenttree.AbstractPredicate(c) new_child = fluenttree.FluentTree( "believes_" + c_predicate_form.identifier + ' ' + ' '.join(c_predicate_form.parameters), depth=c.depth) new_children.append(new_child) ft.child_trees = new_children for c in ft.child_trees: flatten_beliefs_with_not(c)
def convert_effects(ft, agent_list): res = fluenttree.FluentTree("and ") res.is_leaf = False believe_effect_sets = [] for agent in agent_list: believe_effects = deepcopy(ft) make_beleaves(believe_effects, agent) believe_effect_sets.append(believe_effects) flattened_effects = deepcopy(ft) flatten_beliefs(flattened_effects) res.child_trees += believe_effect_sets res.child_trees.append(flattened_effects) return res
def super_simplify_formula(ft, negated=False): ft = deepcopy(ft) if negated: # if ft.identifier == "not" and ft.child_trees[0].is_leaf: # return ft.child_trees[0] # Let nots cancel out if ft.identifier == "not": ft = super_simplify_formula(ft.child_trees[0], False) return cleanup(ft) if ft.is_leaf: # return ft new_not = fluenttree.FluentTree("not (x)") new_not.child_trees = [ft] return new_not # DeMorgans laws elif ft.identifier == "and": ft.identifier = "or" new_kids = [] for c in ft.child_trees: new_kids.append(super_simplify_formula(c, True)) ft.child_trees = new_kids elif ft.identifier == "or": ft.identifier = "and" new_kids = [] for c in ft.child_trees: new_kids.append(super_simplify_formula(c, True)) ft.child_trees = new_kids else: if ft.is_leaf: return ft if ft.identifier == "not": ft = super_simplify_formula(ft.child_trees[0], True) return cleanup(ft) else: new_kids = [] for c in ft.child_trees: new_kids.append(super_simplify_formula(c, False)) ft.child_trees = new_kids return cleanup(ft)
def flatten_beliefs(ft): if ft.is_leaf: return else: new_children = [] for c in ft.child_trees: if not c.is_belief: new_children.append(c) else: c_predicate_form = fluenttree.AbstractPredicate(c) new_child = fluenttree.FluentTree( "believes_" + c_predicate_form.identifier + ' ' + ' '.join(c_predicate_form.parameters)) new_children.append(new_child) ft.child_trees = new_children for c in ft.child_trees: flatten_beliefs(c)
def get_versions_of_expressioned_action(self, action): expression_indices = [ i for i, t in enumerate(action.parameters.types) if t.lower() == "expression" ] if len(expression_indices) == 0: return [action] else: versions = [] # Enumerate all the things the expression(s) could take possible_expressions = itertools.product( self.domain.predicates, repeat=len(expression_indices)) for instantiation in possible_expressions: parameters = copy.deepcopy(action.parameters) action_pre_string = action.precondition.to_string().strip( "() ") action_eff_string = action.effect.to_string().strip("() ") action_fail_string = action.fail.to_string().strip( "() ") if action.fail is not None else "" # For each expression parameter, replace the name/types in parameter list for expr_index, expression_inst in zip( reversed(expression_indices), instantiation): expression_name = action.parameters.parameters[expr_index] new_expr_params = [ f"{p}-for-{expression_name.strip('? ')}" for p in expression_inst.parameters ] parameters.parameters[expr_index:expr_index + 1] = new_expr_params parameters.types[expr_index:expr_index + 1] = expression_inst.types action_pre_string = action_pre_string.replace( " " + expression_name + " ", f" ({expression_inst.identifier} {' '.join(new_expr_params)}) " ) action_eff_string = action_eff_string.replace( " " + expression_name + " ", f" ({expression_inst.identifier} {' '.join(new_expr_params)}) " ) action_fail_string = action_fail_string.replace( " " + expression_name + " ", f" ({expression_inst.identifier} {' '.join(new_expr_params)}) " ) action_pre_string = action_pre_string.replace( expression_name + ")", f"({expression_inst.identifier} {' '.join(new_expr_params)}) )" ) action_eff_string = action_eff_string.replace( expression_name + ")", f"({expression_inst.identifier} {' '.join(new_expr_params)}) )" ) action_fail_string = action_fail_string.replace( expression_name + ")", f"({expression_inst.identifier} {' '.join(new_expr_params)}) )" ) new_pre = fluenttree.FluentTree(action_pre_string) new_eff = fluenttree.FluentTree(action_eff_string) new_fail = fluenttree.FluentTree( action_fail_string) if action.fail is not None else None new_action = Operator.Operator(None) new_action.parameters = parameters new_action.precondition = new_pre new_action.effect = new_eff new_action.fail = new_fail new_action.name = f"{action.name}-{'-'.join([inst.identifier for inst in instantiation])}" new_action.agents = action.agents versions.append(new_action) return versions