Lightweight framework for generating cellular automata in .NET.

Rule 30 in 3 dimensions.
You can install the CellularAutomata.NET package via NuGet:
dotnet add package CellularAutomata.NETThe following examples are included in the project:
- Wolfram.csproj => Rule 30
- Conway.csproj => Game of Life.
- Wolfram3D.csproj => Rule 30 extruded to 3 dimensions. Rendered with SILK.Net
- Conway3D.csproj => Game of Life extruded to 3 dimensions. Rendered with SILK.Net.
- ReverseRuleBuilder.csproj => Example/research of reversing out the rules that match a given state for a 5 cell elementary automata
This framework allows for N-dimensional cellular automata. The limit varies based on your SIMD limitation for vectors.
Each automata has a Rule function which is applied to each cell each step. From Conway's Game of Life example:
public static readonly Action<
CellularAutomataCell<int>,
Dictionary<Vector<int>, CellularAutomataCell<int>>,
CellularAutomata<int>
> Rules = new Action<
CellularAutomataCell<int>,
Dictionary<Vector<int>, CellularAutomataCell<int>>,
CellularAutomata<int>
>(
(cell, neighbors, automata) =>
{
// https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Rules
if (neighbors.Count == 0)
{
return;
}
int livingNeighbors = neighbors.Sum(t => t.Value.State);
if (cell.State == 1)
{
if (livingNeighbors < 2 || livingNeighbors > 3)
{
cell.SetState(0);
}
}
else if (livingNeighbors == 3)
{
cell.SetState(1);
}
else
{
cell.SetState(0);
}
}
);The Func passes the cell being examined, a dictionary of its neighbors (with their relative positions) and the automata that called the function.
In this example, we look at the neighbors of our cell and sum up their State values.
The next piece of an automata is its configuration.
CellularAutomataConfiguration<int> config = new CellularAutomataConfiguration<int>
{
new CellularAutomataDimension { Cells = 48 },
new CellularAutomataDimension { Cells = 48 },
DefaultState = 0
};We specify the two dimensions of our grid and the default state of a cell. Dimensions contain properties for WrappedStart and WrappedEnd, but for this Game of Life we're not wrapping.
We'll also use the predefined Moore neighborhood. This simplifies the process of starting up our automaton. You can reference the Wolfram example for a custom neighborhood configuration.
CellularAutomata<int> automaton = new CellularAutomata<int>(
config,
CellularAutomataNeighborhood<int>.MooreNeighborhood,
Rules
);Now we set our initial state and start steppin'.
Dictionary<System.Numerics.Vector<int>, int> initialState = new Dictionary<
System.Numerics.Vector<int>,
int
>()
{
/*
* Glider at 0,0
* 1 0 0
* 0 1 1
* 1 1 0
*/
{ AutomataVector.Create(0, 0), 1 },
{ AutomataVector.Create(1, 1), 1 },
{ AutomataVector.Create(2, 1), 1 },
{ AutomataVector.Create(0, 2), 1 },
{ AutomataVector.Create(1, 2), 1 },
};
// Glider moves one cell to the right every 4 steps.
// We are moving width - the length of the glider, so 45 total along the x-axis.
int stepsToEnd = 45 * 4;
automaton.InitializeGrid(initialState);
int step = 0;
while (step <= stepsToEnd)
{
Console.WriteLine($"Step {step++}/{stepsToEnd}\n{automaton.Grid}");
automaton.Step();
}