AI Lab Manual - CSE Specialization
AI Lab Manual - CSE Specialization
LAB MANUAL
ARTIFICIAL INTELLIGENCE
LAB
SEMESTER 5 TH
RUNGTA COLLEGE
Rungta Educational Campus,
Kohka-Kurud Road, Bhilai,
Chhattisgarh, India
Phone No. 0788-6666666
MANAGED BY : SANTOSH RUNGTA GROUP OF INSTITUTIONS
Prepared By
NEELABH SAO
RUNGTA COLLEGE OF ENGINEERING & TECHNOLOGY
LAB MANUAL
ARTIFICIAL INTELLIGENCE
LAB
SEMESTER 5 TH
DOs:
Take help from the Manual / Work Book for preparation of the experiment.
For any abnormal working of the machine consult the Faculty In-charge/
Lab Assistant.
Shut down the machine and switch off the power supply after performing
the experiment.
DON’Ts :
Do not tamper the instruments in the Lab and do not disturb their settings.
LIST OF EXPERIMENTS
AS PER THE SYLLABUS PRESCRIBED BY THE UNIVERSITY
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.
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
Experiment No. 2
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
Experiment No. 3
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
Experiment No. 4
Aim : 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
Experiment No. 5
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([],_,[]).
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
Experiment No. 7
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
Experiment No. 8
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 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’.
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
Experiment No. 9
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),cancl
imb(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).
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(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).
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:
CODE:
Predicates
nondeterm digit(integer)
nondeterm
solve(integer,integer,integer,integer,integer,integer,integer,integ
er,integer,integer)
nondeterm
sum(integer,integer,integer,integer,integer,integer,integer,integ
er,integer,integer)
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*10000
0+E*10000+R*1000+A*100+L*10+D,N3=R*100000+O*10000+
B*1000+E*100+R*10+T,N3=N1+N2.
goal
solve(R,O,B,E,T,D,N,L,G,A).
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. 12
CODE:
edge(a, b, 5).
edge(a, c, 3).
edge(b, d, 4).
edge(c, d, 2).
edge(d, e, 6).
edge(c, e, 7).
heuristic(Node, HeuristicValue) :-
goal(Goal),
distance(Node, Goal, HeuristicValue).
goal(e).
a_star_search(Start, Path) :-
empty_pqueue(EmptyQueue),
add_to_pqueue(EmptyQueue, 0, Start, nil, InitialQueue),
a_star_search_loop(InitialQueue, [], Path).
a_star_search_loop(PQueue, Visited, Path) :-
peek_pqueue(PQueue, _, CurrentNode, _, _),
goal(CurrentNode),
reconstruct_path(PQueue, [], PathReversed),
reverse(PathReversed, Path).
% Sample query
:- initialization(main).
main :-
a_star_search(a, Path),
writeln(Path).