Message Boards Message Boards

1
|
15268 Views
|
10 Replies
|
15 Total Likes
View groups...
Share
Share this post:

Problems defining conditional functions in Conway´s game of life

Posted 11 years ago
Hello friends, thank you for helping me with this issue...
    I want to reproduce the animations of the Conway´s game of life in Mathematica. You can find the description and the rules in Wikipedia (http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)

    I first started defining a grid with dimensions II vs JJ, being these values predefined in the sheet. The initial values in the sheet, a[ i,j ], are defined also (a[ i,j ]=1 is a "live" cell, and a[ i,j ]=0 is a "dead" cell). The values of a outside the grid are defined as zero, as follows:
   Table[a[0, j] = 0, {j, 0, JJ+1, 1}]
   Table[a[i, 0] = 0, {i, 0, II+1, 1}]
   Table[a[II + 1, j] = 0, {j, 0, JJ+1, 1}]
   Table[a[i, JJ + 1] = 0, {i,0, II+1, 1}]
   a[II + 1, JJ + 1] = 0


   The following values of a, denoted as s, are defined according to the game´s rules, and depends of the value of the eight neighbours (remember a value of 1 for a live cell and 0 for a dead cell); that is, the value of s[ i,j ], depends of the eight-term sum:
    sum = a[i - 1, j - 1] + a[i - 1, j] + a[i - 1, j + 1] + a[i, j + 1] + a[i + 1, j + 1] + a[i + 1, j] + a[i + 1, j - 1] + a[i, j - 1]

the value of s[ i,j ] is then:
   s[i_, j_] := If[1 <= a[i, j] <= 1,   Which[sum <= 1, s[i, j] = 0,    2 <= sum <= 3, s[i, j] = 1,    4 <= sum, s[i, j] = 0],   If[3 <= sum <= 3, s[i, j] = 1, s[i, j] = 0]]

    In order to repeat the last command successively, I set the new values of a to be the values of s
     Clear[a]
     a=s
     Table[a[0, j] = 0, {j, 0, JJ+1, 1}]
     Table[a[i, 0] = 0, {i, 0, II+1, 1}]
     Table[a[II + 1, j] = 0, {j, 0, JJ+1, 1}]
     Table[a[i, JJ + 1] = 0, {i,0, II+1, 1}]
     a[II + 1, JJ + 1] = 0

   The problem appears when I try to recalculate the values of s, using the conditional functions: it gives me the same values of s obtained the last time the function was used. Why does this happen??
   I tried to reset the values of s (Clear ) before evaluating the expression containing the conditional functions, but it also resets the values of a. Can I avoid this situation, and reset only the values of s, maintaining the values of a unaffected??

   Thank you so much, and dont hesitate to email me if you need further information, or a copy of the sheet. Thanksss

   Daniel
   cricricombowino@gmail.com
POSTED BY: Daniel Branco
10 Replies
Daniel,

Before discussing your code, let me point out the implementation in the documentation
http://reference.wolfram.com/mathematica/tutorial/CellularAutomata.html
ArrayPlot /@ CellularAutomaton[{224, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, {1, 1}}, RandomInteger[1, {20, 20}], 30]

and the  implementation  in the NKS book (which was written before the CellularAutomaton function) is a good place to look to learn more about Wolfram Language.

http://www.wolframscience.com/nksonline/page-949a-text

The code you have written is not a typical approach.  One thing going on is you have sum=... which will assign a value to sum.  More usual would be to write
sum[i_,j_]:=a[i - 1, j - 1] + a[i - 1, j] + a[i - 1, j + 1] + a[i, j + 1] + a[i + 1, j + 1] + a[i + 1, j] + a[i + 1, j - 1] + a[i, j - 1]
There are probably some problems with the boundary.
POSTED BY: Todd Rowland
It's sort of like asking "why should we use numbers?"   You are asking lots of questions here, so this will be a long answer.  The short answer is that you shouldn't panic because it something you aren't used to or because the design is not as user friendly as e.g. Factor.  Read this description in the NKS book for general 2D rules  http://www.wolframscience.com/nksonline/page-927d-text
but the totalistic type of rules have a simpler description and don;t involve Sort.

