Lecture P4: Cellular Automata: Array Review
Lecture P4: Cellular Automata: Array Review
All elements of the same type. double, int N-element array has elements indexed 0 through N-1. Fast access to arbitrary element. a[i] Waste of space if array is "sparse."
Cellular Automata
Cellular automata. (singular = cellular automaton)
s
Computer simulations that try to emulate laws of nature. Simple rules can generate complex patterns.
Wanted to create and simulate artificial life on a machine. Self-replication. "As simple as possible, but no simpler."
s s
Simulations of biology, chemistry, physics. ferromagnetism according to Ising mode forest fire propagation nonlinear chemical reaction-diffusion systems turbulent flow biological pigmentation patterns breaking of materials growth of crystals growth of plants and animals Image processing. Computer graphics. Design of massively parallel hardware. Art.
Example rule. Make cell black at time t if at least one of its proper neighbors was black at time t-1. time 0 time 1 time 2
A multidimensional array. wastes a lot of space An array that stores only previous time step. wastes a little time updating history array
int main(void) { int i, t; int cells[CELLS] = {0}; // cell contents at time t int old[CELLS]; // cell contents at time t-1 cells[CELLS/2] = 1; // init one cell to black 1 // simulate cellular automata // INSERT CODE on next slide here return 0; }
7 8
Turtle graphics.
10
11
12
Pascals Triangle
Pascals Triangle
13
14
Binary Numbers
Binary and decimal representation of integers.
s
111110102 = 25010 Rule 90 (nesting). 010110102 = 9010 Rule 30 (randomness). 000111102 = 3010
7 1
27 25010 =
6 1
5 1
46 3 1? 1
+23 +8
2 0
0 0
1 1
+21 +2
0 0
0 0
15 16
17
18
old[i-1]
old[i]
old[i+1]
0 1
0
cells[i]
possible code for rule 30 // update new cells according to rule for (i = 1; i < CELLS - 1; i++) { if ((old[i-1] == 1 && old[i] == 0 (old[i-1] == 0 && old[i] == 1 (old[i-1] == 0 && old[i] == 1 (old[i-1] == 0 && old[i] == 0 cells[i] = 1;
Source: A New Kind of Science by Steve Wolfram.
21
else cells[i] = 0; }
22
val = 1002 = 410 Ex. possible code for rule 30 // rule 30 int rule[8] = {0, 1, 1, 1, 1, 0, 0, 0}; code for arbitrary rule . . . // update new cells according to rule 30 for (i = 1; i < CELLS - 1; i++) { val = 4*old[i-1] + 2* old[i] + old[i+1]; cells[i] = rule[val]; }
23
#define RULE 30 . . . // update new cells according to arbitrary rule for (i = 1; i < CELLS - 1; i++) { val = 4*old[i-1] + 2* old[i] + old[i+1]; cells[i] = (RULE >> val) & 1; }
24
N x N grid of cells. Each cell is either black (1) or white (0). Update status of each cell, depending on neighbors. Repeat.
Example.
s
Update cell (i, j) by considering all cells within Manhattan distance 3 of (i, j). Color cell (i, j) black if following SUM is greater than 1.4: add together twice the color values of each cell that is distance 0 or 1 away subtract 120% of color values of cells that are distance 2 or 3 away
Algorithmic Speedup
Original program. (code in innermost loop executed 10 N4 times) 221 seconds for N = 128. 10N4 > 2.7 billion 15.7 hours for N = 512. 10N4 > 687 billion.
1.1 seconds for N = 128. 200x speedup 17.5 seconds for N = 512. 3000x speedup
32
34
Based on theories of von Neumann. 2-D cellular automaton to simulate synthetic universe. Can also simulate general purpose computer.
too few? (0 or 1) die of loneliness too many? (4-8) die of overcrowding just right? (2 or 3) survive to next generation
Oscillator. 1 2 3 8 4 7 6 5
Glider.
John Conway
35 36
37
38