How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Julia programming language
How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Julia programming language
Table of Contents
Problem Statement
The purpose of this task is to create a version of an Elementary cellular automaton whose number of cells is only limited by the memory size of the computer. To be precise, consider the state of the automaton to be made of an infinite number of cells, but with a bounded support. In other words, to describe the state of the automaton, you need a finite number of adjacent cells, along with their individual state, and you then consider that the individual state of each of all other cells is the negation of the closest individual cell among the previously defined finite number of cells. Examples: More complex methods can be imagined, provided it is possible to somehow encode the infinite sections. But for this task we will stick to this simple version.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Julia programming language
Cellular automata simulate systems of discrete cells in which local rules are iteratively applied to all cells, resulting in global patterns.
The provided function ecainfinite
performs an elementary cellular automaton (ECA) simulation with an infinite-length cell array. ECAs are one-dimensional cellular automata with a simple rule that determines the next state of each cell based on its own current state and the states of its two neighboring cells.
Function Details:
-
ecainfinite(cells, rule, n)
takes three arguments:cells
: Initial cell state as a string of '1's and '0's.rule
: Rule number for the ECA (8-bit integer, default is 30).n
: Number of iterations or rows to generate (default is 25).
-
notcell(cell)
: Helper function that flips '1' to '0' and vice versa. -
rulebits
: Stores the binary representation of the ECA rule, padded with zeros to ensure an 8-bit length. -
neighbors2next
: Dictionary mapping binary strings representing three consecutive cells to the next state of the middle cell according to the ECA rule. -
ret
: Array of strings to store the generated cell arrays for each iteration. -
The Main Loop:
- Iterates over
n
iterations, generating each row of the ECA. - Pads the current cell array with four additional '0's at each end to account for edge effects.
- Computes the next row by applying the ECA rule to three-cell neighborhood pairs (excluding the padded cells).
- Stores the new row in the
ret
array.
- Iterates over
-
testinfcells(lines::Integer)
: Tests theecainfinite
function by generating 25 rows of ECA simulations with rule numbers 90 and 30.
Usage:
- Calling
testinfcells(25)
withlines=25
generates and prints 25 rows of ECA simulations for rules 90 and 30.
Output:
The output shows the generated cell arrays for each iteration, with '#' representing '1's and '.' representing '0's.
Sample Output with Rule 90:
Rule: 90 (00001011)
1: ..#...
2: ..##...
3: .#..#..
4: ##.#.#.
5: .#.#.##
6: #..#..#
7: ##.#...
8: ..##.#.
9: #.#..##
10: .##..#.#
11: ..#..#.##
12: #..#..#..
Sample Output with Rule 30:
Rule: 30 (00011110)
1: ...#...
2: ....##..
3: ....##..
4: .....##.
5: ......##
6: .......#
7: ......##
8: ......##
9: ......##
10: .....###
11: ....##.#
12: ....###.
Source code in the julia programming language
function ecainfinite(cells, rule, n)
notcell(cell) = (cell == '1') ? '0' : '1'
rulebits = reverse(string(rule, base = 2, pad = 8))
neighbors2next = Dict(string(n - 1, base=2, pad=3) => rulebits[n] for n in 1:8)
ret = String[]
for i in 1:n
push!(ret, cells)
cells = notcell(cells[1])^2 * cells * notcell(cells[end])^2 # Extend/pad ends
cells = join([neighbors2next[cells[i:i+2]] for i in 1:length(cells)-2], "")
end
ret
end
function testinfcells(lines::Integer)
for rule in [90, 30]
println("\nRule: $rule ($(string(rule, base = 2, pad = 8)))")
s = ecainfinite("1", rule, lines)
for i in 1:lines
println("$i: ", " "^(lines - i), replace(replace(s[i], "0" => "."), "1" => "#"))
end
end
end
testinfcells(25)
You may also check:How to resolve the algorithm Knuth's algorithm S step by step in the Ruby programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Tcl programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the Racket programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the BASIC programming language
You may also check:How to resolve the algorithm Bioinformatics/Sequence mutation step by step in the Quackery programming language