Practical Journal (Year: 2024-2025)
Course: Masters of Computer Application (2020 Revised Course)
Semester: II
Subject: Artificial Intelligence
Subject Code: 2103
Faculty: Mrs. Shaesta Mujawar
Name: Neelam Savardekar
Roll No. 47
1
Certificate
This is certified to be the bonafide work of Neelam
Savardekar in the ARTIFICIAL INTILLIGENCE lab
during the academic year 2024-2025.
Signature of Internal Examiner Signature of External Examiner
2
INDEX
Sr.No Date Practical Sign
1 4-02-25 Practical 1
WAP for Usage of rules in Prolog. Create a family tree
program (of EXP2) to include following rules -M is the
mother of P if she is a parent of P and is female
F is the father of P if he is a parent of P and is male X is a
sibling of Y if they both have the same parent. Then add
rules for grandparents, uncle-aunt, sister and brother. Based
on the facts, define goals to answer questions related to
family tree
2 11-02-25 Practical 2
Write predicates One converts centigrade temperatures to
Fahrenheit, the other checks if a temperature is below
freezing.
3 18-02-25 Practical 3
Write a program to solve the Monkey Banana problem.
4 Practical 4
WAP in turbo prolog for medical diagnosis and showthe
advantage and disadvantage of green and red cuts.
5 11-02-25 Practical 5
WAP to implement Factorial, Fibonacci of a given number.
6 25-02-25 Practical 6
Write a program to solve 4-Queen problem.
7 04-02-25 Practical 7
Write a program to solve traveling salesman problem.
8 11-03-25 Practical 8
Write a program to solve water jug problem using LISP.
3
9 Practical 9
Trace the steps involved in concluding “Seema is
talkative” from the facts “All girls are talkative” and
“Seem is a girl”.
10 18-03-25 Practical 10
Write a program to solve Missionaries and CannibalsProblem
11 25-03-25 Practical 11
Write a program to solve Tower of Hanoi Problem
12 01-04-25 Practical 12
Write a program to solve Tic Tac Toe Problem
13 08-04-25 Practical 13
Write a program to solve Cryptarithmetic Problem SEND +
MORE = MONEY.
14 08-04-25 Practical 14
Write a program to solve Cryptarithmetic Problem BASE +
BALL = GAMES.
15 11-04-25 Practical 15
Write a program to solve 8-queen Problem
4
PRACTICAL 1
Aim: WAP for Usage of rules in Prolog. Create a family tree program
(ofEXP2) to include following rules-
M is the mother of P if she is a parent of P and is female
F is the father of P if he is a parent of P and is male
X is a sibling of Y if they both have the same parent.
Then add the rules for grandparents, uncle-aunt, sister and brother.
Basedon the facts, define goals to answer questions related to family tree.
Code:
female(lisa).
female(mary).
female(jesse).
female(kelly).
female(beth).
female(olivia).
female(sarah).
female(stacy).
male(john).
male(tom).
male(joe).
male(tim).
male(james).
male(harry).
male(dan).
male(nick).
parent(lisa,tom).
5
parent(john,tom).
parent(lisa,jesse).
parent(john,jesse).
parent(mary,tim).
parent(james,tim).
parent(mary,dan).
parent(james,dan).
parent(kelly,beth).
parent(harry,beth).
parent(kelly,olivia).
parent(harry,olivia).
parent(sarah,stacy).
parent(nick,stacy).
married(lisa,john).
married(mary,james).
married(kelly,hary).
married(sarah,nick).
mother(X,Y):-parent(X,Y),female(X).
father(X,Y):-parent(X,Y),male(X).
haschild(X):-parent(X,_).
sister(X,Y):-parent(Z,X),parent(Z,Y),female(X),X\==Y.
brother(X,Y):-parent(Z,X),parent(Z,Y),male(X),X\==Y.
6
Output:
7
PRACTICAL 2
Aim: Write predicates One converts centigrade temperatures to
Fahrenheit,the other checks if a temperature is below freezing.
Code:
c_to_f(C,F):- F is C*9/5+32.
f_to_c(F,C):- C is (F-32)*5/9.
freezing(F):- F=<32.
Output:
8
PRACTICAL 3
Aim: Write a program to solve the Monkey Banana problem.
Code:
on(floor,monkey).
on(floor,box).
in(room,monkey).
in(room,box).
in(room,banana).
at(ceiling,banana).
strong(monkey).
grasp(monkey).
climb(monkey,box).
push(monkey,box) :- strong(monkey).
under(banana,box) :- push(monkey,box).
canreach(banana, monkey) :- on(floor,box), at(ceiling,banana), under(banana,box),
climb(monkey,box).
canget(banana, monkey) :- canreach(banana,monkey), grasp(monkey).
Output:
9
PRACTICAL 4
Aim: WAP in turbo prolog for medical diagnosis and show the advantage
and disadvantage of green and red cuts.
Code:
:- dynamic yes/1, no/1.
check:-
checkfor(Disease),
write(' I Believe you Have '),
write(Disease),
nl,
undo.
checkfor(cold):- cold.
cold :-
checksymptom(fever),
checksymptom(cough),
checksymptom(headache).
checksymptom(Symptom):-
( yes(Symptom) ->
true
; no(Symptom) ->
fail
10
; askquestion(Symptom)
).
askquestion(Question):-
write(' Do you have the Symptom '),
write(Question),
write('? (yes/no)'),
read(Reply),
nl,
( Reply == yes ; Reply == y ->
assert(yes(Question))
; assert(no(Question)), fail
).
undo:-
retract(yes(_)), fail.
undo:-
retract(no(_)), fail.
undo.
Output:
11
PRACTICAL 5
Aim: WAP to implement factorial, Fibonacci of a given number.
For factorial:
Code:
factorial(0,1).
factorial(N,F):- N>0,
N1 is N-1,
factorial(N1,F1), F is N*F1.
Output:
For fibanacci:
fib(0,0).
fib(X,Y):- X>0,
fib(X,Y,_).
fib(1,1,0).
fib(X,Y1,Y2):- X>1,
X1 is X-1,
fib(X1,Y2,Y3),
Y1 is Y2 + Y3.
12
Output:
13
PRACTICAL 6
Aim: Write a program to solve 4-Queen problem.
Code:
queens(N,Qs):-range(1,N,Us),
queens(Us,[],Qs).
queens([],Qs,Qs).
queens(Us,Ps,Qs):-select(Q,Us,Us1),\+attack(Q,Ps),queens(Us1,[Q|Ps],Qs).
range(J,J,[J]).
range(I,J,[I|Ns]):-I<J,I1 is I + 1,range(I1,J,Ns).
attack(Q,Qs):-attack(Q,1,Qs).
attack(X,N,[Y|_]):- X is Y + N.
attack(X,N,[Y|_]):- X is Y - N.
attack(X,N,[_|Ys]):-N1 is N + 1, attack(X, N1, Ys).
go:-queens(8,Qs),write(Qs).
Output:
14
PRACTICAL 8
Aim: Write a program to solve water jug problem using LISP.
Code:
water_jug(X,Y):-X>4,Y<3,write('4L water jug overflowed.'),nl.
water_jug(X,Y):-X<4,Y>3,write('3L water jug overflowed.'),nl.
water_jug(X,Y):-X>4,Y>3,write('Both the water jugs overflowed.'),nl.
water_jug(X,Y):-(X=:=0,Y=:=0,nl,write('4L:0 & 3L:3 (Action: Fill 3L jug.)'),YY is 3,
water_jug(X,YY));
(X=:=0,Y=:=0,nl,write('4L:4 & 3L:0 (Action: Fill 4L jug.)'),XX is 4, water_jug(XX,Y));
(X=:=2,Y=:=0,nl,write('4L:2 & 3L:0 (Action: Goal State Reached...)'));
(X=:=4,Y=:=0,nl,write('4L:1 & 3L:3 (Action: Pour Water from 4L jug to 3L jug.)'),XX is X-
3,YY is 3,water_jug(XX,YY));
(X=:=0,Y=:=3,nl,write('4L:3 & 3L:0 (Action: Pour Water from 3L jug to 4L jug.)'),XX is
3,YY is 0,water_jug(XX,YY));
(X=:=1,Y=:=3,nl,write('4L:1 & 3L:0 (Action: Empty 3L jug.)'),YY is 0,water_jug(X,YY));
(X=:=3,Y=:=0,nl,write('4L:3 & 3L:3 (Action: Fill 3L jug.)'),YYis 3,water_jug(X,YY));
(X=:=3,Y=:=3,nl,write('4L:4 & 3L:2 (Action: Pour Water from 3L jug to 4L jug until 4L jug
is full.)'),XX is X+1,YY is Y-1,water_jug(XX,YY));
(X=:=1,Y=:=0,nl,write('4L:0 & 3L:1 (Action: Pour Water from 4L jug to 3L jug.)'),XX is
Y,YY is X,water_jug(XX,YY));
(X=:=0,Y=:=1,nl,write('4L:4 & 3L:1 (Action: Fill 4L jug.)'),XX is 4,water_jug(XX,Y));
(X=:=4,Y=:=1,nl,write('4L:2 & 3L:3 (Action: Pour Water from 4L jug to 3L jug until 3L jug
is full.)'),XX is X-2,YY is Y+2,water_jug(XX,YY));
(X=:=2,Y=:=3,nl,write('4L:2 & 3L:0 (Action: Empty 3L jug.)'),YY is 0,water_jug(X,YY));
(X=:=4,Y=:=2,nl,write('4L:0 & 3L:2 (Action: Empty 4L jug.)'),XX is
0,water_jug(XX,Y));
15
(X=:=0,Y=:=2,nl,write('4L:2 & 3L:0 (Action: Pour Water from 3L jug to 4L jug.)'),XX is
Y,YY is X,water_jug(XX,YY)).
Output:
16
PRACTICAL 9
Aim: Trace the steps involved in concluding “Seema is talkative” from the
facts “All girls are talkative” and “Seem is a girl”.
Code:
girl(seema).
talkitive(X):-girl(X).
Output:
17
PRACTICAL 10
Aim: Write a program to solve Missionaries and Cannibals Problem
Code:
% Main control block and printing
find :- path([3,3,left],[0,0,right],[[3,3,left]],_).
output([]) :- nl, nl.
output([[A,B,String]|T]) :- output(T),
write(B), write(' ~~ '), write(A), write(': '), write(String), nl.
% Base case
path([A,B,C],[A,B,C],_,MoveList):- nl,nl,output(MoveList).
% Recursive call to solve the problem
path([A,B,C],[D,E,F],Traversed,Moves) :- move([A,B,C],[I,J,K],Out), legal([I,J,K]),
% Don't use this move unless it's safe.
not(member([I,J,K],Traversed)), path([I,J,K],[D,E,F],[[I,J,K]|Traversed],[ [[I,J,K],[A,B,C],Out] |
Moves ]).
% Move commands and descriptions of the move
move([A,B,left],[C,B,right],'One missionary crosses the river') :- A > 0, C is A - 1.
move([A,B,left],[C,B,right],'Two missionaries cross the river') :- A > 1, C is A - 2.
move([A,B,left],[C,D,right],'One missionary and One cannibal cross the river') :- A > 0, B > 0,
C is A - 1, D is B - 1.
18
move([A,B,left],[A,D,right],'One cannibal crosses the river') :- B > 0, D is B - 1.
move([A,B,left],[A,D,right],'Two cannibals cross the river') :- B > 1, D is B - 2.
move([A,B,right],[C,B,left],'One missionary returns from the other side') :- A < 3, C is A + 1.
move([A,B,right],[C,B,left],'Two missionaries return from the other side') :- A < 2, C is A + 2.
move([A,B,right],[C,D,left],'One missionary and One cannibal return from the other side') :-
A < 3, B < 3, C is A + 1, D is B + 1.
move([A,B,right],[A,D,left],'One cannibal returns from the other side') :- B < 3, D is B + 1.
move([A,B,right],[A,D,left],'Two cannibals return from the other side') :- B < 2, D is B + 2.
% Legal move definition where B is missionaries and A is cannibals:
legal([B,A,_]) :-(A =< B ; B = 0), C is 3-A, D is 3-B, (C =< D; D = 0).
Output:
19
PRACTICAL 11
Aim: Write a program to solve Tower of Hanoi Problem
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:
20
PRACTICAL 12
Aim: Write a program to solve Tic Tac Toe Problem
Code:
% A Tic-Tac-Toe program in Prolog. S. Tanimoto, May 11, 2003.
% To play a game with the computer, type
% playo.
% To watch the computer play a game with itself, type
% selfgame.
% Predicates that define the winning conditions:
win(Board, Player) :- rowwin(Board, Player).
win(Board, Player) :- colwin(Board, Player).
win(Board, Player) :- diagwin(Board, Player).
rowwin(Board, Player) :- Board = [Player,Player,Player,_,_,_,_,_,_].
rowwin(Board, Player) :- Board = [_,_,_,Player,Player,Player,_,_,_].
rowwin(Board, Player) :- Board = [_,_,_,_,_,_,Player,Player,Player].
colwin(Board, Player) :- Board = [Player,_,_,Player,_,_,Player,_,_].
colwin(Board, Player) :- Board = [_,Player,_,_,Player,_,_,Player,_].
colwin(Board, Player) :- Board = [_,_,Player,_,_,Player,_,_,Player].
diagwin(Board, Player) :- Board = [Player,_,_,_,Player,_,_,_,Player].
diagwin(Board, Player) :- Board = [_,_,Player,_,Player,_,Player,_,_].
% Helping predicate for alternating play in a "self" game:
other(x,o).
other(o,x).
game(Board, Player) :- win(Board, Player), !, write([player, Player, wins]).
game(Board, Player) :-
other(Player,Otherplayer),
move(Board,Player,Newboard),
!,
display(Newboard),
game(Newboard,Otherplayer).
move([b,B,C,D,E,F,G,H,I], Player, [Player,B,C,D,E,F,G,H,I]).
move([A,b,C,D,E,F,G,H,I], Player, [A,Player,C,D,E,F,G,H,I]).
21
move([A,B,b,D,E,F,G,H,I], Player, [A,B,Player,D,E,F,G,H,I]).
move([A,B,C,b,E,F,G,H,I], Player, [A,B,C,Player,E,F,G,H,I]).
move([A,B,C,D,b,F,G,H,I], Player, [A,B,C,D,Player,F,G,H,I]).
move([A,B,C,D,E,b,G,H,I], Player, [A,B,C,D,E,Player,G,H,I]).
move([A,B,C,D,E,F,b,H,I], Player, [A,B,C,D,E,F,Player,H,I]).
move([A,B,C,D,E,F,G,b,I], Player, [A,B,C,D,E,F,G,Player,I]).
move([A,B,C,D,E,F,G,H,b], Player, [A,B,C,D,E,F,G,H,Player]).
display([A,B,C,D,E,F,G,H,I]) :- write([A,B,C]),nl,write([D,E,F]),nl,
write([G,H,I]),nl,nl.
selfgame :- game([b,b,b,b,b,b,b,b,b],x).
% Predicates to support playing a game with the user:
x_can_win_in_one(Board) :- move(Board, x, Newboard), win(Newboard, x).
% The predicate orespond generates the computer's (playing o) reponse
% from the current Board.
orespond(Board,Newboard) :-
move(Board, o, Newboard),
win(Newboard, o),
!.
orespond(Board,Newboard) :-
move(Board, o, Newboard),
not(x_can_win_in_one(Newboard)).
orespond(Board,Newboard) :-
move(Board, o, Newboard).
orespond(Board,Newboard) :-
not(member(b,Board)),
!,
write('Cats game!'), nl,
Newboard = Board.
% The following translates from an integer description
% of x's move to a board transformation.
xmove([b,B,C,D,E,F,G,H,I], 1, [x,B,C,D,E,F,G,H,I]).
xmove([A,b,C,D,E,F,G,H,I], 2, [A,x,C,D,E,F,G,H,I]).
xmove([A,B,b,D,E,F,G,H,I], 3, [A,B,x,D,E,F,G,H,I]).
xmove([A,B,C,b,E,F,G,H,I], 4, [A,B,C,x,E,F,G,H,I]).
22
xmove([A,B,C,D,b,F,G,H,I], 5, [A,B,C,D,x,F,G,H,I]).
xmove([A,B,C,D,E,b,G,H,I], 6, [A,B,C,D,E,x,G,H,I]).
xmove([A,B,C,D,E,F,b,H,I], 7, [A,B,C,D,E,F,x,H,I]).
xmove([A,B,C,D,E,F,G,b,I], 8, [A,B,C,D,E,F,G,x,I]).
xmove([A,B,C,D,E,F,G,H,b], 9, [A,B,C,D,E,F,G,H,x]).
xmove(Board, N, Board) :- write('Illegal move.'), nl.
% The 0-place predicate playo starts a game with the user.
playo :- explain, playfrom([b,b,b,b,b,b,b,b,b]).
explain :-
write('You play X by entering integer positions followed by a period.'),
nl,
display([1,2,3,4,5,6,7,8,9]).
playfrom(Board) :- win(Board, x), write('You win!').
playfrom(Board) :- win(Board, o), write('I win!').
playfrom(Board) :- read(N),
xmove(Board, N, Newboard),
display(Newboard),
orespond(Newboard, Newnewboard),
display(Newnewboard),
playfrom(Newnewboard).
Output:-
23
PRACTICAL 13
Aim: Write a program to solve Cryptarithmetic Problem SEND + MORE = MONEY
Code:
:- use_module(library(clpfd)).
% Define the puzzle
puzzle_solution(Digits) :-
Digits = [S, E, N, D, M, O, R, Y],
% Digits from 0 to 9
Digits ins 0..9,
% Constraints for the letters
all_different(Digits),
% Form the numbers
S #\= 0, M #\= 0,
1000*S + 100*E + 10*N + D +
1000*M + 100*O + 10*R + E #=
10000*M + 1000*O + 100*N + 10*E + Y,
% Search for a solution
label(Digits).
% Entry point to solve the puzzle
solve :-
puzzle_solution([S, E, N, D, M, O, R, Y]),
write('S = '), write(S), nl,
write('E = '), write(E), nl,
write('N = '), write(N), nl,
24
write('D = '), write(D), nl,
write('M = '), write(M), nl,
write('O = '), write(O), nl,
write('R = '), write(R), nl,
write('Y = '), write(Y), nl.
Output:
25
PRACTICAL 14
Aim: Write a program to solve Cryptarithmetic Problem BASE + BALL = GAMES
Code:
:- use_module(library(clpfd)).
% Define the puzzle
puzzle_solution(Digits) :-
Digits = [B, A, S, E, L, G, M],
% Digits from 0 to 9
Digits ins 0..9,
% Constraints for the letters
all_different(Digits),
% Form the numbers
B #\= 0, G #\= 0,
1000*B + 100*A + 10*S + E +
1000*B + 100*A + 10*L + L #=
10000*G + 1000*A + 100*M + 10*E + S,
% Search for a solution
label(Digits).
% Entry point to solve the puzzle
solve :-
puzzle_solution([B, A, S, E, L, G, M]),
write('B = '), write(B), nl,
write('A = '), write(A), nl,
write('S = '), write(S), nl,
26
write('E = '), write(E), nl,
write('L = '), write(L), nl,
write('G = '), write(G), nl,
write('M = '), write(M), nl.
Output:
27
PRACTICAL 15
Aim: Write a program to solve 8-queen Problem
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:
28