0% found this document useful (0 votes)
101 views3 pages

Example of Practical

This document describes a problem to get exactly 2 gallons of water in a 4-gallon jug using two jugs (4-gallon and 3-gallon) and unlimited water. It defines the initial state as (0,0) gallons in each jug, the goal state as (2,y) gallons in the 4-gallon jug, and eight operators to transfer water between jugs. The Python code implements a graph search to find the solution by allowing the user to select operators and tracking the water levels until reaching the goal state.

Uploaded by

Moin Khan Bisru
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)
101 views3 pages

Example of Practical

This document describes a problem to get exactly 2 gallons of water in a 4-gallon jug using two jugs (4-gallon and 3-gallon) and unlimited water. It defines the initial state as (0,0) gallons in each jug, the goal state as (2,y) gallons in the 4-gallon jug, and eight operators to transfer water between jugs. The Python code implements a graph search to find the solution by allowing the user to select operators and tracking the water levels until reaching the goal state.

Uploaded by

Moin Khan Bisru
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/ 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