AI Lab Manual - AI&ML
AI Lab Manual - AI&ML
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
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
CODE:
domains i=integer*
predicates
nondeterm length(i,integer)
length([1,2,3,4,7],B).
OUTPUT:
B=5
1 Solution
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
goal
delete(2,[2,5,6,10,7,8,2],A).
OUTPUT:
A = [5,6,10,7,8]
1 Solution
Experiment No. 5
Aim : Write a program to find union and intersection of two given sets
represented as lists.
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).
OUTPUT:
B=['a','b','c','1','2','d','f']
1 Solution
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
goal intersection(['a','b','c','d','1','2'],
['d','f'], B).
OUTPUT:
B=['d']
1 Solution
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
clauses
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).
goal clever(monkey).
OUTPUT:
Yes
database
visited_state(integer,integer)
predicates
nondeterm state(integer,integer)
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:
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
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
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 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
if __name__ == "__main__":
initial_state = {
'A': 'Table',
'B': 'A',
'C': 'Table'
}
goal_state = {
'A': 'B',
'B': 'C',
'C': 'Table'
}
if plan:
print("Plan found:")
for move in plan:
print(move)
else:
print("No solution found.")
OUTPUT :