100% found this document useful (1 vote)
133 views8 pages

Artificial Intelligence Lab-2

The document provides algorithms to solve water jug problems with 2 jugs, 3 jugs, and for general cases. It includes pseudocode for initializing the problem, reading input values, applying pouring rules, and checking if the goal state is reached. Python code is also provided to implement solutions for sample 2 jug and 3 jug problems.

Uploaded by

Avinash Alla
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
100% found this document useful (1 vote)
133 views8 pages

Artificial Intelligence Lab-2

The document provides algorithms to solve water jug problems with 2 jugs, 3 jugs, and for general cases. It includes pseudocode for initializing the problem, reading input values, applying pouring rules, and checking if the goal state is reached. Python code is also provided to implement solutions for sample 2 jug and 3 jug problems.

Uploaded by

Avinash Alla
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/ 8

ARTIFICIAL INTELLIGENCE LAB-2

NAME: A.AVINASH
REG ID:190030049

PRE-LAB
1) Write an algorithm to solve the water jug problem.
Simple Algorithm:
. Step1: Read
i. capacities of each jar
ii. initial states of jugs and store in a list
iii. Read Goal states of jugs and store in a list Step2:
Data structures used
i. path = [] contains states that are explored as [x,y] #closed
ii. visited_states = [a,b] contains generated states to be explored #open
iii. curr_state = [] contains current state as [x,y]
iv. parent contains states as [px,py] initialize to ['NA'] Step3:
Apply BFS algorithm:
Do until visited_state is EMPTY:
curr_state <- Get first element from visited_state add curr_state
to path
if curr_state == goal_state: exit loop
else next_state_generator(curr_state,visited_states,parent)
Step 4: Use path and parent lists track the parent of goal state, grand parent of goal state and so on.. store in
a solution list
Step 5: Display solution list in reverse order which displays from initial state to goal state 2-Jug

Problem Algorithm:

