the game of life a simulation of "life". from simple rules, complex behavior arises rules...

21
The Game of Life • A simulation of "life". From simple rules, complex behavior arises Rules – A cell that is alive and has fewer than two live neighbors dies (because of loneliness) – A cell that is alive and has more than 3 live neighbors dies (because of over- crowding) – A cell that is dead and has exactly 3 live neighbors comes to life – All other cells maintain their state

Upload: denis-daniels

Post on 03-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

The Game of Life

• A simulation of "life". From simple rules, complex behavior arisesRules– A cell that is alive and has fewer than two live neighbors

dies (because of loneliness) – A cell that is alive and has more than 3 live neighbors

dies (because of over-crowding) – A cell that is dead and has exactly 3 live neighbors

comes to life – All other cells maintain their state

Page 2: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

The Game of Life: History

• Created by John Horton Conway,a British Mathematician

• Inspired by a problem presentedby John Von Neumann:– Build a hypothetical machine that can

build copies of itself

• First presented in 1970, Scientific American

Page 3: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

“Life”

Evolutionary rules

Grid World

• Everything depends on a cell’s eight neighbors

red cells are alive

white cells are empty

• Exactly 3 neighbors give birthto a new, live cell!

• Exactly 2 or 3 neighbors keep anexisting cell alive

• Any other number of neighbors killthe central cell (or keep it dead)

John Conway

Page 4: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Life

Evolutionary rules

Grid World

• Everything depends on a cell’s eight neighbors

red cells are alive

white cells are empty

• Exactly 3 neighbors give birthto a new, live cell!

• Exactly 2 or 3 neighbors keep anexisting cell alive

• Any other number of neighbors killthe central cell (or keep it dead)

Page 5: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Life

Evolutionary rules

Grid World

• Everything depends on a cell’s eight neighbors

red cells are alive

white cells are empty

• Exactly 3 neighbors give birthto a new, live cell!

• Exactly 2 or 3 neighbors keep anexisting cell alive

• Any other number of neighbors killthe central cell (or keep it dead)

Page 6: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Life

Evolutionary rules

Grid World

• Everything depends on a cell’s eight neighbors

red cells are alive

white cells are empty

• Exactly 3 neighbors give birthto a new, live cell!

• Exactly 2 or 3 neighbors keep anexisting cell alive

• Any other number of neighbors killthe central cell (or keep it dead)

life out there...

Keep going!

Page 7: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Creating Life

0 1 2 3 4 50 1 2 3 4 5

0

1

2

3

4

5

0

1

2

3

4

5

updateNextLife(oldB, newB)

old generation or "board" new generation or "board"

Page 8: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Creating Life

0 1 2 3 4 50 1 2 3 4 5

0

1

2

3

4

5

0

1

2

3

4

5

old generation or "board" new generation or "board"

updateNextLife(oldB, newB)

Page 9: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Details

For each generation… • 0 represents an empty cell

• 1 represents a living cell

• outermost edge should always be left empty (even if there are 3 neighbors)

• compute all cells based on their previous neighbors

http://www.math.com/students/wonders/life/life.html

old generation or "board" new generation or "board"

life out there...

updateNextLife(oldB, newB)

Page 10: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Main Loop

def life( width, height ): """ will become John Conway's Game of Life... """ B = createBoard(width, height) csplot.showAndClickInIdle(B) while True: # run forever csplot.show(B) # show current B time.sleep(0.25) # pause a bit oldB = B B = createBoard( width, height ) updateNextLife( oldB, B ) # gets a new board

Page 11: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Main Loop

def life( width, height ): """ will become John Conway's Game of Life... """ B = createBoard(width, height) csplot.showAndClickInIdle(B) while True: # run forever csplot.show(B) # show current B time.sleep(0.25) # pause a bit oldB = B B = createBoard( width, height ) updateNextLife( oldB, B ) # gets a new board

Why not just have update RETURN a new list? (i.e., why bother with mutation at all?)

Update MUTATES the list B

Page 12: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

to and beyond!

• Are there stable life configurations?

• Are there oscillating life configurations?

• Are there self-propagating life configurations?

"rocks"

"plants"

"animals"

period 3

period 2

Page 13: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

to and beyond!

• Are there life configurations that expand forever?

• What is the largest amount of the life universe that can be filled with cells?

• How sophisticated can the structures in the life universe be?

http://www.ibiblio.org/lifepatterns/

• Are all feasible configurations reachable?

Page 14: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Gaussian Elimination

2p + 3n + -1q = -8.00

-3p + -1n + 2q = 42.00

