4/1/15! 4/1/12! 4/1/07! IST 338 Rules ! z = z 2 + c John Conway HW - - PowerPoint PPT Presentation

4 1 15 4 1 12 4 1 07 ist 338 rules
SMART_READER_LITE
LIVE PREVIEW

4/1/15! 4/1/12! 4/1/07! IST 338 Rules ! z = z 2 + c John Conway HW - - PowerPoint PPT Presentation

4/1/15! 4/1/12! 4/1/07! IST 338 Rules ! z = z 2 + c John Conway HW 9 (lab + 1 problem) Simple rules can yield complex results! due Fri. 4/10 Rule #1: Don't follow this rule. Final projects: a look ahead Three-in-a-row? Aliens rule at


slide-1
SLIDE 1

4/1/15!

slide-2
SLIDE 2

4/1/12!

slide-3
SLIDE 3

4/1/07!

slide-4
SLIDE 4

IST 338 Rules !

Three-in-a-row? Aliens rule at this…

John Conway

HW 9 (lab + 1 problem)

due Fri. 4/10

z = z 2 + c Rule #1: Don't follow this rule.

Simple rules can yield complex results!

Final projects: a look ahead…

slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7

In CS, rules rule!

(1b) Numbers and strings are handled "by value"

L

L[0] L[1] L[2] "Reference" "Pointer" id

L = [5,42,'hi']

5 42

'hi'

s

'hi'

s = 'hi' (1a) Lists are handled "by reference"

42,000,042

slide-8
SLIDE 8

Reference vs. Value

Python's two methods for handling data

Lists are handled by reference: L really holds a memory address Numeric data and strings are handled by value: imagine they hold the data L

L[0] L[1] L[2] "Reference" "Pointer" id

L = [5,42,'hi']

5 42

'hi' 42,000,042

s

'hi'

s = 'hi'

x

7

x = 7

slide-9
SLIDE 9

In CS, rules rule!

(1b) Numbers and strings are handled "by value" (2) Inputs pass to functions "by copy"

The contents of the variable's "box" in memory are copied.

7

fav

7

x

def f(x):

x is a copy of fav

L

L[0] L[1] L[2] "Reference" "Pointer" id

L = [5,42,'hi']

5 42

'hi'

s

'hi'

s = 'hi' (1a) Lists are handled "by reference"

42,000,042

in main: fav = 7 f(fav)

slide-10
SLIDE 10

Python functions: pass by copy

def main() print " Welcome! " fav = 7 fav = conform(fav) print " My favorite # is", fav

7

fav fav def conform(fav) fav = 42 return fav

slide-11
SLIDE 11

Python functions: pass by copy

def main() print " Welcome! " fav = 7 fav = conform(fav) print " My favorite # is", fav

7

fav def conform(fav) fav = 42 return fav

7 copy of fav "pass by copy" means the contents of fav are copied to fav

fav

But what if the underlined part were absent… ?

slide-12
SLIDE 12

Rules rule!?

def conform1(fav) fav = 42 return fav def conform2(L) L = [42,42] return L

Try it!

Trace each f'n. What will main1, main2, and main3 print?

def conform3(L) L[0] = 42 L[1] = 42

fav fav L L 7

Notice that there are NO assignment statements after these function calls! The return values aren't being used…

L[0] 7

def main2() L = [7,11] conform2(L) print L def main1() fav = 7 conform1(fav) print fav def main3() L = [7,11] conform3(L) print L

L

L[1] 11

L

L[0] 7 L[1] 11

slide-13
SLIDE 13

Lists are Mutable

You can change the contents of lists in functions that take those lists as input. Those changes will be visible everywhere.

  • Lists are MUTABLE objects

Numbers, strings, etc. are IMMUTABLE – they can't be changed, only reassigned.

slide-14
SLIDE 14

Differing approaches to rules …

Mathematicians CS? Engineers Physicists

slide-15
SLIDE 15

Engineers believe equations approximate reality;

"the rules" Grand Canyon Skywalk

Safety margins!

slide-16
SLIDE 16

Engineers believe equations approximate reality; Physicists believe reality approximates equations…

Not a parabola, so not a real shot!

http://www.youtube.com/watch?feature=player_embedded&v=WbaH52JI3So http://www.fourandsix.com/blog/2011/8/29/seriously-amazing-beer-pong-shots.html

Image forensics' verdict: Fake!

