Search Much of AI has traditionally revolved around search –Search means “searching among all the possibilities for an answer” (or possibly the best answer)

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Knowledge Representation
Advertisements

Adversarial Search We have experience in search where we assume that we are the only intelligent being and we have explicit control over the “world”. Lets.
BackTracking Algorithms
Problem Solving by Searching Copyright, 1996 © Dale Carnegie & Associates, Inc. Chapter 3 Spring 2007.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
October 1, 2012Introduction to Artificial Intelligence Lecture 8: Search in State Spaces II 1 A General Backtracking Algorithm Let us say that we can formulate.
Artificial Intelligence Adversarial search Fall 2008 professor: Luigi Ceccaroni.
September 26, 2012Introduction to Artificial Intelligence Lecture 7: Search in State Spaces I 1 After our “Haskell in a Nutshell” excursion, let us move.
Search in AI.
Search Strategies.  Tries – for word searchers, spell checking, spelling corrections  Digital Search Trees – for searching for frequent keys (in text,
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
Artificial Intelligence in Game Design
Mahgul Gulzai Moomal Umer Rabail Hafeez
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
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.
Problem Solving and Search in AI Heuristic Search
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Using Search in Problem Solving
CIS 310: Visual Programming, Spring 2006 Western State College 310: Visual Programming Othello.
Alpha-Beta Search. 2 Two-player games The object of a search is to find a path from the starting position to a goal position In a puzzle-type problem,
Backtracking.
Game Playing: Adversarial Search Chapter 6. Why study games Fun Clear criteria for success Interesting, hard problems which require minimal “initial structure”
State-Space Searches.
Brute Force Search Depth-first or Breadth-first search
Heuristic Search Heuristic - a “rule of thumb” used to help guide search often, something learned experientially and recalled when needed Heuristic Function.
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.
Game Trees: MiniMax strategy, Tree Evaluation, Pruning, Utility evaluation Adapted from slides of Yoonsuck Choe.
Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning CPSC 315 – Programming Studio Spring 2008 Project 2, Lecture 2 Adapted from slides of Yoonsuck.
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
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.
Game Playing.
Formal Description of a Problem In AI, we will formally define a problem as –a space of all possible configurations where each configuration is called.
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.
Mark Dunlop, Computer and Information Sciences, Strathclyde University 1 Algorithms & Complexity 5 Games Mark D Dunlop.
Introduction to search Chapter 3. Why study search? §Search is a basis for all AI l search proposed as the basis of intelligence l inference l all learning.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
For Wednesday Read Weiss, chapter 12, section 2 Homework: –Weiss, chapter 10, exercise 36 Program 5 due.
BackTracking CS335. N-Queens The object is to place queens on a chess board in such as way as no queen can capture another one in a single move –Recall.
Dr.Abeer Mahmoud ARTIFICIAL INTELLIGENCE (CS 461D) Dr. Abeer Mahmoud Computer science Department Princess Nora University Faculty of Computer & Information.
Today’s Topics FREE Code that will Write Your PhD Thesis, a Best-Selling Novel, or Your Next Methods for Intelligently/Efficiently Searching a Space.
CS 415 – A.I. Slide Set 5. Chapter 3 Structures and Strategies for State Space Search – Predicate Calculus: provides a means of describing objects and.
State-Space Searches. 2 State spaces A state space consists of A (possibly infinite) set of states The start state represents the initial problem Each.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
Search exploring the consequences of possible actions.
Backtracking and Games Eric Roberts CS 106B January 28, 2013.
Lecture 3: Uninformed Search
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
Today’s Topics Playing Deterministic (no Dice, etc) Games –Mini-max –  -  pruning –ML and games? 1997: Computer Chess Player (IBM’s Deep Blue) Beat Human.
ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.
Search in State Spaces Problem solving as search Search consists of –state space –operators –start state –goal states A Search Tree is an efficient way.
CompSci Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the.
Graph Search II GAM 376 Robin Burke. Outline Homework #3 Graph search review DFS, BFS A* search Iterative beam search IA* search Search in turn-based.
CPS Backtracking, Search, Heuristics l Many problems require an approach similar to solving a maze ä Certain mazes can be solved using the “right-hand”
Adversarial Search 2 (Game Playing)
Artificial Intelligence in Game Design Lecture 20: Hill Climbing and N-Grams.
February 18, 2016Introduction to Artificial Intelligence Lecture 8: Search in State Spaces III 1 A General Backtracking Algorithm Sanity check function.
Explorations in Artificial Intelligence Prof. Carla P. Gomes Module 5 Adversarial Search (Thanks Meinolf Sellman!)
Artificial Intelligence in Game Design Board Games and the MinMax Algorithm.
Search CSC 358/ Outline Homework #6 Game search States and operators Issues Search techniques DFS, BFS Beam search A* search Alpha-beta.
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.
BackTracking CS255.
Artificial Intelligence Problem solving by searching CSC 361
adapted from Recursive Backtracking by Mike Scott, UT Austin
Artificial Intelligence
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Unit II Game Playing.
Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning
Presentation transcript:

Search Much of AI has traditionally revolved around search –Search means “searching among all the possibilities for an answer” (or possibly the best answer) consider, as a programmer, you are facing a logical error in your program and want to fix it usually the program’s behavior gives you some indication of the problem –doesn’t print results? Stuck in an infinite loop –garbage output? Possible pointer dereferencing problem –wrong values output? Possible wrong computation you draw conclusions based on your own experience with previous debugging sessions –A computer on the other hand has no experience to draw on, so it might have to consider all possibilities, evaluating each one using some kind of test this is the basic idea of search

Example: 8 Puzzle Consider the 8 puzzle –One of those toys at carnivals, made of plastic move the numbers using the one opening so that the numbers eventually wind up in order 1-8 –How do you solve it? at any given state (board configuration), you have 2-4 options based on where the space is slide tile up, down, left, right remember each state you have already visited so that you don’t get caught in an infinite loop This tree represents the “search space” of one move from the starting point how large is this tree?

8 Puzzle Search Space We consider all possible “board” configurations of the 8 puzzle to be the search space –From any one board, you can manipulate the board into a few new positions –Each board configuration is called a state We start in a starting state and our goal is to reach a goal state All of the states can be illustrated in a tree or graph form Notice the cycle, we want to avoid this

8 Puzzle Algorithm Let’s store the 8 puzzle as a list storing the number of each tile in the given position, for instance our starting point from the last slide would be ( ) –0 denotes the blank Recursive function given the board: –Compare board to goal, are we done yet? If so, return that a solution has been found –Otherwise, add this board configuration to the list of tried states –Generate the possible moves from this position (this depends on where the blank is) For each possible move, see if the board achieved by taking that move is already in our tried states list, if so, skip it Otherwise, recursively call this function passing it the new board

Eight Puzzle Solution (defun eightpuzzle (board) (declare (special *tried* *goal*)) (when (equal board *goal*) (print board) (return-from eightpuzzle t)) (if (memberlis board *tried*) (return-from eightpuzzle nil)) (setf *tried* (append *tried* (list board))) (let* ((blank (position 0 board)) (moves (generate-boards blank board)) temp) (do ((i 0 (+ i 1))) ((or temp (= i (length moves)))) (setf temp (eightpuzzle (nth i moves)))) (if temp (print board)) temp)) (defun memberlis (a lis) (dolist (i lis) (if (equal i a) (return-from memberlis t))) nil)

Continued (defun generate-boards (blank board) (let (temp1 temp2) (setf temp1 (generate-moves blank)) (dolist (i temp1) (setf temp2 (append temp2 (list (swapem board i blank))))) temp2)) (defun generate-moves (blank) (cond ((= blank 0) '(1 3)) ((= blank 1) '(0 2 4)) ((= blank 2) '(1 5)) ((= blank 3) '(0 4 6)) ((= blank 4) '( )) ((= blank 5) '(2 4 8)) ((= blank 6) '(3 7)) ((= blank 7) '(4 6 8)) ((= blank 8) '(5 7)))) (defun swapem (b i j) (let (temp (value (nth i b))) (dotimes (a 9) (if (= a i) (setf temp (append temp '(0))) (if (= a j) (setf temp (append temp (list value))) (setf temp (append temp (list (nth a b))))))) temp))

