AI Lecture 7-8

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

Artificial Intelligence

(CS-401)
Lecture 4:
Heuristic Informed Search
Algorithms

Dr. Fahad Sherwani (Assistant Professor – FAST NUCES)


PhD in Artificial Intelligence
Universiti Tun Hussein Onn Malaysia
[email protected]
What is meant by Heuristics?

• Derived from a Greek word that means “To


Discover"
• Heuristic describes a rule of thumb or a
method
• Comes from experience
• Helps you think through things
• Like the process of elimination
• The process of trial and error
• You can think of a heuristic as a shortcut

5
Best-first search
Idea: use an evaluation function f(n) for each node
– family of search methods with various evaluation functions
(estimate of "desirability“)
– usually gives an estimate of the distance to the goal
– often referred to as heuristics in this context
→Expand most desirable unexpanded node
→A heuristic function ranks alternatives at each branching step
based on the available information (heuristically) in order to
make a decision about which branch to follow during a search.

Implementation:
Order the nodes in fringe in decreasing order of desirability.
Special cases:
– Greedy best-first search
– A* search 5
Romania with step costs in km

6
Greedy best-first search
• Greedy best-first search expands the node that appears
to be closest to goal heuristically.

• Estimate of cost from n to goal ,e.g., hSLD(n) = straight-line


distance from n to Bucharest.
Utilizes a heuristic function as evaluation function
– f(n) = h(n) = estimated cost from the current node to a
goal.
– Heuristic functions are problem-specific.
– Often employs straight-line distance for route-finding
and similar problems.
– Often better than depth-first, although worst-time
complexities are equal or worse (space).
7
Greedy best-first search example

8
Greedy best-first search example

8
Greedy best-first search example

9
Greedy best-first search example

10
Greedy best-first search

11
Properties of greedy best-first search

Complete: No – can get stuck in loops (e.g., Iasi → Neamt → Iasi →


Neamt → ….)
Time: O(bm), but a good heuristic can give significant improvement
Space: O(bm) -- keeps all nodes in memory

Optimal: No

b branching factor
m maximum depth of the
search tree

12
A* search
Idea: avoid expanding paths that are already expensive.
Evaluation function = path cost + estimated cost
to the goal
f(n) = g(n) + h(n)
-g(n) = cost so far to reach n
-h(n) = estimated cost from n to goal
-f(n) = estimated total cost of path through n to goal

Combines greedy and uniform-cost search to find the


(estimated) cheapest path through the current node
– Heuristics must be admissible
• Never overestimate the cost to reach the goal
– Very good search method, but with complexity problems

13
A* search example Example from [1]

16
A* search example

15
A* search example

16
A* search example

17
A* search example

18
A* search example

19
A* Algorithm

20
Greedy Best-First Exercise

Node Coordinates h(n)


A (5,9) 8.0
B (3,8) 7.3
C (8,8) 7.6
D (5,7) 6.0
E (7,6) 5.4
F (4,5) 4.1
G (6,5) 4.1
H (3,3) 2.8
I (5,3) 2.0
J (7,2) 2.2
K (5,1) 0.0

21
Solution to Greedy Best-First Exercise

Node Coordinates h(n)


A (5,9) 8.0
B (3,8) 7.3
C (8,8) 7.6
D (5,7) 6.0
E (7,6) 5.4
F (4,5) 4.1
G (6,5) 4.1
H (3,3) 2.8
I (5,3) 2.0
J (7,2) 2.2
K (5,1) 0.0

22
A* Exercise

How will A* get from Iasi to Fagaras?

23
A* Exercise

Node Coordinates h(n)

A (5,9) 8.0
B (3,8) 7.3
C (8,8) 7.6
D (5,7) 6.0
E (7,6) 5.4
F (4,5) 4.1
G (6,5) 4.1
H (3,3) 2.8
I (5,3) 2.0
J (7,2) 2.2
K (5,1) 0.0

24
Solution to A* Exercise

Node Coordinates h(n)


A (5,9) 8.0
B (3,8) 7.3
C (8,8) 7.6
D (5,7) 6.0
E (7,6) 5.4
F (4,5) 4.1
G (6,5) 4.1
H (3,3) 2.8
I (5,3) 2.0
J (7,2) 2.2
K (5,1) 0.0

25
Another Exercise
Do 1) A* Search and 2) Greedy Best-Fit Search

Node C g(n) h(n)

A (5,10) 0.0 8.0


B (3,8) 2.8 6.3
C (7,8) 2.8 6.3
D (2,6) 5.0 5.0
E (5,6) 5.6 4.0
F (6,7) 4.2 5.1
G (8,6) 5.0 5.0
H (1,4) 7.2 4.5
I (3,4) 7.2 2.8
J (7,3) 8.1 2.2
K (8,4) 7.0 3.6
L (5,2) 9.6 0.0

28
Admissible Heuristics
A heuristic h(n) is admissible if for every node n, h(n) ≤ h*(n),
where h*(n) is the true cost to reach the goal state from n.

An admissible heuristic never overestimates the cost to reach


the goal, i.e., it is optimistic.

The heuristic function hSLD(n) is admissible because it never


overestimates the actual road distance)

Theorem-1: If h(n) is admissible, A* using TREE-SEARCHis


optimal.

27
Optimality of A* (proof)
Recall that f(n) = g(n) + h(n)
Now, 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.
Start
We want to prove:
f(n) < f(G2)
(then A* will prefer n over G2) n

f(G2) = g(G2) since h(G2) = 0 G G2


g(G2) > g(G) since G2 is suboptimal
f(G) = g(G) since h(G) = 0
Then f(G2) > f(G) from above
h(n) ≤ h*(n) since h is admissible
g(n) + h(n) ≤ g(n) + h*(n)
Then f(n) ≤ f(G)
Thus, A* will never select G2 for expansion

28
Optimality of A* (proof)

Recall that f(n) = g(n) + h(n)


Now, 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.
Start
We want to prove:
f(n) < f(G2)
(then A* will prefer n over G2) n

G G2

In other words:
f(G2) = g(G2) + h(G2) = g(G2) > C*,
since G2 is a goal on a non-optimal path (C* is the optimal cost)
f(n) = g(n) + h(n) ≤ C*, since h is admissible
f(n) ≤ C* < f(G2), so G2 will never be expanded
→ A* will not expand goals on sub-optimal paths

29
Properties of A*

• Complete: Yes
unless there are infinitely many nodes with f ≤ f(G)

• Time: Exponential
because all nodes such that f(n) ≤ C* are expanded!

• Space: Keeps all nodes in memory


fringe is exponentially large

• Optimal: Yes

30
Memory Bounded Heuristic Search

How can we solve the memory problem for A* search?


Idea: Try something like iterative deeping search, but the
cutoff is f-cost (g+h) at each iteration, rather than depth first.

Two types of memory bounded heuristic searches:


➢ Recursive BFS
➢ SMA*

31
Simple Memory Bounded A* (SMA*)

• This is like A*, but when memory is full we delete the worst
node (largest f-value).

• Like RBFS, we remember the best descendent in the branch


we delete.

• If there is a tie (equal f-values) we first delete the oldest


nodes first.

• SMA* finds the optimal reachable solution given the memory


constraint.

• But time can still be exponential.

32
SMA* pseudocode
Based on [2]

function SMA*(problem) returns a solution sequence


inputs: problem, a problem
static: Queue, a queue of nodes ordered by f-cost

Queue  MAKE-QUEUE({MAKE-NODE(INITIAL-STATE[problem])})
loop do
if Queue is empty then return failure
n  deepest least-f-cost node in Queue
if GOAL-TEST(n) then return success
s  NEXT-SUCCESSOR(n)
if s is not a goal and is at maximum depth then
f(s)  
else
f(s)  MAX(f(n),g(s)+h(s))
if all of n’s successors have been generated then
update n’s f-cost and those of its ancestors if necessary
if SUCCESSORS(n) all in memory then remove n from Queue
if memory is full then
delete shallowest, highest-f-cost node in Queue
remove it from its parent’s successor list
insert its parent on Queue if necessary
insert s in Queue
end
33
Simple Memory-bounded A* (SMA*)

SMA* is a shortest path algorithm based on the A* algorithm.

The advantage of SMA* is that it uses a bounded memory, while the A*


algorithm might need exponential memory.

All other characteristics of SMA* are inherited from A*.

How it works:
• Like A*, it expands the best leaf until memory is full.
• Drops the worst leaf node- the one with the highest f-value.
• SMA* then backs up the value of the forgotten node to its parent.

34
Simple Memory-bounded A* (SMA*)
(Example with 3-node memory)
Progress of SMA*. Each node is labeled with its
current f-cost. Values in parentheses show the value of
Search space
the best forgotten descendant.
A
f = g+h  = goal A A
13[15]

A
12 12
A 13
G
0+12=12
13
10 8
B G B
B G
10+5=15 8+5=13 15
18 H

10 10 8 16
15 13

C D H I
20+5=25 16+2=18 A A A
15[15] 15[24] 20[24]
20+0=20 24+0=24
10 10 A 8
8 8 15
E F J K B B
G
15 20[]
24[]
30+5=35 30+0=30 24+0=24 24+5=29
B G
I D
15 24 C 25
24
 20

 is given to nodes that the path up to it uses all available memory.


Can tell when best solution found within memory constraint is optimal or not.
35
36
The Algorithm proceeds as follow
Let MaxNodes = 3
•Initially B and G are added to open list, then hit
max.
•B is larger f value, so discard but save f(B)=15
at parent A
Add H, but f(H)=18. This is not a goal and we
can never go deeper, so set f(H)=infinity and
save at G.
•Generate next child I with f(I)=24, bigger other
child of A. Now we have seen all children of G,
so reset f(G) to 24.
•Regenerate B and child C. This is not a goal so
f(C) is reset to infinity.
•Generate second child D with f(D)=20, backing
up value to ancestors.
•D is a goal node, so search terminates. 37
SMA* Properties [2]

• It works with a heuristic, just as A*


• It is complete if the allowed memory is high enough to store the
shallowest solution.
• It is optimal if the allowed memory is high enough to store the
shallowest optimal solution, otherwise it will return the best solution
that fits in the allowed memory.
• It avoids repeated states as long as the memory bound allows it
• It will use all memory available.
• Enlarging the memory bound of the algorithm will only speed up the
calculation.
• When enough memory is available to contain the entire search tree,
then calculation has an optimal speed

38
Recursive Best First Search (RBFS)

best alternative
over fringe nodes,
which are not children:
do I want to back up?

RBFS changes its mind very often


in practice.

This is because the f=g+h


become more accurate (less
optimistic) as we approach the
goal. Hence, higher level nodes
have smaller f-values and will be
explored first.

Problem? If we have more


memory we cannot make use of it.

Ay idea to improve this?


34
Relaxed Problems

A problem with fewer restrictions on the actions is called a


relaxed problem.

The cost of an optimal solution to a relaxed problem is an


admissible heuristic for the original problem.

If the rules of the 8-puzzle are relaxed so that a tile can


move anywhere, then h1(n) gives the shortest solution.

If the rules are relaxed so that a tile can move to any near
square, then h2(n) gives the shortest solution.

40

You might also like