Package: cavl
cavlcavlis a simple library for simulating and visualizing various cellular automata.InstallationInstall usingpip:pip install cavlNote: If you want to use the built-in animation functionality, you also need to installffmpeg.Usagecavlprovides two main classes for creating and simulating cellular automata:CellularAutomaton1DandCellularAutomaton2D, 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 with1Dapplyfunction, as well as the state number of the current cell being worked onThe givenapplyfunction should return the state number that the current cell should be in the next generation.Neighborhoodscavlprovides 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)]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)]Visualizationcavlprovides 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 automaton1D Cellular AutomataRulescavlalso 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 theWolfram codenaming systemTotalistic1DRule: rule that looks at thetotalof the values of the cells in a neighborhood; more informationhereExampleimportcavlstart=cavl.init(50)# returns 1D array with width of 50rule=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 AutomataRulesCurrently, there are no built-in rule classes that work with 2D cellular automata; some will be added later on.Exampleimportcavl# simple period 2 oscillatorstart=[[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 rulesdefapply(neighbors,current)->int:alive=sum(neighbors.values())ifcurrent==1and(alive==2oralive==3):return1elifcurrent==0andalive==3:return1else:return0automaton=cavl.CellularAutomaton2D(init=start,neighbors=neighbors,apply=apply)automaton.evolve(21)cavl.plot2d(automaton,save=True,filename='2D_automaton')TODOMore built-in rulesCyclicLife-likeAdditional grid layoutsHexagonalTriangular...and more