def return_plan(v, universe):
    plan = []
    for action, args in get_plan(v):
        if not hasattr(action.original, 'is_internal'):
            plan.append(
                (action.original, args[:len(action.original.parameters)]))
    universe.print_statistics()
    if plan is not None and universe.verbose:
        print_plan_stats(plan, universe)
    return plan, universe
Ejemplo n.º 2
0
def simultaneous_strips(problem,
                        max_time=INF,
                        max_iterations=INF,
                        frequency=100,
                        verbose=False,
                        debug=False,
                        optimal=False):
    # TODO - prune any unnecessary cond_streams
    universe = Universe(problem,
                        use_ground=False,
                        make_action_instances=True,
                        verbose=verbose)
    stream_queue = deque()
    add_streams(universe, stream_queue)
    universe.root = Vertex(universe.initial_fluents())
    universe.vertices = {universe.root}
    universe.goal_vertices = set()
    universe.vertex_queue = deque()

    universe.state_map = defaultdict(list)
    universe.action_map = defaultdict(list)
    for vertex in universe.vertices:
        for a in universe.name_to_action.values():
            for key in get_vertex_keys(vertex, a, universe):
                universe.state_map[key].append(vertex)

    for _ in irange(0, max_iterations):
        print_helper(universe)
        if debug: debug_helper(universe)
        universe.iterations += 1

        stream_atoms = universe.initial_stream_atoms(
        )  # TODO - could always augment the state with the static atoms or just ignore them
        new_axioms = filter(lambda op: isinstance(op.lifted, Axiom),
                            universe.new_instances)
        new_actions = filter(lambda op: isinstance(op.lifted, Action),
                             universe.new_instances)
        axioms = filter(lambda op: isinstance(op.lifted, Axiom),
                        universe.action_instances)
        actions = filter(lambda op: isinstance(op.lifted, Action),
                         universe.action_instances)
        #if verbose:
        print 'New Actions: %d | New Axioms: %s | Total Actions: %s | Total Axioms: %s\n' % (
            len(new_actions), len(new_axioms), len(actions), len(axioms))

        # TODO - recombine with simultaneous

        for instance in universe.new_instances:
            universe.action_map[get_operator_key(instance,
                                                 universe)].append(instance)

        t0 = time()
        success = False
        for instance in new_actions:
            for vertex in get_vertices(instance, universe):
                #print action.lifted.name, map(get_value, action.args), vertex
                #raw_input('Continue?')
                if apply_actions(vertex, [instance], stream_atoms, universe,
                                 optimal):
                    success = True
                    break
            if success: break
        if success: break

        for vertex in list(
                universe.vertices):  # Process new_axioms and actions
            if apply_axioms(vertex, new_axioms, stream_atoms, universe) and \
                apply_actions(vertex, actions, stream_atoms, universe, optimal):
                break
            #elif apply_actions(vertex, new_actions, stream_atoms, universe, optimal):
            #  break

        universe.new_instances = []  # NOTE - didn't have this before

        while universe.vertex_queue:  # Process new_vertices
            vertex = universe.vertex_queue.popleft()
            apply_axioms(vertex, axioms, stream_atoms, universe)
            #if apply_actions(vertex, actions, stream_atoms, universe, optimal):
            if apply_actions_strips(vertex, stream_atoms, universe, optimal):
                #if apply_actions(vertex, get_actions(vertex, universe), stream_atoms, universe, optimal):
                break
        universe.search_time += time() - t0

        if (not optimal and universe.goal_vertices) or not call_queue(
                stream_queue, universe, frequency, max_time):
            break  # TODO - call queue waves

    dijkstra(universe)
    best_v, best_plan = None, None
    if universe.goal_vertices:
        best_v = argmin(lambda v: v.cost, universe.goal_vertices)
        best_plan = get_plan(best_v)
    if verbose:
        universe.print_statistics()
        if best_plan is not None:
            print_plan_stats(best_plan, universe)
            print 'Cost:', best_v.cost
    return best_plan, universe