1p + -9n + 4q = 56.00

Goal:• get 1s along the diagonal• get 0s elsewhere on the left

Find p,n,q

1p + 0n + 0q = 1.00

0p + 1n + 0q = 5.00

0p + 0n + 1q = 25.00

Just the array is necessary !We can get rid of the variables...

Using only row operations (our methods) !

where to start?

Page 15: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Solve

1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00

multiply Row 0 by 0.5

1.00 1.50 -0.50 -4.00 0.00 3.50 0.50 30.00 0.00 -10.50 4.50 44.00

add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2

1.00 0.00 -0.71 -16.85 0.00 1.00 0.14 30.00 0.00 0.00 6.00 150.00

1.00 0.00 0.00 1.00 0.00 1.00 0.00 5.00 0.00 0.00 1.00 25.00

same for other columnsas far as possible...

multiply Row 1 by 1/3.5 add a multiple of Row 1 to Row 0 add a multiple of Row 1 to Row 2

Page 16: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

def add2ofRow1IntoRow2( A ):

0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 10.00 20.00 30.00 40.00

before

after

A

A

0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 16.00 21.00 31.00 46.00

two of row 1 are to be

added to row 2

row 0

row 1

row 2

row 0

row 1

row 2

Write a method that adds two times the

values in row #1 into the values in row #2. Only row 2's

values change.You may assume that A has at least three rows!

How could you make the source and destination rows inputs to this function?

How would it change the code?

The values in row 1should not change

Page 17: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

def addRowSIntoRowD( rs, rd, A ):

0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 10.00 20.00 30.00 40.00

0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 16.00 21.00 31.00 46.00

row 0

row 1

row 2

row 0

row 1

row 2

Write a method that adds row #s into the

values in row #d. sd

sd

Page 18: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Using 2d arrays

2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00Before

After

2.00 3.00 -3.00 -8.00 -3.00 -1.00 6.00 42.00 1.00 -9.00 12.00 56.00

arr

arr

A method for multiplying columns:

Page 19: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Solving ?!

Gaussian Elimination

2p + 3n + -1q = -8.00

-3p + -1n + 2q = 42.00

1p + -9n + 4q = 56.00

1.00p + 1.50n + -0.50q = -4.00 -3.00p + -1.00n + 2.00q = 42.00 1.00p + -9.00n + 4.00q = 56.00

Goal:• get 1s along the diagonal• get 0s elsewhere on the left

multiply the zeroth row (R0) by 0.5

Find p,n,q

1.00p + 1.50n + -0.50q = -4.00 0.00p + 3.50n + 0.50q = 30.00 -0.00p + -10.50n + 4.50q = 44.00

add 3 times R0 to R1 add -1 times R0 to R2

Page 20: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Solved !

1.00p + 1.50n + -0.50q = -4.00 0.00p + 3.50n + 0.50q = 30.00 -0.00p + -10.50n + 4.50q = 44.00

multiply R1 by 1 / 3.5

1.00p + 1.50n + -0.50q = -4.00 0.00p + 1.00n + 0.14q = 8.57 -0.00p + -10.50n + 4.50q = 44.00

add -1.5 times R1 to R0 add 10.5 times R1 to R2

1.00p + 0.00n + -0.71q = -16.85 0.00p + 1.00n + 0.14q = 30.00 -0.00p + 0.00n + 6.00q = 150.00

add 3 times R0 to R1 add -1 times R0 to R2

continue...

1.00p + 0.00n + 0.00q = 1.00 0.00p + 1.00n + 0.00q = 5.00 -0.00p + 0.00n + 1.00q = 25.00

only the array is necessary !

Page 21: The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies

Solved !

1.00p + 1.50n + -0.50q = -4.00 0.00p + 3.50n + 0.50q = 30.00 -0.00p + -10.50n + 4.50q = 44.00

multiply R1 by 1 / 3.5

1.00p + 1.50n + -0.50q = -4.00 0.00p + 1.00n + 0.14q = 8.57 -0.00p + -10.50n + 4.50q = 44.00

add -1.5 times R1 to R0 add 10.5 times R1 to R2

1.00p + 0.00n + -0.71q = -16.85 0.00p + 1.00n + 0.14q = 30.00 -0.00p + 0.00n + 6.00q = 150.00

add 3 times R0 to R1 add -1 times R0 to R2

continue...

1.00p + 0.00n + 0.00q = 1.00 0.00p + 1.00n + 0.00q = 5.00 -0.00p + 0.00n + 1.00q = 25.00

only the array is necessary !