def hidden_image_maze(fname, style='jittery'): """ Supported styles: jittery, smooth, sketch""" H = models.image_grid_graph(fname) # get a subgraph of the grid corresponding to edges between black pixels G = H.base_graph # for every edge in H, make the corresponding edge in H have weight 0 for u,v in H.edges(): G[u][v]['weight'] = 0 # find a minimum spanning tree on G (which will include the maze solution) T = nx.minimum_spanning_tree(G) # find the maze solution in the spanning tree P = models.my_path_graph(nx.shortest_path(T, (0,0), max(H.nodes()))) # generate the dual graph, including edges not crossed by the spanning tree D = models.dual_grid(G, T) views.add_maze_boundary(D, max(G.nodes())) views.make_entry_and_exit(D, max(G.nodes())) pos = views.layout_maze(D, fast=(style == 'jittery')) views.plot_maze(D, pos, P, G.pos) # make it stylish if requested if style == 'sketch': plt.figure(1) D_pos = views.layout_maze(D, fast=True) nx.draw_networkx_edges(D, D_pos, width=1, edge_color='k') D_pos = views.layout_maze(D, fast=True) nx.draw_networkx_edges(D, D_pos, width=1, edge_color='k') # show the pixel colors loaded from the file, for "debugging" plt.figure(2) for v in G: plt.plot([G.pos[v][0]], [G.pos[v][1]], '.', alpha=.5, color=G.node[v]['color'])
def random_maze(n=25): G = models.my_grid_graph([n,n]) T = nx.minimum_spanning_tree(G) P = models.my_path_graph(nx.shortest_path(T, (0,0), (n-1, n-1))) D = models.dual_grid(G, T) views.add_maze_boundary(D, [n, n]) views.make_entry_and_exit(D, [n, n]) pos = views.layout_maze(D, fast=True) views.plot_maze(D, pos, P, G.pos)
def random_maze(n=25): G = models.my_grid_graph([n, n]) T = nx.minimum_spanning_tree(G) P = models.my_path_graph(nx.shortest_path(T, (0, 0), (n - 1, n - 1))) D = models.dual_grid(G, T) views.add_maze_boundary(D, [n, n]) views.make_entry_and_exit(D, [n, n]) pos = views.layout_maze(D, fast=True) views.plot_maze(D, pos, P, G.pos)
def test_graph_utils(self): P = model.my_path_graph(model.nx.shortest_path(self.G, (0,0), (4,4))) H = model.image_grid_graph('test.png') d = model.dual_grid_edge((0,0), (0,1)) assert d == ((-0.5, 0.5), (0.5, 0.5)), 'dual of integer lattice should be offset by .5s' D = model.dual_grid(H.base_graph, H) graphics.add_maze_boundary(D, [5,5]) graphics.make_entry_and_exit(D, [5,5]) HH = graphics.split_edges(H)
def test_graph_utils(self): P = model.my_path_graph(model.nx.shortest_path(self.G, (0, 0), (4, 4))) H = model.image_grid_graph('test.png') d = model.dual_grid_edge((0, 0), (0, 1)) assert d == ((-0.5, 0.5), (0.5, 0.5)), 'dual of integer lattice should be offset by .5s' D = model.dual_grid(H.base_graph, H) graphics.add_maze_boundary(D, [5, 5]) graphics.make_entry_and_exit(D, [5, 5]) HH = graphics.split_edges(H)
def hidden_image_maze(fname, style='jittery'): """ Supported styles: jittery, smooth, sketch""" H = models.image_grid_graph( fname ) # get a subgraph of the grid corresponding to edges between black pixels G = H.base_graph # for every edge in H, make the corresponding edge in H have weight 0 for u, v in H.edges(): G[u][v]['weight'] = 0 # find a minimum spanning tree on G (which will include the maze solution) T = nx.minimum_spanning_tree(G) # find the maze solution in the spanning tree P = models.my_path_graph(nx.shortest_path(T, (0, 0), max(H.nodes()))) # generate the dual graph, including edges not crossed by the spanning tree D = models.dual_grid(G, T) views.add_maze_boundary(D, max(G.nodes())) views.make_entry_and_exit(D, max(G.nodes())) pos = views.layout_maze(D, fast=(style == 'jittery')) views.plot_maze(D, pos, P, G.pos) # make it stylish if requested if style == 'sketch': plt.figure(1) D_pos = views.layout_maze(D, fast=True) nx.draw_networkx_edges(D, D_pos, width=1, edge_color='k') D_pos = views.layout_maze(D, fast=True) nx.draw_networkx_edges(D, D_pos, width=1, edge_color='k') # show the pixel colors loaded from the file, for "debugging" plt.figure(2) for v in G: plt.plot([G.pos[v][0]], [G.pos[v][1]], '.', alpha=.5, color=G.node[v]['color'])
def ld_maze(n=25): """ having many low-degree vertices makes for hard mazes unfortunately, finding them is slow""" # start with an nxn square grid G = models.my_grid_graph([n,n]) # make a pymc model of a low-degree spanning tree on this T = models.LDST(G, beta=10) mod_mc = mc.MCMC([T]) mod_mc.use_step_method(models.STMetropolis, T) mod_mc.sample(100, burn=99) T = T.value P = models.my_path_graph(nx.shortest_path(T, (0,0), (n-1, n-1))) D = models.dual_grid(G, T) views.add_maze_boundary(D, [n,n]) views.make_entry_and_exit(D, [n,n]) D = views.split_edges(D) D = views.split_edges(D) D_pos = views.layout_maze(D, fast=False) views.plot_maze(D, D_pos, P, G.pos)
def ld_maze(n=25): """ having many low-degree vertices makes for hard mazes unfortunately, finding them is slow""" # start with an nxn square grid G = models.my_grid_graph([n, n]) # make a pymc model of a low-degree spanning tree on this T = models.LDST(G, beta=10) mod_mc = mc.MCMC([T]) mod_mc.use_step_method(models.STMetropolis, T) mod_mc.sample(100, burn=99) T = T.value P = models.my_path_graph(nx.shortest_path(T, (0, 0), (n - 1, n - 1))) D = models.dual_grid(G, T) views.add_maze_boundary(D, [n, n]) views.make_entry_and_exit(D, [n, n]) D = views.split_edges(D) D = views.split_edges(D) D_pos = views.layout_maze(D, fast=False) views.plot_maze(D, D_pos, P, G.pos)