Ejemplo n.º 3
0
def simultaneous(problem,
                 max_time=INF,
                 max_iterations=INF,
                 frequency=100,
                 verbose=False,
                 debug=False,
                 optimal=False):
    # TODO - prune any unnecessary cond_streams
    universe = Universe(problem,
                        use_ground=False,
                        make_action_instances=True,
                        verbose=verbose)
    stream_queue = deque()
    add_streams(universe, stream_queue)
    universe.root = Vertex(universe.initial_fluents())
    universe.vertices = {universe.root}
    universe.goal_vertices = set()
    universe.vertex_queue = deque()

    for _ in irange(0, max_iterations):
        print_helper(universe)
        if debug: debug_helper(universe)

        universe.iterations += 1

        stream_atoms = universe.initial_stream_atoms(
        )  # TODO - could always augment the state with the static atoms or just ignore them
        new_axioms = filter(lambda op: isinstance(op.lifted, Axiom),
                            universe.new_instances)
        new_actions = filter(lambda op: isinstance(op.lifted, Action),
                             universe.new_instances)
        axioms = filter(lambda op: isinstance(op.lifted, Axiom),
                        universe.action_instances)
        actions = filter(lambda op: isinstance(op.lifted, Action),
                         universe.action_instances)
        #if verbose:
        print 'New Actions: %d | New Axioms: %s | Total Actions: %s | Total Axioms: %s\n' % (
            len(new_actions), len(new_axioms), len(actions), len(axioms))
        t0 = time()
        for vertex in list(
                universe.vertices):  # Process new_axioms and actions
            if apply_axioms(vertex, new_axioms, stream_atoms, universe) and \
                apply_actions(vertex, actions, stream_atoms, universe, optimal):
                break
            elif apply_actions(vertex, new_actions, stream_atoms, universe,
                               optimal):
                break

        universe.new_instances = []  # NOTE - didn't have this before

        while universe.vertex_queue:  # Process new_vertices
            vertex = universe.vertex_queue.popleft()
            apply_axioms(vertex, axioms, stream_atoms, universe)
            if apply_actions(vertex, actions, stream_atoms, universe, optimal):
                break
        universe.search_time += time() - t0

        if (not optimal and universe.goal_vertices) or not call_queue(
                stream_queue, universe, frequency, max_time):
            break  # TODO - call queue waves

    dijkstra(universe)
    best_v, best_plan = None, None
    if universe.goal_vertices:
        best_v = argmin(lambda v: v.cost, universe.goal_vertices)
        best_plan = get_plan(best_v)
    if verbose:
        universe.print_statistics()
        if best_plan is not None:
            print_plan_stats(best_plan, universe)
            print 'Cost:', best_v.cost
    return best_plan, universe
Ejemplo n.º 4
0
def incremental_planner(problem,
                        search=DEFAULT_SEARCH,
                        max_time=INF,
                        max_iterations=INF,
                        max_calls=INF,
                        waves=True,
                        frequency=1,
                        optimal=False,
                        verbose=False,
                        debug=False):
    """
    Incremental algorithm.

    :param problem: :class:`.STRIPStreamProblem`
    :param search: python function of the search subroutine
    :param max_time: numeric maximum planning time
    :param max_iterations: int maximum subroutine calls
    :param waves: boolean flag when ``True`` considers each stream evaluation iteration to be the current queue rather than a single stream
    :param frequency: numeric number of evaluation iterations given by ``waves`` (``float('inf')`` implies to evaluate all streams per iteration)
    :param optimal: boolean flag which saves the best current solution
    :param verbose: boolean flag which toggles the print output
    :param debug: boolean flag which prints the objects on each iteration
    :return: a sequence of :class:`.Action` and tuple of :class:`.Constant` pairs as well as :class:`.Universe`
    """

    universe = Universe(problem, use_ground=False, verbose=verbose)

    queue = deque()
    add_streams(universe, queue)
    best_plan, best_cost = None, INF
    for _ in irange(0, max_iterations):
        print 'Iteration: %d | Time: %s | Calls: %s | Search Time: %s | Solved: %s | Cost: %s' % (
            universe.iterations, round(time() - universe.start_time,
                                       3), universe.calls,
            round(universe.search_time, 3), best_plan is not None, best_cost)
        if debug:
            for type in universe.type_to_objects:
                print type, len(universe.type_to_objects[type]), map(
                    get_value, universe.type_to_objects[type])[:10]
            raw_input('Continue?\n')
        universe.iterations += 1
        t0 = time()
        plan = search(universe, max_time - (time() - universe.start_time),
                      best_cost)
        universe.search_time += time() - t0
        if plan is not None:
            cost = plan_cost(universe, plan)
            if cost < best_cost:
                best_plan, best_cost = plan, cost
                if verbose:
                    print best_cost, convert_plan(best_plan)
            if not optimal:
                break
        if waves and not call_queue_wave(queue, universe, frequency, max_time,
                                         max_calls):
            break
        if not waves and not call_queue(queue, universe, frequency, max_time,
                                        max_calls):
            break
    if verbose:
        universe.print_statistics()
        if best_plan is not None:
            print_plan_stats(best_plan, universe)
            print 'Cost:', best_cost
            print 'Length:', len(best_plan)

    return best_plan, universe
