Esempio n. 1
0
    def trace_back(goal_node, start_node, v_distances, visited_nodes, n, mazearray, diags=False):
        
        # begin the list of nodes which will represent the path back, starting with the end node
        path = [goal_node]
        
        current_node = goal_node
        
        # Set the loop in motion until we get back to the start
        while current_node != start_node:
            # Start an empty priority queue for the current node to check all neighbours
            neighbour_distances = PriorityQueue()
            
            neighbours = get_neighbours(current_node, n, diags)

            # Had some errors during testing, not sure if this is still necessary
            try:
                distance = v_distances[current_node]
            except Exception as e:
                print(e)
            
            # For each neighbour of the current node, add its location and distance
            # to a priority queue
            for neighbour, ntype in neighbours:
                if neighbour in v_distances:
                    distance = v_distances[neighbour]
                    neighbour_distances.push(distance, neighbour)
            
            # Pop the lowest value off; that is the next node in our path
            distance, smallest_neighbour = neighbour_distances.pop()
            mazearray[smallest_neighbour[0]][smallest_neighbour[1]].update(is_path=True)
            path.append(smallest_neighbour)
            current_node = smallest_neighbour

        mazearray[start_node[0]][start_node[1]].update(is_path=True)
Esempio n. 2
0
 def _shortest_paths(self, start):
     # Compute a shortest path from start to each other node, if it exists.
     assert self.has_node(start)
     infinity = float('infinity')
     # Keep the nodes ordered by distance from the start node.
     to_visit = PriorityQueue()
     # All nodes are initially unreachable from the start node.
     for node in self.nodes():
         self._nodes[node]['distance'] = infinity
         self._nodes[node]['from'] = None
         to_visit.enqueue(node, infinity)
     # Correct the distance of the start node.
     self._nodes[start]['distance'] = 0
     to_visit.set_priority(start, 0)
     while not to_visit.is_empty():
         node = to_visit.dequeue()
         node_distance = self._nodes[node]['distance']
         for neighbour in self.neighbours(node):
             neighbour_distance = self._nodes[neighbour]['distance']
             # Compute the new distance to neighbour, going through node.
             edge_distance = self._edges[node][neighbour]
             new_distance = node_distance + edge_distance
             # If it's shorter going this way, update the distance and path.
             if new_distance < neighbour_distance:
                 self._nodes[neighbour]['distance'] = new_distance
                 self._nodes[neighbour]['from'] = node
                 to_visit.set_priority(neighbour, new_distance)
Esempio n. 3
0
def prim(g, n):

    # Set initial condition for Prim algorithm
    parent = [None] * n
    toVisit = [True] * n

    pq = PriorityQueue()

    for u in range(n):
        if u == 0:
            pq.push([0, u])
        else:
            pq.push([sys.maxint, u])

    while not pq.empty():
        cost, u = pq.pop()
        toVisit[u] = False
        for v in range(0, n):
            if g[u][v] and toVisit[v] and g[u][v] < pq[v]:
                parent[v] = u
                pq.push([g[u][v], v])

    print("(origem, destino) -> custo")
    for i in range(1, n):
        print("({}, {}) -> {}".format(parent[i], i, g[i][parent[i]]))
    print("Custo total: {}".format(sum(g[i][parent[i]] for i in range(1, n))))
Esempio n. 4
0
class Parser(object):
    """docstring for Parser"""
    def __init__(self, proxies):
        super(Parser, self).__init__()
        self.proxies = proxies

        self.companies = PriorityQueue()

        self.parsed = set()

    def add(self, url, priority = float('inf')):
        if not url in self.parsed:
            self.companies.push(url, priority)

    def pop(self):
        try:
            companyID = self.companies.pop()

            while not companyID:
                companyID = self.companies.pop()

            return companyID
        except IndexError, e:
            return None
        except Exception, e:
            raise
Esempio n. 5
0
 def shortest_path2exit2(self, startsquare):
     pq = PriorityQueue()
     distance = dict()
     pred = dict()
     on_border = set()
     for square in self.open_squares():
         pq.set_priority(square, Maze.infty)
         if self.isborder(square):
             on_border.add(square)
     pq.set_priority(startsquare, 0)
     distance[startsquare] = 0
     while not pq.is_empty():
         square = pq.extract_min()
         if not square in distance:
             continue
         dsquare = distance[square] + 1
         for next_sq in self.neighbors(square):
             if next_sq in pq and \
                ((not next_sq in distance) or dsquare < distance[next_sq]):
                 distance[next_sq] = dsquare
                 pq.set_priority(next_sq, dsquare)
                 pred[next_sq] = square
     min_dist = None
     min_b = None
     for b in on_border:
         if min_dist is None or min_dist > distance[b]:
             min_dist = distance[b]
             min_b = b
     b = min_b
     path = list()
     while b != startsquare:
         path.append(b)
         b = pred[b]
     path.append(startsquare)
     return min_dist, list(reversed(path))
Esempio n. 6
0
def Dijkstra_algorithm(graph: Graph,
                       starting_node: int) -> DijkstraAlgorithmResult:

    dist = [INFINITY for _ in range(graph.nodes_count)]
    prev = [None for _ in range(graph.nodes_count)]

    dist[starting_node] = 0
    prev[starting_node] = starting_node

    # prepare list of tuples of nodes labels with their starting distances
    dist_nodes = [
        PQNode(key=i, priority=dist[i]) for i in range(0, graph.nodes_count)
    ]

    Q = PriorityQueue(raw=dist_nodes)

    while not Q.is_empty:
        # pick the closest node
        fst = Q.pop().key
        for e in graph.get_neighbourhood(fst):
            # scan the neighbourhood
            snd = e.snd
            weight = e.weight
            if dist[snd] > dist[fst] + weight:
                # update if better route found
                dist[snd] = dist[fst] + weight
                prev[snd] = fst
                Q.bottom_bound_flatten_priority(snd, dist[snd])

    return DijkstraAlgorithmResult(dist=dist, prev=prev)
