0% found this document useful (0 votes)
13 views30 pages

Vishvjit Singh

This laboratory file report presents various Prolog programs related to artificial intelligence and expert systems, submitted for a Bachelor of Technology in Computer Science and Engineering. The report includes practical exercises such as finding relationships in family trees, list manipulations, and solving classical AI problems like the Water Jug and Traveling Salesman problems. Each experiment outlines the aim, program structure, and expected outputs.

Uploaded by

cpwsvijaya7
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)
13 views30 pages

Vishvjit Singh

This laboratory file report presents various Prolog programs related to artificial intelligence and expert systems, submitted for a Bachelor of Technology in Computer Science and Engineering. The report includes practical exercises such as finding relationships in family trees, list manipulations, and solving classical AI problems like the Water Jug and Traveling Salesman problems. Each experiment outlines the aim, program structure, and expected outputs.

Uploaded by

cpwsvijaya7
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/ 30

A

LABORATORY FILE REPORT


ON
‘ARTIFICIAL INTELLIGENCE AND EXPERT SYSTEM (PROLOG)’
SUBMITTED TO

CHHATTISGARH SWAMI VIVEKANAND TECHNICAL


UNIVERSITY BHILAI (C.G.)

SESSION 2022-23
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE AND ENGINEERING
J.K. INSTITUTE OF ENGINEERING, BILASPUR (C.G.)

SESSION 2022-23

SUBMITTED TO: SUBMITTED BY:

DR. SUDHA MISHRA NITESH KUMAR SAHU


ASSISTANT PROFESSOR BRANCH – CSE (6TH SEM)
JKIE BILASPR ENROLLMENT NO.- BJ4069
ROLL NO. - 302402220011
INDEX
Practical SUBMISSION
S. No. Practical NAME Page Sign
Date DATE
Write a prolog program to find the
rules for parent, child, male, female,
1. son, daughter, brother, sister, uncle, 1-2
aunt, ancestor given the facts about
father and wife only
Write a program to find the length of
2. a given list 3-4

Write a program to find the last


3. element of a given list 5
Write a program to delete the first
4. occurrence and also all occurrences of 6
a particular element in a given list
Write a program to find union and
5. intersection of two given sets 7
represented as lists
Write a program to read a list at a
time and write a list at a time using
6. the well-defined read & write 8
functions
Write a program given the knowledge
base, If x is on the top of y, y supports
x. If x is above y and they are
touching each other, x is on top of y.
7. A cup is above a book. The cup is 9
touching that book. Convert the
following into wff’s, clausal form; Is
it possible to deduce that `The book
supports the cup’.
Write a program given the knowledge
base, If Town x is connected to Town
y by highway z and bikes are allowed
on z, you can get to y from x by bike.
If Town x is connected to y by z then
y is also connected to x by z. If you
can get to town q from p and also to
8. town r from town q, you can get to 10
town r from town p. Town A is
connected to Town B by Road 1.
Town B is connected to Town C by
Road 2. Town A is connected to
Town C by Road 3. Town D is
connected to Town E by Road 4.
Town D is connected to Town B by
Road 5. Bikes are allowed on roads 3,
4, 5. Bikes are only either allowed on
Road 1 or on Road 2 every day.
Convert the following into wff’s,
clausal form and deduce that `One can
get to town B from town D’.
Solve the classical problems for
demonstrating AI search heuristics:
(Water Jug problem, Monkey Banana
9. problem, Missionary Cannibals 11-13
problem, Travelling Salesman
Problem and alike).
Solve the classical Crypt arithmetic
problems in AI: (DONALD +
10. GERALD = ROBERT, CROSS + 14
ROADS = DANGER, SEND +
MORE = MONEY and alike).
Solve the classical Blocks World
11. Problem demonstrating Planning 15-18
Problem-solving simulation in AI.
Write a program to search any goal
12. given an input graph using AO* 19-21
algorithm.
Write a program to solve
13. traveling salesman problem. 22-24
Solve the classical Blocks
World Problem of AI.

14. Let consider the following 25


block problem to understand
the problem

