A simple library for visualizing cellular automata
Project description
cavl
cavl
is a simple library for simulating and visualizing various cellular automata.
Installation
Install using pip
:
pip install cavl
Note: If you want to use the built-in animation functionality, you also need to install ffmpeg
.
Usage
cavl
provides two main classes for creating and simulating cellular automata: CellularAutomaton1D
and CellularAutomaton2D
, for 1D and 2D cellular automata respectively.
Both classes take the same number and type of parameters for initialization:
init
: the starting grid for the cellular automataneighbors
: list of coordinates to represent the neighborhood of a cellapply
: function to apply a given rule to a single cell (signature depends on whether working with 1D or 2D cellular automata)- 1D: takes dictionary with keys as relative coordinates of a neighbor and values representing the current state number of that neighbor (
dict[tuple[int, int], float]
) - 2D: takes same dictionary as with 1D
apply
function, as well as the state number of the current cell being worked on - The given
apply
function should return the state number that the current cell should be in the next generation.
- 1D: takes dictionary with keys as relative coordinates of a neighbor and values representing the current state number of that neighbor (
Neighborhoods
cavl
provides built-in helper functions for retrieving lists of tuple coordinates representing common neighborhoods.
moore(radius: int)
: returns list oftuple[int, int]
representing a Moore neighborhood with givenradius
(default of 1)- i.e.
moore()
returns[(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
- i.e.
von_neumann(radius: int)
: returns list oftuple[int, int]
representing a von Neumann neighborhood with givenradius
(default of 1)- i.e.
von_neumann()
returns[(0, -1), (-1, 0), (1, 0), (0, 1)]
- i.e.
Visualization
cavl
provides several built-in functions for visualizing your cellular automata:
plot
: used to visualize the generations of a 1D cellular automatonanimate
: used to animate the progression of the generations of a 1D cellular automatonplot2d
: used to visualize the most recent generation of a 2D cellular automatonanimate2d
: used to animate the progression of the generations of a 2D cellular automaton
1D Cellular Automata
Rules
cavl
also provides some built-in classes for common cellular automata rules. Currently, there are only two built-in 1D cellular automata rules:
General1DRule
: takes a rule using the Wolfram code naming systemTotalistic1DRule
: rule that looks at the total of the values of the cells in a neighborhood; more information here
Example
import cavl
start = cavl.init(50) # returns 1D array with width of 50
rule = cavl.General1DRule(30)
automaton = cavl.CellularAutomaton1D(init=start, neighbors=rule.neighbors, apply=rule.apply)
automaton.evolve(20)
cavl.plot(automaton, save=True, filename='1D_automaton')
2D Cellular Automata
Rules
Currently, there are no built-in rule classes that work with 2D cellular automata; some will be added later on.
Example
import cavl
# simple period 2 oscillator
start = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
neighbors = cavl.moore()
# Conway's Game of Life rules
def apply(neighbors, current) -> int:
alive = sum(neighbors.values())
if current == 1 and (alive == 2 or alive == 3):
return 1
elif current == 0 and alive == 3:
return 1
else:
return 0
automaton = cavl.CellularAutomaton2D(init=start, neighbors=neighbors, apply=apply)
automaton.evolve(21)
cavl.plot2d(automaton, save=True, filename='2D_automaton')
TODO
- More built-in rules
- Cyclic
- Life-like
- Additional grid layouts
- Hexagonal
- Triangular
- ...and more
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cavl-0.4.1.tar.gz
.
File metadata
- Download URL: cavl-0.4.1.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0rc2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b11b5f3908c91fc8788545984ea4b9637a2a8b0f3cdd2154677d5d3e353d2cff |
|
MD5 | 2d09f547a671fefad1c9f5afb71a703d |
|
BLAKE2b-256 | c7a7cbd0c6947dee219bd02a9e8ab266648ffef4645c82fcfed57f1f0015d374 |
File details
Details for the file cavl-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: cavl-0.4.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0rc2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 480170a71854413ac108e9e25569b22fca930dc2b2136383ebacc68e333826a8 |
|
MD5 | 14c7e0e1abaff37b092e63dc60295f40 |
|
BLAKE2b-256 | a3ad9c1db1203ca7e61a7a844f6d77abb8cc19114f122f00f334571756c5c9fd |