How Good is Our Algorithm? We have two significant problems with our algorithm –First, because of recursion, we will push a lot of states on the stack before we might hit on a solution how many? how deep is our tree? for the 8 puzzle, it could be as deep as 8! does our stack space have enough room? –Second, our *tried* list grows with each new state visited in the 8 puzzle, it can be as long as 8! near the end, which means that memberlis has a lot of work to do! –It is common to run out of stack space when trying to run such programs as the 8 puzzle

Improvement: Heuristic Search Lets assume that we have another function, h, which can evaluate how likely a given move will lead us quickly to the solution –We slightly alter our previous algorithm as follows: Let temp = F(S) Remove all items in temp that are already in T Sort temp in order using h –that is, (sort temp #’> :key #’h) For each item i in temp, call try(i) –This allows us to try successor states in order of most likely to reach a solution –How much can this improve our search? In the best case, the heuristic tells us the right move every time, so instead of an O(n!) complexity, it is reduced to perhaps as little as O(n) In the worst case, it does nothing for us at all

Good Heuristics? One of the things we study in AI is what makes up a good heuristic –A good heuristic is one that should reduce the complexity of the search by usually telling us the right move –Is there a good heuristic for the 8 puzzle? –Consider two: Local heuristic: add up the number of tiles that are currently in the right position with respect to the goal state and subtract from it the number of tiles that are out of position, select the move that leads us to the state with the highest heuristic value Global heuristic: add up the number of moves that each tile would have to make to get to its proper position in the goal state, select the move that leads us to the state with the lowest heuristic value Some problems may not have heuristics (8 Queens? Knight’s Tour?)

Heuristic Version (defun generate-boards (blank board) (let (temp1 temp2) (setf temp1 (generate-moves blank)) (dolist (i temp1) (setf temp2 (append temp2 (list (swapem board i blank))))) (sort temp2 #'< :key #'heuristic) temp2)) (defun heuristic (board) (declare (special *goal*)) (let ((sum 0) a b) (dotimes (i 9) (setf a (position i board)) (setf b (position i *goal*)) (setf sum (+ sum (compute-distance a b)))) sum)) (defun compute-distance (a b) (cond ((= a b) 0) ((and (= a 0) (or (= b 1) (= b 3))) 1) ((and (= a 0) (or (= b 2) (= b 4) (= b 6))) 2) ((= a 0) 3) ((and (= a 1) (or (= b 0) (= b 2) (= b 5))) 1) ((and (= a 1) (or (= b 3) (= b 5) (= b 7))) 2) ((= a 1) 3) ;; … for a = 2, 3, 4, 5, 6, 7 ((and (= a 8) (or (= b 5) (= b 7))) 1) ((and (= a 8) (or (= b 2) (= b 4) (= b 6))) 2) (t 3)))

General Purpose Search Algorithm Let S = initial state, G be goal state, T be states already reached (initialized to nil) and F be a function that, given S, will generate successor states –try(S) If S = G, then return success Let temp = F(S) Remove all items in temp that are already in T For each item i in temp, call try(i) –Notice that this algorithm will not return T if it never reaches G, so it is implied to return nil The only difficulty is in creating a proper function F –For the 8 puzzle, it was merely a list of new boards reachable from the given state and since there are no more than 8 possible locations for the blank, there are only 8 possible sets of moves, so it was easy to create F

Backtracking What if we get stuck in a deadend when searching? –In doing a maze, if we reach a location where either we cannot continue, or we have tried all of the different branches, we cannot move forward, so instead we backtrack to the most recent intersection and try a different branch To add backtracking to our algorithm, we have to know if our recursive function call ends with success or failure –if success, then return from this recursive call as well –if failure, backtrack and try a new branch from this point forward Basic algorithm: –Backtrack(current) If current is a goal node, return “success” If current is nil (or a deadend state), return “failure” For each child c of current, –value = Backtrack(C) –If value = “success” then return “success” else try the next possible choice from current

Example: 8 Queens We want to place 8 queens on an 8x8 chessboard such that no queen can take any other queen –We will store our solution as a list where list element i will be the row of the queen in column i –We start with nil and extend it –Position a queen in column i at row 1 –If the queen cannot be captured, then extend else continue to try rows 2-8, if we exceed row 8, return failure –By returning failure, a previous recursive call to extend will move its current queen from the current row to the next, and continue Can’t place another queen at this point, so we must backtrack This queen is moved, we wind up continuing to backtrack until we have to move this queen

8 Queens Code (defun queens (lis) (if (not (badboard lis)) (when (= (length lis) 8) (print lis)));; (return-from queens t))) (if (or (badboard lis) (> (length lis) 8)) (return-from queens nil) (progn (let (temp) (setf temp (queens (append lis '(1)))) (if (not temp) (setf temp (queens (append lis '(2))))) (if (not temp) (setf temp (queens (append lis '(3))))) (if (not temp) (setf temp (queens (append lis '(4))))) (if (not temp) (setf temp (queens (append lis '(5))))) (if (not temp) (setf temp (queens (append lis '(6))))) (if (not temp) (setf temp (queens (append lis '(7))))) (if (not temp) (setf temp (queens (append lis '(8))))) temp)))) (defun badboard (lis) (let ((board (butlast lis)) (a (car (last lis)))) (if (member a board) t (progn (let (x (return-value nil) (size (length board))) (dolist (temp board) (setf x (position temp board)) (if (= (/ (abs (- a temp)) (abs (- size x))) 1) (setf return-value t)))) return-value)))))

