Reference manual



A canvas made of losanges reminding stacked cubes. To see what it looks like, type:

>>> Vmaze(** diamond() ).draw_quick()
A canvas representing a six-branches star.
Clearly not the best canvas for a Viennese Maze.

To see what it looks like, type:

>>> Vmaze(** star() ).draw_quick()

A canvas made of losanges reminding stacked cubes. To see what it looks like, type:

>>> Vmaze(**stacked_cubes() ).draw_quick()
vmfactory.canvas.squares_grid(x_grid=4, y_grid=4)

Generates a canvas mades of a squared grid.


>>> canvas = squares_canvas(4,4) # produces
>>>            +-+-+-+
>>>            | | | |
>>>            +-+-+-+
>>>            | | | |
>>>            +-+-+-+
>>>            | | | |
>>>            +-+-+-+
>>> G = Vmaze_NHT(canvas, start=0, goal=15)
vmfactory.canvas.canvas_editor(grid=True, grid_N=12)

This function enables to draw a graph manually using Matplotlib’s interactive plotting capabilites.

In a first phase you are asked to place the nodes (left-click to place a node, right-click to remove the last node, Enter when finished). In a second phase you are asked to place the edges ( click on two nodes to select an edge, remove an edge by selecting it again, Enter when finished). To go faster in this phase you can also select an edge by clicking just one node and hitting enter, it will be assumed that the first node was the last node of the previous edge. This enables to rapidly draw “paths” of edges.


grid :

If True, the points positions are a posteriori projected on a grid for better visual appeal

grid_N :

resolution of the grid, if any.


graph :

A networkx graph object representing the canvas


>>> from vmfactory.canvas import canvas_editor
>>> G = canvas_editor()
>>> vmaze = Vmaze(G, start=0, goal=1)
>>> vmaze.draw_quick() # draw with matplotlib
>>> print vmaze # print the edges, nodes positions.
>>> vmaze.to_file('mycanvas.can') # save for later use


Base class

class vmfactory.Vmaze(canvas, start=None, goal=None, nodes_pos=None)

Base Class for Viennese Mazes with the necessary methods to automatically generate mazes (initialization, optimization, drawing).

Notes that this does not implement states graph and score computation.

Refer to classes Vmaze_NHT and Vmaze_HT for directly usable classes.

For the special class of Viennese Mazes with no half-turns (meaning that you cannot pass the same light twice in a row)


canvas :

A Networkx graph object or a list of edges which form the canvas to be colored.

start :

Node of the canvas from which the maze starts

goal :

Node of the maze that must be reached.

nodes_pos :

Position of the nodes when drawing the graph.


>>> # In this example we initialize a maze from a canvas,
>>> # optimize the colors, and generate a report. 
>>> from vmfactory import Vmaze, squares_canvas
>>> canvas = squares_canvas(4,4) 
>>> maze = Vmaze(canvas, start = 0, goal = 15)
>>> maze.colorize( maze.random_colors() )
>>> maze.anneal(100,20) # optimize the maze
>>> maze.make_report()



Returns the maze’s solution as a path in the state graph.


Makes the solution of Vmaze.format_solution() even more readable, as a string.


Computes a score for the maze.

This is an example of scoring function which implements a few of my favorite criteria for a good maze:

  • The solution is unique and long.
  • There are plenty of loops and bagends.
  • Many openings, many false endings which make the maze difficult to solve backwards.

This is all very subjective and incomplete. To grow mazes according to other criteria, overwrite this function in a subclass of Vmaze.


Sets the colors of the maze’s edges.


Generates a vector of random colors for the maze.


Randomly changes some colors of the maze, each color light having a probability proba_change to be changed.

improve(n=1, proba_change=0.5)

Improves the maze through random color changes.

Each traffic light has a probability proba_change of being modified to create the new maze. If the new maze has a score lower than the current maze it is dumped, otherwise it replaces the current maze.

This procedure is carried over n times.

anneal(n, k=10)

Evolves a maze using annealing (cooldown).

For each i in 0..n-1, the method “improve()” will be called k times with a probability of mutation 1.0*(n-i)/n.


Quick drawing of the maze.

draw_fancy(ax=None, draw_lights=True)

Fancy drawing of the maze.

Draws the maze with lights in a fancy way, with marks on the streets, letters instead of numbers to label the nodes (by default), etc... draw_light determines whether a circle of the corresponding color is drawn in the middle of each edge.

draw_graph(ax=None, node_size=8)

Draws the state graph of the maze in a fancy way.

draw_solution(ax, node_size=30, shift=[1, 1], print_solution=False)

Draws the solution of the maze in a fancy way.


ax :

A matplotlib ax

node_size :

Size of the nodes, obviously.

shift :

Vector (x,y) indicating the direction in which the solution line will be slightly shifted when the solution passes more than one time by the same node.

print_solution :

If true, the solution (sequence of letters designating the different nodes, like a-b-g-e-f-m) is written down as the title of the ax.

make_report(axes=None, figsize=(10, 3))

Makes a full report of the maze (colors, graph, solution)

This is not very flexible but practical for quick scripts.

static from_file(filename)

Reads a labyrinth from a file.


Saves the labyrinth to a file


class vmfactory.Vmaze_HT(canvas, start=None, goal=None, nodes_pos=None)



Computes the states graph of the maze.

This function is useful to solve and score the maze.


Make the solution of Vmaze.solve() human-readable.

This function differs in classes Vmaze and Vmaze_NHT.


class vmfactory.Vmaze_NHT(canvas, start=None, goal=None, nodes_pos=None)

A special class of Viennese Maze in which half-turns are not allowed (you cannot go twice in a row through the same traffic light).

See Vmaze for more doc.



Computes the states maze of the graph.


Solve the maze using a recursive function.

This is slow (slower than solving using the states graph) and not very useful, but it was nice to code.

Table Of Contents

Related Topics

This Page