AI Practical MANUAL 2023
AI Practical MANUAL 2023
Practical - 1
MOTILAL (SWARUPMATI)
INDIRA
(FIROZ)
TARA LEKHA RITA
SANJAY RAJIV
(MENKA) (SONIA)
VARUN
RAHUL PRIYANKA
father('Motilal','Jawaharlal').
father('Motilal','Vijayalakshmi').
father('Motilal','Krishna').
father('Jawaharlal','Indira').
father('Ranjit','Tara').
father('Ranjit','Lekha').
father('Ranjit','Rita').
father('Feroz','Sanjay').
father('Feroz','Rajiv').
father('Sanjay','Varun').
father('Rajiv','Rahul').
father('Rajiv','Priyanka').
Subject: Artificial Intelligence
wife_of('Swaruprani','Motilal').
wife_of('Kamla','Jawaharlal').
wife_of('Vijayalakshmi','Ranjit').
wife_of('Indira','Feroz').
wife_of('Maneka','Sanjay').
wife_of('Sonia','Rajiv').
female('Krishna').
female('Priyanka').
female('Lekha').
female('Tara').
female('Rita').
female(X) :-
wife_of(X,_).
male('Varun').
male('Rahul').
male(X) :-
husband_of(X,_).
husband_of(X,Y):-
wife_of(Y,X).
mother(X,Y):-
wife_of(X,Z),
father(Z,Y).
parent(X,Y):-
father(X,Y);
mother(X,Y).
child(X,Y):-
parent(Y,X).
son(X,Y):-
child(X,Y),
male(X).
daughter(X,Y):-
child(X,Y),
female(X).
brother(X,Y):-
father(Z,X),
father(Z,Y),
Subject: Artificial Intelligence
male(X),
not(X=Y).
sister(X,Y):-
father(Z,X),
father(Z,Y),
female(X),
not(X=Y).
uncle(X,Y):-
parent(Z,Y),
brother(X,Z);
parent(Z,Y),
sister(S,Z),
husband_of(X,S).
aunt(X,Y):-
sister(X,Z),
parent(Z,Y).
aunt(X,Y):-
wife_of(X,Z),
uncle(Z,Y).
ancestor(X,Y):-
parent(X,Y).
ancestor(X,Y):-
parent(Z,Y),
ancestor(X,Z).
grand_father(X,Y):-
parent(X,Z),
parent(Z,Y),
male(X).
grand_mother(X,Y):-
parent(X,Z),
parent(Z,Y),
female(X).
cousin(X,Y):-
parent(Z,X),
parent(W,Y),
brother(Z,W);
Subject: Artificial Intelligence
parent(Z,X);
parent(W,Y),
sister(Z,W).
nephew(X,Y):-
male(X),
uncle(Y,X);
male(X),
aunt(Y,X).
niece(X,Y):-
female(X),
uncle(Y,X);
female(X),
aunt(Y,X).
OUTPUT:-
1 ?- female(X).
X = 'Krishna' .
2 ?- mother(X,Y).
X = ‘Swaruprani’,
Y=’ Jawaharlal’
Subject: Artificial Intelligence
Practical - 2
run:-
write('Enter the value of A :' ),
read(A),
write('Enter the value of B :' ),
read(B),
write('Enter the value of C :' ),
read(C),
D is (B*B)-(4*A*C),
root(A,B,C,D).
root(A,B,C,D):-
A is 0.0,
write('Only one root exists.'),
ANS is (-C/B),
write(ANS)
;
D>=0,
E is sqrt(D),
ANS is (-B - E) / (2*A),
ANS1 is (-B + E) / (2*A),
write('First root is : '),
write(ANS),nl,
write('Second root is : '),
write(ANS1)
;
OUTPUT:-
1 ?- run
Practical – 3
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:-
1 ?- move(2,x,y,z).
Move top disk from x to z
Move top disk from x to y
Move top disk from z to y
true
Subject: Artificial Intelligence
Practical – 4
jug(2, _).
jug(0, 2):- write('(0, 2)'), nl,
write('(2, 0)'), nl.
jug(4, 0):- write('(4, 0)'), nl,
jug(0, 0).
jug(4, 3):- write('(4, 3)'), nl,
jug(0, 0).
jug(3, 0):- write('(3, 0)'), nl,
jug(3, 3).
jug(X, 0):- write('('),write(X), write(', 0)'), nl,
jug(0, 3).
jug(0, 3):- write('(0, 3)'), nl,
jug(3, 0).
jug(0, X):- write('(0, '),write(X),write(')'), nl,
jug(0, 0).
jug(3, 3):- write('(3, 3)'), nl,
jug(4, 2).
jug(4, 2):- write('(4, 2)'), nl,
write('(2, 0)'), nl,
jug(2, 0).
jug(X, Y):- X>4, fail,
Y>3, fail.
OUTPUT:
?- jug(4,3).
(4, 3)
(0, 0)
(0, 3)
(3, 0)
(3, 3)
(4, 2)
(2, 0)
true.
Practical–5
Subject: Artificial Intelligence
in_room(bananas).
in_room(chair).
in_room(monkey).
dexterous (monkey).
tall(chair).
can_move(monkey,chair,bananas).
can_climb(monkey,chair).
can_reach(X,Y):-
dexterous(X),
clos(X,Y).
clos(X,Z):-
get_on(X,Y),
under(Y,Z),
tall(Y).
get_on(X,Y):-
can_climb(X,Y).
under(Y,Z):-
in_room(X),
in_room(Y),
in_room(Z),
can_move(X,Y,Z).
OUTPUT:
1 ?- can_reach(monkey,bananas).
true.
Subject: Artificial Intelligence
Practical–6
goal(1/2/3/8/0/4/7/6/5).
h_function(Puzz,H) :- p_fcn(Puzz,P),
s_fcn(Puzz,S),
H is P + 3*S.
move(P,C,left) :- left(P,C).
move(P,C,up) :- up(P,C).
move(P,C,right) :- right(P,C).
move(P,C,down) :- down(P,C).
p_fcn(A/B/C/D/E/F/G/H/I, P) :-
Subject: Artificial Intelligence
s_fcn(A/B/C/D/E/F/G/H/I, S) :-
s_aux(A,B,S1), s_aux(B,C,S2), s_aux(C,F,S3),
s_aux(F,I,S4), s_aux(I,H,S5), s_aux(H,G,S6),
s_aux(G,D,S7), s_aux(D,A,S8), s_aux(E,S9),
S is S1+S2+S3+S4+S5+S6+S7+S8+S9.
s_aux(0,0) :- !.
s_aux(_,1).
s_aux(X,Y,0) :- Y is X+1, !.
s_aux(8,1,0) :- !.
s_aux(_,_,2).
OUTPUT:
p_fcn(A/B/D/C/E/F/G/H/I, P).
A = B, B = D, D = C, C = E, E =0 F, F = G, G = H, H = I, I = P, P = 0
Subject: Artificial Intelligence
Practical–7
city(bb).
city(cc).
city(dd).
city(ee).
dist(aa,bb,14).
dist(aa,cc,12).
dist(aa,dd,32).
dist(aa,ee,28).
dist(bb,aa,14).
dist(bb,cc,29).
dist(bb,dd,31).
dist(bb,ee,20).
dist(cc,aa,12).
dist(cc,bb,29).
dist(cc,dd,22).
dist(cc,ee,27).
dist(dd,aa,32).
dist(dd,bb,21).
dist(dd,cc,22).
dist(dd,ee,7).
dist(ee,aa,28).
dist(ee,bb,20).
dist(ee,cc,27).
dist(ee,dd,7).
go1(City,[],Totaldist,C):-
dist(C,City,D),
TT is Totaldist + D,
write('Optimal Distance = '),
write(TT).
Subject: Artificial Intelligence
go1(City,List,Totaldist,C):-
chkdist(City,List,City,999,AA,Dist),
write('Next node = '),write(AA),nl,
TT is Totaldist + Dist,
delete(List,AA,List1),
go1(AA,List1,TT,C).
chkdist(_,[],Next,X,Node,Dist):-
duplicate_term(Next,Node),
duplicate_term(X,Dist).
chkdist(C,[H|T],Next,D,AA,Dist):-
dist(C,H,X),
X < D,chkdist(C,T,H,X,AA,Dist);
chkdist(C,T,Next,D,AA,Dist).
OUTPUT:
1 ?- go.
Enter starting node : cc.
Next node = aa
Next node = bb
Next node = ee
Next node = dd
Optimal Distance = 75
True
Subject: Artificial Intelligence
PRACTICAL: 8
CODE:
factorial(0,1).
factorial(N,M) :-
N>0,
N1 is N-1,
factorial(N1, M1),
M is N*M1.
OUTPUT:
?- factorial(5,R).
R=120
CODE:
fib(0,0).
fib(1,1).
fib(N,F) :-
N > 1,N1 is N-1,N2 is N-2,
fib(N1,F1),
fib(N2,F2),
F is F1 + F2.
OUTPUT:
?- fib(6,F).
F=8
PRACTICAL: 9
Subject: Artificial Intelligence
AIM: Write a Prolog program to count even and odd elements from list and count elements
up to specific index in list.
evenlength:-
write('even element in the list').
oddlength:-
write('odd elements in the list').
oddeven([H|T]):-
length(T,L),
L>=0 ->
(
L1 is L+1,
L2 is mod(L1,2),
L2 =:=0 ->
evenlength
;
oddlength
).
OUTPUT :
? oddeven([1,2,3,4]).
PRACTICAL:-10
AIM: Write a Prolog program to find the maximum number from the three numbers.
CODE:
OUTPUT:
?- max(1,2,3).
Larger number is 3
true