Knight’s Tour A related problem is the knight’s tour –Can you move a knight on a chess board so that it eventually lands on every square of the chess board without repeating any board node twice? Basic algorithm –Store the chessboard as an NxN array initialized to nil –move(x y num) If is a legal move (still on the board and this particular position has not been tried before) then set board[x][y] = num Recursively try move (x’ y’ num+1) where x’ and y’ is the next available knight move from this position –this requires a loop that tries any of the 8 possible knight moves If the recursive call does not permit the placement of the knight (no legal moves), then reset the position to nil and try the next position Return nil if you were unable to place a knight at (x y num)

Example Early on, there are numerous choices, it doesn’t seem like any choice will lead us astray, but later on, we might find ourselves “boxed in” and we will have to backtrack to a previous decision point in order to continue

Knight’s Tour Code (defun knight (x y num) (declare (special *board* *size*)) (if (done) (return-from knight t)) (if (not (withinbounds x y)) (return-from knight nil)) (if (aref *board* x y) (return-from knight nil)) (setf (aref *board* x y) num) (let (tempx tempy legal) (do ((i 0 (+ i 1))) ((or legal (= i 8))) (setf tempx (+ x (nextx i))) (setf tempy (+ y (nexty i))) (setf legal (knight tempx tempy (+ num 1)))) (if (not legal) (setf (aref *board* x y) nil)) legal)) (defun nextx (i) (case i (0 2) (1 2) (2 1) (3 -1) (4 -2) (5 -2) (6 -1) (7 1))) (defun nexty (i) (case i (0 -1) (1 1) (2 2) (3 2) (4 1) (5 -1) (6 -2) (7 -2))) (defun withinbounds (x y) (declare (special *size*)) (and (< x *size*) (< y *size*) (>= x 0) (>= y 0)))

