22
CPS 201, Spring 2013 backtrack.1 Backtracking (images and music) Nashville Bluegrass Band

CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.1

Backtracking (images and music)

Nashville Bluegrass Band

Page 2: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.2

Toward Boggle-preparedness   Making progress on Boggle assignment

Ø  Many, many classes, each designed to do one thing Ø  Some related by inheritance, object-oriented concept Ø  Many single purpose classes --- can be hard to grok Ø  Recursive backtracking, also hard to grok

  Toward trees and recurrences Ø  More self-referential structures Ø  Recursive algorithms Ø  Self-referential run-time analysis (kind of)

Page 3: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.3

Searching with no guarantees   Search for best move in automated game play

Ø  Can we explore every move? Ø  Can we rank candidate moves by "goodness"? Ø  Can we explore entire "tree" of possible moves?

  Search with all or partial information Ø  Google search autocomplete aka Google Instant Ø  What numbers fit in Sudoku square Ø  Finding words on a Boggle board

  Try something, if at first you don't succeed ….

Page 4: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.4

Search, Backtracking,Heuristics   How do you find a needle in a haystack?

Ø  How does a computer play chess? Ø  Why would you write that program? Ø 

  How does Bing/Googlemap find routes from one place to another? Or routing in Internet Ø  Shortest path algorithms, very efficient algorithms Ø  Longest path algorithms, no known efficient algorithm!

  Optimal algorithms and heuristic algorithms Ø  When is close good enough? How do measure “closeness”? Ø  When is optimality important, how much does it cost?

Page 5: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.5

Game Tree Search (chess and ttt)

Page 6: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.6

Exhaustive Search/Heuristics   We can probably explore entire game tree for tic-

tac-toe, but not for chess Ø  How many tic-tac-toe boards are there? Ø  How many chess boards are there?

  What do we do when the search space is huge? Ø  Brute-force/exhaustive won't work, need heuristics? Ø  What about google-maps/Garmin finding routes?

  Backtracking can use both concepts Ø  Game tree pruning a good idea most of the time

Page 7: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.7

Classic problem: N queens   Can queens be placed on a

chess board so that no queens attack each other? Ø  Easily place two queens Ø  What about 8 queens?

  Make the board NxN, this is the N queens problem Ø  Place one queen/column Ø  Horiz/Vert/Diag attacks

  Backtracking Ø  Tentative placement Ø  Recurse, if ok done! Ø  If fail, undo tentative, retry

  wikipedia-n-queens

Page 8: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.8

General Backtracking Idea   Explore search space: find gene/protein, TTT move

Ø  Create a state that represents current information Ø  From a list of changes/moves explore/try one next state Ø  Undo the change/move to restore state, continue

  Sometimes stop before exploring all possible states Ø  Run out of time, space, other resources Ø  Find optimal result, no need to continue Ø  Fail and nothing else to explore

Page 9: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.9

Backtracking idea with N queens   State: Number of columns with a Q, row for each Q   For each column C, tentatively place a queen

Ø  Try first row in column C, if ok, move on to next column • Typically “move on” is recursive

Ø  If solved, done, otherwise try next row in column C • Must unplace queen when failing/unwind recursion

  Each column C tracks what row R the Q is on Ø  If first time, that's row zero, but might be an attack Ø  Unwind recursion/backtrack, try “next” location

  Backtracking: record an attempt go forward Ø  Move must be “undoable” on backtracking/unwinding

Page 10: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.10

N queens backtracking: Queens.java public boolean solve(int col){ if (col == mySize) return true; // try each row until all are tried for(int r=0; r < mySize; r++){ if (myBoard.safeToPlace(r,col)){ myBoard.setQueen(r,col,true); if (solve(col+1)){ return true; } myBoard.setQueen(r,col,false); } } return false; }

Page 11: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.11

col=0 int r= ,1,2,3

col=1 int r=0,1, ,3

col=2 int r=

X

X

for(int r=0; r < mySize; r++){ if (myBoard.safeToPlace(r,col)){ myBoard.setQueen(r,col,true); if (solve(col+1)){ return true; } myBoard.setQueen(r,col,false); } } return false;

Run in debug mode, tracing execution

Page 12: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.12

Basic ideas in backtracking search   Enumerate all possible choices/moves

Ø  We try these choices in order, committing to a choice Ø  If the choice doesn’t pan out we must undo the choice

• Backtracking step, choices must be undoable

  Inherently recursive, when to stop searching? Ø  When all columns tried in N queens Ø  When we have found the exit in a maze Ø  When every possible moved tried in Tic-tac-toe or chess?

•  Is there a difference between these games?

  Summary: enumerate choices, try a choice, undo a choice, this is brute force search: try everything

Page 13: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.13

Pruning vs. Exhaustive Search   If we consider every possible placement of 4 queens

on a 4x4 board, how many are there? (N queens) Ø  4x4x4x4 if we don't pay attention to any attacks Ø  4x3x2x1 if we avoid attacks in same row

  What about if we avoid diagonal attacks? Ø  Pruning search space makes more search possible, still

could be lots of searching to do!

  Estimate how long to calculate # solutions to the N-queens problem with our Java code….

Page 14: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.14

Queens Problems   Snarf InClass queens

Ø  If images don't appear: Run>Run Configurations> Ø  …Arguments>Working Directory>Workspace>

http://bit.ly/201-queens

Page 15: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.15

Tuomas Sandholm, CMU Professor   IJCAI Computers and Thought 2003 Organ Network uses Carnegie Mellon algorithm to match live kidney donors with recipients National Pilot Program facilitates kidney paired-donation transplants   http://bit.ly/9No5S5 Using game theory, a group of computer scientists has developed a set of algorithms to help thwart terrorist attacks by randomizing where and when security checkpoints, officers, canine units and other deterrents are located in and around the airport.

Page 16: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.16

Vince Conitzer @ cs.duke.edu Vincent Conitzer will receive the 2011 Computers and Thought Award … The award is presented every two years to the world’s leading AI researchers under the age of 35. Conitzer, professor of computer science at Duke University, is receiving the award in recognition of his seminal work at the boundary of microeconomic theory and artificial intelligence, in particular for groundbreaking work on computational aspects of game theory, social choice, and mechanism design.

Page 17: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.17

Computer v. Human in Games   Computers can explore a large

search space of moves quickly Ø  How many moves possible in

chess, for example?

  Computers cannot explore every move (why) so must use heuristics Ø  Rules of thumb about position,

strategy, board evaluation

Ø  Try a move, undo it and try another, track the best move

  What do humans do well in these games? What about computers? Ø  What about at Duke?

Page 18: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.18

Games at Duke   Alan Biermann

Ø  Natural language processing Ø  Compsci 1: Great Ideas Ø  Duchess, checkers, chess

  Tom Truscott Ø  Duke undergraduate working

with/for Biermann Ø  Usenet: online community

  Second EFF Pioneer Award (with Vint Cerf!)

Page 19: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.19

Heuristics   A heuristic is a rule of thumb, doesn’t always work, isn’t

guaranteed to work, but useful in many/most cases Ø  Search problems that are “big” often can be approximated or

solved with the right heuristics

  What heuristic is good for Sudoku? Ø  Is there always a no-reasoning move, e.g., 5 goes here? Ø  What about “if I put a 5 here, then…” Ø  Do something else?

http://en.wikipedia.org/wiki/Algorithmics_of_sudoku

  What other optimizations/improvements can we make? Ø  For chess, checkers: good heuristics, good data structures

Page 20: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.20

http://nyti.ms/16EbIU8 Heuristic The young man, Peng Shi, a 24-year-old doctoral student at the Massachusetts Institute of Technology, began asking questions and talking to parents.

Then he made a suggestion: why not drop the idea of zones altogether? He is Peng Shi, Duke 2010: http://bit.ly/107k2sR

Page 21: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.21

Gridgame APT   What is this APT about? How is the game played?

Ø  When is the game over, how can you tell who wins Ø  How do you enumerate all possible moves? Ø  How do you "try" a move, and then untry it?

  What is a good representation of the board? Ø  Why not an array of strings? Ø  How to transform into something more useful

  Let's get started

Page 22: CPS 201, Spring 2013 backtrack - Duke Computer ScienceCPS 201, Spring 2013 backtrack.2 Toward Boggle-preparedness Making progress on Boggle assignment Many, many classes, each designed

CPS 201, Spring 2013 backtrack.22

The young man, Peng Shi, a 24-year-old doctoral student at the Massachusetts Institute of Technology, began asking questions and talking to parents. Then he made a suggestion: why not drop the idea of zones altogether?