def task_from_domain_problem(domain, problem): # TODO: prune evaluation that aren't needed in actions #domain_name, domain_requirements, types, type_dict, constants, \ # predicates, predicate_dict, functions, actions, axioms = domain task_name, task_domain_name, task_requirements, objects, init, goal, use_metric, problem_pddl = problem assert domain.name == task_domain_name requirements = pddl.Requirements( sorted( set(domain.requirements.requirements + task_requirements.requirements))) objects = domain.constants + objects check_for_duplicates( [o.name for o in objects], errmsg="error: duplicate object %r", finalmsg="please check :constants and :objects definitions") init.extend(pddl.Atom(EQ, (obj.name, obj.name)) for obj in objects) # TODO: optimistically evaluate (not (= ?o1 ?o2)) for fd_obj in objects: obj = obj_from_pddl(fd_obj.name) if obj.is_unique(): init.append(pddl.Atom(IDENTICAL, (fd_obj.name, fd_obj.name))) else: assert obj.is_shared() task = pddl.Task(domain.name, task_name, requirements, domain.types, objects, domain.predicates, domain.functions, init, goal, domain.actions, domain.axioms, use_metric) normalize.normalize(task) # task.add_axiom return task
def test(state): # TODO: action parameters don't depend on the state # TODO: counter for how many times the action has been remapped if not hasattr(action_instance.action, 'attachments'): return True for literal, stream in action_instance.action.attachments.items(): param_from_inp = remap_certified(literal, stream) input_objects = tuple( obj_from_pddl(action_instance.var_mapping[param_from_inp[inp]]) for inp in stream.inputs) stream_instance = get_fluent_instance( stream, input_objects, state) # Output automatically cached results = stream_instance.first_results(num=1) #results = stream_instance.all_results() failure = not results if literal.negated != failure: return False #args = action_instance.name.strip('()').split(' ') #idx_from_param = {p.name: i for i, p in enumerate(action_instance.action.parameters)} param_from_out = remap_certified(literal, stream) result = results[0] # Arbitrary out_from_obj = invert_dict(result.get_mapping()) for obj in result.output_objects: param = param_from_out[out_from_obj[obj]] action_instance.var_mapping[param] = obj.pddl # idx = idx_from_param[param] # args[1+idx] = obj.pddl #action_instance.name = '({})'.format(' '.join(args)) return True
def test(state): if state in fd_action_from_state: return True #new_instance = action_instance new_instance = copy.deepcopy(action_instance) if not hasattr(action_instance.action, 'attachments'): fd_action_from_state[state] = new_instance return True for literal, stream in new_instance.action.attachments.items(): param_from_inp = remap_certified(literal, stream) input_objects = tuple( obj_from_pddl(new_instance.var_mapping[param_from_inp[inp]]) for inp in stream.inputs) stream_instance = get_fluent_instance( stream, input_objects, state) # Output automatically cached results = stream_instance.first_results(num=1) #results = stream_instance.all_results() failure = not results if literal.negated != failure: return False #args = action_instance.name.strip('()').split(' ') #idx_from_param = {p.name: i for i, p in enumerate(action_instance.action.parameters)} param_from_out = remap_certified(literal, stream) result = results[0] # Arbitrary out_from_obj = invert_dict(result.mapping) for obj in result.output_objects: param = param_from_out[out_from_obj[obj]] new_instance.var_mapping[param] = obj.pddl # idx = idx_from_param[param] # args[1+idx] = obj.pddl #action_instance.name = '({})'.format(' '.join(args)) fd_action_from_state[state] = new_instance return True
def get_identical_atoms(objects): # TODO: optimistically evaluate (not (= ?o1 ?o2)) init = [] for fd_obj in objects: obj = obj_from_pddl(fd_obj.name) if obj.is_unique(): init.append(pddl.Atom(IDENTICAL, (fd_obj.name, fd_obj.name))) else: assert obj.is_shared() return init
def extract_function_results(results_from_head, action, pddl_args): import pddl if (action.cost is None) or not isinstance( action.cost.expression, pddl.PrimitiveNumericExpression): return [] var_mapping = {p.name: a for p, a in zip(action.parameters, pddl_args)} pddl_args = tuple( obj_from_pddl(var_mapping[p]) for p in action.cost.expression.args) head = Head(action.cost.expression.symbol, pddl_args) [(value, stream_result)] = results_from_head[head] if stream_result is None: return [] return [stream_result]
def extract_function_results(results_from_head, action, pddl_args): import pddl if action.cost is None: return None expression = action.cost.expression if not isinstance(expression, pddl.PrimitiveNumericExpression): return None var_mapping = {p.name: a for p, a in zip(action.parameters, pddl_args)} obj_args = tuple( obj_from_pddl(var_mapping[p] if is_parameter(p) else p) for p in expression.args) head = Head(expression.symbol, obj_args) [(_, result)] = results_from_head[head] if result is None: return None return result
def order_fn(state, goal, operators): from strips.utils import ha_applicable actions = ha_applicable(state, goal, operators) # filters axioms action_priorities = {} for action in actions: name, args = parse_action(action.fd_action.name) args = [obj_from_pddl(arg).value for arg in args] if name == 'print': _, _, element, _, _ = args priority = -compute_z_distance(node_points, element) elif name == 'move': _, loc1, loc2 = args priority = 0. # TODO: use the location of the best element here else: raise NotImplementedError(name) action_priorities[action] = priority return sorted(actions, key=action_priorities.__getitem__)
def extract_function_result(results_from_head, action, pddl_args): import pddl if action.cost is None: return None # TODO: retrieve constant action costs # TODO: associate costs with the steps they are applied expression = action.cost.expression if not isinstance(expression, pddl.PrimitiveNumericExpression): return None var_mapping = {p.name: a for p, a in zip(action.parameters, pddl_args)} obj_args = tuple( obj_from_pddl(var_mapping[p] if is_parameter(p) else p) for p in expression.args) head = Head(expression.symbol, obj_args) [result] = results_from_head[head] if result is None: return None return result