Class: Simulation

Simulation(config)

Simulation is the global class of Cacatoo, containing the main configuration for making a grid-based model and displaying it in either browser or with nodejs.

Constructor

new Simulation(config)

The constructor function for a @Simulation object. Takes a config dictionary. and sets options accordingly.
Parameters:
Name Type Description
config dictionary A dictionary (object) with all the necessary settings to setup a Cacatoo simulation.
Source:

Methods

addButton(text, func)

addButton adds a HTML button which can be linked to a function by the user.
Parameters:
Name Type Description
text string Text displayed on the button
func function Function to be linked to the button
Source:

addCheckpointButton({@GridModel})

addCheckpointButton adds a button to the HTML environment which allows the user to reload the grid to the state as found in a JSON file saved by save_grid. The JSON file must of course match the simulation (nrows, ncols, properties in gps), but this is the users own responsibility.
Parameters:
Name Type Description
{@GridModel} targetgrid The gridmodel containing the grid to reload the grid.
Source:

addCustomSlider(func, minopt, maxopt, stepopt)

addCustomSlider adds a HTML slider to the DOM-environment which allows the user to add a custom callback function to a slider
Parameters:
Name Type Attributes Default Description
func function The name of the (global!) parameter to link to the slider
min float <optional>
0 Minimal value of the slider
max float <optional>
2 Maximum value of the slider
step float <optional>
0.01 Step-size when modifying
Source:

addHTML(div, html)

Adds some html to an existing DIV in your web page.
Parameters:
Name Type Description
div String Name of DIV to add to
html String HTML code to add
Source:

addObjectbrush(gridmodel, obj, brushsize, brushflow, canvas)

Add an object-drawing posibility to this canvas
Parameters:
Name Type Description
gridmodel Gridmodel The gridmodel to which this canvas belongs
obj object Replace current gp with this object
brushsize int radius of the brush
brushflow int amounts of substeps taken (1 by default)
canvas string alternative canvas name to draw on (first canvas by default)
Source:

addPatternButton({@GridModel}, property)

addPatternButton adds a pattern button to the HTML environment which allows the user to load a PNG which then sets the state of 'proparty' for the @GridModel. (currently only supports black and white image)
Parameters:
Name Type Description
{@GridModel} targetgrid The gridmodel containing the grid to be modified.
property String The name of the state to be set
Source:

addSlider(parameter, minopt, maxopt, stepopt)

addSlider adds a HTML slider to the DOM-environment which allows the user to modify a model parameter at runtime.
Parameters:
Name Type Attributes Default Description
parameter string The name of the (global!) parameter to link to the slider
min float <optional>
0 Minimal value of the slider
max float <optional>
2 Maximum value of the slider
step float <optional>
0.01 Step-size when modifying
Source:

addStatebrush(gridmodel, property, value_to_place, brushsize, brushflow)

Add a statedrawing posibility to this canvas
Parameters:
Name Type Description
gridmodel Gridmodel The gridmodel to which this canvas belongs
property string the property that should be shown on the canvas
value_to_place set @property to this value
brushsize int radius of the brush
brushflow int amounts of substeps taken (1 by default)
Source:

addToggle(parameter)

addToggle adds a HTML checkbox element to the DOM-environment which allows the user to flip boolean values
Parameters:
Name Type Description
parameter string The name of the (global!) boolean to link to the checkbox
Source:

colourRamp(arr1, arr2, n) → {dict}

colourRamp interpolates between two arrays to get a smooth colour scale.
Parameters:
Name Type Description
arr1 array Array of R,G,B values to start fromtargetgrid The gridmodel containing the grid to be modified.
arr2 array Array of R,B,B values to transition towards
n integer number of steps taken
Source:
Returns:
A dictionary (i.e. named JS object) of colours
Type
dict

createDisplay(name, property, customlab, height, width, scale)

Create a display for a gridmodel, showing a certain property on it.
Parameters:
Name Type Description
name string The name of an existing gridmodel to display
property string The name of the property to display
customlab string Overwrite the display name with something more descriptive
height integer Number of rows to display (default = ALL)
width integer Number of cols to display (default = ALL)
scale integer Scale of display (default inherited from @Simulation class)
Source:

createDisplay_continuous(config, name, property, customlab, height, width, scale, minval, maxval, nticks, decimals, num_colours, fill)

Create a display for a gridmodel, showing a certain property on it.
Parameters:
Name Type Description
config object Object with the keys name, property, label, width, height, scale, minval, maxval, nticks, decimals, num_colours, fill These keys:value pairs are:
name string The name of the model to display
property string The name of the property to display
customlab string Overwrite the display name with something more descriptive
height integer Number of rows to display (default = ALL)
width integer Number of cols to display (default = ALL)
scale integer Scale of display (default inherited from @Simulation class)
minval numeric colour scale is capped off below this value
maxval numeric colour scale is capped off above this value
nticks integer how many ticks
decimals integer how many decimals for tick labels
num_colours integer how many steps in the colour gradient
fill string type of gradient to use (viridis, inferno, red, green, blue)
Source:

createDisplay_discrete(config, name, property, customlab, height, width, scale, minval, maxval, nticks, decimals, num_colours, fill)

