Message Boards Message Boards

[JAM] CellularAutomaton Code Jam Wolfram Summer School 2015

Wolfram Science Summer School and Wolfram Innovation Summer School are happening now in Boston at Bentley University. The goal of this Code Jam is to post interesting code snippets that fit the Wolfram Language functionality described in detail below. By "interesting" I mean simple programs that generate complex patterns. We will be jamming with our students, but everyone is welcome to join. Let's have some fun!

In this Code Jam we will take a look at the CellularAutomaton (CA) function and its syntax for rules defined as functions. This may sound a bit confusing, so let's take a look at some examples. First, if you haven't yet, you have to make yourself familiar with the CellularAutomaton function. The most well known cases are integer Wolfram indexes for CA rules, for instance for the celebrated rule 30:

ArrayPlot[CellularAutomaton[30, RandomInteger[1, 100], 50]]

enter image description here

But CellularAutomaton can take a function as a rule. Below are some examples. Use an arbitrary symbolic function f as the rule to apply to range-1 neighbors:

CellularAutomaton[{f[#] &, {}, 1}, {a, b, c}, 1]

{{a, b, c}, {f[{c, a, b}], f[{a, b, c}], f[{b, c, a}]}}

Set up a "Pascal's triangle cellular automaton":

CellularAutomaton[{f[#] &, {}, 1/2}, {a, b, c}, 1]

{{a, b, c}, {f[{c, a}], f[{a, b}], f[{b, c}]}}

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

{{1, 0, 0, 0}, {1, 1, 0, 0}, {1, 2, 1, 0}, {1, 3, 3, 1}}

Additive cellular automaton modulo 4:

ArrayPlot[
 CellularAutomaton[{Mod[Total[#], 4] &, {}, 1}, {{1}, 0}, 50], 
 ColorFunction -> "Rainbow"]

enter image description here

The second argument to the function is the step number:

CellularAutomaton[{f[#1, #2] &, {}, 1}, {a, b, c}, 1]

{{a, b, c}, {f[{c, a, b}, 1], f[{a, b, c}, 1], f[{b, c, a}, 1]}}

CellularAutomaton[{f, {}, 1}, {a, b, c}, 1]

{{a, b, c}, {f[{c, a, b}, 1], f[{a, b, c}, 1], f[{b, c, a}, 1]}}

Change the rule at successive steps; #2 gives the step number:

ArrayPlot[
 CellularAutomaton[{Mod[Total[#] + #2, 4] &, {}, 1}, {{1}, 0}, 30], 
 ColorFunction -> "Rainbow"]

enter image description here

Use continuous values for cells:

ArrayPlot[CellularAutomaton[{Mod[Total[#]/2, 1] &, {}, 1}, {{1}, 0}, 50]]

enter image description here

Manipulate[
 ArrayPlot[
  CellularAutomaton[{Mod[s Total[#], 1] &, {}, 2}, {{1}, 0}, 50],
  ColorFunction -> "Rainbow", PixelConstrained -> 3]
 , {s, .01, .99}]

enter image description here

Try different functions and different initial conditions. They can be strings, numbers, graphics or expressions. Try ArrayPlot but also try other visualization tools like Grid. Try them out. Post code, images, and text comments. You can also comment on other people's code.

POSTED BY: Vitaliy Kaurov
46 Replies

enter image description here -- you have earned Featured Contributor Badge enter image description here Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

POSTED BY: Moderation Team
Attachments:
POSTED BY: Richard Gaylord
POSTED BY: Udo Krause
POSTED BY: Todd Rowland
POSTED BY: Richard Gaylord

Todds answer was so clear and professional, nevertheless you dig in again

it bothers me a great deal that stephen uses terms like "simple program" without defining what simple means.


Question: Is the following a simple program?

In[17[:= N[I^I, 18]
Out[17]= 0.207879576350761909 + 0.*10^-19 I

with $Mathematica$, yes. Otherwise you have some trouble relying on Leonhard Eulers clarification what the logarithm is. If I understand Stephen Wolfram a little bit he follows the idea of Richard P Feynman, that quantum electrodynamics is in a way far to complicated (or elaborated (or elegant)) because a photon or electron do for sure not do such calculations during scattering: Feynman proposed to search for another way to understand that unbelievable efficiency, correctness and parallelism that particles show. Feynman was also one of the first to discuss physics and computation. And Mr Wolfram pushed simple programs as a candidate.


If you look into the garden you see effortless growing plants, Erwin Schrödinger asked about cleavage, Albert Einstein detected that there is no such thing as central time, but instead of that something propagates, evolves, steps, does the right thing to keep things running ... that step is hoped to be simple, because every entity does it, only we do not see: what and how.

POSTED BY: Udo Krause

i take umbrage at you first sentence which seems to be a personal attack.. of course todd's response is professional - did i say otherwise? and his explanation is quite clear. he says "what is meant by "simple" is elusive and cannot be meaningfully defined in a rigorous way, so it is a matter of opinion.". as for your example, i don't consider it to be a simple program (or even a program) . but then, that's my opinion.

POSTED BY: Richard Gaylord

Please accept my apologies, my post intended neither to attack posts nor its posters.


Terms like simple programs, which

cannot be meaningfully defined in a rigorous way

invoke discussions and discussion means by chance to disagree in a way.

I tend to muse about simple programs under the aspect of a triple $\{ protocol, algorithms, data\}$. Data include units and specify the matter, data are dumb algorithms (Charles Petzold), algorithms do the work and a protocol is needed to do I/O.Lets stay in the area of getting intuition: Is it simple to travel from Pisa to Chicago?

If the data is your own body, the algorithm is walking and swimming and the protocol is self-motivation, it is impossible.

If the data is a plane maintained by an airline, the algorithm is flying and the protocol is boarding to that plane, it is simple.

Progress is made by changing the protocols or better inventing new protocols, implementing data and algorithms (in the widest sense) to them, letting people do things they never dreamt off. $Mathematica$ did excatly that in its realm.

In Heisenbergs book "The Part and The Whole" one finds discussions about the symbol as a mental picture to bring facts to understanding and keep things (which are by no means simple) simple.


Now for something completly different. Expressions like

ArrayPlot[CellularAutomaton[{Count[
     Last[Last[TuringMachine[2016, {1, Mod[#1, 2]}, #2 Total[#1]]]], 1] &, {}, 1}, {{1}, 0}, 100]]

gave reason to ask why they are considered as simple programs. In a way they are accepted as programs by the question, right?

Why then

N[I^I, 18]

is considered not to be program? It is in the same syntax, follows the same protocol as the above CA program, but invokes other algorithms.

POSTED BY: Udo Krause
POSTED BY: Richard Gaylord

why do you consider these to be simple programs?

becuase they are short in length - if so, would that make any program that is simply a function call to CelularAutomaton, perhaps surrounded by visual display commands such as ArrayPlot simple?

because the algorithm used in the built-in CellularAutomaton function is hidden (i don't know if it's written in WL or in C or uses hash tables or what it is actually DOING when it is evaluated) and one only has to type CellularAutomaton?

stephen wrote in NKS that a complex cellular automaton is not a simple program.

for example, is the forest fire CA a simple program?

it can be written using the CellularAutomaton function (http://forum.wolframscience.com/archive/topic/720-1.html) or it can be written in a way which is very simple to understand (http://www.cs.berkeley.edu/~fateman/papers/cashort.pdf)

btw - one can see in the unpublished Fateman manuscript, the program written in LISP which IMO is simply awful looking and essentially unreadable and when compared to the program written in WL is a very clear example of why WL is a MUCH better language for writing computer simulation programs.

does simplicity refer to readability or to the ease with which one can understand the CA?

e.g. the Game of Life as written using CellularAutomaton is very short but totally opaque (at least to me) while if it is written in another way (e.g. see http://library.wolfram.com/infocenter/MathSource/5216 - see the LifeGame program in the last section) it is easy to understand.

this is actually a very important question because as Kovas Boguta has pointed out in his article (i attach it becuase it is not readily available to everyone) "NKS seeks to introduce a basic, empirical science that investigates the behavior of very simple programs." and as Boguta points out in the original unedited version of this article in the NKS Forum (http://forum.wolframscience.com/showthread.php?s=6e483fc3d86a53032f8cd96e48e7a500&threadid=271&highlight=kovas+boguta) "NKS seeks to introduce a basic, empirical science investigating the behavior of very simple programs. A very important part of the intellectual structure of NKS is how this relates to modeling nature--but although interesting in its own right and important for justifying the entire enterprise, this tie-in with the natural world is absolutely not the subject of the core science. "

note - actually, the insightful comments of Boguta actually indicate that NKS really should have been called NS becuase it is proposing an entirely new field of study - although Richard Feynman would say that it is not a science at all because it does not study nature (https://www.youtube.com/watch?v=lL4wg6ZAFIM).

i apologize for the length of this post and ask for the moderator's indulgence. i am posting this here becuase while i was planning to discuss this at the 2015 Wolfram Technology Conference in October, my health prevents my doing it there. also, the Wolfram community deserves to understand what NKS is all about becuase it has been horribly distorted and totally misrepresented by so many others (e.g. those who claim that stephen views the universe as a CA which he most definitely does not - he uses a trivalent causal net as a model as explained in NKS) and who don't even understand the 'science' that stephen is actually introducing which is totally original, whether one is interested in it or not. and WL is an integral part of the NKS research program (which stephen siads is one of the reasons he created WL)

Attachments:
POSTED BY: Richard Gaylord

Total recall

ArrayPlot[CellularAutomaton[{Count[Last[Last[TuringMachine[2016, {1, Mod[#1, 2]}, Total[#1]]]], 
     1] &, {}, 1}, {{1}, 0}, 100]]

turing6

with a factor

ArrayPlot[
 CellularAutomaton[{Count[
     Last[Last[TuringMachine[2016, {1, Mod[#1, 2]}, #2 Total[#1]]]], 
     1] &, {}, 1}, {{1}, 0}, 100]]

turing7

The following both are completely different

ArrayPlot[
 CellularAutomaton[{Count[Last[Last[TuringMachine[1959, {1, Mod[#1, 2]}, #2 Total[#1]]]], 
     0] &, {}, 1}, {{1}, 0}, 100]]

ArrayPlot[
 CellularAutomaton[{Count[Last[Last[TuringMachine[1959, {1, Mod[#1, 2]}, #2 Total[#1]]]], 
     1] &, {}, 1}, {{1}, 0}, 100]]

but toggling

ArrayPlot[
 CellularAutomaton[{Count[Last[Last[TuringMachine[1959, {1, Mod[#1, 2]}, #2 Total[#1]]]], 
     Mod[#2, 2]] &, {}, 1}, {{1}, 0}, 100]]

brings not too much.

POSTED BY: Udo Krause
POSTED BY: Udo Krause

This decays fast

ArrayPlot[CellularAutomaton[{Mean[Fourier[#1]] &, {}, 1}, RandomComplex[{-(10 + I), 1 + 10 I}, 100], 100]]

sorry

fourierCA

Also a bit boring

ArrayPlot[
 CellularAutomaton[{If[OddQ[#2], 
     GeometricMean[DeleteCases[#1, Chop[#] == 0 &]], 
     ContraharmonicMean[#1]] &, {}, 1}, RandomComplex[{-(10 + I), 1 + 10 I}, 100], 200]]

as seen here

meanCA

Improving the Fourier a bit

ArrayPlot[
 CellularAutomaton[{If[OddQ[#2], 
                       Mean[Abs /@ Fourier[#1]], 
                       Max[Abs /@ InverseFourier[#1]]] &, {}, 2},  RandomComplex[{-(10 + I), 1 + 10 I}, 100], 
                   100]]

into

fourierForcedCA

prime numbers have always their attraction

ArrayPlot[
 CellularAutomaton[{If[OddQ[#2], 
                       Mean[Abs /@ Fourier[#1]], 
                       Max[Abs /@ InverseFourier[#1]]] &, {}, 1}, 
                    Table[Prime[o] + I Prime[o + 1], {o, 1, 201, 2}], 100], ColorFunction -> "Rainbow"]

as seen here (but it is still the same idea)

enter image description here

POSTED BY: Udo Krause

This does not use Total

ArrayPlot[CellularAutomaton[{Fold[BesselJ, #] &, {}, 1}, {{1.}, 0}, 100]]

but looks conventional

foldBessel

This turns back to the Total

ArrayPlot[CellularAutomaton[{Total[
     CoefficientList[Cyclotomic[Total[#], x], x]] &, {}, 1}, {{1}, 0},
   200], ColorFunction -> Hue]

but has not found a convincing color function (yet)

cyclotomicCA

because a ListPlot3D of it shows peaks around 2400

cyclotomicCA3D

POSTED BY: Udo Krause

Nice. One thing apparent from the first picture is that it fades out on the top left diagonal. I like how you found the singularity-like behavior. Encouraging for the digital physics people who try to find CAs for the universe.

POSTED BY: Todd Rowland
POSTED BY: Udo Krause

Not in this codejam. Everything from that is here in this discussion.

Funny you should mention "quantum" because this year we had several people doing related projects. ne person simulated a D-Wave device and another was researching the type of CA with quantum mechanics described by t'Hooft.

POSTED BY: Todd Rowland

These are all really great. Everyone can keep posting.

I enjoy the expressions like

f[{x_, y_}] := Table[x, {Max[y]}]
Grid[CellularAutomaton[{f[#] &, {}, 1/2}, Range[4], 3]]

which gives

enter image description here

POSTED BY: Todd Rowland

Slicing the evolution of 2D Totallistic 3-Color Rule 344373765 across the y-axis and animating the slices resembles a Kraken rising from the sea.

enter image description here

POSTED BY: Sibesh Kar

You can upload GIFs - I fixed it for you. Very nice. But the point of this Code Jam is to use not integer rule specification --- we are using functions as rules --- see some code above. Some folks also misunderstood this and there area a few posts with integer rule specification. It is OK - they all look great. But it would be nice to find some cool looking CAs with functions as rules. You should also post code not only the output.

POSTED BY: Vitaliy Kaurov
Posted 9 years ago
POSTED BY: Jesse Dohmann
Posted 9 years ago

Here are a few I looked at.

ArrayPlot[CellularAutomaton[{Mod[Total[#]^2, 10] &, {}, 1}, {{1}, 0}, 100]]

enter image description here

ArrayPlot@
 CellularAutomaton[{Mod[Total[#] + RandomInteger[{0, 1}],4] /. {0 -> 0, 1 -> 0, 2 -> 1, 3 -> 1} &, {}, 1}, 
                   {{1, 1, 1}, 0}, 200]

enter image description here

ArrayPlot@
 CellularAutomaton[{Sqrt@StandardDeviation[#] &, {}, 1}, {{1.}, 0}, 200]

enter image description here

ImageAdjust@Image[
 CellularAutomaton[{Mod[Tr[#] + Tr[Reverse[#]], 7] &, {},
                   {1, 1}}, {{{1}}, 0}, {{{20}}}]]

enter image description here

POSTED BY: K. James

These are all good. The Standard Deviation one is like Juan's above, and the second one is like Vitaliy's.

The first one doesn't ring any bells and looks like a new behavior to me.

The last one is an outer totalistic 7 color rule with only diagonal neighbors. I prefer your expression with trace (Tr), though it can also be written as

ArrayPlot[
 CellularAutomaton[{3020027333325105171577645034668, {7, {{1, 0, 
      1}, {0, 2, 0}, {1, 0, 1}}}, {1, 1}}, {{{1}}, 0}, {{{20}}}]]
POSTED BY: Todd Rowland
POSTED BY: Richard Gaylord

i have tried the 1D forest fire CA using a rule involving pattern matching and i get error messages that i can't even understand.

Where is your code?

POSTED BY: Udo Krause

Here it is:

pyro =
          (treeGrowIgnite = #/. {0 :> Floor[1 + p - Random[]], 1 :> Floor[2 + f - Random[]]};
           forestIgnite = TreeGrowIgnite //. 
                                  {{2, c___, 1} -> {2, c, 2},
                                   {1, c___, 2} -> {2, c, 2},
                                   {a___, 2, 1, b___} -> {a, 2, 2, b},
                                   {a___, 1, 2, b___} -> {a, 2, 2, b}};
             forestNew = forestIgnite /. 2 -> 0)&

the first 2 pattern matching replacement rules are apparently not needed becuase they represent wrap around b.c. which are used by default in CellularAutomaton.

note: sorry that this code appears so badly. i write it in one way but in the preview window, what i write is changed into something which is unreadable and sometimes terms are even dropped or changed. i don't know why.

POSTED BY: Richard Gaylord
POSTED BY: Udo Krause
Attachments:
POSTED BY: Richard Gaylord

CellularAutomaton Code Jam Wolfram Summer School 2015

POSTED BY: Richard Gaylord
Posted 9 years ago
ArrayPlot[CellularAutomaton[{Sin[Total[N@#]] &, {}, 1}, {{1}, 0}, 50],
  ColorFunction -> "Rainbow"]

<code>enter image description here

POSTED BY: Tingting Huang

Compare this to the version where the total is renormalized so the argument to Sin ranges from 0 to 2Pi

ArrayPlot[
 CellularAutomaton[{Sin[Total[N@#] 2 Pi/3] &, {}, 1}, {{1}, 0}, 250]]

That gives something random looking whereas renormalizing to make it range from 0 to Pi instead gives similar behavior. I wonder what the uniform regions are like.

POSTED BY: Todd Rowland
Posted 9 years ago
ArrayPlot[
 CellularAutomaton[{Mod[
     Total[FactorInteger[FromDigits[#, 3]][[All, 1]]], 4] &, {}, 
   1}, {{4}, 1}, 400], ColorRules -> {1 -> White}, ImageSize -> Full]

enter image description here

POSTED BY: Victor Phan

Very cool. Reminds me of shifted 225 combined with ordinary rule 225 (see e.g. p.58 ).

POSTED BY: Todd Rowland
NearestNeighborGraph[
 Position[CellularAutomaton[{604419492, {3, 1}, {1, 1}}, {{{1}}, 
    0}, {10, All, All}], 1]]

enter image description here

POSTED BY: Wenzhen Zhu
Graphics3D[{Opacity[0.7], RandomColor[], Sphere[#]} & /@ 
  Position[CellularAutomaton[{604419492, {3, 1}, {1, 1}}, {{{1}}, 
     0}, {10, All, All}], 1], ViewVertical -> {-1, 0, 0}]

enter image description here

POSTED BY: Wenzhen Zhu

Here's a nested structure from the Standard Deviation of the Square Root of the argument

ArrayPlot[
 CellularAutomaton[{StandardDeviation[#^.5] &, {}, 1}, {{1}, 0}, 25], 
 ColorFunction -> "Rainbow"]

enter image description here

This one starts off as nested and then becomes complex, after step 25 it is all complex. Not sure if it has localized structures or not.

POSTED BY: Todd Rowland

Simple function involving the sum of two cosines whose frequencies ratio is the golden ratio.

g[x_] := Abs[Cos[x] + Cos[N[GoldenRatio] x]];
ArrayPlot[CellularAutomaton[{g[Total[#]] &, {}, 1}, {{1}, 0}, 200],ColorFunction -> GrayLevel]

enter image description here

POSTED BY: Carlo Giacometti

An awesome idea to use a quasi-periodic function ! This is how the function looks plotted:

g[x_] := Abs[Cos[x] + Cos[N[GoldenRatio] x]]
Plot[g[x], {x, 0, 200}, AspectRatio -> 1/5, PlotTheme -> "Scientific", Filling -> Bottom]

enter image description here

POSTED BY: Vitaliy Kaurov

If anyone wants more quasiperiodic awesomeness, here's a superposition of five cosines whose wave vectors form a pentagon (this gives a ten fold symmetry, but if you take any direction you will find no repeating pattern):

PlaneWave[x_List, k_List] := Cos[k.x];
c1 = 1/4*(Sqrt[5] - 1);
c2 = 1/4*(Sqrt[5] + 1);
s1 = 1/4*(Sqrt[10 + 2*Sqrt[5]]);
s2 = 1/4*(Sqrt[10 - 2*Sqrt[5]]);
kvectors = {{0, 1}, {s1, c1}, {s2, -c2}, {-s2, -c2}, {-s1, c1}};
DensityPlot[
 Abs[Total@Map[PlaneWave[{x, y}, #] &, kvectors]], {x, -40, 40}, {y, -40, 40}, 
ColorFunction -> GrayLevel, PlotPoints -> 50]

enter image description here

POSTED BY: Carlo Giacometti

Yes, this is neat. Have you seen this:

Quasicrystal Animation with Mathematica

enter image description here

POSTED BY: Vitaliy Kaurov

By the way the author of the video above, Richard Hennigan, also made a Demonstration: Quasicrystals

enter image description here

POSTED BY: Vitaliy Kaurov

These numerical rules are interesting because they depend on the precision. It appears that the general effect is valid, of having a detailed inner structure. However the exact details are difficult to compute numerically.

Compare with increased precision

g2[x_] := Abs[Cos[x] + Cos[SetPrecision[GoldenRatio, 100]* x]];
ArrayPlot[CellularAutomaton[{g[Total[#]] &, {}, 1}, {{1}, 0}, 200], 
 ColorFunction -> GrayLevel]

enter image description here

POSTED BY: Todd Rowland
ArrayPlot[  CellularAutomaton[{Mod[Total[#1]/N[E], #2] &, {}, 1}, {{1},  0}, {{1000, 1300}, {0, 300}}], PixelConstrained -> 1]

enter image description here

POSTED BY: Ashkan Akbariyeh

If you take a very slowly growing continuous value CA but look very far in its future evolution you can find something interesting

ArrayPlot[
 CellularAutomaton[{Mod[Total[#]/2.96, 1] &, {}, 1}, {{1}, 0}, 
{{1000, 1300}, {-90, 90}}], PixelConstrained -> 2]

enter image description here

POSTED BY: Vitaliy Kaurov

This reminds me of the similar rules from the book, like adding a constant .9 to the mean on p.243

POSTED BY: Todd Rowland
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract