def instantiate(self, var_mapping, init_facts, fluent_facts, fluent_functions, init_function_vals, task, new_constant_axioms): # The comments for Action.instantiate apply accordingly. arg_list = [ var_mapping[conditions.Variable(par.name)].name for par in self.parameters ] name = "(%s %s)" % (self.name, " ".join(arg_list)) condition = [] try: self.condition.instantiate(var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_constant_axioms, condition) except conditions.Impossible: return None effect_args = [ var_mapping.get(conditions.Variable(arg.name), conditions.Variable(arg.name)) for arg in self.parameters ] effect = conditions.Atom(self.name, effect_args) return PropositionalAxiom(name, condition, effect)
def rename_variables(self, renamings): new_params = [] for param in self.parameters: new_var = renamings.get(conditions.Variable(param.name), conditions.Variable(param.name)) new_param = pddl_types.TypedObject(new_var.name, param.type) new_params.append(new_param) new_effects = [] for effect in self.effects: new_effect = effect.rename_variables(renamings) new_effects.append(new_effect) return self.__class__(self.name, new_params, self.type, self.modulecall, new_effects)
def instantiate(self, var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, objects_by_type): """Return a PropositionalAction which corresponds to the instantiation of this action with the arguments in var_mapping. Only fluent parts of the conditions (those in fluent_facts) are included. init_facts are evaluated whilte instantiating. Precondition and effect conditions must be normalized for this to work. Returns None if var_mapping does not correspond to a valid instantiation (because it has impossible preconditions or an empty effect list.)""" arg_list = [ var_mapping[conditions.Variable(par.name)].name for par in self.parameters ] name = "(%s %s)" % (self.name, " ".join(arg_list)) precondition = [] try: self.condition.instantiate(var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, precondition) except conditions.Impossible: return None effects = [] for eff in self.effects: eff.instantiate(var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, objects_by_type, effects) if effects: return PropositionalAction(name, precondition, effects) else: return None
def instantiate(self, var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, objects_by_type): """Return a PropositionalDurativeAction which corresponds to the instantiation of this action with the arguments in var_mapping. Only fluent parts of the conditions (those in fluent_facts) are included. init_facts are evaluated whilte instantiating. Precondition and effect conditions must be normalized for this to work. Returns None if var_mapping does not correspond to a valid instantiation (because it has impossible preconditions or an empty effect list.)""" arg_list = [ var_mapping[conditions.Variable(par.name)].name for par in self.parameters ] name = "(%s %s)" % (self.name, " ".join( arg_list[:self.orig_parameter_length])) try: inst_duration = [[(op, pne.instantiate(var_mapping, fluent_functions, init_function_vals, task, new_axiom)) for op, pne in self.duration[0]], [(op, pne.instantiate(var_mapping, fluent_functions, init_function_vals, task, new_axiom)) for op, pne in self.duration[1]]] except ValueError, e: print "dropped action %s" % name print "Error: %s" % e return None
def instantiate(self, var_mapping, fluent_functions, init_function_vals, task, new_axioms=[]): args = [ var_mapping.get(conditions.Variable(arg.name), arg) for arg in self.args ] pne = PrimitiveNumericExpression(self.symbol, args) # TODO check whether this PNE is fluent. Otherwise substitute it by the # corresponding constant if fluent_functions != None: if pne not in fluent_functions and not pne.symbol.startswith( "derived!"): if pne not in init_function_vals: raise ValueError( "Cannot instantiate non-fluent PNE: no initial value given %s" % pne) constant = init_function_vals[pne] new_axiom_predicate = task.function_administrator.get_derived_function( constant) new_axiom = task.function_administrator.functions[( constant.value, )] new_axiom = new_axiom.instantiate(var_mapping, fluent_functions, init_function_vals, task, new_axioms) new_axioms.add(new_axiom) return new_axiom_predicate return pne
def toModuleCall(self): mod_args = [] for param in self.parameters: if param.name.startswith("?"): mod_args.append(conditions.Variable(param.name)) else: mod_args.append(conditions.ObjectTerm(param.name)) return conditions.ModuleCall(self.name, mod_args)
def remove_duration_variable(self, action, time, duration, pnes): if time == 0: return duration else: name = "duration_" + action.name params = [conditions.Variable(param.name) for param in action.parameters] duration_function = PrimitiveNumericExpression(name, params) pnes.append(duration_function) return duration_function
def instantiate(self, var_mapping, new_modules): new_params = [] for param in self.parameters: new_var = var_mapping.get(conditions.Variable(param.name), conditions.Variable(param.name)) new_param = pddl_types.TypedObject(new_var.name, param.type) new_params.append(new_param) new_effects = [] for effect in self.effects: new_effect = effect.rename_variables(var_mapping) new_effects.append(new_effect) # ahh need to inst the effects too! # -- need vars as args # also for effects mc = Module(self.name, new_params, self.type, self.modulecall, new_effects, self) if mc not in new_modules: new_modules.add(mc)
def instantiate(self, var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, new_modules, objects_by_type): """Return a PropositionalDurativeAction which corresponds to the instantiation of this action with the arguments in var_mapping. Only fluent parts of the conditions (those in fluent_facts) are included. init_facts are evaluated whilte instantiating. Precondition and effect conditions must be normalized for this to work. Returns None if var_mapping does not correspond to a valid instantiation (because it has impossible preconditions or an empty effect list.)""" arg_list = [ var_mapping[conditions.Variable(par.name)].name for par in self.parameters ] name = "(%s %s)" % (self.name, " ".join( arg_list[:self.orig_parameter_length])) if self.grounding_call is not None: result = [] self.grounding_call.instantiate(var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, new_modules, result) inst_grounding_call = result[0] else: inst_grounding_call = None try: inst_duration = [[], []] for dura in (0, 1): for (op, pne) in self.duration[dura]: if isinstance(pne, conditions.ModuleCall): result = [] pne.instantiate(var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, new_modules, result) assert len( result ) == 1, "Something when wrong when instantiating cost module" inst_duration[dura].append((op, result[0])) else: inst_duration[dura].append( (op, pne.instantiate(var_mapping, fluent_functions, init_function_vals, task, new_axiom))) except ValueError, e: print "dropped action %s" % name print "Error: %s" % e return None
def instantiate(self, var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, objects_by_type, result): if self.parameters: var_mapping = var_mapping.copy() # Will modify this. object_lists = [objects_by_type.get(par.type, []) for par in self.parameters] for object_tuple in cartesian_product(*object_lists): for (par, obj) in zip(self.parameters, object_tuple): var_mapping[conditions.Variable(par.name)] = conditions.ObjectTerm(obj) self._instantiate(var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, result) else: self._instantiate(var_mapping, init_facts, fluent_facts, init_function_vals, fluent_functions, task, new_axiom, result)
def instantiate(self, var_mapping, fluent_functions, init_function_vals, task, new_constant_axioms): arg_list = [ var_mapping[conditions.Variable(par.name)] for par in self.parameters ] name = "(%s %s)" % (self.name, " ".join([arg.name for arg in arg_list])) parts = [] for part in self.parts: if isinstance(part, f_expression.NumericConstant): parts.append(part) else: parts.append( part.instantiate(var_mapping, fluent_functions, init_function_vals, task, new_constant_axioms)) effect = f_expression.PrimitiveNumericExpression(self.name, arg_list) return PropositionalNumericAxiom(name, self.op, parts, effect)
def get_default_variables(nr): return [conditions.Variable("?v%s" % varnr) for varnr in range(nr)]