Continued (defun run (&optional size) (declare (special *board* *size*)) (if size (setf *size* size)) (setf *board* (make-array (list *size* *size*))) (if (knight 0 0 0) (print-board) (format t "~%Failed"))) (defun done () (declare (special *board* *size*)) (dotimes (i *size*) (dotimes (j *size*) (if (null (aref *board* i j)) (return-from done nil)))) t) (defun print-board () (declare (special *board* *size*)) (dotimes (i *size*) (format t "~%") (dotimes (j *size*) (format t "~4D" (aref *board* i j)))))

Water Jugs You have 2 water jugs, one can hold exactly 4 gallons and one can hold exactly 3 gallons –Goal: Fill the 4 gallon jug with exactly 2 gallons of water –Assume: An infinite amount of water is available –Operations: fill a jug to the top dump the contents of a jug out (back into the well or onto the ground) pour the contents of one jug into the other jug until the other jug is full or the current jug is empty Search space will be represented as a list (x y) where x is the current contents in gallons of the 4 gallon jug and y is the current contents of the 3 gallon jug –Initial state: (0 0) –Goal state: (2 0) –We can search either recursively (depth-first) or using a queue (breadth-first) Solution on the website

Water Jugs Code (defun waterjugs (x y) (declare (special *tried*)) (if (memberlis (list x y) *tried*) (return-from waterjugs nil) (setf *tried* (append *tried* (list (list x y))))) (when (and (= x 2) (= y 0)) (print (list x y)) (return-from waterjugs t)) (if (not (legal x y)) (return-from waterjugs nil)) (let (temp) (setf temp (waterjugs 4 y)) (if (not temp) (setf temp (waterjugs x 3))) (if (not temp) (setf temp (waterjugs 0 y))) (if (not temp) (setf temp (waterjugs x 0))) (if (not temp) (if (and (>= (+ x y) 4) (> y 0)) (setf temp (waterjugs 4 (- y (- 4 x))))))

Continued (if (not temp) (if (and (>= (+ x y) 3) (> x 0)) (setf temp (waterjugs (- x (- 3 y)) 3)))) (if (not temp) (if (and ( 0 y)) (setf temp (waterjugs (+ x y) 0)))) (if (not temp) (if (and ( x 0)) (setf temp (waterjugs 0 (+ x y))))) (if temp (print (list x y))) temp)) (defun legal (x y) (and (>= x 0) ( = y 0) (<= y 3))) (defun memberlis (a lis) (dolist (i lis) (if (equal a i) (return-from memberlis t))) nil)