Esempio n. 7
0
    def trace_back(goal_node, start_node, v_distances, visited_nodes, n, mazearray, diags=False, visualise=VISUALISE):
        path = [goal_node]
        current_node = goal_node
        
        while current_node != start_node:
            neighbour_distances = PriorityQueue()
            neighbours = get_neighbours(current_node, n, diags)
            try:
                distance = v_distances[current_node]
            except Exception as e:
                print(e)

            for neighbour, ntype in neighbours:
                if neighbour in v_distances:
                    distance = v_distances[neighbour]
                    neighbour_distances.push(distance, neighbour)
            
            distance, smallest_neighbour = neighbour_distances.pop()
            mazearray[smallest_neighbour[0]][smallest_neighbour[1]].update(is_path=True)
            draw_square(smallest_neighbour[0],smallest_neighbour[1],grid=mazearray)
            path.append(smallest_neighbour)
            current_node = smallest_neighbour

        pygame.display.flip()
        mazearray[start_node[0]][start_node[1]].update(is_path=True)
Esempio n. 8
0
def test_insert():
    """Test if value is inserted into the queue."""
    from priority_queue import PriorityQueue
    pq = PriorityQueue()
    item = (1, 'value')
    pq.insert(item)
    assert pq.container[0] == (1, 'value')
Esempio n. 9
0
def test_pop():
    """Test if removes highest priority."""
    from priority_queue import PriorityQueue
    pq = PriorityQueue()
    pq.container = [(2, 'fish'), (3, 'sticks'), (4, 'chix'), (10, 'ticks')]
    pq.pop()
    assert pq.container == [(10, 'ticks'), (3, 'sticks'), (4, 'chix')]
Esempio n. 10
0
    def test_PriorityQueue_RemoveElement_ElementIsRemoved(self):
        priority_queue = PriorityQueue()
        priority_queue.add("a")
        
        priority_queue.remove("a")

        self.assertFalse("a" in priority_queue)
Esempio n. 11
0
    def __init__(self,
                 world,
                 s_start: (int, int),
                 s_goal: (int, int),
                 view_range=2):
        # init the graphs
        self.est_global_map = OccupancyGridMap(x_dim=world.x_dim,
                                               y_dim=world.y_dim,
                                               exploration_setting='8N')

        # real_graph
        self.gt_global_map: OccupancyGridMap = world

        # init variables
        self.view_range = view_range
        self.position = s_start
        self.goal = s_goal
        self.back_pointers = {}

        # procedure initialize
        self.U = PriorityQueue()
        self.k_m = 0
        self.RHS_VALS = {}
        self.G_VALS = {}  # empty set
        self.RHS_VALS[s_goal] = 0.0
        self.U.insert(s_goal, self.calculate_key(s_goal))

        # keeps track of the best path starting from goal to our position
        self.back_pointers[self.goal] = None

        # replan
        self.compute_shortest_path(s_start=s_start)
Esempio n. 12
0
def color_most_constrained_first(graphs, color):
    global spills, unspillable, num_unused, used_registers

    if debug:
        print 'starting color_most_constrained_first'
    
    for v in graphs.vertices():
        used_registers[v] = set([])
        num_unused[v] = len(registers) - len(used_registers[v])

    left = set(graphs.vertices()) - set(reserved_registers) - set(registers)
    queue = PriorityQueue(left, avail_reg_then_unspill)

    while not queue.empty():
        v = queue.pop()
        if debug:
            print 'next to color is ' + v
        if v not in color.keys():
            c = choose_color(v, color, graphs)
            color[v] = c
            if debug:
                print 'color of ' + str(v) + ' is ' + str(c)
            for u in graphs.interferes_with(v):
                #if u in graphs.vertices():
                used_registers[u] |= set([c])
                num_unused[u] = len(registers) - len(used_registers[u])
                queue.update(u)
            if not is_reg(c):
                spills += 1
# spills happen to unspillable in test2.py on schof! -Jeremy
#                 if v in unspillable[0]:
#                     raise Exception('spilled an unspillable! ' + v)
    if debug:
        print 'finished with color_most_constrained_first'
    return color
Esempio n. 13
0
def color_most_constrained_first(graphs, color):
    global spills, unspillable, num_unused, used_registers

    if debug:
        print 'starting color_most_constrained_first'
    
    for v in graphs.vertices():
        used_registers[v] = set([])
        num_unused[v] = len(registers) - len(used_registers[v])

    left = set(graphs.vertices()) - set(reserved_registers) - set(registers)
    queue = PriorityQueue(left, avail_reg_then_unspill)

    while not queue.empty():
        v = queue.pop()
        if debug:
            print 'next to color is ' + v
        if v not in color.keys():
            c = choose_color(v, color, graphs,False)
            color[v] = c
            for u in graphs.interferes_with(v):
                #if u in graphs.vertices():
                queue.update(u)
                used_registers[u] |= set([c])
                num_unused[u] = len(registers) - len(used_registers[u])
                
            if not is_reg(c):
                spills += 1
# spills happen to unspillable in hw6! -Jeremy
# Also, register allocation takes too long on some programs.
#                 if v in unspillable[0]:
#                     raise Exception('spilled an unspillable! ' + v)
    return color
