A library for Cellular Automaton simulation made by Rishabh Anand
To run a Cellular Automaton simulation, you first create a grid using the Hringhorni
object of dimensions n*n
:
from hringhorni import Hringhorni
my_grid = Hringhorni(n=15)
You can initialise the grid with randomly chosen points too:
from hringhorni import Hringhorni
my_grid = Hringhorni(n=15)
my_grid.random_spawn(n_cells=30) # default is 10
You can even specify t points on the grid you want live cells at. Follow this diagram for reference:
from hringhorni import Hringhorni
my_grid = Hringhorni(n=15)
# using 0-indexing for both horizontal and vertical coordinates
all_points = [[3, 3], [2, 4], [1, 10], [0, 7]]
my_grid.populate(all_points)
# ensure the points in your array are within the limits of your gride size `n`
To move from one state to another, you can use the step
method. It returns a numpy.ndarray
representation of the next board state after the birth/death rules are applied cell-by-cell.
Ideally, use this method in a loop (like for
or while
) over T
timesteps:
from hringhorni import Hringhorni
my_grid = Hringhorni(n=15)
my_grid.random_spawn(n_cells=30)
T = 15
for i in range(T):
next_state = my_grid.step()
There are several methods to obtain or view the grid's current state.
.display()
This prints out grid to terminal using 0
for dead cells and 255
for alive cells
from hringhorni import Hringhorni
my_grid = Hringhorni(n=15)
my_grid.random_spawn(n_cells=50)
my_grid.display()
"""
0 0 0 0 0 0 0 0 255 0 0 0 0 255 0
255 0 0 255 0 0 0 255 0 0 0 255 0 0 0
0 0 0 0 255 0 0 0 0 0 0 0 0 0 0
0 255 0 0 0 0 255 0 0 0 0 0 0 0 0
0 255 0 0 255255255 0 0 0 255 0 255 0 255
0 0 0 255 0 0 0 255 0 0 0 0 0 255 0
0 255 0 0 0 0 255255 0 255 0 0 0 0 255
0 0 255 0 0 0 255 0 0 0 0 255 0 0 0
0 255 0 255255 0 255 0 0 0 0 0 0 0 255
0 0 0 0 255 0 255 0 0 0 0 0 0 0 0
0 255 0 0 255 0 0 255 0 255 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 255 0 0 255 0
0 255255 0 0 0 0 255 0 0 0 0 255 0 0
255 0 0 0 0 0 255255 0 0 0 255 0 0 0
0 0 0 255255 0 0 0 0 0 0 0 0 0 0
"""
.raw_obs()
Returns a numpy.ndarray
object of the current grid state
from hringhorni import Hringhorni
my_grid = Hringhorni(n=15)
my_grid.random_spawn(n_cells=30)
s_0 = my_grid.raw_obs()
# this saves a numpy array representation of the current grid state
.tstep_display()
Shows a collective plot of all states across all timesteps like this.
Arguments:
frames
: list of states (in innumpy.ndarray
representation from the.raw_obs()
method)r
: number of rows in the plotc
: number of columns in the plot
Note: Ensure that
r * c = T
, whereT
is the total number of states inframes
(ie. timesteps)
from hringhorni import Hringhorni
my_grid = Hringhorni(n=15)
my_grid.random_spawn(n_cells=30)
s_0 = my_grid.raw_obs()
all_states = [s_0] # starting with initial state at T=0
T = 15
for i in range(T):
state = grid.step()
all_states.append(state)
grid.tstep_display(all_states)
You can even try simulating interesting shapes in Hringhorni
using the set_shapes
method that takes in a list of different shapes. Even if you want
"glider"
You can spawn a centrally-placed glider on the grid by adding "glider"
to the list.
from hringhorni import Hringhorni
my_grid = Hringhorni(n=15)
my_grid.set_shapes(["glider"])
...
More shapes to come!