Write a program to search any


goal given an input graph
15. using AO* algorithm. 26
Experiment No. 1

Aim: Write a prolog program to find the rules for parent, child, male, female, son,
daughter, brother , sister, uncle, aunt, ancestor given the facts about father and wife only.

Rules:
We said earlier a predicate is defined by clauses, which may be facts or rules. A rule is no
more than a stored query. Its syntax is

head :- body.
where
head a predicate definition (just like a fact)
:- the neck symbol, sometimes read as "if"
body one or more goals (a query)

Production rules:
parents father and wife
aunt wife and uncle

Parse tree:
aunt(A,C)

wife uncle

kusum naresh naresh C

Program:
predicates

nondeterm father(symbol,symbol) nondeterm wife(symbol,symbol)


nondeterm male(symbol) nondeterm female(symbol) nondeterm
parents(symbol,symbol,symbol) nondeterm child(symbol,symbol) nondeterm
son(symbol,symbol) nondeterm daughter(symbol,symbol) nondeterm
brother(symbol,symbol) nondeterm sister(symbol,symbol) nondeterm
uncle(symbol,symbol)
nondeterm aunt(symbol,symbol)

1
clauses father(naresh,poonam). father(naresh,abhishek).
father(naresh,sanjay).
wife(kusum,naresh). male(abhishek). male(sanjay). female(poonam).
parents(F,M,C):- father(F,C),wife(M,F). child(C,F):-father(F,C). son(C,F):-
father(F,C),male(C). daughter(D,F):-father(F,D),female(D).
brother(B,C):-father(F,B),father(F,C),male(B), B<>C. sister(Y,C):-
father(F,Y),father(F,C),female(Y), Y<>C. uncle(U,C):-
brother(F,U),father(F,C),male(U).
aunt(A,C):-wife(A,U),uncle(U,C).

goal
son (Who,Whom).

Output:

Who=abhishek, Whom=naresh
Who=sanjay, Whom=naresh
2 Solutions

2
Experiment No. 2
Aim:
Write a prolog program to find length of given list.

Lists:
Lists are powerful data structures for holding and manipulating groups of things.

In Prolog, a list is simply a collection of terms. The terms can be any Prolog data types,
including structures and other lists. Syntactically, a list is denoted by square brackets with
the terms separated by commas. For example, a list of things in the kitchen is represented
as

[apple, broccoli, refrigerator]

Rather than having separate location predicates for each thing, we can have one location
predicate per container, with a list of things in the container.

loc_list([apple, broccoli, crackers], kitchen). loc_list([desk, computer], office).


loc_list([flashlight, envelope], desk).
loc_list([stamp, key], envelope).
loc_list(['washing machine'], cellar).
loc_list([nani], 'washing machine').

The special notation for list structures.

[X | Y]

There is a special list, called the empty list, which is represented by a set of empty
brackets ([]). It is also referred to as nil. It can describe the lack of contents of a place or
thing.

loc_list([], hall)

Unification works on lists just as it works on other data structures. With what we now
know about lists we can ask

?- loc_list(X, kitchen).
X = [apple, broccoli, crackers]

?- [_,X,_] = [apples, broccoli, crackers]. X = broccoli

Production rule:
length increment,length

3
Program:
domains
i=integer*

predicates
nondeterm length(i,integer)

clauses length([],0).
length([H|T],N):- length(T,M),N=M+1.

goal

length([4,5,6,7,8,9],Length_of_List).

Output:

Length_of_List=6
1 Solution

4
Experiment No. 3

Aim: Write a program to find the position of an element into a given list.

Program:
domains
namelist = symbol*

predicates
nondeterm lastd(namelist,symbol)

clauses lastd([Head],X):-X=Head. lastd([_|Tail],X):-lastd(Tail,X).

goal lastd([l,k,a,m],X).

Output:
X=m
1 Solution

5
Experiment No. 4
Aim: Write a program to delete the first occurrence and also all occurrences of a
particular element in a given list.

Program:
domains
i= integer*

predicates
nondeterm delete(integer,i,i)

