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

AI Assignement-3

The document describes an assignment to measure 2 gallons of water using two jugs that hold 4 and 3 gallons respectively. It asks the student to represent the problem as a state-space problem by defining the states and operators, and then show a solution path and check for alternative paths. The jugs can be emptied, filled, and their contents transferred between each other until one is empty or full to reach the goal of having 2 gallons measured in one of the jugs.
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)
33 views3 pages

AI Assignement-3

The document describes an assignment to measure 2 gallons of water using two jugs that hold 4 and 3 gallons respectively. It asks the student to represent the problem as a state-space problem by defining the states and operators, and then show a solution path and check for alternative paths. The jugs can be emptied, filled, and their contents transferred between each other until one is empty or full to reach the goal of having 2 gallons measured in one of the jugs.
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/ 3

Assignment details

Water-Jug Problem
------------------
A person wants to fetch 2 gallons of water.
He was given two jugs - one 4, the other 3 gallons. How can he measure the
requested amount of water?

Two jugs of capacity 4 and 3 units.


The jugs have no marker on them to measure the amount of water inside it.

It is possible to empty a jug, fill a jug, transfer the content of a jug


to the other jug until the former empties or the later fills.
There is an infinite source of water to fill the jugs.
Produce a jug with 2 units.

i) Represent the problem as a state-space problem (define the states and


the moves/operators).

ii) Show a solution path (the sequence of operators to reach the goal from
a start state).

iii) Check if there is an alternative solution path. If so, display the


path.
—-------------------------------------------------------------------------

Code :
LAB_Assignment-3.txt

Open wi

from collections import deque


def Solution(a, b, target):
m = {}
isSolvable = False
path = []

q = deque()

q.append((0, 0))

while (len(q) > 0):

u = q.popleft()
if ((u[0], u[1]) in m):
continue
if ((u[0] > a or u[1] > b or
u[0] < 0 or u[1] < 0)):
continue
path.append([u[0], u[1]])

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

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


isSolvable = True

if (u[0] == target):
if (u[1] != 0):
path.append([u[0], 0])
else:
if (u[0] != 0):

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

sz = len(path)
for i in range(sz):
print("(", path[i][0], ",",
path[i][1], ")")
break

q.append([u[0], b])
q.append([a, u[1]])

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


c = u[0] + ap
d = u[1] - ap

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


q.append([c, d])

c = u[0] - ap
d = u[1] + ap

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


q.append([c, d])

q.append([a, 0])

q.append([0, b])
if (not isSolvable):
print("Solution not possible")

if __name__ == '__main__':

Jug1, Jug2, target = 4, 3, 2


print("Path from initial state "
"to solution state ::")

Solution(Jug1, Jug2, target)

You might also like