0% found this document useful (0 votes)
4 views6 pages

Practical 2

The document outlines a Python program for solving the Water Jug Problem using the Breadth First Search (BFS) algorithm. It includes specifications for user input for jug capacities and goal states, and emphasizes the use of a class structure without global variables. The program also evaluates BFS criteria such as completeness, optimality, execution time, and the number of explored nodes.
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)
4 views6 pages

Practical 2

The document outlines a Python program for solving the Water Jug Problem using the Breadth First Search (BFS) algorithm. It includes specifications for user input for jug capacities and goal states, and emphasizes the use of a class structure without global variables. The program also evaluates BFS criteria such as completeness, optimality, execution time, and the number of explored nodes.
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/ 6

Artificial Intelligence A.Y.

2024-2025 Practical-2

Practical-2
AIM: Write a python program to solve a Water Jug Problem by using Breadth first search (BFS).

Program should be written in generalized way to solve by using any capacity of jug.

 Take only two jugs: jug1, jug2

 Don’t use any global variable

 Create Class Node, bfs_algo, water_jug with appropriate method and variables.

 Capacity of jug1 and jug2 should have entered by user.

 Don’t use any python package throughout program except time.

 Find the minimum number of steps to reach the goal states entered by user.

 Check evaluating criteria Completeness, Optimality, execution time, No. of explored Nodes of BFS
algorithm. (Only “time” packages can be used in python if it is necessary to use)
Example:A Water Jug Problem: 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?

Let X represents the content of the water in 4-gallon jug.

Let Y represent the content of the water in 3-gallon jug.

Start from initial state: (X=0, Y=0)

Reach any of the Goal states: (X=2, Y=0), (X=2, Y=1), (X=2, Y=2), (X=2, Y=3)

Name: Krishna Chitlangia


Enrollment No: 22012011016
Batch: 6CE-D-2 Page | 1
Artificial Intelligence A.Y. 2024-2025 Practical-2

CODE:

import time

class node:

def __init__(self, data):

self.x = 0

self.y = 0

self.parent = data

def operation(self, cnode, rule):

x = cnode.x

y = cnode.y

if rule == 1:

if x < maxjug1:

x = maxjug1

else:

return None

elif rule == 2:

if y < maxjug2:

y = maxjug2

else:

return None

elif rule == 3:

if x > 0:

x=0

else:

return None

elif rule == 4:

if y > 0:
Name: Krishna Chitlangia
Enrollment No: 22012011016
Batch: 6CE-D-2 Page | 2
Artificial Intelligence A.Y. 2024-2025 Practical-2

y=0

else:

return None

elif rule == 5:

if x + y >= maxjug1:

y = y - (maxjug1 - x)

x = maxjug1

else:

return None

elif rule == 6:

if x + y >= maxjug2:

x = x - (maxjug2 - y)

y = maxjug2

else:

return None

elif rule == 7:

if x + y < maxjug1:

x=x+y

y=0

else:

return None

elif rule == 8:

if x + y < maxjug2:

y=x+y

x=0

else:

return None
Name: Krishna Chitlangia
Enrollment No: 22012011016
Batch: 6CE-D-2 Page | 3
Artificial Intelligence A.Y. 2024-2025 Practical-2

if x == cnode.x and y == cnode.y:

return None

nextnode = node(cnode)

nextnode.x = x

nextnode.y = y

nextnode.parent = cnode

return nextnode

class searchBFS:

def __init__(self, initialNode, goalNode):

self.queue = []

def printnode(self, i):

print("(", i.x, ",", i.y, ")")

def popnode(self):

return self.queue.pop(0)

def search(self):

self.queue.append(initialNode)

while len(self.queue) != 0:

cnode = self.popnode()

if cnode.x == goalNode.x and cnode.y == goalNode.y:

return cnode

l = self.generateAllSuccessors(cnode)

self.queue.extend(l)

return None

def generateAllSuccessors(self, cnode):

list1 = []

for rule in range(1, 9):


Name: Krishna Chitlangia
Enrollment No: 22012011016
Batch: 6CE-D-2 Page | 4
Artificial Intelligence A.Y. 2024-2025 Practical-2

nextnode = cnode.operation(cnode, rule)

if nextnode != None:

list1.append(nextnode)

return list1

def printPath(self, cnode):

temp = cnode

list1 = []

while temp != None:

list1.append(temp)

temp = temp.parent

list1.reverse()

for i in list1:

self.printnode(i)

print("Path cost:" + str(len(list1)))

maxjug1 = int(input("Enter value of maxjug1: "))

maxjug2 = int(input("Enter value of maxjug2: "))

initialNode = node(None)

initialNode.x = 0

initialNode.y = 0

initialNode.parent = None

goalNode = node(None)

goalNode.x = int(input("Enter value of goal in jug1: "))

goalNode.y = 0

goalNode.parent = None

print("\nBFS algorithm is running...")

start_time = time.time()

solution = searchBFS(initialNode, goalNode)


Name: Krishna Chitlangia
Enrollment No: 22012011016
Batch: 6CE-D-2 Page | 5
Artificial Intelligence A.Y. 2024-2025 Practical-2

solutionNode = solution.search()

end_time = time.time()

if solutionNode != None:

print("Solution can be found")

solution.printPath(solutionNode)

else:

print("Solution can't be found")

diff = end_time - start_time

print("Executed in time:", diff * 1000, "ns")

OUTPUT:

Name: Krishna Chitlangia


Enrollment No: 22012011016
Batch: 6CE-D-2 Page | 6

You might also like