Assignment 0

You have two jars, where you can put water, with capacities cap_1 and cap_2 respectively. Your goal is to fill the jars with a given amount of water. The actions that you can perform are the following:

  1. Fill 1: fill jar 1 to its maximum capacity with water.
  2. Fill 1: fill jar 2 to its maximum capacity with water.
  3. Empty 1: totally empty jar 1.
  4. Empty 2: totally empty jar 2.
  5. 1 -> 2: pour the content of jar 1 into jar 2 until jar 1 is empty or jar 2 is full.
  6. 2 -> 1: pour the content of jar 2 into jar 1 until jar 2 is empty or jar 1 is full.

1. Model the jars problem as a search problem


In [ ]:
class jars_problem(search.SearchProblem):
    def __init__(self, cap_1, cap_2, goal):
        '''
        cap_1: jar 1 capacity
        cap_2: jar 2 capacity
        goal:  goal state
        '''
        self.cap_1 = cap_1
        self.cap_2 = cap_2
        self.start = (0, 0)
        self.goal = goal
        
    def getStartState(self):
        return self.start

    def isGoalState(self, state):
        return self.goal == state

    def getSuccessors(self, state):
        '''
        Receives a state and calculates the list of successors. Each successor 
        correspond a triple of the form (state, action, cost). 
        For instance for state = (0, 0) the list of successors could be:
        [((5, 0), "Fill 1", 5), ((0, 4), "Fill 2", 4)]
        '''
        # Your code here
        return successors

2. Given two jars of capacity 5 and 4 liters respectively measure 2 liters in jar 2.


In [ ]:
problem = jars_problem(5, 4, (0,2))
actions = dfs(problem)
print actions