You are on page 1of 3

AI Lab using PYTHON | 19FM1456 Raj

Artificial Intelligence Lab using PYTHON


Date:

Practical No.
Write a program to implement Water Jug Problem using PYTHON

PROBLEM STATEMENT
You are given two jugs, a 4-gallon one and a 3-gallon one, a pump which has unlimited
water which you can use to fill the jug, and the ground on which water may be poured.
Neither jug has any measuring markings on it. How can you get exactly 2 gallons of
water in the 4-gallon jug?

STATE REPRESENTATION AND INITIAL STATE


we will represent a state of the problem as a tuple (x, y) where x represents the
amount of water in the 4-gallon jug and y represents the amount of water in the 3-
gallon jug. Note 0 ≤ x ≤ 4, and 0 ≤ y ≤ 3. Our initial state: (0,0)

GOAL PREDICATE
state = (2,y) where 0 ≤ y ≤ 3.

OPERATORS
we must define a set of operators that will take us from one state to another:

Page 1 of 3
AI Lab using PYTHON | 19FM1456 Raj

Through Graph Search, the following solution is found:

Code
x = 0
y = 0
m = 4
n = 3
print("Initial state = (0,0)")
print("Capacities = (4,3)")
print("Goal state = (2,y)")
while x != 2:
r = int(input("Enter rule"))
if(r == 1):
x = m
elif(r == 2):
y = n
elif(r == 3):
x = 0
elif(r == 4):
y = 0
elif(r == 5):
t = n - y
y = n
x -= t
elif(r == 6):
t = m - x
x = m
y -= t
elif(r == 7):
y += x
x = 0
elif(r == 8):
x += y
y = 0
print (x, y)

Page 2 of 3
AI Lab using PYTHON | 19FM1456 Raj

OUTPUT

Page 3 of 3

You might also like