0% found this document useful (0 votes)
8 views19 pages

Ai Final

Uploaded by

ola moviess
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)
8 views19 pages

Ai Final

Uploaded by

ola moviess
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/ 19

Shree Swaminarayan Institute of Technology CE DEPT.

(VII SEMESTER)

ARTIFICIAL INTELLIGENCE
Subject Code: 3170716

Lab Manual
Branch: CE
7thSemester

Shree Swaminarayan Institute of Technology


Sardar Patel Ring Road Circle,Gandhinagar to
Ahmedabad Airport Highway, Bhat,
Gandhinagar- 382428, Gujarat (INDIA)
Shree Swaminarayan Institute of Technology CE DEPT. (VII SEMESTER)

Satsang Shiksha Parishad


Shree Swaminarayan Institute of Technology

This is to Certify that Mr./Ms……………………………………………………… Of

Semester………………and ...........................................................................Branch

......................................................................

Academic year of 20_ 20

Subject In-charge Head of the Department

Approved By Principal
SHREE SWAMINARAYAN INSTITUTE OF TECHNOLOGY-GANDHINAGAR

CONTINUOUS ASSESSMENT SHEET-PRACTICAL

STUDENT NAME:

ENROLLMENT NO: BRANCH:COMPUTER ENGINEERING

SEMESTER: 7th SUBJECT CODE:3170716 SUBJECT: ARTIFICIAL INTELLIGENCE

INDEX
Sr. Details of Work Date Sign of
No. Faculty

1 Write a PROLOG program for Family


Relationship.
2 Write a PROLOG program for diagnosis
the childhood diseases.
3 Write a program of map coloring

4 Write a Turbo program to implement


Tower Of Hanoi Problem.
5 Write a program to solve Water-Jug
Problem.
6 Write a Prolog program for monkey
banana problem.
7 Write a program to solve 8 puzzle
problem.
8 Write a program to solve Travelling
salesman problem.
9 Write a program to implement A*
algorithm.
10 Write a program to solve N-Queens
problem using Prolog.
TOTAL

Date of Submission:

Subject In-charge HOD Sign


Subject: Artificial Intelligence Subject Code: 3170716

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)

JAVAHARLAL VIJYALAKSHM KRISHNA


(KAMLA) I
(RANJIT)

INDIRA
(FIROZ)
TARA LEKHA RITA

SANJAY RAJIV
(MENKA) (SONIA)

VARUN

RAHUL PRIYANKA
Subject: Artificial Intelligence Subject Code: 3170716

Practical- 2

Write a PROLOG program for diagnosis the childhood diseases.

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

Write a Turbo 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 Subject Code: 3170716

Practical – 5

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

Write a program to solve 8 puzzle problems.

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 Subject Code: 3170716

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 Subject Code: 3170716

Practical–8

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

Write a program to implement A* algorithm.

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.

search([State#_#_#Soln|_], Soln) :- goal(State).


search([B|R],S) :- expand(B,Children),
insert_all(Children,R,Open),
search(Open,S).

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

cheaper( _#_#F1#_ , _#_#F2#_ ) :- F1 < F2.

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

Write a program to solve N-Queens problem using Prolog.

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:

Eight queens problem is a constraint satisfaction problem. The task is to place


eight queens in the 64 available squares in such a way that no queen attacks each
other. So the problem can be formulated with variables x1,x2,x3,x4,x 5,x6,x7,x 8
and y1,y2,y3,y4,y5,y6, y7,y8; the xs represent the rows and ys the column. Now a
solution for this problem is to assign values for x and for y such that the constraint
is satisfied.
The problem can be formulated as
P={(x1,y1),(x2,y2), .................................. (x8,y8)}
where (x1,y1) gives the position of the first queen and so on. So it can be clearly
seen that the domains for xi and yi are
Dx = {1,2,3,4,5,6,7,8}and Dy ={1,2,3,4,5,6,7,8} respectively.

The constraints are


i. No two queens should be in the same row,
i.e yi≠yj for i=1 to 8;j=1 to 8;i≠j

ii. No two queens should be in the same


column, i.e xi≠xj for i=1 to 8;j=1 to 8;i≠j
iii. There should not be two queens placed on the same diagonal line
i.e (yi-yj) ≠ ±(xi-xj).

You might also like