0% found this document useful (0 votes)
55 views

KCS751A

Uploaded by

nushrao2203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

KCS751A

Uploaded by

nushrao2203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

Shri Ram Murti Smarak College of Engineering &

Technology Bareilly SRMS


Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

PART C: LAB MANUAL


Semester/Session Even (2024--25) Class B.Tech
Course Code KCS-751A Course Title Artificial Intelligence Lab
Hours 20 Hours/week 3
Faculty Name Dr.Jyoti Agarwal Contact Details [email protected]
Course The course should enable the students to learn how to analyze a problem
Description and design the solution for the problem.
The main objectives of this course are given as follow:
1. Design and implement efficient algorithms for a specified application.
2. Strengthen the ability to identify and apply the suitable algorithm for
the given real world problem.
Course Outcome and Bloom’s Taxonomy
Bloom
CO Statements
Taxonomy
CO1 Students are able to learn different logic Programming Languages K1, K2

Students are able to apply and analyse various AI Problem Solving K2, K3
CO2 Techniques on AI Problems.

Students acquire skills to identify the given problem and design the rule K3, K4, K1
CO3 based system.

Students develop better understanding to represent real life problem K5, K3


CO4 domains using logic based techniques and used this to perform
inference.
Students understand the working knowledge in Prolog and K2, K3, K6
CO5 demonstrate that for solving AI problems.
CO-CIA Mapping
Components of Assessment
CO
CIA I CIA II CIA III CIA IV CIA V CIA VI CIA VII CEA I
CO1 Yes Yes Yes No No No No Yes
CO2 Yes Yes Yes No No No No Yes
CO3 Yes Yes Yes No No No No Yes
CO4 Yes Yes Yes No No No No Yes
CO5 Yes Yes Yes No No No No Yes
CO-PO Mapping
Course PO
PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
Outcome 1
CO1 2 2 2 1 - - - - - - - -

CO2 2 2 2 1 - - - - - - - -

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

CO3 2 2 3 1 - - - - - - - -

CO4 2 2 3 1 - - - - - - - -
CO5 2 2 3 1 - - - - - - - -
AVG 2 2 2.6 1 - - - - - - - 1

1- Less Relevant; 2 – Moderately relevant; 3 – Highly relevant

CO-PSO Mapping

PSO 1 PSO2 PSO3


CO1 2 2 1
CO2 2 2 1
CO3 3 2 1
CO4 3 2 1
CO5 3 2 1
Avg. 2.6 2 1
1- Less Relevant; 2 – Moderately relevant; 3 – Highly relevant

DESIGN AND ANALYSIS OF ALGORITHM LAB (KCS 553)


List of Experiments:
Sr. No. Experiments CO BL
1. Study of Prolog. 1 K2

2. Write simple fact for the statements using PROLOG. 1 K3


3. Write predicates one converts centigrade temperatures to 1 K3
Fahrenheit, the other checks if a temperature is below
freezing.

4. Write a program to solve the Monkey Banana problem. 2 K3

5. WAP in turbo prolog for medical diagnosis and show the 3 K3


advantage and disadvantage of green and red cuts.

6. WAP to implement factorial, fibonacci of a given number. 2 K3

7. Write a program to solve 4-Queen problem. 4 K3


8. Write a program to solve traveling salesman problem. 4 K3
9. Write a program to solve water jug problem using LISP 5 K3

Assignment No – 1
Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

OBJECTIVE: STUDY OF PROLOG


PROLOG stands for Programming In Logic – an idea that emerged in the early 1970s to use logic as
programming language. The early developers of this idea included Robert Kowalski at Edinburgh (on the
theoretical side), Marrten van Emden at Edinburgh (experimental demonstration) and Alain Colmerauer at
Marseilles (implementation).
David D.H.Warren’s efficient implementation at Edinburgh in the mid – 1970’s greatly contributed to the
popularity of PROLOG.
PROLOG is a programming language centered on a small set of basic mechanisms, including pattern matching,
tree-based data structuring and automatic backtracking. This small set constitutes a surprisingly powerful and
flexible programming framework.
PROLOG is especially well suited for problems that involve objects – in particular, structured objects – and
relations between them.
SYMBOLIC LANGUAGE
PROLOG is a programming language for symbolic, non – numeric computation. It is especially well suited for
solving problems that involve objects and relations between objects.
For example, it is an easy exercise in prolog to express spatial relationship between objects, such as the blue
sphere is behind the green one. It is also easy to state a more general rule: if object X is closer to the observer
than object Y, and Y is closer than Z, then X must be closer than Z. PROLOG can reason about the spatial
relationships and their consistency with respect to the general rule. Features like this make PROLOG a
powerful language for Artificial Language (A1) and non – numerical programming.
There are well-known examples of symbolic computation whose implementation in other standard languages
took tens of pages of indestiblecode . When the same algorithms were implemented in PROLOG, the result was
a cryetal-clear program easily fitting on one page.
FACTS, RULES AND QUERIES
Programming in PROLOG is accomplished by creating a database of facts and rules about objects, their
properties, and their relationships to other objects. Queries then can be posed about the objects and valid
conclusions will be determined and returned by the program. Responses to user queries are determined through
a form of inferencing control known as resolution.
FOR EXAMPLE:

