You are on page 1of 73

ARTIFICIAL INTELLIGENCE

(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

 Solution: optimal sequence of operators


 Action: “blank moves”

- Condition: the move is within the board


- Directions: Left, Right, Up, Dn
Problem
- which 8-puzzle move is best?
- what heuristic(s) can decide?
- which move is “best” (worth considering first) ?
Actions
Apply the Heuristic
 Three different approaches
- Count correct position of each tile, compare to goal state
- Count incorrect position of each tile, compare to goal state
- Count how far away each tile is from it is correct position.
Heuristic Approaches
 Three different approaches
■ 1st approach :
 Count correct position of each tile, compare to goal state.

 Higher the number the better it is.

 Easy to compute (fast and takes little memory).

 Probably the simplest possible heuristic.

■ 2nd approach
 Count incorrect position of each tile, compare to goal state

 Lower the number the better it is.


 The “best” move is where lowest number returned by heuristic.
■ 3rd approach
 Count how far away each tile is from it’s correct position

 Count how far away (how many tile movements) each tile is from it’s correct

position.
 Sum up these count over all the tiles.

 The “best” move is where lowest number returned by heuristic.


Heuristic Search:Algorithms
 Generate and Test
 Hill Climbing
 Best First Search
 A* Search
Constraint Satisfaction Problems
(CSPs)
 Constraints arise in most areas of human endeavor.
 Constraints are a natural medium for people to express

problems in many fields.


 Many real problems in AI can be modeled as Constraint

Satisfaction Problems(CSPs) and are solved through search.


 Examples of constraints :

− The sum of three angles of a triangle is 180 degrees,


− The su
 The Constraint satisfaction is a process of finding a solution

to a set of constraints m of the currents flowing into a node


must equal zero
Examples
 Eight Queens Puzzle Problem : How can
one put 8 queens on a (8 x 8)chess board
such that no queen can attack any other
queen ?

 Sudoku Problem : How can one fill a


partially completed (9 × 9) grid such that
each row, each column, and each of the
nine (3 × 3) boxes contains the numbers
from 1 to 9.
Generate and Test
 The equivalent of a sequential search in an
array
 A possible solution is generated and is

compared (tested) with the actual solution. If


they match, the problem’s solution has been
found.
Generate and Test:Algorithm
1. Generate a possible solution. For some problems,
this means generating a particular point in the
problem space. For others, it means generating a
path from a start state.
2. Test to see if this is actually a solution by
comparing the chosen point or the endpoint of
the chosen path to the set of acceptable goal
states.
3. If a solution has been found, quit. Otherwise,
return to step 1.
Example
 One possible solution is to systematically try
every placement of queens until we find a
solution.
 The process is known as "Generate and Test“
 Generate and Test conditions
Hill Climbing
 Is a variant of generate-and-test in which feedback
from the test procedure is used to help the problem
solver decide which direction to move towards in the
search space.
 In generate-and-test search, the test function
responds only with a Yes or No.
 If the test function is augmented with a heuristic
function (a function that provides an estimate of how
close a given state is to a goal state) , the search can
be done more efficiently and with a negligible extra
cost.
 One search technique based on a heuristic function is
Hill Climbing.
 Called “Hill Climbing” because it is the equivalent of
climbing a hill in search of the top of the hill, where
you never come down the hill.
Simple Hill Climbing: Algorithm
1. Evaluate the initial state. If it is a goal state, then
return it and quit. Otherwise, continue with the initial
state as the current state.
2. Loop until a solution is found or until there are no
new operators left to be applied in the current state:
a) Select an operator that has not yet been applied
to the current state and apply it to produce a new
state.
b) Evaluate the new state.
i. If it is a goal state, then return it and quit.
ii. If it is not a goal state but it is better than the
current state, then make it the current state.
iii. If it is not better than the current state, then
continue in the loop.
Examples
Example 1
Apply the hill climbing algorithm to solve the blocks
world problem shown in Figure.
Solution

To use the hill climbing algorithm


