def test_update_vertex_helper(update_vertex_helper): print("Testing update_vertex_helper...") # Test 1: triangle problem; A not in queue, don't insert g = dict(A=9, B=10, C=2) g_original = g.copy() rhs = dict(A="value_to_be_overwritten") graph = graph_triangle.copy() queue = PriorityQueue() #empty update_vertex_helper("A", g, rhs, "non-existent_goal_node", graph, queue) assert g == g_original #not modified assert rhs == dict(A=9) assert graph == graph_triangle #not modified assert queue == PriorityQueue() # Test 2: triangle problem; remove A from queue and don't re-insert g = dict(A=9, B=10, C=2) g_original = g.copy() rhs = dict(A="value_to_be_overwritten") graph = graph_triangle.copy() queue = PriorityQueue() #empty queue.extend(list("XYAZ")) expected_queue = queue.copy() expected_queue.remove("A") update_vertex_helper("A", g, rhs, "non-existent_goal_node", graph, queue) assert g == g_original #not modified assert rhs == dict(A=9) assert graph == graph_triangle #not modified #problem here assert queue == expected_queue # Test 3: triangle problem; A not in queue, do insert g = dict(A=8, B=10, C=2) g_original = g.copy() rhs = dict(A=0) graph = graph_triangle.copy() queue = PriorityQueue() #empty queue.extend(list("XYZ")) expected_queue = queue.copy() expected_queue.insert("A") update_vertex_helper("A", g, rhs, "non-existent_goal_node", graph, queue) assert g == g_original #not modified assert rhs == dict(A=9) assert graph == graph_triangle #not modified assert queue == expected_queue # Test 4: triangle problem; A in queue, remove and re-insert g = dict(A=20, B=10, C=2) g_original = g.copy() rhs = dict(B="other_stuff") graph = graph_triangle.copy() queue = PriorityQueue() #empty queue.extend(list("XYAZ")) expected_queue = queue.copy() update_vertex_helper("A", g, rhs, "non-existent_goal_node", graph, queue) assert g == g_original #not modified assert rhs == dict(A=9, B="other_stuff") assert graph == graph_triangle #not modified #problem here assert queue == expected_queue # Test 5: triangle problem; B has only one successor g = dict(A=1, B=100, C=10) g_original = g.copy() rhs = dict() queue = PriorityQueue() #empty expected_queue = queue.copy() expected_queue.insert("B") update_vertex_helper("B", g, rhs, "non-existent_goal_node", graph_triangle.copy(), queue) assert rhs == dict(B=13) assert queue == expected_queue # Test 6: triangle problem; B is goal g = dict(A=1, B=0, C=10) g_original = g.copy() rhs = dict(B=0) queue = PriorityQueue() #empty update_vertex_helper("B", g, rhs, "B", graph_triangle.copy(), queue) assert rhs == dict(B=0) assert queue == PriorityQueue() # Test 7: bidirectional graph from problem_simple g = { (1, 2): inf, (0, 1): inf, (3, 2): inf, (0, 0): inf, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): inf, (0, 2): 2.0, (2, 0): 0, (1, 3): 5, (2, 3): inf, (2, 1): 1.0, (2, 2): inf, (1, 0): inf, (4, 2): inf, (0, 3): inf, (0, 4): inf, (4, 1): inf, (1, 1): 1.0, (4, 0): inf } rhs = { (1, 2): inf, (0, 1): 2.0, (3, 2): inf, (0, 0): 2.0, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): inf, (0, 2): 2.0, (2, 0): 0, (1, 3): 9, (2, 3): inf, (2, 1): 1.0, (2, 2): 2.0, (1, 0): 1.0, (4, 2): inf, (0, 3): inf, (0, 4): inf, (4, 1): inf, (1, 1): 1.0, (4, 0): inf } queue = PriorityQueue() queue.extend([(1, 0), (0, 1), (2, 2), (0, 0)]) expected_rhs = { (1, 2): inf, (0, 1): 2.0, (3, 2): inf, (0, 0): 2.0, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): inf, (0, 2): 2.0, (2, 0): 0, (1, 3): inf, (2, 3): inf, (2, 1): 1.0, (2, 2): 2.0, (1, 0): 1.0, (4, 2): inf, (0, 3): inf, (0, 4): inf, (4, 1): inf, (1, 1): 1.0, (4, 0): inf } expected_queue = queue.copy() expected_queue.insert((1, 3)) update_vertex_helper((1, 3), g, rhs, goal_simple, graph_simple, queue) assert rhs == expected_rhs assert queue == expected_queue
def test_compute_shortest_path_helper(compute_shortest_path_helper, calc_key_helper): print("Testing compute_shortest_path_helper...") # problem_simple, first step g = { (1, 2): inf, (0, 1): inf, (3, 2): inf, (0, 0): inf, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): inf, (0, 2): inf, (2, 0): inf, (1, 3): inf, (2, 3): inf, (2, 1): inf, (2, 2): inf, (1, 0): inf, (4, 2): inf, (0, 3): inf, (0, 4): inf, (4, 1): inf, (1, 1): inf, (4, 0): inf } rhs = { (1, 2): inf, (0, 1): inf, (3, 2): inf, (0, 0): inf, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): inf, (0, 2): inf, (2, 0): 0, (1, 3): inf, (2, 3): inf, (2, 1): inf, (2, 2): inf, (1, 0): inf, (4, 2): inf, (0, 3): inf, (0, 4): inf, (4, 1): inf, (1, 1): inf, (4, 0): inf } graph = graph_simple.copy() calc_key = lambda node: calc_key_helper(node, g, rhs, (0, 3), 0) queue = PriorityQueue(f=lambda node: calc_key(node)) queue.insert(goal_simple) g_expected = { (1, 2): inf, (0, 1): inf, (3, 2): inf, (0, 0): inf, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): inf, (0, 2): 2.0, (2, 0): 0, (1, 3): inf, (2, 3): inf, (2, 1): 1.0, (2, 2): inf, (1, 0): inf, (4, 2): inf, (0, 3): 3.0, (0, 4): inf, (4, 1): inf, (1, 1): 1.0, (4, 0): inf } rhs_expected = { (1, 2): inf, (0, 1): 2.0, (3, 2): inf, (0, 0): 2.0, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): 4.0, (0, 2): 2.0, (2, 0): 0, (1, 3): inf, (2, 3): inf, (2, 1): 1.0, (2, 2): 2.0, (1, 0): 1.0, (4, 2): inf, (0, 3): 3.0, (0, 4): 4.0, (4, 1): inf, (1, 1): 1.0, (4, 0): inf } compute_shortest_path_helper(g, rhs, (0, 3), goal_simple, 0, graph, queue) assert graph == graph_simple #not modified assert g == g_expected assert rhs == rhs_expected queue_expected = PriorityQueue(f=lambda node: calc_key(node)) queue_expected.extend([(1, 0), (0, 1), (2, 2), (0, 0), (0, 4), (1, 4)]) assert queue == queue_expected # problem_simple, second step g = { (1, 2): inf, (0, 1): inf, (3, 2): inf, (0, 0): inf, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): inf, (0, 2): 2.0, (2, 0): 0, (1, 3): inf, (2, 3): inf, (2, 1): 1.0, (2, 2): inf, (1, 0): inf, (4, 2): inf, (0, 3): 3.0, (0, 4): inf, (4, 1): inf, (1, 1): 1.0, (4, 0): inf } rhs = { (1, 2): inf, (0, 1): 3.0, (3, 2): inf, (0, 0): inf, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): 4.0, (0, 2): 4.0, (2, 0): 0, (1, 3): inf, (2, 3): inf, (2, 1): 1.0, (2, 2): 2.0, (1, 0): 1.0, (4, 2): inf, (0, 3): 3.0, (0, 4): 4.0, (4, 1): inf, (1, 1): inf, (4, 0): inf } graph = graph_simple_step2.copy() calc_key = lambda node: calc_key_helper(node, g, rhs, (0, 2), 1) queue = PriorityQueue(f=lambda node: calc_key(node)) queue.extend([(1, 1), (0, 2), (1, 0), (2, 2), (0, 1), (0, 4), (1, 4)]) g_expected = { (1, 2): inf, (0, 1): 2.0, (3, 2): inf, (0, 0): inf, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): inf, (0, 2): 3.0, (2, 0): 0, (1, 3): inf, (2, 3): inf, (2, 1): 1.0, (2, 2): inf, (1, 0): 1.0, (4, 2): inf, (0, 3): 3.0, (0, 4): inf, (4, 1): inf, (1, 1): inf, (4, 0): inf } rhs_expected = { (1, 2): inf, (0, 1): 2.0, (3, 2): inf, (0, 0): 2.0, (3, 0): inf, (3, 1): inf, (2, 4): inf, (1, 4): 4.0, (0, 2): 3.0, (2, 0): 0, (1, 3): inf, (2, 3): inf, (2, 1): 1.0, (2, 2): 2.0, (1, 0): 1.0, (4, 2): inf, (0, 3): 4.0, (0, 4): 4.0, (4, 1): inf, (1, 1): inf, (4, 0): inf } compute_shortest_path_helper(g, rhs, (0, 2), goal_simple, 1, graph, queue) assert graph == graph_simple_step2 #not modified assert g == g_expected assert rhs == rhs_expected queue_expected = PriorityQueue(f=lambda node: calc_key(node)) queue_expected.extend([(0, 0), (2, 2), (0, 3), (0, 4), (1, 4)]) assert queue == queue_expected