a) FACTS:
Some facts about family relationships could be written as :
sister(sue,bill)
Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

parent(ann,sam)
male(jo)
female(riya)

b) RULES:
To represent the general rule for grandfather, wewrite:
grandfather(X,Z)
parent(X,Y)
parent(Y,Z)
male(X)

c) QUERIES:
Given a database of facts and rules such as that above, we may make queries by typing after a query a symbol
‘?_’ statements such as:
?_ parent(X,sam)
X=ann
?_grandfather(X,Y)
X=jo, Y=sam
PROLOG in Designing Expert Systems
An Expert System is a set of programs that manipulates encoded knowledge to solve problems in a specialized
domain that normally requires human expertise. An Expert system’s knowledge is obtained from the expert
sources such as texts, journals articles, databases etc. and coded in a form suitable for the system to use in its
inference or reasoning processes. Once a sufficient body of Expert knowledge has been acquired, it must be
encoded in some form, loaded into knowledge base, then tested, and refined continually throughout the life of
the system.

PROLOG serves as a powerful language in designing expert systems because of its following features:
 Use knowledge rather thandata.
 Modification of the knowledge base without recompilation of controlprograms.
Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

 Capable of explaingconclusions.
 Symbolic computations resembling manipulations of natural language.
 Reason withmeta-knowledge.
META-PROGRAMMING
A meta-program is a program that other programs as data. Interpreters and compilers are examples of meta-
programs. Meta-interpreter is a particular kind of meta-program: an interpreter for a language written in that
language. So a PROLOG meta-interpreter is an interpreter for PROLOG, itself written in PROLOG.
Due to its symbol-manipulation capabilities, prolog is a powerful language for meta- programming. Therefore,
it is often used as an implementation language for other languages. PROLOG is particularly suitable as a
language for rapid prototyping where we are interested in implementing new ideas quickly. New ideas are
rapidlyimplemented and experimented with. In prototyping the emphasis is on bringing new ideas to life
quickly and cheaply, so that they can be immediatelytested.
On the other hand, there is not much emphasis on efficiency of implementation. Once the ideas are developed, a
prototype may have to be re-implemented, possibly in another, more efficient programming language. Even if
this is necessary, the prototype is useful because it usually helps to speed up the creative development stage.

VIVA QUESTION

S.No. Questions Marks CO BL


1. Study How to Program in Prolog and its working Environment. 2.5 1 1
2. Explain What Is Swi-prolog? 2.5 1 2

OUTCOME: Students will get the basic idea of how to program in prolog and its working
environment

ASSIGNMENT 2
OBJECTIVE: Write simple fact for following:
Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

a. Ram likes mango.


b. Seema is a girl.
c. Bill likes Cindy.
d. Rose is red.
e. John owns gold.

Program:
Clauses
likes(ram ,mango).
girl(seema).
red(rose).
likes(bill ,cindy).
owns(john ,gold).

Output:
Goal
queries
?-likes(ram,What).
What= mango
?-likes(Who,cindy).
Who= cindy
?-red(What).
What= rose
?-owns(Who,What).
Who= john
What= gold.

VIVA QUESTION

S.No. Questions Marks CO BL


1. suppose our database consists of the following facts about a 2.5 1 2
fictitious family.
father_of(joe,paul).
father_of(joe,mary).
mother_of(jane,paul).
mother_of(jane,mary).
male(paul).
male(joe).
female(mary).
female(jane). Find who is father of mary?
2 friends(X,Y) :- likes(X,Y),likes(Y,X). /* X and Y 2.5 1 3
are friends if they like each other */
hates(X,Y) :- not(likes(X,Y)). /* X hates Y if X
does not like Y. */
enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)). Write

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

