Crate lenia_ca

source ·
Expand description

Lenia_ca is a crate that provides core functionality for simulating the Lenia system of cellular automata. The crate was made as a programming excersize in making a large-ish Rust project. Since this was the first proper Rust project for the author, then the crate has some weird quirks and inefficient… perhaps even illogical ways of structuring it. In the future, the trait based system will probably be replaced by a system using phantom data or some other way of differenciating different Lenia systems from one-another.

For now, the general way to use the crate is to import it like you would any other Rust crate, and then use the Simulator struct essentially exclusively. You may also want to look into the kernels module and growth_functions module, as they contain a number of useful generators and functions for Lenia systems.

A rough example of a quick-start code is below… Please note that display() function would have to be implemented by the user.

let starting_pattern: ndarray::ArrayD<f64>; // fill with your data
let channel_shape: Vec<usize> = vec![100, 100];
let mut simulator = Simulator::<StandardLenia>::new(&channel_shape);
simulator.fill_channel(&starting_pattern, 0);
while true {
    simulator.iterate();
    display(get_channel_as_ref(0));
}

Types of Lenia

This version of lenia_ca crate supports only 2 different types of Lenia systems. StandardLenia and ExpandedLenia types, and is not capable of simulating types like “asymptotic” or “particle” Lenia.

Implementation notes

The working principle for StandardLenia is the following:

  • Perform a convolution operation (implemented as a FFT-based convolution) between the channel and kernel of the convolution_channel
  • Each point/pixel’s value is then passed into a growth_function of the convolution_channel.
  • The resulting points/pixels are then multiplied by the integration step dt and added onto the original values in the channel.
  • The resulting points/pixels are then clamped to be in range 0..1. This result is the next time-step of the channel, and would be used as the next iteration’s channel values.

use set_kernel() to change how the kernel looks.

use set_growth_function() to set a specific growth function for the convolution result.

use set_dt() to change the integration-step of the simulation.

Image of the algorithm available on Github

The working principle for ExpandedLenia is the following:

  • For each convolution_channel, perform a convolution operation (implemented as a FFT-based convolution) between a source channel and the convolution_channel’s kernel.
  • For each convolution_channel, pass the convolution results into the growth_function of the convolution_channel.
  • For each channel, perform an elementwise multiplication between the corresponding convolution_channel results and weights of the channel
  • For each channel, perform a weighted-sum on the results of the weight-convolution multiplicated results.
  • For each channel, multiply the weighted-sum by the integration step dt and add it to the original values in the channel.
  • For each channel, clamp the resulting values to be in range 0..1. This result is the next time-step of the corresponding channel, and would be used as the next iteration’s channel values.

Image of the algorithm available on Github

use set_channels() to set the number of channels in the simulation.

use set_convolution_channels() to set the number of kernels and the associated growth functions.

use set_convolution_channel_source() to set the channel which will be convoluted by a particular kernel.

use set_kernel() to change how a convolution_channel’s kernel looks like.

use set_growth_function() to set a specific growth function for the convolution result.

use set_weights() to set a channel’s weights for the corresponding convolution channel results.

use set_dt() to change the integration-step of the simulation.

Modules

  • A collection of often used growth functions.
  • Collection of generators for often used kernel shapes.
  • Collection of different types of Lenia systems.

Structs

Traits

  • Lenia functionality trait.

Functions