Esempio n. 14
0
    def __init__(self, map: OccupancyGridMap, s_start: (int, int, int),
                 s_goal: (int, int, int)):
        """
        :param map: the ground truth map of the environment provided by gui
        :param s_start: start location
        :param s_goal: end location
        """
        ## we need to also update our occupancy grid map
        self.new_edges_and_old_costs = None

        # algorithm start
        self.s_start = s_start  # now takes in an x, y and z
        self.s_goal = s_goal  # now takes in an x, y and z
        self.s_last = s_start  # now takes in an x, y and z
        self.k_m = 0  # accumulation
        self.U = PriorityQueue()
        self.rhs = np.ones((map.x_dim, map.y_dim, map.z_dim)) * np.inf
        self.g = self.rhs.copy()

        self.sensed_map = OccupancyGridMap(x_dim=map.x_dim,
                                           y_dim=map.y_dim,
                                           z_dim=map.z_dim,
                                           exploration_setting='26N')

        self.rhs[self.s_goal] = 0
        self.U.insert(self.s_goal,
                      Priority(heuristic(self.s_start, self.s_goal), 0))
 def test_not_empty(self):
     """
     A queue with one enqueued value is not empty.
     """
     pq = PriorityQueue()
     pq.enqueue(Job(1, 'People'))
     self.assertFalse(pq.is_empty())
Esempio n. 16
0
    def __init__(self, board, data):
        self.board = board
        self.data = data

        self.forward_begin_state = State(self.board, None, None, 0, 0, 0)
        self.forward_data = Data(data.board_num, data.heuristic, data.time_limit, data.indicators, data.given_solution,
                                 data.min_cost_path, data.use_difficulty)
        self.forward_data.bidirectional_direction = BidirectionalDirection.FORWARD
        self.forward_f_values = dict()
        self.forward_open_list = PriorityQueue()
        self.forward_closed_list = set()

        state = self.forward_begin_state.run_steps_on_board(data, False, False)
        self.backward_begin_state = State(state.board, None, None, 0, 0, 0)
        self.backward_data = Data(data.board_num, data.heuristic, data.time_limit, data.indicators, data.given_solution,
                                  data.min_cost_path, data.use_difficulty)
        self.backward_data.bidirectional_direction = BidirectionalDirection.BACKWARD
        self.backward_f_values = dict()
        self.backward_open_list = PriorityQueue()
        self.backward_closed_list = set()

        self.forward_data.goal_board = self.backward_begin_state.board
        self.backward_data.goal_board = self.forward_begin_state.board

        f = self.forward_begin_state.calculate_f(self.forward_data)
        self.forward_begin_state.f_value = f
        self.forward_f_values[hash(self.forward_begin_state.board.grid_to_str())] = f
        self.forward_open_list.push(self.forward_begin_state)
        f = self.backward_begin_state.calculate_f(self.backward_data)
        self.backward_begin_state.f_value = f
        self.backward_f_values[hash(self.backward_begin_state.board.grid_to_str())] = f
        self.backward_open_list.push(self.backward_begin_state)

        self.forward_prev_state = None
        self.backward_prev_sate = None
Esempio n. 17
0
    def build_tree(self, text):
        """
        Users letter frequency found in text to construct the Huffman tree

        :param text: Input text to use for calculating letter frequencies
        """
        if not text:
            raise ValueError
        self.input = text
        self.char_freqs = frequencies(self.input)
        pqueue = PriorityQueue()
        for k, v in self.char_freqs.items():
            pqueue.push(HuffmanNode([k], v, None, None))

        left = pqueue.pop()
        right = pqueue.pop()
        while left and right:
            pqueue.push(
                HuffmanNode(left.char_list + right.char_list,
                            left.freq + right.freq, left, right))
            left = pqueue.pop()
            right = pqueue.pop()

        self.head = left

        self.code_dict = {}
        for c in self.head.char_list:
            self.code_dict[c] = self.get_code(c)
def test_priority_queue(make_jobs):
    pq = PriorityQueue()
    assert pq.jobs == []
    assert pq._iterations == 0
    pq = PriorityQueue(make_jobs)
    temp = [p.priority for p in pq.jobs]
    assert temp == [9, 9, 9, 7, 8, 3, 7, 3, 7, 4]
Esempio n. 19
0
def prims_mst_alt(g):
    visited = set()  # visited set, already in MST
    pq = PriorityQueue()  # For edges
    mst = []  # result as a list of edges
    N = g.num_vert

    start = 5  # Arbitrarily choose a start node.

    while len(mst) < N-1:
        node = g.vert_list[start]
        visited.add(start)
        # Check all the neighbours and add them to PQ if needed.
        for neigh in node.get_neighs():
            if neigh not in visited:
                key = node.get_weight(neigh)
                edge = (start, neigh, key)
                pq.decrease_priority(edge, key)

        while pq.length > 0:
            u, v, w = pq.extract_min()
            if v not in visited:
                break
        else:
            print('disconnected')
            return
        mst.append((u, v, w))
        start = v

    print(mst)
Esempio n. 20
0
 def a_star(self, map, start, goal):
     rospy.loginfo("Executing A* from (%d,%d) to (%d,%d)" %
                   (start[0], start[1], goal[0], goal[1]))
     queue = PriorityQueue(start)
     visited = set()
     came_from = {}
     while queue:
         #self.yeet(map, visited, queue.get_elements())
         element, previous_element = queue.pop()
         visited.add(element)
         came_from[element] = previous_element
         if element == goal:
             path = [element]
             while came_from[element]:
                 element = came_from[element]
                 path.append(element)
             return path[::-1]
         [
             queue.put(
                 (map.euclidean_distance(start, e) +
                  map.euclidean_distance(e, goal) +
                  100 * map.euclidean_distance(e, element), e, element))
             for e in map.get_neighbors(element, threshold2=0)
             if e not in visited and e not in queue.get_elements()
         ]
