You are on page 1of 7

Practical 4 20CS067

Faculty of Technology and Engineering


Chandubhai S Patel Institute of Technology
Department of Computer Science & Engineering

Date: / / 2022

Laboratory Manual

Academic Year : 2022-23 Semester : 5

Course code : CS341 Course name : Artificial Intelligence

Practical 4.1

Aim: Design a Family Tree for Your Family: Write a program which contains three predicates:
male, female, parent. Make rules for following family relations: father, mother,
grandfather, grandmother, brother, sister, uncle,aunt, nephew and niece, cousin.

Code : female(sheetal).
female(tanvi).
female(disha).

male(pankaj).
male(pavan).
male(aakash).

parent(pankaj,tanvi).
parent(sheetal,tanvi).
parent(tanvi,disha).
parent(tanvi,aakash).
parent(pavan,disha).
parent(pavan,aakash).

mother(X,Y):- parent(X,Y),female(X).
father(X,Y):- parent(X,Y),male(X).

1
Practical 4 20CS067

grandfather(X,Y):- parent(X,Z),parent(Z,Y),male(X).
grandmother(X,Y):- parent(X,Z),parent(Z,Y),female(X).
brother(X,Y):- parent(Z,X),parent(Z,Y),male(X).
sister(X,Y):- parent(Z,X),parent(Z,Y),female(X).

Output :

Practical 4.2

Aim: Design a Medical Diagnosis Expert System

Code : go :-
write('What is the patient''s name? '),
read(Patient),get_single_char(Code),
hypothesis(Patient,Disease),
write_list([Patient,', probably has ',Disease,'.']),nl.

go :-
write('Sorry, I don''t seem to be able to'),nl,
write('diagnose the disease.'),nl.

symptom(Patient,fever) :-
verify(Patient," have a fever (y/n) ?").
symptom(Patient,rash) :-
verify(Patient," have a rash (y/n) ?").
symptom(Patient,headache) :-
verify(Patient," have a headache (y/n) ?").
symptom(Patient,runny_nose) :-
verify(Patient," have a runny_nose (y/n) ?").
symptom(Patient,conjunctivitis) :-
verify(Patient," have a conjunctivitis (y/n) ?").
symptom(Patient,cough) :-
verify(Patient," have a cough (y/n) ?").
symptom(Patient,body_ache) :-
verify(Patient," have a body_ache (y/n) ?").
symptom(Patient,chills) :-
verify(Patient," have a chills (y/n) ?").

2
Practical 4 20CS067

symptom(Patient,sore_throat) :-
verify(Patient," have a sore_throat (y/n) ?").
symptom(Patient,sneezing) :-
verify(Patient," have a sneezing (y/n) ?").
symptom(Patient,swollen_glands) :-
verify(Patient," have a swollen_glands (y/n) ?").

ask(Patient,Question) :-
write(Patient),write(', do you'),write(Question),
read(N),
( (N == yes ; N == y)
->
assert(yes(Question)) ;
assert(no(Question)), fail).

:- dynamic yes/1,no/1.

verify(P,S) :-
(yes(S) -> true ;
(no(S) -> fail ;
ask(P,S))).

undo :- retract(yes(_)),fail.
undo :- retract(no(_)),fail.
undo.

hypothesis(Patient,german_measles) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,runny_nose),
symptom(Patient,rash).

hypothesis(Patient,common_cold) :-
symptom(Patient,headache),
symptom(Patient,sneezing),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),
symptom(Patient,chills).

hypothesis(Patient,measles) :-
symptom(Patient,cough),
symptom(Patient,sneezing),
symptom(Patient,runny_nose).

hypothesis(Patient,flu) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,body_ache),
symptom(Patient,conjunctivitis),
symptom(Patient,chills),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),

3
Practical 4 20CS067

symptom(Patient,cough).

hypothesis(Patient,mumps) :-
symptom(Patient,fever),
symptom(Patient,swollen_glands).

hypothesis(Patient,chicken_pox) :-
symptom(Patient,fever),
symptom(Patient,chills),
symptom(Patient,body_ache),
symptom(Patient,rash).

write_list([]).
write_list([Term| Terms]) :-
write(Term),
write_list(Terms).

response(Reply) :-
get_single_char(Code),
put_code(Code), nl,
char_code(Reply, Code).

Output :

Practical 4.3

Aim: Write a Prolog program to perform following operations on list: Print member of a list,
write list, membership, concatenation, add an item,delete an item, sub list,
permutations, append list, finding nth element

Code : list_member(X,[X|_]).
list_member(X,[_|TAIL]) :-
list_member(X,TAIL).

list_concat([],L,L).
list_concat([X1|L1],L2,[X1|L3]) :-
list_concat(L1,L2,L3).

4
Practical 4 20CS067

list_delete(X, [X], []).


list_delete(X,[X|L1], L1).
list_delete(X, [Y|L2], [Y|L1]) :-
list_delete(X,L2,L1).

list_member(X,[X|_]).
list_member(X,[_|TAIL]) :-
list_member(X,TAIL).
list_append(A,T,T) :-
list_member(A,T),!.
list_append(A,T,[A|T]).l

Output :

5
Practical 4 20CS067

Practical 4.4

Aim: Write a Prolog program to demonstrate arithmetic operations

Code : calc :- X is 100 + 200,write('100 + 200 is '),write(X),nl,


Y is 400 - 150,write('400 - 150 is '),write(Y),nl,
Z is 10 * 300,write('10 * 300 is '),write(Z),nl,
A is 100 / 30,write('100 / 30 is '),write(A),nl, B
is 100 // 30,write('100 // 30 is '),write(B),nl, C
is 100 ** 2,write('100 ** 2 is '),write(C),nl,
D is 100 mod 30,write('100 mod 30 is '),write(D),nl.

Output :

Practical 4.5

Aim: Write a program to solve Monkey Banana problem in Prolog

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):-
at(floor,banana);
at(ceiling,banana);
under(banana,box);
climb(monkey,box);

canget(banana,monkey):-

6
Practical 4 20CS067

canreach(banana,monkey),grasp(monkey).

Output :

Grade / Marks Sign of Lab Teacher with Date


( _______ / 10 )

You might also like