clauses delete(_,[],[]). delete(X,[X|L],M):- delete(X,L,M).


delete(X,[Y|L],[Y|M]):- not(X=Y), delete(X,L,M).

goal

delete(2,[2,5,6,10,7,8,2],A).

Ouput:

A=[5,6,10,7,8]
1 Solution

6
Experiment No. 5
Write a program to find union and intersection of two given sets represented as lists.

Program:
domains
i=char*

predicates
nondeterm member(char,i) nondeterm union(i,i,i) nondeterm subset(i,i)
nondeterm intersection(i,i,i)

clauses member(X,[_|R]) :- member(X,R). member(X,[X|_]). union( [ ], X,


X ). union( [ X | R ], Y, Z ) :- member( X, Y ),!,union( R, Y, Z ). union( [ X | R
], Y, [ X | Z ] ) :- union( R, Y, Z ). subset( [ A | X ], Y ) :- member( A, Y ), subset( X,
Y ). subset( [ ], _ ). intersection( [ ], _, [ ] ). intersection( [ X | R ], Y, [ X
| Z ] ) :- member( X, Y ),!,intersection( R, Y, Z ). intersection( [ _ | R ], Y, Z ) :-
intersection( R, Y, Z ).

goal intersection(['a','b','c','d','1','2'],['d','f','2','3'],B).
%union(['a','b','c','d','1','2'],['d','f'],B).
%subset(['a','z'],['a','b','c','d','1','2']).

Output:

1) B=['d','2']
1 Solution

2) B=['a','b','c','1','2','d','f']
1 Solution

3) no

7
Experiment No. 6
Write a program to read a list at a time and write a list at a time using the well
defined read & write functions.

Term I/O:
read(Term)
Reads next full-stop (period) delimited term from the current input stream, if eof
then returns the atom 'end_of_file'.
write(Term)
Writes a term to the current output stream.
print(Term)
Writes a term to the current output stream. Uses a user defined predicate portray/1 to
write the term, otherwise uses write.
writeq(Term)
Writes a term to the current output stream in a form aceptable as input to read.

Program:
predicates

Clauses

goal readln(X),write(X).

Output:

I love India
I love India X=I love India
1 Solution

8
Experiment No. 7

Aim: Write a program given the knowledge base,


If x is on the top of y, y supports x.
If x is above y and they are touching each other, x is on top of y. A cup
is above a book. The cup is touching that book.
Convert the following into wff’s, clausal form; Is it possible to deduce that
`The book supports the cup’.
Program:
predicates
ontop(symbol,symbol) supports(symbol,symbol)
touch(symbol,symbol)
above(symbol,symbol)

clauses ontop(X,Y):-above(X,Y),touch(X,Y). supports(Y,X):-ontop(X,Y).


above(cup,book). above(jug,plate). touch(cup,book).

goal supports(book,cup).

Output:

yes

9
Experiment No. 8
Write a program given the knowledge base,
If Town x is connected to Town y by highway z and bikes are allowed on z, you
can get to y from x by bike.
If Town x is connected to y by z then y is also connected to x by z.
If you can get to town q from p and also to town r from town q, you can get to
town r from town p.
Town A is connected to Town B by Road 1. Town B is connected to Town C by
Road 2.
Town A is connected to Town C by Road 3. Town D is connected to Town E by
Road 4.
Town D is connected to Town B by Road 5. Bikes are allowed on roads 3, 4, 5.
Bikes are only either allowed on Road 1 or on Road 2 every day. Convert the
following into wff’s, clausal form and deduce that
`One can get to town B from town D’.

Program:
predicates
nondeterm connected(symbol,symbol,symbol) nondeterm
canget(symbol,symbol)
nondeterm bikesallowed(symbol)

clauses connected(a,b,r1). connected(b,c,r2). connected(a,c,r3).


connected(d,e,r4). connected(d,b,r5). connected(X,Y,Z):-
connected(Y,X,Z).
canget(Y,X):-connected(X,Y,Z),bikesallowed(Z). canget(R,P):-
canget(Q,P),canget(R,Q).
bikesallowed(r3). bikesallowed(r4). bikesallowed(r5).

