Saturday, July 17, 2010

Cafun: Extending the Game of Life

As promised in the last entry this time around we are going to dissect a Cafun simulation file that I created. This file extends Conway's Game of Life by adding some color to the cells. It also borrows from Edward Rietman's Exploring the Geometry of Nature. Rietman included BASIC programs in his book that looked at how a Life game develops when it begins with a certain random population densities. So before we begin the discussion, here is the file in its entirety, with line numbers:

  1. <simulation name="Life_Extended" author="John Conway adpated by Jack">
  2. <description>
  3. <section>This is a simple implementation of the famous Conway's
  4. Game of Life, extended to add color to longer lived cells. Including
  5. probability starters from Rietman's Exploiring the Geometry of Nature
  6. Paint some "living" cells on a "dead" background and
  7. enjoy!</section>
  8. <section caption="Description">This cellular automaton is guided by the
  9. following rules: A dead cell becomes alive when there are exactly three living
  10. cells in its neighborhood. A living cell dies if there are less than two or more
  11. than three cells in its neighborhood which are alive. This version of Conway's
  12. Game of Life extends the original one by marking what rules were applied at
  13. certain locations in the universe. When a living cell dies because there are
  14. less than two living cells in its neighborhood it is marked blue, if it dies
  15. because there are more than three cells in its neighorhood which are alive it is
  16. marked green. You can also determine the lifetime of living cells since their
  17. color changes to red after some time. The cells will change color from White
  18. to Yellow to Green to Blue to Gray as they "age".</section>
  19. </description>
  20. <abstract-cell-type id="living" />
  21. <cell-type id="Start10" color="90 90 90">
  22.     <mutation cell-type="Alive" probability="0.10">
  23.         <condition cell-type="Start10"  />
  24.     </mutation>
  25.     <mutation cell-type="Dead" probability="0.90">
  26.         <condition cell-type="Start10"  />
  27.     </mutation>  
  28. </cell-type>
  29. <cell-type id="Start15" color="85 85 85">
  30.     <mutation cell-type="Alive" probability="0.15">
  31.         <condition cell-type="Start15"  />
  32.     </mutation>
  33.     <mutation cell-type="Dead" probability="0.85">
  34.         <condition cell-type="Start15"  />
  35.     </mutation>  
  36. </cell-type>
  37. <cell-type id="Start20" color="80 80 80">
  38.     <mutation cell-type="Alive" probability="0.20">
  39.         <condition cell-type="Start20"  />
  40.     </mutation>
  41.     <mutation cell-type="Dead" probability="0.80">
  42.         <condition cell-type="Start20"  />
  43.     </mutation>  
  44. </cell-type>
  45. <cell-type id="Start25" color="75 75 75">
  46.     <mutation cell-type="Alive" probability="0.25">
  47.         <condition cell-type="Start25"  />
  48.     </mutation>
  49.     <mutation cell-type="Dead" probability="0.75">
  50.         <condition cell-type="Start25"  />
  51.     </mutation>  
  52. </cell-type>
  53. <cell-type id="Start50" color="50 50 50">
  54.     <mutation cell-type="Alive" probability="0.50">
  55.         <condition cell-type="Start50"  />
  56.     </mutation>
  57.     <mutation cell-type="Dead" probability="0.50">
  58.         <condition cell-type="Start50"  />
  59.     </mutation>  
  60. </cell-type>
  61. <cell-type id="Dead" color="0 0 0" active="false">
  62.     <mutation cell-type="Alive">
  63.         <condition cell-type="living" min="3" max="3"/>
  64.     </mutation>
  65. </cell-type>
  66. <cell-type id="Alive" color="255 255 255">
  67.     <implementation cell-type="living" />
  68.     <mutation cell-type="Dead">
  69.         <condition cell-type="living" max="1"/>
  70.     </mutation>
  71.     <mutation cell-type="Dead">
  72.         <condition cell-type="living" min="4"/>
  73.     </mutation>
  74.     <mutation cell-type="Young">
  75.         <condition cell-type="living" min="2" max="3" />
  76.     </mutation>
  77. </cell-type>
  78. <cell-type id="Young" color="255 255 0">
  79.     <implementation cell-type="living" />
  80.     <mutation cell-type="Dead">
  81.         <condition cell-type="living" max="1"/>
  82.     </mutation>
  83.     <mutation cell-type="Dead">
  84.         <condition cell-type="living" min="4"/>
  85.     </mutation>
  86.     <mutation cell-type="Adult">
  87.         <condition cell-type="living" min="2" max="3" />
  88.     </mutation>
  89. </cell-type>
  90. <cell-type id="Adult" color="0 255 0">
  91.     <implementation cell-type="living" />
  92.     <mutation cell-type="Dead">
  93.         <condition cell-type="living" max="1"/>
  94.     </mutation>
  95.     <mutation cell-type="Dead">
  96.         <condition cell-type="living" min="4"/>
  97.     </mutation>
  98.     <mutation cell-type="Mature">
  99.         <condition cell-type="living" min="2" max="3" />
  100.     </mutation>
  101. </cell-type>
  102. <cell-type id="Mature" color="0 64 255">
  103.     <implementation cell-type="living" />
  104.     <mutation cell-type="Dead">
  105.         <condition cell-type="living" max="1"/>
  106.     </mutation>
  107.     <mutation cell-type="Dead">
  108.         <condition cell-type="living" min="4"/>
  109.     </mutation>
  110.     <mutation cell-type="Old">
  111.         <condition cell-type="living" min="2" max="3" />
  112.     </mutation>
  113. </cell-type>
  114. <cell-type id="Old" color="192 192 192">
  115.     <implementation cell-type="living" />
  116.     <mutation cell-type="Dead">
  117.         <condition cell-type="living" max="1"/>
  118.     </mutation>
  119.     <mutation cell-type="Dead">
  120.         <condition cell-type="living" min="4"/>
  121.     </mutation>
  122. </cell-type>
  123. </simulation>
