0% found this document useful (0 votes)
9 views20 pages

AI Lab Manual - AI&ML

The document outlines a series of experiments for a B.Tech Artificial Intelligence Lab course, detailing various programming tasks primarily in Prolog. These tasks include creating rules for familial relationships, list manipulations, knowledge base deductions, and solving classical AI problems like the Water Jug problem and the Travelling Salesman Problem. Each experiment includes code snippets and expected outputs, serving as practical applications of AI concepts.

Uploaded by

yeeshandas
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)
9 views20 pages

AI Lab Manual - AI&ML

The document outlines a series of experiments for a B.Tech Artificial Intelligence Lab course, detailing various programming tasks primarily in Prolog. These tasks include creating rules for familial relationships, list manipulations, knowledge base deductions, and solving classical AI problems like the Water Jug problem and the Travelling Salesman Problem. Each experiment includes code snippets and expected outputs, serving as practical applications of AI concepts.

Uploaded by

yeeshandas
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/ 20

TABLE OF CONTENT

S.NO. DATE NAMEOFTHEEXPERIMENT PAGE NO.


SIGNATURE

1 Write a prolog program to find the rules for


parents, child, male, female, son, daughter,
sister, uncle, aunt, ancestor given the fact about
father and wife only.

2 Write a program to find the length of a given list

3 Write a program to find the last element of a


given list

4 Write a program to delete the first occurrence


and also all occurrences of a particular element
in a given list.

5 Write a program to find union and intersection of


two given sets represented as lists.

6 Write a program to give a knowledge base,


If x is in 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
forms; Is it possible to deduce that ‘The book
supports the cup’.

7 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 q, you can get to town r from 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.
B.TECH 5th AIML
Artificial Intelligence Lab Manual
Bikes are only either allowed on Road 1 or on
Road 2 every day. Convert the following into
wff’s clausal formed deduce that ‘One can get to
Town B from Town D’.

8 Solve the classical problems for demonstrating


AI search heuristics: (Water Jug problem,
Monkey Banana problem, Missionary Cannibals
problem, Travelling Salesman Problem and
alike).

9 Solve the classical Crypt arithmetic problems in


AI: (DONALD +
GERALD = ROBERT,
CROSS + ROADS = DANGER, SEND +
MORE = MONEY and alike).

10 Solve the classical Blocks Word problem


demonstrating Planning Problem-solving
simulation in AI.

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Artificial Intelligence Lab Manual

LIST OF EXPERIMENTS
AS PER THE SYLLABUS PRESCRIBED BY THE UNIVERSITY

1. Write a prolog program to find the rules for parents, child, male, female, son,
daughter, sister, uncle, aunt, ancestor given the fact about father and wife only.
2. Write a program to find the length of a given list
3. Write a program to find the last element of a given list
4. Write a program to delete the first occurrence and also all occurrences of a
particular element in a given list.
5. Write a program to find union and intersection of two given sets represented as
lists.
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.
7. Write a program to give a knowledge base,
If x is in 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 forms; Is it possible to deduce that ‘The book supports the cup’.
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 q, you can get to town r
from 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 formed deduce that ‘One can get to Town B from
Town D’.
9. Solve the classical problems of demonstrating AI search heuristics: (Water jug
problem, Monkey Banana problem, Missionary Cannibals problem, Travelling
Salesman problem and alike)
Computer Science & Engineering Department
RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Artificial Intelligence Lab Manual

10.Solve the classical crypt arithmetic problems in AI: (DONALD + GERALD =


ROBERT, CROSS + ROADS = DANGER, SEND + MORE = MONEY and
alike)
11. Solve the classical Blocks Word problem demonstrating Planning Problem-
solving simulation in AI.
12. Write a program to search any goal given an input graph using AO* algorithm.

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

Experiment No. 1

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.

CODE:

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)

clauses father(naresh,poonam).
father(naresh,abhishek).
father(naresh,sanjay).
wife(kusum,naresh).

male(abhishek). male(sanjay).
female(poonam). female(kusum).

parents(F,M,C):- father(F,C),wife(M,F).
child(C,F):-father(F,C). son(S,F):-father(F,S),male(S). 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 sister(Who,abhishek).
%brother(Who,sanjay).
%male(Who).
%parents(naresh,kusum,Who).
%son(Who,naresh).
%female(Who).

OUTPUT :
Who = Poonam
1 Solution
Computer Science & Engineering Department
RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

Experiment No. – 2

Write a program to find the length of a given list

CODE:

domains i=integer*
predicates
nondeterm length(i,integer)

clauses length([],0). length([_|T],N):-


length(T,M),N=M+1.
goal

length([1,2,3,4,7],B).

OUTPUT:

B=5
1 Solution

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai
Experiment No. – 3

Write a program to find the last element of a given list

CODE:

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
B.TECH 5th AIML
Machin Learning Lab Manual

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

CODE:

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).

OUTPUT:

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

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

Experiment No. 5

Aim : Write a program to find union and intersection of two given sets
represented as lists.

