def simultaneous_stream_plan(evaluations, goal_expression, domain, stream_results, negated, unit_costs=False, **kwargs): if negated: raise NotImplementedError(negated) for result in stream_results: if isinstance(result.external, Stream) and result.external.is_fluent(): raise NotImplementedError('Fluents are not supported') applied_streams, deferred_streams = partition_results( evaluations, stream_results, lambda r: False) opt_evaluations = apply_streams(evaluations, applied_streams) stream_domain, stream_result_from_name = add_stream_actions( domain, deferred_streams) if any(map(is_optimizer_result, stream_results)): goal_expression = augment_goal(stream_domain, goal_expression) combined_plan, _ = solve_finite(opt_evaluations, goal_expression, stream_domain, unit_costs=unit_costs, **kwargs) if combined_plan is None: return None, INF stream_plan, action_plan = partition_plan(combined_plan, stream_result_from_name) function_plan = extract_function_plan(opt_evaluations, action_plan, domain, unit_costs) cost = get_plan_cost(opt_evaluations, action_plan, domain, unit_costs) combined_plan = stream_plan + function_plan + action_plan return combined_plan, cost
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
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
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, externals = parse_problem(problem) plan, cost = solve_finite(evaluations, goal_expression, domain, **search_kwargs) return revert_solution(plan, cost, evaluations)
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
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, externals = parse_problem(problem) ensure_no_fluent_streams(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)
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) ensure_no_fluent_streams(externals) #load_stream_statistics(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) #write_stream_statistics(externals, verbose) return revert_solution(store.best_plan, store.best_cost, evaluations)