Você está na página 1de 16

AI Lab Manual

ITS Engineering College, Greater Noida

I.T.S. Engineering College, Greater


Noida
Department of Information
Technology

Lab File
Artificial Intelligence
(EIT-752)
B.Tech (IT)- VIIth Semester (IV Year )

Submitted To: Ms. Mallika Gandhi

AI Lab Manual

ITS Engineering College, Greater Noida

Prepared By: Ruchin


Somal
1222213039

INDEX
S.No.

Date

Topic

Write a program for demonstrating the


Medical Diagnosis System using simple facts.

Write a program for demonstrating the use of


Variables in Medical Diagnosis Systems.

Write a program for demonstrating the use of


Compound Variables in Medical Diagnosis
Systems.

Write a simple program in Prolog for


demonstrating Unification.

Write a simple program for demonstrating


Recursion in Prolog

Write a program for demonstrating Input &


Output files in Prolog.

Write a program to construct a Family Tree

Write a program to print the list of customers


having different colors cars with the price and
models available.

Signature

AI Lab Manual

ITS Engineering College, Greater Noida

Write a program for demonstrating the use


Cut, Fail & Not as backtracking techniques.

10

Write a program to dynamically add rule or


fact in database dynamically using assert and
retract.

PROLOG
SWI-Prolog is free Prolog compiler licensed under the GPL, targeting primarily
the research and education. SWI-Prolog is designed and implemented by Jan
Wielemaker from the University of Amsterdam, department of Social Science
Informatics (SWI).
SWI-Prolog is written in ANSI C and should configure for most Unix machines
having an ANSI C Compiler. Ports have been made for:
1. MS Win32
2. MS Win3.1
3. MS DOS
4. OS/2

The Program
The program, sometimes called Database is a text file (*.pl) that contain the facts
and rules that will be used by the user of the program. It contains all the relations
that make this program.

The Query
When you launch a program you are in query mode. This mode is represented by
the sign?- at the beginning of the line. In query mode you ask questions about
relations described in the program.

Loading a program
When Prolog is launched the ?- should appear meaning you are in query mode. The
manner to launch a program depends of your compiler. For SWI-Prolog you can load
a program by typing the command [file]. when the file of your program is file.pl.
If you compiler is not SWI-Prolog you can also try the command reconsult(file).

[swi('demo/likes')].

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 1: Write a program for demonstrating the Medical Diagnosis System using
simple facts.
EXPLANATION:
symptom(chicken_pox,high_fever).
symptom(chicken_pox,chills).
symptom(dengu,high_fever).
symptom(cough,mild_body_ache).
symptom(cough,runny_nose).
symptom(jaundice,vomiting).
symptom(jaundice,high_fever).
QUERY:
goal:- symptom(dengu,high_fever).
goal:- symptom(dengu,chills).

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 2: Write a program for demonstrating the use of Variables in Medical


Diagnosis Systems.

EXPLANATION:
symptom(chicken_pox,high_fever).
symptom(chicken_pox,chills).
symptom(dengu,high_fever).
symptom(cough,mild_body_ache).
symptom(cough,runny_nose).
symptom(jaundice,vomiting).
symptom(jaundice,high_fever).
QUERY:
symptom(dengu,What).
symptom(Disease,mild_body_ache).

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 3: Write a program for demonstrating the use of Compound Variables in


Medical Diagnosis Systems.

EXPLANATION:
symptom(banana,yellow).
symptom(apple,red).
symptom(guvava,green).
symptom(grapes,green).
symptom(cheery,red).
symptom(mango,green).
symptom(mango,yellow).
QUERY:
symptom(Fruit,red),symptom(Fruit,yellow).
symptom(apple,What), symptom(mango, What).
symptom(apple,What), symptom(mango, X).

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 4: Write a simple program in Prolog for demonstrating Unification.


EXPLANATION:
cricket_match(1,ground1,player1).
cricket_match(2, ground2,player2).
cricket_match(3, ground3,player3).
cricket_match(4, ground4,player4).
QUERY:
cricket_match(_,_,player2).
cricket_match(_,X,ground1).

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 5: Write a simple program for demonstrating Recursion in Prolog