Create a display for a gridmodel, showing a certain property on it.
Parameters:
Name Type Description
config object Object with the keys name, property, label, width, height, scale, minval, maxval, nticks, decimals, num_colours, fill These keys:value pairs are:
name string The name of the model to display
property string The name of the property to display
customlab string Overwrite the display name with something more descriptive
height integer Number of rows to display (default = ALL)
width integer Number of cols to display (default = ALL)
scale integer Scale of display (default inherited from @Simulation class)
minval numeric colour scale is capped off below this value
maxval numeric colour scale is capped off above this value
nticks integer how many ticks
decimals integer how many decimals for tick labels
num_colours integer how many steps in the colour gradient
fill string type of gradient to use (viridis, inferno, red, green, blue)
Source:

display()

Display all the canvases linked to this simulation
Source:

events()

Apply global events to all grids in the model. (only perfectmix currently... :D)
Source:

getCursorPosition(canvas, event, scale)

Get the position of the cursor on the canvas
Parameters:
Name Type Description
canvas canvas A (constant) canvas object
event event-handler Event handler (mousedown)
scale scale The zoom (scale) of the grid to grab the correct grid point
Source:

initialGrid({@GridModel}, property, value, fraction)

initialGrid populates a grid with states. The number of arguments is flexible and defined the percentage of every state. For example, initialGrid('grid','species',1,0.5,2,0.25) populates the grid with two species (1 and 2), where species 1 occupies 50% of the grid, and species 2 25%.
Parameters:
Name Type Description
{@GridModel} grid The gridmodel containing the grid to be modified.
property String The name of the state to be set
value integer The value of the state to be set (optional argument with position 2, 4, 6, ..., n)
fraction float The chance the grid point is set to this state (optional argument with position 3, 5, 7, ..., n)
Source:

initialPattern()

initialPattern takes a @GridModel and loads a pattern from a PNG file. Note that this will only work when Cacatoo is ran on a server due to security issues. If you want to use this feature locally, there are plugins for most browser to host a simple local webserver. (currently only supports black and white image)
Source:

initialSpot({@GridModel}, property, value)

initialSpot populates a grid with states. Grid points close to a certain coordinate are set to state value, while other cells are set to the bg-state of 0.
Parameters:
Name Type Description
{@GridModel} grid The gridmodel containing the grid to be modified.
property String The name of the state to be set
value integer The value of the state to be set (optional argument with position 2, 4, 6, ..., n)
Source:

log(msg, target)

log a message to either the console, or to a HTML div.
Parameters:
Name Type Description
msg String String to write to log
target String If defined, write log to HTML div with this name
Source:

makeGridmodel(name)

Generate a new GridModel within this simulation.
Parameters:
Name Type Description
name string The name of your new model, e.g. "gol" for game of life. Cannot contain whitespaces.
Source:

populateGrid({@GridModel}, individuals, freqs)

populateGrid populates a grid with custom individuals.
Parameters:
Name Type Description
{@GridModel} grid The gridmodel containing the grid to be modified.
individuals Array The properties for individuals 1..n
freqs Array The initial frequency of individuals 1..n
Source:

populateSpot({@GridModel}, individuals, freqs)

populateSpot populates a spot with custom individuals.
Parameters:
Name Type Description
{@GridModel} grid The gridmodel containing the grid to be modified.
individuals Array The properties for individuals 1..n
freqs Array The initial frequency of individuals 1..n
Source:

printCursorPosition(canvas, event, scale)

Get *and print the GP* at the cursor position
Parameters:
Name Type Description
canvas canvas A (constant) canvas object
event event-handler Event handler (mousedown)
scale scale The zoom (scale) of the grid to grab the correct grid point
Source:

putSpot({@GridModel}, property, value, fraction)

putSpot sets values at a certain position with a certain radius. Grid points close to a certain coordinate are set to state value, while other cells are set to the bg-state of 0.
Parameters:
Name Type Description
{@GridModel} grid The gridmodel containing the grid to be modified.
property String The name of the state to be set
value integer The value of the state to be set (optional argument with position 2, 4, 6, ..., n)
fraction float The chance the grid point is set to this state (optional argument with position 3, 5, 7, ..., n)
Source:

setupRandom(seed)

Set up the random number generator
Parameters:
Name Type Description
seed int Seed for fast-random module
Source:

spaceTimePlot(name, source_canvas_label, label, col_to_draw, ncol, scale)

Create a space time display for a gridmodel
Parameters:
Name Type Description
name string The name of an existing gridmodel to display
source_canvas_label string The name of the property to display
label string Overwrite the display name with something more descriptive
col_to_draw integer Col to display (default = center)
ncol integer Number of cols (i.e. time points) to display (default = ncol)
scale integer Scale of display (default inherited from @Simulation class)
Source:

start()

Start the simulation. start() detects whether the user is running the code from the browser or, alternatively, in nodejs. In the browser, a GUI is provided to interact with the model. In nodejs the programmer can simply wait for the result without wasting time on displaying intermediate stuff (which can be slow)
Source:

step()

Update all the grid models one step. Apply optional mixing
Source:

stop()

Stop the simulation
Source:

toggle_mix()

Toggle the mix option
Source:

toggle_play()

Toggle the pause option. Restart the model if pause is disabled.
Source:

write(text, filename)

write a string to either a file, or generate a download request in the browser
Parameters:
Name Type Description
text String String to write
filename String write to this filename
Source:

write_append(text, filename)

append a string to a file (only supported in nodejs mode)
Parameters:
Name Type Description
text String String to write
filename String write to this filename
Source:

write_grid(msg, target)

Write a gridmodel to a file (only works outside of the browser, useful for running stuff overnight) Defaults to -1 if the property is not set
Parameters:
Name Type Description
msg String String to write to log
target String If defined, write log to HTML div with this name
Source: