Пример #1
0
def generate_guided_search_figure(G, positions, src, target):
    """Generate Guided Search solution .. ultimately omitted from book."""
    if plt_error:
        return None
    import matplotlib.pyplot as plt

    (G, positions, _) = tmg_load(highway_map())
    plt.clf()
    plot_gps(positions)
    plot_highways(positions, G.edges())

    def distance_gps(from_cell, to_cell):
        """These ids are indexed into positions to get GPS coordinates."""
        return abs(positions[from_cell][0] -
                   positions[to_cell][0]) + abs(positions[from_cell][1] -
                                                positions[to_cell][1])

    node_from = guided_search(G, src, target, distance=distance_gps)
    total = compute_distance(positions, node_from, src, target)

    plot_node_from(positions, src, target, node_from, color='purple')
    print(
        '{0} total steps for Guided Search with distance={1:.1f} miles'.format(
            len(path_to(node_from, src, target)) - 1, total))
    plt.axis('off')
    output_file = image_file('figure-mass-highway-guided.svg')
    plt.savefig(output_file, format="svg")
    print(output_file)
    plt.clf()
    return output_file
Пример #2
0
def generate_bfs_and_dijkstra_figure(src, target):
    """Generate BFS solution overlaying Massachusetts highway."""
    if plt_error:
        return None
    import matplotlib.pyplot as plt

    (G, positions, _) = tmg_load(highway_map())
    (dist_to, edge_to) = dijkstra_sp(G, src)
    print('Dijkstra shortest distance is {} total steps with distance={:.1f}'.
          format(
              len(edges_path_to(edge_to, src, target)) - 1, dist_to[target]))
    path = edges_path_to(edge_to, src, target)
    plt.clf()
    plot_gps(positions)
    plot_highways(positions, G.edges())
    plot_path(positions, path)
    node_from = bfs_search(G, src)
    total = compute_distance(positions, node_from, src, target)

    plot_node_from(positions, src, target, node_from, color='purple')
    print(
        '{0} total steps for Breadth First Search with distance={1:.1f} miles'.
        format(len(path_to(node_from, src, target)) - 1, total))
    plt.axis('off')
    output_file = image_file('figure-mass-highway-bfs.svg')
    plt.savefig(output_file, format="svg")
    print(output_file)
    plt.clf()
    return output_file
Пример #3
0
    def test_small_example(self):
        from ch07.search import dfs_search, path_to
        from ch07.challenge import path_to_recursive
        G = nx.Graph()
        self.small_example(G)

        node_from = dfs_search(G, 'A2')
        self.assertEqual(['A2', 'A3', 'A4', 'A5'],
                         path_to(node_from, 'A2', 'A5'))
        self.assertEqual(['A2', 'A3', 'A4', 'A5'],
                         list(path_to_recursive(node_from, 'A2', 'A5')))

        with self.assertRaises(ValueError):
            path_to(node_from, 'A2', 'B2')  # No path exists
        with self.assertRaises(ValueError):
            # No path exists: force issue by list(...)
            list(path_to_recursive(node_from, 'A2', 'B2'))
Пример #4
0
    def test_guided_search(self):
        from ch07.maze import Maze, to_networkx, solution_graph, distance_to
        from ch07.search import guided_search, path_to, node_from_field
        import random
        random.seed(15)
        m = Maze(3, 5)
        G = to_networkx(m)

        # BFS search solution
        node_from = guided_search(G, m.start(), m.end(), distance_to)
        self.assertEqual((1, 2), node_from[(2, 2)])

        # Create graph resulting from the BFS search results
        F = node_from_field(G, node_from)
        self.assertEqual(14, len(list(F.edges())))

        # The actual solution is a two-edge, three node straight path
        H = solution_graph(G, path_to(node_from, m.start(), m.end()))
        self.assertEqual(2, len(list(H.edges())))
Пример #5
0
def generate_dfs_figure(src, target):
    """Generate DFS solution overlaying Massachusetts highway."""
    if plt_error:
        return None
    import matplotlib.pyplot as plt

    (G, positions, _) = tmg_load(highway_map())
    plt.clf()
    plot_gps(positions)
    plot_highways(positions, G.edges())

    node_from = dfs_search_recursive(G, src)
    total = compute_distance(positions, node_from, src, target)

    plot_node_from(positions, src, target, node_from, color='purple')
    print('{0} total steps for Depth First Search with distance={1:.1f} miles'.
          format(len(path_to(node_from, src, target)) - 1, total))
    plt.axis('off')
    output_file = image_file('figure-mass-highway-dfs.svg')
    plt.savefig(output_file, format="svg")
    print(output_file)
    plt.clf()
    return output_file