7

I want to illustrate the iterations of a cellular automaton picture-by-picture in some slides. For those not familiar with the topic, imagine a two-dimensional grid of square cells where each square in the grid can have a "state" (which will be represented by its color). Every iteration or timestep, the state of a cell/square changes depending on its surroundings and current state - its color should change in the next picture.

It seems obvious to use TikZ since it directly supports drawing of grids, but the part where I struggle is when thinking of a solution to minimize the effort when coloring every single square, every single iteration - the cells (or "squares") change state with every picture, so I would essentially have to color every square manually for each iteration. That seems like a lot of nasty, unmantainable code will be unavoidable; I essentially know how to solve my problem but wanted to ask if there is (hopefully) a smarter way of drawing/doing this (I'm hoping for some \foreach magic and such.)

Phrasing concrete questions:

  1. Is there, maybe, a package for drawing cellular automatons already?
  2. If not, is there a smarter way to use TikZ to draw many iterations of a cellular automaton than to manually draw the grid and color it by hand?
  3. If those last two questions are too localized/hard: Are there other "control structure" commands similar to \foreach that I could use to draw using some "rules", so that I don't have to color each square by hand?

Update 1

After considering the situation to the best of my knowledge, I want to try an implementation with the matrix command in TikZ. I will define a matrix of empty, minimum-sized and square cells, and change the style of the nodes according to the slide number. This might very well be a suboptimal approach, but since the pgf-magic happening in the answer by Mark Wibrow is still almost undecipherable to me, I think this the best way forward.

Desired Automaton Behavior

In simple terms: a cell is a square in the grid, and connected to its four orthogonal neighbors (up, down, right left). At each point in time, it is either excited, resting, or in refractory phase. For each iteration, the following happens:

  • A cell that is resting goes into the excited state if any of its neighbors are excited themselves.
  • An excited cell goes into the refractory phase.
  • A cell in refractory phase goes into the resting state, regardless of the states of its neighbors.

Here is an image of what the model looks like (n is the time, a dark cell is excited, a striped one is refractory):

Cellular automaton model of excitable medium

I realize my problem is highly localized, and don't mind if I need to adapt potential answers to fit my purposes if they describe a general way the problem could be tackled; what I am trying to learn is elegant ways of bending TikZ to one's will, so those answers are very much welcome.

As of now, I am "painting" the cells by applying the respective style to the node in the matrix. My real matrix is of course bigger than 3

Below is my MWE:

\documentclass[ngerman,compress]{beamer}
\setbeamercovered{invisible}

% Packages
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
\begin{frame}{The cellular automaton}
  \center
  \begin{tikzpicture}
  [help lines/.style={draw=black},
  every node/.style={help lines,rectangle,minimum size=3mm},
  cellular automaton/.style={draw=none,row sep=0mm,column sep=0mm, ampersand replacement=\&},
  rst/.style={fill=white,help lines},
  exc/.style={fill=blue!70,help lines}
  ref/.style={fill=blue!30!white,help lines]

    \matrix[cellular automaton] {
      \node[rst] {};\& \node[rst] {};\& \node[rst] {}; \\
      \node[rst] {};\& \node[exc] {};\& \node[rst] {}; \\
      \node[rst] {};\& \node[rst] {};\& \node[rst] {}; \\
    };
  \end{tikzpicture}
\end{frame}
\end{document}
MaxAxeHax
  • 1,210
  • I would just draw your attention to Section 63 ("Syntax for mathematical expressions") of the TikZ/PGF manual. In particular, note the x ? y : z syntax, which could be useful for deciding the colour of a cell (e.g. ... ? black : white). Note also that TikZ supports arrays (via the syntax {a,b,c}), which may be useful for storing your data. I don't know if it supports 2D arrays directly. – John Wickerson Jun 09 '13 at 20:06
  • @percusse Thanks for your answers. Would you mind to ellaborate on the name of the package (answer 1)? – MaxAxeHax Jun 09 '13 at 20:07
  • TikZ has a full-fledged automata library. – percusse Jun 09 '13 at 20:10
  • @John Thanks for the hint, the TikZ documentation is really awesome and I wish I could have gone through all of it already. Guess it takes more than a few weeks to get to know what it has to offer... – MaxAxeHax Jun 09 '13 at 20:11
  • 1
    While we’re impatiently waiting for a MWE ;) could you show us an image (from the web, another application or drawn by hand)? – Qrrbrbirlbel Jun 09 '13 at 20:52
  • @Qrrbrbirlbel Something like this, in a smaller sense. – MaxAxeHax Jun 09 '13 at 21:34
  • @percusse The automata library doesn't really cover my use case, since it's mainly for finite automata and Turing Machines... but thanks for teaching me that it exists! (since it will certainly prove useful in other contexts) – MaxAxeHax Jun 09 '13 at 21:36
  • @MHaaZ So it’s either \usetikzlibrary{epilepsy} or \usetikzlibrary{drugs.lsd}? ;D But seriously, do you have a kind of function for every cell? Is there some mathematical rule? If you have that, we could certainly wrap that inside two \foreach loops and use that function to color the cell. Or is it more path like? Is it recursive? It looks a bit like a Mandelbrot set and maybe the lindenmayersystems library. – Qrrbrbirlbel Jun 09 '13 at 21:47
  • @Qrrbrbirlbel I don't want this to get too technical and off-topic, but yes, there is a mathematical rule at play. The state of every cell in an iteration n+1 depends on the state of its "neighborhood" (itself and the 4 cells adjacent to it) at iteration n. This simulates diffusion in an excitable medium (for example flutter in heart tissue). I will try to edit this into the question without delving too deep tomorrow, but right now I'm going to sleep a couple hours... – MaxAxeHax Jun 09 '13 at 22:04
  • 1
    @MHaaZ You have to set your threshold and your use case explicitly. You are so far evaluating the options presented to you which is not efficient. If you can provide a test case then people will give you a sharp answer. Mark Wibrow is one of the TikZ team so it goes hardly better than that. So your input and contribution is required. – percusse Jun 10 '13 at 15:36
  • @percusse You are absolutely right. I should not have posed the question yesterday, since I knew I would lack the time to make it comfortable and fun to engage with. Should have waited until today when I knew I had the time to go into necessary detail, but that's me: a little overeager and impatient. Added a MWE and explanation of the expected behavior... better late than never, I guess. – MaxAxeHax Jun 10 '13 at 18:46