Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Experiment No- 02

AIM: Write a program in Python or Prolog to implement Water jug


problem.

Theory:

Water Jug Problem Definition:


“We are given two jugs, a 4-liter one and a 3-liter 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 by using only these two jugs and no other material. Initially,
both our jugs are empty.

State: (x, y) where x represents the quantity of water in a 4-liter jug and y represents the
quantity of water in a 3-liter jug.
That is, x = 0, 1, 2, 3, or 4 y = 0, 1, 2, 3
Start state: (0, 0).
Goal state: (2, n) for any n.

Production Rules for Water Jug Problem in Artificial Intelligence

1 (x, y) is X<4 ->(4, Y) Fill the 4-liter jug

2 (x, y) if Y<3 -> (x, 3) Fill the 3-liter jug

3 (x, y) if x>0 -> (x-d, d) Pour some water out of the 4-liter jug.

4 (x, y) if Y>0 -> (d, y-d) Pour some water out of the 3-liter jug.

5 (x, y) if x>0 -> (0, y) Empty the 4-liter jug on the ground
6 (x, y) if y>0 -> (x,0) Empty the 3-liter jug on the ground

(x, y) if X+Y >= 4 and y>0 Pour water from the 3-liter jug into the 4-liter jug until
7
-> (4, y-(4-x)) the 4-liter jug is full

(x, y) if X+Y>=3 and x>0 Pour water from the 4-liter jug into the 3-liter jug until
8
-> (x-(3-y), 3)) the 3-liter jug is full.

(x, y) if X+Y <=4 and y>0 Pour all the water from the 3-liter jug into the 4-liter
9
-> (x+y, 0) jug.

(x, y) if X+Y<=3 and x>0 Pour all the water from the 4-liter jug into the 3-liter
10
-> (0, x+y) jug.

Pour the 2-liter water from the 3-liter jug into the
11 (0, 2) -> (2, 0)
4-liter jug

12 (2, Y) -> (0, y) Empty the 2-liter in the 4-liter jug on the ground.

Solution of water jug problem according to the production rules

S.No. 4 gallon jug contents 3 gallon jug contents Rule followed

1. 0 gallon 0 gallon Initial state

2. 4 0 Rule no.1
3. 1 3 Rule no. 8

4. 1 0 Rule no. 6

5. 0 1 Rule no. 10

6. 4 1 Rule no. 1

7. 2 3 Rule no. 8

On reaching the 7th attempt, we reach a state which is our goal state. Therefore, at this
state, our problem is solved.

Code:

def water_jug_problem(jug1_cap, jug2_cap, target_amount):


# Initialize the jugs and the possible actions
j1 = 0
j2 = 0
actions = [("fill", 1), ("fill", 2), ("empty", 1), ("empty", 2), ("pour", 1, 2), ("pour", 2, 1)]
# Create an empty set to store visited states
visited = set()
# Create a queue to store states to visit
queue = [(j1, j2, [])]
while queue:
# Dequeue the front state from the queue
j1, j2, seq = queue.pop(0)
# If this state has not been visited before, mark it as visited
if (j1, j2) not in visited:
visited.add((j1, j2))
if j1 == target_amount:
return seq
# Generate all possible next states from this state
for action in actions:
if action[0] == "fill":
if action[1] == 1:
next_state = (jug1_cap, j2)
else:
next_state = (j1, jug2_cap)
elif action[0] == "empty":
if action[1] == 1:
next_state = (0, j2)
else:
next_state = (j1, 0)
else:
if action[1] == 1:
amount = min(j1, jug2_cap - j2)
next_state = (j1 - amount, j2 + amount)
else:
amount = min(j2, jug1_cap - j1)
next_state = (j1 + amount, j2 - amount)

if next_state not in visited:


next_seq = seq + [action]
queue.append((next_state[0], next_state[1], next_seq))
# If the queue becomes empty without finding a solution, return None

result = water_jug_problem(4, 3, 2)
print(result)

Output:

[('fill', 1), ('pour', 1, 2), ('empty', 2), ('pour', 1, 2), ('fill', 1), ('pour', 1, 2)]

You might also like