fact forabove statment

Assignment 3
OBJECTIVE: Write predicates one converts centigrade temperatures to Fahrenheit, the other checks if a
temperature is below freezing.

Program:

Production rules:

Arithmetic:
c_to_f →f is c * 9 / 5 +32
freezing→ f < = 32
Rules:c_to_f(C,F) :-
F is C * 9 / 5 + 32.
freezing(F) :-
F =< 32.

Output:
Queries:
?- c_to_f(100,X).
X = 212
Yes
?- freezing(15) .
Yes
?- freezing(45).
No

VIVA QUESTION
S.No. Questions Marks CO BL
1. What is horn clause? 2.5 2 2
2. 2.5 2 1

Assignment 4

OBJECTIVES: PROGRAM TO SOLVE MONEY BANANA PROBLEM

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

Imagine a room containing a monkey, chair and some bananas. That have been hanged from the centre 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.The monkey wants it, but cannot jump high enough
from the floor. At the window of the room there is a box that the monkey can use. The monkey can perform the following
actions:-
1) Walk on the floor.
2) Climb the box.
3) Push the box around (if it is beside the box).
4) Grasp the banana if it is standing on the box directly under the banana.
Production Rules
can_reach→ clever,close.
get_on: →can_climb.
under →in room,in_room, in_room,can_climb.
Close →get_on,under| tall

Parse Tree:

Clauses:
in_room(bananas).
in_room(chair).
in_room(monkey).
clever(monkey).
can_climb(monkey, chair).
tall(chair).
can_move(monkey, chair, bananas).
can_reach(X, Y):-
clever(X),close(X, Y).
get_on(X,Y):- can_climb(X,Y).
under(Y,Z):-
in_room(X),in_room(Y),in_room(Z),can_climb(X,Y,Z).
close(X,Z):-get_on(X,Y),
under(Y,Z);
tall(Y).

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

Output:
Queries:
?- can_reach(A, B).
A = monkey.
B = banana.
?- can_reach(monkey, banana).
Yes.

VIVA QUESTION

S.No. Questions Marks CO BL


1. How Backtracking is done in Prolog. 2.5 1 3
2. 2.5 1 2

Assignment 5

OBJECTIVE: WAP in turbo prolog for medical diagnosis and show the advantage and disadvantage of green and
red cuts.

Program:

Domains:
disease,indication=symbol
name-string
Predicates:
hypothesis(name,disease)
symptom(name,indication)
response(char)
go
goonce
Clauses
go:-
goonce
write("will you like to try again (y/n)?"),
response(Reply),
Reply='n'.
go.
goonce:-
write("what is the patient's name"),nl,
readln(Patient),
hypothesis(Patient,Disease),!,

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

write(Patient,"probablyhas",Disease),!,
goonce:-
write("sorry, i am not ina position to diagnose"),
write("the disease").
symptom(Patient,fever):-
write("does",Patient,"has a fever (y/n)?"),nl,
response(Reply),
Reply='y',nl.
symptom(Patient,rash):-
write ("does", Patient,"has a rash (y/n)?" ),nl,
response(Reply),
Reply='y',
symptom(Patient_body,ache):-
write("does",Patient,"has a body ache (y/n)?"),nl,
response(Reply).
Reply='y',nl.
symptom(Patient,runny_nose):-
write("does",Patient,"has a runny_nose (y/n)?"),
response(Reply),
Reply='y'
hypothesis(Patient,flu):-
symptom(Patient,fever),
symptom(Patient,body_ache),
hypothesis(Patient,common_cold):-
symptom(Patient,body_ache),
Symptom(Patient,runny_nose).
response(Reply):-
readchar(Reply),
write(Reply).
Output: makewindow(1,7,7"Expert Medical Diagnosis",2,2,23,70),
go.

VIVA QUESTION

S.No. Questions Marks CO BL


1. What are the applications of AI in health 2.5 1 3
2. Write the fact for finding patient has flu or not 2.5 1 4

Assignment 6
OBJECTIVES: WAP to implement Factorial, Fibonacci of a given number.

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

ALGORITHM TO CALCULATE FACTORIAL OF A GIVEN NUMBER


