Slide 1

Slide 1 text

Computer Recreations OR, REDISCOVERING THE 1980S FOR PROGRAMMING FUN Andrew Kuchling amk @ amk.ca speakerdeck.com/akuchling/computer-recreations

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Photo by Colm Mulcahy

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

#1: Cellular automata 4 5 4 1 0 4 3 5 1 (N = 6) If a cell is in state n, And has a neighbour in state n+1, Then the cell changes to state n+1

Slide 14

Slide 14 text

#1: Cellular automata 4 5 4 1 0 4 3 5 1 5 0 5 1 1 4 4 0 1

Slide 15

Slide 15 text

@classmethod def __init__(self, width=WIDTH, height=HEIGHT): self.board = {} self.width = width self.height = height for i in range(width): for j in range(height): board.board[(i,j)] = random.randrange( 0, NUM_STATES)

Slide 16

Slide 16 text

def step(self): ”Do one iteration step of the cellular automaton" new = self.board.copy() for i in range(self.width): for j in range(self.height): new[(i,j)] = self.cell_rule(i, j) self.board.update(new)

Slide 17

Slide 17 text

def cell_rule_4_cell_neighbourhood(self, x, y): "Evaluate the rule for a single cell…” value = self.board[x,y] successor = (value + 1) % NUM_STATES for (i, j) in [(-1, 0), (1, 0), (0, -1), (0, 1)]: neighb = self.board.get((x+i, y+j)) if neighb == successor: return successor else: return value

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

01011001 11001011 #2: The evolution of flibs

Slide 20

Slide 20 text

01011001 11001011 Result: 5 matches of 8 = 62%

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

1B1C0C0B1A0A

Slide 23

Slide 23 text

1 B 1 C 0 C 0 B 1 A 0 A 1 B 0 A 1 A 1 C 0 B 0 C

Slide 24

Slide 24 text

1 B 1 C 0 C 0 B 1 A 0 A 1 B 0 A 1 A 1 C 0 B 0 C

Slide 25

Slide 25 text

1 B 1 C 0 C 0 B 1 A 0 A 1 B 0 A 1 A 1 C 0 B 0 C 1 B 1 A 1 A 1 C 0 A 0 A

Slide 26

Slide 26 text

Generation 0 Best score in pool: 75% Environment = 00100110100010011010 Prediction = 01000100100110010010 Worst score in pool: 20% Environment = 00100110100010011010 Prediction = 11101101011101110101

Slide 27

Slide 27 text

Generation 100 Best score in pool: 90% Environment = 00100110100010011010 Prediction = 00100100100010010010 Worst score in pool: 45% Environment = 00100110100010011010 Prediction = 10001100011110110110

Slide 28

Slide 28 text

Generation 1500 Best score in pool: 95% Environment = 00100110100010011010 Prediction = 00100110100110011010 Worst score in pool: 50% Environment = 00100110100010011010 Prediction = 00001000110000100011

Slide 29

Slide 29 text

Perfect score found! In 5248 generations Environment = 00100110100010011010 Prediction = 00100110100010011010 0 1 A D,0 B,0 B A,0 C,0 C A,1 A,1 D E,1 B,1 E F,0 E,0 F D,1 B,1

Slide 30

Slide 30 text

" = −1 or " = −1 (+) (+) = (+) (+) (-) = (-) (+) = (-) (-) (-) = (-) #3: Iterated functions

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

>>> c = 0.5 + 2j ; c (0.5+2j) >>> c2 = complex(1,3) ; c2 (1+3j) >>> c2.real, c2.imag (1.0, 3.0) >>> abs(c2) 3.1622776601683795

Slide 33

Slide 33 text

, = ,-. " + Now, repeat: 0 = 0 Pick a complex number

Slide 34

Slide 34 text

, = ,-. " + 0 = 0 = 0.1 n Zn 1 0.1 2 0.11 3 0.1121 4 0.11256641 5 0.112671196660288 6 0.112694798556861 7 0.112700117621771 55

Slide 35

Slide 35 text

, = ,-. " + 0 = 0 = -1.5 + 0.5j n Zn 1 -1.5 + 0.5j 2 0.5 - 1j 3 -2.25 - 0.5j 4 3.3125 + 2.75j 5 1.9101+ 18.71875j 6 -348.24 + 72.01j 7 116085 -50154j

Slide 36

Slide 36 text

, = ,-. " + 0 = 0 = 0.1 + 0.4j n Zn 1 0.1 + 0.4j 2 -0.05 + 0.48j 3 0.1279 + 0.352j 4 0.00754559 + 0.3099584j 5 0.0039827261978 + 0.3953223619930j 6 0.0562639077838 + 0.4031489214554j 7 -0.059363425551 + 0.3546345325201j

Slide 37

Slide 37 text

def iterate(c, max_iterations=100): "Iterates z**2 + c, for up to max_iterations” z = 0j for i in range(max_iterations): z = z**2 + c if abs(z) > 2.0: return (i+1) else: return 0

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

def compute_grid(self, im_width, im_height): cx, cy = self.get_center() x_inc, y_inc = (self.width / (im_width / 2), …) grid = np.zeros((im_width, im_height)) for i in range(im_width): for j in range(im_height): coord = complex((cx - self.width) + i * x_inc, (cy - self.height) + j * y_inc) count = iterate(coord) grid[i,j] = count return grid

Slide 44

Slide 44 text

# Vectorized code from Jean-Francois Puget def compute_grid_vectorized(self,im_width,im_height,maxiter=100): # … set up np.linspace() for x and y … c = np.array([complex(x,y) for x in rx for y in ry]) c = c.reshape((im_width,im_height)) grid = np.zeros(c.shape) z = np.zeros(c.shape, np.complex64) for it in range(maxiter): notdone = np.less(z.real*z.real + z.imag*z.imag, 4.0) grid[notdone] = it z[notdone] = z[notdone]**2 + c[notdone] return grid

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

Q & A speakerdeck.com/akuchling/computer-recreations