Step1: Start
Step2: Read the initial values of the container a,b. Step3:
Read the container capacities x,y
Step4: Read the goal state g. Step5: 5.1:
While (True):
5.1.1 : Read the Rule No
5.1.1.1 :if(ruleno==1): if a<x : a=x
if(ruleno==2): if b<y:b=y
if(ruleno==3): if a>0:a=0
if(ruleno==4): if b>0:b=0
if(ruleno==5): if a+b>=x and b>0:a,b=x,b-(x-
a) if(ruleno==6): if a+b>=y and a>0:a,b=a-
(x-b),y if(ruleno==7): if a+b<=x and
b>0:a,b=a+b,0 if(ruleno==8): if a+b<=y and
a>0:a,b=0,a+b print ("A=”, a)
print ("B=”, b)
if (a==z and b==0):
print ("The goal state is
reached") break

Step6: Stop

3-Jug Problem Algorithm:

Step1: Start
Step2: Read the container capacities x,y,z
Step3: Read the goal state g
Step4: Declare a,b,c and initialize with z,y,z values
Step5:5.1:While(True):
5.1.1 :Read the Rule No
5.1.1.1 : if(ruleno==1):if
x<a:x=a
if(ruleno==2):if
y<b:y=b
if(ruleno==3):if
z<c:z=c
if(ruleno==4):if
x>0:x=0
if(ruleno==5):if
y>0:y=0
if(ruleno==6):if
z>0:z=0 if(ruleno==7):
if x+y>=a and y>0:x,y=a,y-(a-
x) if(ruleno==8):
if x+y<=a and y>0:x,y=x+y,0
if(ruleno==9):
if x+y>=b and x>0:x,y=x-(b-
y),b if(ruleno==10):
if x+y<=b and x>0:x,y=0,x+y
if(ruleno==11):
if y+z>=a and z>0:y,z=a,z-(a-
y) if(ruleno==12):
if y+z<=a and z>0:y,z=y+z,0
if(ruleno==13):
if y+z>=c and y>0:y,z=y-(c-
z),c if(ruleno==14):
if y+z<=c and
y>0:y,z=0,y+z if(ruleno==15):
if z+x>=b and z>0:x,z=b,z-(b-
x) if(ruleno==16):
if z+x<=b and z>0:x,z=x+z,0
if(ruleno==17):
if z+x>=c and x>0:x,z=x-(c-
z),c if(ruleno==18):
if z+x<=c and
x>0:x,z=0,x+z print("X=",x)
print("Y=",y)
print("Z=",z)
if(x==c or y==c or z==c):
print("The goal state is
reached") break

Step6: Stop

2. You have two jugs with capacities x and y liters. There is an infinite amount of water
supply available to you. Now you need to determine whether it is possible to measure z
liters using these two jugs. If z liters of water are measurable, you must have z liters
contained within one or both jugs by the end.
We can do these few operations −
• Fill any of the jugs fully with water.
• Empty any of the jugs.
• Pour water from one jug into another till the other jug is completely full or the first
jug itself is empty.

print("**WATER JUG PROBLEM**")


# INITIALLY IF x,y LITRES CONTAINS ANY WATER,THEN GIVE a AND b. #
ELSE GIVE a=0 ,b=0 AS INITIAL STATE

x=int(input("Enter 1st Container Capacity"));


y=int(input("Enter 2nd container Capacity"));
z=int(input("Enter the Goal State
Capacity:")); a=int(input("Initial value of 1st
container")); b=int(input("Initial value of 2nd
Container")); while True:
ruleno=int(input("enter the ruleno:"))
if(ruleno==1):
if a<x :
a=x
if(ruleno==2)
:
if b<y:b=y
if(ruleno==3)
: if a>0:a=0
if(ruleno==4)
: if b>0:b=0
if(ruleno==5)
:
if a+b>x and b>0:a,b=x,b-(x-
a) if(ruleno==6):
if a+b>=y and a>0:a,b=a-(x-b),y
if(ruleno==7):
if a+b<=x and
b>0:a,b=a+b,0 if(ruleno==8):
if a+b<=y and a>0:a,b=0,a+b
print("x=",a)
print("y=",b)
if(a==z and
b==0):
print("The goal state is
reached") break

IN-LAB
1.A Water Jug Problem with 2 gallons: You are given two jugs, a 5-gallon one and a 4- gallon
one, a pump which can supply unlimited water that can be used to fill the jugs, and a ground
on which water can be disposed. Neither jug has any measuring markings on it. Implement a
python code to get exactly 2 gallons of water in the 5-gallon jug.

print("**WATER JUG PROBLEM**")


# INITIALLY IF 5,4 LITRES CONTAINS ANY WATER,THEN GIVE a AND b. #
ELSE GIVE a=0,b=0AS INITIAL STATE

x=int(input("Enter 1st Container Capacity"));


y=int(input("Enter 2nd container Capacity"));
z=int(input("Enter the Goal State Capacity:"));
a=int(input("Initial value of 1st container"));
b=int(input("Initial value of 2nd Container"));
while True:
ruleno=int(input("enter the ruleno:"))
if(ruleno==1):
if a<x :
a=x
if(ruleno==2)
:
if b<y:b=y
if(ruleno==3)
: if a>0:a=0
if(ruleno==4)
: if b>0:b=0
if(ruleno==5)
:
if a+b>x and b>0:a,b=x,b-(x-
a) if(ruleno==6):
if a+b>=y and a>0:a,b=a-(x-
b),y if(ruleno==7):
if a+b<=x and b>0:a,b=a+b,0
if(ruleno==8):
if a+b<=y and a>0:a,b=0,a+b
print("x=",a)
print("y=",b)
if(a==z and
b==0):
print("The goal state is
reached") break
POST-LAB

1. A Water jug problem with 3 gallon: You are given three jugs, a 12-gallon one and an 8-
gallon one and a 5-gallon one, a pump which can supply unlimited water that can be used to
fill the jugs, and a ground on which water can be disposed. Neither jug has any measuring
markings on it. Implement a python code to get exactly 6 gallons of water in any of the jug.

#GIVE THE X,Y,Z INPUT AS 12,8,5 LITRES


print("** WATER JUG **")
x=int(input("Enter X value:"))
y=int(input("Enter Y value:"))
z=int(input("Enter Z value:"))
g=int(input("Enter the Goal
State:")) a=x;
b=y
;
c=z;
while True:
ruleno=int(input("Enter the ruleno:"))
if(ruleno==1):
if x<a:x=a
if(ruleno==2)
: if y<b:y=b
if(ruleno==3)
: if z<c:z=c
if(ruleno==4)
: if x>0:x=0
if(ruleno==5)
: if y>0:y=0
if(ruleno==6)
: if z>0:z=0
if(ruleno==7)
:
if x+y>=a and y>0:x,y=a,y-(a-
x) if(ruleno==8):
if x+y<=a and y>0:x,y=x+y,0
if(ruleno==9):
if x+y>=b and x>0:x,y=x-(b-
y),b if(ruleno==10):
if x+y<=b and x>0:x,y=0,x+y
if(ruleno==11):
if y+z>=a and z>0:y,z=a,z-(a-
y) if(ruleno==12):
if y+z<=a and z>0:y,z=y+z,0
if(ruleno==13):
if y+z>=c and y>0:y,z=y-(c-
z),c if(ruleno==14):
if y+z<=c and
y>0:y,z=0,y+z if(ruleno==15):
if z+x>=b and z>0:x,z=b,z-(b-
x) if(ruleno==16):
if z+x<=b and z>0:x,z=x+z,0
if(ruleno==17):
if z+x>=c and x>0:x,z=x-(c-
z),c if(ruleno==18):
if z+x<=c and
x>0:x,z=0,x+z print("X=",x)
print("Y=",y)
print("Z=",z)
if(x==c or y==c or z==c):
print("The goal state is
reached") break

You might also like