Step 1. Start
Step 2. If the number is 0 then return 1 and goto step5.
Step 3. Call the function with a number one less than the original number
Step 4. Return product of number received in step 3 and number itself asfactorial
Step 5. Exit
Source code
domains
X = integer

predicaes
fact(X,X)

clauses fact(0,1):-!.
fact(No,Fac):-
N = No -1, Fact(N,F),
Fac = No*F.

OUTPUT:Fact (5, X) X=120


Fibonacci:
domains
X,Y=integer

predicaes
fib(0, 0).
clauses
fib(X, Y) :- X > 0, fib(X, Y, _).
fib(1, 1, 0).
fib(X, Y1, Y2) :-
X > 1,
X1 is X - 1,
fib(X1, Y2, Y3),
Y1 is Y2 + Y3.

Output:
Goal:
?-fib(10,X).
X=55

VIVA QUESTION

S.No. Questions Marks CO BL


1. What is Recursion in PROLOG. 2.5 2 3
2. What are the applications of Prolog. 2.5 2 3

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

OUTCOME: Student will understand the implementation of Fibonacci and factorial series

using prolog.

Assignment no – 7

OBJECTIVE: Write a program to solve 4-Queen problem.

Program:
In the 4 Queens problem the object is to place 4 queens on a chessboard in such a way that no queens can
capture a piece. This means that no two queens may be placed on the same row, column, or diagonal.

The n Queens Chessboard.


domains
queen = q(integer, integer)
queens = queen*
freelist = integer*
board = board(queens, freelist, freelist, freelist, freelist)

predicates
nondetermplaceN(integer, board, board)
nondetermplace_a_queen(integer, board, board)
nondetermnqueens(integer)
nondetermmakelist(integer, freelist)
nondetermfindandremove(integer, freelist, freelist)
nextrow(integer, freelist, freelist)

clausesnqueens(N):-
makelist(N,L),
Diagonal=N*2-1,
makelist(Diagonal,LL),
Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

placeN(N,board([],L,L,LL,LL),Final), write(Final).
placeN(_,board(D,[],[],D1,D2),board(D,[],[],D1,D2)):-!.
placeN(N,Board1,Result):-
place_a_queen(N,Board1,Board2),
placeN(N,Board2,Result).
place_a_queen(N,
board(Queens,Rows,Columns,Diag1,Diag2),
board([q(R,C)|Queens],NewR,NewC,NewD1,New D2)):-
nextrow(R,Rows,NewR),
findandremove(C,Columns,NewC),
D1=N+C-R,findandremove(D1,Diag1,NewD1),
D2=R+C-1,findandremove(D2,Diag2,NewD2).
findandremove(X,[X|Rest],Rest).
findandremove(X,[Y|Rest],[Y|Tail]):-
findandremove(X,Rest,Tail).
makelist(1,[1]).
makelist(N,[N|Rest]) :-
N1=N-1,makelist(N1,Rest).
nextrow(Row,[Row|Rest],Rest).

