Skip to content

MartinuzziFrancesco/CellularAutomata.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CellularAutomata.jl

Documentation Build Status Julia Testing DOI
docs CI codecov Julia Code Style: Blue Aqua QA DOI

Installation

CellularAutomata.jl is registered on the general registry. For the installation follow:

julia> using Pkg
julia> Pkg.add("CellularAutomata")

or, if you prefer:

julia> using Pkg
julia> Pkg.add("https://github.com/MartinuzziFrancesco/CellularAutomata.jl")

Discrete Cellular Automata

The package offers creation of all the cellular automata described in A New Kind of Science by Wolfram, and the rules for the creation are labelled as in the book. We will recreate some of the examples that can be found in the wolfram atlas both for elementary and totalistic cellular automata.

Elementary Cellular Automata

Elementary Cellular Automata (ECA) have a radius of one and can be in only two possible states. Here we show a couple of examples:

Rule 18

using CellularAutomata, Plots

states = 2
radius = 1
generations = 50
ncells = 111
starting_val = zeros(Bool, ncells)
starting_val[Int(floor(ncells/2)+1)] = 1

rule = 18

ca = CellularAutomaton(DCA(rule), starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false)

dca18

Rule 30

using CellularAutomata, Plots

states = 2
radius = 1
generations = 50
ncells = 111
starting_val = zeros(Bool, ncells)
starting_val[Int(floor(ncells/2)+1)] = 1

rule = 30

ca = CellularAutomaton(DCA(rule), starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false)

dca30

General Cellular Automata

General Cellular Automata have the same rule of ECA but they can have a radius larger than unity and/or a number of states greater than two. Here are provided examples for every possible permutation, starting with a Cellular Automaton with 3 states.

Rule 7110222193934

using CellularAutomata, Plots

states = 3
radius = 1
generations = 50
ncells = 111
starting_val = zeros(ncells)
starting_val[Int(floor(ncells/2)+1)] = 2

rule = 7110222193934 

ca = CellularAutomaton(DCA(rule,states=states,radius=radius), 
                       starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false,
    size=(ncells*10, generations*10))

dca7110222193934

The following examples shows a Cellular Automaton with radius=2, with two only possible states:

Rule 1388968789

using CellularAutomata, Plots

states = 2
radius = 2
generations = 30
ncells = 111
starting_val = zeros(ncells)
starting_val[Int(floor(ncells/2)+1)] = 1

rule = 1388968789 

ca = CellularAutomaton(DCA(rule,states=states,radius=radius), 
                           starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false,
    size=(ncells*10, generations*10))

dca1388968789

And finally, three states with a radius equal to two:

Rule 914752986721674989234787899872473589234512347899

using CellularAutomata, Plots

states = 3
radius = 2
generations = 30
ncells = 111
starting_val = zeros(ncells)
starting_val[Int(floor(ncells/2)+1)] = 2

rule = 914752986721674989234787899872473589234512347899 

ca = CellularAutomaton(DCA(rule,states=states,radius=radius), 
                       starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false,
    size=(ncells*10, generations*10))

dca914752986721674989234787899872473589234512347899

It is also possible to specify asymmetric neighborhoods, giving a tuple to the kwarg detailing the number of neighbors to considerate at the left and right of the cell: Rule 1235

using CellularAutomata, Plots

states = 2
radius = (2,1)
generations = 30
ncells = 111
starting_val = zeros(ncells)
starting_val[Int(floor(ncells/2)+1)] = 1

rule = 1235 

ca = CellularAutomaton(DCA(rule,states=states,radius=radius), 
                       starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false,
    size=(ncells*10, generations*10))

dca1235

Totalistic Cellular Automata

Totalistic Cellular Automata takes the sum of the neighborhood to calculate the value of the next step.

Rule 1635

using CellularAutomata, Plots

states = 3
radius = 1
generations = 50
ncells = 111
starting_val = zeros(Integer, ncells)
starting_val[Int(floor(ncells/2)+1)] = 1

rule = 1635

ca = CellularAutomaton(TCA(rule, states=states), 
                       starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false)

dca1635

Rule 107398

using CellularAutomata, Plots

states = 4
radius = 1
generations = 50
ncells = 111
starting_val = zeros(Integer, ncells)
starting_val[Int(floor(ncells/2)+1)] = 1

rule = 107398

ca = CellularAutomaton(TCA(rule, states=states), 
                       starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false)

dca107398

Here are some results for a bigger radius, using a radius of 2 as an example.

Rule 53

using CellularAutomata, Plots

states = 2
radius = 2
generations = 50
ncells = 111
starting_val = zeros(Integer, ncells)
starting_val[Int(floor(ncells/2)+1)] = 1

rule = 53

ca = CellularAutomaton(TCA(rule, radius=radius), 
                           starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false)

dca53r2

Continuous Cellular Automata

Continuous Cellular Automata work in the same way as the totalistic but with real values. The examples are taken from the already mentioned book NKS.

Rule 0.025

using CellularAutomata, Plots

generations = 50
ncells = 111
starting_val = zeros(Float64, ncells)
starting_val[Int(floor(ncells/2)+1)] = 1.0

rule = 0.025

ca = CellularAutomaton(CCA(rule), starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false)

cca0025

Rule 0.2

using CellularAutomata, Plots

radius = 1
generations = 50
ncells = 111
starting_val = zeros(Float64, ncells)
starting_val[Int(floor(ncells/2)+1)] = 1.0

rule = 0.2

ca = CellularAutomaton(CCA(rule, radius=radius), 
                       starting_val, generations)

heatmap(ca.evolution, 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    axis=false,
    ticks=false)

cca02

Game of Life

This package can also reproduce Conway's Game of Life, and any variation based on it. The Life() function takes in a tuple containing the number of neighbors that will gave birth to a new cell, or that will make an existing cell survive. (For example in the Conways's Life the tuple (3, (2,3)) indicates having 3 live neighbors will give birth to an otherwise dead cell, and having either 2 or 3 lie neighbors will make an alive cell continue living.) The implementation follows the Golly notation.

This script reproduces the famous glider:

using CellularAutomata, Plots

glider = [[0, 0, 1, 0, 0] [0, 0, 0, 1, 0] [0, 1, 1, 1, 0]]

space = zeros(Bool, 30, 30)
insert = 1
space[insert:insert+size(glider, 1)-1, insert:insert+size(glider, 2)-1] = glider
gens = 100
space_gliding = CellularAutomaton(Life((3, (2,3))), space, gens)

anim = @animate for i = 1:gens
    heatmap(space_gliding.evolution[:,:,i], 
    yflip=true, 
    c=cgrad([:white, :black]),
    legend = :none,
    size=(1080,1080),
    axis=false,
    ticks=false)
end
 
gif(anim, "glider.gif", fps = 15)

glider