Esempio n. 21
0
    def run_reverse_a_star(self, show_maze=False):

        if show_maze:
            maze_vis = MazeVisualization(self.maze)
        re_compute = True
        print("Bot's destination is - row: {}, col: {}".format(
            self.maze.goal.row, self.maze.goal.col))
        traversed_path = list()
        while re_compute:
            manhattan_heuristic = Utils.compute_heuristic(
                self.maze, self.bot.pos)
            start_row = self.maze.goal.row
            start_col = self.maze.goal.col
            my_queue = PriorityQueue()
            my_queue.push(0, manhattan_heuristic[start_row][start_col],
                          self.bot.bot_maze.get_cell(start_row, start_col),
                          None)
            path = self.compute_path(my_queue, manhattan_heuristic)
            if path is None:
                print("Path does not exist")
                return None, None
            traversed_path.extend(self.bot.traverse_path(path))
            if self.bot.pos.get_co_ordinates(
            ) == self.maze.goal.get_co_ordinates():
                re_compute = False

        if show_maze:
            Utils.print_path(traversed_path, maze_vis, "Reverse A*",
                             self.maze.start, self.maze.goal)
            maze_vis.show_maze()
        return self.pop_counter, len(traversed_path)
def dijkstra(g, n, origin=0):

    # Set initial condition for dijkstra algorithm
    toVisit = [True] * n
    dist = [sys.maxint] * n

    pq = PriorityQueue()

    for u in range(n):
        if u == origin:
            pq.push([0, u])
        else:
            pq.push([sys.maxint, u])

    while not pq.empty():
        cost, u = pq.pop()
        toVisit[u] = False
        for v in range(0, n):
            if g[u][v] != 0 and toVisit[v] and cost + g[u][v] < pq[v]:
                pq.push([g[u][v] + cost, v])
                dist[v] = pq[v]

    print("(origem, destino) -> custo")
    for i in range(1, n):
        print("({}, {}) -> {}".format(origin, i, dist[i]))
 def run_a_star_with_agent(self):
     maze_vis = MazeVisualizer(self.maze)
     re_compute = True
     traversed_path = list()
     manhattan_heuristic = Utils.compute_heuristic(self.maze, self.maze.end)
     while re_compute:
         start_row = self.agent.current_loc.row
         start_col = self.agent.current_loc.col
         my_queue = PriorityQueue()
         my_queue.push(0, manhattan_heuristic[start_row][start_col],
                       self.agent.agent_maze.get_cell(start_row, start_col),
                       None)
         path = self.compute_path(my_queue, manhattan_heuristic)
         if path is None:
             print("Path does not exist")
             return None, None
         traversed_path.extend(self.agent.traverse_path(path))
         if self.agent.current_loc.get_co_ordinates(
         ) == self.maze.end.get_co_ordinates():
             re_compute = False
     Utils.print_path(traversed_path, maze_vis, "A*", self.maze.start,
                      self.maze.end)
     print("\nTotal popped nodes are: ", self.pop_counter)
     maze_vis.show_maze()
     return self.pop_counter, len(traversed_path)
    def run_adaptive_a_star(self):
        maze_vis = MazeVisualizer(self.maze)
        traversed_path = [self.agent.current_loc]
        re_compute = True
        print("Agent's destination is - row: {}, col: {}".format(
            self.maze.end.row, self.maze.end.col))
        while re_compute:
            start_row = self.agent.current_loc.row
            start_col = self.agent.current_loc.col
            my_queue = PriorityQueue()
            my_queue.push(0, self.heuristics[start_row][start_col],
                          self.agent.agent_maze.get_cell(start_row, start_col),
                          None)
            path = self.compute_path(my_queue)
            if path is None:
                print("Path does not exists\n")
                return None, None
            traversed_path.extend(self.agent.traverse_path(path))
            if self.agent.current_loc.get_co_ordinates(
            ) == self.maze.end.get_co_ordinates():
                re_compute = False

        print("Adaptive * path exists\n")
        self.update_heuristics(traversed_path)
        Utils.print_path(traversed_path, maze_vis, "Adaptive A*",
                         self.maze.start, self.maze.end)
        maze_vis.show_maze()
        return self.pop_counter, len(traversed_path)
def test_assign_priority_via_insert():
    new_pq = PriorityQueue()
    for i in range(5):
        new_pq.insert(Job(priority= i+1))
    new_pq.jobs[-1].time_created -= 100
    new_pq.insert(Job(priority=6))
    assert [job.priority for job in new_pq.jobs] == [8,5,6,1,4,2]
 def execute_algorithm(self, show_maze=False):
     if show_maze:
         maze_vis = MazeVisualization(self.maze)
     traversed_path = [self.bot.pos]
     re_compute = True
     print("Bot's destination is - row: {}, col: {}".format(
         self.maze.goal.row, self.maze.goal.col))
     while re_compute:
         start_row = self.bot.pos.row
         start_col = self.bot.pos.col
         my_queue = PriorityQueue()
         my_queue.push(0, self.heuristics[start_row][start_col],
                       self.bot.bot_maze.get_cell(start_row, start_col),
                       None)
         path = self.find_path(my_queue)
         if path is None:
             print("Path does not exists\n")
             return None, None
         traversed_path.extend(self.bot.traverse_path(path))
         if self.bot.pos.get_co_ordinates(
         ) == self.maze.goal.get_co_ordinates():
             re_compute = False
     self.time_end = time.time()
     print("Adaptive * path exists\n")
     self.update_heuristics(traversed_path)
     if show_maze:
         Utils.print_path(traversed_path, maze_vis, "Adaptive A*",
                          self.maze.start, self.maze.goal)
         maze_vis.show_maze()
     return self.pop_counter, len(traversed_path) - 1