Output:
Goal:
?-nqueens(4),nl. board([q(1,2),q(2,4),q(3,1),q(4,3),[],[],[7,4,1],[7,4,1])
yes

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

VIVA QUESTION

S.No. Questions Marks CO BL


1. Write a program to solve 8-queen problem. 2.5 3 1
2. Write the 8-queen problem statement 2.5 3 3

Assignment 8
OBJECTIVE: Write a program to solve traveling salesman problem.

The following is the simplified map used for the prototype:

Program:

Production Rules:-

route(Town1,Town2,Distance)
road(Town1,Town2,Distance).
route(Town1,Town2,Distance)
road(Town1,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2, domains
town = symbol
distance = integer predicates
nondeterm road(town,town,distance)
Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

nondeterm route(town,town,distance)

clauses
road("tampa","houston",200).
road("gordon","tampa",300).
road("houston","gordon",100).
road("houston","kansas_city",120).
road("gordon","kansas_city",130).
route(Town1,Town2,Distance):-
road(Town1,Town2,Distance).
route(Town1,Town2,Distance):-
road(Town1,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2,!.

Output:
Goal:
route("tampa", "kansas_city", X),
write("Distance from Tampa to Kansas City is ",X),nl.

Distance from Tampa to Kansas City is 320


X=320

VIVA QUESTION

S.No. Questions Marks CO BL


1. Which algorithm is used for the Travelling salesman problem? 2.5 4 2
2. What is the complexity of the Travelling salesman problem? 2.5 4 1

Assignment 9
OBJECTIVE: Write a program to solve water jug problem using LISP.

Program:
;returns the quantity in first jug (defun get-first-jug
(state) (carstate))

;returns the quantity in second jug


(defun get-second-jug (state) (cadr state))

;returns the state of two jugs (defun get-


state (f s) (list f s))

;checks whether a given state is a goal


; GOAL IS TO GET 4 IN SECOND JUG

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

(defun is-goal (state)


(eq (get-second-jug state) 4))

;returns all possible states that can be derived


;from a given state
(defun child-states(state)
(remove-null
(list
(fill-first-jug state)
(fill-second-jug state)
(pour-first-second state)
(pour-second-first state)
(empty-first-jug state)
(empty-second-jug state))))

;remove the null states (defun remove-null (x)


(cond ((null x) nil)
((null (car x)) (remove-null (cdr x)))
((cons (car x) (remove-null (cdr x))))))

;return the state when the first jug is filled (first jug can hold 3)
(defun fill-first-jug (state)
(cond
((< (get-first-jug state) 3) (get-state 3 (get-second-jug state))))))

;returns the state when the second jug is filled (second jug can hold 5) (defun fill-second-jug
(state)
(cond
((< (get-second-jug state) 5) (get-state (get-first-jug state) 5))))

;returns the state when quantity in first


;is poured to second jug
(defun pour-first-second (state)
(let ( (f (get-first-jug state))
(s (get-second-jug state)))
(cond
((zerop f) nil) ; first jug is empty
((= s 5) nil) ;Second jug is full
((<= (+ f s) 5)
(get-state 0 (+ f s)))
(t ; pour to first from second
(get-state (- (+ f s) 5)5)))))

;returns the state when second jug is poured to first


(defun pour-second-first (state)
(let ( (f (get-first-jug state)) (s (get-second-
jug state)))
(cond
Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

((zerop s) nil) ; second jug is empty ((= f 3) nil) ;


second jug is full
((<= (+ f s) 3)
(get-state (+ f s) 0))
(t ;pour to second from first
(get-state 3 (- (+ f s)3))))))

;returns the state when first jug is emptied


(defun empty-first-jug (state)
(cond
((> (get-first-jug state) 0) (get-state 0 (get-second-jug state)))))

;returns the state when second jug is emptied


(defun empty-second-jug (state)
(cond
((> (get-second-jug state) 0) (get-state (get-first-jug state) 0))))

;;;MAIN FUNCTION
(defundfs (start-state depth lmt) (setf *node* 0)
(setf *limit* lmt)
(dfs-node start-state depth)
)

;dfs-node expands a node and calls dfs-children to recurse on it


(defundfs-node (state depth)
(setf *node* (+ 1 *node*)) (cond
((is-goal state) (list state)) ((zerop
depth) nil)
((> *node* *limit*) nil)
((let ((goal-child (dfs-children (child-states state) (- depth 1))))
(and goal-child (cons state goal-child)))) ;for back-tracking if the branch don't have a goal
state
))

;dfs-children expands each node recursively and give it


;todfs-node to process
(defundfs-children (states depth) (cond
((null states) nil)
((dfs-node (car states) depth)) ((dfs-children (cdr
states) depth))))

(print "ENTER YOUR INPUT AS")


(print "(dfsstart_state depth limit)")

VIVA QUESTION

Reviewed By

Faculty DQAC HOD (CSE)


Shri Ram Murti Smarak College of Engineering &
Technology Bareilly SRMS
Department of Computer Science & Engineering
Course File – Sub. Code: KCS-751A – Sub. Name: Class: B.Tech. Computer Science & Engineering 7th
Artificial Intelligence Lab Semester (2019 Batch)
Session: Odd/Even 2022-2023; Faculty – Dr. Jyoti Agarwal

S.No. Questions Marks CO BL


1. Which algorithm is used for water jug problem? 2.5 3 3
2. You have given two jugs, a 4-liter one and a 3-liter 2.5 3 4
one. Neither has any measuring markers on it. There
is a pump that can be used to fill the jugs with
water. How can you get exactly 2 liters of water into
a 4-liter jug.” Write the production rules.

Reviewed By

Faculty DQAC HOD (CSE)

You might also like