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

AI Experiment2

This document describes the water jug problem using a Python implementation. It represents the state of the problem as a tuple (x, y) indicating the contents of a 4-gallon and 3-gallon jug. It defines 12 rules for how water can be moved between the jugs and poured or emptied. The code uses a breadth-first search algorithm with queue to systematically explore all possible states from the initial state (0,0) until reaching the goal state of (2,n).

Uploaded by

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

AI Experiment2

This document describes the water jug problem using a Python implementation. It represents the state of the problem as a tuple (x, y) indicating the contents of a 4-gallon and 3-gallon jug. It defines 12 rules for how water can be moved between the jugs and poured or emptied. The code uses a breadth-first search algorithm with queue to systematically explore all possible states from the initial state (0,0) until reaching the goal state of (2,n).

Uploaded by

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

AI experiment2

Water jug problem in python implementation

State representation: (x, y)

–x: Contents of four gallon

–y: Contents of three gallon

•Start state: (0, 0)

•Goal state (2, n)

•Operators

–Fill 3-gallon from pump, fill 4-gallon from pump

–Fill 3-gallon from 4-gallon , fill 4-gallon from 3-gallon

–Empty 3-gallon into 4-gallon, empty 4-gallon into 3-gallon

–Dump 3-gallon down drain, dump 4-gallon down drain

Rules for water jug problem

1. (x, y) -----> (4, y) if x is less than 4, fill the 4 gallon jug

if x < 4

2. (x, y)----->(x, 3) fill the 3-gallon jug

if y < 3

3. (x, y)----->(x - d, y) pour some water out of the 4 gallon jug

if x > 0

4. (x, y)----->(x, y - d) pour some water out of the 3 gallon jug

if y > 0

5. (x, y)----->(0, y) empty the 4-gallon jug on the ground

if x > 0

6. (x, y)----->(x, 0) empty the 3-gallon jug on the ground

if y >0
. 7. (x, y) ----->(4, y -(4 - x))

if x + y >= 4, y > 0 pour water from the 3- gallon jug into the 4-gallon jug until the 4-gallon jug is full

8. (x, y) ----->(x - (3 - y), 3)

if x + y >= 3, x > 0 ) pour water from the 4- gallon jug into the 3-gallon jug until the 3-gallon is full

9. (x, y) ----->(x + y, 0)

if x + y <= 4, y > 0 pour all the water from the 3-gallon jug into the 4-gallon jug

10. (x, y) ----->(0, x + y)

if x + y <= 3, x > 0 pour all the water from the 4-gallon jug into the 3-gallon jug

11. (0, 2) ----->(2, 0) pour the 2 gallons from the 3-gallon jug into the 4-gallon jug

12. (2, y) ----->(0, y) empty the 2 gallons in the 4 gallon jug on the ground \

Code:

from collections import deque

def BFS(a, b, target):


# Map is used to store the states, every

# state is hashed to binary value to

# indicate either that state is visited

# before or not

m = {}

isSolvable = False

path = []

# Queue to maintain states

q = deque()

# Initializing with initial state

q.append((0, 0))

while (len(q) > 0):

# Current state

u = q.popleft()

# q.pop() #pop off used state

# If this state is already visited

if ((u[0], u[1]) in m):

continue

# Doesn't met jug constraints

if ((u[0] > a or u[1] > b or

u[0] < 0 or u[1] < 0)):


continue

# Filling the vector for constructing

# the solution path

path.append([u[0], u[1]])

# Marking current state as visited

m[(u[0], u[1])] = 1

# If we reach solution state, put ans=1

if (u[0] == target or u[1] == target):

isSolvable = True

if (u[0] == target):

if (u[1] != 0):

# Fill final state

path.append([u[0], 0])

else:

if (u[0] != 0):

# Fill final state

path.append([0, u[1]])

# Print the solution path

sz = len(path)

for i in range(sz):

print("(", path[i][0], ",",

path[i][1], ")")
break

# If we have not reached final state

# then, start developing intermediate

# states to reach solution state

q.append([u[0], b]) # Fill Jug2

q.append([a, u[1]]) # Fill Jug1

for ap in range(max(a, b) + 1):

# Pour amount ap from Jug2 to Jug1

c = u[0] + ap

d = u[1] - ap

# Check if this state is possible or not

if (c == a or (d == 0 and d >= 0)):

q.append([c, d])

# Pour amount ap from Jug 1 to Jug2

c = u[0] - ap

d = u[1] + ap

# Check if this state is possible or not

if ((c == 0 and c >= 0) or d == b):

q.append([c, d])

# Empty Jug2

q.append([a, 0])
# Empty Jug1

q.append([0, b])

# No, solution exists if ans=0

if (not isSolvable):

print("No solution")

# Driver code

if __name__ == '__main__':

Jug1, Jug2, target = 4, 3, 2

print("Path from initial state "

"to solution state ::")

BFS(Jug1, Jug2, target)

Output:

You might also like