HEURISTIC
HEURISTIC
George F Luger
2
Heuristic Functions
A heuristic function is a function f(n) that gives an estimation on
the “cost” of getting from node n to the goal state – so that the
node with the least cost among all possible choices can be
selected for expansion first.
f measures the value of the current state (its “goodness”)
f measures the estimated cost of getting to the goal from the current
state:
f(n) = h(n) where h(n) = an estimate of the cost to get
from n to a goal
f measures the estimated cost of getting to the goal state from the
current state and the cost of the existing path to it. Often, in this
case, we decompose f:
f(n) = g(n) + h(n) where g(n) = the cost to get to n (from
initial state) 3
Approach 1: f Measures the Value
of the Current State
N-Queens
Example: the number of queens under attack …
Data mining
Example: the “predictive-ness” (a.k.a. accuracy) of a rule
discovered 4
Approach 2: f Measures the Cost
to the Goal
• 8–Puzzle
h1: 8 The number of misplaced tiles
(squares with number).
h2: The sum of the distances of the tiles
from their goal positions.
h2 = 3+1+2+2+2+3+3+2= 18 5
Approach 3: f measures the total cost
of the solution path (Admissible
Heuristic Functions)
A heuristic function f(n) = g(n) + h(n) is admissible if h(n) never
overestimates the cost to reach the goal.
Admissible heuristics are “optimistic”: “the cost is not that much …”
However, g(n) is the exact cost to reach node n from the initial state.
Therefore, f(n) never over-estimate the true cost to reach the goal
state through node n.
Theorem: A search is optimal if h(n) is admissible.
I.e. The search using h(n) returns an optimal solution.
Given h2(n) > h1(n) for all n, it’s always more efficient to use h2(n).
h2 is more realistic than h1 (more informed), though both are optimistic.
6
Traditional informed
search strategies
Greedy Best first search
“Always chooses the successor node with the best f value”
where f(n) = h(n)
We choose the one that is nearest to the final state among
all possible choices
A* search
Best first search using an “admissible” heuristic function f
that takes into account the current cost g
Always returns the optimal solution path
7
Admissible 30
Heuristics
● E.g., for the 8-puzzle
Philipp Koehn
Admissible 31
h 1(S) =?
● h2(S) =? Artificial Intelligence: Informed 15 February
Search 2024
Philipp Koehn
Admissible 32
h 1(S) =? 6
● h2(S) =? 4+0+3+3+1+0+2+1
Artificial= 14
Intelligence: Informed 15 February
Search 2024
Philipp Koehn
Dominan 33
ce
● If h 2( n) ≥ h1 (n) for all n (both admissible)
→ h2 dominates h1 and is better for search
Philipp Koehn
The successive stages of open and closed that generate this graph are:
• Advantages:
– Use very little memory
– Can often find reasonable solutions in large or
infinite (continuous) state spaces.
State-Space Landscape
•If elevation corresponds with cost, then the
aim is to find the lowest valley – a global
minimum; if elevation corresponds to an
objective function, then the aim is to find
the highest peak – a global maximum.
Hill-Climbing (Greedy Local
Search)
function HILL-CLIMBING( problem) return a state that is a local
maximum
input: problem, a problem
local variables: current, a node.
neighbor, a node.
current MAKE-NODE(INITIAL-STATE[problem])
loop do
neighbor a highest valued successor of current
if VALUE [neighbor] ≤ VALUE[current] then return STATE[current]
current neighbor
• Successor function
– move a single queen to another square in the same
column.
• However…
– Takes only 4 steps on average when it
succeeds
– And 3 on average when it gets stuck
Hill Climbing
Drawbacks
• Local
maxima
• Plateau
s
• Diagonal
ridges 1
7
Escaping Shoulders: Sideways
• If no downhill (uphill)Move
moves, allow sideways
moves in hope that algorithm can escape
– Need to place a limit on the possible number of sideways
moves to avoid infinite loops
• For 8-queens
– Now allow sideways moves with a limit of 100
– Raises percentage of problem instances solved from
14 to 94%
– However….
• 21 steps for every successful solution
• 64 for each failure
1
8
Tabu
• Search
Prevent returning quickly to the same state
• Keep fixed length queue (“tabu list”)
• Add most recent state to queue; drop oldest
• Never make the step that is currently tabu’ed
• Properties:
– As the size of the tabu list grows, hill-climbing will
asymptotically become “non-redundant” (won’t look at the
same state twice)
– In practice, a reasonable sized tabu list (say 100 or so)
improves the performance of hill climbing in many
problems
Escaping Shoulders/local Optima
Enforced Hill Climbing
• Perform breadth first search from a local
optima
– to find the next state with better h function
• Typically,
– prolonged periods of exhaustive search
– bridged by relatively quick periods of hill-
climbing
2
• Middle ground b/w local and systematic 0
Hill-climbing: stochastic
• variations
Stochastic hill-climbing
– Random selection among the uphill moves.
– The selection probability can vary with the steepness of the uphill
move.
2
1
Hill Climbing: stochastic
variations
When the state-space landscape has local
minima, any search that moves only in the
greedy direction cannot be complete
• Analysis
– Say each search has probability p of success
• E.g., for 8-queens, p = 0.14 with no sideways moves
Search
● Implementation:
fringe is a queue sorted in decreasing order of desirability
● Special cases
– greedy search
– A∗ search
Philipp Koehn
Roman 6
ia
Philipp Koehn
Roman 7
ia
Philipp Koehn
Greedy 8
Search
Philipp Koehn
Romania with Step Costs 9
in km
Philipp Koehn
Greedy Search 10
Example
Philipp Koehn
Greedy Search 11
Example
Philipp Koehn
Greedy Search 12
Example
Philipp Koehn
Greedy Search 13
Example
Philipp Koehn
Properties of Greedy 14
Search
● Complete? No, can get stuck in loops, e.g., with Oradea as goal,
Iasi → Neamt → Iasi → Neamt →
Philipp Koehn
15
a* search
Philipp Koehn
A∗ 16
Search
● Idea: avoid expanding paths that are already expensive
Philipp Koehn
A∗ Search 17
Example
Philipp Koehn
A∗ Search 18
Example
Philipp Koehn
A∗ Search 19
Example
Philipp Koehn
A∗ Search 20
Example
Philipp Koehn
A∗ Search 21
Example
Philipp Koehn
A∗ Search 22
Example
Philipp Koehn
A∗ Search 23
Example
Philipp Koehn
A∗ Search 24
Example
Philipp Koehn
A∗ Search 25
Example
Philipp Koehn
A∗ Search 26
Example
Philipp Koehn
A∗ Search 27
Example
Philipp Koehn
Optimality of 28
A∗
● Suppose some suboptimal goal G2 has been generated and is in the queue
since h(G )2 = 0
f (G2) =
>g(G) since G2 is suboptimal
g(G2) ≥ f (n) since h is admissible
Philipp Koehn
Properties of 29
A∗
● Complete? Yes, unless there are infinitely many nodes with f ≤ f (G)
Philipp Koehn