we need an evaluation function or
a heuristic function.

We consider the following


evaluation function:

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

Hill climbing will halt because all these


states have lower scores than the current
state. The process has reached a local
maximum that is not a global maximum.
We have reached such a situation because
of the particular choice of the heuristic
function. A different choice of heuristic
function may not produce such a situation.
Example 2
Given the 8-puzzle shown in Figure, use the hill-climbing
algorithm with the Manhattan distance heuristic to fi nd a
path to the goal state.

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

Fig shows the various states reached by


applying the various operations
specified in the hill-climbing
algorithm. The numbers written
alongside the grids are the values of
the heuristic function.

Note that the problem is a


minimization problem. The value of
the heuristic function at the goal state
is 0 which is the minimum value of the
function. Note further that once we
find an operation which produces a
better value than the value in current
state we do proceed to that state
without considering whether there is
any other move which produces a state
Selma Joseph MC A
LMC ST
having a still better value.
Example3
Key point while solving any hill-climbing
problem is to choose an appropriate
heuristic function.

Here we have move from initial state to


final state using Hill climbing.
2. Steepest-ascent hill climbing
In steepest-ascent hill climbing, we consider all the moves
from the current state and selects the best as the next state. In
the basic hill climbing, the first state that is better than the
current state is selected.
Steps involved in Steepest-Ascent hill climbing algorithm
Step 1: Evaluate the initial state, if it is goal state then return success and
stop, else make the current state as your initial state.
Step 2: Loop until a solution is found or the current state does not change.
Let S be a state such that any successor of the current state will be better
than it. For each operator that applies to the current state;
• Apply the new operator and generate a new state.
• Evaluate the new state.
• If it is goal state, then return it and quit, else compare it to the S.
• If it is better than S, then set new state as S.
•If the S is better than the current state, then set the current state to S.
Step 3: Exit.
Advantages of Hill Climbing

• Hill climbing is very useful in routing-related problems like


travelling salesmen problem, job scheduling, chip designing, and
portfolio management.
• It is good in solving optimization problems while using only
limited computation power.
• It is sometimes more efficient than other search algorithms.
• Even though it may not give the optimal solution, it gives decent
solutions to computationally challenging problems.
Disadvantages of Hill Climbing
• Both the basic hill climbing and the steepest-ascent hill climbing may
fail to produce a solution. Either the algorithm terminates without
fi nding a goal state or getting into a state from which no better state
can be generated. This will happen if the programme has reached
either a local maximum, a ridge or a plateau.
Steepest – Ascent Hill Climbing: Algo
1. Evaluate the initial state. If it is also a goal state, then return
it and quit. Otherwise, continue with the initial state as the
current state.
2. Loop until a solution is found or until a complete iteration
produces no change to current state:
a) Let SUCC be a state such that any possible successor of the
current state will be better than SUCC.
b) For each operator that applies to the current state do:
i. Apply the operator and generate a new state.
ii. Evaluate the new state. If it is a goal state, then return it
and quit. If not, compare it to SUCC. If it is better, then
set SUCC to this state. If not, leave SUCC alone.
c) If the SUCC is better than current state, then set current
state to SUCC.
Problems with Hill Climbing
 Hill Climbing will fail to find a solution by getting to a
state from which no better states can be generated. This
will happen in the following three situations:
 A local maximum is reached, which is a state that is
better than all its nearby states but is not better than
some other states farther away.
►At a local maxima, every neighboring state in the
state space is worse than the local maximum.

 A plateau is reached, which is a flat area of the search


space in which a whole set of neighboring states have
the same value.
►In such a situation, comparisons cannot result in
deciding a better state, since they are all equal.
Problems with Hill Climbing
 A ridge, a special kind of local maximum, is
reached. It is an area of the search space that is
higher than surrounding areas and that itself has a
slope (which one would like to climb).
►The high region cannot be covered in single
moves, that is, the immediately neighbouring
states are either not better or are equal and
therefore a better state can not be selected.
How to deal with these problems?
► Backtrack to some earlier node and try going in a
different direction. This is particularly fairly good
way of dealing with local maxima.
► Make a big jump in some direction so as to start