EXPLANATION:
fact1(0,1).
fact1(X,Y) :- X1 is X - 1,
fact1(X1,Z),
Y is Z*X.
QUERY:
fact1(8,X).

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 6: Write a program for demonstrating Input & Output files in Prolog.

EXPLANATION:
hello:write('wat is ur name?'),
read(X),
write('hello'), tab(1),write(X).

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 7: Write a program to construct a Family Tree and


EXPLANATION:
parent(rohit, sagar). %the fact that rohit is a parent of sagar %
parent(zoya, sagar).
parent(sagar, pat).
parent(sagar, bob).
parent(abc, ben).
female(mary). %the fact that zoya is a female%
female(ann).
female(abc).
male(rohit). %the fact that John is a rohit%
male(sagar).
male(bob).
male(ben).
mother(X,Y) :- parent(X, Y), female(X). %definition of the predicate mother
father(X,Y) :- parent(X, Y), male(X). %definition of the predicate father
QUERY:
female(X).
mother(X, sagar).
mother(abc, Y).
father(X, Y).

AI Lab Manual

ITS Engineering College, Greater Noida

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 8: Write a program to print the list of customers having different colors cars
with the price and models available.
EXPLANATION:
% List of customers with different colors%
cust_names([ruchin,sahil,shadab,anurag,siva],zinc).
cust_names([sadhana,soni,sonali],yellow).
cust_names([kannu,bharti,nirja,pooja],white).
% List of model with different colors%
model(white,[audi,jaguar,bmw]).
model(yellow,[polo,i20]).
model(zinc,[honda_city,fortuner]).
% Prices of cars with different colors%
price(white,700000).
price(yellow,380000).
price(zinc,660000).

cost(yellow):-cust_names(A,yellow),
write( '\n The name of customers who own a yellow color car is: '),tab(1),write(A),
price(yellow,X),
write( '\n The price of yellow color car is: '),tab(1),write(X),
model(yellow,Y),
write('\n The models available in yellow color '),tab(1),write(Y).

cost(white):-cust_names(A,white),
write( '\n The name of customers who own a white color car is: '),tab(1),write(A),

AI Lab Manual

ITS Engineering College, Greater Noida

price(white,X),
write( '\n The price of white color car is: '),tab(1),write(X),
model(white,Y),
write('\n The models available in white color '),tab(1),write(Y).
goal:write('\n Details of Customers who own a white car: \n '),
cost(white).
QUERY:
goal.

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 9: Write a program for demonstrating the use Cut, Fail & Not as
backtracking techniques in PROLOG.
EXPLANATION:
FAIL: The fail predicate is provided by Prolog, it causes the failure of the rule which
will be for ever valid, and nothing can change the statement of this predicate. A
typical use of fail is a negation of a predicate.
CUT: It is represented by an exclamation mark(!).The cut effectively tells Prolog to
freeze all the decisions made so far in this predicate. That is, if required to
backtrack, it will automatically fail without trying other alternatives. When the is
encountered, it re-routes backtracking.
NOT:

/* Fixes the value of X and then changes the values of Y*/


a(X, Y) :- b(X), !, c(Y).
b(1).
b(2).
b(3).
c(1).
c(2).
c(3).
/* NO ANSWER IS AVAILABLE AS NO */
/*TWO VALUES OF B N C ARE SAME AFTER FIXING THE VALUE TO 1*/
a(Z) :- b(Z),!. c(Z).
b(1).
b(2).
b(3).

c(2).
/* Example of Not*/
bachelor(P) :- male(P), not(married(P)).

AI Lab Manual

male(rajat).
male(jerry).
married(jerry).
married(laxman).
QUERY:
bachelor(jerry).
married(jerry).
bachelor(rajat).
married(rajat).

ITS Engineering College, Greater Noida

AI Lab Manual

ITS Engineering College, Greater Noida

PROGRAM 10: Write a program to dynamically add rule or fact in database dynamically
using assert and retract.
EXPLANATION:
assert(c).: Add the rule c in the database.assert()
retract(c): Remove the c from the database.retract(c)
asserta(c): Add c at the beginning of the database.asserta()
assertz(c): Add c at the end of the database.assertz()

Você também pode gostar