0% found this document useful (0 votes)
61 views9 pages

Lecture P4: Cellular Automata: Array Review

This document provides an overview of cellular automata and their applications. It discusses how arrays can be used to store and manipulate cellular automata data. Different one-dimensional and two-dimensional cellular automata rules are presented, including their effects on pattern generation over time. Examples of cellular automata simulations include Conway's Game of Life and reaction-diffusion systems. The document also covers optimizing cellular automata code to improve performance for large grid sizes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views9 pages

Lecture P4: Cellular Automata: Array Review

This document provides an overview of cellular automata and their applications. It discusses how arrays can be used to store and manipulate cellular automata data. Different one-dimensional and two-dimensional cellular automata rules are presented, including their effects on pattern generation over time. Examples of cellular automata simulations include Conway's Game of Life and reaction-diffusion systems. The document also covers optimizing cellular automata code to improve performance for large grid sizes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Array Review

Lecture P4: Cellular Automata

Arrays allow manipulation of potentially huge amounts of data.


s

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."

Reaction diffusion textures. Andy Witkin and Michael Kass


Princeton University COS 126 General Computer Science Fall 2002 http://www.Princeton.EDU/~cs126
2

Cellular Automata
Cellular automata. (singular = cellular automaton)
s

Applications of Cellular Automata


Modern applications.
s

Computer simulations that try to emulate laws of nature. Simple rules can generate complex patterns.

John von Neumann. (Princeton IAS, 1950s)


s

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.

How Did the Zebra Get Its Stripes?


s

One Dimensional Cellular Automata


1-D cellular automata. Sequence of cells. Each cell is either black (alive) or white (dead). In each time step, update status of each cell, depending on color of nearby cells from previous time step.

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

Synthetic zebra. Greg Turk


5 6

Cellular Automata: Designing the Code


How to store the row of cells.
s

Cellular Automata: The Code


cellular.c (part 1) #include <stdio.h> #define STEPS 128 #define CELLS 256 #define PS 512.0

An array. // # of iterations to simulate // # of cells // size of canvas

How to store the history of cells.


s

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

How to output results.


s

Turtle graphics.

Cellular Automata: The Code


cellular.c (part 2) // simulate cellular automata for (t = 1; t < STEPS; t++) { // output current row in turtle graphics for (i = 0; i < CELLS; i++) { if(cells[i] == 1) { printf("F %f %f\n", PS*i/N, PS - PS*j/CELLS); printf("S %f\n", PS / CELLS); } } // copy old values for (i = 0; i < CELLS; i++) old[i] = cells[i]; // update new cells according to rule 250 for (i = 1; i < CELLS - 1; i++) { if (old[i-1] + old[i+1] > 0) cells[i] = 1; else cells[i] = 0; } }
9

Cellular Automata Rules


Rule 250 (repetition).

10

Cellular Automata: Change the Rules


What happens if we change the update rule? New rule. Make cell black at time t if exactly one proper neighbor was black at time t-1. Replace update rule in cellular.c // update new cells according to rule 90 for (i = 1; i < CELLS - 1; i++) { if (old[i-1] + old[i+1] == 1) cells[i] = 1; else cells[i] = 0; } Original rule: > 1

Cellular Automata: Change the Rules


Rule 90 (nesting).

11

12

Pascals Triangle

Pascals Triangle

13

14

Binary Numbers
Binary and decimal representation of integers.
s

Cellular Automata Rules


Rule 250 (repetition).
Dec 0 1 2 3 4 5 6 7 Bin 0000 0001 0010 0011 0100 0101 0110 0111 Dec 8 9 10 11 12 13 14 15 Bin 1000 1001 1010 1011 1100 1101 1110 1111

Binary is base 2. Decimal is base 10.

111110102 = 25010 Rule 90 (nesting). 010110102 = 9010 Rule 30 (randomness). 000111102 = 3010

Example. 25010 = 111110102.


s

7 1
27 25010 =

6 1

5 1

46 3 1? 1
+23 +8

2 0
0 0

1 1
+21 +2

0 0

Rule 110 (localized structure). 011011102 = 11010

+26 +25 +24

0 0
15 16

128 +64 +32 +16

Cellular Automata Rules


Rule 250 (repetition). Rule 90 (nesting).

Cellular Automata Rules

17

18

Cellular Automata Rules


Rule 30 (randomness).

Cellular Automata Rules


Rule 110 (localized structure).

Source: A New Kind of Science by Steve Wolfram.


19

Source: A New Kind of Science by Steve Wolfram.


20

Pigmentation Patterns on Mollusk Shells

Cellular Automata: Designing the Code


Rule 30.
1 0 0 0 1 1 0 1 0 0 0 1

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

30 && && && && old[i+1] old[i+1] old[i+1] old[i+1] == == == == 0) || 1) || 0) || 1))

else cells[i] = 0; }
22

Cellular Automata: Designing the Code


Rule 30.
old[i-1] old[i] old[i+1]

Cellular Automata: Designing the Code


Rule 30.

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

rule = 3010 = 000111102 val = 210 = 0102

000111102 >> 210 = 0001112 0001112 & 0000012 0000012

#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

Two Dimensional Cellular Automata


2-D cellular automata.
s

Two Dimensional Cellular Automata


2-d cellular automata #include <stdio.h> #define N 512 #define STEPS 10 #define GRIDSIZE 512.0 #define MAXDIST 3 int randomInteger(int n) { . . . } int cells[N][N]; // two dimensional array int main(void) { int i, j, k, ii, jj, dist, color; double sum, threshhold = 1.4; double weight[MAXDIST + 1] = {2.0, 2.0, -1.2, -1.2}; // initialize with random pattern for (i = 0; i < N; i++) for (j = 0; j < N; j++) cells[i][j] = randomInteger(2);
25 26

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

Two Dimensional Cellular Automata


2-d cellular automata (brute force)
for (k = 0; k < STEPS; k++) { for (i = 0; i < N; i++) { for (j = 0; j < N; j++) // update cell (i, j) sum = 0.0; // consider only cells within distance 3 for (ii = 0; ii < N; ii++) { for (jj = 0; jj < N; jj++) { absolute value dist = abs(ii-i) + abs(jj-j); color = cells[ii][jj]; if (dist <= MAXDIST) sum += weight[dist] * color; } } if (sum > threshhold) cells[i][j] = 1; else cells[i][j] = 0; } } }
27
s

Why Are You So Slow?


Why is the program so slow? 221 seconds for N = 128. 10N4 > 2.7 billion 15.7 hours for N = 512. 10N4 > 687 billion. A quintuply nested loop for (a = 0; a < 10; a++) { for (b = 0; b < N; b++) { for (c = 0; c < N; c++) { for (d = 0; d < N; d++) { for (e = 0; e < N; e++) { // code here in innermost loop is // executed 10N4 times. } } } } }
30

Two Dimensional Cellular Automata


2-d cellular automata
for (k = 0; k < STEPS; k++) { for (i = 0; i < N; i++) { for (j = 0; j < N; j++) // update cell (i, j) sum = 0.0; // consider only cells within distance 3 for (ii = i - MAXDIST; ii <= i + MAXDIST; ii++) { for (jj = j - MAXDIST; jj <= j + MAXDIST; jj++) { dist = abs(ii-i) + abs(jj-j); color = cells[ii % N][jj % N]; if (dist <= MAXDIST) sum += weight[dist] * color; } } if (sum > 0) cells[i][j] = 1; else cells[i][j] = -1; } } }
31
s

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.

Improved program. (code in innermost loop executed 490 N2 times)


s

1.1 seconds for N = 128. 200x speedup 17.5 seconds for N = 512. 3000x speedup

Stay tuned: analysis of algorithms lecture.

32

Two Dimensional Cellular Automata


Tweaking the parameters.

Source: A New Kind of Science by Steve Wolfram.


33

Source: A New Kind of Science by Steve Wolfram.

34

Conways Game of Life


Conways Game of Life.
s

Conways Game of Life


Stable.

Based on theories of von Neumann. 2-D cellular automaton to simulate synthetic universe. Can also simulate general purpose computer.

Critters live and die in depending on 8 neighboring cells:


s

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

exactly 3 parents? critter born in empty square

Glider.

John Conway
35 36

Conways Game of Life


Glider gun.
s

Conways Game of Life


Glider gun synthesizer.
s

Produces a new glider every 30 iterations.

8 gliders collide to form a glider gun.

37

38

You might also like