One-Slide Summary • We can solve a game by recursively enumerating Puzzles, all legal moves from a position, stopping when we're left with a winning board or no more Costs possible moves. and • The basic recursive computation of Fibonacci can Sneezewort take quite a while. There are faster ways . • We can formally measure and evaluate the cost of a computer program. We abstract away details such as processor speed and instead measure how the solving time increases as the input increases . #1 #2 Example Board: “Grey” Outline (define grey-rows 5) • That Cursed Pegboard! (define grey-holes (list (make-position 1 1) (make- – Jumps, legal moves, winning, ... position 2 1) (make-position 2 2) (make-position 3 2)) • Sneezewort and Fibonacci (define grey-board (make-board grey-rows grey-holes)) • Cost of computing Fibonacci • Cost of sorting 1,1 2,1 2,2 3,1 3,2 3,3 4,1 4,2 4,3 4,4 5,1 5,2 5,3 5,4 5,5 #3 #4 Solving the Peg Board Game Filter Remove • Try all possible moves on the board (define (filter pred lst) (if (null? lst) • Try all possible moves from the positions null (if (pred (car lst)) ; pred is true, keep it you get after each possible first move (cons (car lst) (filter proc (cdr lst))) (filter pred (cdr lst))))) ; pred is false, drop it • Try all possible moves from the positions you get after trying each possible move (define ( remove-hole lst posn) from the positions you get after each ( filter (lambda (pos) possible first move (not (same-position pos posn))) lst)) • … #5 #6
Jumps Finding a Winning Strategy ;;; move creates a list of three positions: a start (the posn Start ;;; that the jumping peg starts from), a jump (the posn ;;; that is being jumped over), and end (the posn that ;;; the peg will end up in) (define ( make-move start jump end) (list start jump end)) (define ( get-start move) (first move)) (define ( get-jump move) (second move)) (define ( get-end move) (third move)) ;;; execute-move evaluates to the board after making move How is winning 2- ;;; move on board. person games (define ( execute-move board move) ... (e.g., chess, (add-peg (remove-peg (remove-peg board (get-start move)) poker) different? (get-jump move)) Winning position! (get-end move))) #7 #8 Pegboard Puzzle Pegboard Puzzle Blue Circle = Hole Position 1,1 1,1 2,1 2,2 2,1 2,2 3,1 3,2 3,3 3,1 3,2 3,3 4,1 4,2 4,3 4,4 4,1 4,2 4,3 4,4 5,1 5,2 5,3 5,4 5,5 5,1 5,2 5,3 5,4 5,5 How do we find all possible jumps that land in a How do we find all possible jumps that land in a given target hole? given target hole? #9 #10 Pegboard Puzzle All Moves into Target ;;; generate-moves evaluates to all possible moves that move a peg into ;;; the position target, even if they are not contained on the board. (define ( generate-moves target) (map (lambda (hops) 1,1 (let ((hop1 (car hops)) (hop2 (cdr hops))) 2,1 2,2 (make-move (make-position (+ (get-row target) (car hop1)) 3,1 3,2 3,3 (+ (get-col target) (cdr hop1))) (make-position (+ (get-row target) (car hop2)) 4,1 4,2 4,3 4,4 (+ (get-col target) (cdr hop2))) 5,1 5,2 5,3 5,4 5,5 target))) (list (cons (cons 2 0) (cons 1 0)) ;; right of target, hopping left (cons (cons -2 0) (cons -1 0)) ;; left of target, hopping right (cons (cons 0 2) (cons 0 1)) ;; below, hopping up How do we find all possible jumps that land in a (cons (cons 0 -2) (cons 0 -1)) ;; above, hopping down (cons (cons 2 2) (cons 1 1)) ;; above right, hopping down-left given target hole? (cons (cons -2 2) (cons -1 1)) ;; above left, hopping down-right (cons (cons 2 -2) (cons 1 -1)) ;; below right, hopping up-left (cons (cons -2 -2) (cons -1 -1)))))) ;; below left, hopping up-right #11 #12
All Possible Moves Legal Move (define ( legal-move? move) (define ( all-possible-moves board) ;; A move is valid if: (append-all ;; o the start and end positions are on the board (map generate-moves (board-holes holes)))) ;; o there is a peg at the start position ;; o there is a peg at the jump position (define ( append-all lst) (if (null? lst) null ;; o there is not a peg at the end position (append (car lst) (append-all (cdr lst))))) (and (on-board? board (get-start move)) (on-board? board (get-end move)) (peg? board (get-start move)) But…only legal if: start and end are positions (peg? board (get-jump move)) on the board containing pegs! (not (peg? board (get-end move))))) Note: could use (apply append ...) instead of append-all. #13 #14 All Legal Moves All Legal Moves (define ( legal-move? move) (define ( legal-move? move) (define ( all-possible-moves board) ;; A move is valid if: (define ( all-possible-moves board) ;; A move is valid if: ;; o the start and end positions are on the board ;; o the start and end positions are on the board (append-all (append-all ;; o there is a peg at the start position ;; o there is a peg at the start position (map generate-moves (map generate-moves ;; o there is a peg at the jump position ;; o there is a peg at the jump position (board-holes holes)))) (board-holes holes)))) ;; o there is not a peg at the end position ;; o there is not a peg at the end position (and (on-board? board (get-start move)) (and (on-board? board (get-start move)) (on-board? board (get-end move)) (on-board? board (get-end move)) (peg? board (get-start move)) (peg? board (get-start move)) (peg? board (get-jump move)) (peg? board (get-jump move)) (not (peg? board (get-end move))))) (not (peg? board (get-end move))))) (define ( legal-moves board) (define ( legal-moves board) ??? (filter legal-move? (all-possible-moves board))) #15 #16 Winning Becoming a “Genius”! Position Start Try all possible legal moves How do we tell if a board is in a winning position? ... Winning position! #17 #18
is-winning-position? Solve Pegboard (define ( solve-pegboard board) (define ( board-squares board) (find-first-winner board (legal-moves board))) (count-squares (board-rows board))) (define ( find-first-winner board legal-moves) ;; returns a list of moves that will solve the given board (define ( count-squares nrows) (if (null? legal-moves) (if (= nrows 1) 1 (if (is-winning-position? board) (+ nrows (count-squares (- nrows 1))))) null ;; Found winning game, no moves needed #f) ;; A losing position, no more moves (define ( is-winning-position? board) ( let ((result (solve-pegboard (execute-move board (car legal-moves))))) (= (length (board-holes board)) (if result ;; winner (not #f) (- (board-squares board) 1))) (cons (car legal-moves) result) ;; this move wins! (find-first-winner board (cdr legal-moves)))))) ;; try rest #19 #20 All Cracker Barrel Games All Cracker Barrel Games (starting with peg 2 1 missing) (starting with peg 2 1 missing) Pegs Number Fraction of Cracker Barrel Pegs Number Fraction of Cracker Barrel Left of Ways Games Left of Ways Games IQ Rating IQ Rating 1 1550 0.01 1 1550 0.01 “You’re Genius” “You’re Genius” 2 20686 0.15 “You’re Purty Smart” 2 20686 0.15 “You’re Purty Smart” 3 62736 0.46 “Just Plain Dumb” 3 62736 0.46 “Just Plain Dumb” 4 46728 0.33 4 46728 0.33 “Just Plain “Just Plain 5 5688 0.04 5 5688 0.04 6 374 0.0027 6 374 0.0027 Eg-no-ra-moose” Eg-no-ra-moose” 7 82 0.00058 7 82 0.00058 Leaving 10 pegs requires much 10 2 0.00001 10 2 0.00001 more brilliance than leaving 1!? #21 #22 Liberal Arts Trivia: History Liberal Arts Trivia: Physics • This 20 th -century American inventor is credited • Count Alessandro Antonio Anastasio Volta was with the phonograph, the carbon telephone a 19 th -century Italian physicist. Volta studied transmitter, the practical electric light, and what we now call capacitance, developing the phrase “Genius is one percent inspiration, separate means to study both electrical ninety-nine percent perspiration.” He fought potential V and charge Q , and discovering that against Nikola Tesla's alternating current in the for a given object they are proportional. His so-called War of the Currents. experiments in “animal electricity”, in which two different metals were connected in series with frog's legs, eventually led to his most famous discovery. What was it? #23 #24
Sneezewort • Achillea ptarmica is real. • It is “moste efficacious in the inflaming of the braine, and [is] "V" shrubbery therefore much used in Confusing by Andrew Jesien, Becky Elstad and Befuddlement Draughts, where the wizard is desirous of producing hot-headedness and recklessness.” – Order of the Phoenix, p.18 After the Incident • Sneezewort's pattern of by Ben Morrison and Liz Peterson development displays the Robot Cav Man Fibonacci sequence. by Jamie Jeon & Walter Borges #25 #26 Sneezewort Growth Sneezewort Numbers 13 8? 5 3 2 1 1 pink First Time Unit Second Time Unit Offshoot by Jessica Geist, Ellen Clarke Could we model Sneezewort with PS3 code? #27 #28 Fibo Results Tracing Fibo > (fibo 2) > (require-library "trace.ss") 1 > (trace fibo) > (fibo 3) (fibo) 2 > (fibo 3) > (fibo 4) |(fibo 3) 3 | (fibo 2) > (fibo 10) | 1 55 | (fibo 1) > (fibo 60) | 1 Still working… |2 Purple Arrow At least we finished. by Rachel Lathbury and Andrea Yoon 2 by Dmitriy Semenov and Sara Alspaugh #29 #30
Recommend
More recommend