Ejemplo n.º 5
0
def print_solved(universe, plan):
    universe.print_statistics()
    print_plan_stats(plan, universe)
def incremental_planner(
        problem,
        search=DEFAULT_SEARCH,
        max_time=INF,
        min_cost=INF,
        waves=True,
        frequency=1,
        optimal=False,  # TODO: remove optimal
        postprocess_time=None,
        verbose=False,
        debug=False):
    """
  Incremental algorithm.

  :param problem: :class:`.STRIPStreamProblem`
  :param search: python function of the search subroutine
  :param max_time: numeric maximum planning time
  :param max_iterations: int maximum subroutine calls
  :param waves: boolean flag when ``True`` considers each stream evaluation iteration to be the current queue rather than a single stream
  :param frequency: numeric number of evaluation iterations given by ``waves`` (``float('inf')`` implies to evaluate all streams per iteration)
  :param optimal: boolean flag which saves the best current solution
  :param verbose: boolean flag which toggles the print output
  :param debug: boolean flag which prints the objects on each iteration
  :return: a sequence of :class:`.Action` and tuple of :class:`.Constant` pairs as well as :class:`.Universe`
  """
    for stream in problem.cond_streams:
        if not stream.sign and not stream.eager:
            print 'Warning! Setting stream', stream, 'to eager because used negatively'
            stream.eager = True

    #print SEPARATOR
    universe = Universe(problem, use_ground=False, verbose=verbose)
    #useful_atoms, useful_types = useful_universe(universe)
    #stream_pairs = get_streams(useful_atoms, useful_types, universe) # TODO - prune any unnecessary cond_streams
    queue = deque()
    add_streams(universe, queue)
    best_plan, best_cost = None, INF
    while True:
        print_status(universe, best_plan, best_cost)
        if debug:
            debug_planner(universe)
        universe.iterations += 1
        if (not optimal) or (postprocess_time is None):
            best_plan, best_cost = update_best_plan(
                universe, search, best_plan, best_cost,
                max_time - (time() - universe.start_time))
        if (best_plan is not None) and ((not optimal) or (best_cost <= 0)):
            break
        if waves and not call_queue_wave(queue, universe, frequency, max_time):
            break
        if not waves and not call_queue(queue, universe, frequency, max_time):
            break
    add_streams(
        universe, queue
    )  # TODO: hack for postprocess_time != None to not give "Could not find instantiation for PNE!"
    best_plan, best_cost = update_best_plan(universe, search, best_plan,
                                            best_cost, postprocess_time)
    print_status(universe, best_plan, best_cost)
    if verbose:
        universe.print_statistics()
        if best_plan is not None:
            print_plan_stats(best_plan, universe)
            print 'Cost: {}\nLength: {}'.format(best_cost, len(best_plan))
    return best_plan, universe