Download as pdf or txt
Download as pdf or txt
You are on page 1of 67

Artificial Intelligence

Informed (Heuristic) Search Strategies


Uninformed vs Informed Search
Effect of heuristics
Guide search towards the goal instead of all
over the place
Start Goal Start Goal

Informed Uninformed
Content
• Heuristic function
• Inform Search
– Greedy Best-first search
– Beam Search
– Hill-climbing Search (Depth First Search)
– A* search

P. B. Sơn & N. V. Vinh 3/23/2024 4


Heuristics (Kinh nghiệm)
• “Heuristics are criteria, methods or principles for deciding which among several
alternative courses of action promises to be the most effective in order to achieve some
goal.”
• Can make use of heuristics in deciding which is the most “promising” path to take
during search.
• Evaluation function h(u): a measure to evaluate the distance of state u from the goal.
e.g: h(u) = 0 if u is the goal state.
• Evaluation functions (or heuristic functions) are problem specific functions that provide
an estimate of solution cost.

P. B. Sơn & N. V. Vinh 3/23/2024 5


Search heuristics: estimates
of distance-to-goal
• Often, even if we don’t
know the distance to the Start state
goal, we can estimate it.
• This estimate is called a
heuristic.
• A heuristic is useful if:
1. Accurate: ℎ(𝑛) ≈ 𝑑(𝑛),
where ℎ(𝑛) is the heuristic
estimate, and 𝑑(𝑛) is the
true distance to the goal Goal state
2. Cheap: It can be computed
in complexity less than
𝑂 𝑏𝑑
Search Heuristics
A heuristic function is:
 A function that estimates how close a state is to a
goal
 Designed for a particular search problem
 Examples: Manhattan distance, Euclidean distance
for pathing

10

5
11.2
Heuristic function

P. B. Sơn & N. V. Vinh 3/23/2024 8


Heuristic function
The 8-puzzle problem:
• Number of misplaced tiles, or
• Total Manhattan distance (number of squares from desired location of
each tile)

4 3 1 1 2 3

6 5 8 4

8 2 7 7 6 5

P. B. Sơn & N. V. Vinh 3/23/2024 9


Heuristic function
- Number of misplaced tiles : 8
- Total Manhattan distance (number of squares from desired
location of each tile) : 3 + 1 + 2 + 1 + 1 + 1 + 1 + 2 + 2 = 14

4 3 1 1 2 3

6 5 8 4

8 2 7 7 6 5

P. B. Sơn & N. V. Vinh 3/23/2024 10


Heuristic function
• There can be many ways to evaluate
• Evaluation functions may not be optimal
• How the evaluation function is chosen determines a lot
of the results of heuristics search

P. B. Sơn & N. V. Vinh 3/23/2024 11


Heuristics Search
Three phases:
1. Find an appropriate representation describing the states and
algorithms of the problem
2. Build the evaluation function
3. Design a strategy to choose a state to expand at each step
(heuristic search)

P. B. Sơn & N. V. Vinh 3/23/2024 12


Greedy Best-first search

• Greedy Best-first search = Breadth first search + Heuristic


function
• Unlike breadth-first search, Best-first search chooses the
node to expand as the best node determined by the
evaluation function.
• The best node is the one with the smallest evaluation
function value, which can be at the current level or at
levels above.

P. B. Sơn & N. V. Vinh 3/23/2024 13


Greedy Best-first search (Ex.)

P. B. Sơn & N. V. Vinh 3/23/2024 14


Example

P. B. Sơn & N. V. Vinh 3/23/2024 15


Example

P. B. Sơn & N. V. Vinh 3/23/2024 16


Example

P. B. Sơn & N. V. Vinh 3/23/2024 17


Greedy Best-first search
1. Initialize queue L containing only start state
2. Loop do
2.1 If (L == Empty) then
{failed search message; end}
2.2 Remove u state from beginning of the L;
2.3 If (u == Goal state) then
{successful search message; end}
2.4 For (each state v expanding from u) do
{Put v in the list L so that L is sorted in best to worse order of the
evaluation function}

P. B. Sơn & N. V. Vinh 3/23/2024 18


Greedy Best-first search (Ex1)
Find path from A to E • Find E
A (20)
D (10) • L: A - Let A
• L: C, D - Let C
• L: D, B - Let D
C (5) • L: E, B - Let E
E (0) • Found E

