Custom Cellular Automata rule with k colors
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][[2]]]/@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