3 releases
new 0.1.2 | Apr 3, 2025 |
---|---|
0.1.1 | Mar 27, 2025 |
0.1.0 | Mar 23, 2025 |
#364 in Machine learning
241 downloads per month
275KB
2.5K
SLoC
LiveIron
Description
A performant cellular automata simulator library with visualisation capabilities built in Rust. The library is designed to be flexible and easy to use, with a focus on performance. It provides a simple API for creating and running cellular automata simulations, as well as visualising them using the dioxus
library. In addition, the library also supports evolving cellular automata using genetic algorithms, as conceptualised by Mitchell et al using the GeneticAutomaton
struct and its associated components.
Components
Cellular Automata
-
state
: One of the two atomic elements of a cellular automaton, the state module contains thestate
struct, which is used to represent the state of a cell in the simulation. It also containsstate
implementations for Conway's Game of Life and Langton's Ant. -
rule
: One of the two atomic elements of a cellular automaton, the rule module contains therule
trait that defines how the state of a cell changes. It also containsrule
implementations for Conway's Game of Life and Langton's Ant. -
board
: The board module contains theboard
struct, which is the main data structure used to represent the board of cells in the simulation. It contains methods and structs to create the board with a specifiedBoundaryCondition
, read and write cell states, and convert the board to its representation for visualisation. -
neighbourhood
: The neighbourhood module contains theneighbourhood
struct andneighbourhood
implementations for theMoore
andVonNeumann
neighbourhoods. Theneighbourhood
struct is used to define the neighbourhood of a cell in the simulation, and implements methods to efficiently calculate the neighbours of a cell and return their states and coordinates. -
automaton
: The most important module, the automaton module contains theautomaton
struct, which is used to represent the cellular automaton simulation. It contains methods to create an automaton with a specified board and a set of rules, evolve the automaton by applying the rules to the board, and visualise the automaton using theui
module. -
ui
: Theui
module uses thedioxus
library to create a window and render the automaton to the screen. The module contains main simulation function as well as several Dioxus components used to visualise the automaton.
Genetic Automata
-
genotype
: The atomic element of a genetic automaton, thegenotype
module contains thegenotype
trait, which is used to represent the genotype in the population. It replaces therule
trait in thecellular automata
module as a genetic rule. Thegenotype
trait is used to define how the genotype is represented, how it is mutated, how it is evaluated, and how it is used to create offspring. -
population
: The population module contains thepopulation
struct, which is used to represent the population of genotypes in the simulation. It contains methods to create a population with a specified size and genotype, evaluate the population, select the best genotypes for reproduction, cull the population, and create offspring. -
selection_strategy
: The selection strategy module contains theSelectionStrategy
enum, which provides methods to select parents for reproduction from the population and select genotypes to cull from the population. It contains implementations for several selection strategies, includingTournament
,RouletteWheel
,Rank
, andTruncation
. The selection strategy is used to select the best genotypes for reproduction based on their fitness scores. -
genetic_automaton
: Thegenetic_automaton
module contains theGeneticAutomaton
struct, which is used to represent the genetic automaton simulation. It contains methods to create a genetic automaton with a specified population and board, evolve the genetic automaton by applying the genotypes to the board, and visualise the genetic automaton using theui
module.
Usage
To use the library, add the following to your Cargo.toml
:
[dependencies]
live-iron = "0.1.2"
Using the library is both simple and flexible. Here's an example of Conway's Game of Life:
// Create a 20x20 board with a glider pattern
let mut initial_state: Vec<Vec<GameOfLifeState>> = vec![vec![GameOfLifeState::Dead; 20]; 20];
initial_state[0][0] = GameOfLifeState::Alive;
initial_state[0][2] = GameOfLifeState::Alive;
initial_state[1][1] = GameOfLifeState::Alive;
initial_state[1][2] = GameOfLifeState::Alive;
initial_state[2][1] = GameOfLifeState::Alive;
// Create a board with the initial state and periodic boundary conditions
let mut board: Board<GameOfLifeState> = Board::new(initial_state, BoundaryCondition::Periodic);
// Create a Game of Life rule and an automaton with the board and rule
let rule: GameOfLifeRule = GameOfLifeRule;
let mut automaton: automaton::Automaton<'_, GameOfLifeState> = automaton::Automaton::new(&mut board, vec![Box::new(rule)]);
// Visualise the automaton for 100 steps with an interval of 500ms.
let _ = automaton.visualise(100, 500);
Dependencies
~6–50MB
~740K SLoC