0% found this document useful (0 votes)
121 views14 pages

AI Lab Manual

The document discusses implementing various problems in Prolog including simple facts and queries, arithmetic operations, the monkey banana problem, towers of Hanoi problem, 8 puzzle problem, N queens problem, and travelling salesman problem. Source code is provided for programs to solve each problem.

Uploaded by

ksaipraneeth1103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views14 pages

AI Lab Manual

The document discusses implementing various problems in Prolog including simple facts and queries, arithmetic operations, the monkey banana problem, towers of Hanoi problem, 8 puzzle problem, N queens problem, and travelling salesman problem. Source code is provided for programs to solve each problem.

Uploaded by

ksaipraneeth1103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

AIM : 1)program in prolog to implement simple facts and queries ?

Source Code:

FACTS:
A fact is a predicate expression that makes a declarative statement about the problem
domain. Whenever a variable occurs in a Prolog expression, it is assumed to
be universally quantified. Note that all Prolog sentences must end with a period.
likes(john, susie). /* John likes Susie */
likes(X, susie). /* Everyone likes Susie */
likes(john, Y). /* John likes everybody */
likes(john, Y), likes(Y, john). /* John likes everybody and everybody
likes John */
likes(john, susie); likes(john,mary). /* John likes Susie or John likes Mary
*/
not(likes(john,pizza)). /* John does not like pizza */
likes(john,susie) :- likes(john,mary)./* John likes Susie if John likes Mary.

Queries

The Prolog interpreter responds to queries about the facts and rules represented in its
database. The database is assumed to represent what is true about a particular problem
domain. In making a query you are asking Prolog whether it can prove that your query
is true. If so, it answers "yes" and displays any variable bindings that it made in
coming up with the answer. If it fails to prove the query true, it answers "No".

Whenever you run the Prolog interpreter, it will prompt you with ?-. For example,
suppose our database consists of the following facts about a fictitious family.
father_of(joe,paul).
father_of(joe,mary).
mother_of(jane,paul).
mother_of(jane,mary).
male(paul).
male(joe).
female(mary).
female(jane).
AIM : 2) Write a program to implement simple arithmetic

Source code :

i) Write a prolog program to calculate the sum of two numbers.


A:

sum(X,Y,Z):-
Z is X+Y.

Output : sum(4,5,Z).
Z = 9

ii) Write a Prolog program to implement max(X, Y, M)and min(X,Y,M) so that


M is the maximum of two numbers X and Y
A:

max(X,Y,M):-X>Y,M is X.
max(X,Y,M):-Y>=X,M is Y.

find_min(X, Y, X) :- X =< Y, !.
find_min(X, Y, Y) :- X > Y.

output:

| ?- find_min(40,10,Min). min=10

| ?- find_max(40,10,Max). Max = 40

iii) Write a Prolog program to implement power (Num,Pow, Ans) : where Num
is raised to the power Pow to get Ans.
A:

power(X,0):- !.
power(Num,Pow,Ans):-
Ans is Num^Pow.

iv) Prolog program to implement multi (N1, N2, R) : where N1 and N2


denotes the numbers to be multiplied and R represents the result.
A:

multi(X,0).
multi(N1,N2,R):- R is N1*N2.

AIM : 3) Write a program to implement Monkey Banana Problem

STATE SPACE TREE:


Source Code:

move(state(middle,onbox,middle,hasnot),

grasp,

state(middle,onbox,middle,has)).

move(state(P,onfloor,P,H),

climb,

state(P,onbox,P,H)).

move(state(P1,onfloor,P1,H),

drag(P1,P2),

state(P2,onfloor,P2,H)).

move(state(P1,onfloor,B,H),

walk(P1,P2),

state(P2,onfloor,B,H)).

canget(state(_,_,_,has)).

canget(State1) :-

move(State1,_,State2),

canget(State2).

Output:

| ?- [monkey_banana].
compiling D:/TP Prolog/Sample_Codes/monkey_banana.pl for byte code...

D:/TP Prolog/Sample_Codes/monkey_banana.pl compiled, 17 lines read - 2167 bytes written,


19 ms

(31 ms) yes

| ?- canget(state(atdoor, onfloor, atwindow, hasnot)).

true ?

yes

| ?- trace

The debugger will first creep -- showing everything (trace)

yes

{trace}

| ?- canget(state(atdoor, onfloor, atwindow, hasnot)).

1 1 Call: canget(state(atdoor,onfloor,atwindow,hasnot)) ?

2 2 Call: move(state(atdoor,onfloor,atwindow,hasnot),_52,_92) ?

22

Exit:move(state(atdoor,onfloor,atwindow,hasnot),walk(atdoor,_80),state(_80,onfloor,atwindo
w,hasnot)) ?

3 2 Call: canget(state(_80,onfloor,atwindow,hasnot)) ?

4 3 Call: move(state(_80,onfloor,atwindow,hasnot),_110,_150) ?
4 3 Exit:
move(state(atwindow,onfloor,atwindow,hasnot),climb,state(atwindow,onbox,atwindow,hasno
t)) ?

5 3 Call: canget(state(atwindow,onbox,atwindow,hasnot)) ?

6 4 Call: move(state(atwindow,onbox,atwindow,hasnot),_165,_205) ?

6 4 Fail: move(state(atwindow,onbox,atwindow,hasnot),_165,_193) ?

5 3 Fail: canget(state(atwindow,onbox,atwindow,hasnot)) ?

4 3 Redo:
move(state(atwindow,onfloor,atwindow,hasnot),climb,state(atwindow,onbox,atwindow,hasno
t)) ?

4 3 Exit:
move(state(atwindow,onfloor,atwindow,hasnot),drag(atwindow,_138),state(_138,onfloor,_138
,hasnot)) ?

5 3 Call: canget(state(_138,onfloor,_138,hasnot)) ?

6 4 Call: move(state(_138,onfloor,_138,hasnot),_168,_208) ?

6 4 Exit: move(state(_138,onfloor,_138,hasnot),climb,state(_138,onbox,_138,hasnot)) ?

7 4 Call: canget(state(_138,onbox,_138,hasnot)) ?

8 5 Call: move(state(_138,onbox,_138,hasnot),_223,_263) ?

8 5 Exit: move(state(middle,onbox,middle,hasnot),grasp,state(middle,onbox,middle,has)) ?

9 5 Call: canget(state(middle,onbox,middle,has)) ?

9 5 Exit: canget(state(middle,onbox,middle,has)) ?

7 4 Exit: canget(state(middle,onbox,middle,hasnot)) ?

5 3 Exit: canget(state(middle,onfloor,middle,hasnot)) ?

3 2 Exit: canget(state(atwindow,onfloor,atwindow,hasnot)) ?

1 1 Exit: canget(state(atdoor,onfloor,atwindow,hasnot)) ?

true ?
(78 ms) yes

AIM : 4) Write a program to implement Tower’s of Hanoi Problem?

State Space Tree:

Source Code:

move(1,X,Y,_) :-
write('Move top disk from '), write(X), write(' to '), write(Y), nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).

Output:

| ?- [towersofhanoi].
compiling D:/TP Prolog/Sample_Codes/towersofhanoi.pl for byte code...
D:/TP Prolog/Sample_Codes/towersofhanoi.pl compiled, 8 lines read - 1409 bytes written, 15 ms

yes
| ?- move(4,source,target,auxiliary).
Move top disk from source to auxiliary
Move top disk from source to target
Move top disk from auxiliary to target
Move top disk from source to auxiliary
Move top disk from target to source
Move top disk from target to auxiliary
Move top disk from source to auxiliary
Move top disk from source to target
Move top disk from auxiliary to target
Move top disk from auxiliary to source
Move top disk from target to source
Move top disk from auxiliary to target
Move top disk from source to auxiliary
Move top disk from source to target
Move top disk from auxiliary to target

true ?

(31 ms) yes

AIM: 5) Write a program to implement 8 Puzzle Problem?

State Space Tree:

Source Code:

test(Plan
):-
write('Initial state:'),nl,
Init= [at(tile4,1), at(tile3,2), at(tile8,3), at(empty,4), at(tile2,5), at(tile6,6),
at(tile5,7), at(tile1,8), at(tile7,9)],
write_sol(Init),
Goal= [at(tile1,1), at(tile2,2), at(tile3,3), at(tile4,4), at(empty,5), at(tile5,6),
at(tile6,7), at(tile7,8), at(tile8,9)],
nl,write('Goal state:'),nl,
write(Goal),nl,nl,
solve(Init,Goal,Plan).
solve(State, Goal, Plan):-
solve(State, Goal, [], Plan).

is_movable(X1,Y1) :- (1 is X1 - Y1) ; (-1 is X1 - Y1) ; (3 is X1 - Y1) ; (-3 is X1 - Y1).

solve(State, Goal, Plan, Plan):-


is_subset(Goal, State), nl,
write_sol(Plan).
solve(State, Goal, Sofar, Plan):-
act(Action, Preconditions, Delete, Add),
is_subset(Preconditions, State),
\+ member(Action, Sofar),
delete_list(Delete, State, Remainder),
append(Add, Remainder, NewState),
solve(NewState, Goal, [Action|Sofar], Plan).
act(move(X,Y,Z),
[at(X,Y), at(empty,Z), is_movable(Y,Z)],
[at(X,Y), at(empty,Z)],
[at(X,Z), at(empty,Y)]).

is_subset([H|T], Set):-
member(H, Set),
is_subset(T, Set).
is_subset([], _).

delete_list([H|T], Curstate, Newstate):-


remove(H, Curstate, Remainder),
delete_list(T, Remainder, Newstate).

delete_list([], Curstate, Curstate).


remove(X, [X|T], T).
remove(X, [H|T], [H|R]):-
remove(X, T, R).
write_sol([]).
write_sol([H|T]):-
write_sol(T),
write(H), nl.
append([H|T], L1, [H|L2]):-
append(T, L1, L2).
append([], L, L).
member(X, [X|_]).
member(X, [_|T]):-
member(X, T).

Output:
?-
test(Pla
n).
Initial state:
at(tile7,9)
at(tile1,8)
at(tile5,7)
at(tile6,6)
at(tile2,5)
at(empty,4)
at(tile8,3)
at(tile3,2)
at(tile4,1)
Goal state:
[at(tile1,1),at(tile2,2),at(tile3,3),at(tile4,4),at(empty,5),at(tile5,6),at(tile6,7),a
t(tile7,8),at(tile8,9)]
false.

AIM : write a program to implement 4 Queens Problem ?

State Space Tree :

Source Code:
n_queens(N, Qs) :-
length(Qs, N),
Qs ins 1..N,
safe_queens(Qs).

safe_queens([]).
safe_queens([Q|Qs]) :-
safe_queens(Qs, Q, 1),
safe_queens(Qs).

safe_queens([], _, _).
safe_queens([Q|Qs], Q0, D0) :-
Q0 #\= Q,
abs(Q0 - Q) #\= D0,
D1 #= D0 + 1,
safe_queens(Qs, Q0, D1).
Output:

?- N = 8, n_queens(N, Qs), labeling([ff], Qs).


N = 8, Qs = [1,5,8,6,3,7,2,4]
; ...

?- N = 20, n_queens(N, Qs), labeling([ff], Qs).


N = 20, Qs = [1,3,5,14,17,4,16,7,12,18|...]
; ...

AIM : 7) Write a program to implement Travelling salesman Problem ?

State Space Tree :

Source Code :

edge(a, b, 3).
edge(a, c, 4).
edge(a, d, 2).
edge(a, e, 7).
edge(b, c, 4).
edge(b, d, 6).
edge(b, e, 3).
edge(c, d, 5).
edge(c, e, 8).
edge(d, e, 6).
edge(b, a, 3).
edge(c, a, 4).
edge(d, a, 2).
edge(e, a, 7).
edge(c, b, 4).
edge(d, b, 6).
edge(e, b, 3).
edge(d, c, 5).
edge(e, c, 8).
edge(e, d, 6).
edge(a, h, 2).
edge(h, d, 1).

/* Finds the length of a list, while there is something in the list


it increments
N when there is nothing left it returns.*/