B (30)
P. B. Sơn & N. V. Vinh 3/23/2024 19
Greedy (Ex 2)
• Using Greedy Search, find path from S to t

P. B. Sơn & N. V. Vinh 3/23/2024 20


Properties of Greedy best-first search

• Complete? No – can get stuck in loops, e.g., Iasi  Neamt  Iasi 


Neamt 
Complete in finite space with repeated-state checking
• Time? O(bm), m is the maximum depth in search space
• Space? O(bm) -- keeps all nodes in memory
• Optimal? No
A good evaluation function can reduce memory time and
space significantly.

P. B. Sơn & N. V. Vinh 3/23/2024 21


Beam Search
• Similar to best first search
• However, expand only k nodes at the next level, not
the whole expands.
• Advantages: better computational complexity
• Disadvantage: does not search all, so the best nodes
(goal nodes) may not be found.

P. B. Sơn & N. V. Vinh 3/23/2024 22


Example
• Imagine the problem of finding a route on a
road map and that the NET below is the road
map:

4 4
A B C
3
S 5 5
G
4 3
D E F
2 4

23
Beam search (1):
Depth 1) S • Assume a pre-fixed
WIDTH (example :
3 4 2)
A D

• Perform breadth-
Depth 2) first, BUT:
S
S
• Only keep the
A D WIDTH best new
nodes
7 B 8 6 depending on
D A 9 E heuristic
X X at each new level.
Bỏ Bỏ 24
Beam search (2):
Depth 3) S

A D • Optimi-
zation:
B D A E ignore
11 C 12 11 B F 10
_ E X X leafs that
X are not
end ignore goal
nodes
Depth 4) S

A D

B D A E
_C E X X B F
X
1525 A 0.0
_C G
Beam search algorithm:
• Homework
Properties:
 Completeness:
 Beam search: NO
 Speed/Memory:
Beam search:
 QUEUE always has length WIDTH, so memory usage is
constant = WIDTH, time is of the order of WIDTH*m*b or
WIDTH*d*b if no solution is found

26
Hill-climbing Search

• Hill-climbing Search= Depth-first search + Heuristic


function
• Unlike depth-first search, where we expand from node
u, in the next step, we choose from among the
expanded nodes of u, the node with the most promise
to expand.
• The most promising node is the one with the smallest
evaluation function
P. B. Sơn & N. V. Vinh 3/23/2024 27
 Define f(T) = the straight-line distance from T to G

A 10.4 B 6.7 C 4

S 11
8.9 G
D E 6.9 F 3

P. B. Sơn & N. V. Vinh 3/23/2024 28


Hill-climbing Search
 Example: using the straight-line distance:

• Perform depth-
A 10.4 D 8.9 first, BUT:
• instead of left-to-
A 10.4 E 6.9 right selection,
• FIRST select the
6.7 B F 3.0
child with the best
G heuristic value
29
Hill-climbing Algorithm
1. Initialize queue L containing only start state;
2. Loop do
2.1. if (L == Empty) then
failed search message; end};
2.2. Remove u state from beginning of the L;
2.3. if (u == Goal state) then
{successful search message; end};
2.4. for (each state v expanding from u) do put v into L1;
2.5. Sort L1 in ascending order of the evaluation function so that the best state is at the
top of the L1
2.6. Move L1 to the beginning of L so that the beginning of L1 becomes the beginning
of L
P. B. Sơn & N. V. Vinh 3/23/2024 30
Properties of Hill-climbing search
• Completeness:
– Hill climbing: YES (backtracking)
• Speed/Memory:
– same as Depth-first (in worst case)
• Optimal
– No

P. B. Sơn & N. V. Vinh 3/23/2024 31


How worse “Greedy Best First Search” ?

• Best First Search guarantees the optimal path?


• How can we fix the greedy problem?

P. B. Sơn & N. V. Vinh 3/23/2024 32


A* Search

P. B. Sơn & N. V. Vinh 3/23/2024 33


A* Search

UCS Greedy

A*
A* Search
• A* Search uses evaluation function f(n) = g(n) + h(n)
– g(n): cost from initial node to node n
– h(n): estimated cost of cheapest path from n to goal.
– f(n): estimated total cost of cheapest solution through n.
• Best first (Greedy) Search minimises h(n)
– Effective but not optimal
• Uniform-cost Search minimizes g(n)
– Optimal but not effective

P. B. Sơn & N. V. Vinh 3/23/2024 35


A* Search Example

P. B. Sơn & N. V. Vinh 3/23/2024 36


A* Search Example

P. B. Sơn & N. V. Vinh 3/23/2024 37


A* Search Example

P. B. Sơn & N. V. Vinh 3/23/2024 38


A* Search Example

P. B. Sơn & N. V. Vinh 3/23/2024 39


A* Search Example

P. B. Sơn & N. V. Vinh 3/23/2024 40


A* Search Example

P. B. Sơn & N. V. Vinh 3/23/2024 41


A* Algorithm
1. Initialize queue L containing only start state
2. Loop do
2.1 If (L == Empty) then
{failed search message; end}
2.2 Remove u state from beginning of the queue L;;
2.3 If (u == Goal state) then
{successful search message; end}
2.4 For (each state v expands from u) do
{g(v) := g(u) + k(u,v);
f(v) := g(v) + h(v);
Put v into the queue L so that L is sorted in best to worse order of the evaluation
function;}

P. B. Sơn & N. V. Vinh 3/23/2024 42


Another example

Source: Wikipedia
Uniform cost search vs. A* search

Source: Wikipedia
Greedy vs. UCS vs. A*

P. B. Sơn & N. V. Vinh 3/23/2024 45


Is A* Optimal?

P. B. Sơn & N. V. Vinh 3/23/2024 46


A* Search
• A* Search makes f(n) = g(n) + h(n) minimize.
– Idea: preserve efficiency of Greedy Search but
avoid expanding path that are already expensive
• Question: Is A* search optimal and complete?
• Yes! Provided h(n) is admissible- if h(n) does
not exceed the actual value from state n to
the goal state.
• http://theory.stanford.edu/~amitp/GameProgr
amming/AStarComparison.html

P. B. Sơn & N. V. Vinh 3/23/2024 47


Admissible Heuristics
• A heuristic h is admissible (optimistic-lạc
quan) if:
0  h(n)  h*(n)
where h*(n) is the true cost to a nearest goal

• Example:

15

• With admissible heuristics, A* search is


optimal.
.
Optimality of A * (proof)
• Suppose some suboptimal goal G2 has been generated and is in the
fringe. Let n be an unexpanded node in the fringe such that n is on a
shortest path to an optimal goal G.

• f(G2) > f(G) from above


• h(n) ≤ h*(n) since h is admissible
• g(n) + h(n) ≤ g(n) + h*(n)
• f(n) ≤ f(G)

Hence f(G2) > f(n), and A* will never select G2 for expansion

P. B. Sơn & N. V. Vinh 3/23/2024 49


Optimality of A* Search
• Since f(G2) > f(n), A* will never select G2 for expansion.
• The suboptimal goal node G2 may be generated, but it will never be
expanded.
• In other words, even after a goal node has been generated, A* will keep
searching so long as there is a possibility of finding a shorter solution.
• Once a goal node is selected for expansion, we know it must be optimal,
so we can terminate the search.

P. B. Sơn & N. V. Vinh 3/23/2024 50


Consistency of Heuristics
• Main idea: Estimated heuristic
1
costs ≤ actual costs
A – Admissibility:
C heuristic cost ≤ actual cost to goal
h(A) ≤ actual cost from A to G
h=4 – Consistency:
h=2 h=1
“heuristic step cost” ≤ actual cost for each
step
G h(A) – h(C) ≤ cost(A to C)
triangle inequality
h(A) ≤ cost(A to C) + h(C)
Properties of A* search
• Complete? Yes (unless there are
infinitely many nodes with f ≤ f(G) )
• Time? Exponential
• Space? Keeps all nodes in memory
• Optimal? Yes

P. B. Sơn & N. V. Vinh 3/23/2024 52


Designing heuristic functions
Heuristics for 8-puzzle problem:
• h1(n) = Number of misplaced tiles
• h2(n) = Total Manhattan distance (number of squares from desired location of each tile)

• h1(S) = ?
• h2(S) = ?

P. B. Sơn & N. V. Vinh 3/23/2024 53
Designing heuristic functions
Heuristics for 8-puzzle problem :
• h1(n) = Number of misplaced tiles
• h2(n) = Total Manhattan distance (number of squares from desired location of each tile)

• h1(S) = ? 8
• h2(S) = ? 3+1+2+2+2+3+3+2 = 18

P. B. Sơn & N. V. Vinh 3/23/2024 54


Dominance
• If h2(n) ≥ h1(n) for all n (both admissible)
• then h2 dominates h1 (h2 áp đảo h1 )
• h2 is better for search

• Typical search costs (average number of nodes expanded):

• Depth d=12
IDS = 3,644,035 nodes
A*(h1) = 227 nodes
A*(h2) = 73 nodes
• Depth d=24
IDS = too many nodes ~ 54 * 109 nodes
A*(h1) = 39,135 nodes
A*(h2) = 1,641 nodes

P. B. Sơn & N. V. Vinh 3/23/2024 55


Heuristics from relaxed problems

• Reduce constraints
• A problem with less constraints than in actions is called relaxed problem
• The value of the optimal solution for the relaxed problem is the
admissible function of the original problem

• If the rule of the 8-puzzle problem is relaxed so that the pieces can move
freely (anywhere), then h1(n) gives the shortest solution.
• If the rule of the 8-puzzle problem is relaxed so that the pieces can move
any adjacent squares, then h2(n) gives the shortest solution.

P. B. Sơn & N. V. Vinh 3/23/2024 56


Creating Admissible Heuristics
• Most of the work in solving hard search problems optimally is
in coming up with admissible heuristics

• Often, admissible heuristics are solutions to relaxed


problems, where new actions are available

366
15
Composite Heuristic Functions
• Let h1, h2,.., hm be admissible heuristics for a given
task.
• Define the composite heuristic:
– h(n) = max (h1(n), h2(n), …, hm(n)).
• h is admissible heuristic
• h dominate h1, h2, …, hm

P. B. Sơn & N. V. Vinh 3/23/2024 58


Weighted A* search
• Idea: speed up search at the expense of optimality
• Take an admissible heuristic, “inflate” it by a multiple α
> 1, and then perform A* search as usual
• Fewer nodes tend to get expanded, but the resulting
solution may be suboptimal (its cost will be at most α
times the cost of the optimal solution)
Example of weighted A* search

Heuristic: 5 * Euclidean distance from goal


Source: Wikipedia
Example of weighted A* search

Heuristic: 5 * Euclidean distance Compare: Exact A*


from goal
Source: Wikipedia
Improve A* by branch and bound
1. Initialize queue L containing only start state
2. Loop do
2.1 If (L == Empty) then
{failed search message; end}
2.2 Remove u state from beginning of the queue L;
2.3’ If (L includes path P ending at state X at cost_P, and path Q includes X at
cost_Q and cost_P >= cost_Q) then
{ Remove P from L}
2.3 If (u == Goal state) then
{successful search message; end}
2.4 For (each state v expands from u) do
{g(v) := g(u) + k(u,v);
f(v) := g(v) + h(v);
Put v into the queue L so that L is sorted in best to worse order of the evaluation
function;}
P. B. Sơn & N. V. Vinh 3/23/2024 62
A* Applications
Pathing / routing problems
Resource planning problems
Robot motion planning
Language analysis
Video games
Machine translation
Speech recognition

Image: maps.google.com
A*: Summary
A* uses both backward costs and (estimates of) forward costs

A* is optimal with admissible / consistent heuristics

Heuristic design is key: often use relaxed problems


All search strategies. C*=cost of best path.

Space Implement the


Algorithm Complete? Optimal? Time complexity
complexity Frontier as a…

BFS Yes If all step costs are equal O(b^d) O(b^d) Queue

DFS No No O(b^m) O(bm) Stack

IDS Yes If all step costs are equal O(b^d) O(bd) Stack

Number of nodes w/ Number of nodes w/ Priority Queue sorted by


UCS Yes Yes
g(n) ≤ C* g(n) ≤ C* g(n)

Worst case: O(b^m) Worse case: O(b^m) Priority Queue sorted by


Greedy No No
Best case: O(bd) Best case: O(bd) h(n)

Number of nodes w/ Number of nodes w/ Priority Queue sorted by


A* Yes Yes
g(n)+h(n) ≤ C* g(n)+h(n) ≤ C* h(n)+g(n)
Reference
• Trí Tuệ Nhân Tạo, Đinh Mạnh Tường
• Artificial Intelligence, A modern Approach. Chapter 4.

P. B. Sơn & N. V. Vinh 3/23/2024 66


Find shortest path from S-> G by A*
algorithm

P. B. Sơn & N. V. Vinh 3/23/2024 67

You might also like