3 Hueristic Search
3 Hueristic Search
3 Hueristic Search
Outline
• Heuristics and Optimal search strategies
– heuristics
– hill-climbing algorithms
– Best-First search
– A*: optimal search using heuristics
– An Algorithm for Heuristic Search
– Implementing “Best-first” Search
– Implementing Heuristic Evaluation Function
– Heuristic Search and Expert Systems
• Admissibility, Monotonicity, and Informedness
– Admissibility Measure
– Monotonicity
– When One Heuristic Is Better: More Informed Heuristics
• Using Heuristics in Games
– The Minimax Procedure on Exhaustively Searchable Graphs
– Minimaxing to Fixed Ply Depth
– The Alpha-Beta Procedure
1
Problem: finding a Minimum Cost Path
• Previously we wanted an arbitrary path to a goal.
• Now, we want the minimum cost path to a goal G
– Cost of a path = sum of individual transitions along path
• Examples of path-cost:
– Navigation
• path-cost = distance to node in miles
– minimum => minimum time, least fuel
– VLSI Design
• path-cost = length of wires between chips
– minimum => least clock/signal delay
– 8-Puzzle
• path-cost = number of pieces moved
– minimum => least time to solve the puzzle
2
Informed Search
• A search strategy that uses problem-
specific knowledge beyond the
definition of the problem itself.
• The purpose of a heuristic is to guide the
search process in the most profitable
direction by suggesting which path to
follow first when more than one are
available.
3
Informed Search
• A search strategy which searches the most
promising branches of the state-space first
can:
– find a solution more quickly,
– find solutions even when there is limited time
available,
– often find a better solution, since more
profitable parts of the state-space can be
examined, while ignoring the unprofitable parts.
4
Heuristic Search
• To use heuristic search we need an evaluation/
heuristic function that provides an estimate of
how close a given state is to a goal state.
• There are 2 major ways in which domain specific,
heuristic knowledge can be incorporated into a
search procedure:
– In the rules themselves
– As a heuristic function that evaluates individual
problem states and determines how desirable
they are.
5
State Space Graph Search
OL is a queue OL is stack
(BFS) (DFS) OL is a Priority Queue
accessed by using
functions f= g+h
(Best First Search)
Heuristic Search
• In order to implement any graph search
procedure we will need to use two lists of nodes:
– OPEN: nodes that have been generated and had
heuristic function applied to them but which have not
yet been examined.
• OPEN is a actually a priority queue in which the elements
with the highest priority are those with the most promising
value of heuristic function.
6
Greedy best-first search
Evaluation function f(n) = h(n) (heuristic)
= estimate of cost from n to goal
7
Route finding problem
8
Progress
of Greedy
Best First
Search
using SLD
9
Source: Luger book
Evaluation function f*
f* (n)= g* (n) + h* (n)
• g* (n) is the cost of the shortest path from the start
node to the current node n.
• h* (n) is the actual cost of the shortest path from node
‘n’ to a goal state.
• f* (n) is the actual cost of optimal path from the initial
state to a goal state that passes through current node n.
• Note: Although oracles such as f* do not exist for
most real problems, we would like the evaluation
function ‘f’ to be a close estimate of ‘f*’.
10
Evaluation function f
f (n)= g (n)+ h (n)
• g(n) is measure of the cost of current path to state n, is
a reasonable estimate of g*, but they may not be
equal: g(n) ≥ g* (n).
– These are equal only if the graph search has discovered the
optimal path to state n.
Behavior of A* Search
11
Underestimation
i.e. h≤h*
Overestimation h≥h*
12
Combining cost-so-far and heuristic function
• We can combine the strengths of uniform-cost search and
greedy search.
• Since what we're really looking for is the optimal path
between the initial state, and some goal state, a better
measure of how promising a state is, is the sum of the
cost-so-far, and our best estimate of the cost from there to
the nearest goal state.
Role of g and h’
• If we want to find the cheapest path and some operators cost
more than others, then we set the cost of going from one
node to another to reflect those costs.
• If we only care about getting to a solution somehow, we can
define g always to be zero.
• If the value of h’ is always 0, the search will be controlled by
g same as uniform cost search, BFS if g=1.
• If the value of both g and h is 0, the search strategy will be
random.
• If we want to find path involving the fewest no. of steps, then
– we set the cost of going from a node to its successor as a constant
usually 1 (h=0);
– in this case search will be breadth first search as all nodes on one
level will have lower g values, and thus lower f’ values than all nodes
on the next level.
13
A* search
Idea: avoid expanding paths that are already
expensive
Uniform-cost search
• One simple way to sort the states is by the cost-so-far. This
might be the number of moves we've made so far in a game, or
the distance we've travelled so far looking for a route between
towns.
• If we sort the OPEN list nodes so that the states with the lowest
costs come first, then we'll always expand these first, and that
means that we're sure we'll always find an optimal solution first.
• This is uniform-cost search. It looks a lot like breadth-first
search, except that it will find an optimal solution even if the
steps between states have different costs (e.g. the distance
between towns is irregular).
• However, uniform-cost search doesn't really direct us towards
the goal we're looking for, so it isn't very informed.
14
Defining A* Algorithm
1. If Best First Search algorithm is used with evaluation
function h(n), s.t. h(n) ≤ h*(n), where h*(n) is the true cost
to reach the goal state from n then the resulting algorithm
is called A* algorithm.
A* Search Definitions
• Optimality Definition
A solution is optimal if it conforms to the
minimal-cost path between S0 and SG. If
operators cost is uniform, then the optimal
solution = shortest path
• Admissibility Definition
A heuristic is admissible with respect to a
search method if it guarantees finding the
optimal solution first, even when its value
is only an estimate.
15
Admissibility of A*
• Admissibility theorem:
– A* using Tree-search is optimal if h(n) is
admissible.
Admissibility of A*
• A search algorithm is admissible, if for any graph, it
always terminates in an optimal path from initial state
to goal state, if path exists.
16
Monotonicity
• Theorem I: Any consistent/monotonic heuristic is always
admissible.
17
Admissibility, Informedness and
Monotonicity
• Heuristics that find the shortest path to a goal whenever
it exists are said to be admissible (role of g).
Admissible, Consistent/Monotonic
Heuristics
• Admissible:
if we define h*(n) as the exact lowest cost from node n to a goal, a
heuristic function h(n) is admissible if and only if n
h(n) h*(n)
• Consistent:
this quality is similar to the triangle inequality of all metrics. If c(n,m) is
the cost of a shortest path from node n to successor node m , then a
heuristic function h(x) is consistent if n,m
18
Informedness
• We say that a search strategy which searches less of the state-
space in order to find a goal state is more informed.
• Ideally, we'd like a search strategy which is both admissible (so
it will find us an optimal path to the goal state), and informed
(so it will find the optimal path quickly.)
• Admissibility requires that the heuristic evaluation function,
h(n) doesn't overestimate, but we do want a function which is
as close as possible to the actual cost of getting to the goal.
• Formally, for two admissible heuristics h1 and h2,
if h1(n) <= h2(n) for all states n in the state-space, then
heuristic h2 is said to be more informed than h1.
– then h2 dominates h1
i.e. h2 is better for search as it will search state
space graph by exploring lesser number of
nodes, and without much deviation, quickly
reaching goal.
19
8-puzzle: heuristics
• In fact, it can easily be shown that h2 is both
admissible and more informed than h1.
– It cannot overestimate, since the number of moves we need
to make to get to the goal state must be at least the sum of
the distances of the tiles from their goal positions.
20
The Jug Puzzle: There are two jugs, the first with 3l content,
the second with 4 l content. There is an unlimited amount of
water. How can you get exactly 2 l of water in the second one?
(0,0)
(3,0) (0,4)
(3,0) (0,4) (3,3) (3,0) (0,0) (0,4) (3,4) (3,0) (0,1) (0,4)
21
Specifications
–Try different heuristics and
compare with baseline case,
i.e., the breadth first search.
boat River
boat
L
Missionaries Cannibals
Missionaries Cannibals
Constraints
The boat can carry at most 2 people
22
Missionaries and Cannibals Problem:
heuristics
Start state: <3, 3, L>
Goal state: <0, 0, R>
– h1(n) = (no. of m + no. of c) / 2, on the left side
Best
First
Search
-
pseudo
algorithm
23
A* search complexity
• how many nodes A* generates in the
process of finding a solution.
Time Complexity of A*
• The running time of A* is proportional to the number of
nodes generated or expanded.
– Therefore the branching factor is at most a constant,
and heuristic evaluation of a node can be done in
constant time.
24
Open and closed list
• The Open and Closed lists can be maintained in
constant time per node expansion.
– Closed list - can be organized as hash table,
since we only need to check for the occurrence of a
node.
– Open list - we have to be able to insert a node and
retrieve a lowest-cost node in constant time. This
would take time that is logarithmic in the size of the
Open list.
Special Cases
• Worst case: Cost function f(n) = g(n)
– the heuristic function returns zero for every node and provides
no information to the algorithm, but is still a lower-bound on actual
cost.This is identical to the UCS or Dijkstra’s algorithm, which has a
worst-case time complexity of
O (b C */ )
• Best case: Cost function f(n) = g(n) + h*(n)
– the heuristic function is perfect and always returns the exact
optimal cost to a goal state from any given state, The optimal path
will be chosen, and the number of node-expansion cycles will be d
the depth of the optimal path. Thus, the asymptotic time complexity
is
O(bd) = O(d)
25
Cost of the search
• the closer h is to the actual cost, the fewer states
considered
– however, the cost of computing h tends to go up as it
improves
the best algorithm is
one that minimizes
cost of solving problem
the total cost of the
computation
solution
cost cost of computing h
closeness of h to
actual cost
Drawback of A* Search
• That A* search is complete, optimal, and optimally
efficient among all such algorithms is rather satisfying.
26
Complexity of A* Search
• In mathematical notation, the condition for sub exponential
growth is that
Complexity of A*
• The use of a good heuristic still provides enormous savings
compared to an uninformed search.
27
IDA*
• The idea is to use increasing limits on path cost.
• The main difference between IDA* and standard iterative
deepening is that the cut off used is the f-cost rather than
depth, at each iteration.
• At each iteration, the cut off value is the smallest f-cost of
any node that exceeded the cut off on the previous
iteration.
• IDA* is practical for many problems with unit step costs
and avoids the substantial overhead associated with
keeping a sorted queue of nodes.
• Unfortunately, it suffers from same difficulties with real
valued costs as does the iterative version of uniform
cost search.
28
Local Search Algorithms
• Local search algorithms operate using a single current
state rather than multiple paths and generally move only
to neighbors of that state.
– E.g. Simulated Annealing, Genetic Algorithm, etc.
29
Hill-Climbing Search
1. Evaluate the initial state. If it is also a goal state, then
return it and quit. Otherwise continue with the initial
state as current state.
2. Loop: (until a solution is found or until there are no
operators left to be applied in the current state)
• Select an operator that has yet not been applied to
the current state and apply it to produce a new state.
• Evaluate the new state
• If it is goal, then return it and quit
• If it is not goal but it is better than the current state, then
make it the current state.
• If it is not better than the current state, then continue in the
loop.
30
8-queens using HC
• Local search algorithms use complete state formulation.
E.g.; for 8-queen puzzle,
– each state has 8 queens on the board one per column
• Successors of a state are:
– all possible states generated by moving a single
queen to another square in the same column;
– so each state has 8X7=56 successors
8-queens using HC
31
Drawback of Hill Climbing
• A major drawback of HC strategies is their
tendency to become stuck at local maxima.
32
hill-climbing
• if you think of the state space as a topographical map (heuristic value =
elevation), then hill-climbing selects the steepest gradient to move up
towards the goal
potential dangers
plateau: successor states have
same values, no way to
choose
foothill: local maximum, can
get stuck on minor peak
ridge: results in a sequence of
local maxima, foothill
where N-step lookahead
might help
33
Hill-climbing variants
• Stochastic HC chooses at random from among uphill
moves, probability of selection can vary with steepness
of uphill move.
This usually converges more slowly than steepest ascent, but in
some state landscapes it finds better solutions.
• First choice HC implements stochastic HC by generating
successors randomly until one is generated that is better
than current state.
This is a good strategy when a state has thousands of
successors.
• Random-restart hill climbing adopts the strategy of try
and try again from randomly generated initial states,
stopping when a goal is found i.e., it runs many HC.
If each HC search has a probability p of success, then expected
no.of restarts required is 1/p.
Hill-climbing variants
simulated annealing
allow moves in the wrong direction on a probabilistic
basis
decrease the probability of a backward move as the
search continues
idea: early in the search, when far from the goal,
heuristic may not be good
heuristic should improve as you get closer to the goal
approach is based on a metallurgical technique
34
Simulated Annealing (S. A.)
• hill climbing suffers from problems in getting stuck at local
minima (or maxima).
• C is local max, G is global max
• E is local min, A is global min
– Search must go wrong way to proceed!
Simulated Annealing
• A alternative to a random-restart hill-climbing when stuck on a
local maximum is to do a ‘reverse walk’ to escape the local
maximum.
35
Idea behind S.A.
• If you heat a solid past melting point and then cool it, the
structural properties of the solid depend on the rate of
cooling.
• If the liquid is cooled slowly enough, large crystals will be
formed.
• However, if the liquid is cooled quickly (quenched) the
crystals will contain imperfections.
• Simulated annealing
– Pick a random action
– If action improves evaluation function then go with it
– If not, choose with probability based on how bad it is
• Can go the ‘wrong’ way
• Effectively rules out really bad moves
Simulated Annealing
• Probability of transition to higher energy state is
given by function:
P = e –∆E/kt
Where ∆E is the positive change in the energy level
T is the temperature
K is Boltzmann constant.
36
Local Beam Search
• Keeping just one node in memory might seem to be an
extreme reaction to problem of memory limitations.
• Local beam search algorithm keeps track of k states rather
than just one.
• It begins with k randomly generated states.
• At each step, all successors of all k states are
generated.
• If any one is a goal, algorithm halts.
• Otherwise, it selects the k best successors from complete
list and repeats.
A variant called stochastic beam search, instead of
choosing best k from pool of candidate successors, it
chooses k successors at random, with probability of
choosing a given successor being increasing function of its
value.
37
Genetic Algorithm
• GA is a variant of stochastic beam search in which
successor states are generated by combining 2
parent states, rather than by modifying a single
state.
• GA begins with a set of k randomly generated states,
called population.
• Successor states are generated by combining 2 parent
states, using genetic operators called crossover and
mutation.
• Each state is rated by evaluation function or fitness
function which signifies how well it solves the problem.
8 Queens using GA
38
Local search in Continuous space
• Most real-world environments are continuous as
opposed to discrete.
Online Search
• offline search algorithms (all algos discussed so far)
compute a complete solution before setting foot in the
real world and then execute the solution.
• In contrast, an online search algorithm interleaves
computation and action i.e. first it takes an action, then it
observes the environment and computes the next action.
39
Searching with non-deterministic
actions
• When environment is either partially observable or non-
deterministic (or both) percepts become useful.
OR graphs
• A directed graph in which each node represents a point
in the problem space.
40
AND-OR graphs
• AND-OR graph (or tree) is useful for representing the
solution of problems that can be solved by decomposing
them into a set of smaller problems, all of which must then
be solved.
• One AND arc may point to any number of successor
nodes, all of which must be solved in order for the arc to
point to a solution.
Goal: Acquire TV Set
Goal: Steal a TV Set Goal: Earn some money Goal: Buy TV Set
AND – OR Graphs
• AND – OR graph is useful for
representing the solution of
problems that can be solved by
A
decomposing them into a set of
9 smaller problems, all of which
must then be solved.
B C D
• This decomposition or reduction
5 3 4 generates arcs that we call AND
arcs.
41
Business Applications of Heuristic
Search
• Route-finding (computer networks, military operations
planning, airline travel planning)
• Touring
• Manufacturing design (e.g., VLSI layout)
• Manufacturing control
• Robot navigation
• Internet searching
Business Applications
• Network management
• Profit optimization
• Resource optimization
• Stock portfolio optimization
42
Summary
• Blind search: Depth-First, Breadth-First, IDS
– Do not use knowledge of problem space to find solution.
• Informed search
• Best-first search: Order agenda based on some measure of how
‘good’ each state is.
• Uniform-cost: Cost of getting to current state from initial state = g(n)
• Greedy search: Estimated cost of reaching goal from current state
– Heuristic evaluation functions, h(n)
• A* search: f(n) = g(n) + h(n)
• Admissibility & Monotonicity: h(n)never overestimates the
actual cost of getting to the goal state and the triangle property.
• Informedness: A search strategy which searches less of the state-
space in order to find a goal state is more informed.
• AO* search
43