Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS 100 9.1 Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”

Similar presentations


Presentation on theme: "CPS 100 9.1 Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”"— Presentation transcript:

1 CPS 100 9.1 Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand” rule ä Other mazes, e.g., with islands, require another approach ä If you have “markers”, leave them at intersections, don’t explore the same place twice l What happens if you try to search the web, using links on pages to explore other links, using those links to … ä How many web pages are there? ä What rules to webcrawlers/webspiders follow? Who enforces the rules? l Keep track of where you’ve been don’t go there again ä Any problems with this approach?

2 CPS 100 9.2 Backtracking with Boggle l Boggle tm played on 4x4 board ä Other sizes possible ä Form words by connecting letters horizontally, vertically, diagonally ä Cannot re-use letters (normally) l Two approaches ä Build words from connections, find partial words in dictionary ä Look up every word in the dictionary on the board l Which is better? How is backtracking used?

3 CPS 100 9.3 Classic problem: N queens l Can queens be placed on a chess board so that no queens attack each other? ä Easily place two queens ä What about 8 queens? l Make the board NxN, this is the N queens problem ä Place one queen/column ä # different tries/column? l Backtracking ä Use “current” row in a col ä If ok, try next col ä If fail, back-up, next row

4 CPS 100 9.4 Backtracking idea with N queens l Try to place a queen in each column in turn ä Try first row in column C, if ok, move onto next column ä If solved, great, otherwise try next row in column C, place queen, move onto the next column Must unplace the placed queen to keep going l What happens when we start in a column, where to start? ä If we fail, move back to previous column (which remembers where it is/failed) ä When starting in a column anew, start at beginning When backing up, try next location, not beginning l Backtracking in general, record an attempt go forward ä If going forward fails, undo the record and backup

5 CPS 100 9.5 Basic ideas in backtracking search l We need to be able to 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 This is the backtracking step, choices must be undoable l Process is inherently recursive, so we need to know when the search finishes ä When all columns tried in N queens ä When all board locations tried in boggle ä When every possible moved tried in Tic-tac-toe or chess? Is there a difference between these games? l Summary: enumerate choices, try a choice, undo a choice, this is brute force search: try everything

6 CPS 100 9.6 Computer v. Human in Games l Computers can explore a large search space of moves quickly ä How many moves possible in chess, for example? l 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 l What do humans do well in these games? What about computers? ä What about at Duke?

7 CPS 100 9.7 Backtracking, minimax, game search l We’ll use tic-tac-toe to illustrate the idea, but it’s a silly game to show the power of the method ä What games might be better? Problems? l Minimax idea: two players, one maximizes score, the other minimizes score, search complete/partial game tree for best possible move ä In tic-tac-toe we can search until the end-of-the game, but this isn’t possible in general, why not? ä Use static board evaluation functions instead of searching all the way until the game ends l Minimax leads to alpha-beta search, then to other rules and heuristics

8 CPS 100 9.8 Minimax for tic-tac-toe l Players alternate, one might be computer, one human (or two computer players) l Simple rules: win scores +10, loss scores –10, tie is zero ä X maximizes, O minimizes l Assume opponent plays smart ä What happens otherwise? l As game tree is explored is there redundant search? ä What can we do about this? X O X X O X X O X X X O X O X O X O X O X O X O X X O X O

9 CPS 100 9.9 yfzhltea byveqye l The words above represent a simple substitution cypher ä Each letter mapped to one other letter, no inconsistencies ä Often used in cryptogram puzzles (newspaper, online, …) ä How can we write a computer program to solve this? l Ideas for solving the problem? Benchmark/ballpark idea to accept (or not) ä l Problems on the horizon?


Download ppt "CPS 100 9.1 Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”"

Similar presentations


Ads by Google