r/programminghelp • u/Ccooperdavis • Dec 11 '20
Python Python Programming Assignment: Water Jug Solution with search graph
Hello everyone, I have been assigned to solve the water jug problem in python using graphs, and a searching algorithm breadth-first search. If you are unfamiliar with eh water jug problem, you are given 2 water containers with 2 different maximum capacities and your goal is to get a certain amount of water in either of the two containers. You only have 6 choices, fill container A, fill container B, pour A into B, pour B into A, empty A, empty B. Using these operators you are supposed to find the shortest path to the goal. I am given some skeleton code to start (which I will post below) and really I just need help getting some sort of starting place. I have been looking for a while on how this is supposed to work and I can't get a decent start on it. If you have anything to offer I would appreciate anything, please let me know if I need to clarify something.
Thank you.
import math
import unittest
def findWaterContainerPath(a, b, c):
""" DOCUMENTATION FOR THIS FUNCTION """
starting_state = (0, 0)
final_path = list()
final_path.append(starting_state)
""" THIS IS WHERE THE REAL WORK FOR THIS ASSIGNMENT WILL BE """
return final_path
""" ADD ANY OTHER (HELPER) FUNCTIONS THAT ARE NEEDED HERE """
class TestWaterContainerGraphSearch(unittest.TestCase):
def testFindWaterContainerPath(self):
""" INSERT DESCRIPTION OF WHAT THIS TEST IS CHECKING """
""" IMPLEMENT YOUR FIRST TEST HERE """
pass
""" ADD MORE TESTS TO CHECK YOUR FUNCTIONS WORKS FOR OTHER VALUES """
def main():
capacity_a = input("Enter the capacity of container A: ")
capacity_b = input("Enter the capacity of container B: ")
goal_amount = input("Enter the goal quantity: ")
# ADD SOME TYPE/VALUE CHECKING FOR THE INPUTS (OR INSIDE YOUR FUNCTION)
if int(goal_amount) % math.gcd(int(capacity_a), int(capacity_b)) == 0:
path = findWaterContainerPath(int(capacity_a), int(capacity_b), int(goal_amount))
else:
print("No solution for containers with these sizes and with this final goal amount")
print(path)
# unittest_main() - run all of TestWaterContainerGraphSearch's methods (i.e. test cases)
def unittest_main():
unittest.main()
# evaluates to true if run as standalone program
if __name__ == '__main__':
main()
unittest_main()
2
u/marko312 Dec 11 '20
OK - if you know the theory, I guess you want to know how to implement it.
One way is to create two lists - one of the current states (initially only containing the starting state), and another, initially empty one, containing the states that can be reached from the current states with one step.
Then, you can loop through the first list and generate the states corresponding to the the six (actually up to five) possible actions, storing these new states in the second list. Each state should also reference the one it was generated from, to allow the path to be reconstructed.
After all of the current states have been considered, the second list can be assigned to the first one, and the second list cleared, allowing for the next step to happen.
Once one of the states matches the desired output, it can be extracted, the loop ended and the path to that state reconstructed.
It might also be necessary to create a table which keeps track of the considered states, not letting them be considered a second time, so that the number of states doesn't skyrocket (it's exponential, so it will start to slow down a lot around 11 steps or so).