Exemple #1
0
def recover_axioms_plans(instantiated, action_instances):
    #axioms, axiom_init, _ = axiom_rules.handle_axioms(
    #    instantiated.actions, instantiated.axioms, instantiated.goal_list)

    new_action_instances = [
        copy.deepcopy(instance) for instance in action_instances
    ]

    axioms, axiom_init = instantiated.axioms, [
    ]  # TODO: bug when needing to reachieve negated
    axioms_from_effect = defaultdict(list)
    for axiom in axioms:
        axioms_from_effect[axiom.effect].append(axiom)
    axioms_from_name = get_derived_predicates(instantiated.task.axioms)

    state = set(instantiated.task.init) | set(axiom_init)
    axiom_plans = []
    for action in new_action_instances + [
            get_goal_instance(instantiated.task.goal)
    ]:
        all_conditions = list(get_precondition(action)) + list(
            flatten(cond
                    for cond, _ in action.add_effects + action.del_effects))
        axioms = backtrack_axioms(all_conditions, axioms_from_effect, set())
        axiom_from_atom, _ = get_achieving_axioms(state, axioms)

        action.applied_effects = []
        for effects in [action.add_effects, action.del_effects]:
            negate = (effects is action.del_effects)
            for i, (conditions, effect) in reversed(list(enumerate(effects))):
                if all(
                        literal_holds(state, literal) or (
                            literal in axiom_from_atom)
                        for literal in conditions):
                    action.precondition.extend(conditions)
                    effects[i] = ([], effect)
                    action.applied_effects.append(
                        effect.negate() if negate else effect)
                else:
                    effects.pop(i)

        # RuntimeError: Preimage fact ('new-axiom@0',) is not achievable!
        #precondition = action.precondition # TODO: strange bug if this applies
        precondition = [
            literal for literal in action.precondition
            if literal.predicate in axioms_from_name
        ]
        axiom_plans.append([])
        success = extract_axioms(state, axiom_from_atom, precondition,
                                 axiom_plans[-1])
        if not success:
            print(all_conditions)
            print(action)
            print(axioms)
            raise RuntimeError('Could not extract axioms')
        apply_action(state, action)
    return new_action_instances, axiom_plans
Exemple #2
0
def reinstantiate_action_instances(task, old_instances, **kwargs):
    # Recomputes the instances with without any pruned preconditions
    state = set(task.init)
    new_instances = []
    for old_instance in old_instances:
        # TODO: better way of instantiating conditional effects (when not fluent)
        new_instance = reinstantiate_action(state, old_instance, **kwargs)
        assert (new_instance is not None)
        new_instances.append(new_instance)
        apply_action(state, new_instance)
    new_instances.append(get_goal_instance(task.goal))  # TODO: move this?
    return new_instances
Exemple #3
0
def recover_axioms_plans2(instantiated, action_instances):
    #import axiom_rules
    #with Verbose(False):
    #    normalized_axioms, axiom_init, axiom_layer_dict = axiom_rules.handle_axioms(
    #        [], instantiated.axioms, instantiated.goal_list)
    #state = set(instantiated.task.init + axiom_init)
    normalized_axioms = instantiated.axioms  # TODO: ignoring negated because cannot reinstantiate correctly
    state = set(instantiated.task.init)
    fluents = get_fluents(state, action_instances)

    unprocessed_from_atom = defaultdict(list)
    fluents_from_axiom = {}
    remaining_from_axiom = {}
    for axiom in normalized_axioms:
        fluent_conditions = []
        for literal in axiom.condition:
            if literal.positive() in fluents:
                fluent_conditions.append(literal)
            elif not literal_holds(state, literal):
                fluent_conditions = None
                break
        if fluent_conditions is None:
            continue
        for literal in fluent_conditions:
            unprocessed_from_atom[literal].append(axiom)
        fluents_from_axiom[id(axiom)] = len(fluent_conditions)
        remaining_from_axiom[id(axiom)] = fluents_from_axiom[id(axiom)]
    static_axioms = [
        axiom for axiom, num in fluents_from_axiom.items() if num == 0
    ]

    axiom_plans = []
    for action in action_instances + [
            get_goal_instance(instantiated.task.goal)
    ]:
        axiom_from_atom = mark_iteration(state, unprocessed_from_atom,
                                         fluents_from_axiom,
                                         remaining_from_axiom, static_axioms)
        preimage = []
        for literal in action.precondition:
            if not literal_holds(state, literal):
                preimage.append(literal)
                assert literal in axiom_from_atom
        for cond, eff in (action.add_effects + action.del_effects):
            # TODO: add conditional effects that must hold here
            assert not cond
        axiom_plans.append([])
        assert extract_axioms(axiom_from_atom, preimage, axiom_plans[-1])
        apply_action(state, action)
    return axiom_plans
Exemple #4
0
def recover_axioms_plans(instantiated, action_instances):
    #axioms, axiom_init, _ = axiom_rules.handle_axioms(
    #    instantiated.actions, instantiated.axioms, instantiated.goal_list)
    axioms, axiom_init = instantiated.axioms, [
    ]  # TODO: bug when needing to reachieve negated
    axioms_from_effect = defaultdict(list)
    for axiom in axioms:
        axioms_from_effect[axiom.effect].append(axiom)

    state = set(instantiated.task.init) | set(axiom_init)
    axiom_plans = []
    for action in action_instances + [
            get_goal_instance(instantiated.task.goal)
    ]:
        all_conditions = list(get_precondition(action)) + list(
            flatten(cond
                    for cond, _ in action.add_effects + action.del_effects))
        axioms = backtrack_axioms(all_conditions, axioms_from_effect, set())
        axiom_from_atom, _ = get_achieving_axioms(state, axioms)
        action.applied_effects = []
        for effects in [action.add_effects, action.del_effects]:
            negate = effects is action.del_effects
            for i, (conditions, effect) in reversed(list(enumerate(effects))):
                if all(
                        literal_holds(state, literal) or (
                            literal in axiom_from_atom)
                        for literal in conditions):
                    action.precondition.extend(conditions)
                    effects[i] = ([], effect)
                    action.applied_effects.append(
                        effect.negate() if negate else effect)
                else:
                    effects.pop(i)
        axiom_plans.append([])
        assert extract_axioms(state, axiom_from_atom, action.precondition,
                              axiom_plans[-1])
        apply_action(state, action)
    return axiom_plans