goal canget(b,d).

Output:

yes

10
Experiment No. 9

Aim:
Write a program for water jug problem.

"You are given two jugs, a 4-gallon one and a 3-gallon one. Neither have any measuring
markers on it. There is a tap that can be used to fill the jugs with water. How can you get
exactly 2 gallons of water into the 4-gallon jug?"
Production Rules:-
R1: (x,y) --> (4,y) if x < 4
R2: (x,y) --> (x,3) if y < 3
R3: (x,y) --> (x-d,y) if x > 0
R4: (x,y) --> (x,y-d) if y > 0
R5: (x,y) --> (0,y) if x > 0
R6: (x,y) --> (x,0) if y > 0
R7: (x,y) --> (4,y-(4-x)) if x+y >= 4 and y > 0
R8: (x,y) --> (x-(3-y),y) if x+y >= 3 and x > 0
R9: (x,y) --> (x+y,0) if x+y =< 4 and y > 0 R10: (x,y) --> (0,x+y) if x+y =< 3 and x >
0

Parse Tree:

11
Program:

12
Output:

Fill the 4-Gallon Jug: (0,0) --> (4,0) Fill the 3-Gallon Jug: (4,0) --> (4,3)
Empty the 4-Gallon jug on ground: (4,3) --> (0,3)
Pour all the water from 3-Gallon jug to 4-gallon: (0,3) --> (3,0)
Fill the 3-Gallon Jug: (3,0) --> (3,3)
Pour water from 3-Gallon jug to 4-gallon until it is full: (3,3) --> (4,2)
Empty the 4-Gallon jug on ground: (4,2) --> (0,2)
Pour all the water from 3-Gallon jug to 4-gallon: (0,2) --> (2,0) yes

13
Experiment No. 10

Aim: Write a program to solve the Monkey Banana problem.


Imagine a room containing a monkey, chair and some bananas. That has been hanged from
the center of ceiling. If the monkey is clever enough he can reach the bananas by placing
the chair directly below the bananas and climb on the chair . The problem is to prove the
monkey can reach the bananas.

Program:

Output:
yes

14
Experiment No. 11

Aim: Write a prolog program for Adding DONALD + GERALD =ROBERT.


We have to assign digit values to each letter coming in the three words; no two letters can
have same digit value.

Program: (not necessarily unique):

c1 c2 c3 c4 c5 carries
D O N A L D
G E R A L D
---------------- R O B E R T

Steps:
1. T is even ( D + D)

2. D >= 5 <=> c5 == 1
D <= 4 <=> c5 == 0

3. L >= 5 <=> c4 == 1
L <= 4 <=> c4 == 0

4. A >= 5 <=> c3 == 1
A <= 4 <=> c3 == 0

5. c5 == 1 <=> R is odd c5 == 0 <=> R is even ( L + L)


R not 0 (because R and L cannot both be 0)

6. c4 == 1 <=> E is odd c4 == 0 <=> E is even ( A + A)


E not 0 (because E and A cannot both be 0)

7. c2 == 0 <=> E == 0
c2 == 1 <=> E == 9 and c3 + N + R > 9

8. c1 + D + G <= 9 (no carry)


R <= D
R <= G

9. By (6) and (7), E must odd and hence 9.


By (7) it is either 0 or 9, but if it is 0, then it is even and by (6) it cannot
be even and 0.
==> c4 = 1, E = 9, c2 = 1
O is the last variable to be solved for - it can be anything

15
10. By (9): E == 9, c2 == 1
==> c1 = 1

11. By (9) and (8),


==> D + G <= 8

12. By (9): c4 == 1
==> L >= 5

13. By (9): c4 == 1, E == 9
==> A = 4 (A cannot be 9 because E is already 9)
==> c3 = 0

14. Three instance of R and of D. Make assumption.