slide-17
SLIDE 17

Mathematicians don't care either way! Engineers believe equations approximate reality; Physicists believe reality approximates equations…

A solid sphere can be split into 5 parts and rigidly reassembled into two spheres the same size as the original

Banach-Tarski paradox

Banach-Tarski XKCD the parts are "mist"

slide-18
SLIDE 18

Mathematicians don't care either way! Engineers believe equations approximate reality; Physicists believe reality approximates equations…

why settle for gears, when you could have fractal gears?

Don't like reality? Build a new one! In CS?

slide-19
SLIDE 19

Axioms Definitions

math worldview

Mathematics reasons about structural rules… Engineers believe equations approximate reality; Physicists believe reality approximates equations… … and CS reasons about procedural ones.

Insights, tools, mathematical truths

if/else while for arithmetic operations variables lists

CS worldview Insights, tools, algorithms

proofs programs Data Data

slide-20
SLIDE 20

2D data!

slide-21
SLIDE 21

Lists ~ 2D data

A = [ 42, 75, 70 ]

42 75 70

int int int list A

1D lists are familiar – but lists can hold ANY kind of data – including lists!

slide-22
SLIDE 22

list A

Lists ~ 2D data

list list list A[0] A[1] A[2]

A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]

A[0][0] A[0][1] A[0][3] A[1][0] A[1][1] A[2][0] A[2][1] A[2][2] A[2][3] A[2][4]

len(A[0]) len(A) Replace 10 with 42.

1 2 4 ? 3 5 6 7 8

10

9

11

Where's 3?

slide-23
SLIDE 23

list A list list list A[0] A[1] A[2]

Using len, how many rows does A have, in general ? Using len, how many columns does A have, in general ? What does each component of A[1][2] mean ? What is A[1][2]?

A[2][3] A[0][0]

A = [ [1,2,3,4], [5,6,7,8], [9,0,1,2] ]

1 2 3 4 5 6 7 8 9 1 2

To try…

Rectangular 2D data

slide-24
SLIDE 24

Try it…

def mystery(A): """ what happens to A ? """ NROWS = len(A) NCOLS = len(A[0]) for r in range( 0,NROWS ): for c in range( 0,NCOLS ): if A[r][c] == 4: A[r][c] = 1 else: A[r][c] += 1 After – with the code as written…

A

Write in the resulting values in A:

A = [ [4, 2, 2, 2], [2, 2, 4, 4], [2, 4, 4, 2] ]

Starting with the 2d array A shown above, write the values in A after running the code?

4 2 2 2 2 2 4 4 2 4 4 2

Before

A

row 0 row 1 row 2 col 0 col 1 col 2 col 3

slide-25
SLIDE 25

hw9pr2

4 2 2 2 2 2 4 4 2 4 4 2 def inarow_2east(A): """ what happens to A ? """ NROWS = len(A) NCOLS = len(A[0]) for r in range( 0,NROWS ): for c in range( 0,NCOLS ):

Before

A

row 0 row 1 row 2 col 0 col 1 col 2 col 3

A = [ [4, 2, 2, 2], [2, 2, 4, 4], [2, 4, 4, 2] ]

How would you change the code above to produce a True where the original value is equal to its neighbor to the right?!

(False, if no neighbor or a different neighbor.)

F T T F T F T F F T F F

Challenge:

slide-26
SLIDE 26

First, try it by eye…

A = [ [' ','X','O',' ','O'], ['X','X','X','O','O'], [' ','X','O','X','O'], ['X','O','O',' ','X'] ]

inarow_3east('X', 1, 0, A)

checker start row start col LoL

col 0 col 1 col 2 col 3 col 4 row 0 row 1 row 2 row 3

… then, on hw9pr2, by Python!

slide-27
SLIDE 27

First, try it by eye…

A = [ [' ','X','O',' ','O'], ['X','X','X','O','O'], [' ','X','O','X','O'], ['X','O','O',' ','X'] ]

inarow_3east('X', 1, 0, A) inarow_3south('O', 0, 4, A) inarow_3southeast('X', 2, 3, A) inarow_3northeast('X', 3, 1, A)

checker start row start col LoL

True

col 0 col 1 col 2 col 3 col 4 row 0 row 1 row 2 row 3

… then, on hw9pr2, by Python!

slide-28
SLIDE 28

John Conway

