Examples of scripts

These scripts summarize what is said in Getting started. All these scripts are available in the examples folder of the package.

Basics

# This is a simple script to automatically design a maze:
    
from vmfactory import Vmaze_NHT
from vmfactory.canvas import squares_grid

canvas = squares_grid(4,4) # nodes will be numbered 0..15
# NHT means no half-turns (can't pass a light twice in a row) 
maze = Vmaze_NHT(canvas, start = 0, goal = 15)
maze.colorize( maze.random_colors() )
maze.anneal(400,20) # optimize the maze
maze.make_report().savefig('myreport.png')
maze.to_file('my_nice_maze.vm')

Canvas editor

# This script shows how to make a canvas with the editor,
# save it, and load it later ::
    
from vmfactory import Vmaze
from vmfactory.canvas import canvas_editor
canvas = canvas_editor() # will open an interactive session.
# For when the session has ended:
maze = Vmaze(canvas)
maze.to_file('mycanvas.can')

# Later:
from vmfactory import Vmaze_NHT
maze_nht = Vmaze_NHT.from_file('mycanvas.can')
maze_nht.draw_quick()

Custom optimization

# This script shows how to evolve mazes with custom criteria.
# We create a subclass ``Vmaze_NHT_long`` where the score is
# simply the length of a minimal solution of the labyrinth is
#there is one. Note that we d'ont even check the unicity of
#sthe solution. ::

import networkx as nx
from vmfactory import Vmaze_NHT
from vmfactory.canvas import squares_grid

class Vmaze_NHT_long(Vmaze_NHT):
    """ A class that will tend to optimize mazes so that
        their solution is very long. The unicity of the
        solution is not granted. """
    
    def compute_score(self):
        
        graph = self.compute_graph()
        if self.start not in graph.nodes(): return 0
        shortest = nx.shortest_path(graph, self.start)
        if graph.goal not in shortest.keys(): return 0
        return len(shortest[graph.goal])
        
canvas = squares_grid(4,4)
maze = Vmaze_NHT_long(canvas, start = 0, goal = 15)
maze.colorize( maze.random_colors() )
maze.anneal(200,20) # optimize the maze
maze.make_report().savefig('myreport.png')

Cinematic of the evolution

# The next script shows how to make an animation of a
# maze being optimized (requires a recent Matplotlib
# and ImageMagick): ::

import os
import networkx as nx
from matplotlib import animation
import matplotlib.pyplot as plt
from vmfactory import Vmaze_NHT
from vmfactory.canvas import stacked_cubes


input_name_format = "_tmp_evograph_%03d.vm"
 
G = Vmaze_NHT(** stacked_cubes())
G.start, G.goal = 0, max(G.nodes())
G.colorize( G.random_colors() )

counter = 0
score = G.score
for i in range(100):
    G.improve(100,max(.1,0.3*(1-1.0*i/400.0)))
    if G.score != score:
        score = G.score
        G.to_file(input_name_format%counter)
        counter += 1


fig, axes = plt.subplots(1,3, figsize=(10,3))

def animate(nframe):
    G = Vmaze_NHT.from_file(input_name_format % nframe)
    fig = G.make_report(axes=axes)
    fig.savefig("_tmp_%02d.jpeg"%nframe)

anim = animation.FuncAnimation(fig, animate, frames=counter)
anim.save("test.gif", fps=.5, writer="imagemagick")

Result:

http://i.imgur.com/yc1lwgh.gif

Table Of Contents

Related Topics

This Page