当前位置: 首页>>代码示例>>Python>>正文


Python PriorityQueue.extend方法代码示例

本文整理汇总了Python中queue.PriorityQueue.extend方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.extend方法的具体用法?Python PriorityQueue.extend怎么用?Python PriorityQueue.extend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在queue.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.extend方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_compute_shortest_path_helper

# 需要导入模块: from queue import PriorityQueue [as 别名]
# 或者: from queue.PriorityQueue import extend [as 别名]
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
开发者ID:jakebarnwell,项目名称:incremental-path-planning,代码行数:38,代码来源:tests.py

示例2: test_update_vertex_helper

# 需要导入模块: from queue import PriorityQueue [as 别名]
# 或者: from queue.PriorityQueue import extend [as 别名]
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
    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
    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
开发者ID:jakebarnwell,项目名称:incremental-path-planning,代码行数:93,代码来源:tests.py


注:本文中的queue.PriorityQueue.extend方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。