1
$\begingroup$

By default, initial conditions specified to CellularAutomaton are cyclic:

enter image description here

I know that if I add a background, then I avoided the cyclicity, but the automaton then extends beyond the size of the initial condition:

enter image description here

Is there a way to get a cellular automaton that neither wraps around, nor extends beyond the initial range (by taking values outside of the initial range to always keep the background value)? This is different than allowing the automaton to extend and then cut off back to the initial region. Consider the following example:

enter image description here

There, I'd like the automaton to stay inside the first three cells, so that the successive states would be {0,1,0}, {1,1,1}, {1, 0, 1}, {1, 1, 1}, {1, 0, 1}...

I know that I could achieve this, in principle, by adding a third color, setting the background to that third color, and create a rule that 1. counts the third color as zero, 2. never changes the third color into anything else. But because the software counts the colors as {0,1,2...}, if I wanted to encode the rule using a totalistic rule number, a background cell would count as 2 alive cells. So I'd need to handwrite my own cellular automaton map... but then what's the point in using CellularAutomaton to being with. I'd like to avoid this hack.

$\endgroup$

2 Answers 2

1
$\begingroup$

I'm fairly certain there's no built-in way to do this. If you really want it, you'll have to hack it out one way or another.

Depending on how many iterations you're planning to do, an easier workaround might be something like

 NestList[ArrayPad[CellularAutomaton[ruleNum, #][[2 ;; -2]], 1] &, 
  initialRow, numRows]

e.g. example

Note this will be slower than a one-big-size CellularAutomaton call, but that only starts to matter up around 10-100k range, and there's the side benefit of smaller memory footprint.

$\endgroup$
1
  • $\begingroup$ (Depending on what you're doing, you could maybe make that a bit more efficient by changing it to a NestWhileList and stopping if it sees it's repeating itself.) $\endgroup$
    – Trev
    Dec 11, 2022 at 16:26
0
$\begingroup$

We can run CellularAutomation one step at a time and adjust the output as needed before calculating the next step.

BoundedCellularAutomaton[rule_, initialvalue_, steps_, bound_] := 
 Module[{currentvalue, output = {}},
  currentvalue = initialvalue;
   AppendTo[output, CenterArray[currentvalue[[1]],bound]];(*put input in to output array, pad to correct length*)
   Do[
    currentvalue = CellularAutomaton[rule, currentvalue];(*Calculate next line*)
     (*If we are out of bounds, make ends = 0*)
     If[Length[currentvalue[[1]]] > bound,
      currentvalue = ReplacePart[currentvalue, {{1, 1}, {1, -1}} -> 0]];

    AppendTo[output, CenterArray[currentvalue[[1]], bound]];,(*add current line to output array,
    pad to correct length**)
    steps]; (*loop*)
  Return[output]]  

BoundedCellularAutomaton[{4 + 2, {2, 1}}, {{0, 1, 0}, 0}, 11, 21]
ArrayPlot[%]

{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0},
{1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}}
enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.