-3

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.

noni
  • 67
  • 1
  • 9
  • 2
    Your question is general, it amounts to requesting a full implementation. Please show us what you've tried so far. – Szabolcs Jul 25 '11 at 12:17
  • ok, I will edit my question with what I have done until now. – noni Jul 25 '11 at 12:42
  • 2
    I tried to clean up the writing of your question, but the descriptions you provide are so vague that I have a hard time doing this. You don't define any of the terms you use. What are the 'elements' of your cell? What is 'living from a cell', what are your 'movements between my cells'? Please note that the second part of the listed code is not a correct Mathematica statement, so please correct this. Could you add drawings or diagrams to explain your problem better? I'm tempted to vote to close this question given its poor phrasing. – Sjoerd C. de Vries Jul 25 '11 at 15:49
  • @Sjoerd C. de Vries By movements between my cell I mean that I want to take one element from a cell and randomly give to another. For example if a cell has 5 elements in it and one of its neigbours has 1 then I would like to give it one. So the first cell will have 4 and the second 2. But I want to check the cell with all of its neigbours and randomly choose a cell to give an element – noni Jul 25 '11 at 18:59
  • Are you trying to model a transport problem? – Dr. belisarius Jul 25 '11 at 21:15
  • It's a population simulation. And the real issue is not a Mathematica one, but a lack of clarity about the model. – Verbeia Jul 25 '11 at 21:26

1 Answers1

2

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.

JeremyKun
  • 2,987
  • 2
  • 24
  • 44