Artificial Intelligence: CIT 246

Artificial Intelligence
By
Dr. A.S.Alvi
(M.E., PhD.)
Forward Versus Backward Reasoning

Forward : From the Start State.
Start applying inference rules to the logical expressions you
have, and stop if one of your results happens to be the
conclusion you want

Backward: From the Goal State.
Start from the conclusion you want, and try to choose
inference rules that will get you back to the logical
expressions you have
Problem Trees Versus Problem Graphs
A simple way to implement any search strategy is as
a tree traversal.
 A tree search procedure can be converted to a graph
search procedure by modifying the action performed
each time a node is generated. Instead of simply
adding the node to the graph.
 Procedure (Water Jug Problem)
1. Examine the set of nodes that have been created so
far to see if the new node already exists.
2. If it does not, simply add it to the graph just as for
a tree.

3. If it does already exist, then do the following two
things.
a. Select the node that is being expanded to point to already
existing node corresponding to its successor, rather than to
the new one. The new one can simply be thrown away.
b. If you are keeping track of the best path to each node, then
check to see if the new path is better or worse than the old
one. If worse, do nothing. If better, record the new path as
correct path to use to get to the node, and propagate the
corresponding change in cost down through successor node
as necessary.
Knowledge Representation & the Frame problem

In complex domain, it is often useful to divide the
representation question into three subquestions
1. How can individual objects and facts be represented.
2.How can the representation of individual objects be combined
to form a representation of a complex problem state.
3.How can the sequences of problem states that arise in a
search process be represented efficiently.
The first two questions are usually referred to as the
problem of knowledge representation.
The third question is particularly important in the context
of a search process.
The whole problem of representing the facts that change as
well as those that do not is known as the frame problem
Matching

Indexing:
One way to select applicable rules is to do a simple search
through all the rules, comparing each one’s preconditions to
the current state and extracting all the ones that matches.
Matching

The condition/premise patterns in the rules need to be
matched with the known facts.


Consider, a typical rule (about the value of horses) that matches
a set of facts:
Rule
Facts
IF: x is a horse
Comet
is
a horse
x is the parent of y
Prancer
is a horse
y is fast
Comet
is
parent-of Dasher
THEN: x is valuable
Comet
is
parent-of Prancer
Prancer
is
fast
Dasher
is
parent-of Thunder
Thunder is fast
Thunder is a horse
Dasher
is a horse
Binding
We simply need to see which values can be assigned to the
variables in the rule. For example, “Comet is-a horse” matches “x
is-a horse”, but “Comet is-a lion” would not.
 Bindings for
Bindings for
Bindings for
“x is-a horse”
“y is fast”
“x is a parent of y”
x = Comet
y = Prancer
x = Comet , y = Dasher
x = Prancer
y = Thunder
x = Comet , y = Prancer
x = Thunder
x = Dasher , y = Thunder
x = Dasher





From these we can deduce that there are two
possible bindings applicable to the rule:
“x = Comet and y = Prancer” ,
“x = Dasher and y =
Thunder”
The rule then tells us “x is valuable”,
i.e. “Comet is valuable” and “Dasher is valuable”.
Matching

Matching with variables:
Consider the following facts
MOTHER(Mary , John) MOTHER (Kittu , Mita)
MOTHER (Tina , Kittu) MOTHER (John , Mike)
Consider the following Rules
1.MOTHER(x,y) ^ PARENT(y,z)
GRANDMOTHER(x,z)
2. PARENT(x,y)
FATHER(x,y)
3. PARENT(x,y)
MOTHER(x,y)
GOAL: 1. GRANDMOTHER(Marry, Mike) = ?
2. GRANDMOTHER(Marry, Mita) = ?
3. GRANDMOTHER( ? , Mita)
4. GRANDMOTHER(Tinay, ? )
Heuristic Function

A Heuristic is a function that, when applied to a state,
returns a number that is an estimate of the merit of the state,
with respect to the goal.

In other words, the heuristic tells us approximately how far
the state is from the goal state*.

