650 likes | 1.28k Views
Artificial Intelligence in the 21st Century S. Lucci / D. Kopec. Chapter 2: Uninformed Search . Problem Solving by Searching. “Problem solving agents decide what to do by finding sequences of actions that lead to desirable states.” Search is a natural part of people’s lives.
E N D
Artificial Intelligence in the 21st CenturyS. Lucci / D. Kopec • Chapter 2:Uninformed Search
Problem Solving by Searching • “Problem solving agents decide what to do by finding sequences of actions that lead to desirable states.” • Search is a natural part of people’s lives
2.1 State Space Graphs • A mathematical structure that helps to formalize the search process • Possible alternative paths leading to a solution can be explored and analyzed • A solution to a problem will correspond to a path through a state space graph
Remember this Problem? • Three missionaries and three cannibals • Want to cross a river using one canoe. • Canoe can hold up to two people. • Can never be more cannibals than missionaries on either side of the river. • Aim: To get all safely across the river without any missionaries being eaten. • Did you solve it? How????
Single-State Problem Formulation • A problem is defined by four items: • initial state • successor function (which actually defines all reachable states) • goal test • path cost (additive) e.g., sum of distances, number of actions executed, etc. C(x,a,y) is the step cost, assumed to be 0
Problem Representation : Cannibals and Missionaries • Initial State • We can show number of cannibals, missionaries and canoes on each side of the river. • Start state is therefore: • [3,3,1,0,0,0]
Problem Representation : Cannibals and Missionaries • Initial State • However, since the system is closed, we only need to represent one side of the river, as we can deduce the other side. • We will represent the starting side of the river, and omit the ending side. • So start state is: • [3,3,1]
Problem Representation : Cannibals and Missionaries • Successor Function • There are five actions we can represent • Move one cannibal across the river. • Move two cannibals across the river. • Move one missionary across the river. • Move two missionaries across the river. • Move one missionary and one cannibal.
Problem Representation : Cannibals and Missionaries • Successor Function • To be a little more mathematical/computer like, we want to represent this in a true successor function format… S(state) state • Move one cannibal across the river. S([x,y,1]) [x-1,y,0] 0 y , 0 x-1 S([x,y,0]) [x+1,y,1] 0 y, 0 x 2
Problem Representation : Cannibals and Missionaries • Successor Function S([x,y,0]) [x-1,y,1] S([x,y,1]) [x+1,y,0] S([x,y,0]) [x-2,y,1] S([x,y,1]) [x+2,y,0] S([x,y,0]) [x,y-1,1] S([x,y,1]) [x,y+1,0] S([x,y,0]) [x,y-2,1] S([x,y,1]) [x,y+2,0] S([x,y,0]) [x-1,y-1,1] S([x,y,1]) [x+1,y+1,0]
Problem Representation : Cannibals and Missionaries • Successor Function S([x,y,1]) {[x-1,y,0] , [x-2,y,0] , [x,y-1,0] , [x,y-2,0] , [x-1,y-1,0] } S([x,y,0]) {[x+1,y,1] , [x+2,y,1] , [x,y+1,1] , [x,y+2,1] , [x+1,y+1,1] } Where valid states [a,b,c] are those such that : 0 a 3 0 b 3 0 c 1
Problem Representation : Cannibals and Missionaries • Successor Function S([x,y,1]) {[x-1,y,0] , [x-2,y,0] , [x,y-1,0] , [x,y-2,0] , [x-1,y-1,0] } S([x,y,0]) {[x+1,y,1] , [x+2,y,1] , [x,y+1,1] , [x,y+2,1] , [x+1,y+1,1] } Where valid states [a,b,c] are those such that : 0 a 3 0 b 3 0 c 1 • If b>0 then a b • If b<3 and a<3 then 3-a 3-b
Problem Representation : Cannibals and Missionaries • Goal State • [0,0,0,]
Problem Representation : Cannibals and Missionaries • Path Cost • One unit per trip across the river.
Problem Representation : Cannibals and Missionaries • Initial State • [3,3,1] • Successor Function S([x,y,0]) {[x+1,y,1] , [x+2,y,1] , [x,y+1,1] , [x,y+2,1] , [x+1,y+1,1] } S([x,y,1]) {[x-1,y,0] , [x-2,y,0] , [x,y-1,0] , [x,y-2,0] , [x-1,y-1,0] } • Goal State • [0,0,0] • Path Cost • One unit per trip across the river.
The False Coin Problem • 12 coins, 1 counterfeit • Unknown whether the fake is lighter or heavier • Uses a balance scale to determine • If any two sets weigh the same • One set is lighter or heavier than the other • Solve the problem in three weighings
The False Coin Problem / cont. • IF C1 C2 : C5 C6 are equal then we know either C3 or C4 is fake and we branch to the left
The False Coin Problem / cont. • At the left branch we test C1 : C3 . If they are equal then C4 is fake otherwise C3 is fake. • If C1 C2 : C5 C6 are not equal then to branch right and test C1 : C4 . If they are equal then we know C2 is fake otherwise C1 is fake.
Remember • All “trees” are graphs, but not all graphs are trees… • The previous example may make you think that all of our state space problems are trees with a starting state at the “root” and a solution at one or more of the leaves. • This is often the case. • But the reality is that it need not be represented that way.
Single-State Problem Formulation • A solution is a sequence of actions leading from the initial state to a goal state
Problem Formulation • Example: vacuum cleaner world state space graph • States?? Actions?? Goal test?? Path cost??
Problem Formulation • States?? Integer dirt and robot locations (ignore dirt amounts) • Actions?? Left, Right, Suck, NoOp • Goal test?? No dirt • Path cost?? 1 per action (0 for NoOp)
Example: the 8-puzzle • States??Actions?? Goal test?? Path cost??
Example: The 8-puzzle • States?? 9!/2 integer locations of tiles (ignore intermediate positions) • Actions?? Move blank left, right, up, down • Goal test?? = goal state (given) • Path cost?? 1 per move • Note: optimal solution of n-Puzzle family is NP-hard (although 8-Puzzle is NP-complete)
Why do we look at “toy” problems. • Real world is absurdly complex state space must be abstracted for problem solving • (Abstract) state = complex set of real states(Abstract) action = complex combination of real actions e.g., “Arad Zerind” represents a complex set of possible route, detours, rest stops, etc.For guaranteed realizability, any real state “in Arad” must get to some real state “in Zerind” • (Abstract) solution = set of real paths that are solutions in the real worldeach abstract action should be “easier” than the original problem!
A Starter Activity • Cannibals and Missionaries • Three missionaries and three cannibals • Want to cross a river using one canoe. • Canoe can hold up to two people. • Can never be more cannibals than missionaries on either side of the river. • Get all safely across the river without any missionaries being eaten. • Find a solution…
Moving On • So even if we know how to express a problem, how do we actually go about solving them?
Generate and Test Paradigm • Propose possible solutions then test whether each proposal constitutes a solution • Will illustrate with n-Queens problem
n-Queens Problem • n-Queens are to be placed on an n x n board • No two Queens should occupy the same row, column or diagonal • Example with a 4 x 4 board with 4 Queens
Generate and Test • A total of 16C4 or 1820 ways to accomplish this • Figure 2.4 illustrates that many of the proposed solutions violate one or more of the constraints • A reliable generator is: • Complete – that is, must propose every subset of size four that satisfies the problem constraints. • Informed – that is, capable of excluding certain proposals that cannot possibly be successful. • Non-redundant – if a proposed solution is rejected or successful, it should not be proposed again
Pseudocode {While no solution is found and more candidates remain [Generate a possible solution Test if all problem constraints are satisfied] End While} If a solution has been found, announce success and output it. Else announce no solution found.
Exhaustive Enumeration • A search methodology that looks everywhere for a solution to a problem • A partial solution is developed further even after it has been discovered that this set of steps cannot possibly lead to a successful problem solution
Backtracking • An improvement to exhaustive enumeration • A proposed solution is divided into stages • In the 4-Queens problem, placing each queen on the board is a stage • In Stage i, Queens have been successfully placed in columns 1,…, i-1 • If no square remains on which the ith Queen may be placed that does not violate any of the constraints, then we must return to Stage i-1
Backtracking / cont. • Undo the placement of the Queen at Stage i-1, make the next choice for this Queen, and return to Stage i. If it is not possible to successfully place the (i-1)st Queen, then backtracking continues to Stage i-2 • Can use Backtracking with Generate and Test • The Generator will attempt to place a Queen in each column. • The Test module will view a possible solution as it is being developed. • The algorithm contains four stages.
Backtracking / cont. • a) 1st Queen is placed • b) 2nd Queen is placed but it violates one of the constraints so it placed in a different spot
Backtracking / cont. c) the new location for the 2nd Queen d) no more Queens can be placed that will not violate any of the constraints e) Algorithm will backtrack and replace the 2nd Queen in the next figure.
Backtracking /cont. • The algorithm will move back to stage one and place the 1st Queen in column one, row two. • It turns out that the 4-Queens problem has an additional solution.
Backtracking / cont. • To find it, print the solution then backtrack to stage one and place the 1st Queen in row 3.
Backtracking /cont. • These two solutions have a symmetric relationship. • The 2nd solution is found by vertically flipping the board.