Constructor
new Gridmodel(name, config, rng)
The constructor function for a @Gridmodel object. Takes the same config dictionary as used in @Simulation
Parameters:
Name | Type | Description |
---|---|---|
name |
string | The name of your model. This is how it will be listed in @Simulation 's properties |
config |
dictionary | A dictionary (object) with all the necessary settings to setup a Cacatoo GridModel. |
rng |
MersenneTwister | A random number generator (MersenneTwister object) |
- Source:
Methods
MargolusDiffusion()
Apply diffusion algorithm for grid-based models described in Toffoli & Margolus' book "Cellular automata machines"
The idea is to subdivide the grid into 2x2 neighbourhoods, and rotate them (randomly CW or CCW). To avoid particles
simply being stuck in their own 2x2 subspace, different 2x2 subspaces are taken each iteration (CW in even iterations,
CCW in odd iterations)
- Source:
apply_async()
Analogous to apply_sync(func), but asynchronous
- Source:
apply_sync()
Like the synchronous function above, but can not take a custom user-defined function rather
than the default next-state function. Technically one should be able to refarctor this by making
the default function of synchronous "nextstate". But this works. :)
- Source:
asynchronous()
Asynchronously apply the nextState function (defined by user) to the entire grid
Asynchronous means that all grid points will be updated in a random order. For this
first the update_order will be determined (this.set_update_order). Afterwards, the nextState
will be applied in that order. This means that some cells may update while all their neighours
are still un-updated, and other cells will update while all their neighbours are already done.
- Source:
attachODE(eq, conf)
Attaches an ODE to all GPs in the model. Each gridpoint has it's own ODE.
Parameters:
Name | Type | Description |
---|---|---|
eq |
function | Function that describes the ODEs, see examples starting with "ode" |
conf |
Object | dictionary style configuration of your ODEs (initial state, parameters, etc.) |
- Source:
clearGrid()
Replaces current grid with an empty grid
- Source:
colourGradient(property, n)
Initiate a gradient of colours for a property.
Parameters:
Name | Type | Description |
---|---|---|
property |
string | The name of the property to which the colour is assigned |
n |
int | How many colours the gradient consists off For example usage, see colourViridis below |
- Source:
colourGradientArray(property, n)
Initiate a gradient of colours for a property (return array only)
Parameters:
Name | Type | Description |
---|---|---|
property |
string | The name of the property to which the colour is assigned |
n |
int | How many colours the gradient consists off For example usage, see colourViridis below |
- Source:
colourViridis(property, n, rev)
Initiate a gradient of colours for a property, using the Viridis colour scheme (purpleblue-ish to green to yellow) or Inferno (black to orange to yellow)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
property |
string | The name of the property to which the colour is assigned | |
n |
int | How many colours the gradient consists off | |
rev |
bool | false | Reverse the viridis colour gradient |
- Source:
copyGridpoint(x, y)
Return a copy of the gridpoint at position x,y
Makes sure wrapping is applied if necessary
Parameters:
Name | Type | Description |
---|---|---|
x |
int | position (column) for the focal gridpoint |
y |
int | position (row) for the focal gridpoint |
- Source:
copyIntoGridpoint(x, y, @Gridpoint)
Change the gridpoint at position x,y into gp
Makes sure wrapping is applied if necessary
Parameters:
Name | Type | Description |
---|---|---|
x |
int | position (column) for the focal gridpoint |
y |
int | position (row) for the focal gridpoint |
@Gridpoint |
Gridpoint | object to set the gp to |
- Source:
countMoore8()
countNeighbours for range 1-8 (see countNeighbours)
- Source:
countMoore9()
countNeighbours for range 0-8 (see countNeighbours)
- Source:
countNeighbours(grid, col, row, property, val, range) → {int}
Count the number of neighbours with 'val' in 'property' (Neu4, Neu5, Moore8, Moore9 depending on range-array)
Parameters:
Name | Type | Description |
---|---|---|
grid |
GridModel | The gridmodel used to check neighbours. Usually the gridmodel itself (i.e., this), but can be mixed to make grids interact. |
col |
int | position (column) for the focal gridpoint |
row |
int | position (row) for the focal gridpoint |
property |
string | the property that is counted |
val |
int | value property must have to be counted |
range |
Array | which section of the neighbourhood must be counted? (see this.moore, e.g. 1-8 is Moore8, 0-4 is Neu5,etc) |
- Source:
Returns:
The number of grid points with "property" set to "val"
Below, 4 version of this functions are overloaded (Moore8, Moore9, Neumann4, etc.)
For example, if one wants to count all the "alive" individuals in the Moore 9 neighbourhood, use
this.countNeighbours(this,10,10,1,'alive',[0-8]);
or
this.countMoore9(this,10,10,1,'alive');
- Type
- int
countNeumann4()
countNeighbours for range 1-4 (see countNeighbours)
- Source:
countNeumann5()
countNeighbours for range 0-4 (see countNeighbours)
- Source:
diffuseODEstates()
Diffuse ODE states on the grid. Because ODEs are stored by reference inside gridpoint, the
states of the ODEs have to be first stored (copied) into a 4D array (x,y,ODE,state-vector),
which is then used to update the grid.
- Source:
diffuseStateVector(rate)
Diffuse continuous states on the grid.
* @param {string} state The name of the state to diffuse
but can be mixed to make grids interact.
Parameters:
Name | Type | Description |
---|---|---|
rate |
float | the rate of diffusion. (<0.25) |
- Source:
diffuseStates(rate)
Diffuse continuous states on the grid.
* @param {string} state The name of the state to diffuse
but can be mixed to make grids interact.
Parameters:
Name | Type | Description |
---|---|---|
rate |
float | the rate of diffusion. (<0.25) |
- Source:
getGridpoint(xpos, ypos)
Get the gridpoint at coordinates x,y
Makes sure wrapping is applied if necessary
Parameters:
Name | Type | Description |
---|---|---|
xpos |
int | position (column) for the focal gridpoint |
ypos |
int | position (row) for the focal gridpoint |
- Source:
getMoore8()
getNeighbours for the Moore8 neighbourhood (range 1-8 in function getNeighbours)
- Source:
getMoore9()
getNeighbours for the Moore8 neighbourhood (range 1-8 in function getNeighbours)
- Source:
getNeighXY()
Get the x,y coordinates of a neighbour in an array.
Makes sure wrapping is applied if necessary
- Source:
getNeighbour(grid, col, row, direction)
Get a neighbour at compass direction
Parameters:
Name | Type | Description |
---|---|---|
grid |
GridModel | The gridmodel used to check neighbours. Usually the gridmodel itself (i.e., this), but can be mixed to make grids interact. |
col |
int | position (column) for the focal gridpoint |
row |
int | position (row) for the focal gridpoint |
direction |
int | the neighbour to return |
- Source:
getNeighbours(grid, col, row, property, val, range) → {int}
Get array of grid points with val in property (Neu4, Neu5, Moore8, Moore9 depending on range-array)
Parameters:
Name | Type | Description |
---|---|---|
grid |
GridModel | The gridmodel used to check neighbours. Usually the gridmodel itself (i.e., this), but can be mixed to make grids interact. |
col |
int | position (column) for the focal gridpoint |
row |
int | position (row) for the focal gridpoint |
property |
string | the property that is counted |
val |
int | value 'property' should have |
range |
Array | which section of the neighbourhood must be counted? (see this.moore, e.g. 1-8 is Moore8, 0-4 is Neu5,etc) |
- Source:
Returns:
The number of grid points with "property" set to "val"
Below, 4 version of this functions are overloaded (Moore8, Moore9, Neumann4, etc.)
If one wants to count all the "cheater" surrounding a gridpoint in cheater.js in the Moore8 neighbourhood
one needs to look for value '3' in the property 'species':
this.getNeighbours(this,10,10,3,'species',[1-8]);
or
this.getMoore8(this,10,10,3,'species')
- Type
- int
getNeumann4()
getNeighbours for the Moore8 neighbourhood (range 1-8 in function getNeighbours)
- Source:
getNeumann5()
getNeighbours for the Moore8 neighbourhood (range 1-8 in function getNeighbours)
- Source:
getODEstates(property, values)
Returns an array with the population sizes of different types
Parameters:
Name | Type | Description |
---|---|---|
property |
String | Return popsizes for this property (needs to exist in your model, e.g. "species" or "alive") |
values |
Array | Which values are counted and returned (e.g. [1,3,4,6]) |
- Source:
getPopsizes(property, values)
Returns an array with the population sizes of different types
Parameters:
Name | Type | Description |
---|---|---|
property |
String | Return popsizes for this property (needs to exist in your model, e.g. "species" or "alive") |
values |
Array | Which values are counted and returned (e.g. [1,3,4,6]) |
- Source:
grid_from_json(gridjson)
Loads a JSON object onto this gridmodel.
Parameters:
Name | Type | Description |
---|---|---|
gridjson |
string | JSON object to build new grid from |
- Source:
load_grid(file)
Reads a JSON file and loads a JSON object onto this gridmodel. Reading a local JSON file will not work in browser mode because of security reasons,
You can instead use 'addCheckpointButton' instead, which allows you to select a file from the browser manually.
Parameters:
Name | Type | Description |
---|---|---|
file |
string | Path to the json file |
- Source:
nextState(x, y)
The most important function in GridModel: how to determine the next state of a gridpoint?
By default, nextState is empty. It should be defined by the user (see examples)
Parameters:
Name | Type | Description |
---|---|---|
x |
int | Position of grid point to update (column) |
y |
int | Position of grid point to update (row) |
- Source:
perfectMix()
Assign each gridpoint a new random position on the grid. This simulated mixing,
but does not guarantee a "well-mixed" system per se (interactions are still local)
calculated based on neighbourhoods.
- Source:
plotArray(graph_labels, graph_values, cols, title, opts)
Adds a dygraph-plot to your DOM (if the DOM is loaded)
Parameters:
Name | Type | Description |
---|---|---|
graph_labels |
Array | Array of strings for the graph legend |
graph_values |
Array | Array of floats to plot (here plotted over time) |
cols |
Array | Array of colours to use for plotting |
title |
String | Title of the plot |
opts |
Object | dictionary-style list of opts to pass onto dygraphs |
- Source:
plotODEstates(ODE, values)
Easy function to add a ODE states (wrapper for plot array)
Parameters:
Name | Type | Description |
---|---|---|
ODE |
String | name Which ODE to plot the states for |
values |
Array | Which states are plotted (if undefined, all of them are plotted) |
- Source:
plotPoints(graph_values, title, opts)
Adds a dygraph-plot to your DOM (if the DOM is loaded)
Parameters:
Name | Type | Description |
---|---|---|
graph_values |
Array | Array of floats to plot (here plotted over time) |
title |
String | Title of the plot |
opts |
Object | dictionary-style list of opts to pass onto dygraphs |
- Source:
plotPopsizes(property, values)
Easy function to add a pop-sizes plot (wrapper for plotArrays)
Parameters:
Name | Type | Description |
---|---|---|
property |
String | What property to plot (needs to exist in your model, e.g. "species" or "alive") |
values |
Array | Which values are plotted (e.g. [1,3,4,6]) |
- Source:
plotXY(graph_labels, graph_values, cols, title, opts)
Adds a dygraph-plot to your DOM (if the DOM is loaded)
Parameters:
Name | Type | Description |
---|---|---|
graph_labels |
Array | Array of strings for the graph legend |
graph_values |
Array | Array of 2 floats to plot (first value for x-axis, second value for y-axis) |
cols |
Array | Array of colours to use for plotting |
title |
String | Title of the plot |
opts |
Object | dictionary-style list of opts to pass onto dygraphs |
- Source:
printGrid(property, fract)
Print the entire grid to the console. Not always recommended, but useful for debugging
Parameters:
Name | Type | Description |
---|---|---|
property |
float | What property is printed |
fract |
float | Subset to be printed (from the top-left) |
- Source:
print_grid()
Print the entire grid to the console
- Source:
randomMoore8()
randomMoore for range 1-8 (see randomMoore)
- Source:
randomMoore9()
randomMoore for range 0-8 (see randomMoore)
- Source:
randomNeighbour(grid, col, row, range)
Return a random neighbour from the neighbourhood defined by range array
Parameters:
Name | Type | Description |
---|---|---|
grid |
GridModel | The gridmodel used to check neighbours. Usually the gridmodel itself (i.e., this), but can be mixed to make grids interact. |
col |
int | position (column) for the focal gridpoint |
row |
int | position (row) for the focal gridpoint |
range |
Array | from which to sample (1-8 is Moore8, 0-4 is Neu5, etc.) |
- Source:
randomNeumann4()
randomMoore for range 1-4 (see randomMoore)
- Source:
randomNeumann5()
randomMoore for range 0-4 (see randomMoore)
- Source:
rouletteWheel(gps, property, non)
From a list of grid points, e.g. from getNeighbours(), sample one weighted by a property. This is analogous
to spinning a "roulette wheel". Also see a hard-coded versino of this in the "cheater" example
Parameters:
Name | Type | Default | Description |
---|---|---|---|
gps |
Array | Array of gps to sample from (e.g. living individuals in neighbourhood) | |
property |
string | The property used to weigh gps (e.g. fitness) | |
non |
float | 0 | Scales the probability of not returning any gp. |
- Source:
save_grid(filename)
Saves the current grid in a JSON object. In browser mode, it will throw download-request, which may or may not
work depending on the security of the user's browser.
Parameters:
Name | Type | Description |
---|---|---|
filename |
string | The name of of the JSON file |
- Source:
setGridpoint(x, y, @Gridpoint)
Change the gridpoint at position x,y into gp (typically retrieved with 'getGridpoint')
Makes sure wrapping is applied if necessary
Parameters:
Name | Type | Description |
---|---|---|
x |
int | position (column) for the focal gridpoint |
y |
int | position (row) for the focal gridpoint |
@Gridpoint |
Gridpoint | object to set the gp to (result of 'getGridpoint') |
- Source:
set_update_order()
If called for the first time, make an update order (list of ints), otherwise just shuffle it.
- Source:
setupColours(object)
Initiate a dictionary with colour arrays [R,G,B] used by Graph and Canvas classes
Parameters:
Name | Type | Description |
---|---|---|
object |
statecols | given object can be in two forms | either {state:colour} tuple (e.g. 'alive':'white', see gol.html) | or {state:object} where objects are {val:'colour}, | e.g. {'species':{0:"black", 1:"#DDDDDD", 2:"red"}}, see cheater.html |
- Source:
solveAllODEs(delta_t, opt_pos)
Numerically solve the ODEs for each grid point
Parameters:
Name | Type | Default | Description |
---|---|---|---|
delta_t |
float | 0.1 | Step size |
opt_pos |
bool | false | When enabled, negative values are set to 0 automatically |
- Source:
sumMoore8()
sumNeighbours for range 1-8 (see sumNeighbours)
- Source:
sumMoore9()
sumNeighbours for range 0-8 (see sumNeighbours)
- Source:
sumNeighbours(grid, col, row, property, range) → {int}
Sum the properties of grid points in the neighbourhood (Neu4, Neu5, Moore8, Moore9 depending on range-array)
Parameters:
Name | Type | Description |
---|---|---|
grid |
GridModel | The gridmodel used to check neighbours. Usually the gridmodel itself (i.e., this), but can be mixed to make grids interact. |
col |
int | position (column) for the focal gridpoint |
row |
int | position (row) for the focal gridpoint |
property |
string | the property that is counted |
range |
Array | which section of the neighbourhood must be counted? (see this.moore, e.g. 1-8 is Moore8, 0-4 is Neu5,etc) |
- Source:
Returns:
The number of grid points with "property" set to "val"
Below, 4 version of this functions are overloaded (Moore8, Moore9, Neumann4, etc.)
For example, if one wants to sum all the "fitness" surrounding a gridpoint in the Neumann neighbourhood, use
this.sumNeighbours(this,10,10,'fitness',[1-4]);
or
this.sumNeumann4(this,10,10,'fitness')
- Type
- int
sumNeumann4()
sumNeighbours for range 1-4 (see sumNeighbours)
- Source:
sumNeumann5()
sumNeighbours for range 0-4 (see sumNeighbours)
- Source:
synchronous()
Synchronously apply the nextState function (defined by user) to the entire grid
Synchronous means that all grid points will be updated simultaneously. This is ensured
by making a back-up grid, which will serve as a reference to know the state in the previous
time step. First all grid points are updated based on the back-up. Only then will the
actual grid be changed.
- Source:
update()
The update is, like nextState, user-defined (hence, empty by default).
It should contains all functions that one wants to apply every time step
(e.g. grid manipulations and printing statistics)
For example, and update function could look like:
this.synchronous() // Update all cells
this.MargolusDiffusion() // Apply Toffoli Margolus diffusion algorithm
this.plotPopsizes('species',[1,2,3]) // Plot the population sizes
- Source: