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

Slides:



Advertisements
Similar presentations
Computers playing games. One-player games Puzzle: Place 8 queens on a chess board so that no two queens attack each other (i.e. on the same row, same.
Advertisements

 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CPS 100, Fall Backtracking by image search.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
The N-Queens Problem lab01.
CompSci 100e Program Design and Analysis II March 3, 2011 Prof. Rodger CompSci 100e, Spring
Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
Backtracking.
Document Editing PCS Lesson 4. Objectives List situations that require you to edit and undo documentation Demonstrate how to edit your documentation Demonstrate.
CPS 100e, Spring Day, Week, Month Overview l Backtracking p/review  Boggle  APT l Assignment review  Boggle  APT l PQ Overview.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Problem Solving and Mazes
CPS 100, Fall PFTN2Wks l Making progress on Boggle assignment  Many, many classes, each designed to do one thing  Some related by inheritance,
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Sudoku Jordi Cortadella Department of Computer Science.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
CHAPTER 4 RECURSION. BASICALLY, A METHOD IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
CompSci 100 Prog Design and Analysis II Oct 26, 2010 Prof. Rodger CPS 100, Fall
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
CompSci Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
CSCE350 Algorithms and Data Structure Lecture 21 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
CompSci 100e 7.1 Hashing: Log ( ) is a big number l Comparison based searches are too slow for lots of data  How many comparisons needed for a.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”
1 CSC 384 Lecture Slides (c) , C. Boutilier and P. Poupart CSC384: Lecture 16  Last time Searching a Graphplan for a plan, and relaxed plan heuristics.
CompSci Problem Solving: Sudoku  Rules of the Game Sudoku is played with a 9 by 9 "board" consisting of nine 3 by 3 sub-boards. The symbols 1 -
CPS 100, Spring Search, Trees, Games, Backtracking l Trees help with search  Set, map: binary search tree, balanced and otherwise  Quadtree.
CPS 100, Spring Search, Backtracking,Heuristics l How do you find a needle in a haystack?  How does a computer play chess?  Why would you write.
Understanding AI of 2 Player Games. Motivation Not much experience in AI (first AI project) and no specific interests/passion that I wanted to explore.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
TWTG What work is still to come APT, Huff, APT
Game playing Types of games Deterministic vs. chance
TWTG What work is still to come APT, Huff, APT
Backtracking, Search, Heuristics
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Fundamentals of Programming II Backtracking with Stacks
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
CSE 143 Lecture 19 More Recursive Backtracking
Using a Stack Chapter 6 introduces the stack data type.
Recursion Copyright (c) Pearson All rights reserved.
Back Tracking.
Analysis and design of algorithm
Using a Stack Chapter 6 introduces the stack data type.
adapted from Recursive Backtracking by Mike Scott, UT Austin
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
CSE 143 Lecture 18 More Recursive Backtracking
CSE 143 Lecture 18 More Recursive Backtracking
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Backtracking, Search, Heuristics
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Backtracking, Search, Heuristics
Presentation transcript:

CPS 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?

CPS 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

CPS 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

CPS 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 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? l Summary: enumerate choices, try a choice, undo a choice, this is brute force search: try everything

CPS 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; }

CPS Queens Details l How do we know when it’s safe to place a queen?  No queen in same row, or diagonal  For each column, store the row that a queen is in  See QBoard.java for details l For GUI version, we use a decorator  The QBoardGUI is an IQueenState class and it has an IQueenState object in it  Appears as an IQueenState to client, but uses an existing one to help do its work  One of many object oriented design patterns, seen in Huff in the BitInputStream class

CPS 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?

CPS Heuristics l 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 l 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? l What other optimizations/improvements can we make?  For chess, checkers: good heuristics, good data structures