The main point to the rule number is that it is grouped with all of the other rule numbers in its rule space, which is more meaningful than the historical artifact "game of life".

The first thing to understand is the number system in the 1D case.  You take the rule number in base 2 and the nth digit corresponds to the nth neighborhood.  In1D the nth neighborhood is the number n in base 2 (here n starts at 0 unlike the usual Mathematica convention of starting at 1).  That only works because the number in base 2 is 1D, so in higher dimensions the neighborhoods are sorted using the convention of Sort.

For totalistic rules, the conventions are not affected by the dimensions.  Instead of the nth neighborhood, you have the nth possible total, where 0 is a possible total and corresponds to the 2^0 bit of the rule number.  In general the 2^i bit of the rule number gives the result for a neighborhood with the total of i.
http://www.wolframscience.com/nksonline/page-60

For weighted totalistic rules, it is the same except that the total is computed differently.  Each cell in the neighborhood has a weight that you multiply the cells value by before adding them to get the total.  Then you get the total and then lookup the result in the table given by the rule number in base 2.

224=11100000

To say in words, because there are only three ones, it is easiest to state the rule definition by what makes a black cell.  The weighted total has to be  5, 6, or 7.  The weighting scheme for outer totalistic rules have a 1 in the center and the other cells are a 2.  This means that the odd totals are the ones with the center cell black, so either the center cell is black with 2 or 3 neighbors black, or else the center cell is white with exactly three neighbors black.

I don't know how the CellularAutomaton function is implemented, but one doesn't need that level of detail because there is no ambiguity in the result.  There are many ways to implement which are easily verified to be correct, unlike various numeric or optimization things where several different methods may be available and the correctness of results may even be formally undecidable.  There are algorithms for outer totalistic 224 which are faster than the general CA algorithm, e.g. implemented in Golly which can perform extremely large numbers of steps on large arrays of cells, but I don't know if they are implemented in Mathematica.

As for education, I am not a professional educator, but I would recommend not shying away from base 2 numbers.  My guess is that a typical high school graduate has been taught this before (also how to spell).  For the youngest students with a math aptitude, they can learn base 2 numbers pretty easily if they know about even and odd.  True, for other students, the very young with no math, it is hard to explain how to get the update rule using base 2 numbers, but there is not much difference in meaning between "code 224" and "game of life".  The point of whether the use of "life" is educationally helpful is debatable as well.  For one thing, everyone knows what life is and this idea of cells being alive under certain conditions does not correspond to the everyday notion of life.

