Skip to main content

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 automata
  • neighbors: list of coordinates to represent the neighborhood of a cell
  • apply: 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.

Neighborhoods

cavl provides built-in helper functions for retrieving lists of tuple coordinates representing common neighborhoods.

  • moore(radius: int): returns list of tuple[int, int] representing a Moore neighborhood with given radius (default of 1)
    • i.e. moore() returns [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
  • von_neumann(radius: int): returns list of tuple[int, int] representing a von Neumann neighborhood with given radius (default of 1)
    • i.e. von_neumann() returns [(0, -1), (-1, 0), (1, 0), (0, 1)]

Visualization

cavl provides several built-in functions for visualizing your cellular automata:

  • plot: used to visualize the generations of a 1D cellular automaton
  • animate: used to animate the progression of the generations of a 1D cellular automaton
  • plot2d: used to visualize the most recent generation of a 2D cellular automaton
  • animate2d: 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 system
  • Totalistic1DRule: 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cavl-0.4.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

cavl-0.4.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

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

Hashes for cavl-0.4.1.tar.gz
Algorithm Hash digest
SHA256 b11b5f3908c91fc8788545984ea4b9637a2a8b0f3cdd2154677d5d3e353d2cff
MD5 2d09f547a671fefad1c9f5afb71a703d
BLAKE2b-256 c7a7cbd0c6947dee219bd02a9e8ab266648ffef4645c82fcfed57f1f0015d374

See more details on using hashes here.

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

Hashes for cavl-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 480170a71854413ac108e9e25569b22fca930dc2b2136383ebacc68e333826a8
MD5 14c7e0e1abaff37b092e63dc60295f40
BLAKE2b-256 a3ad9c1db1203ca7e61a7a844f6d77abb8cc19114f122f00f334571756c5c9fd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page