Title: | Cellular Automata |
---|---|
Description: | Create cellular automata from Wolfram rules. Allows the creation of Wolfram style plots, as well as of animations. Easy to create multiple plots, for example the output of a rule with different initial states, or the output of many different rules from the same state. The output of a cellular automaton is given as a matrix, making it easy to try to explore the possibility of predicting its time evolution using various statistical tools available in R. |
Authors: | Vlad Tarko [aut, cre] |
Maintainer: | Vlad Tarko <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2024-11-22 08:31:23 UTC |
Source: | https://github.com/vladtarko/cellularautomata |
Create Cellular Automaton
ca(wolframrule, initialstate, steps = 100, ncols = 101, wrap = TRUE)
ca(wolframrule, initialstate, steps = 100, ncols = 101, wrap = TRUE)
wolframrule |
integer identifying the algorithm according to Wolfram numbering |
initialstate |
a vector setting up the initial state |
steps |
integer spacifying for how long to run the algorithm |
ncols |
how many columns to have. If 'initialstate' is specified, 'ncols' is calculated as 'length(initialstate)'. If 'initialstate' is not specified, it is defined as a 1 in the middle of zeros. For instance, with the default 'ncols = 11', the 'initialstate' is a vector of 5 zeros, 1, and another 5 zeros. |
wrap |
boolean, default is TRUE. Whether it uses a circular wrap at the end and beginning of lines. If FALSE it puts empty slots on the first and last columns. |
an object of class 'c("cellular_automaton", "matrix")'
Adapted from code by Nicola Procopio
<https://en.wikipedia.org/wiki/Cellular_automaton>
# Wolfram's rule 30 ca(30) # Wolfram's rule 126 with a random initial state ca(126, initialstate = sample(c(0, 1), size = 100, replace = TRUE), steps = 100)
# Wolfram's rule 30 ca(30) # Wolfram's rule 126 with a random initial state ca(126, initialstate = sample(c(0, 1), size = 100, replace = TRUE), steps = 100)
Plot a cellular automaton
## S3 method for class 'cellular_automata' plot( x, time_flow = "down", circle = FALSE, title = paste("Rule: ", attr(x, "wolfram_rule")), animate = FALSE, ... )
## S3 method for class 'cellular_automata' plot( x, time_flow = "down", circle = FALSE, title = paste("Rule: ", attr(x, "wolfram_rule")), animate = FALSE, ... )
x |
A cellular automaton, usually previously defined by 'ca()'. |
time_flow |
String: "down" (default) or "up". Whether time flow is represented as going from top-to-bottom or bottom-to-top. |
circle |
Whether to make the plot circular. Default is FALSE. |
title |
Title of the plot. Use 'NULL' to remove. |
animate |
Whether to return a gganimate object instead of a static ggplot. Default FALSE. |
... |
Not used (included for consistency with the 'plot' generic). |
A ggplot of the visual representation of the cellular automaton, or a gganimate object.
ca(30) |> plot() ca(30, ncols = 100, steps = 100) |> plot() ca(45, ncols = 100, steps = 100) |> plot() ca(86, ncols = 100, steps = 100) |> plot() # use a random initial state ca(126, initialstate = sample(c(0, 1), size = 100, replace = TRUE), steps = 100) |> plot()
ca(30) |> plot() ca(30, ncols = 100, steps = 100) |> plot() ca(45, ncols = 100, steps = 100) |> plot() ca(86, ncols = 100, steps = 100) |> plot() # use a random initial state ca(126, initialstate = sample(c(0, 1), size = 100, replace = TRUE), steps = 100) |> plot()
Create the rule for a specific Wolfram number
wolfram_rule(rule)
wolfram_rule(rule)
rule |
the Wolfram rule |
a vector with 8 elements defining the responses to: (111), (110), (101), (100), (011), (010), (001), (000) on the previous row
# get the definition of rule 30 wolfram_rule(30)
# get the definition of rule 30 wolfram_rule(30)
Plot the definition of a Wolfram rule
wolfram_rule_def(rule)
wolfram_rule_def(rule)
rule |
integer, the Wolfram rule |
a ggplot object defining the rule
wolfram_rule_def(30)
wolfram_rule_def(30)