The file is an xml file and the <simulation> tag set is the root of xml structure. Lines 2-19 provide the description that shows in the left pane of the main Cafun window. It can be left out but it helps make sure you know what this simulation is about.

Line 20 is an example of one of the things that makes Cafun a powerful cellular automaton engine. The abstract-cell-type allows many actual cell-types (we'll talk about those below) to behave as a single type. In this case we will have several variants of "living" cells and we want them all to contribute to whether a cell lives or dies in the next generation.

The remainder of the file defines the various cell-types and the rules that control them. The repetitive nature of some of the definitions is required so we can have the each generation progress to the next type. One thing to be aware of, Cafun determines a cell-type by it's color. So don't make two types the same color. The program will define them as the first cell-type with that color definition. I'll describe one example of the three distinct cell-types we have in the file, and then you're on your own.

Line 61-65: Cell-type Dead
The Dead cell type is the background type. In most runs of the simulation the bulk of the cells will be dead. Dead cells will change to Alive cells if they are surrounded by exactly 3 living cells. We'll look at the details of the cell-type elements in the next section on the Alive Cell type.

Lines 66-77 Cell-type Alive
The cell-type definition for the Alive cells is repeated below with explanatory notes.

<cell-type id="Alive" color="255 255 255">
This tag defines the cell-type. The id attribute is how the type will be referred to in the simulation definition. The color is how it will be shown on screen. Reminder, this is how Cafun identifies the type. If you created another cell-type with the same color (R:255 G:255 B:255 or white) it would be identified as Alive.

    <implementation cell-type="living" />
This tag shows that this cell-type implements the abstract cell-type "living" and any mutations defined to it.

    <mutation cell-type="Dead">
        <condition cell-type="living" max="1"/>
    </mutation>
The three tags above are a mutation definition. These are the rules that the cell follows. This first mutation and the one below could be moved the "living" abstract cell-type but having them in each cell-type definition allows for modifying the rules for some types of living cells compared to others.  The cell-type in the mutation element is what the cell under consideration would become if the conditions are met. In this case the cell will become a Dead cell id it is a living cell with a maximum of 1 neighbor. Since there is no minimum number it is implied that have zero neighbors would also satisfy this rule. So the cell-type in the condition indicates what type of cells in the eight cell neighborhood contribute the the count to satisfy this rule.

    <mutation cell-type="Dead">
        <condition cell-type="living" min="4"/>
    </mutation>
This mutation is the same as the first but for all count values 4 and higher.

    <mutation cell-type="Young">
        <condition cell-type="living" min="2" max="3" />
    </mutation>
This is the "continues to live rule" from the game of life. But in this case we are going to transform the cell into the next "generation" of living cell. This allows us to use color to see where the activity is in the simulation. In this case the cell will become a "Young" cell (color: yellow) if it survives. The progression is Alive(white), Young(yellow), Adult(green), Mature(blue), Old(gray). Stable GoL configurations will eventually turn gray. Oscillators will most often have a gray part and a white part. Gliders are mostly white but some of the cells survive a couple of generations to turn yellow and green.
</cell-type> - This is the closing cell-type tag.

You can define new cell-types with different mutations using this basic pattern. If you find an article that describes a cellular automaton that you wish to try, just code the rules that they give you as mutations.

Line 29-36: A Rietman style probabilistic starter
This particular starter is the 15% starter that allows you to randomly fill the CA world with 15% live cells. There are starters for 10%, 15%, 20%, 25% and 50%. Simply fill with one of these cell types to get your random start. You could also use a large brush and put down areas of each and  see what happens. The piece that provides the randomness is in the mutation element:
<mutation cell-type="Alive" probability="0.15">
The probability attribute indicates what the chance is that this mutation will trigger. In this case it is set to 0.15 or 15%. So there is a 15% chance that the cell will change to an Alive cell in the next generation. There is a matching mutation that has an 85% chance to trigger and it would turn the cell to a Dead cell.

So there you have it. Extensions to Conway's Game of Life that make it a little more colorful and easier to randomly seed your starting world. One addition I've considered that would create a distinct change from the Game of Life and add a new dynamic to the simulation is to give the Old cells a chance, say 50%, to convert to a Start50 cell. This would mean that all Old cells, those that had lived at least 5 generations would have a 50 percent chance to be changed into a cell that had a 50 percent chance to die. This would potentially cause certain stable constructs to whither away as one or more cells die off. You are encouraged to try to make that change yourself.

That's it for now. Next time we return to Object Oriented Programming.

No comments:

Post a Comment