CS101 - Lab 6

In this lab you will implement a cellular automaton.

Always start by thinking of the problem top-down. Split it into logical steps, and write one step at a time. Then compile it, and check if it does what you want. Write your code incrementally! This will save you much trouble.

As usual, work individually, and call me if you need help. You are encouraged to discuss ideas and techniques broadly with other class members, but not specifics. Discussions should be limited to questions that can be asked and answered without using any written medium (e.g. pencil and paper or email). You should at no point look at the screen of your colleagues.

Enjoy!


  • Cellular Automaton.

    In this lab you will write a Java program that implements a simple, one-dimensional cellular automaton (CA).

    A one-dimensional CA can be thought of as a one-dimensional array. Each cell of the CA can have one of two values: 0 (zero) or 1 (one). It will start with a 0 in all cells except one cell, which will contain a 1. Call this time step 0. Your program will apply a simple rule, which I'll explain below, and the values in the CA will change, thus producing a new set of CA values at time step 1. The rule is applied again, resulting in a new set of CA values at time step 2. The process is repeated some number of times. Depending on the initial set of values in the CA and the rule applied, some unexpected patterns can arise, which are easily seen if the sets of values are printed out in successive rows in a certain way, also described below.

    Your program should do the following:

    Change the values of the CA according to the following rule. Given the current values:

    Note that since all the new values in the next configuration must be computed using the current values of the current configuration, you can't apply the rule to each cell in the CA, changing the values as you go along. For example, suppose the current configuration (in a smaller CA) is:
                  0     1     2     3     4     5     6     7     8
               |-----|-----|-----|-----|-----|-----|-----|-----|-----|
         CA    |  1  |  1  |  0  |  0  |  1  |  1  |  0  |  0  |  1  |
               |-----|-----|-----|-----|-----|-----|-----|-----|-----|
    

    You need to compute the new values and put them somewhere else until you have computed all the new values, then transfer them back into the CA.

    To illustrate the application of the rule described above, the values in the next configuration of the CA pictured here would be:

                  0     1     2     3     4     5     6     7     8
               |-----|-----|-----|-----|-----|-----|-----|-----|-----|
    new values |  1  |  0  |  1  |  1  |  0  |  0  |  1  |  1  |  1  |
               |-----|-----|-----|-----|-----|-----|-----|-----|-----|
    

    The value at index 0 remains the same. The value at index 1 becomes 0 because the sum of its value and the values of its immediate neighbors in CA (indices 0 and 2) is not 1, whereas the value at index 2 becomes 1 because the sum of its value and the values of its immediate neighbors in CA (indices 1 and 3) is 1, and so forth.

    Display the contents of the CA on the screen in the following way:

    For example, the printout for the array {\tt CA} above would be:

    **  **  *
    

    So each line of output will be a pattern of spaces and asterisks that reflects the configuration of values in the CA at a particular time step. To test your program, set the value of the cell at index 4 to 1 (all other cells have the value 0) and run it for 5000 time steps. You should see an interesting pattern of triangles of all different sizes. By the way, there's nothing special about index 4 -- try it with other locations.

    Miscellania

    You may wonder, what on earth is this problem good for? Cellular automata were studied in the 1950s as a possible model for biological systems. The theory of celular automata is rich, with simple rules and structures being capable of producing a great variety of unexpected behaviors. Check out wikipedia on Cellular automaton to get an idea of the problem and its history.

    The most famous example of a cellular automaton is the Game of life devised by Conway in 1970. The Game of life is slightly more general than the version you imlement in this lab: it works on a 2-dimensional grid (rather than a 1-dimensional array). [from Wikipedia:] The universe of the Game of Life is an infinite two-dimensional grid of cells, each of which is either alive or dead. Cells interact with their eight neighbours, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following effects occur:

    1. Any live cell with fewer than two neighbours dies of loneliness.
    2. Any live cell with more than three neighbours dies of crowding.
    3. Any dead cell with exactly three neighbours comes to life.
    4. Any live cell with two or three neighbours lives, unchanged, to the next generation.
    The initial pattern constitutes the first generation of the system. The second generation is created by applying the above rules simultaneously to every cell in the first generation -- births and deaths happen simultaneously, and the discrete moment at which this happens is called a tick. The rules continue to be applied repeatedly to create further generations. Check out wikipedia's Game of life to see simulations.

    Though the Game of life is more general than the version in this lab, you can easily make the connection between the simpler problem in this lab and the rules of evolution of a simple population. Saying that the new value at position i is 1 if and only if a[i-1] + a[i] + a[i+1] == 1 is the same as saying:

    The cellular automaton that you implement in this problem simulates the evolution of a population that would evolve by these rules.

    What to turn in: