๐Ÿค–
B.TECH III YEAR โ€ข I SEMESTER โ€ข R22

Artificial Intelligence Using Python - Lab Manual

Artificial Intelligence Laboratory โ€ข Program Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ III Year ๐Ÿ“š I Semester ๐Ÿค– Artificial Intelligence ๐Ÿงช Lab Program
Selected Program

Write a Program to Implement Water-Jug problem using Python.

โ† All Artificial Intelligence Programs

SOURCE CODE :

 # jug1 and jug2 contain the value 
jug1, jug2, goal = 4, 3, 2 

# Initialize a 2D list for visited states
# The list will have dimensions (jug1+1) x (jug2+1) to cover all possible states
visited = [[False for _ in range(jug2 + 1)] for _ in range(jug1 + 1)] 

def waterJug(vol1, vol2):
	# Check if we reached the goal state
	if (vol1 == goal and vol2 == 0) or (vol2 == goal and vol1 == 0): 
		print(vol1,"\t", vol2)
		print("Solution Found")
		return True

    # If this state has been visited, return False
	if visited[vol1][vol2]:
		return False
	# Mark this state as visited
	visited[vol1][vol2] = True
	# Print the current state
	print(vol1,"\t", vol2)
	# Try all possible moves:
	return (
        waterJug(0, vol2) or  # Empty jug1
        waterJug(vol1, 0) or  # Empty jug2
        waterJug(jug1, vol2) or  # Fill jug1
        waterJug(vol1, jug2) or  # Fill jug2
        waterJug(vol1 + min(vol2, (jug1 - vol1)), vol2 - min(vol2, (jug1 - vol1))) or  # Pour water from jug2 to jug1
        waterJug(vol1 - min(vol1, (jug2 - vol2)), vol2 + min(vol1, (jug2 - vol2)))  # Pour water from jug1 to jug2
    ) 

print("Steps: ") 
print("Jug1 \t Jug2 ")
print("----- \t ------")
waterJug(0, 0)

OUTPUT :


Steps:
Jug1	Jug2
-----	------
0	0
4	0
4	3
0	3
3	0
3	3
4	2
0	2
Solution Found

๐Ÿ’ป Run This Python Program

Write a Program to Implement Water-Jug problem using Python.
The selected Python program is loaded automatically into the editor. Edit the program, provide input if required and click Run.

The compiler is embedded using OneCompiler. Your Python program runs inside the embedded Python environment; Study Glance does not execute Python code directly on your web server. If the program asks for input (for example the starting node), type the value in the STDIN box before clicking Run.
โ† Back to Artificial Intelligence Lab Programs ๐Ÿ“š All Lab Manuals