Simulator

The Simulator takes in a seagull.Board, and runs a simulation given a set number of iterations and a rule. For each iteration, the rule is applied to the Board in order to evolve the lifeforms. After the simulation, run statistics are returned.

import seagull as sg

board = sg.Board()
board.add(Blinker(), loc=(0,0))

# Initialize a simulator
sim = sg.Simulator(board)
stats = sim.run(sg.rules.conway_classic, iters=1000)

You can always get the history of the whole simulation by calling the get_history() method. The length of the history will always be equal to iters + 1 since we include the initial state

Note

Running a simulation does not change the state attribute of the board. Internally, the simulator makes a copy of that layout and updates that instead. This is to avoid unintended behaviour when running simulations again and again.

Various statistics such as entropy, peak cell coverage, and the like are returned as a dictionary. This gives us an idea on the characteristics of the simulation experiment.

Note

Some statistics are highly-dependent on the size of the board and the number of iterations. For example, peak cell coverage (pertaining to the max. amount of active cells during the whole run) depends on board size. If you have better ideas for computing these statistics, please open-up an Issue!

The run() method only computes the progress of the board for the whole simulation, but it does not animate it yet. To create an animation, call the animate() method:

sim.animate()

This returns a matplotlib.animation.FuncAnimation that you can turn into an interactive animation in your notebook or exported as a GIF.

Note

When exporting to GIF, it is required to have the ffmpeg backend installed.

class seagull.simulator.Simulator(board)[source]
__init__(board)[source]

Initialize the class

Parameters

board (seagull.Board) – The board to run the simulation on

animate(figsize=(5, 5), interval=100)[source]

Animate the resulting simulation

Parameters
  • figsize (tuple) – Size of the output figure

  • interval (int) – Interval for transitioning between frames

Returns

Animation generated from the run

Return type

matplotlib.animation.FuncAnimation

compute_statistics(history)[source]

Compute various statistics for the board

Parameters

history (list or numpy.ndarray) – The simulation history

Returns

Compute statistics

Return type

dict

get_history(exclude_init=False)[source]

Get the simulation history

Parameters

exclude_init (bool) – If True, then excludes the initial state in the history

Returns

Simulation history of shape (iters+1, board.size[0], board.size[1])

Return type

numpy.ndarray

run(rule, iters, **kwargs)[source]

Run the simulation for a given number of iterations

Parameters
  • rule (callable) – Callable that takes in an array and returns an array of the same shape.

  • iters (int) – Number of iterations to run the simulation.

Returns

Computed statistics for the simulation run

Return type

dict