CODE: Union of List

domains i=char* predicates


nondeterm member(char,i)
nondeterm union(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).

goal union(['a','b','c','d','1','2'], ['d','f'],


B).

OUTPUT:

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

1 Solution

CODE: Intersection of List

domains i=char* predicates


nondeterm member(char,i)
nondeterm intersection(i,i,i)

clauses member(X,[_|R]):-
member(X,R). member(X,[X|_]).
intersection([],_,[]).
Computer Science & Engineering Department
RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

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'], B).

OUTPUT:

B=['d']

1 Solution

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai
Experiment No. - 6

Aim:
Write a program to give a knowledge base,
If x is in 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 forms; Is it possible to deduce that ‘The book supports the cup’.

CODE:

predicates

nondeterm above(symbol,symbol). nondeterm


touch(symbol,symbol). nondeterm
on_top(symbol,symbol). nondeterm
supports(symbol,symbol).

clauses

above (cup,book). touch


(cup,book).

on_top(X,Y) :- above(X,Y),touch(X,Y). supports(Y,X)


:- on_top(X,Y).

goal supports(book,cup).

OUTPUT:

Yes
B.TECH 5th AIML
Machin Learning Lab Manual

Experiment No. 7
Aim :
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 q, you can get to town r from 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 formed deduce that ‘One can get to Town B from Town D’.

CODE:

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
Computer Science & Engineering Department
RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

Experiment No. – 8

Aim : Solve the classical problems for demonstrating AI search heuristics: (Water
Jug problem, Monkey Banana problem, Missionary Cannibals problem, Travelling
Salesman Problem and alike).

CODE: monkey banana problem

predicates nondeterm inroom(symbol)


nondeterm canmove(symbol,symbol)
nondeterm canclimb(symbol,symbol)
nondeterm below(symbol,symbol)
nondeterm canreach(symbol,symbol)
nondeterm clever(symbol)

clauses inroom(monkey). inroom(chair). inroom(banana). canreach(M,B):-


inroom(M),inroom(C),inroom(B),canmove(M,C),below(C,B),canclimb(M,C).
clever(M):- canreach(M,B). canmove(monkey,chair). below(chair,banana).
canclimb(monkey,chair).

goal clever(monkey).

OUTPUT:

Yes

CODE: Water Jug Problem

database
visited_state(integer,integer)

predicates
nondeterm state(integer,integer)

clauses state(2,0). state(X,Y):- X<4,


