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

Ai 2 1

Uploaded by

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

Ai 2 1

Uploaded by

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

DEPARTMENT OF BIOMEDICAL ENGINEERING

Academic Year: 2023-24

Lab Session:
2

Semester B.E. Semester VIII – Biomedical Engineering

Subject Artificial Intelligence

Subject Professor Prof. Priyanka Shrivastava


In-charge

Experiment 2
Number
Experiment Water jug problem
Title We are provided with two jugs: one having the capacity to hold 3 gallons of water and
the other has the capacity to hold 4 gallons of water. There is no other measuring
equipment available and the jugs also do not have any kind of marking on them. So,
the agent’s task here is to fill the 4-gallon jug with 2 gallons of water by using only these
two jugs and no other material.
Note : Assume, initially, both our jugs are empty.
Concept

Student Name Abhijit Gawai


Roll Number 20105A0021

Grade and Subject


Teacher’s Signature
Solution

Program def water_jug_dfs(jug1_capacity, jug2_capacity, jug1_goal, jug2_goal, path,


visited_states): current_state = (path[-1][0], path[-1][1]) if current_state in
visited_states: return None # Already visited this state
visited_states.add(current_state) if path[-1][0] == jug1_goal
and path[-1][1] == jug2_goal: return path
# Goal state reached
# Try all possible operations operations
=[
("Fill Jug 1", jug1_capacity, path[-1][1]),
("Fill Jug 2", path[-1][0], jug2_capacity),
("Empty Jug 1", 0, path[-1][1]),
("Empty Jug 2", path[-1][0], 0),
("Pour Jug 1 to Jug 2", max(0, path[-1][0] - (jug2_capacity - path[-1][1])),
min(jug2_capacity, path[-1][1] + path[-1][0])),
("Pour Jug 2 to Jug 1", min(jug1_capacity, path[-1][0] + path[-1][1]), max(0,
path[1][1] - (jug1_capacity - path[-1][0])))

for operation in operations:


new_path = list(path)
new_path.append((operation[1], operation[2]))
result = water_jug_dfs(jug1_capacity, jug2_capacity, jug1_goal,
jug2_goal, new_path, visited_states) if result: return result #
Goal state found return None # Goal state not reachable

def water_jug_problem(jug1_capacity, jug2_capacity, jug1_goal, jug2_goal):


initial_state = (0, 0) initial_path = [initial_state] visited_states = set()

result_path = water_jug_dfs(jug1_capacity, jug2_capacity, jug1_goal, jug2_goal,


initial_path, visited_states)
if
result_path:
print("Solution:") for step, state in enumerate(result_path):
print(f"Step {step + 1}: Jug 1 - {state[0]} gallons, Jug 2 - {state[1]} gallons")
else: print("No solution found.")

# Example: Fill 4-gallon jug with 2 gallons using 3-gallon and 4-gallon jugs
water_jug_problem(3, 4, 2, 0)
Result
Conclusion The above mentioned problem can be solved by using aforementioned set of rules.

You might also like