def test_priority_queue_empty(make_jobs):
    pq = PriorityQueue()
    assert pq.jobs == []
    assert pq._operations == 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]
Beispiel #2
0
def search(start: Vertex.Vertex, graph: Graph.Graph):
    unvisited_set = set(graph.vert_dict.values())
    goal = set(graph.vert_dict.values())
    f_score = collections.defaultdict(lambda: sys.maxsize)
    visited_set = set()
    f_score[start] = 0
    open_list = PriorityQueue.PriorityQueue()
    open_list.add_task(start)
    came_from = dict()

    while open_list:
        current = open_list.pop_task()
        visited_set.add(current)
        unvisited_set.remove(current)

        if visited_set == goal:
            path = generate_path.reconstruct_path(came_from, current)
            path.append(list(graph.vert_dict.values())[0])
            return path

        for x in unvisited_set:
            tentative_fscore = graph.vert_dict[current.get_id()].get_weight(
                x) + mst_heuristic(start, current, unvisited_set)
            if x not in open_list or x not in visited_set:
                f_score[x] = tentative_fscore
                open_list.add_task(x, f_score[x])

            if x in open_list or x in visited_set and tentative_fscore < f_score[
                    x]:
                came_from[x] = current
                f_score[x] = tentative_fscore
                if (x in visited_set):
                    unvisited_set.add(x)

    return None
def test_enqueue(items, expected_head):
    queue = PriorityQueue()
    for item in items:
        queue.enqueue(item)

    assert len(queue) == len(items)
    assert queue.peek() == expected_head
def test_iter():
    queue = PriorityQueue()
    queue.enqueue("a")
    queue.enqueue("c")
    queue.enqueue("b")

    assert [item for item in queue] == ["c", "b", "a"]
def test_dequeue_empty_queue():
    queue = PriorityQueue()

    with pytest.raises(IndexError) as exc:
        queue.dequeue()

    assert str(exc.value) == "dequeue from empty priority queue"
def test_dequeue():
    queue = PriorityQueue()
    queue.enqueue("a")
    queue.enqueue("b")

    assert queue.dequeue() == "b"
    assert len(queue) == 1
def test_peek_empty_queue():
    queue = PriorityQueue()

    with pytest.raises(IndexError) as exc:
        queue.peek()

    assert str(exc.value) == "peek from empty priority queue"
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]
Beispiel #9
0
def search(start: Vertex.Vertex, goal: Vertex.Vertex, graph: Graph.Graph, f, g, h):
    f_score = dict();
    f_score[start] = h(start, goal);
    g_score = collections.defaultdict(lambda: sys.maxsize);
    g_score[start] = 0;
    open_list = PriorityQueue.PriorityQueue();
    open_list.add_task(start);
    came_from = dict();
    discovered = collections.defaultdict(lambda: False);
    discovered[start] = True;

    while open_list:
        current = open_list.pop_task();
        discovered[current] = True;

        if(current == goal):
            return generate_path.reconstruct_path(came_from, current);

        neighbors = current.get_neighbors();
        for x in neighbors:

            tentative_gScore =  g_score[current] + g(current, x);
            if tentative_gScore < g_score[x]:
                came_from[x] = current;
                g_score[x] = tentative_gScore;
                f_score[x] = f(g_score[x], h(x, goal));

                if not discovered[x]:
                    open_list.add_task(x, f_score[x]);
    return None;
Beispiel #10
0
def search(start, goal, map, f, g, h):
    f_score = dict()
    f_score[start] = h(start, goal)
    g_score = collections.defaultdict(lambda: sys.maxsize)
    g_score[start] = 0
    open_list = PriorityQueue.PriorityQueue()
    open_list.add_task(start)
    came_from = dict()
    discovered = collections.defaultdict(lambda: False)
    discovered[start] = True

    while open_list:
        current = open_list.pop_task()
        discovered[current] = True

        if (current == goal):
            return generate_path.reconstruct_path(came_from, current)

        neighbors = current.get_neighbors()
        for x in neighbors:

            tentative_gScore = g_score[current] + g(current, x)
            if tentative_gScore < g_score[x]:
                came_from[x] = current
                g_score[x] = tentative_gScore
                f_score[x] = f(g_score[x], h(x, goal))

                if not discovered[x]:
                    open_list.add_task(x, f_score[x])
    return None