The purpose of a heuristic function is to guide the search
process in the most profitable direction, by suggesting which
path to follow first when more than one paths are available.
Heuristics for 8-puzzle I
•The number
of misplaced
tiles (not
including the
blank)
Current
State
1
2
3
4
5
6
7
1
Goal
State
8
2
4
5
7
8
3
h(current state) = 1
22
33
44
55
66
77
8
8
N
N
N
N
N
N
N
Y
6
In this case, only “8” is misplaced, so the heuristic
function evaluates to 1.
In other words, the heuristic is telling us, that the
solution might be available in just 1 more move.
Notation: h(n)
11
Heuristics for 8-puzzle II
Current
State
•The Manhattan
Distance (not
including the
blank)
3
2
8
4
5
6
7
1
3
3
2 spaces
8
1
Goal
State
4
2
5
3
3 spaces
6
8
7
8
1
In this case, only the “3”, “8” and “1” tiles are
misplaced, by 2, 3, and 3 squares respectively, so
the heuristic function evaluates to 8.
In other words, the heuristic is telling us, that it thinks a
solution is available in just 8 more moves.
Notation: h(n)
h(current state) = 8
3 spaces
1
Total 8
We can use heuristics to
guide “hill climbing” search.
In this example, the
Manhattan Distance
heuristic helps us quickly
find a solution to the 8puzzle.
1
2
4
8
7
6
1
2
4
8
7
6
3
5
h(n)
3
5
5
6
1
2
3
4
8
5
7
6
1
2
3
4
8
5
6
7
1
2
3
4
5
6
7
8
1
2
3
4
8
7
5
6
1
2
3
4
5
7
8
0
4
3
1
4
2
3
7
8
5
6
1
2
3
4
5
6
4
1
6
7
1
2
4
5
7
8
3
6
8
2
2
1
3
3
4
2
7
8
5
6
3
In this example, hill
climbing does not
work!
All the nodes on
the fringe are
taking a step
“backwards”
(local minima)
Note that this
puzzle is solvable
in just 12 more
steps.
1
2
3
4
5
8
6
1
2
3
4
5
8
6
7
7
7
1
2
4
6
h(n)
6
1
2
4
5
6
7
3
5
7
8
6
3
5
8
1
2
4
5
3
6
7
8
6
Generate and Test
Generate and Test consists of the following steps:
 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.
 Test
to see if this is actually a solution by comparing the
chosen point or the end point of the chosen path to the set
of acceptable goal state.
 If
solution has been found quit. Otherwise return to step 1.
Generate and Test

This procedure could lead to an eventual solution within a
short period of time if done systematically.

However if the problem space is very large, the eventual
solution may take a very long time.

For a simple problems, exhaustive generate-and-test is often
a reasonable technique.

For problems much harder than this, even heuristic generateand-test, is not very effective technique.
6
A
1
2
3
5
D
B
44
C
Hill Climbing
The simplest way to implement hill climbing is as follows:
 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.
 Loop until a solution is found or until there are no new
operators left to be applied in the current state.
 Select an operator that has not yet been applied to the
current state and apply it to produce new state.
 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
Depth-first searching

A
B
C

D
E
F
H
L
M
I
N
O
G
J
P
K

Q

20
A depth-first search (DFS)
explores a path all the way to
a leaf before backtracking and
exploring another path
For example, after searching
A, then B, then D, the search
backtracks and tries another
path from B
Node are explored in the
order A B D E H L M N I O P
CFGJKQ
N will be found before J
Depth-first Search
Algorithm:
 Put the root node on a stack;
while (stack is not empty) {
remove a node from the stack;
if (node is a goal node) return success;
put all children of node onto the stack;
}
return failure;
Depth-first Search
Algorithm for Depth-First Search






1. Put the initial node on the list of START.
2. If (START is empty) or (START = GOAL) terminate
search.
3. Remove the first node from the list of START. Call this
node d.
4. If (d = GOAL) terminate search with success.
5. Else if node d has successors, generate all of them and add
them at the beginning of START.
6. Go to step 2.
Breadth-first searching

A
B
C

D
E
F
G

H
L
M
I
N
O
J
P
K
Q