not(visited_state(4,Y)),assert(visited_state(X,Y)), write("Fill the 4-gallon jug:
(",X,",",Y,")-> (",4,",",Y,")\n"), state(4,Y).
Computer Science & Engineering Department
RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

state(X,Y):- Y<3, not(visited_state(X,3)),assert(visited_state(X,Y)), write("Fill the


3-gallon jug: (",X,",",Y,",)-> (",X,",",3,")\n"), state(X,3).

state(X,Y):- X>0, not(visited_state(0,Y)),assert(visited_state(X,Y)),


write("Empty the 4-gallon jug on ground:(",X,",",Y,",)->
(",0,",",Y,")\n"),state(0,Y).

state(X,Y):- Y>0, not(visited_state(X,0)),assert(visited_state(X,0)), write("Empty


the 3-gallon jug on ground: (",X,",",Y,")-> (",X,",",0,",)\n"), state(X,0).

state(X,Y):- X+Y>=4,Y>0, NEW_Y=Y-(4-X),


not(visited_state(NEW_Y,0)),assert(visited_state(X,Y)), write("Pour all
water from 3-gallon jug to 4-gallon untill it is full:(",X,",",Y,")->
(",4,",",NEW_Y,")\n"),state(4,NEW_Y).

state(X,Y):- X+Y>=3,X>0, NEW_X=X-(3-Y),


not(visited_state(X,3)),assert(visited_state(X,Y)), write("Pour all water
from 4-gallon jug to 3-gallon untill it is full:(",X,",",Y,")->
(",NEW_X,",",3,")\n"),state(NEW_X,3).

state(X,Y):- X+Y<=4,Y>0, NEW_X=X+Y,


not(visited_state(NEW_X,0)),assert(visited_state(X,Y)), write("Pour all
water from 3-gallon jug to 4-gallon:(",X,",",Y,",)->
(",NEW_X,",",0,")\n"),state(NEW_X,0).

state(X,Y):- X+Y<=3,X>0, NEW_Y=X+Y,


not(visited_state(0,NEW_Y)),assert(visited_state(X,Y)), write("Pour all
water from 4-gallon jug to 3-gallon:(",X,",",Y,",)->
(",0,",",NEW_Y,")\n"),state(0,NEW_Y).

state(0,2):- not(visited_state(2,0)),assert(visited_state(0,2)), write("Pour 2-


gallon water from 3-gallon jug to 4-gallon:(",0,",",2,",)->
(",2,",",0,",)\n"),state(2,0).

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

state(2,Y):- not(visited_state(0,Y)),assert(visited_state(2,Y)),
write("Empty 2-gallon water from 4-gallon jug on groung:(",2,",",Y,",)->
(",0,",",Y,",)\n"),state(0,Y).

goal state(0,0).

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 water from 3-gallon jug to 4-gallon:(0,3,)-> (3,0)
Fill the 3-gallon jug: (3,0,)-> (3,3)
Pour all water from 3-gallon jug to 4-gallon untill it is full:(3,3)-> (4,2)
Empty the 4-gallon jug on ground:(4,2,)-> (0,2)
Pour all water from 3-gallon jug to 4-gallon:(0,2,)-> (2,0)
Yes

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

Experiment No. 9
Aim : Solve the classical Crypt arithmetic problems in AI: (DONALD +
GERALD = ROBERT, CROSS + ROADS = DANGER, SEND + MORE =
MONEY and alike).

CODE:

Predicates

nondeterm digit(integer) nondeterm


solve(integer,integer,integer,integer,integer,integer,integer,integer,integer,int eger)
nondeterm
sum(integer,integer,integer,integer,integer,integer,integer,integer,integer,inte ger)

clauses
digit(0). digit(1). digit(2). digit(3). digit(4). digit(5). digit(6). digit(7). digit(8).
digit(9).
solve(R,O,B,E,T,D,N,L,G,A):- digit(R),
digit(O),O<>R, digit(B),B<>O,B<>R,
digit(E),E<>B,E<>O,E<>R,
digit(T),T<>E,T<>B,T<>O,T<>R,
digit(D),D<>T,D<>E,D<>B,D<>O,D<>R,
digit(N),N<>D,N<>T,N<>E,N<>B,N<>O,N<>R,
digit(L),L<>N,L<>D,L<>T,L<>E,L<>B,L<>O,L<>R,

digit(G),G<>L,G<>N,G<>D,G<>T,G<>E,G<>B,G<>O,G<>R,
digit(A),A<>G,A<>L,A<>N,A<>D,A<>T,A<>E,A<>B,A<>O,A<>R,
sum(R,O,B,E,T,D,N,L,G,A).

sum(R,O,B,E,T,D,N,L,G,A):-
N1=D*100000+O*10000+N*1000+A*100+L*10+D,N2=G*100000+E*1000
0+R*1000+A*100+L*10+D,N3=R*100000+O*10000+B*1000+E*100+R*1
0+T,N3=N1+N2.

goal

solve(R,O,B,E,T,D,N,L,G,A).
Computer Science & Engineering Department
RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

OUTPUT:

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

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

Experiment No. – 10
Aim:
Solve the classical Blocks Word problem demonstrating Planning Problem-solving
simulation in AI.

CODE:
class BlocksWorld:
def __init__(self, initial_state, goal_state):
self.initial_state = initial_state
self.goal_state = goal_state

def is_goal_reached(self, current_state):


return current_state == self.goal_state

def get_possible_moves(self, current_state):


possible_moves = []
for block1 in current_state:
if current_state[block1] != 'Table':
continue # Cannot move blocks that are not on the table or another block
for block2 in current_state:
if block1 != block2 and current_state[block2] == 'Table':
possible_moves.append(('Move', block1, block2))
return possible_moves

def apply_move(self, current_state, move):


if move[0] == 'Move':
block, destination = move[1], move[2]
new_state = current_state.copy()
new_state[block] = destination
return new_state
else:
raise ValueError("Invalid move")

def solve_bfs(self):
Computer Science & Engineering Department
RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

"""
Solves the Blocks World problem using Breadth-First Search.
"""
frontier = [self.initial_state]
explored = set()
parent_map = {} # To track the parent state of each state
parent_map[str(self.initial_state)] = None

while frontier:
current_state = frontier.pop(0)
explored.add(str(current_state))

if self.is_goal_reached(current_state):
# Reconstruct the solution path
plan = []
while current_state:
if parent_map[str(current_state)] is not None:
parent = parent_map[str(current_state)]
for move in self.get_possible_moves(parent):
if self.apply_move(parent, move) == current_state:
plan.insert(0, move)
break
current_state = parent_map[str(current_state)]
return plan

for move in self.get_possible_moves(current_state):


next_state = self.apply_move(current_state, move)
if str(next_state) not in explored:
frontier.append(next_state)
parent_map[str(next_state)] = current_state

return None # No solution found

if __name__ == "__main__":
initial_state = {

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai
B.TECH 5th AIML
Machin Learning Lab Manual

'A': 'Table',
'B': 'A',
'C': 'Table'
}
goal_state = {
'A': 'B',
'B': 'C',
'C': 'Table'
}

blocks_world = BlocksWorld(initial_state, goal_state)


plan = blocks_world.solve_bfs()

if plan:
print("Plan found:")
for move in plan:
print(move)
else:
print("No solution found.")

OUTPUT :

Plan found: ('Move', 'A', 'C') ('Move', 'B', 'A')

Computer Science & Engineering Department


RSR Rungta College of Engineering & Technology Bhilai

You might also like