Beruflich Dokumente
Kultur Dokumente
This Lab is based on the book by Ivan Bratko, Prolog Programming for Artificial Intelligence, Third
Edition, 2001, Pearson Education Ltd., Second Impression 2007 (in India).
Given the heuristic function f(n) = g(n) + h(n), where g(n) is an estimate of the cost of an optimal path
from starting node, s, to current node, n, and h(n) is an estimate of the cost of an optimal path from
node n to the goal node t.
When node n is encountered by the search process we have following situation: a path from s to n
must have already been found and its cost can be computed as the sum of the arc-costs on the path. It
may not be optimal cost but its cost can serve as an estimate g(n) of the minimal cost from s to n.The
other term, h(n), is more problematic because the ‘world’ between n and t has not been explored by the
search until this point. Therefore, h(n) is typically is real heuristic guess, based on the algorithm’s
general knowledge about the particular problem.
Starting with the start node, the search keeps generating new successor nodes, always expanding in the
most promising direction according to the f-values. During this process, a search tree is generated
whose root is the start node of the search. The best-first search program will thus kep expanding this
search tree until a solution is found. This tree will be represented in the program by terms of two
forms:
1. l(N, F/G) represents a single node tree (a leaf); N is a node in the state space, G is g(N) (cost of
the path found from the start node to N); F is f(N) = G + h(N).
2. t(N, F/G, Subs) represents a tree with non-empty subtree; N is the root of the tree, Subs is a list
of its subtrees; G is g(N); F is updated f-value of N – that is, the f-value of the most promising
successor of N; the list Subs is ordered according to increasing f-values of the sub-trees.
The updating of the f-values is necessary to enable the program to recognize the most promising
subtree at each level of the search tree. This modification of f-estimates lead, in fact, to a function f
from nodes to tree. For a single node tree (a leaf), n, we have the original definition of f(n) i.e. f(n) =
g(n) + h(n). For a tree, T, hose root is n, and n’s subtrees are S1, S2, etc., f(T) = minif(Si).
A best-first program along these lines is shown in Table 1. Some more explanation of this program
follows.
It expands a current (sub)tree as long as the f-value of this tree remains less or equal to Bound. The
arguments of expand are given in Table 2.
P, Tree and Bound are ‘input’ parameters to expand. Expand produces three kinds of results which is
indicated by the value of the argument Solved as follows:
(1). Solved = yes.
Solution = a solution path found by expanding Tree within Bound.
Tree1 = uninstantiated.
A Prolog program (based on the best first search) to solve 8-puzzle problem is given in Table 3.
However, some predicates need more clauses to be added.
Do the following:
1. Apply the Best-first search (Table 2) to
solve the routing problem (as given in
Fig. 1) to find the shortest path from
node s to node t.
2. Find the time taken by the method to
find the shortest path for problem 1
above. [Hint: Read and Use time/2
predicate given in the LPA Win-
Prolog help]
3. Comparing the program in Table 1
and Table 3, make a table of predicates
of Table 1 and Table 3 by stating
which predicate(s) in Table 3 does the
analogous or similar work of which
predicate(s) in Table 1. Figure 1: A routing network [The numbers along
4. The Table 3 (Best-first search applied arcs are g(n) and numbers in square are h(n)]
to 8-puzzle problem) does not have some clauses of some predicate. Complete
1 2
those and run the program for five initial conditions to reach to the goal state.
The goal state is given in Fig. 2. 3 4 5
5. Name your files as L4_AI_YourID_pN and zip them as L4_AI_YourID.zip and 6 7 8
transmit to instructor’s email id. Replace the text YourID by your BITS
IDNumber and N in pN by problem number 1, 2, 3, or 4 as the case may be. Figure 2
% A best-first search program.
% Extract f-value
min( X, Y, X) :-
X =< Y, !.
min( X, Y, Y).
Table 1
Table 2
3 1 2 3
2 8 4 [2/2, 1/3, 2/3, 3/3, 3/2, 3/1, 2/1, 1/1, 1/2]
1 7 6 5
1 2 3
dif( A, B, D) :- % D is |A-B|
D is A-B, D >= 0, !
;
D is B-A.
h( [Empty | Tiles], H) :-
goal( [Empty1 | GoalSquares] ), ヘ
totdist( Tiles, GoalSquares, D), % Total distance from home squares
seq( Tiles, S), % Sequence score
H is D + 3*S.
showsol( [P | L] ) :-
showsol( L),
nl, write( '---'),
showpos( P).
showpos( [S0,S1,S2,S3,S4,S5,S6,S7,S8] ) :-
member( Y, [3,2,1] ), % Order of Y-coordinates
nl, member( X, [1,2,3] ), % Order of X-coordinates
member( Tile-X/Y, % Tile on square X/Y
[' '-S0,1-S1,2-S2,3-S3,4-S4,5-S5,6-S6,7-S7,8-S8] ),
write( Tile),
fail % Backtrack to next square
;
true. % All squares done
Table 3