Professional Documents
Culture Documents
(BCS-DS-503)
UNIT – 2
Heuristic Techniques
CONTENTS
Heuristic Search Techniques
1. Generate & Test
2. Hill Climbing
Simple Hill Climbing
Steepest Ascent Hill Climbing
3. Best First Search & A*
4. Problem Reduction & AO*
5. Constraint Satisfaction
6. Means – Ends Analysis
Informed Search or Heuristic Search
Heuristic search is also called guided search because in this,
the search is guided in a specific direction.
Heuristics are like tour guides.
Additional information or knowledge about the problems is
given in the form of clues or guidelines, which are called
‘heuristics.’
Additional information or clues restrict the expansion of
only promising nodes in search tree and guides the search
in a specific direction towards the goal.
On the average they improve the quality of the paths that
are explored, besides increasing the efficiency of the
search.
Special purpose heuristics exploit domain specific
knowledge.
Heuristic Function
This is a function that maps from problem state
descriptions to measures of desirability, usually
represented as numbers.
Which aspects of the problem state are considered,
How those aspects are evaluated, and
The weights given to individual aspects are chosen in
such a way that
the value of the heuristic function at a given node in
the search process gives as good an estimate as
possible of whether that node is on the desired path
to a solution.
Well designed heuristic functions can play an
important part in efficiently guiding a search
process toward a solution.
Nearest Neighbour Heuristic
It works by selecting locally superior alternative at
each step.
Applying to TSP:
1. Arbitrarily select a starting city
2. To select the next city, look at all cities not yet visited
and select the one closest to the current city. Go to next
step.
3. Repeat step 2 until all cities have been visited.
– This procedure executes in time proportional to
N2
– It
is possible to prove an upper bound on the error
it incurs. This provides reassurance that one is
not paying too high a price in accuracy for speed.
Example
State space: Configuration of 8- tiles on the board
state Initial: any configuration Goal: tiles in a specific order
■ 2nd approach
Count incorrect position of each tile, compare to goal state
Count how far away (how many tile movements) each tile is from it’s correct
position.
Sum up these count over all the tiles.
h(n) = Add one(+1) point for every block that is resting on the right block.
Subtract one(-1) point for every block that is sitting on wrong block
Example
1We call “initial state” “State 0” and “goal state” ”Goal”.
Then considering the blocks A, B, C, D, E, F, G, H in that
order we have
-1 +1
+1 +1
+1 +1
state0
+1 +1
+1 +1
+1 +1
+1 +1
-1 +1
There is only one move from
State 0, namely, to move block
A to the table. This produces
+1 the State 1 with a score of 6:
+1
+1
+1 state1
+1
+1
-1 +1
Example 1
There are three possible moves form State 1 as shown in
Figure. We denote these three states State 2(a), State 2(b) and
State 2 (c). We also have
Solution
By defi nition, the Manhattan distance heuristic is the sum
of the Manhattan distances of tiles from their goal
positions. In Figure, only the tiles 5, 6 and 8 are misplaced
and their distances from the goal positions are
respectively 2, 2 and 1. Therefore ,h(initial state) = 2 + 2 + 1
Example 2
A-20
A
Expand A and arrange in Asc order of cost
B-15 C-10
C B
Expand C (front)
B-4 C-23
4 3
D-2
1
C(n,m)
E-3
20
F-0
OPEN CLOSED
A(5)
A(5)
Minimum cost
Initial
state
state
in OPEN
of OPEN
is A.and
PutCLOSED.
A in CLOSED.
g(A)=0,
A does
f(A)=h(A)=5.
not belong to G.
OPEN CLOSED
A(5)
B(7) C(25) A(5)
A(5) B(7)
Minimum
Expand cost
A to state
obtaininBOPEN is BB.does
and C. Put B in belong
not CLOSED.to B[OPEN
does not belong
union to G.
CLOSED].
Therefore, set g(B)=g(A) + C(A, B) = 0 + 3 = 3.
And, set f(B) = g(B) + h(B) = 3 + 4 = 7. Place B on OPEN.
Similarly, g(C)=0 + 2 = 2 and f(C)=0+23=25. Place C in OPEN. Return to step 2.
OPEN CLOSED
A(5)
B(7) C(25) A(5)
Minimum
Expandcost
B to
state
obtain
in OPEN
D. D does
is D. Put
not belong
D in CLOSED.
to [OPEN
D does
union
not
CLOSED].
belong to G.
Therefore, set g(D)=g(B) + C(B, D) = 3 + 4 = 7.
And, set f(D) = g(D) + h(D) = 7 + 2 = 9. Place D in OPEN. Return to step 2.
OPEN CLOSED
A(5)
B(7) C(25) A(5)
Minimum
Expandcost
D to
state
obtain
in OPEN
E. E does
is E. not
Put belong
E in CLOSED.
to [OPEN
E does
union
notCLOSED].
belong to G.
Therefore, set g(E)=g(D) + C(D, E) = 7 + 1 = 8.
And, set f(E) = g(E) + h(E) = 8 + 3 = 11. Place E in OPEN. Return to step 2.
OPEN CLOSED
A(5)
B(7) C(25) A(5)
Minimum
Expandcost state
E to in OPEN
obtain is C. not
F. F does Put belong
C in CLOSED. C does
to [OPEN not
union belong to G.
CLOSED].
Therefore, set g(F)=g(E) + C(E, F) = 8 + 20 = 28.
And, set f(F) = g(F) + h(F) = 28 + 0 = 28. Place F in OPEN. Return to step 2.
OPEN CLOSED
A(5)
B(7) C(25) A(5)
A(5)
B(7) C(25) A(5)
A(5)
B(7) C(25) A(5)
F(26)
F(28) A(5) B(7) C(25) D(7) E(9)
Expand cost
Minimum E to obtain
state inF.OPEN
F belongs to [OPEN
is E. Put union CLOSED]
E in CLOSED. as belong
E does not it is in OPEN.
to G.
Therefore, set g(F)=min{g(F), g(E) + C(E, F)} = min{28, 6 + 20} = 26.
Minimum cost state in OPEN is F with the cost 26. F belongs to G. Therefore,
And, set f(F) =
return g(F) + h(F)
f(F)=26. This = is 26
the+minimum
0 = 26. As f(F)
cost ofhas decreased
reaching fromnode
the goal 28 to
F.26, but
F does not belong to CLOSED, it is already in OPEN. Replace it. Return to step 2.
Problem Reduction
State Space Search: Searching technique in
which a single goal state or a set of goal
states have to be found or identified.
Problem Reduction Search: Planning how
best to solve a problem that can be
recursively decomposed into subproblems
in multiple ways
AO* Search
Game Trees
PROBLEM REDUCTION:
EXAMPLE OF MATRIX MULTIPLICATION
Given: A set of matrices A1, A2, ……, An
To find: A1 X A2 X ……X An
Method 1: ((A1XA2)XA3)XA4……
(not an example of problem reduction.)
Method 2: ([(A1A2)(A3A4)][(A5A6)(A7A8)])…
Represents AND
(A1XA2) (A3XA4)
A1A2A3A4
AO = AND-OR
An OR node represents a choice between possible
decompositions
Only one child has to be solved.
Solving is done by choosing the minimum cost
sub-tree.
B h=4 C h=3
B h=4 C h=3
2
1
h=2 D h=5 E
AZ={B}.
is not SOLVED.
B has no Select B, a non-terminal
descendants nodem=B.
in Z, therefore, in the
Dmarked subtree
and E are ORed,and expand it
therefore,
to obtain D and E with ORing betweenD)]
f(B)=min([f(D)+C(B, them. f(D)=h(D)=2
, [f(E)+C(B, E)]) and f(E)=h(E)=5.
Neither D nor E are terminal nodes, therefore they6)=4.
=min([2+2],[5+1])=min(4, are not marked SOLVED.
Thus costCall
of cost-revise(B).
B remains 4. Mark D.
Go to step 2.
h=10
h=14
A
2 1
B h=4 C h=3
h=7
2 0 0
1
h=2 D h=5 E F G
h=9 h=7
Z={C}. C has no descendants in Z, therefore, m=C. F and G are ORed, therefore, f(C)
A isZ={C, A}. A has
not SOLVED. Select C, a=min([f(F)+C(C,
no descendants
non-terminal nodeF)]in
, [f(G)+C(C,
in Z, therefore, m=A.
the B and
marked G)])
C are ANDed,
subtree and expand
therefore, f(A)=[f(B)+C(A, B)] +
=min([9+0],[7+0])=min(9, [f(C)+C(A,
7)=7. C)]=[4+2]+[7+1]=6+8=14.
Thus cost
it to obtain F and G with ORing between them. of C becomes ThusG.
7. Mark
Since C is a marked successor cost
of Aofand
f(F)=h(F)=9 A becomescost 14.
the f(G)=h(G)=7.
and of C has changed, therefore insert A
Both F and G are terminal Gothat
Z, to step
into nodes, 2. Z={A}.
is, now
therefore they are marked SOLVED.
Reiterate Call
through
cost-revise(C). with n=A.
cost-revise
h=14
h=16
A
2 1
B h=4
h=6 C h=7
2 0 0
1
h=14
h=2 D h=5 E F G
1 0 h=9 h=7
H I
h=3 h=10
A Z={B}.
is not SOLVED. Select D, a non-terminal
Reiterate through node m=B.
from with
cost-revise the marked
n=A. subtree and expand it
Z={D}. DBhas hasnono descendants
descendants in
in Z, therefore,
Z, therefore, m=D. H D and
and IEare
areANDed,
ORed, therefore,
therefore,
to obtain
Z={A}. A has no descendants
f(B) Hin
and
Z, I with ANDing
therefore, m=A. between
B and Cthem.
are ANDed, therefore,
f(D) =min([f(D)+C(B,
=[f(H)+C(D, H)] D)]+ ,[f(I)+C(D,
[f(E)+C(B, E)])
I)]
f(A) f(H)=h(H)=3
=[f(B)+C(A, and f(I)=h(I)=10.
B)] + [f(C)+C(A,6)=6.
=min([14+2],[5+1])=min(16, C)]
=[3+1] + [10+0]=4+10=14.
H is acost
Thus non-terminal
of D node,
B becomes
becomes 6.whereas
Unmark I is a terminal
=[6+2]+[7+1]=8+8=16. node, B therefore I is marked SOLVED.
Thus cost of 14. Mark H Dand andI. mark
Since E.
D Since
is a markedis a marked
successor successor
of A andofthe
A
and the cost of B has changed, Call cost-revise(D).
therefore insert BA and
into Z, that is, now Z={D,
cost of D Thus
has cost of A becomes
changed, therefore 16. Mark
insert B into Z, C.Go
that to step
is, now 2.
Z={D, B}.B, A}.
Reiterate through cost-revise with n=B.
h=16
h=18
A
2 1
B h=6
h=8 C h=7
2 0 0
1
h=14 D h=5
h=7 E F G
1 0 0
0 h=9 h=7
H I J K
Since, B
Z={B}. F,has
G, I,
noJ,descendants
K areReiterate
now allinthrough
labelled cost-revise
SOLVED,
Z, therefore, m=B. therefore,
with
D andn=A.
Ea is SOLVED
are and we
ORed, therefore,
Z={E}.
Aterminate
is not E has
SOLVED. no descendants
Select E, a in
non-terminal Z, therefore,
node from m=E.
the J
marked and K are
subtree ANDed,
and expand
been it
Z={A}. Athe hasalgorithm.
nof(B) Thus,
descendants the minimum
in Z, therefore,
=min([f(D)+C(B, cost
D)] , of
m=A. solving
B and Cthe
[f(E)+C(B, areproblem
E)])ANDed, A has
therefore,
to obtain
found to Jbe
and
18.K The
withproblem
ANDing can therefore,
between them.
be solved f(J)=h(J)=3
at[f(C)+C(A,
minimum cost andbyf(K)=h(K)=4.
solving the
f(A) =[f(B)+C(A,
=min([14+2],[7+1])=min(16, B)] +
8)=8. Thus costC)]of B becomes 8.
Both JBand
subproblems andf(E)
KC,are
each =[f(J)+C(E,
terminal
havingnodes,
further J)] +Thus
[f(K)+C(E,
therefore
marked K)]of [E,
both are marked
subproblems SOLVED.
J, K] and [G],
Since Brespectively. =[8+2]+[7+1]=10+8=18.
is a marked successor of A and the cost of B costchanged,
has of A becomes 18. insert A
therefore
=[3+0] + This is shownCall
[4+0]=3+4=7. by cost-revise(E).
into Z, thatGo
the
Thus
is, to
marked
now cost
Z={D,
step
subtree
of E
2. B, A}.
of the AND-OR
becomes 7. Mark tree.
J and K.
Since E is a marked successor of B and the cost of E has changed,
therefore insert B into Z, that is, now Z={E, B}.Reiterate through cost-
revise with n=B.
Constraint Satisfaction
Many AI problems can be viewed as problems of
constraint satisfaction. SEND
Cryptarithmetic puzzle:
MORE
MONEY
As compared with a straightforward search
procedure, viewing a problem as one of constraint
satisfaction can reduce substantially the amount of
search.
Constraint Satisfaction
Operates in a space of constraint sets.
Initial state contains the original constraints given
in the problem.
A goal state is any state that has been constrained
“enough”.
Two-step process:
1. Constraints are discovered and propagated
as far as possible.
2. If there is still not a solution, then search
begins, adding new constraints.
Constraint Satisfaction
Two kinds of rules:
1. Rules that define valid constraint
propagation.
2. Rules that suggest guesses when necessary.
Means-ends analysis
Involves detection of difference between
current state and goal state
Once difference identified, an operator to
reduce the difference must be found
But perhaps operator cannot be applied to
current state
Subproblem of getting to state where
operator can be applied
Operator may not result in goal state
Second subproblem of getting from new
state to goal state
MEA
MEA process applied recursively
Each rule (operator) has
LHS preconditions and RHS aspects of
can reduce.
Example
Problem for household robot: moving desk with 2
things on it from one room to another.
Main difference between start and goal state is
location.
Choose PUSH and CARRY
Move desk with 2 things on it to
new room
Push Carry Walk Pickup Putdown Place
Move * *
object
Move *
robot
Clear *
object
Get object *
on object
Get arm
empty * *
Be holding
object *
Operator Preconditions Results