Here is a breakdown of the arguments
{224 is the rule number
{2 is the number of colors
{{2,2,2},{2,1,2},{2,2,2}} is the weight matrix for an outer totalistic rule.  The shortcut is to take the total of the outer neighbors and multiply by 2, then add 1 if the center cell is black.
{1,1} is the radius of the rule which is two numbers showing the radius in each dimension separately.  This list being length 2 declares it is a 2D rule.

One of the things that makes this confusing is the list structure determines whether it is a general rule, a totalistic rule or a weighted rule.  There is also the possibility of being a second order rule (or nth order) depending on more than one step back.  All these things, with the different needs and meanings of arguments, have to fit into the design of the CellularAutomaton function.

Among the various lectures at the summer school http://www.wolframscience.com/summerschool/ there is a lecture explaining how to use this function.
POSTED BY: Todd Rowland
Also, if you need an animation, take a look at Demonstrations Project. Those are basically advanced interactive animations. Code is freely available for download.
POSTED BY: Vitaliy Kaurov
Posted 11 years ago
Thank you Todd and Vitaliy for your response....  Sorry for my delay in the discussion, but i was without net.... 

 Todd: you are right about the "sum" definition, but it gave me no problem: the "sum" definition I used here was to simplify the explanation. I will check the references you suggested me. Thank you. Nevertheless, I think my approach can work, but I still need to solve the issue of the definition of the s function, or, instead, clear the values of s, after it is calculated in each step. Can you help me with that?? Thanks

 Vitaliy: I don´t want to use the demonstations posted on the wolfram website. I want to do it by myself ;-)   
POSTED BY: Daniel Branco
Posted 11 years ago
Todd:
in genreral, does the built-in CellularAutomaton function work? is it defined in terms of WL or in another language, e.g. C? does it use Gosper's algortihm or what? it's concerning to use built-in functions if you don't know the underlying algorithm or see the code. the GoL rules are easy to write down and the b.c. explained. how can someone possibly know that CellularAutomaton[{224, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, {1, 1}}, RandomInteger[1, {20, 20}], 30]
is in fact, implementing the GoL correctly? i can't figure out what b.c. are being used or what the GoL rules are from looking at the arguments of Cellular Automaton. and isn't this function, limited to totalistic rules where the site values are integers? and what does one do for other CA's. e.g. how is CellularAutomaton function used for the forest fire CA?  i am unable to understand what the arguments 'mean'.  how do you discuss the GoL with anyone using CellularAutomaton? do you say 'here are the rules and the b.c. and now just trust me that CellularAutomaton[{224, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, {1, 1}}, RandomInteger[1, {20, 20}], 30] is implementing the GoL correctly, i don't see how one can teach the GoL to students if you can't show them the code. obviously at some point, you just say use this built-in function (e.g. Sort but even there you can tell them what Sort is doing even you don't explain that it might be doing it using a mergesort or bubblesort but CellularAutomaton[{224, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, {1, 1}},...]  is totally opaque. 
POSTED BY: Richard Gaylord
Posted 11 years ago
other than http://reference.wolfram.com/mathematica/ref/CellularAutomaton.htmlhttp://reference.wolfram.com/mathematica/ref/CellularAutomaton.html i can't gfind a lecture on using on using the function. can you tell me where to locate it it? thanks.

but thanks for the explanation of CellularAutomaton.

btw - as both an educator and scientist, i have found that the use of terms like Life, glider gun, etc. actually does indeed, have pedagogical value (the use of analogy is an important learning and thinking tool) and it is one of the reasons that the GoL is, without a doubt, the best known CA (much more so than rule 30 or 110) even amongst professional scientists  and it's really no worse for introducing  people to cellular automata than using the Bohr model of the atom  to introduce people to the concept of quantum behavior. it's a satrting point and a hook to get someone interested in a subject. there's nothing wrong with that.

funally, here is a youtube video (https://www.youtube.com/watch?v=MYGJ9jrbpvg) that illustrates the difficulty in dfining 'life. it is not in fact, not true that "everyone knows what life is - . especually not biologists or robotics specialists (or people who work in artifical intelligence).
POSTED BY: Richard Gaylord
The original CellularAutomaton lecture was given in 2004
http://www.wolframscience.com/summerschool/resources/NKSCA-phillips-ss04.cdf

Some of the other lectures from past summer schools can be found at http://www.wolframscience.com/summerschool/resources/
POSTED BY: Todd Rowland
Daniel, the reason I recommended the Demonstrations Project is to learn from the source code. I am not sure if you are aware of this, but demonstrations are not black-boxed, - any demonstration can be downloaded and the source code can be studied to understand how it works. There are quite a few experienced programmers that made an effort to develop effective code running GoL using Wolfram Language at Demonstrations Project, - I personally learned from that.
POSTED BY: Vitaliy Kaurov
Posted 11 years ago
Thank yo Vitaliy, I´ll check the Demostrations Project... I´ll let you know if I find something....
POSTED BY: Daniel Branco
Posted 11 years ago
thanks for the info, todd.

btw - i entered that address in safari and it won't go there (in fact, the address imply dissapears from the address bar). so instead,  i went to http://www.wolframscience.com/summerschool/2006/faculty.html#Phillips and then clicked on presentations by Richard Phillips which bought up NKS Research Tools: CellularAutomaton and cicking on that downloads the cdf. i mention this for others who may want to get the lecture.
POSTED BY: Richard Gaylord
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