hw9pr1 (lab): Conway's Game of Life

1970

60˚ 70˚ 20˚ 10˚ ? (no trig)

Geometer @ Princeton

Solution: www.cs.yale.edu/homes/toyama/tri/sol.html

simple rules ~ surprising behavior 1995

slide-29
SLIDE 29

Lab Problem: Conway's Game of 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

birth to a new, live cell.

  • Exactly 2 or 3 neighbors

keep an existing cell alive.

  • Any other # of neighbors

and the central cell dies…

slide-30
SLIDE 30

white cells are empty

Lab Problem: Conway's Game of Life

Evolutionary rules Grid World

  • Everything depends on a

cell's eight neighbors red cells are "alive"

  • Exactly 3 neighbors give

birth to a new, live cell.

  • Exactly 2 or 3 neighbors

keep an existing cell alive.

  • Any other # of neighbors

and the central cell dies…

slide-31
SLIDE 31

white cells are empty

Lab Problem: Conway's Game of Life

Evolutionary rules Grid World

  • Everything depends on a

cell's eight neighbors red cells are "alive"

  • Exactly 3 neighbors give

birth to a new, live cell.

  • Exactly 2 or 3 neighbors

keep an existing cell alive.

  • Any other # of neighbors

and the central cell dies…

slide-32
SLIDE 32

red cells are alive white cells are empty

Lab Problem: Conway's Game of Life

Evolutionary rules Grid World

  • Everything depends on a

cell's eight neighbors

  • Exactly 3 neighbors give

birth to a new, live cell.

  • Exactly 2 or 3 neighbors

keep an existing cell alive.

  • Any other # of neighbors

and the central cell dies…

slide-33
SLIDE 33

For each cell…

  • 3 live neighbors – life!
  • 2 live neighbors – same
  • 0, 1, 4, 5, 6, 7, or 8 live

neighbors – death

  • computed all at once, not cell-

by-cell, so the ? at left does NOT come to life!

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

?

Lab Problem: Creating life

next_life_generation( A )

slide-34
SLIDE 34

1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

next_life_generation( A )

  • ld generation is the input, A

returns the next generation

Lab Problem: Creating life

?

slide-35
SLIDE 35

Stable configurations: Periodic

"rocks" "plants" "animals" period 3 period 2

Lab Problem: Creating life

Self-propagating

glider

slide-36
SLIDE 36

Many life configurations expand forever… What is the largest amount of the life universe that can be filled with cells? How sophisticated can Life-structures get?

www.ibiblio.org/lifepatterns/

"glider" "Gosper glider gun"

Lab Problem: Creating life

slide-37
SLIDE 37

Ex Cr: the Mandelbrot Set

Consider an update rule for all complex numbers c

z0 = 0 zn+1 = zn

2 + c

slide-38
SLIDE 38

Nothing's too complex for Python!

Python's complex #s

>>> c = 3 + 4j >>> c.real 3.0 >>> c.imag 4.0 >>> abs(c) 5.0

slide-39
SLIDE 39

c z0 z1 z2 z3 z4 Real axis Imaginary axis

Mandelbrot Definition

Consider an update rule for all complex numbers c

z0 = 0 zn+1 = zn

2 + c

Small values of c keep the sequence near the

  • rigin, 0+0j.

z5

some "stick around":

  • scillate or converge
slide-40
SLIDE 40

Mandelbrot Definition

Real axis Imaginary axis

Other values of c make the sequence head to infinity.

Benoit B. Mandelbrot 1924 – 2010

c

Small values of c keep the sequence near the

  • rigin, 0+0j.

c

Consider an update rule for all complex numbers c

z0 = 0 zn+1 = zn

2 + c

slide-41
SLIDE 41

hw9pr3: the Mandelbrot Set

Consider an update rule for all complex numbers c

z0 = 0 zn+1 = zn

2 + c

slide-42
SLIDE 42

hw9pr3: the Mandelbrot Set

Consider an update rule for all complex numbers c

z0 = 0 zn+1 = zn

2 + c

slide-43
SLIDE 43

hw9pr3 Mandelbrot Set ~ points that stick around

The shaded area are points that do not diverge for z = z**2 + c

slide-44
SLIDE 44

Higher-resolution M. Set

The black pixels are points that do not diverge for z = z**2 + c

  • 2 + 1j
  • 2 - 1j

1 + 1j 1 - 1j

