Esempio n. 1
0
def solve_pyplanners(instantiated):
    # https://github.mit.edu/caelan/stripstream/blob/c8c6cd1d6bd5e2e8e31cd5603e28a8e0d7bb2cdc/stripstream/algorithms/search/pyplanners.py
    import sys
    pyplanners_path = get_pyplanners_path()
    if pyplanners_path is None:
        raise RuntimeError(
            'Must clone https://github.com/caelan/pyplanners '
            'and set the environment variable {} to its path'.format(
                PYPLANNERS_VAR))

    if pyplanners_path not in sys.path:
        sys.path.append(pyplanners_path)

    # TODO: could operate on translated SAS instead
    from strips.states import State, PartialState
    from strips.operators import Action, Axiom
    from strips.utils import default_derived_plan
    import pddl

    if instantiated is None:
        return None, INF

    py_actions = []
    for action in instantiated.actions:
        py_action = Action({'fd_action': action})
        py_action.conditions = set(action.precondition)
        py_action.effects = set()
        for condition, effect in action.del_effects:
            assert not condition
            py_action.effects.add(effect.negate())
        for condition, effect in action.add_effects:
            assert not condition
            py_action.effects.add(effect)
        py_action.cost = action.cost
        py_action.test = get_attachment_test(action)
        py_actions.append(py_action)

    py_axioms = []
    for axiom in instantiated.axioms:
        py_axiom = Axiom({'fd_axiom_id':
                          id(axiom)})  # Not hashable for some reason
        py_axiom.conditions = set(axiom.condition)
        py_axiom.effects = {axiom.effect}
        py_axioms.append(py_axiom)

    goal = PartialState(instantiated.goal_list)
    fluents = {f.positive() for f in goal.conditions}
    for py_operator in py_actions + py_axioms:
        fluents.update(f.positive() for f in py_operator.conditions)

    initial = State(atom for atom in instantiated.task.init
                    if isinstance(atom, pddl.Atom) and (atom in fluents))

    plan, state_space = default_derived_plan(initial, goal, py_actions,
                                             py_axioms)
    if plan is None:
        return None, INF
    actions = [pddl_from_instance(action.fd_action) for action in plan]
    return actions, plan.cost
Esempio n. 2
0
def solve_pyplanners(instantiated,
                     planner=None,
                     max_planner_time=DEFAULT_MAX_TIME,
                     max_cost=INF):
    if instantiated is None:
        return None, INF

    # https://github.mit.edu/caelan/stripstream/blob/c8c6cd1d6bd5e2e8e31cd5603e28a8e0d7bb2cdc/stripstream/algorithms/search/pyplanners.py
    pyplanners_path = get_pyplanners_path()
    if pyplanners_path is None:
        raise RuntimeError(
            'Must clone https://github.com/caelan/pyplanners '
            'and set the environment variable {} to its path'.format(
                PYPLANNERS_VAR))
    if pyplanners_path not in sys.path:
        sys.path.append(pyplanners_path)

    # TODO: could operate on translated SAS instead
    from strips.states import State, PartialState
    from strips.operators import Action, Axiom
    from strips.utils import solve_strips, default_derived_plan
    import pddl

    default_planner = {
        'search': 'eager',
        'evaluator': 'greedy',
        'heuristic': 'ff',
        'successors': 'all',
    }
    if isinstance(planner, dict):
        default_planner.update(planner)

    py_actions = []
    for action in instantiated.actions:
        #action.dump()
        py_action = Action({'fd_action': action})
        py_action.conditions = set(action.precondition)
        py_action.effects = set()
        for condition, effect in action.del_effects:
            assert not condition
            py_action.effects.add(effect.negate())
        for condition, effect in action.add_effects:
            assert not condition
            py_action.effects.add(effect)
        py_action.cost = action.cost
        py_action.test = get_attachment_test(action)
        py_actions.append(py_action)

    py_axioms = []
    for axiom in instantiated.axioms:
        #axiom.dump()
        py_axiom = Axiom({'fd_axiom_id':
                          id(axiom)})  # Not hashable for some reason
        py_axiom.conditions = set(axiom.condition)
        py_axiom.effects = {axiom.effect}
        py_axioms.append(py_axiom)

    goal = PartialState(instantiated.goal_list)
    fluents = {f.positive() for f in goal.conditions}
    for py_operator in py_actions + py_axioms:
        fluents.update(f.positive() for f in py_operator.conditions)

    initial = State(atom for atom in instantiated.task.init
                    if isinstance(atom, pddl.Atom) and (atom in fluents))

    plan, state_space = solve_strips(initial,
                                     goal,
                                     py_actions,
                                     py_axioms,
                                     max_time=max_planner_time,
                                     max_cost=max_cost,
                                     **default_planner)
    if plan is None:
        return None, INF
    actions = [pddl_from_instance(action.fd_action) for action in plan]
    #print(actions)
    return actions, plan.cost