Esempio n. 27
0
class Search:
    def __init__(self, goal_state, search_type="bfs"):
        self.goal_state = goal_state
        self.open_list = PriorityQueue()
        self.closed_list = []
        self.id_count = 1
        self._open_list_pop_count = 0
        self._open_list_add_count = 0
        self._closed_list_add_count = 0
        self.search_type = search_type

    def swapPositions(self, lst, pos1, pos2):
        lst[pos1], lst[pos2] = lst[pos2], lst[pos1]
        return lst

    def getPathTaken(self, goal):
        path = [goal]
        node = goal
        found_original = False
        while not found_original:
            for closed in self.closed_list:
                if node.parent_stateid == closed.stateid:
                    node = closed
                    path.append(node)
                    if node.stateid == 1:
                        found_original = True
                    break
        path.reverse()
        return path

    def getOpenListNode(self):
        self._open_list_pop_count += 1
        return self.open_list.pop()

    def checkForDuplicates(self, nodes):
        to_pop = []
        for i in nodes:
            for k in self.closed_list:
                if i.state == k.state:
                    to_pop.append(i)
                    break
            for j in self.open_list:
                if i.state == j.state:
                    if i.priority > j.priority:
                        self.open_list.remove(j)
                    else:
                        to_pop.append(i)
                    break
        for item in to_pop:
            nodes.remove(item)
        return nodes

    def returnResults(self, path):
        return (
            "Length Of Path To Get To Goal: {}".format(len(path)),
            "Nodes Pushed To Open List: {}".format(self._open_list_add_count),
            "Nodes Pushed To Closed List: {}".format(
                self._closed_list_add_count),
        )
 def test_enqueue_dequeue_one(self):
     """
     Enqueueing a single value is immediately dequeable.
     """
     pq = PriorityQueue()
     j = Job(5, 'The')
     pq.enqueue(j)
     self.assertEqual(j, pq.dequeue())
def new_pq():
    pq = PriorityQueue()
    for index,value in enumerate([3,3,4,9,7,7,9,9,7,8]):
        pq.insert(Job(priority=value))
        if not index % 3:
            pq._iterations = 0
    pq._iterations = 0
    return pq
Esempio n. 30
0
 def test_4(self):
     array = [randint(100000, 150000) for i in range(randint(30, 60))]
     array_max = max(array)
     queue = PriorityQueue()
     for element in array:
         queue.insert(element)
     max_element = queue.extract_max()
     self.assertEqual(max_element, array_max, msg=array)
Esempio n. 31
0
def solve_board(board, data):
    global DATA
    DATA = data
    steps = 0
    f_values = dict()
    f = board.calculate_f(steps, DATA)
    f_values[hash(grid_to_str(board.grid))] = f
    first_state = State(board, None, None, f, steps, 0)

    open_list = PriorityQueue()
    closed_list = set()

    # Step 1: Put the start node on a list called OPEN of unexpanded nodes.
    # Calculate f(s) and associate its value with node s.
    open_list.push(first_state)
    # Step 2: If OPEN is empty, exit with failure, no solution exists.
    while open_list.is_empty():
        # Step 3: Select from OPEN a node i at which f is minimum. If several nodes qualify,
        # choose a goal node if there is one, otherwise choose among them arbitrarily.
        state = open_list.pop().state

        # Step 4: Remove node i from OPEN and place it on a list called CLOSED, of expanded nodes.
        DATA.scanned_nodes = DATA.scanned_nodes + 1
        closed_list.add(hash(grid_to_str(state.board.grid)))

        # Step 5: If i is a goal node, exit with success; a solution has been found.
        if state.goal_state():
            solution_steps = get_solution_steps(state)
            solution_str = create_solution_string(solution_steps, state)
            DATA.finalize(solution_str, len(solution_steps), open_list)
            return solution_str

        # Step 6: Expand node i, creating nodes for all of its successors. For every successor node j of i:
        # Step 6.1: Calculate f(j)
        expanded_states = expand_state(state)
        for expanded_state in expanded_states:
            # Step 6.2: If j is neither in OPEN nor in CLOSED, then add it to OPEN with its f value.
            # Attach a pointer from j back to its predecessor i
            if hash(grid_to_str(expanded_state.board.grid)) not in f_values:
                open_list.push(expanded_state)
                f_values[hash(grid_to_str(
                    expanded_state.board.grid))] = expanded_state.f_value
            else:
                # Step 6.3: If j was already on either OPEN or CLOSED, compare the f value just calculated for j with
                # the value previously associated with the node.
                if expanded_state.f_value < f_values[hash(
                        grid_to_str(expanded_state.board.grid))]:
                    f_values[hash(grid_to_str(
                        expanded_state.board.grid))] = expanded_state.f_value
                    # Step 6.3.1: Substitute it for the old value.
                    if is_expansion_in_closed_list(expanded_state,
                                                   closed_list):
                        # Step 6.3.2: Point j back to i instead of to its previously found predecessor.
                        # Step 6.3.3: If node j was on the CLOSED list, move it back to OPEN
                        open_list.push(expanded_state)
                        remove_state(expanded_state, closed_list)
    DATA.finalize("FAILED", 0, open_list)
    return None
Esempio n. 32
0
 def test_PriorityQueue_NonEmptyQueueToString_ReturnsCorrectString(self):
     priority_queue = PriorityQueue()        
     priority_queue.add("a", 2)
     priority_queue.add("b", 3)
     priority_queue.add("c", 1)
     
     s = str(priority_queue)
     
     self.assertEquals(s, "<PriorityQueue[(c,1),(a,2),(b,3)]>")