len([], 0).
len([H|T], N):- len(T, X), N is X+1 .

/*Best path, is called by shortest_path. It sends it the paths found


in
a path, distance format*/

best_path(Visited, Total):- path(a, a, Visited, Total).

/*Path is expanded to take in distance so far and the nodes visited


*/

path(Start, Fin, Visited, Total) :- path(Start, Fin, [Start],


Visited, 0, Total).

/*This adds the stopping location to the visited list, adds the
distance and
then calls recursive
to the next stopping location along the path */

path(Start, Fin, CurrentLoc, Visited, Costn, Total) :-

edge(Start, StopLoc, Distance), NewCostn is Costn + Distance, \+


member(StopLoc, CurrentLoc),
path(StopLoc, Fin, [StopLoc|CurrentLoc], Visited, NewCostn, Total).

/*When we find a path back to the starting point, make that the total
distance and
make sure the graph has touch every node*/

path(Start, Fin, CurrentLoc, Visited, Costn, Total) :-


edge(Start, Fin, Distance), reverse([Fin|CurrentLoc], Visited),
len(Visited,Q), (Q\=7 -&gt; Total is 100000; Total is Costn +
Distance).

/*This is called to find the shortest path, takes all the paths,
collects them
in holder.
Then calls pick on that holder which picks the shortest path and
returns it*/