Consider Chess In Chess, your goal state is a checkmate state –However, unlike the 8-puzzle, you can’t just search all states for a checkmate or use a heuristic the opponent will make moves too –We need a different kind of search Make this assumption: your opponent will always select a move that maximizes his board position’s heuristic worth –Therefore, as a player, you want to not only maximize your board position’s worth, but also minimize your opponent’s this is called minimax –Compute the heuristic worth of all board configurations for x moves ahead If x is even, take the minimum value from each subtree If x is odd, take the maximum value from each subtree Even or odd indicates whose turn that particular layer is (odd being your move and even being your opponent’s move)

Example Assume we have looked 1 level further and found the maximum heuristic values, we select only the best move Now, we know that our opponent will make an intelligent move such that he/she limits our options, so we assume that the opponent will look over the maximum heuristic values and select the move that is worst for us (so instead of offering us a move of a possible 10, our opponent will select a move that results in a move worth at most 3) Our best choice is to make the move on the right – if our opponent selects wisely, we can reach a state worth as much as 5

Other Search Strategies Alpha-beta pruning –If we were to look ahead m turns, and at each turn, there are n possible moves to make, then we have to look ahead m^n Consider chess, you want to look ahead 5 turns, and there are 20 possible moves per turn, you have to consider around 5^20 board configurations! –Alpha-beta pruning uses minimum and maximum threshold values to determine if a move should continue to be searched because it has already exceeded the maximum that we want the opponent to achieve or the minimum that we would desire to achieve AND Search –In some cases, we are not searching for a single move, but a collection of moves We may have a tree in which some nodes are OR nodes, you can pursue any one of the subtrees, but if we have AND nodes, then we must pursue all subtrees This is more common in design/planning problems than in game playing