A breadth-first search (BFS)
explores nodes nearest the
root before exploring nodes
further away
For example, after searching
A, then B, then C, the search
proceeds with D, E, F, G
Node are explored in the
order A B C D E F G H I J K
LMNOPQ
J will be found before N
Breadth first Search
Algorithm:
 Put the root node on a queue;
while (queue is not empty) {
remove a node from the queue;
if (node is a goal node) return success;
put all children of node onto the queue;
}
return failure;
Breadth-First Search
Algorithm for Breadth-First Search
 1. Put the initial node on the list of START.
 2. If (START is empty) or (START = GOAL) terminate
search.
 3. Remove the first node from the list of START. Call this
node d.
 4. If (d = GOAL) terminate search with success.
 5. Else if node d has successors, generate all of them and add
them at the tail of START.
 6. Go to step 2.
Hill Climbing

Algorithm for Hill Climbing Search
1. Put the initial node on the list of START.
2. If (START is empty) or (STRAT = GOAL) terminate search.
3. Remove the first node from the list of START. Call this node d.
4. If (d = GOAL) terminate search with success.
5. Else if node d has successors, generate all of them. Find out
how far they are from the goal node. Sort them by the
remaining distance from the goal and add them to the
beginning of START.
6. Go to step 2.
Best-First Search






Is the new method which combine the advantages of both
depth-first and breadth-first search into single method.
At each step of the best-first-search process, we select the
most promising of the nodes generated.
This is done by applying appropriate heuristic function to
each of them.
Then expand the chosen node by using the rules to generate
its successors.
If one of them is a solution we can quit, if not, all those new
nodes are added to the set of nodes generated so far.
Again the most promising node is selected and the process
continues.
Best First Search
Algorithm
1. Put the initial node on the list of START.
2. If (START is empty) or (STRAT = GOAL) terminate search.
3. Remove the first node from the list of START. Call this node d.
4. If (d = GOAL) terminate search with success.
5. Else if node d has successors, generate all of them. Find out
how far they are from the goal node. Sort all the children
generated so far by the remaining distance from the goal.
6. Name this list as START 1.
7. Replace START with START 1.
8. Go to step 2.

Algorithm: Best-First Search
I. 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 successors.
(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
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 .
[A]
Apply best-first search to find ‘3’ defect in
[A]
a production
[B D C]
[B D C]
A
[E I D C]
[D C E I]
[I D C]
[H G C E I]
[G C E I]
8
13
B
[D C]
11
C
D
[H G C]
[G C]
[J C E I]
[J C]
E 14
16
I
23
Traversing order (i.e. the search path) is
ABDHGJ
F
7
G
3
J
If hill climbing is used, the solution path will be
A B E I D H G J (it is longer, less ‘intelligent’)
5
H
A* Algorithm
f(n) = g(n) + h(n)
Where
g(n) = depth of node in the search tree
h(n) = misplaced positions
Initial
1
2
4
8
7
6
Goal
3
5
1
2
3
4
5
6
7
8
Problem decomposition into an and-or graph
A technique for reducing a problem to a production system, as
follows:
The principle goal is identified; it is split into two or more
sub-goals; these, too are split up.
A goal is something you want to achieve. A sub-goal is a
goal that must be achieved in order for the main goal to be
achieved.
A graph is drawn of the goal and subgoals. Each goal is
written in a box, called a node, with its subgoals underneath it,
joined by links.
A goal may be split into 2 (or more) subgoals, BOTH of which
must be satisfied if the goal is to succeed; the links joining the
goals are marked with a curved line, like this:
Goal 1
Goal 2
Or a goal may be split into 2 (or more) sub-goals, EITHER of
which must be satisfied if the goal is to succeed; the links joining
the goals aren't marked with a curved line:
Goal 1
Goal 2
Example
"The function of a financial advisor is to help the user decide
whether to invest in a savings account, or the stock market, or
both. The recommended investment depends on the investor's
income and the current amount they have saved:
Individuals with inadequate savings should always increase the
amount saved as their first priority, regardless of income.
Individuals with adequate savings and an adequate income
should consider riskier but potentially more profitable investment
in the stock market.
Individuals with low income who already have adequate
savings may want to consider splitting their surplus income
between savings and stocks, to increase the cushion in savings
while attempting to increase their income through stocks.