connected finite area ∞ ∞ ∞ ∞ perimeter!

slide-45
SLIDE 45

Complex things always consisted of simple parts…

Chaos?

slide-46
SLIDE 46

Chaos!

http://www.youtube.com/watch?v=0jGaio87u3A not self-similar but quasi-self-similar

This was a "naturally occurring" object where zooming uncovers more detail, not less:

Before the M. Set, complex things were made of simple parts:

slide-47
SLIDE 47

The black pixels are points that do not diverge for z = z**2 + c

What are these colors?

slide-48
SLIDE 48

What are these colors?

The black pixels are points that do not diverge for z = z**2 + c

escape velocities

slide-49
SLIDE 49

Atlas of the M. Set

slide-50
SLIDE 50

In the Seahorse Valley….

slide-51
SLIDE 51

Happy Mandelbrotting!

www.cs.hmc.edu/~jgrasel/projects

http://www.youtube.com/watch?v=0jGaio87u3A

slide-52
SLIDE 52

Today is about the our final projects + it's a sales pitch for the three possible options:

Tis the season for final projects…

vPool TextID Picobot

slide-53
SLIDE 53

Final projects

Final CS assignment

  • pen-ended

comprehensive

Working solo or in a pair is OK

same projects for black/gold Pairs need to work together - in the same place - and they need to share the work equally... three choices…

slide-54
SLIDE 54

Project options…

TextID Picobot! VPool

See the IST 338 Final Projects page…

slide-55
SLIDE 55

Pre-planned possibilities

TextID Picobot! VPool

at least to think about…

slide-56
SLIDE 56

Big ideas: (1) Implement Picobot in Python (2) Train Python to write successful Picobot programs!

The Picobot project

talk about going full circle...

slide-57
SLIDE 57

Picobot returns!

slide-58
SLIDE 58

Picobot's classes

class Program:

0 xxxx -> N 0 0 Nxxx -> W 0 0 NxWx -> S 0 0 xxWx -> S 0 0 xxWS -> E 0 0 xxxS -> E 0 0 xExS -> N 0 0 xExx -> N 0 0 NExx -> S 1 1 xxxx -> S 1 1 Nxxx -> E 1 1 NxWx -> E 1 1 xxWx -> N 1 1 xxWS -> N 1 1 xxxS -> W 1 1 xExS -> W 1 1 xExx -> S 1 1 NExx -> W 0

What in Python could most usefully hold all of these rules? What type should self.rules be? self.rules[ (1,"NExx") ] = ("W",0)

key value

slide-59
SLIDE 59

Picobot's classes

class World:

What in Python could most usefully hold the environment? What type should self.room be?

+++++++++++++++++++++++++ +oooooPooooooooooooooooo+ +o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +ooooooooooooooooooooooo+ +++++++++++++++++++++++++

+

  • P

Picobot: Visited: Wall:

slide-60
SLIDE 60

Picobot's classes

class World:

What in Python could most usefully hold the environment? What type should self.room be?

+++++++++++++++++++++++++ +oooooPooooooooooooooooo+ +o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o

  • +

+o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +o o+ +ooooooooooooooooooooooo+ +++++++++++++++++++++++++

+

  • P

Picobot: Visited: Wall:

slide-61
SLIDE 61

Current State: 1 Current Rule: 1 N*W* -> X 2

++++++++++ +o++o+o+++ +oooooo ++ ++++o++ + +oooo+++++ ++++o + +oooo+++ + ++++o+++++ +Pooo + ++++++++++

The Picobot project

First build an ASCII simulation

Picobot started here… and is now here…

slide-62
SLIDE 62

Program evolution

program p1 program p2

An example of genetic algorithms, used for

  • ptimizing hard-to-describe functions.

Start with a population of, say, ~200 random Picobot programs…

slide-63
SLIDE 63

Program evolution

program p1 fitness = 0.03 program p2 fitness = 0.05

An example of genetic algorithms, used for

  • ptimizing hard-to-describe functions.

Then, evaluate the fitness

  • f all of those programs

Evaluate? How??

slide-64
SLIDE 64

program p1 fitness = 0.03 program p2 fitness = 0.05

mate + mutate the fittest rulesets

to create a new generation

  • f ~200 programs…

program c1 fitness = 0.19

slide-65
SLIDE 65

fitness = 0.90

Repeat this "survival of the fittest" process for many generations…

