Skip to content

A Python library for Cellular Automaton simulations

License

Notifications You must be signed in to change notification settings

rish-16/Hringhorni

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚓️ Hringhorni

A library for Cellular Automaton simulation made by Rishabh Anand


Usage

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`

Traversing Timesteps

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()

State Visualisation

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:

  1. frames: list of states (in in numpy.ndarray representation from the .raw_obs() method)
  2. r: number of rows in the plot
  3. c: number of columns in the plot

Note: Ensure that r * c = T, where T is the total number of states in frames (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)

Interesting Shapes

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!


License

MIT

About

A Python library for Cellular Automaton simulations

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages