Module 3
Module 3
2
Using problem specific
knowledge to aid searching
With knowledge, one can search the state space as if he was
given “hints” when exploring a maze.
Heuristic information in search = Hints
Leads to dramatic speed up in efficiency.
B C D E
Search
F G H I J
only in
this K L M N
subtree!!
O 3
More formally, why heuristic
functions work?
In any search problem where there are at most b choices
at each node and a depth of d at the goal node, a naive
search algorithm would have to, in the worst case, search
around O(bd) nodes before finding a solution
(Exponential Time Complexity).
4
Informed Search
Strategies
6
Informed Search
Strategies
Greedy Search
eval-fn: f(n) = h(n)
8
9
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
10
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
11
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
12
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
13
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
14
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
15
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
16
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
17
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
18
Greedy Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
19
Greedy Search: Tree
Search
Start
A
20
Greedy Search: Tree
Search
Start
A 75
118
[253] E
21
Greedy Search: Tree
Search
Start
A 75
118
[253] E
80 99
[193] [178]
G F
[366] A
22
Greedy Search: Tree
Search
Start
A 75
118
[253] E
80 99
[193] [178]
G F
[366] A
211
[253] E I [0]
Goal
23
Greedy Search: Tree
Search
Start
A 75
118
[253] E
80 99
[193] [178]
G F
[366] A
211
[253] E I [0]
Goal
Path cost(A-E-F-I) = 253 + 178 + 0 = 431
24
dist(A-E-F-I) = 140 + 99 + 211 = 450
Greedy Search: Optimal ?
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
25
Greedy Search:
Complete ?
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E ** C 250
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = h (n) = straight-line distance
heuristic
26
Greedy Search: Tree
Search
Start
A
27
Greedy Search: Tree
Search
Start
A 75
118
[253] E
28
Greedy Search: Tree
Search
Start
A 75
118
[253] E
111
[244] D
29
Greedy Search: Tree
Search
Start
A 75
118
[253] E
111
[244] D
Infinite Branch !
[250] C
30
Greedy Search: Tree
Search
Start
A 75
118
[253] E
111
[244] D
Infinite Branch !
[250] C
[244] D
31
Greedy Search: Tree
Search
Start
A 75
118
[253] E
111
[244] D
Infinite Branch !
[250] C
[244] D
32
Greedy Search: Time and
Space Complexity ?
Start
A
118 75 • Greedy search is not
C 140 B optimal.
111
E
• Greedy search is
D 80 99
incomplete without
G F systematic checking of
97 repeated states.
H 211
• In the worst case, the
101
I Time and Space Complexity
Goal of Greedy Search are both
33
O(b )
m
Informed Search
Strategies
A* Search
eval-fn: f(n)=g(n)+h(n)
A* (A Star)
Greedy Search minimizes a heuristic h(n) which is an
estimated cost from a node n to the goal state. Greedy
Search is efficient but it is not optimal nor complete.
35
A* (A Star)
A* uses a heuristic function which
combines g(n) and h(n): f(n) = g(n) + h(n)
g(n)
f(n) = g(n) n
+h(n)
h(n)
37
A* Search
Start State Heuristic:
A 75 h(n)
118
A 366
140 B
C B 374
111
E C 329
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = g(n) + h (n)
g(n): is the exact cost to reach node n from the initial 38
A* Search: Tree Search
A Start
39
A* Search: Tree Search
A Start
118 75
140
40
A* Search: Tree Search
A Start
118 75
140
[413 G F [417
] ]
41
A* Search: Tree Search
A Start
118 75
140
[413 G F [417
] ]
97
[415 H
]
42
A* Search: Tree Search
A Start
118 75
140
[413 G F [417
] ]
97
[415 H
]
101
Goal I [418]
43
A* Search: Tree Search
A Start
118 75
140
[413 G F [417
] ]
97
[415 H I [450
] ]
101
Goal I [418]
44
A* Search: Tree Search
A Start
118 75
140
[413 G F [417
] ]
97
[415 H I [450
] ]
101
Goal I [418]
45
A* Search: Tree Search
A Start
118 75
140
[413 G F [417
] ]
97
[415 H I [450
] ]
101
Goal I [418]
46
A* with f() not Admissible
49
A* Search: Tree Search
A Start
118 75
140
50
A* Search: Tree Search
A Start
118 75
140
[413 G F [417
] ]
51
A* Search: Tree Search
A Start
118 75
140
[413 G F [417
] ]
97
[455 H
]
52
A* Search: Tree Search
A Start
118 75
140
[413 G F [417
] ]
97
[455 H Goal I [450
] ]
53
A* Search: Tree Search
A Start
118 75
140
54
A* Search: Tree Search
A Start
118 75
140
55
A* Search: Tree Search
A Start
118 75
140
56
A* Search: Tree Search
A Start
118 75
140
Use f(N) = g(N) + h(N) with
admissible and consistent h
N’ h(N)
h(N) c(N,N’) + h(N’)
h(N’)
(triangular inequality)
A consistent heuristic is admissible.
63
Recursive breadth first
Search (RBSF)
F(n)=g(n)+h(n)
Requires linear space
Uses f-limit variable to keep track of f-value to
keep track of best alternative path.
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) 67
When to Use Search
Techniques
The search space is small, and
There are no other available techniques,
or
It is not worth the effort to develop a
more efficient technique
Environment
Square adjacent to the Wumpus are
smelly
Squares adjacent to the pit are breezy
Glitter iff gold is in the same square
Shooting kills Wumpus if you are
facing it
Shooting uses up the only arrow
Grabbing picks up the gold if in the
same square
Releasing drops the gold in the same
square
Actuators
Left turn, right turn, forward, grab,
release, shoot
Sensors
Breeze, glitter, and smell
83
Wumpus World
Characterization of Wumpus World
Observable
partial, only local perception
Deterministic
Yes, outcomes are specified
Static
Yes, Wumpus and pits do not move
Discrete
Yes
Single Agent
Yes
Wumpus World
85
Wumpus World
Wumpus World
86
Logic
Knowledge bases Example:
consist of sentences x + 2 >= y is a sentence
in a formal language
Syntax x2 + y > is not a sentence
Sentences are well
formed x + 2 >= y is true iff x + 2
Semantics is no less than y
The “meaning” of the
sentence x + 2 >= y is true in a
The truth of each world where x = 7, y=1
sentence with
respect to each x + 2 >= y is false in
possible world
world where x = 0, y =6
(model)
Example:
x + y = 4 entails 4 = x + y
AI: Chapter 7: Logical Agents 89
Logic
90
Propositional logic: Syntax
Propositional logic is the simplest logic – illustrates basic
ideas
Atomic sentences = single proposition symbols
E.g., P, Q, R
Special cases: True = always true, False = always false
Complex sentences:
If S is a sentence, S is a sentence (negation)
If S1 and S2 are sentences, S1 S2 is a sentence (conjunction)
If S1 and S2 are sentences, S1 S2 is a sentence (disjunction)
If S1 and S2 are sentences, S1 S2 is a sentence (implication)
If S1 and S2 are sentences, S1 S2 is a sentence (biconditional)
Wumpus world sentences
Let Pi,j be true if there is a pit in [i, j].
Let Bi,j be true if there is a breeze in [i, j].
start: P1,1
B1,1
B2,1
R1: ¬P1,1 KB consists of
R2: B1,1 (P1,2 P2,1) sentences R1 thru R5
R3: B2,1 (P1,1 P2,2 P3,1)
R4: ¬ B1,1
R1 R2 R3 R4 R5
R5: B2,1
Modus Ponens
Given: S1 S2 and S1, derive S2
And-Elimination
Given: S1 S2, derive S1
Given: S1 S2, derive S2
DeMorgan’s Law
Given: ( A B) derive A B
Given: ( A B) derive A B
From a
Whenever sentences
of the form and
conjunction, any of are given, then
the conjuncts can sentence can be
be inferred inferred
(WumpusAhead WumpusAlive)
Shoot and (WumpusAhead
(WumpusAhead WumpusAlive), Shoot can be
WumpusAlive), WumpusAlive inferred
can be inferred
3. Replace implication (A B) by A B
(B22 ( P21 P23 P12 P32 )) ((P21 P23 P12 P32 ) B22)
4. Distributive Law
(B22 P21 P23 P12 P32 ) (P21 B22) (P23 B22) (P12 B22) (P32
B22)
Avoid loops
Check if new subgoal is already on the goal stack