def test_peek():
    queue = PriorityQueue()
    queue.enqueue("a")
    queue.enqueue("b")

    assert queue.peek() == "b"
    assert len(queue) == 2
Beispiel #12
0
    def test_dequeue(self):
        queue = PriorityQueue()
        priority_item_one = PriorityItem("item1", 1)
        priority_item_two = PriorityItem("item2", 3)
        priority_item_three = PriorityItem("item3", 99)
        queue.enqueue(priority_item_one)
        front_item = queue.front()

        assert len(queue) == 1
        assert front_item.value == priority_item_one.value
        assert front_item.priority == priority_item_one.priority

        dequed_value = queue.dequeue()

        assert len(queue) == 0
        assert dequed_value.value == priority_item_one.value
        assert dequed_value.priority == priority_item_one.priority
        assert queue.dequeue() == None

        queue.enqueue(priority_item_three)
        queue.enqueue(priority_item_one)
        queue.enqueue(priority_item_two)

        assert queue.dequeue().value == priority_item_three.value
        assert queue.dequeue().value == priority_item_two.value
        assert queue.dequeue().value == priority_item_one.value
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._operations = 0
    pq._operations = 0
    return pq
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._operations = 4
    new_pq.pop()
    assert [job.priority for job in new_pq.jobs] == [6, 1]
Beispiel #15
0
def dijkstra(aGraph, start):
    pq = PriorityQueue()
    start.set_distance(0)
    pq.build_heap([(v, v.get_distance()) for v in aGraph])
    while not pq.is_empty():
        current_vert = pq.dequeue()[0]
        for next_vert in current_vert.get_connections():
            new_dist = current_vert.get_distance() + current_vert.get_weight(
                next_vert)
            if new_dist < next_vert.get_distance():
                next_vert.set_distance(new_dist)
                next_vert.set_pred(current_vert)
                pq.decrease_priority(next_vert, new_dist)
Beispiel #16
0
def prim(aGraph, start):
    pq = PriorityQueue()
    for v in aGraph:
        v.set_distance(maxsize)
        v.set_pred(None)
    start.set_distance(0)
    pq.build_heap([(v, v.get_distance()) for v in aGraph])
    while not pq.is_empty():
        current_vert = pq.dequeue()[0]
        for next_vert in current_vert.get_connections():
            new_cost = current_vert.get_weight(next_vert)
            if next_vert in pq and new_cost < next_vert.get_distance():
                next_vert.set_distance(new_cost)
                next_vert.set_pred(current_vert)
                pq.decrease_priority(next_vert, new_cost)
Beispiel #17
0
def mst(g: Graph.Graph):
    min_tree = Graph.Graph()
    open_list = PriorityQueue.PriorityQueue()
    cost = collections.defaultdict(lambda: sys.maxsize)
    for x in g.vert_dict.values():
        open_list.add_task(x, cost[x])
    edge = collections.defaultdict(lambda: False)

    while open_list:
        current = open_list.pop_task()
        min_tree.add_vertex(current.get_id())
        if edge[current]:
            min_tree.add_edge(current.get_id(), edge[current][0].get_id(),
                              edge[current][1])
        for x in current.get_neighbors():
            if x in open_list and current.get_weight(x) < cost[x]:
                cost[x] = current.get_weight(x)
                edge[x] = (current, cost[x])
                open_list.add_task(x, cost[x])
    return min_tree
Beispiel #18
0
    def test_enqueue(self):
        queue = PriorityQueue()
        priority_item_one = PriorityItem("item1", 1)
        priority_item_two = PriorityItem("item2", 1)

        queue.enqueue(priority_item_one)
        queue.enqueue(priority_item_two)
        front = queue.front()

        assert len(queue) == 2
        assert front.value == priority_item_one.value
        assert front.priority == priority_item_one.priority

        priority_item_three = PriorityItem("item3", 2)
        queue.enqueue(priority_item_three)
        front = queue.front()

        assert len(queue) == 3
        assert front.value == priority_item_three.value
        assert front.priority == priority_item_three.priority
def test_empty_pop():
    pq = PriorityQueue()
    with pytest.raises(IndexError):
        pq.pop()
def test_priority_queue_sequence(make_jobs):
    pq = PriorityQueue(make_jobs)
    temp = [p.priority for p in pq.jobs]
    assert temp == [9, 9, 9, 7, 8, 3, 7, 3, 7, 4]