Package: cellular-automaton
Cellular AutomatonThis package provides an cellular automaton forPython 3A cellular automaton defines a grid of cells and a set of rules.
All cells then evolve their state depending on their neighbours state simultaneously.For further information on cellular automatons consult e.g.mathworld.wolfram.comYet another cellular automaton module?It is not the first python module to provide a cellular automaton,
but it is to my best knowledge the first that provides all of the following features:easy to usen dimensionalspeed optimizeddocumentedtestedI originally did not plan to write a new cellular automaton module,
but when searching for one, I just found some that had little or no documentation with an API that really did not fit my requirements
and/or Code that was desperately asking for some refactoring.So I started to write my own module with the goal to provide an user friendly API
and acceptable documentation. During the implementation I figured, why not just provide
n dimensional support and with reading Clean Code from Robert C. Martin the urge
to have a clean and tested code with a decent coverage added some more requirements.
The speed optimization and multi process capability was more of challenge for myself.
IMHO the module now reached an acceptable speed, but there is still room for improvements (e.g. with Numba?).InstallationThis module can be loaded and installed frompypi:pip install cellular-automatonUsageTo start and use the automaton you will have to define four things:The neighborhoodThe dimensions of the gridThe evolution ruleThe initial cell stateclassMyCellularAutomaton(CellularAutomaton):definit_cell_state(self,coordinate:tuple)->Sequence:returninitial_cell_statedefevolve_rule(self,last_state:tuple,neighbors_last_states:Sequence)->Sequence:returnnext_cell_stateneighborhood=MooreNeighborhood(EdgeRule.IGNORE_EDGE_CELLS)ca=MyCellularAutomaton(dimension=[100,100],neighborhood=neighborhood)NeighbourhoodThe Neighborhood defines for a cell neighbours in relative coordinates.
The evolution of a cell will depend solely on those neighbours.The Edge Rule passed as parameter to the Neighborhood defines, how cells on the edge of the grid will be handled.
There are three options:Ignore edge cells: Edge cells will have no neighbours and thus not evolve.Ignore missing neighbours: Edge cells will add the neighbours that exist. This results in varying count of neighbours on edge cells.First and last cell of each dimension are neighbours: All cells will have the same neighbour count and no edge exists.DimensionA list or Tuple which states each dimensions size.
The example above defines a two dimensional grid with 100 x 100 cells.There is no limitation in how many dimensions you choose but your memory and processor power.Evolution and Initial StateTo define the evolution rule and the initial state create a class inheriting fromCellularAutomaton.Theinit_cell_statemethod will be called once during the creation process for every cell.It will get the coordinate of that cell and is supposed to return a tuple representing that cells state.Theevolve_rulegets passed the last cell state and the states of all neighbors.It is supposed to return a tuple representing the new cell state.All new states will be applied simultaneously, so the order of processing the cells is irrelevant.VisualisationThe package provides a module for visualization of a 2D automaton in a pygame window.CAWindow(cellular_automaton=StarFallAutomaton()).run()The visual part of this module is fully decoupled and thus should be easily replaceable.ExamplesThe package contains three examples:simple_star_fallconways_game_of_lifecreation_and_process_time_analysisThose example implementations should provide a good start for your own project.Getting InvolvedFeel free to open pull requests, send me feature requests or even join as developer.
There ist still quite some work to do.And for all others, don't hesitate to open issues when you have problems!Changelog1.0.8Fixes automaton using edge cells with radius > 1 not workingFixes automaton is not stopping after evolution ended1.0.7Fixes automaton not active on reactivation1.0.6Fixes reactivation not redrawing all cells1.0.5Fixes black lines in the display between some cell lines/columns1.0.4Adds active state for automatonAdds reactivation methodFixes cells active state1.0.3Fixes init_cell_state called twice the necessary amount1.0.2Adds set CellularAutomaton.cells capability to be able to move cell states from one Automaton to another.1.0.1Add KeyboardInterrupt suppression to CAWindow1.0.0CI was addedCoverage was increasedChange project structureremoved multiprocess capabilityreduced class countImproved algorithm to increase:creation time by factor of ~2processing speed by factor of ~15Changed APINo separate factory anymore: Just create a CellularAutomaton(...)No Rule class anymore: Subclass CellularAutomaton and overrideevolve_ruleandinit_cell_stateCell color is now defined by the CAWindowstate_to_color_cbparameter.Neighborhood does not need to know the dimension anymoreDependenciesThere is only a dependency forrecordclassat the moment.If you want to use the CAWindow or execute the examples you will have to installpygamefor visualisation.
If you don't want to use this engine for some reason pass another draw_ending to the CAWindow.
It has to provides the same interface as thePygameEngineLicenceThis package is distributed under theApache License, Version 2.0,
seeLICENSE.txt