示例#1
0
def solve_restart(problem, max_time=INF, max_restarts=0, iteration_time=INF, abort=True, **kwargs):
    # TODO: iteratively lower the cost bound
    # TODO: a sequence of different planner configurations
    # TODO: reset objects and/or streams
    if (max_restarts >= 1) and (iteration_time == INF):
        iteration_time = min(2 * 60, iteration_time)
    assert (max_restarts == 0) or (iteration_time != INF)

    assert max_restarts >= 0
    start_time = time.time()
    for attempt in irange(1+max_restarts):
        iteration_start_time = time.time()
        if elapsed_time(start_time) > max_time:
            break
        if attempt >= 1:
            print(SEPARATOR)
        #solution = planner_fn(problem) # Or include the problem in the lambda
        remaining_time = min(iteration_time, max_time-elapsed_time(start_time))
        solution = solve(problem, max_time=remaining_time, **kwargs)
        plan, cost, certificate = solution
        if is_plan(plan): # TODO: INFEASIBLE
            return solution
        if abort and (elapsed_time(iteration_start_time) < remaining_time):
            break # TODO: return the cause of failure

    certificate = Certificate(all_facts=[], preimage_facts=[]) # TODO: aggregate
    return Solution(None, INF, certificate)
示例#2
0
def revert_solution(plan, cost, evaluations):
    all_facts = list(map(value_from_evaluation, evaluations))
    if isinstance(plan, OptPlan):
        action_plan = transform_plan_args(plan.action_plan, param_from_object)
        preimage_facts = list(
            map(value_from_obj_expression, plan.preimage_facts))
    else:
        action_plan = transform_plan_args(plan, param_from_object)
        preimage_facts = None
    certificate = Certificate(all_facts, preimage_facts)
    return Solution(action_plan, cost, certificate)
示例#3
0
def solve_next_goal(initial_problem, serialize=True, **kwargs):
    domain_pddl, constant_map, stream_pddl, stream_map, init, goal_parts = initial_problem
    # TODO: store serialization state to ensure progress is made
    # goal_formula = And(Or(*task_parts), *reset_parts) # TODO: still possibly having the disjunctive goal issue
    indices = list(range(0, len(goal_parts),
                         1)) if serialize else [len(goal_parts)]
    for i in indices:
        goal_parts = goal_parts[:i + 1]
        goal_formula = And(*goal_parts)
        problem = PDDLProblem(domain_pddl, constant_map, stream_pddl,
                              stream_map, init, goal_formula)
        print('Goal {}: {}'.format(i, goal_formula))

        # TODO: reset streams?
        solution = solve_restart(problem, **kwargs)
        # TODO: detect which goals were achieved
        plan, _, _ = solution
        if plan is None:
            return solution
        if (i == len(indices) - 1) or (len(plan) >= 1):
            return solution
    return Solution(plan=[], cost=0, certificate=[])
示例#4
0
def revert_solution(obj_plan, cost, evaluations):
    plan = value_from_obj_plan(obj_plan)
    init = list(
        map(value_from_obj_expression, map(fact_from_evaluation, evaluations)))
    return Solution(plan, cost, init)
示例#5
0
def solve_serialized(initial_problem,
                     stream_info={},
                     unit_costs=False,
                     unit_efforts=False,
                     verbose=True,
                     retain_facts=True,
                     **kwargs):
    # TODO: be careful of CanMove deadends
    domain_pddl, constant_map, stream_pddl, stream_map, init, goal = initial_problem
    _, _, domain, streams = parse_problem(initial_problem,
                                          stream_info,
                                          constraints=None,
                                          unit_costs=unit_costs,
                                          unit_efforts=unit_efforts)
    static_init, _ = partition_facts(
        domain, init)  # might not be able to reprove static_int
    #global_all, global_preimage = [], []
    global_plan = []
    global_cost = 0
    state = list(init)
    goals = serialize_goal(goal)
    # TODO: instead just track how the true init updates
    for i in range(len(goals)):
        # TODO: option in algorithms to pass in existing facts
        for stream in streams:
            stream.reset()
        goal = And(*goals[:i + 1])
        print('Goal:', str_from_object(goal))
        # No strict need to reuse streams because generator functions
        #local_problem = PDDLProblem(domain_pddl, constant_map, stream_pddl, stream_map, state, goal)
        local_problem = PDDLProblem(domain_pddl, constant_map, streams, None,
                                    state, goal)
        with Verbose(verbose):
            solution = solve_focused(local_problem,
                                     stream_info=stream_info,
                                     unit_costs=unit_costs,
                                     unit_efforts=unit_efforts,
                                     verbose=True,
                                     **kwargs)
        print_solution(solution)
        local_plan, local_cost, local_certificate = solution
        if local_plan is None:
            # TODO: replan upon failure
            global_certificate = Certificate(all_facts={}, preimage_facts=None)
            return Solution(None, INF, global_certificate)

        if retain_facts:
            state = local_certificate.all_facts
        else:
            _, fluent_facts = partition_facts(domain, state)
            state = static_init + fluent_facts + local_certificate.preimage_facts  # TODO: include functions
        #print('State:', state)
        # TODO: indicate when each fact is used
        # TODO: record failed facts
        global_plan.extend(
            local_plan)  # TODO: compute preimage of the executed plan
        global_cost += local_cost

        static_state, _ = partition_facts(domain, state)
        #global_all.extend(partition_facts(domain, local_certificate.all_facts)[0])
        #global_preimage.extend(static_state)
        print('Static:', static_state)
        state = apply_actions(domain, state, local_plan, unit_costs=unit_costs)
        print(SEPARATOR)
        #user_input('Continue?')
        # TODO: could also just test the goal here
        # TODO: constrain future plan skeletons

    global_certificate = Certificate(all_facts={}, preimage_facts=None)
    return global_plan, global_cost, global_certificate