… and by the end, your Python code should have evolved a much more capable Picobot program!

slide-66
SLIDE 66

Project options...

TextID Picobot! VPool

slide-67
SLIDE 67

the TextID project

First paragraph of The Cuckoo's Calling by R. Galbraith

slide-68
SLIDE 68

kottke.org/13/08/how-jk-rowling-was-found-to-be-robert-galbraith

slide-69
SLIDE 69

the TextID project

Big ideas: (1) Build lexical models of bodies of text… (2) Create a similarity score that defines Rowlingness Shakepearity

NYTimes-iness WSJournalicity vs. vs. your own choice of two comparisons… Big Bang Theory? Arrested Development vs.

slide-70
SLIDE 70

TextID model

class TextModel

word-frequencies stem-frequencies

word-length-freq.'s /

all will be Python dictionaries…

WS: { "love": 50, "spell": 8, … } JKR: { "love": 11, "spell": 277, … } WS: { "lov": 612, "spell": 9, … } JKR: { "lov": 98, "spell": 306, … } WS: { 4: 7042, 5: 6203, … } JKR: { 4: 980, 5: 42005, … } sentence-length-freq.'s

slide-71
SLIDE 71

Processing steps: one idea

Split the text into words… Clean the words … Remove all non-alphabetic characters Find sentence lengths using punctuation Create dictionaries of the words and their lengths Stem the words Create a dictionary of word stems You're ready to match against another model!

This looks familiar...

slide-72
SLIDE 72

Stemming

Or tries to output the root!

stem( "party" ) "parti" stem( "parties" ) "parti" stem( "love" ) "lov" stem( "loving" ) "lov" An algorithm that outputs the root of the input word. these don't have to be words, just stems…

slide-73
SLIDE 73

Stemming

Or tries to output the root!

stem( "party" ) "parti" stem( "parties" ) "parti" stem( "love" ) "lov" stem( "loving" ) "lov" An algorithm that outputs the root of the input word. these don't have to be words, just stems…

slide-74
SLIDE 74

Model matching

Suppose we have two dictionaries: WS: { "love": 50, "spell": 8, "thou": 42 } JKR: { "love": 25, "spell": 275, "potter": 700 }

"potter" is not here. "thou" is not here.

How could we give a match score for this New dictionary against each one above? New: { "love": 3, "thou": 1, "potter": 2, "spam": 4 }

slide-75
SLIDE 75

Naïve Bayes classification

WS: { "love": 50, "spell": 8, "thou": 42 } JKR: { "love": 25, "spell": 275, "potter": 700 }

"potter" is not here. "thou" is not here.

New: { "love": 3, "thou": 1, "potter": 2, "spam": 4 }

score vs. WS score vs. JKR

50 100 50 100 50 100 42 100 100 25 1000 25 1000 25 1000 1000 700 1000

score = 0 score = 0

slide-76
SLIDE 76

Naïve Bayes classification

WS: { "love": 50, "spell": 8, "thou": 42 } JKR: { "love": 25, "spell": 275, "potter": 700 }

"potter" is not here. "thou" is not here.

New: { "love": 3, "thou": 1, "potter": 2, "spam": 4 }

score vs. WS score vs. JKR

50 100 50 100 50 100 42 100 1 100 25 1000 25 1000 25 1000 1 1000 700 1000

score = 0.0525 score = 0.000011

slide-77
SLIDE 77

Naïve Bayes classification

slide-78
SLIDE 78

Naïve Bayes classification

slide-79
SLIDE 79

Project options...

TextID Picobot! VPool

slide-80
SLIDE 80

3d graphics-based game using VPython

vPool

Let's play! I'll take your cue...

A few constraints

  • physically interacting "pool balls"
  • allow the user to direct 1+ objects
  • needs a game goal + be winnable!

not really very constrained at all!

  • must detect some collisions and

model their results on the motion

slide-81
SLIDE 81

The vPool project

Funky Physics is OK… !

Collisions with walls should be handled... Collisions with other pool balls should be handled... You need pockets – or some other game objective A few examples to get you thinking…

VPython was designed to make 3d physics simulations simpler to program

So far, VPython has eventually worked for everyone...

slide-82
SLIDE 82

Project options…

TextID Picobot! VPool

slide-83
SLIDE 83

Project details…

slide-84
SLIDE 84

Enjoy the projects!

  • … we certainly do!