AI Heuristic search - poojasuratia.files.wordpress.com · Tree search example 3. Tree search...
Transcript of AI Heuristic search - poojasuratia.files.wordpress.com · Tree search example 3. Tree search...
Dr. Pooja K Chauhan,
IAI- Search methods
Dr. Pooja K Chauhan,
EC Dept,
BITs edu campus.
Tree search algorithms
� Basic idea:
� offline, simulated exploration of state space by generating successors of already-explored states (expanding states
2
Tree search example
3
Tree search example
4
Tree search example
5
Implementation: general tree search
6
Search strategies
� A search strategy is defined by picking the order of node expansion
� Strategies are evaluated along the following dimensions:� completeness: does it always find a solution if one exists?� time complexity: number of nodes generated� space complexity: maximum number of nodes in memory
7
� space complexity: maximum number of nodes in memory� optimality: does it always find a least-cost solution?
� Time and space complexity are measured in terms of � b: maximum number of successors of any node or branching factor.
� d: depth of the shallowest goal node.� m: maximum length of any path in state space (may be ∞)
Uninformed search strategies
� Uninformed (Blind Search) search strategies use only the information available in the problem definition:
� Breadth-first search
8
� Breadth-first search
� Uniform-cost search
�Depth-first search
� Depth-limited search
� Iterative deepening search
Breadth-first search� Expand the root node first,
� Then all the successors of the root node are expanded,
� Then their successors and so on.
� Shallowest unexpanded node is chosen for expansion.
� Implementation:
� fringe is a FIFO queue, i.e., new successors go at end
9
� fringe is a FIFO queue, i.e., new successors go at end
Breadth-first search
� Expand shallowest unexpanded node
� Implementation:� fringe is a FIFO queue, i.e., new successors go at end
10
Breadth-first search� Expand shallowest unexpanded node
� Implementation:� fringe is a FIFO queue, i.e., new successors go at end
11
Breadth-first search� Expand shallowest unexpanded node
� Implementation:� fringe is a FIFO queue, i.e., new successors go at end
12
Properties of breadth-first search
� Complete?Yes (if shallowest goal node d is finite and b is finite)
� Time? 1+b+b2+b3+… +bd = O(bd)�Goal test to nodes when selected for expansion, rather than when generated. (node at d will be
13
rather than when generated. (node at d will be expanded before goal is detected)
� Space? O(bd) (keeps every node in memory)�Optimal?Yes (if cost = 1 per step)
� Space is the bigger problem (more than time).
Uniform-cost search� Expand least-cost unexpanded node� Implementation:� fringe = queue ordered by path cost
� Equivalent to breadth-first if step costs all equal� Complete?Yes, if step cost ≥ εTime? # of nodes with g ≤ cost of optimal solution,
14
� Time? # of nodes with g ≤ cost of optimal solution, O(b(1+(C*/ ε)) where C* is the cost of the optimal solution
� Space? # of nodes with g ≤ cost of optimal solution, O(b(1+(C*/ ε))
�Optimal?Yes – nodes expanded in increasing order of g(n)
Depth-first search
� Expand deepest unexpanded node
� Implementation:
� fringe = LIFO queue, i.e. most recently generated node is chosen for expansion.
15
Depth-first search
16
Depth-first search
17
Depth-first search
18
Depth-first search
19
Depth-first search
20
Depth-first search
21
Depth-first search
22
Depth-first search
23
Depth-first search
24
Depth-first search
25
Depth-first search
26
Properties of depth-first search
� Complete? No: fails in infinite-depth spaces, spaces with loops
�Modify to avoid repeated states along path
� complete in finite spaces
� Time? O(bm): m=maximum depth of any node,
27
� Time? O(bm): m=maximum depth of any node,�Can be greater than state space, if m is much larger than d or tree is unbounded.
� but if solutions are dense, may be much faster than breadth-first
� Space? O(bm), i.e., linear space!
�Optimal? No
Backtracking Search� Variant of depth-first
� Uses less memory.
� Only one successor generated at a time rather than all successors.
� Each partially expanded node remembers which successor to generate next. O(m) memory is needed.
Depth-limited search
� Depth-first search with depth limit l,
i.e., nodes at depth l have no successor.
� Incompleteness if l<d
�Non optimal if l>d.
29
�Non optimal if l>d.
�Time complexity : O(bl)
� Space complexity: O(bl)
�With l=∞, it is a depth-first search.
Iterative deepening search or
Iterative deepening depth-first
search� Used in combination with depth-first tree search to find the best depth limit.
� Gradually increases the limit, until a goal is found.
30
� Combines the benefit of Depth-first and Breadth-First.
� Like Depth-first search: Memory requirement is modest=O(bd).
� Breadth-first: Is complete, if b is finite and optimal.
Iterative deepening search l =0
31
Iterative deepening search l =1
32
Iterative deepening search l =2
33
Iterative deepening search l =3
34
Iterative deepening search� Number of nodes generated in a depth-limited search to depth d with branching factor b:
NDLS = b0 + b1 + b2 + … + bd-2 + bd-1 + bd
� Number of nodes generated in an iterative deepening search to depth d with branching factor b:
N = d b^1 + (d-1)b^2 + … + 3bd-2 +2bd-1 + 1bd
35
NIDS = d b^1 + (d-1)b^2 + … + 3bd-2 +2bd-1 + 1bd
� For b = 10, d = 5,�NBFS = 10 + 100 + 1,000 + 10,000 + 100,000 = 111,110.
�NIDS = 50 + 400 + 3,000 + 20,000 + 100,000 = 123,450.
Properties of iterative deepening
search
� Complete?Yes
� Time? d b1 + (d-1)b2 + … + bd = O(bd)
� Space? O(bd)
�Optimal?Yes, if step cost = 1
36
�Optimal?Yes, if step cost = 1
� Iterative deepening is the preferred uninformed search method when the search space is large and the depth of the solution is not known.
Summary of algorithms
37
Informed (Heuristic) Search Strategies
� Uses Problem specific knowledge beyond the definition of the problem.
� Can find solution more efficiently.
� Best-first search is an instance of the general TREE-SEARCH or GRAPH-SEARCH algorithm.SEARCH or GRAPH-SEARCH algorithm.
� A node EVALUATION selected for expansion based on an evaluation function, f(n).
� The evaluation function is FUNCTION construed as a cost estimate, so the node with the lowest evaluation is expanded first.
� Uninformed search methods have access only to the problem definition. The basic algorithms are as follows:
� Breadth-first search expands the shallowest nodes first; it is complete, optimal for unit step costs, but has exponential space complexity.
� Uniform-cost search expands the node with lowest path cost, g(n), and is optimal for general step costs.g(n), and is optimal for general step costs.
� Depth-first search expands the deepest unexpanded node first. It is neither complete nor optimal, but has linear space complexity. Depth-limited search adds a depth bound.
� Iterative deepening search calls depth-first search with increasing depth limits until a goal is found. It is complete, optimal for unit step costs, has time complexity comparable to breadth-first search, and has linear space complexity.
Heuristic Search Techniques
� Best-first Search
�Hill climbing
�A* Search
� AO* Algorithm, � AO* Algorithm,
� IDA*,
�Recursive Best First Search.
Generated and Test
�� AlgorithmAlgorithm1.1. Generate a (potential goal) state:Generate a (potential goal) state:
–– Particular point in the problem space, orParticular point in the problem space, or–– A path from a start stateA path from a start state
2.2. Test if it is a goal stateTest if it is a goal state2.2. Test if it is a goal stateTest if it is a goal state–– Stop if positiveStop if positive–– go to step 1 otherwisego to step 1 otherwise
�� Systematic or Heuristic?Systematic or Heuristic?�� It depends on “Generate”It depends on “Generate”
4141
INFORMED (HEURISTIC) SEARCH
STRATEGIES
� One that uses problem-specific knowledge beyond the definition of the problem itself.
� can find solutions more efficiently than can an uninformed strategy.
� Best-first search is an instance of the general TREE-SEARCH � Best-first search is an instance of the general TREE-SEARCH or GRAPH-SEARCH algorithm,
� in which a node is selected for expansion based on an evaluation function, f(n).
� The evaluation function is construed as a cost estimate, so the node with the lowest evaluation is expanded first.
HEURISTIC FUNCTION
� The choice of f determines the search strategy.
�Most best-first algorithms include as a component off a heuristic function, denoted h(n):
� h(n) = estimated cost of the cheapest path from thestate at node n to a goal state.state at node n to a goal state.
� Heuristic functions are the most common form inwhich additional knowledge of the problem isimparted to the search algorithm.
� If n is a goal node, then h(n)=0.
Best First SearchBest First Search
�� Example:Example:The EightThe Eight--puzzlepuzzle
�� The task is to transform the initial puzzleThe task is to transform the initial puzzle
22 88 33
11 66 44
77 55
44
�� into the goal stateinto the goal state
11 22 33
88 44
77 66 55
�� by shifting numbers up, down, left or right.by shifting numbers up, down, left or right.
BestBest--First (First (HeuristicHeuristic) Search) Search
22 88 33
11 66 44
77 55
44
22 88 33
11 66 44
77 55
22 88 33
11 44
77 66 55
22 88 33
11 66 44
77 5555 33 55
22 88 33 22 33
11 22 33
88 44
77 66 55
22 88 33
11 44
77 66 55
22 33
11 88 44
77 66 55
22 88 33
11 44
77 66 55
33
to the goalto the goal
44
88 33
22 11 44
77 66 5533
33
22 88 33
77 11 44
66 5544
88 33
22 11 44
77 66 5533 to further unnecessary expansionsto further unnecessary expansions
Greedy bestGreedy bestGreedy bestGreedy best----first searchfirst searchfirst searchfirst search� Greedy best-first search tries to expand the node that is closest to the goal, on the grounds that this is likely to lead to a solution quickly.
� Thus, it evaluates nodes by using just the heuristic function; that is, f(n) = h(n).
� Greedy best-first tree search is also incomplete even in a finite state space, much like depth-first search.space, much like depth-first search.
� The worst-case time and space complexity for the tree version is O(b^m), where m is the maximum depth of the search space.
� With a good heuristic function, however, the complexity can be reduced substantially.
� The amount of the reduction depends on the particular problem and on the quality of the heuristic.
A* search: Minimizing the total A* search: Minimizing the total A* search: Minimizing the total A* search: Minimizing the total
estimated solution costestimated solution costestimated solution costestimated solution cost
� The most widely known form of best-first search is called A∗∗∗∗search.
� It evaluates nodes by combining:
� g(n), path cost from the start node to node n, and
h(n), the estimated cost of the cheapest path from n to the � h(n), the estimated cost of the cheapest path from n to the goal :
� f(n) = g(n) + h(n) .
� f(n) = estimated cost of the cheapest solution through n.
� A∗ search is both complete and optimal.
� The algorithm is identical to UNIFORM-COST-SEARCH
� except that A∗ uses g + h instead of g.
Conditions for optimality:Conditions for optimality:Conditions for optimality:Conditions for optimality:� The first condition we require for optimality is that h(n ) be an admissible heuristic.
� An admissible heuristic is one that never overestimates the cost to reach the goal.
� Because g(n) is the actual cost to reach n along the current path, and f(n)=g(n) + h(n), path, and f(n)=g(n) + h(n),
� f(n) never overestimates the true cost of a solution along the current path through n.
� Admissible heuristics are by nature optimistic because they think the cost of solving
� the problem is less than it actually is.
� An obvious example of an admissible heuristic is the
� straight-line distance hSLD that we used in getting to Bucharest. Straight-line distance is
� admissible because the shortest path between any two points is a straight line, so the straight line cannot be an overestimate.
CONSISTENCY � A second, slightly stronger condition called consistency (or sometimes monotonicity) is required only for applications of A∗ to graph search.
� A heuristic h(n) is consistent if, for every node n and
� every successor n of n generated by any action a,
� the estimated cost of reaching the goal from n is no greater � the estimated cost of reaching the goal from n is no greater than the step cost of getting to n plus the estimated cost of reaching the goal from n:
� h(n) ≤ c(n, a, n’) + h(n’) .
� This is a form of the general triangle inequality, which stipulates that each side of a triangle cannot be longer than the sum of the other two sides.
� the triangle is formed by n, n’,and the goal Gn closest to n.
A properties:� The tree-search version of A∗ is optimal if h(n) is admissible,
� while the graph-search version is optimal if h(n) is consistent.
� Stages in an A∗ search for Bucharest.
� Nodes are labeled with f = g +h.
� The
� h values are the straight-line distances to Bucharest taken from Figure 3.22.
� the sequence of nodes expanded by A∗ using GRAPH-SEARCH is in nondecreasing order of f(n).
� Hence, the first goal node selected for expansion must be an optimal solution because f is the true cost for goal nodes
� (which have h=0) and all later goal nodes will be at least as � (which have h=0) and all later goal nodes will be at least as expensive.
� The fact that f-costs are nondecreasing along any path also means that we can draw contours in the state space, just like the contours in a topographic map.
Properties
� A∗ is optimally efficient for any given consistent heuristic.
� A∗ search is complete.
� The complexity of A∗ often makes it impractical to insist on finding an optimal solution.
� Drawbacks:
∗
� Drawbacks:� Computation time.
� it keeps all generated nodes in memory , A∗ usually runs out of space long before it runs out of time.
MemoryMemoryMemoryMemory----bounded heuristic searchbounded heuristic searchbounded heuristic searchbounded heuristic search
� To reduce memory requirements for A∗,� Adapt the idea of iterative deepening to the heuristic search context,
� resulting in the iterative-deepening A∗∗∗∗ (IDA∗∗∗∗) algorithm.
∗
∗∗∗∗ ∗∗∗∗
algorithm.
� The main difference between IDA∗ and standard iterative deepening is:
� the cutoff used is the f-cost (g+h) rather than the depth; at each iteration,
� the cutoff value is the smallest f-cost of any node that exceeded the cutoff on the previous iteration.
Recursive bestRecursive bestRecursive bestRecursive best----first search (RBFS)first search (RBFS)first search (RBFS)first search (RBFS)� Recursive best-first search (RBFS) is a simple recursive algorithm that attempts to mimic the operation of standard best-first search, but using only linear space.
� Its structure is similar to that of a recursive depth-first search, but rather than continuing indefinitely down the current path,
� it uses the f limit variable to keep track of the f-value of the best alternative path available from any ancestor of the current node.
� If the current node exceeds this limit, the recursion unwinds back to the alternative path.
� As the recursion unwinds, RBFS replaces the f-value of each node along the path
� BACKED-UP VALUE with a backed-up value—the best f-value of its children.
� RBFS is somewhat more efficient than IDA∗, but still suffers from excessive node regeneration.
�RBFS follows the path via RimnicuVilcea, then “changes its mind” and tries Fagaras, and then changes its mind back again.
∗
changes its mind back again.
�Each mind change corresponds to an iteration of IDA∗ and could require many reexpansionsof forgotten nodes to recreate the best path and extend it one more node.
Properties
� Like A∗ tree search, RBFS is an optimal algorithm if the heuristic function h(n) is admissible.
� Its space complexity is linear in the depth of the deepest optimal solution, but
� its time complexity is rather difficult to characterize:
� it depends both on the accuracy of the heuristic function and on
∗
� it depends both on the accuracy of the heuristic function and on how often the best path changes as nodes are expanded.
� IDA∗ and RBFS suffer from using too little memory.
� Between iterations, IDA∗ retains only a single number: the current f-cost limit.
� RBFS retains more information in memory, but it uses only linear space.
Generate-and-Test
Algorithm1. Generate a possible solution.
2. Test to see if this is actually a solution.
65
3. Quit if a solution has been found. Otherwise, return to step 1.
Generate-and-Test
�Acceptable for simple problems.
� Inefficient for problems with large space.
66
Generate-and-Test
� Exhaustive generate-and-test.
�Heuristic generate-and-test: not consider paths that seem unlikely to lead to a solution.
� Plan generate-test:
67
� Plan generate-test:− Create a list of candidates.
−Apply generate-and-test to that list.
Generate-and-Test
Example: coloured blocks“Arrange four 6-sided cubes in a row, with each side of each cube painted one of four colours, such that on all four sides of the row one block face of each colour is showing.”
68
sides of the row one block face of each colour is showing.”
Generate-and-Test
Example: coloured blocks
Heuristic: if there are more red faces than other colours then, when placing a block with several red faces, use few
69
then, when placing a block with several red faces, use few of them as possible as outside faces.
Hill Climbing
� Searching for a goal state = Climbing to the top of a hill
70
Hill ClimbingHill Climbing
�� SimpleSimple HillHill ClimbingClimbing��expandexpand thethe currentcurrent nodenode��evaluateevaluate itsits childrenchildren oneone byby oneone (using(using thetheheuristicheuristic evaluationevaluation function)function)
��choosechoose thethe FIRSTFIRST nodenode withwith aa betterbetter valuevalue
�� SteepestSteepest AscendAscend HillHill ClimbingClimbing��expandexpand thethe currentcurrent nodenode��EvaluateEvaluate allall itsits childrenchildren (by(by thethe heuristicheuristic evaluationevaluationfunction)function)
��choosechoose thethe BESTBEST nodenode withwith thethe bestbest valuevalue7171
Hill Climbing
�Generate-and-test + direction to move.
�Heuristic function to estimate how close a given state is to a goal state.
72
Simple Hill Climbing
Algorithm1. Evaluate the initial state.
2. Loop until a solution is found or there are no new operators left to be applied:
73
left to be applied:− Select and apply a new operator
− Evaluate the new state:
goal → quit
better than current state → new current state
Simple Hill Climbing� Evaluation function as a way to inject task-specific knowledgeinto the control process.
74
Simple Hill Climbing
Example: coloured blocks
Heuristic function: the sum of the number of different colours on each of the four sides (solution = 16).
75
colours on each of the four sides (solution = 16).
Hill-climbing example
a) b)
a) shows a state of h=17 and the h-value for each possible successor.
b) A local minimum in the 8-queens state space (h=1).7676
Hill-climbing variations� Stochastic hill-climbing
� Random selection among the uphill moves.
� The selection probability can vary with the steepness of the uphill move.
� First-choice hill-climbing
� Stochastic hill climbing by generating successors randomly � Stochastic hill climbing by generating successors randomly until a better one is found.
� Random-restart hill-climbing
�� A series of Hill Climbing searches from randomly generated A series of Hill Climbing searches from randomly generated initial statesinitial states
�� SimulatedSimulatedAnnealingAnnealing
� Escape local maxima by allowing some "bad" moves butgradually decrease their frequency 7777
Steepest-Ascent Hill Climbing (Gradient
Search)
�Considers all the moves from the current state.
� Selects the best one as the next state.
78
Steepest-Ascent Hill Climbing (Gradient
Search)
Algorithm1. Evaluate the initial state.
2. Loop until a solution is found or a complete iteration produces no change to current state:
79
produces no change to current state:− SUCC = a state such that any possible successor of the current state will be better than SUCC (the worst state).
− For each operator that applies to the current state, evaluate the new state:
goal → quit
better than SUCC → set SUCC to this state
− SUCC is better than the current state → set the current state to SUCC.
Hill Climbing: DisadvantagesLocal maximumA state that is better than all of its neighbours, but not better than some other states far away.
80
Hill Climbing: DisadvantagesPlateauA flat area of the search space in which all neighbouring states have the same value.
81
Hill Climbing: DisadvantagesRidgeThe orientation of the high region, compared to the set of available moves, makes it impossible to climb up. However, two moves executed serially may increase the height.
82
the height.
Hill Climbing: Disadvantages
Ways Out
� Backtrack to some earlier node and try going in a different direction.
�Make a big jump to try to get in a new section.
83
�Make a big jump to try to get in a new section.
�Moving in several directions at once.
Hill Climbing: Disadvantages�Hill climbing is a local method: Decides what to do next by looking only at the “immediate” consequences of its choices.
�Global information might be encoded in heuristic functions.
84
Hill Climbing: Disadvantages
CD
BC
Start GoalA D
85
BC
AB
Blocks World
Hill Climbing: Disadvantages
CD
BC
Start GoalA D0 4
86
BC
AB
Blocks World
Local heuristic: +1 for each block that is resting on the thing it is supposed to be resting on. −1 for each block that is resting on a wrong thing.
Hill Climbing: Disadvantages
CD
CDA0 2
87
BC
BC
A
Hill Climbing: Disadvantages
BCD
A
2
88
BCDA
BC D
A BC
DA
00
0
B A
Hill Climbing: Disadvantages
CD
BC
Start GoalA D−6 6
89
BC
AB
Blocks World
Global heuristic: For each block that has the correct support structure: +1 to
every block in the support structure. For each block that has a wrong support structure: −1 to
every block in the support structure.
Hill Climbing: Disadvantages
BCD
A
−3
90
BCDA
BC D
A BC
DA
−6−2
−1
B A
Hill Climbing: Conclusion�Can be very inefficient in a large, rough problem space.
�Global heuristic may have to pay for computational complexity.
�
91
�Often useful when combined with other methods, getting it started right in the right general neighbourhood.
Problem Reduction
Goal: Acquire TV set
Goal: Steal TV set Goal: Earn some money Goal: Buy TV set
92
AND-OR Graphs
Goal: Steal TV set Goal: Earn some money Goal: Buy TV set
Algorithm AO* (Martelli & Montanari 1973, Nilsson 1980)
Problem Reduction: AO*
ADCB
43 5
A5
69
93
FE44
ADCB
4310
99
FE44
ADCB
46 10
1112
HG75
Problem Reduction: AO*
A
CB 10
11
13
ED F
A
CB 15
14
13
ED F
94
G 5
ED 65 F 3
G 10
ED 65 F 3
H 9Necessary backward propagation
Constraint Satisfaction�Many AI problems can be viewed as problems of constraint satisfaction.
Cryptarithmetic puzzle:
95
SENDMORE
MONEY +
Constraint Satisfaction�As compared with a straightforard search procedure, viewing a problem as one of constraint satisfaction can reduce substantially the amount of search.
96
Constraint Satisfaction�Operates in a space of constraint sets.
� Initial state contains the original constraints given in the problem.
�
97
�A goal state is any state that has been constrained “enough”.
Constraint SatisfactionTwo-step process:
1. Constraints are discovered and propagated as far as possible.
2. If there is still not a solution, then search begins, adding new constraints.
98
new constraints.
M = 1S = 8 or 9O = 0N = E + 1C2 = 1N + R > 8E ≠ 9
N = 3
E = 2
Initial state:• No two letters have the same value.
• The sum of the digits must be as shown.
SENDMORE
MONEY+
99
N = 3R = 8 or 92 + D = Y or 2 + D = 10 + Y
2 + D = YN + R = 10 + ER = 9S =8
2 + D = 10 + YD = 8 + YD = 8 or 9
Y = 0 Y = 1
C1 = 0 C1 = 1
D = 8 D = 9
Constraint SatisfactionTwo kinds of rules:
1. Rules that define valid constraint propagation.
2. Rules that suggest guesses when necessary.
100
ENDEND