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 - - 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
4/1/12!
4/1/07!
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…
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
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
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)
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
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… ?
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
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.
Differing approaches to rules …
Mathematicians CS? Engineers Physicists
Engineers believe equations approximate reality;
"the rules" Grand Canyon Skywalk
Safety margins!
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!
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"
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?
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
2D data!
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!
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?
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
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
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:
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!
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!
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
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…
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…
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…
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…
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 )
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
?
Stable configurations: Periodic
"rocks" "plants" "animals" period 3 period 2
Lab Problem: Creating life
Self-propagating
glider
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
Ex Cr: the Mandelbrot Set
Consider an update rule for all complex numbers c
z0 = 0 zn+1 = zn
2 + c
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
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
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
hw9pr3: the Mandelbrot Set
Consider an update rule for all complex numbers c
z0 = 0 zn+1 = zn
2 + c
hw9pr3: the Mandelbrot Set
Consider an update rule for all complex numbers c
z0 = 0 zn+1 = zn
2 + c
hw9pr3 Mandelbrot Set ~ points that stick around
The shaded area are points that do not diverge for z = z**2 + c
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!
Complex things always consisted of simple parts…
Chaos?
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:
The black pixels are points that do not diverge for z = z**2 + c
What are these colors?
What are these colors?
The black pixels are points that do not diverge for z = z**2 + c
escape velocities
Atlas of the M. Set
In the Seahorse Valley….
Happy Mandelbrotting!
www.cs.hmc.edu/~jgrasel/projects
http://www.youtube.com/watch?v=0jGaio87u3A
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
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…
Project options…
TextID Picobot! VPool
See the IST 338 Final Projects page…
Pre-planned possibilities
TextID Picobot! VPool
at least to think about…
Big ideas: (1) Implement Picobot in Python (2) Train Python to write successful Picobot programs!
The Picobot project
talk about going full circle...
Picobot returns!
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
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:
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:
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…
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…
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??
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
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!
Project options...
TextID Picobot! VPool
the TextID project
First paragraph of The Cuckoo's Calling by R. Galbraith
kottke.org/13/08/how-jk-rowling-was-found-to-be-robert-galbraith
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.
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
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...
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…
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…
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 }
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
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
Naïve Bayes classification
Naïve Bayes classification
Project options...
TextID Picobot! VPool
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
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...
Project options…
TextID Picobot! VPool
Project details…
Enjoy the projects!
- … we certainly do!