Ai Final
Ai Final
(VII SEMESTER)
ARTIFICIAL INTELLIGENCE
Subject Code: 3170716
Lab Manual
Branch: CE
7thSemester
Semester………………and ...........................................................................Branch
......................................................................
Approved By Principal
SHREE SWAMINARAYAN INSTITUTE OF TECHNOLOGY-GANDHINAGAR
STUDENT NAME:
INDEX
Sr. Details of Work Date Sign of
No. Faculty
Date of Submission:
Practical - 1
Write a PROLOG program for Family Relationship.
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').
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):-
Subject: Artificial Intelligence Subject Code: 3170716
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),
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):-
Subject: Artificial Intelligence Subject Code: 3170716
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);
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 Subject Code: 3170716
MOTILAL (SWARUPMATI)
INDIRA
(FIROZ)
TARA LEKHA RITA
SANJAY RAJIV
(MENKA) (SONIA)
VARUN
RAHUL PRIYANKA
Subject: Artificial Intelligence Subject Code: 3170716
Practical- 2
symptom(charlie,fever).
symptom(charlie,rash).
symptom(charlie,headache).
symptom(charlie,runny_nose).
symptom(Mack,rash).
symptom(Mack,headache).
hypothesis(Patient,german_measles):-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,runny_nose),
symptom(Patient,rash).
hypothesis(Patient,flu):-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,body_ache),
symptom(Patient,cough).
OUTPUT:-
3 ?- symptom(X,'fever').
X = charlie ;
True
Subject: Artificial Intelligence Subject Code: 3170716
Practical - 3
Write a program of map coloring.
adjacent(1,2). adjacent(2,1).
adjacent(1,3). adjacent(3,1).
adjacent(1,4). adjacent(4,1).
adjacent(1,5). adjacent(5,1).
adjacent(2,3). adjacent(3,2).
adjacent(2,4). adjacent(4,2).
adjacent(3,4). adjacent(4,3).
adjacent(4,5). adjacent(5,4).
color(1,red,a). color(1,red,b).
color(2,blue,a). color(2,blue,b).
color(3,green,a). color(3,green,b).
color(4,yellow,a). color(4,blue,b).
color(5,blue,a). color(5,green,b).
conflict(Coloring) :-
adjacent(X,Y),
color(X,Color,Coloring),
color(Y,Color,Coloring).
conflict(R1,R2,Coloring) :-
adjacent(R1,R2),
color(R1,Color,Coloring),
color(R2,Color,Coloring).
OUTPUT:-
1 ?- adjacent(2,3).
true
2 ?- adjacent(5,3).
false
3 ?- adjacent(3,R).
R = 1 ;
R = 2 ;
R = 4 ;
false
Subject: Artificial Intelligence Subject Code: 3170716
Practical – 4
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 Subject Code: 3170716
Practical – 5
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.
Subject: Artificial Intelligence Subject Code: 3170716
Practical – 6
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),
close(X,Y).
close(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 Subject Code: 3170716
Practical–7
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 Subject Code: 3170716
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 Subject Code: 3170716
Practical–8
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 Subject Code: 3170716
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 Subject Code: 3170716
Practical–9
solve(State,Soln) :- f_function(State,0,F),
search([State#0#F#[]],S), reverse(S,Soln).
f_function(State,D,F) :- h_function(State,H),
F is D + H.
insert_all([F|R],Open1,Open3) :- insert(F,Open1,Open2),
insert_all(R,Open2,Open3).
insert_all([],Open,Open).
insert(B,Open,Open) :- repeat_node(B,Open), ! .
insert(B,[C|R],[B,C|R]) :- cheaper(B,C), ! .
insert(B,[B1|R],[B1|S]) :- insert(B,R,S), !.
insert(B,[],[B]).
repeat_node(P#_#_#_, [P#_#_#_|_]).
expand(State#D#_#S,All_My_Children) :-
bagof(Child#D1#F#[Move|S],
(D1 is D+1,
move(State,Child,Move),
f_function(Child,D1,F)),
All_My_Children).
OUTPUT:
1 ?- solve(0/8/1/2/4/3/7/6/5, S).
S = [right, right, down, left, left, up, right, down]
Subject: Artificial Intelligence Subject Code: 3170716
Practical-10
DOMAINS
cell=c(integer,integer)
list=cell*
int_list=integer*
PREDICATES
solution(list)
member(integer,int_list)
nonattack(cell,list)
CLAUSES
solution([]).
solution([c(X,Y)|Others]):-
solution(Others),
member(Y,[1,2,3,4,5,6,7,8]),
nonattack(c(X,Y),Others).
nonattack(_,[]).
nonattack(c(X,Y),[c(X1,Y1)|Others]):-
Y<>Y1,
Y1-Y<>X1-X,
Y1-Y<>X-X1,
nonattack(c(X,Y),Others).
member(X,[X|_]).
member(X,[_|Z]):-
member(X,Z).
GOAL
solution([c(1,A),c(2,B),c(3,C),c(4,D),c(5,E),c(6,F),c(7,G),c(8,H)]).
Subject: Artificial Intelligence Subject Code: 3170716
OUTPUT:-
A=4, B=2, C=7, D=3, E=6, F=8, G=5, H=1
A=5, B=2, C=4, D=7, E=3, F=8, G=6, H=1
A=3, B=5, C=2, D=8, E=6, F=4, G=7, H=1
A=3, B=6, C=4, D=2, E=8, F=5, G=7, H=1
A=5, B=7, C=1, D=3, E=8, F=6, G=4, H=2
A=4, B=6, C=8, D=3, E=1, F=7, G=5, H=2
A=3, B=6, C=8, D=1, E=4, F=7, G=5, H=2
A=5, B=3, C=8, D=4, E=7, F=1, G=6, H=2
A=5, B=7, C=4, D=1, E=3, F=8, G=6, H=2
A=4, B=1, C=5, D=8, E=6, F=3, G=7, H=2
A=3, B=6, C=4, D=1, E=8, F=5, G=7, H=2
A=4, B=7, C=5, D=3, E=1, F=6, G=8, H=2
A=6, B=4, C=2, D=8, E=5, F=7, G=1, H=3
A=6, B=4, C=7, D=1, E=8, F=2, G=5, H=3
92 solutions….
Explanation: