CPS 100, Fall 2009 9.1 Backtracking by image search.

Slides:



Advertisements
Similar presentations
Types of Algorithms.
Advertisements

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
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?
Game Playing CSC361 AI CSC361: Game Playing.
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
CIS 310: Visual Programming, Spring 2006 Western State College 310: Visual Programming Othello.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Backtracking.
1 Adversary Search Ref: Chapter 5. 2 Games & A.I. Easy to measure success Easy to represent states Small number of operators Comparison against humans.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Game Playing Chapter 5. Game playing §Search applied to a problem against an adversary l some actions are not under the control of the problem-solver.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
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.
NxN Queens Problem Q- -- -Q Q- -- -Q Q- -- QQ -- Q- Q- Q- -Q QQ -- -Q -- -Q Q- -Q -Q Q- Q- -Q Q- -- Q- -- QQ Q- -Q -Q -Q -- QQ -- -Q START.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
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.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze  Certain mazes can be solved using the “right-hand”
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,
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
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
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.
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
Problem Solving: Brute Force Approaches
Backtracking, Search, Heuristics
CSSE 230 Day 25 Skip Lists.
CSCI 104 Backtracking Search
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.
Problem Solving: Brute Force Approaches
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
Owen Astrachan November 9, 2018
Presentation transcript:

CPS 100, Fall Backtracking by image search

CPS 100, Fall Search, Trees, Games, Backtracking l Trees help with search  Set, map: binary search tree, balanced and otherwise  Quadtree and more, scenes and objects in games  3-4 Trees, KD-trees, search for files or points in “space”  Gametrees, search for game-playing l Intelligent search  How do we find the “best move” in a game?  How do we find optimal needle in haystack?  When is brute-force, exhaustive search (im)possible?

CPS 100, Fall Search, Backtracking,Heuristics l How do you find a needle in a haystack?  How does a computer play chess?  Why would you write that program?  l How does Bing/Googlemap find routes from one place to another?  Shortest path algorithms  Longest path algorithms l Optimal algorithms and heuristic algorithms  When is close good enough? How do measure “closeness”?  When is optimality important, how much does it cost?

CPS 100, Fall Exhaustive Search/Heuristics l We use binary search trees to organize data, in searching we don’t need to examine all the data to find what we’re looking for  Where is the smallest item in a search tree? Largest?  How are trees balanced? l What do we do when the search space is huge?  How many chess boards are there?  Count routes between my house and yours? l Exhaustive search: look at everything!

CPS 100, Fall 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  Horiz/Vert/Diag attacks l Backtracking  Tentative placement  Recurse, if ok done!  If fail, undo tentative, retry l wikipedia-n-queens wikipedia-n-queens

CPS 100, Fall Backtracking idea with N queens l For each column C, tentatively place a queen  Try first row in column C, if ok, move onto next column Typically “move on” is recursive  If solved, done, otherwise try next row in column C Must unplace queen when failing/unwind recursion l Each column try “knows” where to start its row  If first time, that’s row zero, but might be an attack  Unwind recursion/backtrack, try “next” location l Backtracking: record an attempt go forward  Move must be “undoable” on backtracking/unwinding

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

CPS 100, Fall Pruning vs. Exhaustive Search l 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 l What about if we avoid diagonal attacks?  Pruning search space makes more search possible, still could be lots of searching to do! l Estimate how long to calculate # solutions to the N-queens problem with our Java code….

CPS 100, Fall 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 100, Fall Daphne Koller l 2004, Macarthur l 2008, first ACM/Infosys l “The world is noisy and messy …You need to deal with the noise and uncertainty.” “I find it distressing that the view of the field is that you sit in your office by yourself surrounded by old pizza boxes and cans of Coke, hacking away at the bowels of the Windows operating system,” she said. “I spend most of my time thinking about things like how does a cell work or how do we understand images in the world around us?”

CPS 100, Fall 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 100, Fall Games at Duke l Alan Biermann  Natural language processing  Compsci 1: Great Ideas  Duchess, checkers, chess l Tom Truscott  Duke undergraduate working with/for Biermann  Usenet: online community l Second EFF Pioneer Award (with Vint Cerf!) Second EFF Pioneer Award (with Vint Cerf!)

CPS 100, Fall 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

CPS 100, Fall Boggle Program

CPS 100, Fall Boggle Search for Word l Starting at board location (row,col): find a string S  We want to keep track of where we are in the string  Also track what board locations used for S search l How do we know when we’re done?  Base case of recursive, backtracking call  Where we are in the string? l How do we keep track of used locations?  Store in array list: tentatively use current one, recurse  If we don’t succeed, take off the last one stored!