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

AI Lab Manual - CSE Specialization

Uploaded by

Tarun Kushwaha
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)
44 views20 pages

AI Lab Manual - CSE Specialization

Uploaded by

Tarun Kushwaha
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

RUNGTA COLLEGE OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF CSE (SPECIALISATION)

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

DEPARTMENT OF CSE (SPECIALISATION)

LAB MANUAL

ARTIFICIAL INTELLIGENCE
LAB
SEMESTER 5 TH

PREPARED AS PER THE SYLLABUS PRESCRIBED BY

CHHATTISGARH SWAMI VIVEKANAND TECHNICAL UNIVERSITY, BHILAI


List of DOs & DON’Ts.

(Give instructions as per < Name Of The Department > Laboratories)

DOs:

 Remove your shoes outside the laboratory.

 Come prepared in the lab regarding the experiment to be performed in the


lab.

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

 Maintain silence and proper discipline in the lab.

 Enter your machine number in the Login register.

DON’Ts :

 Do not bring any magnetic material in the lab.

 Do not eat or drink any thing in the lab.

 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

Aim : 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
Experiment No. 3

Aim : 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
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

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([],_,[]).
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

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

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

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

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
Experiment No. 10

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,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

Aim : Write a program to search any goal given an input graph


using AO* algorithm.

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

a_star_search_loop(PQueue, Visited, Path) :-


peek_pqueue(PQueue, _, CurrentNode, _, _),
pop_pqueue(PQueue, _, _, _, NewPQueue),
findall(NextNode, (edge(CurrentNode, NextNode, _), \+
member(NextNode, Visited)), NextNodes),
update_queue(NewPQueue, CurrentNode, NextNodes,
UpdatedQueue),
a_star_search_loop(UpdatedQueue, [CurrentNode | Visited],
Path).

update_queue(Queue, _, [], Queue).


update_queue(Queue, Parent, [Node | Nodes], UpdatedQueue) :-
edge(Parent, Node, Cost),
priority(Queue, _, ParentPriority),
heuristic(Node, HeuristicValue),
NewPriority is ParentPriority + Cost + HeuristicValue,
add_to_pqueue(Queue, NewPriority, Node, Parent,
TempQueue),
update_queue(TempQueue, Parent, Nodes, UpdatedQueue).

reconstruct_path(PQueue, AccPath, Path) :-


peek_pqueue(PQueue, _, Node, Parent, _),
Parent \== nil,
pop_pqueue(PQueue, _, _, _, NewPQueue),
reconstruct_path(NewPQueue, [Node | AccPath], Path).
reconstruct_path(_, Path, Path).

% Sample query
:- initialization(main).
main :-
a_star_search(a, Path),
writeln(Path).

You might also like