Пример #1
0
def simultaneous_stream_plan(evaluations, goal_expression, domain, stream_results, negated, unit_costs=True, **kwargs):
    if negated:
        raise NotImplementedError()
    function_evaluations = {e: None for e in evaluations}
    for result in stream_results:
        if isinstance(result, FunctionResult):
            for fact in result.get_certified():
                function_evaluations[evaluation_from_fact(fact)] = result
    new_domain, stream_result_from_name = add_stream_actions(domain, stream_results)
    combined_plan, _ = solve_finite(function_evaluations, goal_expression, new_domain,
                                                unit_costs=unit_costs, **kwargs)
    if combined_plan is None:
        return None, None, INF # TODO: return plan cost
    stream_plan = []
    action_plan = []
    for name, args in combined_plan:
        if name in stream_result_from_name:
            stream_plan.append(stream_result_from_name[name])
        else:
            action_plan.append((name, args))

    action_cost = len(action_plan)
    function_plan = set()
    if not unit_costs:
        action_cost = 0
        results_from_head = get_results_from_head(function_evaluations)
        for name, args in action_plan:
            action = find(lambda a: a.name == name, domain.actions)
            pddl_args = tuple(map(pddl_from_object, args))
            function_plan.update(extract_function_results(results_from_head, action, pddl_args))
            action_cost += get_cost(domain, results_from_head, name, args)
    return (stream_plan + list(function_plan)), action_plan, action_cost
Пример #2
0
def exhaustive_stream_plan(evaluations, goal_expression, domain, stream_results, **kwargs):
    if stream_results:
        return stream_results, None, INF
    plan, cost = solve_finite(evaluations, goal_expression, domain, **kwargs)
    if plan is None:
        return None, plan, cost
    return [], plan, cost
Пример #3
0
def incremental_stream_plan(evaluations, goal_expression, domain, stream_results, **kwargs):
    stream_plan = evaluate_functions(evaluations, stream_results)
    plan, cost = solve_finite(evaluations, goal_expression, domain, **kwargs)
    if plan is not None:
        return [], plan, cost
    if stream_plan:
        return stream_plan, plan, cost
    return None, plan, cost
Пример #4
0
def solve_current(problem, **search_kwargs):
    """
    Solves a PDDLStream problem without applying any streams
    Will fail if the problem requires stream applications
    :param problem: a PDDLStream problem
    :param search_kwargs: keyword args for the search subroutine
    :return: a tuple (plan, cost, evaluations) where plan is a sequence of actions
        (or None), cost is the cost of the plan, and evaluations is init but expanded
        using stream applications
    """
    evaluations, goal_expression, domain, stream_name, externals = parse_problem(
        problem)
    compile_to_exogenous(evaluations, domain, externals)
    plan, cost = solve_finite(evaluations, goal_expression, domain,
                              **search_kwargs)
    return revert_solution(plan, cost, evaluations)
Пример #5
0
def solve_exhaustive(problem, max_time=300, verbose=True, **search_kwargs):
    """
    Solves a PDDLStream problem by applying all possible streams and searching once
    Requires a finite max_time when infinitely many stream instances
    :param problem: a PDDLStream problem
    :param max_time: the maximum amount of time to apply streams
    :param verbose: if True, this prints the result of each stream application
    :param search_kwargs: keyword args for the search subroutine
    :return: a tuple (plan, cost, evaluations) where plan is a sequence of actions
        (or None), cost is the cost of the plan, and evaluations is init but expanded
        using stream applications
    """
    start_time = time.time()
    evaluations, goal_expression, domain, stream_name, externals = parse_problem(
        problem)
    compile_to_exogenous(evaluations, domain, externals)
    instantiator = Instantiator(evaluations, externals)
    while instantiator.stream_queue and (elapsed_time(start_time) < max_time):
        process_stream_queue(instantiator, evaluations, verbose=verbose)
    plan, cost = solve_finite(evaluations, goal_expression, domain,
                              **search_kwargs)
    return revert_solution(plan, cost, evaluations)
Пример #6
0
def solve_incremental(problem,
                      max_time=INF,
                      max_cost=INF,
                      layers=1,
                      verbose=True,
                      **search_kwargs):
    """
    Solves a PDDLStream problem by alternating between applying all possible streams and searching
    :param problem: a PDDLStream problem
    :param max_time: the maximum amount of time to apply streams
    :param max_cost: a strict upper bound on plan cost
    :param layers: the number of stream application layers per iteration
    :param verbose: if True, this prints the result of each stream application
    :param search_kwargs: keyword args for the search subroutine
    :return: a tuple (plan, cost, evaluations) where plan is a sequence of actions
        (or None), cost is the cost of the plan, and evaluations is init but expanded
        using stream applications
    """
    store = SolutionStore(max_time, max_cost,
                          verbose)  # TODO: include other info here?
    evaluations, goal_expression, domain, _, externals = parse_problem(problem)
    compile_to_exogenous(evaluations, domain, externals)
    instantiator = Instantiator(evaluations, externals)
    num_iterations = 0
    while not store.is_terminated():
        num_iterations += 1
        print(
            'Iteration: {} | Evaluations: {} | Cost: {} | Time: {:.3f}'.format(
                num_iterations, len(evaluations), store.best_cost,
                store.elapsed_time()))
        function_process_stream_queue(instantiator, evaluations, store)
        plan, cost = solve_finite(evaluations, goal_expression, domain,
                                  **search_kwargs)
        store.add_plan(plan, cost)
        if not instantiator.stream_queue:
            break
        layered_process_stream_queue(instantiator, evaluations, store, layers)
    return revert_solution(store.best_plan, store.best_cost, evaluations)