0% found this document useful (0 votes)
39 views16 pages

AI Practical MANUAL 2023

Uploaded by

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

AI Practical MANUAL 2023

Uploaded by

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

Subject: Artificial Intelligence

Practical - 1

Write a PROLOG program for Family Relationship.

MOTILAL (SWARUPMATI)

JAVAHARLAL VIJYALAKSHMI KRISHNA


(KAMLA) (RANJIT)

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

Write a PROLOG program to calculate the roots of quadratic equation


Consider all possibilities real, equal, imaginary).

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)
;

REAL is (-B) / (2*A),


IMG is sqrt(-D) / (2*A),
write('Real root is : '),
write(REAL),nl,
write('Imaginary root is : '),
write(IMG).
Subject: Artificial Intelligence

OUTPUT:-

1 ?- run

Enter the value of A :5.


Enter the value of B :3.
Enter the value of C :2.
Real root is : -0.3
Imaginary root is : 0.5567764362830021
true
.
Subject: Artificial Intelligence

Practical – 3

Write a Prolog program to implement Tower Of Hanoi Problem.

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

Write a program to solve Water-Jug Problem.

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

Write a PROLOG program for monkey banana problem.

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

Write a program to solve 8 puzzle problem.

goal(1/2/3/8/0/4/7/6/5).

left( A/0/C/D/E/F/H/I/J , 0/A/C/D/E/F/H/I/J ).


left( A/B/C/D/0/F/H/I/J , A/B/C/0/D/F/H/I/J ).
left( A/B/C/D/E/F/H/0/J , A/B/C/D/E/F/0/H/J ).
left( A/B/0/D/E/F/H/I/J , A/0/B/D/E/F/H/I/J ).
left( A/B/C/D/E/0/H/I/J , A/B/C/D/0/E/H/I/J ).
left( A/B/C/D/E/F/H/I/0 , A/B/C/D/E/F/H/0/I ).

up( A/B/C/0/E/F/H/I/J , 0/B/C/A/E/F/H/I/J ).


up( A/B/C/D/0/F/H/I/J , A/0/C/D/B/F/H/I/J ).
up( A/B/C/D/E/0/H/I/J , A/B/0/D/E/C/H/I/J ).
up( A/B/C/D/E/F/0/I/J , A/B/C/0/E/F/D/I/J ).
up( A/B/C/D/E/F/H/0/J , A/B/C/D/0/F/H/E/J ).
up( A/B/C/D/E/F/H/I/0 , A/B/C/D/E/0/H/I/F ).

right( A/0/C/D/E/F/H/I/J , A/C/0/D/E/F/H/I/J ).


right( A/B/C/D/0/F/H/I/J , A/B/C/D/F/0/H/I/J ).
right( A/B/C/D/E/F/H/0/J , A/B/C/D/E/F/H/J/0 ).
right( 0/B/C/D/E/F/H/I/J , B/0/C/D/E/F/H/I/J ).
right( A/B/C/0/E/F/H/I/J , A/B/C/E/0/F/H/I/J ).
right( A/B/C/D/E/F/0/I/J , A/B/C/D/E/F/I/0/J ).

down( A/B/C/0/E/F/H/I/J , A/B/C/H/E/F/0/I/J ).


down( A/B/C/D/0/F/H/I/J , A/B/C/D/I/F/H/0/J ).
down( A/B/C/D/E/0/H/I/J , A/B/C/D/E/J/H/I/0 ).
down( 0/B/C/D/E/F/H/I/J , D/B/C/0/E/F/H/I/J ).
down( A/0/C/D/E/F/H/I/J , A/E/C/D/0/F/H/I/J ).
down( A/B/0/D/E/F/H/I/J , A/B/F/D/E/0/H/I/J ).

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

a(A,Pa), b(B,Pb), c(C,Pc),


d(D,Pd), e(E,Pe), f(F,Pf),
g(G,Pg), h(H,Ph), i(I,Pi),
P is Pa+Pb+Pc+Pd+Pe+Pf+Pg+Ph+Pg+Pi.

a(0,0). a(1,0). a(2,1). a(3,2). a(4,3). a(5,4). a(6,3). a(7,2). a(8,1).


b(0,0). b(1,1). b(2,0). b(3,1). b(4,2). b(5,3). b(6,2). b(7,3). b(8,2).
c(0,0). c(1,2). c(2,1). c(3,0). c(4,1). c(5,2). c(6,3). c(7,4). c(8,3).
d(0,0). d(1,1). d(2,2). d(3,3). d(4,2). d(5,3). d(6,2). d(7,2). d(8,0).
e(0,0). e(1,2). e(2,1). e(3,2). e(4,1). e(5,2). e(6,1). e(7,2). e(8,1).
f(0,0). f(1,3). f(2,2). f(3,1). f(4,0). f(5,1). f(6,2). f(7,3). f(8,2).
g(0,0). g(1,2). g(2,3). g(3,4). g(4,3). g(5,2). g(6,2). g(7,0). g(8,1).
h(0,0). h(1,3). h(2,3). h(3,3). h(4,2). h(5,1). h(6,0). h(7,1). h(8,2).
i(0,0). i(1,4). i(2,3). i(3,2). i(4,1). i(5,0). i(6,1). i(7,2). i(8,3).

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

Write a program to solve Travelling salesman problem.

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).

go:- write('Enter starting node : '),


read(City),
delete([aa,bb,cc,dd,ee],City,List1),
go1(City,List1,0,City).

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

AIM: Write a program to find Factorial and Fibonacci series.

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]).

even element in the list


true
Subject: Artificial Intelligence

PRACTICAL:-10

AIM: Write a Prolog program to find the maximum number from the three numbers.

CODE:

max(P,Q,R):-P>Q,P>R,write('Larger number is '),write(P).


max(P,Q,R):-P<Q,Q>R,write('Larger number is '),write(Q).
max(P,Q,R):-R>Q,P<R,write('Larger number is '),write(R).
max(P,Q,R):-P=Q,P<R,write('Larger number is '),write(R).
max(P,Q,R):-P<Q,P=R,write('Larger number is '),write(Q).
max(P,Q,R):-Q=R,P>Q,write('Larger number is '),write(P).
max(P,Q,R):-P=Q,P>R,write('Larger numbers are '),write(P),write(' and '),write(Q).
max(P,Q,R):-P=R,Q<R,write('Larger numbers are '),write(P),write(' and '),write(R).
max(P,Q,R):-Q=R,P<R,write('Larger numbers are '),write(R),write(' and '),write(Q).
max(P,Q,R):-P=Q,P=R,write('All numbers are equal ').

OUTPUT:
?- max(1,2,3).
Larger number is 3
true

You might also like