# Fifth Program

AI and Robotics

Dec 1, 2013 (4 years and 7 months ago)

86 views

Program 5

Handed out: November 24

Due Date: December 4

In this program

you will implement a cellular automaton.

You can start working on the program over the break. If you run into problems, then we can take
them up when you return from vacation. Work

on “small pieces” at a time.
Enjoy!

Cellular Automaton

In this assignment

you will write a Java program that implements a simple, one
-
dimensional
cellular automaton

(CA).

You might ask “what the heck is Cellular Automaton? (See last
section for
a ful
l
explanation

of cellular automaton
!)

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
should

all cells

except one cell,
which will contain a 1. Ca
ll this
time st
ep 0.

Your program will apply
simple rule
s
, 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
s are
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:

Allow the user to select the size of the CA, from 10 to 100 cells inclusive. Ensure that their
c
hoice is not out of this range (
hint: start small with
a small array of 5 or

10 cells to begin
with
).

Make sure that the CA starts with zeros in every cell except for a single 1 placed by the user.
Provide a way for the user to indicate where they want that 1 to go. Be sure it is in a legal cell
of the CA, given the size they chose. You can force them t
o specify a 0
-
based position (like
the array indices).

Get the number of time steps to run the CA from the user, making sure it's greater than 0

(

to make sure your system is working
)
.

For each time step, change th
e values of the CA and print out the current values as described
below.

Change the values of the CA according to the following rules
. Given
a set of
current values

for an array
:

The values of the first and last cells stay the same

(so you’ll only be cha
nging 1 to
array.length
-
1
.

The value of the cell at index
i

(where
i

i
s not the index of the first or last cell) is 1 if the
sum of the current values of the cells at indices
i
-
1, i
, and
i+1

is exactly 1
; otherwise, the
new value at this cell should be 0.

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 c
onfiguration (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 v
alues 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, wher
eas 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:

If the array cell contains a 0, prin
t out a single blank.

If the array cell contains a 1, print out a single asterisk.

For example, the printout for the

first

array above would be:

** ** *

[it is interpreting the 110011001]

So each line of output

will be a pattern of spaces and aster
isks 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 5

time steps.
Once you have your
programming running, incr
ease the time steps to 5000.
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.

Cellular Automaton

Explantion

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
cellular

automata is rich, with
simple rules and structures being capable of producing a great variety of unexpected behaviors.
Check o
ut
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
implement

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
neighbors
, which are the cells that are directly
horizontally, vertically, or diagonally adjacent. At each step in time, the followin
g effects occur:

Any live cell with fewer than two
neighbors

dies of loneliness.

Any live cell with more than three
neighbors

dies of crowding.

Any dead cell with exactly three
neighbors

comes to life.

Any live cell with two or three
neighbors’

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 simultane
ously, 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:

Any live cell stays live if both its neighbors are dead (self
-
preservation).

Any dead cell with precisely one live neighbor comes to life.

Any live cell that has live neighbors dies of c
rowding.

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:

Turn in your program.java program in Moodle. Since these “runs” may be large, just show me a
run with an array size of 10, with initial value of 1 in cell 4, with a step size of 20…!