Esempio n. 33
0
    def __init__(self, dim, res, dx, real):
        super().__init__(dim, res, dx, real)

        self.priority_queue = PriorityQueue(dim, res, real)
        self.surface_grid = ti.Vector.field(dim,
                                            dtype=ti.i32,
                                            shape=reduce(
                                                lambda x, y: x * y, res))
        self.total_sg = ti.field(dtype=ti.i32, shape=())
Esempio n. 34
0
 def __init__(self, goal_state, search_type="bfs"):
     self.goal_state = goal_state
     self.open_list = PriorityQueue()
     self.closed_list = []
     self.id_count = 1
     self._open_list_pop_count = 0
     self._open_list_add_count = 0
     self._closed_list_add_count = 0
     self.search_type = search_type
Esempio n. 35
0
    def test_PriorityQueue_RemoveFirstElement_StatesRemaingCorrect(self):
        priority_queue = PriorityQueue()
        priority_queue.add("a", 1)
        priority_queue.add("b", 2)
       
        priority_queue.remove("a")

        self.assertEquals(list(priority_queue), [('b', 2)])
        self.assertEquals(len(priority_queue), 1)
Esempio n. 36
0
 def test_sequence2(self):
     Q = PriorityQueue()
     Q.insert(1)
     Q.insert(2)
     Q.insert(3)
     self.assertEqual(Q.extract_max(), 3)
     self.assertEqual(Q.extract_max(), 2)
     self.assertEqual(Q.extract_max(), 1)
     with self.assertRaises(NoMoreDataError):
         Q.extract_max()
Esempio n. 37
0
 def test_PriorityQueue_ChangePriorityUsingAddRemoveElement_PeekReturnsLowestElement(self):
     priority_queue = PriorityQueue()        
     priority_queue.add("a", 2)
     priority_queue.add("b", 3)
     priority_queue.add("c", 1)
     priority_queue.add("d", 0)
     priority_queue.remove("c")
     priority_queue.add("c", 4)
     elm, pri = priority_queue.peek()
     self.assertEquals(elm, "d")
def k_nearest(points, center, k):
    pq = PriorityQueue()
    for point in points:
        distance_square = (point[0] - center[0])**2 + (point[1] - center[1])**2
        distance = math.sqrt(distance_square)
        node = Node(point, distance)
        pq.enque(node)

    for _ in range(k):
        print(pq.deque())
Esempio n. 39
0
 def test_PriorityQueue_GetPriority_ReturnsCorrectPriority(self):
     priority_queue = PriorityQueue()
     priority_queue.add("a", 0)
     priority_queue.add("b", 1)
     
     p_a = priority_queue.get_priority("a")
     p_b = priority_queue.get_priority("b")
     
     self.assertEquals(p_a, 0)
     self.assertEquals(p_b, 1)
Esempio n. 40
0
    def run_algorithm(self):
        """
        run the algorithm
        :return: True is there is a path from strat to end, False otherwise
        """
        open_list = PriorityQueue(f=self.f)
        open_list.append(self.start_point)
        closed_list = set()
        while not len(open_list) == 0:
            next_n = open_list.pop()
            closed_list.add(next_n)

            if self.end_point == next_n:
                self.value = next_n.total_value()
                self.path = next_n.arrived_from
                return True
            else:
                self.path_count += 1
            for suc in next_n.successors:
                suc.arrived_from = next_n.arrived_from + [self.get_direction(next_n.x, next_n.y, suc.x, suc.y)]
                suc.fathers = next_n.fathers + [next_n]
                if suc not in closed_list and suc not in open_list:
                    open_list.append(copy.deepcopy(suc))
                elif suc in open_list and self.f(suc) < open_list[suc]:
                    del open_list[suc]
                    open_list.append(copy.deepcopy(suc))

        self.path = self.NO_PATH
        return False
def test_insert_key():
    pq = PriorityQueue()
    pq.insert_key(24)
    pq.insert_key(12)
    pq.insert_key(1)
    pq.insert_key(35)

    assert(pq.contains(24) == True)
    assert(pq.contains(12) == True)
    assert(pq.contains(1) == True)
    assert(pq.contains(35) == True)
    assert(pq.contains(10) == False)
Esempio n. 42
0
class StockExchange(object):
    
    def __init__(self):
        self.buyers = PriorityQueue()
        self.sellers = PriorityQueue()
        
    def buy(self, buy_order):
        '''Input a buy order, regardless of any suitable sell order.'''
        self.buyers.push(buy_order, buy_order)  # use order value for key and value.
        
    def sell(self, sell_order):
        '''Input a sell order, regardless of any suitable buy order.'''
        self.sellers.push(sell_order, sell_order)   # use order value for key and value.
        
    def get_buyers(self):
        '''Return a list of all pending buyers.'''
        summary = 'Pending Buyers: \n'
        for buyer in self.buyers.elements():
            summary += ('$' + str(buyer._key) + '\n')
        return summary
        
    def get_sellers(self):
        '''Return a list of all pending sellers.'''
        summary = 'Pending Sellers: \n'
        for seller in self.sellers.elements():
            summary += ('$' + str(seller._key) + '\n')
        return summary
        
    def process(self):
        '''Process buy orders in an optimal match-up fashion. O(n2) time.'''
        # A buy order for $x can only be processed if there is an existing sell order with price $y
        # such that $x >= $y.
        summary = ''
        temp_buyers = PriorityQueue()                       # Create a temp pq for buyers
        while self.buyers.peek() is not None:               # Iterate all buyers
            buyer = self.buyers.pop()                       # Get the next buyer
            temp_sellers = PriorityQueue()
            buyer_matched = False                           # Assume there's no match for this buyer
            while self.sellers.peek() is not None:
                seller = self.sellers.pop()                 # Get the next seller
                if buyer._key >= seller._key:               # This is a valid matchup
                    buyer_matched = True
                    summary += ('Buyer $' + str(buyer._key) + ' matched with seller $' + str(seller._key) + '\n')
                else:
                    # if this seller was no good, push it on the temp seller pq
                    temp_sellers.push(seller._key, seller._value)
            self.sellers = temp_sellers                     # Copy the temp seller pq back to the original
            if not buyer_matched:
                temp_buyers.push(buyer._key, buyer._value)
        self.buyers = temp_buyers                       # Copy the temp buyer pq back to the original
        return summary
