0% found this document useful (0 votes)
5 views

Ai Programming

AI assignment

Uploaded by

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

Ai Programming

AI assignment

Uploaded by

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

INSTITUTE OF ENGINEERING &TECHNOLOGY

AGRA
LAB MANUAL OF ARTIFICIAL INTELLIGENCE.
BELONGS TO
MANVI GUPTA
OF
CSE 3RD YEAR
1709005371025
INDEX
S. NO. PROGRAM
1 Write a program to
implement simple facts
and rules.
2 Write a program to
implement family tree.
3 Write a program to
implement monkey
banana problem using
prolog.
4 Write a program to
implement I/O in
prolog.
5 Write a program to
implement towers of
Hanoi problem.
6 Write a program to find
the factorial of a
number using prolog.
7 Write a program to
implement water jug
problem.
8 :Write a program to
implement various
predicates on List:
i. Append ii. Prefix iii.
Suffix

9 Write a program to
implement member
predicate on lists.

10 Write a program to
implement cut and fail
operations.
Program No. - 1
Aim: - Write a program to implement simple
facts and rules.
Program: -
barking(dog).
lion(roar).
cats(mew).
ducks(quack).
Program No. -2
Aim: - Write a program to implement family tree.
Program: -
male(jack).
male(oliver).
male(ali).
male(james).
male(simon).
male(harry).
female(helen).
female(sophie).
female(jess).
female(lily).

parent_of(jack,jess).
parent_of(jack,lily).
parent_of(helen, jess).
parent_of(helen, lily).
parent_of(oliver,james).
parent_of(sophie, james).
parent_of(jess, simon).
parent_of(ali, simon).
parent_of(lily, harry).
parent_of(james, harry).
/* Rules */
father_of(X,Y):- male(X),
parent_of(X,Y).
mother_of(X,Y):- female(X),
parent_of(X,Y).

grandfather_of(X,Y):- male(X),
parent_of(X,Z),
parent_of(Z,Y).
grandmother_of(X,Y):- female(X), parent_of(X,Z),
parent_of(Z,Y).
sister_of(X,Y):- %(X,Y or Y,X)%
female(X),
father_of(F, Y), father_of(F,X),X \= Y.
sister_of(X,Y):- female(X),
mother_of(M, Y), mother_of(M,X),X \= Y.
aunt_of(X,Y):- female(X),
parent_of(Z,Y), sister_of(Z,X),!.
brother_of(X,Y):- %(X,Y or Y,X)%
male(X),
father_of(F, Y), father_of(F,X),X \= Y.
brother_of(X,Y):- male(X),
mother_of(M, Y), mother_of(M,X),X \= Y.
uncle_of(X,Y):-
parent_of(Z,Y), brother_of(Z,X).
ancestor_of(X,Y):- parent_of(X,Y).
ancestor_of(X,Y):- parent_of(X,Z),
ancestor_of(Z,Y).

Program No. – 3
Aim: - Write a program to implement monkey
banana problem using prolog.
(Description: There is a monkey at the door of a
room. In the middle of the room a banana hangs
from the ceiling. The monkey wants it, but cannot
jump high enough from the floor. At the window
of the room there is a chair that the monkey can
use. The monkey can perform the following
actions:
• Walk on the floor
• Climb the chair
• Push the box around (if it is beside the box)
•Grasp the banana if it is standing on the box
Program: -
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):-at(floor,banana);
at(ceiling,banana),under(banana,box),climb(mon
key,box).
canget(banana,monkey):-
canreach(banana,monkey),grasp(monkey).
Program No. -4
AIM: Write a program to implement I/O in
prolog.
Program: -
start(Y) :- write('What season is it?: '), read(X), nl,
season(X,Y).
season(winter,cold).
season(summer,warm).

Program No. -5
AIM: Write a program to implement towers of
Hanoi problem.
(Description: Tower of Hanoi consists of three
pegs or towers with n disks placed one over the
other. The objective of the puzzle is to move the
stack to another peg following these simple rules.
1. Only one disk can be moved at a time.
2. No disk can be placed on top of the smaller
disk.)
Program: -
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).

Program No. -6
Aim: - Write a program to find the factorial of a
number using prolog.
Program: -
factorial(0,1).
factorial(N,F):-
N>0,
N1 is N-1,
factorial(N1,F1),
F is N * F1.

Program No. -7
Aim: - Write a program to implement water jug
problem.
(Description: A Water Jug Problem: You are given
two jugs, a 4-gallon one and a 3-gallon one, a
pump which has unlimited water which you can
use to fill the jug, and the ground on which water
may be poured. Neither jug has any measuring
markings on it. How can you get exactly 2 gallons
of water in the 4-gallon jug?)
Program: -
move(s(X,Y),s(Z,4)) :- Z is X - (4 - Y), Z >= 0.
move(s(X,Y),s(Z,0)) :- Z is X + Y, Z =< 3.
move(s(X,Y),s(3,Z)) :- Z is Y - (3 - X), Z >=0.
move(s(X,Y),s(0,Z)) :- Z is X + Y, Z =< 4.

move(s(0,Y),s(3,Y)).
move(s(X,0),s(X,4)).
move(s(X,Y),s(X,0)) :- Y > 0.
move(s(X,Y),s(0,Y)) :- X > 0.

moves(Xs) :- moves([s(0,0)],Xs).
moves([s(X0,Y0)|T], [s(X1,2),s(X0,Y0)|T])
:- move(s(X0,Y0),s(X1,2)), !.
moves([s(X0,Y0)|T],Xs) :-
move(s(X0,Y0),s(X1,Y1)),
not(member(s(X1,Y1),[s(X0,Y0)|T])),
moves([s(X1,Y1),s(X0,Y0)|T],Xs).

Program No. -8
Aim: - : Write a program to implement various
predicates on list:
i. Append
ii. Prefix
Program: -
append(integer,integer, integer).
append([],L,L).
append([H|L1], L2, [H|L3]):-append(L1,L2,L3).
prefix(element, list).

Program No. -9
AIM: - Write a program to implement member
predicate on lists.
Program: -
member(X, [X|_]).
member(X, [_|Tail]) :- member(X, Tail).

Program No. -10


Aim: -: Write a program to implement cut and fail
operations.
Program: -
a(X) :- b(X),c(X),!.
a(X) :- d(X).
m(Y):- n(Y),o(Y),fail.
b(1).
b(4).
c(1).
c(3).
n(9).
o(1).
d(4).

You might also like