# Custom Cellular Automata rule with k colors

How can I use the CellularAutomaton function to create a 2D automaton using a custom function and k colors?

I understood from this post how to use custom rules for 1D automata.

I'm trying now to do that for 2D automata but I don't quite understand how CellularAutomaton is reacting to this.

I tried this to see what the general function gets:

CellularAutomaton[{Print[#] &, {}, {1, 1}}, {{{1}}, 0}, 1]


And I see that I get a matrix representing, I assume, the possible cases for rules Compare this to the 1D case But I still don't completely understand how to interpret this matrix and how to use more colors.

My final goal is to create this kind of cyclic automata using the CellularAutomaton function

Any kind of help would be useful. Thanks

• Can you put more information? I mean what is the input of your function and how you plotted it? – wandermonde May 27 at 7:07
• @wandermonde I solved it. The approved answer is the one I did. About the input, it's just an implementation of this paper (math.ucdavis.edu/~gravner/papers/cca.pdf). To plot it it's just an ArrayPlot – xtian777x May 28 at 13:33
• @wandermondeI'm actually working on a computational essay about this. It's not ready yet (and will probably be posted in the Wolfram Community). Once it's done I can let you know here. If you have other questions please let me know. – xtian777x May 28 at 13:37
• @wandermonde You can see other preliminary CCA work I did here: wolfr.am/MSTvSyVA – xtian777x May 28 at 13:56

I did it. It's probably not the best implementation but works. Any feedback about how to optimize the code would be highly appreciated.

CyclicFunction[N_,matrix_,preylist_]:=Module[{cell=matrix[[2,2]],prey},prey=preylist[cell];If[MemberQ[Flatten[matrix],prey],prey,cell]]

CyclicCellularAutomaton[k_,size_,steps_]:=With[{preylist=Association@@Function[u,u-> RotateLeft[Range[0,k-1],u][]]/@Range[0,k-1]},CellularAutomaton[{CyclicFunction[k,#,preylist]&,{},{1,1}},RandomInteger[{0,k-1},size],{{{steps}}}]]


BTW, I exported the evolution of the automaton as a gif but doesn't loop infinitely...why is that? Every possible 2D function is enumerated in CellularAutomaton, so you need merely pick the function number. To get your desired colors, use ColorFunction, e.g.,

ArrayPlot[
CellularAutomaton[{746,
{2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}},
{1, 1}},
{{Table[1, {7}]}, 0}, {{{150}}}],
ColorFunction -> "Rainbow"]


or

ColorRules -> {0 -> Red, 1 -> Green, _ -> Black}


If you want to write your own function, look at the documentation for CellularAutomaton to see examples, such as:

CellularAutomaton[{Total[#] &, {}, 1/2}, {{1}, 0}, 5] // Grid

• How do you go from an arbitrary function to a function number? I want to write my own function, i.e. using the rule form {fun,{},rspec} – xtian777x Mar 16 '17 at 20:44
• As I mentioned in my post, I already know how to write my own function for 1D. What I'm asking is how to do that for 2D automata. Examples in the ref pages are for 1D – xtian777x Mar 16 '17 at 20:48