Question

How can I have random movements in my Cellular automaton model? For example, if the elements in a cell is much more than two or more neighboring cells I'd like to randomly choose a few neighbors to give some elements. I tried all the codes that came to my mind but my problem is that in Mathematica I have to be sure that the same time an element is living from a cell and is going to another. I thought of doing it using conditions but I am not sure how. Can anyone please help me?

Edit: the code I used so far

My actual code is very complicated so I will try to tell you what I have done with a simpler cellular automaton. I wanted to succeed movements in a Moore neighbourhood. Every cell in my cellular automaton has more than one individual (or none). I want to make random movements between my cells. I couldn't do this, so I tried the following code and in my cellular automaton I used it like you can see below.

w[i_, j_] := 
  If[(i - 4) > j, -1, If[(i - 4) == j, 0, If[(j - 4) > i, 1, 0]]];


dogs[p, p1, p2,p3,p4,p5,p6,p7,p8]:=newp &[
  newp = w[p, p1] + w[p, p2] + w[p, p3] + w[p, p4] + w[p, p5] + 
    w[p, p6] + w[p, p7] + w[p, p8]]

This code is doing movements, but is not exactly what I want because if a cell has 0 individuals in it and its neighbours all 5, then at the end it has 8 and its neighbours 4 but I don't want that because I don't want the cell with the less individuals in it to have more than its neighbours at the end. I want all of them to have close values in them and still have movements. I don't know how to do that in Mathematica.

Was it helpful?

Solution

A cellular automaton is not particularly complicated, so my first point of advice is to figure out exactly what you want. Then, I recommend you separate the classical transition rules from the "random" aspect you're introducing.

For instance, here is my implementation of Conway's Game of Life:

 (* We abbreviate 'nbhd' for neighborhood *)
 getNbhd[A_, i_, j_] := A[[i - 1 ;; i + 1, j - 1 ;; j + 1]];

 evaluateCell[A_, i_, j_] :=
   Module[{nbhd, cell = A[[i, j]], numNeighbors},

    (* no man's land edge strategy *)
    If[i == 1 || j == 1 || i == Length[A] || j == Length[A[[1]]],
       Return[0]];

    nbhd = getNbhd[A, i, j];
    numNeighbors = Apply[Plus, Flatten[nbhd]];

    If[cell == 1 && (numNeighbors - 1 < 2 || numNeighbors - 1 > 3),
      Return[0]];
    If[cell == 0 && numNeighbors == 3, Return[1]];
     Return[cell];
  ];

 evaluateAll[A_] := Table[evaluateCell[A, i, j],
    {i, 1, Length[A]}, {j, 1, Length[A[[1]]]}];

After performing evaluateAll, you can search through the matrix for "lonely" cells and move them as you please.

For additional information about how the code works, and to see examples of the code in action, see my blog post on Conway's Life. It includes a Mathematica notebook with the full implementation and plenty of examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top