shortest_path(Path):-setof(Cost-Path,best_path(Path,Cost),
Holder),pick(Holder,Path).

best(Cost-Holder,Bcost-_,Cost-Holder):- Cost&lt;Bcost,!.
best(_,X,X).
pick([Cost-Holder|R],X):-
pick(R,Bcost-Bholder),best(Cost-Holder,Bcost-
Bholder,X),!.
pick([X],X).

AIM : write a program in prolog to implement Water Jug Problem.

State Space Tree :

Or
Source Code :

jugY(Y, Vy, Yc) :- ( Y =:= 0

-> Yc is Vy; % Yc is the new volume of jug Y

Yc is Y

).

jugX(X, Vx, Xc) :- ( X =:= Vx

-> Xc is 0; % Xc is the new volume of jug X

Xc is X

).

pouring(Y, X, Vx, K, Xc, Yc) :- ( Y =\= 0, X < Vx

-> K is min(Y, Vx - X)

, Xc is X + K
, Yc is Y - K

).

do(Vy, Vx, Y, X, Yc, Xc, K) :-

jugY(Y, Vy, Yc);

jugX(X, Vx, Xc);

pouring(Yc, Xc, Vx, K, Xc1, Yc1).

loop(Vy, Vx, Y, X, Z, Yc, Xc, K) :-

( X =\= Z, Y =\= Z ),

jugY(Y, Vy, Yc);

jugX(X, Vx, Xc);

pouring(Yc, Xc, Vx, K, Xc1, Yc1),

loop(Vy, Vx, Y, X, Z, Yc, Xc, K).

You might also like