Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
def compile_fluents_as_attachments(domain, externals):
    import pddl
    state_streams = set(
        filter(lambda e: isinstance(e, Stream) and e.is_fluent(),
               externals))  # is_special
    if not state_streams:
        return externals

    predicate_map = get_predicate_map(state_streams)
    if predicate_map and (get_pyplanners_path() is None):
        # TODO: fluent streams with outputs
        # Could convert the free parameter to a constant
        raise NotImplementedError(
            'Algorithm does not support fluent streams: {}'.format(
                [stream.name for stream in state_streams]))

    domain.constants.append(make_object(PLACEHOLDER.pddl))
    for action in domain.actions:
        for effect in action.effects:
            # TODO: conditional effects
            if any(literal.predicate in predicate_map
                   for literal in get_literals(effect.condition)):
                raise ValueError(
                    'Attachments cannot be in action effects: {}'.format(
                        effect))
        action.attachments = {}
        preconditions = set()
        for literal in get_conjunctive_parts(action.precondition):
            #if not isinstance(literal, pddl.Literal):
            #    raise NotImplementedError('Only literals are supported: {}'.format(literal))
            if not get_predicates(literal) & set(predicate_map):
                preconditions.add(literal)
                continue
            if not isinstance(literal, pddl.Literal):
                raise NotImplementedError(literal)
            # Drops the original precondition
            stream = predicate_map[literal.predicate]
            mapping = remap_certified(literal, stream)
            assert mapping is not None
            action.attachments[literal] = stream
            preconditions.update(
                pddl.Atom(EQ, (mapping[out], PLACEHOLDER.pddl))
                for out in stream.outputs)
            preconditions.update(
                fd_from_fact(substitute_fact(fact, mapping))
                for fact in stream.domain)
        action.precondition = pddl.Conjunction(preconditions).simplified()
        #fn = lambda l: pddl.Truth() if l.predicate in predicate_map else l
        #action.precondition = replace_literals(fn, action.precondition).simplified()
        #action.dump()
    return [
        external for external in externals if external not in state_streams
    ]