searching again in another part of the search space.


This is particularly good way of dealing with
plateaus.
► Apply two or more rules before doing the test. This

corresponds to moving in several directions at once.


This is particularly good strategy for dealing with
ridges.
Informed Search:
Best-first searching
 A best-first search depends on the use of a heuristic to
select the most promising path to a goal node.
 Best first search selects the next node based on the
best estimate out of all the estimates of the heuristic
function calculated so far – this is where Best-First
Search differs from Hill Climbing.
 For example, in a tree in which the heuristic function
calculates the cost of reaching the goal node from the
current node, the best node to choose is the one with
the minimum cost.
Best First Search:
Algorithm
1. Start with OPEN containing just the initial state.
2. Until a goal is found or there are no nodes left on
OPEN do:
a) Pick the best node on OPEN.
b) Generate its successor.
c) For each successor do:
i. If it has not been generated before, evaluate it,
add it to OPEN, and record its parent.
ii. If it has been generated before, change the parent
if it this new path is better than the previous one.
In that case, update the cost of getting to this
node and to any successors that this node may
already, have.
Best First Search:
Dry run
Queue
Tree Front Initial State: A Tail

A-20
A
Expand A and arrange in Asc order of cost

B-15 C-10
C B
Expand C (front)

D-8 E-6 F-0 F


The goal F is found
A* Search
 A specialization of Best-First Search
 The procedure followed by A* Search is:
Along a path to the goal, at each node, the A*
algorithm generates all the child nodes,
For each child node, A* computes the estimate of
the distance (cost) from the start node to a goal
node through each of the children,
A* selects the child node which has the minimum
cost and expands it. The process than repeats for
this selected node until the goal is found or the
search ends in failure.
A* Search
 The total cost function f(n) used by A*, for the
current node n during the search process is given
by
f(n)=g(n) + h(n),
where, g(n) = the cost of reaching the
node n from the root of the tree,
and, h(n) = heuristic function used to
estimate the cost of completing
the processing task at node n.
 The list OPEN stores the nodes which have been
generated but not expanded and are available (are
“open”) for expansion, that is, their child nodes are
yet to be identified.
 The list CLOSED stores the nodes that have been
expanded and are not available (are “closed”) for
further expansion.
A* Search: Issues
 When will a node have to be moved from
CLOSED to OPEN?
This is required only in the case when the data
structure being used for the search is a graph
rather than a tree.
In the case of a graph, the moving of a node from
CLOSED to OPEN indicates a backtracking and
that the path followed till the node being moved
is not the best path and there are other
alternative paths which may be better.
A* Search: Algorithm

1. Initialize: Set OPEN={s}, CLOSED={ },


g(s)=0 and f(s)=h(s).
2. Fail: if OPEN={ }; terminate and fail.
3. Select: Select the minimum cost state, n
from OPEN. Save n in CLOSED.
4. Terminate: If n  G, where G is the set of
goal states, terminate with success and
return f(n).
A* Search: Algorithm
5. Expand: For each child, m, of n
if m  [OPEN  CLOSED]
Set g(m) = g(n) + C(n, m)
Set f(m) = g(m) + h(m)
Insert m in OPEN. C(n, m) is the cost of moving
from node n to node m.
If m  [OPEN  CLOSED]
Set g(m) = min {g(m), g(n) + C(n, m)}
Set f(m) = g(m) + h(m)
If f(m) has decreased and m  CLOSED, move m to
OPEN.
6. Return: Return to Step 2.
A* Search:Dry Run
h(n)
A-5
3 2

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)

C(25) D(9) A(5) B(7)

A(5) B(7) D(9)

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)

C(25) D(9) A(5) B(7)

C(25) E(11) A(5) B(7) D(9)

A(5) B(7) D(9) E(11)

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)

C(25) D(9) A(5) B(7)

C(25) E(11) A(5) B(7) D(9)