def test_get_max():
    pq = PriorityQueue()
    pq.insert_key(24)
    pq.insert_key(12)
    pq.insert_key(1)
    pq.insert_key(35)

    assert(pq.get_max() == 35)
Esempio n. 44
0
    def __init__(self, proxies):
        super(Parser, self).__init__()
        self.proxies = proxies

        self.companies = PriorityQueue()

        self.parsed = set()
def test_extract_max():
    pq = PriorityQueue()
    pq.insert_key(24)
    pq.insert_key(12)
    pq.insert_key(1)
    pq.insert_key(35)

    assert(pq.extract_max() == 35)
    assert(pq.extract_max() == 24)
Esempio n. 46
0
def trial(trial_no, num_actions):
    """Perform one test run of the priority queue."""
	logging.info("Trial %s", trial_no)
	pq = PriorityQueue()
	insert_delete(pq, int(num_actions))
	items = []
	while not pq.empty():
		items.append(pq.get())
	if len(items) < 2:
		logging.info("Order is trivially preserved; fewer than 2 items.")
	preserved = True
	for i1, i2 in it.izip(items, items[1:]):
		if i1 >= i2:
			preserved = False
			break
	logging.info("Order%s preserved.", {True: "", False: " not"}[preserved])
	log_items(items)
def test_assign_priority_via_pop():
    new_pq = PriorityQueue()
    for _ in range(4):
        new_pq.insert(Job(priority=1))
    new_pq.jobs[3].time_created -= 100
    new_pq.pop()
    assert [job.priority for job in new_pq.jobs] == [6, 1, 1]
    new_pq.jobs[2].time_created -= 100
    new_pq._iterations = 4
    new_pq.pop()
    assert [job.priority for job in new_pq.jobs] == [6, 1]
Esempio n. 48
0
def astar(start_gamestate, heuristic=heuristic_3, targets=range(1,17), q=False):
    """
    parameters:
    start_gamestate -- a Gamestate object
    heuristic -- a function
    targets -- a list of (row, col) positions which need to be in the correct
               spot on the board. Determines goal state.
    q -- quiet flag. If False, print out info to give some indication of
         progress
    """
    start_time = time.time()
    cur_state = start_gamestate
    cur_actions = []
    prev_len = -1

    fringe = PriorityQueue()
    visited_states = set([cur_state])

    while not cur_state.is_goal_state(targets):
        
        # print info to the screen
        # pretty poor way to track program's progress
        if not q and len(cur_actions) > prev_len:
            print len(cur_actions), time.time() - start_time
            prev_len = len(cur_actions)

        # push neighboring states onto fringe
        for action in cur_state.get_legal_actions():
            successor = cur_state.get_successor(action)
            
            if successor not in visited_states:
                # add neighbor to fringe
                # g = len(cur_actions) + 1
                # h = heuristic
                priority = len(cur_actions) + 1 + heuristic(cur_state, targets)
                # remember state as visited
                visited_states.add(successor)
                fringe.push( (successor, cur_actions + [action]), priority )

        # get next state from fringe, skipping ones we've seen already
        cur_state, cur_actions = fringe.pop()

    return cur_actions, cur_state
Esempio n. 49
0
    def astar(self, animate=False):
        empty_space_count = self.image.histogram()[255]
        fringe = PriorityQueue()
        fringe.push(self.start, self.heuristic(self.start))
        explored = set()

        while fringe:
            if len(explored) % 100000 == 0:
                print "Explored %d/%d nodes! %.3f%% done!" % (len(explored), empty_space_count, len(explored)/float(empty_space_count))

            node, cost = fringe.pop()
            if node == self.end:
                self.path = [node]
                while self.parents[node]:
                    self.path.append(self.parents[node])
                    node = self.parents[node]

            explored.add(node)

            for neighbor in self.neighbors(node):
                if neighbor in explored:
                    continue

                g_score = self.g_scores[node] + 1

                if neighbor not in fringe or g_score < self.g_scores[neighbor]:
                    self.parents[neighbor] = node
                    self.g_scores[neighbor] = g_score
                    fringe.push(neighbor, g_score + self.heuristic(neighbor))

        print "No Solution"
Esempio n. 50
0
class LRUCache(object):
	"""A least-recently-used cache."""
	
	def __init__(self, maxsize=100):
		self.items = {}
		self.oldest = PriorityQueue(key=lambda item: item['timestamp'])
		self.maxsize = maxsize
		self.numitems = 0
	
	def insert(self, key, value):
		"""Insert a piece of data into the cache."""
		if self.numitems >= self.maxsize:
			throw_away = self.oldest.get()
			del self.items[throw_away['key']]
			self.numitems -= 1
		pq_data = {'key': key, 'timestamp': time.time()}
		self.items[key] = {
			'index': self.oldest.put(pq_data),
			'value': value
		}
		self.numitems += 1
	
	def update(self, key):
		"""Update the timestamp of a piece of data in the cache."""
		index = self.items[key]['index']
		self.oldest[index]['timestamp'] = time.time()
		self.oldest.heap.heapify_down(index)
	
	def remove(self, key):
		"""Remove a piece of data from the cache."""
		index = self.items[key]['index']
		thrown_away = self.oldest.delete(index)
		del self.items[key]
		self.numitems -= 1
	
	def __contains__(self, key):
		return key in self.items
	
	def __getitem__(self, key):
		return self.items[key]['value']
