def focused(problem, planner='ff-astar', max_planner_time=10, max_time=INF, max_cost=INF, effort_weight=1, single=False, reset_fn=revisit_reset_fn, verbose=False): start_time = time.time() num_epochs = 0 num_iterations = 0 if effort_weight is not None: initialize_effort_functions(problem) evaluations = infer_evaluations(problem.initial) disabled = deque() while (time.time() - start_time) < max_time: num_iterations += 1 print '\nEpoch: {} | Iteration: {} | Time: {:.3f}'.format(num_epochs, num_iterations, time.time() - start_time) evaluations = evaluate_eager(problem, evaluations) universe = Universe(problem, evaluations, use_bounds=True, only_eager=False) stream_from_head, bound_streams = bound_stream_instances(universe) if effort_weight is not None: add_effort_evaluations(evaluations, universe, bound_streams) mt = (max_time - (time.time() - start_time)) if disabled: mt = min(max_planner_time, mt) plan = solve_universe(universe, planner=planner, max_time=mt, max_cost=max_cost, verbose=verbose) print 'Length: {} | Cost: {}'.format(get_length(plan, universe.evaluations), get_cost(plan, universe.evaluations)) print 'Plan:', plan if plan is None: if not disabled: break reset_fn(disabled, evaluations) num_epochs += 1 continue evaluated = {e.head for e in evaluations} supporting_heads = required_heads(universe, plan) useful_streams = retrace_streams( stream_from_head, evaluated, supporting_heads) print useful_streams evaluable_streams = filter(lambda s: set( s.domain()) <= evaluations, useful_streams) if not evaluable_streams: return plan, evaluations if single: evaluable_streams = evaluable_streams[:1] for instance in evaluable_streams: if isinstance(instance, Head): evaluations.update(infer_evaluations([instance.get_eval()])) elif not instance.enumerated: evaluations.update(infer_evaluations(instance.next_atoms())) instance.disabled = True disabled.append(instance) return None, evaluations
def print_plan(plan, evaluations): if plan is None: print '\nFailed to find a plan' else: print '\nFound a plan' print 'Length:', get_length(plan, evaluations) print 'Cost:', get_cost(plan, evaluations) / SCALE_COST for i, (action, args) in enumerate(plan): print i, action, list(args)
def incremental(problem, max_time=INF, max_cost=INF, terminate_cost=INF, planner='ff-astar', max_planner_time=10, verbose=False, verbose_search=False): start_time = time.time() search_time = 0 num_iterations = 0 universe = Universe(problem, problem.initial, use_bounds=False, only_eager=False) best_plan = None best_cost = INF while (time.time() - start_time) < max_time: num_iterations += 1 elapsed_time = (time.time() - start_time) print 'Iteration: {} | Evaluations: {} | Cost: {} | ' 'Search time: {:.3f} | Total time: {:.3f}'.format( num_iterations, len(universe.evaluations), best_cost, search_time, elapsed_time) t0 = time.time() plan = solve_universe(universe, planner=planner, max_time=min(max_planner_time, (max_time - elapsed_time)), max_cost=min(best_cost, max_cost), verbose=verbose_search) search_time += (time.time() - t0) cost = get_cost(plan, universe.evaluations) if cost < best_cost: best_plan = plan best_cost = cost if (best_cost != INF) and (best_cost <= terminate_cost): break if not universe.stream_queue: break evaluate_stream_instances(universe, len(universe.stream_queue), start_time, max_time, verbose=verbose) return best_plan, universe.evaluations
def main(argv): testNum = int(argv[0]) if argv else 0 print 'Test number', testNum print 'Args = ', TEST_ARGS[testNum] problem = create_problem(verboseFns=False, **TEST_ARGS[testNum]) print problem plan, evaluations = incremental(problem, planner='ff-astar', max_time=5, terminate_cost=INF, verbose=False, verbose_search=False) if plan is None: print '\nFailed to find a plan' return print '\nFound a plan' print 'Length:', get_length(plan, evaluations) print 'Cost:', get_cost(plan, evaluations) / SCALE_COST for i, (action, args) in enumerate(plan): print i, action, list(args)
def sequence_focused(problem, max_time=INF, max_cost=INF, terminate_cost=INF, planner='ff-astar', waves=1, verbose=False): start_time = time.time() num_epochs = 1 num_iterations = 0 evaluations = infer_evaluations(problem.initial) disabled = deque() best_plan = None best_cost = INF search_time = 0 stream_time = 0 while (time.time() - start_time) < max_time: num_iterations += 1 print '\nEpoch: {} | Iteration: {} | Disabled: {} | Cost: {} | ' 'Search time: {:.3f} | Stream time: {:.3f} | Total time: {:.3f}'.format( num_epochs, num_iterations, len(disabled), best_cost, search_time, stream_time, time.time() - start_time) evaluations = evaluate_eager(problem, evaluations) universe = Universe(problem, evaluations, use_bounds=True, only_eager=False) if not all(f.eager for f in universe.defined_functions): raise NotImplementedError( 'Non-eager functions are not yet supported') bound_streams = bound_stream_instances(universe) t0 = time.time() plan = solve_universe(universe, planner=planner, max_time=(max_time - (time.time() - start_time)), max_cost=min(best_cost, max_cost), verbose=verbose) search_time += (time.time() - t0) cost = get_cost(plan, universe.evaluations) print 'Actions | Length: {} | Cost: {} | {}'.format( get_length(plan, universe.evaluations), cost, plan) t0 = time.time() streams = solve_streams(universe, evaluations, plan, bound_streams, start_time, max_time) stream_time += (time.time() - t0) print 'Streams | Length: {} | {}'.format(get_length(streams, None), streams) if streams: add_sequence(evaluations, disabled, StreamSequence(streams, plan, cost)) elif (streams is not None) and (cost < best_cost): best_plan = plan best_cost = cost prune_sequences(disabled, best_cost) if (best_cost < terminate_cost) or not disabled: break for _ in xrange(waves): evaluate_sequences(evaluations, disabled, INF) num_epochs += 1 return best_plan, evaluations
def plan_focused(problem, max_time=INF, max_cost=INF, terminate_cost=INF, effort_weight=1, planner='ff-astar', max_planner_time=10, reset_fn=revisit_reset_fn, bind=False, verbose=False, verbose_search=False, defer=False): start_time = time.time() num_iterations = 0 num_epochs = 1 if effort_weight is not None: problem.objective = TotalCost() for action in problem.actions: action.effects = action.effects + (Increase(TotalCost(), 1), ) evaluations = infer_evaluations(problem.initial) disabled = deque() stream_actions, stream_axioms = make_stream_operators( problem, effort_weight) stream_problem = Problem([], problem.goal, problem.actions + stream_actions, problem.axioms + stream_axioms, problem.streams, objective=TotalCost()) best_plan = None best_cost = INF while (time.time() - start_time) < max_time: num_iterations += 1 print '\nEpoch: {} | Iteration: {} | Disabled: {} | Cost: {} | ' 'Time: {:.3f}'.format( num_epochs, num_iterations, len(disabled), best_cost, time.time() - start_time) evaluations = evaluate_eager(problem, evaluations) universe = Universe(stream_problem, evaluations, use_bounds=True, only_eager=False) if not all(f.eager for f in universe.defined_functions): raise NotImplementedError( 'Non-eager functions are not yet supported') abstract_evals = bound_stream_instances(universe) universe.evaluations -= abstract_evals mt = (max_time - (time.time() - start_time)) if disabled: mt = min(max_planner_time, mt) combined_plan = solve_universe(universe, planner=planner, max_time=mt, max_cost=min(best_cost, max_cost), verbose=verbose_search) if combined_plan is None: if not disabled: break reset_fn(disabled, evaluations) num_epochs += 1 continue combined_plan = defer_streams( combined_plan) if defer else prioritize_streams(combined_plan) stream_plan, action_plan = detangle_plan(evaluations, combined_plan) print 'Length: {} | Cost: {} | Streams: {}'.format( get_length(combined_plan, universe.evaluations), get_cost(combined_plan, universe.evaluations), len(stream_plan)) print 'Actions:', action_plan print 'Streams:', stream_plan if not stream_plan: cost = get_cost(combined_plan, universe.evaluations) if cost < best_cost: best_plan = combined_plan best_cost = cost if (best_cost < terminate_cost) or not disabled: break reset_fn(disabled, evaluations) num_epochs += 1 continue negative_atoms = [] if bind: reattempt = multi_bind_call_streams(evaluations, disabled, stream_plan, negative_atoms) else: reattempt = call_streams(evaluations, disabled, stream_plan, negative_atoms) return best_plan, evaluations
def dual_focused(problem, max_time=INF, max_cost=INF, terminate_cost=INF, effort_weight=None, solve=False, defer=False, use_context=False, planner='ff-astar', max_planner_time=10, reset_fn=revisit_reset_fn, bind=False, revisit=False, verbose=False, verbose_search=False, **kwargs): start_time = time.time() num_epochs = 1 num_iterations = 0 if effort_weight is not None: initialize_effort_functions(problem) evaluations = infer_evaluations(problem.initial) disabled = deque() best_plan = None best_cost = INF search_time = 0 stream_time = 0 reattempt = True has_fluent_streams = len(problem.fluent_streams()) != 0 assert not has_fluent_streams if has_fluent_streams: initialize_fluent_streams(problem) while (time.time() - start_time) < max_time: num_iterations += 1 if verbose: print '\nEpoch: {} | Iteration: {} | Disabled: {} | Cost: {} | ' 'Search time: {:.3f} | Stream time: {:.3f} | Total time: {:.3f}'.format( num_epochs, num_iterations, len(disabled), best_cost, search_time, stream_time, time.time() - start_time) real_plan, evaluations = solve_eager( problem, evaluations, solve=(solve and reattempt), planner=planner, max_time=(max_time - (time.time() - start_time)), max_cost=min(best_cost, max_cost), verbose=verbose, **kwargs) reattempt = False real_cost = get_cost(real_plan, evaluations) if real_cost < best_cost: best_plan = real_plan best_cost = real_cost if best_cost < terminate_cost: break if has_fluent_streams: add_computed_evals(evaluations) universe = Universe(problem, evaluations, use_bounds=True, only_eager=False) if not all(f.eager for f in universe.defined_functions): raise NotImplementedError( 'Non-eager functions are not yet supported') bound_streams = bound_stream_instances(universe) if effort_weight is not None: add_effort_evaluations(evaluations, universe, bound_streams) if has_fluent_streams: add_fluent_streams(evaluations, bound_streams, universe) mt = (max_time - (time.time() - start_time)) if disabled: mt = min(max_planner_time, mt) t0 = time.time() opt_plan = solve_universe(universe, planner=planner, max_time=mt, max_cost=min(best_cost, max_cost), verbose=verbose_search, **kwargs) search_time += (time.time() - t0) if verbose: print 'Actions | Length: {} | Cost: {} | {}'.format( get_length(opt_plan, universe.evaluations), get_cost(opt_plan, universe.evaluations), opt_plan) if use_context: success, negative_atoms = evaluate_negative_atoms( universe, evaluations, opt_plan) if verbose: print 'External | Success: {} | {}'.format( success, negative_atoms) else: success, negative_atoms = True, set() t0 = time.time() solve_streams_fn = solve_streams_new if ( defer or has_fluent_streams) else solve_streams stream_plan, action_plan = solve_streams_fn(universe, evaluations, opt_plan, bound_streams, start_time, max_time, defer, **kwargs) stream_time += (time.time() - t0) if verbose: print 'Streams | Length: {} | {}'.format( get_length(stream_plan, []), stream_plan) if stream_plan: if revisit: isolated_reset_fn(disabled, evaluations) if bind: reattempt = multi_bind_call_streams(evaluations, disabled, stream_plan, negative_atoms, verbose=verbose) else: reattempt = call_streams(evaluations, disabled, stream_plan, negative_atoms, verbose=verbose) if verbose: print 'Reattempt:', reattempt continue cost = get_cost(action_plan, universe.evaluations) if success and (stream_plan is not None) and (cost < best_cost): best_plan = action_plan best_cost = cost if (best_cost < terminate_cost) or not disabled: break reset_fn(disabled, evaluations) num_epochs += 1 return best_plan, evaluations