Aside: simplified version of this puzzle gives clue "R is odd".
14a. R is odd ==> c5 == 1, D >= 5
Choices sorted for this additional information. c5 == 1 :
T == 0 <=> D == 5
T == 2 <=> D == 6
T == 4 <=> D == 7 : unavailable: (13): D == 4
T == 6 <=> D == 8
T == 8 <=> D == 9 : unavailable: (9): E == 9 c5 == 0 (not possibilities with the
additional clue):
T == 2 <=> D == 1
T == 4 <=> D == 2 : unavailable: (13): D == 4
T == 6 <=> D == 3
T == 8 <=> D == 4 : unavailable: (13): D == 4

Iterate through the possibilities:


==> D=5, T=0, c5=1

15. By (14): D == 5 and (10): c1 == 1


==> R == c1 + D + G == 1 + 5 + G
==> R == G + 6

16. By (15),
R == 6 <=> G == 0
R == 7 <=> G == 1
R == 8 <=> G == 2
R == 9 <=> G == 3

17. Unavailable values:


By (14): 0
By (9): 9
R == 7 <=> G == 1
R == 8 <=> G == 2 (invalidated by additional clue that "R is odd")

18. c3 + N + R == B with carry c2


16
By (9): c2 == 1
By (13): c3 == 0
mod10(N + R) == 1
rem10(N + R) == B

19. Assume two possible values for R from (17).


First, assume R == 7, and hence G == 1.
By (18), N >= 3.
N == 3 <=> B == 0 : unavailable value (14)
N == 4 unavailable value (13)
N == 5 unavailable value (14)
N == 6 <=> B == 3
N == 7 unavailable value (19) (value assumed here for R)
N == 8 <=> B == 5 : unavailable value (14)
N == 9 unavailable value (9)
==> N = 6, B = 3

20. By (12): L >= 5,


Unavailable values: 5, 6, 7, 9
==> L = 8

21. By unavailable values:


==> O = 2
Program:

17
Output:
D=5, O=2, N=6, A=4, L=8, G=1, E=9, R=7, B=3, T=0
1 Solution

18
Experiment No. 12

Aim
Solve the classical Missionary Cannibals problem of AI.

• There are three missionaries and three cannibals on the left bank of a river.
• They wish to cross over to the right bank using a boat that can only carry two at a
time.
• The number of cannibals on either bank must never exceed the number of
missionaries on the same bank, otherwise the missionaries will become the
cannibals' dinner!
• Plan a sequence of crossings that will take everyone safely across.
• The initial state is shown to the right here, where black triangles represent
missionaries and red circles represent cannibals.

Parse Tree:

Program:

19
20
Output:

21
Experiment No. 13
Aim: Write a program to solve traveling salesman problem.
The well-known "Travelling Salesman Problem" is defined as follows. An instance of the
problem consists of a collection of sites, together with numerical values indicating
distances between pairs of sites. Given an instance, the question is: in what order should
one visit all sites so as to minimize the total distance travelled? Note that we want to return
to the starting point of the tour, so there is no issue regarding which site we should start
from. Each site is to be visited exactly once, and the distance travelled between two
consecutive sites on the tour is always the given distance between that pair of sites. If no
distance is given, that means that the tour is not allowed to have those two sites
consecutively.
Production Rules:

22
23
Output:

aedbc D = 15
aedbc D = 15
adebc D = 24
aebdc D = 25
abedc D = 27
adbec D = 31
abdec D = 24
Finally:
aedbc MIN_D = 15
AllTowns=["a","b","c","d","e"],
AllWays=[rout("a","c",1),rout("a","b",6),rout("a","e",5),rout("a","d",8),rout("c","b",
2),rout(" c","d",7),rout("c","e",10),rout("b","d",3),rout("b","e",9),rout("d","e",4)],
Route=["a","e","d","b","c"], Distance=15

24
Experiment No. 14
Aim:
Solve the classical Blocks World Problem of AI.

Let consider the following block problem to understand the problem

A A D
B C D B C
Program :

Output:

Pick up block: (d) holding: d


Stack block: (d,c)
yes

25
Experiment No. 15
Aim:
Write a program to search any goal given an input graph using AO* algorithm.

Program :

26

You might also like