Esempio n. 51
0
def merge_streams(streams):
    """Merges sorted streams into one sorted list.
    :param streams: list of MyStream objects.
    :return: sorted list of integers.
    """

    min_buffer = PriorityQueue()
    all_streams_are_empty = False
    iterations = 0
    next_i = None
    while not all_streams_are_empty:
        all_streams_are_empty = True
        for i, stream in enumerate(streams):
            while not stream.empty:
                if (next_i is not None) and i != next_i:
                    break

                new_int = stream.pop()

                if new_int is not None:
                    min_buffer.insert((i, new_int))
                if iterations < 1: # first step is to get the top
                                   # "layer" of all streams
                    break
                else: # if we already have our top "layer" 
                      # we will run the main sorting algorithm
                    next_i, min_int = min_buffer.pop_min()
                    yield min_int # return minimums one by one
                    next_i = None if streams[next_i].empty else next_i

            all_streams_are_empty = all_streams_are_empty and stream.empty
        iterations += 1

    while len(min_buffer): # when all streams became empty
                                # we just ineratively pop all heap content
        yield min_buffer.pop_min()

    print ('DEBUG: heap (list) of real size == %s were allocated '
           'during sorting.' % len(min_buffer.heap))
Esempio n. 52
0
 def test_PriorityQueue_ChangePriority_GetPriorityReturnsNewPriority(self):
     priority_queue = PriorityQueue()
     priority_queue.add("a", 0)
     
     priority_queue.change_priority("a", 5)
     
     p_a = priority_queue.get_priority("a")
     self.assertEquals(p_a, 5)
Esempio n. 53
0
 def test_PriorityQueue_Peek_ReturnsElementWithLowestPriority(self):
     priority_queue = PriorityQueue()        
     priority_queue.add("a", 2)
     priority_queue.add("b", 3)
     priority_queue.add("c", 1)
     
     elm, pri = priority_queue.peek()
     self.assertEquals(elm, "c")
    def __construct_trie(self):
        queue = PriorityQueue.heapify([Huffman.Node(k, v) for (k, v) in self.frequencies.items()])

        self.num_tries_nodes = queue.size()

        while queue.size() > 1:
            x = queue.del_min()
            y = queue.del_min()
            parent = Huffman.Node(0, x.freq + y.freq, x, y, )
            queue.insert(parent)

            self.num_tries_nodes += 1

        self.trie = queue.del_min()
Esempio n. 55
0
    def test_PriorityQueue_PopAllSmallerThan_LIstIsEmpty(self):
        priority_queue = PriorityQueue()        
        priority_queue.add("a", 2)
        priority_queue.add("b", 1)
        priority_queue.remove("b")

        l = priority_queue.pop_smaller_than(3)
        
        self.assertEquals(list(priority_queue), [])
Esempio n. 56
0
def aStar(grid_map, source):
    grid_max = np.amax(grid_map)
    if grid_max == 0:
        grid_max = 1
    def heuristic(current):
        (t,y) = current
        return NUM_GRIDS_Y-y

    def neighbors(current):
        res = []
        (t,y) = current
        if t+1 < grid_map.shape[1]:
            num_offset = int(round(CHICKEN_MAX_SPEED/GRID_Y))
            for yy in range(-num_offset, num_offset+1):
                neighbor = (t+1, y+yy)
                if neighbor[1] < NUM_GRIDS_Y and neighbor[1] >= 0:
                    res.append(neighbor)
        return res

    def checkGoal(current):
        (t, y) = current
        if (y >= NUM_GRIDS_Y - EXTRA_GRIDS):
            return True
        return False

    frontier = PriorityQueue()
    frontier.put(source, 0)
    came_from = {}
    cost_so_far = {}
    came_from[source] = None
    cost_so_far[source] = 0

    while not frontier.empty():
        current = frontier.pop()

        if checkGoal(current):
            break

        for next in neighbors(current):
            new_cost = cost_so_far[current] + (np.exp(COEFF*grid_map[next[1]][next[0]]/grid_max)-1)/EXP_COEFF*NUM_GRIDS_Y
            if next not in cost_so_far or new_cost < cost_so_far[next]:
                cost_so_far[next] = new_cost
                priority = new_cost + heuristic(next)
                frontier.put(next, priority)
                came_from[next] = current

    goal=sorted(came_from.keys())[-1]
    return came_from, goal
Esempio n. 57
0
    def __init__(self, root_node, goal_node):
        """
        Initializes the AStar object and sets function that determines the
        order in which the nodes are evaluated.

        Precondition: nodes can be any type of object, but must be able to
        be compared by str(node1) == str(node2)
        """
        self.goal_node = goal_node

        # min heap for the fringe
        self.fringe = PriorityQueue()
        self.fringe.add_node( root_node, self.h_score(root_node) )

        # min cost to node
        self.min_node_score = {}
        self.min_node_score[ str(root_node) ] = 0

        # path to start
        self.came_from = {}

        self.nodes_visited = 0
Esempio n. 58
0
    def search(self):
        pq = PriorityQueue(self.heuristic)
        pq.put((self.start, []))
        while not pq.empty():
            self.space = max(self.space, len(pq))

            state, path = pq.get()
            self.nodes_visited += 1

            if state.is_goal():
                yield path
            for move, board in state.successors():
                if self.check_duplicates and board in self.visited:
                    continue
                elif self.check_duplicates:
                    self.visited += [board]

                    if self.check_symmetrical:
                        self.visited += board.get_symmetrically_equivalent_boards()

                pq.put((board, path + [move]))