C(25) F(28) A(5) B(7) D(9) E(11)

A(5) B(7) D(9) E(11) C(25)

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)

C(25) D(9) A(5) B(7)

C(25) E(11) A(5) B(7) D(9)

C(25) F(28) A(5) B(7) D(9) E(11)

F(28) D(7) A(5) B(7) D(9)


E(11 C(25) C(25)
E(11)

Expand C to obtain D. D belongs to [OPEN union CLOSED] as it is in CLOSED.


Therefore, set g(D)=min{g(D), g(C) + C(C, D)} = min{7, 2 + 3} = 5.
And, set f(D) = g(D) + h(D) = 5 + 2 = 7. As f(D) has decreased from 9 to 7, and D
belongs to CLOSED, therefore move D from CLOSED to OPEN. Return to step 2.
OPEN CLOSED

A(5)
B(7) C(25) A(5)

C(25) D(9) A(5) B(7)

C(25) E(11) A(5) B(7) D(9)

C(25) F(28) A(5) B(7) D(9) E(11)

F(28) D(7) A(5) B(7) E(11 C(25)

F(28) E(9) A(5) B(7) E(11 C(25)


C(25 D(7) D(7)

Expand D to obtain E. E belongs to [OPEN union CLOSED] as it is in CLOSED.


Minimum cost state in OPEN is D. Put D in CLOSED. D does not belong to G.
Therefore, set g(E)=min{g(E), g(D) + C(D, E)} = min{8, 5 + 1} = 6.
And, set f(E) = g(E) + h(E) = 6 + 3 = 9. As f(E) has decreased from 11 to 9, and E
belongs to CLOSED, therefore move E from CLOSED to OPEN. Return to step 2.
OPEN CLOSED

A(5)
B(7) C(25) A(5)

C(25) D(9) A(5) B(7)

C(25) E(11) A(5) B(7) D(9)

C(25) F(28) A(5) B(7) D(9) E(11)

F(28) D(7) A(5) B(7) E(11 C(25)

F(28) E(9) A(5) B(7) C(25) D(7)

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.

 An AND node represents a given decomposition in


which
All successors have to be solved.
Solving is done by taking the sum of the costs of
the sub-trees.
AND-OR GRAPHS
 An AND-OR node is a combination of one or more ORed
children and one or more ANDed children. For example:
GOAL: Acquire TV set

GOAL: Borrow a TV GOAL: Earn GOAL: Buy a TV


set some money set

FUTILITY: It is value that should be chosen to correspond


to a threshold such that any solution with a cost above
it is too expensive to be practical, even if it could ever
be found.
If estimated cost(node)>FUTILITY, then, we abandon the
search
Problem Reduction: Algorithm
1. Initialize the graph to the starting node.
2. Loop until the starting node is labeled SOLVED or until its cost goes
above FUTILITY:
a) Traverse the graph, starting at initial node and following the
current best path, and accumulate the set of nodes that are on
that path and have not yet been expanded or labeled as solved.
b) Pick one of these unexpanded nodes and expand it.
i. If there are no successors, assign FUTILITY as the value of this
node.
ii. Otherwise, add its successors to the graph and for each of them
compute f’. If f’= 0, mark that node as SOLVED.
iii. Pick one of these unexpanded nodes and
c) Change f’ estimate of newly expanded node to reflect the new
information provided by its successors. Propagate this change
backwards through the graph. If any node contains a succor arc
whose descendants are all solved, label the node itself as SOLVED
AND-OR GRAPH SEARCH PROBLEM

 Given: [G, s, T],


where
G= implicitly specified AND-OR graph
s = start node of the AND-OR graph
T = set of terminal nodes
h(n)= heuristic function estimating the cost of
solving the sub-problem at n.
To find: A minimum cost solution tree.
AO* ALGORITHM
1. Initialize: Set G* = {s}, f(s) = h(s).
G* = marked subtree (or subgraph) rooted at the start
node s. If s  T, label s as SOLVED.

T = set of leaf or terminal subproblems which are unit


level subproblems, which can not be decomposed further
but which have to be solved directly.

2. Terminate: If s is SOLVED, then Terminate.


AND node is SOLVED when all children are SOLVED.
OR node is SOLVED when the best cost child is SOLVED.
AO* ALGORITHM
3. Select: Select a non-terminal leaf node n from the marked
subtree.
4. Expand: Make explicit the children of n. (Generate or
expand the children of n).
For each new child m:
Set f(m) = h(m)
If m is a terminal node, label m as SOLVED.
5. Cost-revision: Call cost-revise(n).
6. Loop: Go to step 2.
AO* ALGORITHM: Cost Revision
Procedure cost-revise(n)
1. Create Z={n}.
n=the node from where cost revision is to be
started.
2. If Z={ }, return.
3. Select a node m from Z such that m has no descendants
in Z (because we want to go bottom up, we select a node
which has no descendants).
4. If m is an AND node with children r1, r2, ….., rk,
Set f(m) =  [ f(ri) + C(m, ri)].
where, f(ri ) = Cost of child node,
and, C(m, ri) = Cost of edge.
Mark the edge to each child of m with a double line
indicating it is SOLVED. If each child is labeled SOLVED, then
label m as SOLVED.
AO* ALGORITHM: Cost Revision
5. If m is an OR node with children r1, r2,….., rk,
Set f(m) = min { f(ri) + C(m, ri) }
Mark the edge to the best child of m with a double
line indicating it is SOLVED. If the marked child is
labeled SOLVED, then label m as SOLVED.
6. If the cost or label of m has changed, then insert
those parents of m into Z for which m is a marked
child.
h=7
A

1.Initialize : G*={s}={A} & f(s)=h(s)=7


T={I, J, K, F, G} and A  T, therefore, A is not SOLVED.
Select a non-terminal node, namely, A and expand it.
h=10
h=7
A
2 1

B h=4 C h=3

Expanding A, we get B and C with ANDing between them.


Z={A}. A has no descendants in Z, and
f(B)=h(B)=4 therefore, m=A. B and C are ANDed,
f(C)=h(C)=3
therefore, f(A)=[f(B)+C(A,
Neither B nor B)]
C are terminal + [f(C)+C(A,
nodes, thereforeC)]=[4+2]+[3+1]=6+4=10.
they are not marked SOLVED.
Thus cost of Call
A becomes 10. Mark B and C.
cost-revise(A).
Go to step 2.
h=10
A
2 1

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

h=3 h=10 h=3 h=4

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

problem state changed.


 Difference table of rules and differences they

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

PUSH (obj, loc) at(robot,obj) at(obj, loc)


&large (obj) & at (robot, loc)
&clear (obj) &
arm empty
CARRY (obj, loc) at(robot, obj) &Small at(obj, loc) &at(robot,
(obj) loc)

WALK(loc) none At(robot, loc)

PICKUP(obj) At(robot, obj) Holding(obj)

PUTDOWN(obj) Holding(obj) Not holding (obj)


PLACE(obj1, obj2) At(robot,obj2) & holding on(obj1, obj2)
(obj1)
CARRY: preconditions cannot be met
PUSH: 4 preconditions
WALK to object, clear desk using PICKUP and PLACE. After PUSH objects not on
desk. Must WALK to collect them and put on table using PICKUP and CARRY
Means-Ends Analysis
1. Compare CURRENT to GOAL. If no differences, return.
2. Otherwise select most important difference and reduce
it by doing the following until success or failure is
indicated.
(a) Select an as yet untried operator O that is applicable to
the current difference. If there are no such operators
then signal failure.
(b) Attempt to apply O to the current state. Generate
descriptions of two states O-START a state in which
O’s preconditions are satisfied and O-RESULT, the
state that would result if O were applied in O-START.
(c) If (FIRST-PART MEA (CURRENT,O-START) AND (LAST-
PART MEA (O-RESULT, GOAL) are successful then
signal success.
8-Puzzle Problem(Solution)

You might also like