Ejemplo n.º 1
0
 def domain(self):
     if self._domain is None:
         #self._domain = substitute_expression(self.external.domain, self.get_mapping())
         self._domain = tuple(
             substitute_fact(atom, self.get_mapping())
             for atom in self.external.domain)
     return self._domain
Ejemplo n.º 2
0
 def remap_inputs(self, bindings):
     # TODO: speed this procedure up
     #if not any(o in bindings for o in self.instance.get_objects()):
     #    return self
     input_objects = apply_mapping(self.input_objects, bindings)
     fluent_facts = [substitute_fact(f, bindings) for f in self.instance.fluent_facts]
     new_instance = self.external.get_instance(input_objects, fluent_facts=fluent_facts)
     new_instance.opt_index = self.instance.opt_index
     return self.__class__(new_instance, self.output_objects, self.opt_index,
                           self.call_index, self.list_index, self.optimistic)
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
    ]