예제 #1
0
def maxLevel(state, problem):
    """
    The heuristic value is the number of layers required to expand all goal propositions.
    If the goal is not reachable from the state your heuristic should return float('inf')
    A good place to start would be:
    propLayerInit = PropositionLayer()          #create a new proposition layer
    for prop in state:
      propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
    pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
    pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
    """

    level = 0
    graph = []

    initial_prop_layer = PropositionLayer()
    for prop in state:
        initial_prop_layer.addProposition(prop)

    curr_graph_level = PlanGraphLevel()
    curr_graph_level.setPropositionLayer(initial_prop_layer)
    graph.append(curr_graph_level)

    while not problem.isGoalState(
            graph[level].getPropositionLayer().getPropositions()):
        if isFixed(graph, level):
            return float('inf')
        level += 1
        next_level = PlanGraphLevel()
        next_level.expandWithoutMutex(graph[level - 1])
        graph.append(next_level)

    return level
예제 #2
0
def levelSum(state, problem):
    """
    The heuristic value is the sum of sub-goals level they first appeared.
    If the goal is not reachable from the state your heuristic should return float('inf')
    """

    level = 0
    sum = 0
    graph = []
    goals = [goal for goal in problem.goal]

    initial_prop_layer = PropositionLayer()
    for prop in state:
        initial_prop_layer.addProposition(prop)

    initial_level = PlanGraphLevel()
    initial_level.setPropositionLayer(initial_prop_layer)
    graph.append(initial_level)

    while len(goals) > 0:
        if isFixed(graph, level):
            return float('inf')

        for goal in goals:
            if goal in graph[level].getPropositionLayer().getPropositions():
                sum += level
                goals.remove(goal)

        level += 1
        next_level = PlanGraphLevel()
        next_level.expandWithoutMutex(graph[level - 1])
        graph.append(next_level)

    return sum
예제 #3
0
def maxLevel(state, problem):
    """
  The heuristic value is the number of layers required to expand all goal propositions.
  If the goal is not reachable from the state your heuristic should return float('inf')  
  A good place to start would be:
  propLayerInit = PropositionLayer()          #create a new proposition layer
  for prop in state:
    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
  """
    "*** YOUR CODE HERE ***"

    propLayerInit = PropositionLayer()
    for prop in state:
        propLayerInit.addProposition(prop)
    pgInit = PlanGraphLevel()
    pgInit.setPropositionLayer(propLayerInit)

    level = 0
    graph = []
    graph.append(pgInit)

    while not problem.isGoalState(
            graph[level].getPropositionLayer().getPropositions()):
        level += 1
        pgNext = PlanGraphLevel()
        pgNext.expandWithoutMutex(graph[level - 1])
        graph.append(pgNext)

    return level
예제 #4
0
def levelSum(state, problem):
    """
  The heuristic value is the sum of sub-goals level they first appeared.
  If the goal is not reachable from the state your heuristic should return float('inf')
  """
    "*** YOUR CODE HERE ***"
    propLayerInit = PropositionLayer()
    for prop in state:
        propLayerInit.addProposition(prop)
        pgInit = PlanGraphLevel()
        pgInit.setPropositionLayer(propLayerInit)

    level = 0
    sum = 0
    graph = []
    goals = [goal for goal in problem.goal]
    graph.append(pgInit)

    while len(goals) > 0:
        for goal in goals:
            if goal in graph[level].getPropositionLayer().getPropositions():
                sum += level
                goals.remove(goal)

        level += 1
        pgNext = PlanGraphLevel()
        pgNext.expandWithoutMutex(graph[level - 1])
        graph.append(pgNext)

    return sum
예제 #5
0
def maxLevel(state, problem):
  """
  The heuristic value is the number of layers required to expand all goal propositions.
  If the goal is not reachable from the state your heuristic should return float('inf')  
  """
  level = 0
  propLayerInit = PropositionLayer()
  # Add all propositions in current state to proposition layer
  for p in state:
    propLayerInit.addProposition(p)

  pgInit = PlanGraphLevel()
  pgInit.setPropositionLayer(propLayerInit)
  # Graph is a list of PlanGraphLevel objects
  graph = []
  graph.append(pgInit)

  # While goal state is not in proposition layer, keep expanding
  while problem.goalStateNotInPropLayer(graph[level].getPropositionLayer().getPropositions()):
    # If the graph has not changed between expansions, we should halt
    if isFixed(graph, level):
      return float('inf')
    level += 1
    pgNext = PlanGraphLevel()
    # Expand without mutex (relaxed version of problem)
    pgNext.expandWithoutMutex(graph[level-1])
    graph.append(pgNext)

  return level
예제 #6
0
def maxLevel(state, problem):
  """
  The heuristic value is the number of layers required to expand all goal propositions.
  If the goal is not reachable from the state your heuristic should return float('inf')  
  A good place to start would be:
  propLayerInit = PropositionLayer()          #create a new proposition layer
  for prop in state:
    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
  """
  propLayerInit = PropositionLayer()
  for prop in state:
    propLayerInit.addProposition(prop)
  pgInit = PlanGraphLevel()
  pgInit.setPropositionLayer(propLayerInit)

  graph = []  # list of PlanGraphLevel objects
  graph.append(pgInit)
  level = 0
  while True:
    # check if this level has the goal
    if problem.goalStateNotInPropLayer(graph[level].getPropositionLayer().getPropositions()):
      break
    # else if the goal is not in this level, and we finished to max graph, meens we vant reach the goal.
    elif isFixed(graph, level):
      return float('inf')

    pgNext = PlanGraphLevel()
    pgNext.expandWithoutMutex(graph[level])
    graph.append(pgNext)
    level += 1
  # if we got into break meens last level contain goal. So lets return the level.
  return level
예제 #7
0
def levelSum(state, problem):
  """
  The heuristic value is the sum of sub-goals level they first appeared.
  If the goal is not reachable from the state your heuristic should return float('inf')
  """
  propLayerInit = PropositionLayer()          #create a new proposition layer
  for prop in state:
    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer

  level = 0
  sumLevel = 0
  currentGoals = set(copy.copy(problem.goal))

  while currentGoals: #TODO: Changed: run until all goals found/no solution possible 
    #check for new goals achieved
    goalsInHand = set(pgInit.getPropositionLayer().getPropositions()) & currentGoals

    if goalsInHand:
      sumLevel +=  len(goalsInHand) * level;
      currentGoals -= goalsInHand;

    level += 1

    ## Expand to the next leyer
    prevLayerSize = len(pgInit.getPropositionLayer().getPropositions())
    pgInit.expandWithoutMutex(pgInit)
    ## Check if the expanded leyer is the same leyer as before
    if len(pgInit.getPropositionLayer().getPropositions()) == prevLayerSize:
      return float("inf")
    
  return sumLevel
예제 #8
0
def maxLevel(state, problem):
    """
  The heuristic value is the number of layers required to expand all goal propositions.
  If the goal is not reachable from the state your heuristic should return float('inf')  
  """
    level = 0
    propLayerInit = PropositionLayer()
    # Add all propositions in current state to proposition layer
    for p in state:
        propLayerInit.addProposition(p)

    pgInit = PlanGraphLevel()
    pgInit.setPropositionLayer(propLayerInit)
    # Graph is a list of PlanGraphLevel objects
    graph = []
    graph.append(pgInit)

    # While goal state is not in proposition layer, keep expanding
    while problem.goalStateNotInPropLayer(
            graph[level].getPropositionLayer().getPropositions()):
        # If the graph has not changed between expansions, we should halt
        if isFixed(graph, level):
            return float('inf')
        level += 1
        pgNext = PlanGraphLevel()
        # Expand without mutex (relaxed version of problem)
        pgNext.expandWithoutMutex(graph[level - 1])
        graph.append(pgNext)

    return level
예제 #9
0
def maxLevel(state, problem):
    """
    The heuristic value is the number of layers required to expand all goal propositions.
    If the goal is not reachable from the state your heuristic should return float('inf')
    A good place to start would be:
    propLayerInit = PropositionLayer()          #create a new proposition layer
    for prop in state:
      propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
    pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
    pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
    """

    level = 0
    graph = []

    initial_prop_layer = PropositionLayer()
    for prop in state:
        initial_prop_layer.addProposition(prop)

    curr_graph_level = PlanGraphLevel()
    curr_graph_level.setPropositionLayer(initial_prop_layer)
    graph.append(curr_graph_level)

    while not problem.isGoalState(graph[level].getPropositionLayer().getPropositions()):
        if isFixed(graph, level):
            return float('inf')
        level += 1
        next_level = PlanGraphLevel()
        next_level.expandWithoutMutex(graph[level-1])
        graph.append(next_level)

    return level
예제 #10
0
def maxLevel(state, problem):
    """
    The heuristic value is the number of layers required to expand all goal propositions.
    If the goal is not reachable from the state your heuristic should return float('inf')
    A good place to start would be:
    propLayerInit = PropositionLayer()          #create a new proposition layer
    for prop in state:
      propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
    pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
    pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
    """

    """ this is copy paste from graph plan algorithm, with small change in loop definition and disabling of mutex """

    propLayerInit = PropositionLayer()  # create a new proposition layer
    for prop in state:
        propLayerInit.addProposition(prop)  # update the proposition layer with the propositions of the state
    pgInit = PlanGraphLevel()  # create a new plan graph level (level is the action layer and the propositions layer)
    pgInit.setPropositionLayer(propLayerInit)

    graph = []
    level = 0
    graph.append(pgInit)

    while problem.goalStateNotInPropLayer(graph[level].getPropositionLayer().getPropositions()):
        if isFixed(graph, level):
            return float("inf")  # this means we stopped the while loop above because we reached a fixed point in the graph. nothing more to do, we failed!
        level = level + 1
        pgNext = PlanGraphLevel()  # create new PlanGraph object
        pgNext.expandWithoutMutex(graph[level - 1])  # calls the expand function, which you are implementing in the PlanGraph class
        graph.append(pgNext)  # appending the new level to the plan graph

    return level
예제 #11
0
def levelSum(state, problem):
    """
    The heuristic value is the sum of sub-goals level they first appeared.
    If the goal is not reachable from the state your heuristic should return float('inf')
    """
    """ this is copy paste from graph plan algorithm, with small change in loop definition and disabling of mutex """

    propLayerInit = PropositionLayer()  # create a new proposition layer
    for prop in state:
        propLayerInit.addProposition(prop)  # update the proposition layer with the propositions of the state
    pgInit = PlanGraphLevel()  # create a new plan graph level (level is the action layer and the propositions layer)
    pgInit.setPropositionLayer(propLayerInit)

    graph = []
    sum_sub_goals = 0
    level = 0
    graph.append(pgInit)

    while problem.goalStateNotInPropLayer(graph[level].getPropositionLayer().getPropositions()):
        if isFixed(graph, level):
            return float("inf")  # this means we stopped the while loop above because we reached a fixed point in the graph. nothing more to do, we failed!

        if problem.isSubGoal(graph[level].getPropositionLayer().getPropositions()):  # if we have sub goal here. count it
            sum_sub_goals += 1
        level += 1

        pgNext = PlanGraphLevel()  # create new PlanGraph object
        pgNext.expandWithoutMutex(graph[level - 1])  # calls the expand function, which you are implementing in the PlanGraph class
        graph.append(pgNext)  # appending the new level to the plan graph

    sum_sub_goals += 1  # the latest full sub goal that is equals goal, we take it to attention too
    return sum_sub_goals
예제 #12
0
def levelSum(state, problem):
    """
    The heuristic value is the sum of sub-goals level they first appeared.
    If the goal is not reachable from the state your heuristic should return float('inf')
    """

    level = 0
    sum = 0
    graph = []
    goals = [goal for goal in problem.goal]

    initial_prop_layer = PropositionLayer()
    for prop in state:
        initial_prop_layer.addProposition(prop)

    initial_level = PlanGraphLevel()
    initial_level.setPropositionLayer(initial_prop_layer)
    graph.append(initial_level)

    while len(goals) > 0:
        if isFixed(graph, level):
            return float('inf')

        for goal in goals:
            if goal in graph[level].getPropositionLayer().getPropositions():
                sum += level
                goals.remove(goal)

        level += 1
        next_level = PlanGraphLevel()
        next_level.expandWithoutMutex(graph[level-1])
        graph.append(next_level)

    return sum
def levelSum(state, problem):
  """
  The heuristic value is the sum of sub-goals level they first appeared.
  If the goal is not reachable from the state your heuristic should return float('inf')
  """
  "*** YOUR CODE HERE ***"
  propLayerInit = PropositionLayer()          #create a new proposition layer
  # initialize the propositions
  for prop in state:
    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
  currentLevel = pgInit
  level = 0
  sum = 0
  graph = list()
  graph.append(currentLevel)
  goals = copy.deepcopy(problem.goal)
  while(len(goals) > 0):
    if isFixed(graph, level):
      return float("inf")
    layer = currentLevel.getPropositionLayer().getPropositions()
    for prop in layer:
      if prop in goals:
        sum = sum + level
        goals.remove(prop)
    nextLevel = PlanGraphLevel()
    nextLevel.expandWithoutMutex(graph[level])
    level = level + 1
    graph.append(nextLevel)
    currentLevel = nextLevel

  return sum
def maxLevel(state, problem):
  """
  The heuristic value is the number of layers required to expand all goal propositions.
  If the goal is not reachable from the state your heuristic should return float('inf')  
  A good place to start would be:
  propLayerInit = PropositionLayer()          #create a new proposition layer
  for prop in state:
    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
  """
  "*** YOUR CODE HERE ***"
  propLayerInit = PropositionLayer()          #create a new proposition layer
  for prop in state:
    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
  currentLevel = pgInit
  level = 0
  graph = list()
  graph.append(currentLevel)
  while(problem.goalStateNotInPropLayer(graph[level].getPropositionLayer().getPropositions())):
    newLevel = PlanGraphLevel()
    newLevel.expandWithoutMutex(graph[level])
    level += 1
    graph.append(newLevel)
    currentLevel = newLevel
    if isFixed(graph, level):
      return float("inf")

  return level
예제 #15
0
def maxLevel(state, problem):
  """ El valor de la heurística es el número de capas
  necesarias para expandir todas las proposiciones de gol.
  Si el objetivo no es alcanzable desde el estado de su
  heurística debe volver float('inf') """
  level = 0
  propLayerInit = PropositionLayer()
  # Añadir todas las proposiciones en el estado actual en la propositionLayer
  for p in state:
    propLayerInit.addProposition(p)

  pgInit = PlanGraphLevel()
  pgInit.setPropositionLayer(propLayerInit)
  # El Grafo es una lista de objetos PlanGraphLevel
  graph = []
  graph.append(pgInit)

  # Mientras que el estado objetivo no está en la capa proposición, seguimos expandiendolo
  while problem.goalStateNotInPropLayer(graph[level].getPropositionLayer().getPropositions()):
    # Si el grafo no ha cambiado entre expansiones, lo detenemos.
    if isFixed(graph, level):
      return float('inf')
    level += 1
    pgNext = PlanGraphLevel()
    # Expandir sin mutex (versión relajada de problema)
    pgNext.expandWithoutMutex(graph[level-1])
    graph.append(pgNext)

  return level  
예제 #16
0
def maxLevel(state, problem):
  """
  The heuristic value is the number of layers required to expand all goal propositions.
  If the goal is not reachable from the state your heuristic should return float('inf')  
  A good place to start would be:
  propLayerInit = PropositionLayer()          #create a new proposition layer
  for prop in state:
    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
  """
  "*** YOUR CODE HERE ***"
  propLayerInit = PropositionLayer()          #create a new proposition layer
  for prop in state:
    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer

  level = 0;

  while not problem.isGoalState(pgInit.getPropositionLayer().getPropositions()):
    level += 1
    ## Expand to the next leyer
    prevLayerSize = len(pgInit.getPropositionLayer().getPropositions())
    pgInit.expandWithoutMutex(pgInit)
    ## Check if the expanded leyer is the same leyer as before
    if len(pgInit.getPropositionLayer().getPropositions()) == prevLayerSize:
      return float("inf")
    
  return level
예제 #17
0
def levelSum(state, problem):
  """
  The heuristic value is the sum of sub-goals level they first appeared.
  If the goal is not reachable from the state your heuristic should return float('inf')
  """
  total = 0
  propLayerInit = PropositionLayer()

  for prop in state:
    propLayerInit.addProposition(prop)

  pgInit = PlanGraphLevel()
  pgInit.setPropositionLayer(propLayerInit)
  g = [pgInit]
  level = 0

  while len(problem.goal) > 0:
    if isFixed(g, level):
      return float("inf")

    for goal in problem.goal:
      if goal in g[level].getPropositionLayer().getPropositions():
        problem.goal.remove(goal)
        total += level

    nextPlanGraphLevel = PlanGraphLevel()
    nextPlanGraphLevel.expandWithoutMutex(g[level])
    level += 1
    g.append(nextPlanGraphLevel)
  return total
예제 #18
0
    def getStartState(self):
        "*** YOUR CODE HERE ***"

        # explain: A state is a planGraphLevel,
        #   and here I am building a level with the initial state propositions

        propLayerInit = PropositionLayer()
        for prop in self.initialState:
            propLayerInit.addProposition(prop)
        pgInit = PlanGraphLevel()
        pgInit.setPropositionLayer(propLayerInit)

        return pgInit
예제 #19
0
    def graphPlan(self):
        """
        The graphplan algorithm.
        The code calls the extract function which you should complete below
        """
        #initialization
        initState = self.initialState
        level = 0
        self.noGoods = [] #make sure you update noGoods in your backward search!
        self.noGoods.append([])
        #create first layer of the graph, note it only has a proposition layer which consists of the initial state.
        propLayerInit = PropositionLayer()
        for prop in initState:
            propLayerInit.addProposition(prop)            
        pgInit = PlanGraphLevel()
        pgInit.setPropositionLayer(propLayerInit)
        self.graph.append(pgInit)
        

        """
        While the layer does not contain all of the propositions in the goal state,
        or some of these propositions are mutex in the layer we,
        and we have not reached the fixed point, continue expanding the graph
        """

        while self.goalStateNotInPropLayer(self.graph[level].getPropositionLayer().getPropositions()) or \
            self.goalStateHasMutex(self.graph[level].getPropositionLayer()):
            if self.isFixed(level):
                return None #this means we stopped the while loop above because we reached a fixed point in the graph. nothing more to do, we failed!

            self.noGoods.append([])
            level = level + 1
            pgNext = PlanGraphLevel() #create new PlanGraph object
            pgNext.expand(self.graph[level - 1]) #calls the expand function, which you are implementing in the PlanGraph class
            self.graph.append(pgNext) #appending the new level to the plan graph
            
            sizeNoGood = len(self.noGoods[level]) #remember size of nogood table

        plan = self.extract(self.graph, self.goal, level) #try to extract a plan since all of the goal propositions are in current graph level, and are not mutex
        while(plan is None): #while we didn't extract a plan successfully
            level = level + 1
            self.noGoods.append([])
            pgNext = PlanGraphLevel() #create next level of the graph by expanding
            pgNext.expand(self.graph[level - 1]) #create next level of the graph by expanding
            self.graph.append(pgNext)
            plan = self.extract(self.graph, self.goal, level) #try to extract a plan again
            if (plan is None and self.isFixed(level)): #if failed and reached fixed point
                if sizeNoGood == len(self.noGoods[level]): #if size of nogood didn't change, means there's nothing more to do. We failed.
                    return None
                sizeNoGood = len(self.noGoods[level]) #we didn't fail yet! update size of no good
        return plan
예제 #20
0
  def graphPlan(self):
    """
    The graphplan algorithm.
    The code calls the extract function which you should complete below
    """
    #initialization
    initState = self.initialState
    level = 0
    self.noGoods = [] #make sure you update noGoods in your backward search!
    self.noGoods.append([])
    #create first layer of the graph, note it only has a proposition layer which consists of the initial state.
    propLayerInit = PropositionLayer()
    for prop in initState:
      propLayerInit.addProposition(prop)
    pgInit = PlanGraphLevel()
    pgInit.setPropositionLayer(propLayerInit)
    self.graph.append(pgInit)

    """
    While the layer does not contain all of the propositions in the goal state,
    or some of these propositions are mutex in the layer we,
    and we have not reached the fixed point, continue expanding the graph
    """

    while self.goalStateNotInPropLayer(self.graph[level].getPropositionLayer().getPropositions()) or \
        self.goalStateHasMutex(self.graph[level].getPropositionLayer()):
      if self.isFixed(level):
        return None #this means we stopped the while loop above because we reached a fixed point in the graph. nothing more to do, we failed!

      self.noGoods.append([])
      level = level + 1
      pgNext = PlanGraphLevel() #create new PlanGraph object
      pgNext.expand(self.graph[level - 1]) #calls the expand function, which you are implementing in the PlanGraph class
      self.graph.append(pgNext) #appending the new level to the plan graph

      sizeNoGood = len(self.noGoods[level]) #remember size of nogood table

    plan = self.extract(self.graph, self.goal, level) #try to extract a plan since all of the goal propositions are in current graph level, and are not mutex
    while(plan is None): #while we didn't extract a plan successfully
      level = level + 1
      self.noGoods.append([])
      pgNext = PlanGraphLevel() #create next level of the graph by expanding
      pgNext.expand(self.graph[level - 1]) #create next level of the graph by expanding
      self.graph.append(pgNext)
      plan = self.extract(self.graph, self.goal, level) #try to extract a plan again
      if (plan is None and self.isFixed(level)): #if failed and reached fixed point
        if sizeNoGood == len(self.noGoods[level]): #if size of nogood didn't change, means there's nothing more to do. We failed.
          return None
        sizeNoGood = len(self.noGoods[level]) #we didn't fail yet! update size of no good
    return plan
예제 #21
0
	def graphPlan(self): 
		#El algoritmo graphplan en sí
		
		#Inicialización
		initState = self.initialState
		level = 0
		self.noGoods = []
		self.noGoods.append([])
		
		#Crea la primera capa del grafo, que no consiste más que en el estado inicial
		propLayerInit = PropositionLayer()
		for prop in initState:
			propLayerInit.addProposition(prop)
		pgInit = PlanGraphLevel()
		pgInit.setPropositionLayer(propLayerInit)
		self.graph.append(pgInit)
		
		#Mientras que la capa no contiene todos los estados del estado final buscado (o están mutex) continuamos expandiendo el grafo
		while self.goalStateNotInPropLayer(self.graph[level].getPropositionLayer().getPropositions()) or \
				self.goalStateHasMutex(self.graph[level].getPropositionLayer()):
			if self.isFixed(level):
				return None	#Si llegamos aquí paramos porque significa que hemos llegado a un fixed point en el grafo, así que no podemos hacer nada más
				
			self.noGoods.append([])
			level = level + 1 #Actualizamos el nivel
			pgNext = PlanGraphLevel() #Crea un nuevo objeto GraphPlan
			pgNext.expand(self.graph[level - 1]) #Llama a la función expandir
			self.graph.append(pgNext) #Une el nuevo nivel generado con el graphplan
		
			sizeNoGood = len(self.noGoods[level])
		
		plan = self.extract(self.graph, self.goal, level) #Intentamos hallar un plan (si todos los estados objetivos están en este nivel y no están mutex)
		while(plan is None): #Hacemos esto mientras no podemos encontrar un plan
			level = level + 1 
			self.noGoods.append([])
			pgNext = PlanGraphLevel() #Crea el próximo nivel del grafo
			pgNext.expand(self.graph[level - 1]) #Y ahora lo expande
			self.graph.append(pgNext)
			plan = self.extract(self.graph, self.goal, level) #Intentamos econtrar el plan
			if (plan is None and self.isFixed(level)): #Si fallamos y encontramos un punto un fixed point
				if sizeNoGood == len(self.noGoods[level]): #Si el tamaño de noGood no cambia significa que hemos fallado y no hay plan
					return None
				sizeNoGood = len(self.noGoods[level]) #Si no, significa que aún podemos encontrar el plan y actualizamos el tamaño de noGood
		return plan
예제 #22
0
파일: planningProblem.py 프로젝트: odedm/ai
def expansionGenerator(state, problem):
    """
    Generates and yields the propositions in each level,
    Until the graph becomes fixed.
    """
    propLayerInit = PropositionLayer()          #create a new proposition layer
    for prop in state:
        propLayerInit.addProposition(prop)      #update the proposition layer with the propositions of the state
    pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
    pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
    graph = [pgInit]
    count = 0
    
    while not isFixed(graph, count):
        props = graph[count].getPropositionLayer().getPropositions()
        yield count, props
        pgNext = PlanGraphLevel()
        pgNext.expandWithoutMutex(graph[count])
        graph.append(pgNext)
        count += 1
예제 #23
0
def levelSum(state, problem):
    """
  The heuristic value is the sum of sub-goals level they first appeared.
  If the goal is not reachable from the state your heuristic should return float('inf')
  """
    propLayerInit = PropositionLayer()
    for p in state:
        propLayerInit.addProposition(p)

    pgInit = PlanGraphLevel()
    pgInit.setPropositionLayer(propLayerInit)

    graph = []  # list of PlanGraphLevel objects
    graph.append(pgInit)
    goals = problem.goal[:]
    level = 0
    sum_ = 0

    # keep expanding as long as we still have goal states we didn't see
    while goals:
        if isFixed(graph, level):
            # if the graph is fixed and expansions didn't change in the last level, it means that we can't reach
            # the goal state, and we return infinity
            return float('inf')

        props = graph[level].getPropositionLayer().getPropositions()
        for goal in goals:
            if goal in props:
                # each goal state that we run into, we should add to the sum, and remove it from the goals we need to see
                sum_ += level
                goals.remove(goal)

        pg = PlanGraphLevel()
        # expanding using a easier version of the problem - without mutexes
        pg.expandWithoutMutex(graph[level])
        graph.append(pg)
        level += 1

    sum_ += level

    return sum_
예제 #24
0
def maxLevel(state, problem):
    """
  The heuristic value is the number of layers required to expand all goal propositions.
  If the goal is not reachable from the state your heuristic should return float('inf')  
  A good place to start would be:
  propLayerInit = PropositionLayer()          #create a new proposition layer
  for prop in state:
    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
  """

    propLayerInit = PropositionLayer()
    for p in state:
        propLayerInit.addProposition(p)

    pgInit = PlanGraphLevel()
    pgInit.setPropositionLayer(propLayerInit)

    graph = []  # list of PlanGraphLevel objects
    graph.append(pgInit)
    level = 0

    # keep expanding as long as we don't hit the goal state
    while problem.goalStateNotInPropLayer(
            graph[level].getPropositionLayer().getPropositions()):
        if isFixed(graph, level):
            # if the graph is fixed and expansions didn't change in the last level, it means that we can't reach
            # the goal state, and we return infinity
            return float('inf')

        pg = PlanGraphLevel()
        # expanding using a easier version of the problem - without mutexes
        pg.expandWithoutMutex(graph[level])
        graph.append(pg)
        level += 1

    return level
예제 #25
0
def levelSum(state, problem):
  """
  The heuristic value is the sum of sub-goals level they first appeared.
  If the goal is not reachable from the state your heuristic should return float('inf')
  """
  propLayerInit = PropositionLayer()
  for prop in state:
    propLayerInit.addProposition(prop)
  pgInit = PlanGraphLevel()
  pgInit.setPropositionLayer(propLayerInit)

  graph = []  # list of PlanGraphLevel objects
  graph.append(pgInit)
  level = 0
  leftGoals = problem.goal.copy()
  level_sum = 0

  while True:
    # if leftGoals is empty, means we reached all the goals.
    if len(leftGoals) == 0:
      break
    # else if the goal is not in this level, and we finished to max graph, meens we vant reach the goal.
    elif isFixed(graph, level):
      return float('inf')
    props = graph[level].getPropositionLayer().getPropositions()
    # check for each goal if it is in the next props. If so, remove it from the left golas, and add the level to the sum
    for goal in leftGoals:
      if goal in props:
        level_sum += level
        leftGoals.remove(goal)

    pgTemp = PlanGraphLevel()
    pgTemp.expandWithoutMutex(graph[level])
    graph.append(pgTemp)
    level += 1
  # adding last level to the sum, and return it
  level_sum += level
  return level_sum
예제 #26
0
def maxLevel(state, problem):
  """
  The heuristic value is the number of layers required to expand all goal propositions.
  If the goal is not reachable from the state your heuristic should return float('inf')
  """
  newPropositionLayer = PropositionLayer()
  [newPropositionLayer.addProposition(p) for p in state]

  newPlanGraphLevel = PlanGraphLevel()
  newPlanGraphLevel.setPropositionLayer(newPropositionLayer)

  level = 0
  g = [newPlanGraphLevel]

  while problem.goalStateNotInPropLayer(g[level].getPropositionLayer().getPropositions()):
    if isFixed(g, level):
      return float("inf")

    level += 1
    nextPlanGraphLevel = PlanGraphLevel()
    nextPlanGraphLevel.expandWithoutMutex(g[level - 1])
    g.append(newPlanGraphLevel)

  return level
예제 #27
0
def levelSum(state, problem):
    """
    The heuristic value is the sum of sub-goals level they first appeared.
    If the goal is not reachable from the state your heuristic should return float('inf')
    """
    def nextPlan(plan):
        next_plan = PlanGraphLevel()
        next_plan.expandWithoutMutex(plan)
        return next_plan, next_plan.getPropositionLayer().getPropositions()

    propLayerInit = PropositionLayer()
    # add all to the new proposition layer
    lmap(propLayerInit.addProposition, state)

    plan = PlanGraphLevel()
    plan.setPropositionLayer(propLayerInit)
    plan_propositions = plan.getPropositionLayer().getPropositions()

    # create a graph that will store all the plan levels
    graph = []
    graph.append(plan)

    goals_levels = dict()
    goal = problem.goal

    # init goals levels
    for p in goal:
        goals_levels[p.getName()] = None

    # as long as we have for one of the goal None we didnt find the first level
    while None in goals_levels.values():
        # if fixed we won't have a solution
        if isFixed(graph, len(graph) - 1):
            return float('inf')
        # for each prop in the goal check if exist on the current plan
        # propositions
        for p in goal:
            # check that we didnt assign a value yet
            if p in plan_propositions and goals_levels[p.getName()] == None:
                # set the current level as the fist appearance of the prop
                goals_levels[p.getName()] = len(graph) - 1
        # create the next plan by the prev
        plan, plan_propositions = nextPlan(plan)
        # store in the graph
        graph.append(plan)

    return sum(goals_levels.values())
예제 #28
0
def maxLevel(state, problem):
    """
    The heuristic value is the number of layers required to expand all goal propositions.
    If the goal is not reachable from the state your heuristic should return float('inf')
    A good place to start would be:
    propLayerInit = PropositionLayer()          #create a new proposition layer
    for prop in state:
      #update the proposition layer with the propositions of the state
      propLayerInit.addProposition(prop)
    # create a new plan graph level (level is the action layer and the
    # propositions layer)
    pgInit = PlanGraphLevel()
    #update the new plan graph level with the the proposition layer
    pgInit.setPropositionLayer(propLayerInit)
    """
    def nextPlan(plan):
        next_plan = PlanGraphLevel()
        next_plan.expandWithoutMutex(plan)
        return next_plan, next_plan.getPropositionLayer().getPropositions()

    propLayerInit = PropositionLayer()
    # add all to the new proposition layer
    lmap(propLayerInit.addProposition, state)

    plan = PlanGraphLevel()
    plan.setPropositionLayer(propLayerInit)
    plan_propositions = plan.getPropositionLayer().getPropositions()

    # create a graph that will store all the plan levels
    graph = []
    graph.append(plan)

    # if we found we can rest
    while not problem.isGoalState(plan_propositions):
        # if fixed we won't have a solution
        if isFixed(graph, len(graph) - 1):
            return float('inf')
        # create the next plan by the prev
        plan, plan_propositions = nextPlan(plan)
        # store in the graph
        graph.append(plan)

    return len(graph) - 1
예제 #29
0
class PlanGraphLevel(object):
  """
  A class for representing a level in the plan graph.
  For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
  """
  independentActions = []  # updated to the independentActions of the propblem GraphPlan.py line 31 
  actions = []             # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
  props = []               # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26
  
  @staticmethod
  def setIndependentActions(independentActions):
    PlanGraphLevel.independentActions = independentActions
  
  @staticmethod  
  def setActions(actions):
    PlanGraphLevel.actions = actions
  
  @staticmethod    
  def setProps(props):
    PlanGraphLevel.props = props   
  
  def __init__(self):
    """
    Constructor
    """
    self.actionLayer = ActionLayer()    		# see actionLayer.py
    self.propositionLayer = PropositionLayer()	# see propositionLayer.py

  
  def getPropositionLayer(self):
    return self.propositionLayer
  
  def setPropositionLayer(self, propLayer):
    self.propositionLayer = propLayer  
  
  def getActionLayer(self):
    return self.actionLayer
    
  def setActionLayer(self, actionLayer):
    self.actionLayer = actionLayer

  def updateActionLayer(self, previousPropositionLayer):
    """ 
    Updates the action layer given the previous proposition layer (see propositionLayer.py)
    allAction is the list of all the actions (include noOp in the domain)
    """ 
    allActions = PlanGraphLevel.actions
    for a in allActions:
      if previousPropositionLayer.allPrecondsInLayer(a):
        self.actionLayer.addAction(a)
   

  def updateMutexActions(self, previousLayerMutexProposition):
    """
    Updates the mutex list in self.actionLayer,
    given the mutex proposition from the previous layer.
    currentLayerActions are the actions in the current action layer
    """
    currentLayerActions = self.actionLayer.getActions()
    for a_i in currentLayerActions:
      for a_j in currentLayerActions:
        if a_i != a_j and mutexActions(a_i, a_j, previousLayerMutexProposition):
          if Pair(a_i,a_j) not in self.actionLayer.mutexActions:
            self.actionLayer.addMutexActions(a_i,a_j)
   

  def updatePropositionLayer(self):
    """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    """
    currentLayerActions = self.actionLayer.getActions()
    for a in currentLayerActions:
      for p in a.getAdd():
        if p not in self.propositionLayer.getPropositions():
          self.propositionLayer.addProposition(p)
        p.addProducer(a)



  def updateMutexProposition(self):
    """
    updates the mutex propositions in the current proposition layer
    """
    currentLayerPropositions = self.propositionLayer.getPropositions()
    currentLayerMutexActions =  self.actionLayer.getMutexActions()
    for p_i in currentLayerPropositions:
      for p_j in currentLayerPropositions:
        if p_i != p_j and mutexPropositions(p_i,p_j,currentLayerMutexActions):
          if Pair(p_i,p_j) not in self.propositionLayer.mutexPropositions:
            self.propositionLayer.mutexPropositions.append(Pair(p_i,p_j)) 
	

  def expand(self, previousLayer):
    """
    Your algorithm should work as follows:
    First, given the propositions and the list of mutex propositions from the previous layer,
    set the actions in the action layer. 
    Then, set the mutex action in the action layer.
    Finally, given all the actions in the current layer, set the propositions and their mutex relations in the proposition layer.   
    """
    previousPropositionLayer = previousLayer.getPropositionLayer()
    previousLayerMutexProposition = previousPropositionLayer.getMutexProps()

    self.updateActionLayer(previousPropositionLayer) 
    self.updateMutexActions(previousLayerMutexProposition)

    self.updatePropositionLayer()
    self.updateMutexProposition()

            
  def expandWithoutMutex(self, previousLayer):
    """
    Questions 11 and 12
    You don't have to use this function
    """
    previousLayerProposition = previousLayer.getPropositionLayer()
    self.updateActionLayer(previousLayerProposition)
    self.updatePropositionLayer()
예제 #30
0
class PlanGraphLevel(object):
    """
  A class for representing a level in the plan graph.
  For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
  """
    independentActions = [
    ]  # updated to the independentActions of the propblem GraphPlan.py line 31
    actions = [
    ]  # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
    props = [
    ]  # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26

    @staticmethod
    def setIndependentActions(independentActions):
        PlanGraphLevel.independentActions = independentActions

    @staticmethod
    def setActions(actions):
        PlanGraphLevel.actions = actions

    @staticmethod
    def setProps(props):
        PlanGraphLevel.props = props

    def __init__(self):
        """
    Constructor
    """
        self.actionLayer = ActionLayer()  # see actionLayer.py
        self.propositionLayer = PropositionLayer()  # see propositionLayer.py

    def getPropositionLayer(self):
        # returns the proposition layer
        return self.propositionLayer

    def setPropositionLayer(self, propLayer):
        # sets the proposition layer
        self.propositionLayer = propLayer

    def getActionLayer(self):
        # returns the action layer
        return self.actionLayer

    def setActionLayer(self, actionLayer):
        # sets the action layer
        self.actionLayer = actionLayer

    def updateActionLayer(self, previousPropositionLayer):
        """ 
    Updates the action layer given the previous proposition layer (see propositionLayer.py)
    You should add an action to the layer if its preconditions are in the previous propositions layer,
    and the preconditions are not pairwise mutex. 
    allAction is the list of all the action (include noOp) in the domain
    You might want to use those functions:
    previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
    previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
    self.actionLayer.addAction(action) adds action to the current action layer
    """
        allActions = PlanGraphLevel.actions
        "*** YOUR CODE HERE ***"

        for action in allActions:
            if previousPropositionLayer.allPrecondsInLayer(action):
                self.actionLayer.addAction(action)

    def updateMutexActions(self, previousLayerMutexProposition):
        """
    Updates the mutex list in self.actionLayer,
    given the mutex proposition from the previous layer.
    currentLayerActions are the actions in the current action layer
    You might want to use this function:
    self.actionLayer.addMutexActions(action1, action2)
    adds the pair (action1, action2) to the mutex list in the current action layer
    Note that action is *not* mutex with itself
    """
        currentLayerActions = self.actionLayer.getActions()
        "*** YOUR CODE HERE ***"

        index = 0
        for action1 in currentLayerActions:
            index += 1
            for action2 in currentLayerActions[index:]:

                is_mutex = False
                for pre1 in action1.getPre():
                    for pre2 in action2.getPre():
                        if Pair(pre1, pre2) in previousLayerMutexProposition:
                            is_mutex = True
                            break

                if Pair(action1, action2) not in self.independentActions:
                    is_mutex = True

                if is_mutex:
                    self.actionLayer.addMutexActions(action1, action2)

    def updatePropositionLayer(self):
        """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    Note that same proposition in different layers might have different producers lists,
    hence you should create two different instances.
    currentLayerActions is the list of all the actions in the current layer.
    You might want to use those functions:
    dict() creates a new dictionary that might help to keep track on the propositions that you've
           already added to the layer
    self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer       
    
    """
        currentLayerActions = self.actionLayer.getActions()
        "*** YOUR CODE HERE ***"

        Propositions = dict()

        for action in currentLayerActions:
            for prop in action.getAdd():

                name = prop.getName()
                if Propositions.has_key(name):
                    new_prop = Propositions[name]
                else:
                    new_prop = Proposition(name)
                    Propositions[name] = new_prop
                    self.propositionLayer.addProposition(new_prop)

                new_prop.addProducer(action)

    def updateMutexProposition(self):
        """
    updates the mutex propositions in the current proposition layer
    You might want to use those functions:
    mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
    self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer
    """
        currentLayerPropositions = self.propositionLayer.getPropositions()
        currentLayerMutexActions = self.actionLayer.getMutexActions()
        "*** YOUR CODE HERE ***"

        index = 0
        for prop1 in currentLayerPropositions:
            index += 1
            for prop2 in currentLayerPropositions[index:]:
                if mutexPropositions(prop1, prop2, currentLayerMutexActions):
                    self.propositionLayer.addMutexProp(prop1, prop2)

    def expand(self, previousLayer):
        """
    Your algorithm should work as follows:
    First, given the propositions and the list of mutex propositions from the previous layer,
    set the actions in the action layer. 
    Then, set the mutex action in the action layer.
    Finally, given all the actions in the current layer,
    set the propositions and their mutex relations in the proposition layer.   
    """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        previousLayerMutexProposition = previousPropositionLayer.getMutexProps(
        )

        "*** YOUR CODE HERE ***"

        self.updateActionLayer(previousPropositionLayer)
        self.updateMutexActions(previousLayerMutexProposition)
        self.updatePropositionLayer()
        self.updateMutexProposition()

        #print('expand')
        #print('actions              ' + str([action.getName() for action in self.actionLayer.actions]))
        #print('propositions         ' + str([prop.getName() for prop in self.propositionLayer.propositions]))
        #print('actions mutexes      ' + str([pair.a.getName()+","+pair.b.getName() for pair in self.actionLayer.mutexActions]))
        #print('propositions mutexes ' +  str([pair.a.getName()+","+pair.b.getName() for pair in self.propositionLayer.mutexPropositions]))

    def expandWithoutMutex(self, previousLayer):
        """
    Questions 11 and 12
    You don't have to use this function
    """
        previousLayerProposition = previousLayer.getPropositionLayer()
        "*** YOUR CODE HERE ***"

        self.updateActionLayer(previousLayerProposition)
        self.updatePropositionLayer()
예제 #31
0
class PlanGraphLevel(object):
    """
    A class for representing a level in the plan graph.
    For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
    """
    independentActions = []  # updated to the independentActions of the propblem GraphPlan.py line 31
    actions = []  # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
    props = []  # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26

    @staticmethod
    def setIndependentActions(independentActions):
        PlanGraphLevel.independentActions = independentActions

    @staticmethod
    def setActions(actions):
        PlanGraphLevel.actions = actions

    @staticmethod
    def setProps(props):
        PlanGraphLevel.props = props

    def __init__(self):
        """
        Constructor
        """
        self.actionLayer = ActionLayer()  # see actionLayer.py
        self.propositionLayer = PropositionLayer()  # see propositionLayer.py

    def getPropositionLayer(self):
        # returns the proposition layer
        return self.propositionLayer

    def setPropositionLayer(self, propLayer):
        # sets the proposition layer
        self.propositionLayer = propLayer

    def getActionLayer(self):
        # returns the action layer
        return self.actionLayer

    def setActionLayer(self, actionLayer):
        # sets the action layer
        self.actionLayer = actionLayer

    def updateActionLayer(self, previousPropositionLayer):
        """
        Updates the action layer given the previous proposition layer (see propositionLayer.py)
        You should add an action to the layer if its preconditions are in the previous propositions layer,
        and the preconditions are not pairwise mutex.
        allAction is the list of all the action (include noOp) in the domain
        You might want to use those functions:
        previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
        previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
        self.actionLayer.addAction(action) adds action to the current action layer
        """
        # Check all possible actions
        for action in PlanGraphLevel.actions:
            if previousPropositionLayer.allPrecondsInLayer(action):  # only if all pre. exist, then try to add it
                mutexes = previousPropositionLayer.getMutexProps()
                has_mutexes = False
                # check for all pairs of mutexes, also if we have one pre. only, it will work
                for p1, p2 in product(action.getPre(), action.getPre()):
                    if Pair(p1, p2) in mutexes:
                        has_mutexes = True
                        break  # mutex found, we don't want to continue our checking
                if not has_mutexes:
                    self.actionLayer.addAction(action)

    def updateMutexActions(self, previousLayerMutexProposition):
        """
        Updates the mutex list in self.actionLayer,
        given the mutex proposition from the previous layer.
        currentLayerActions are the actions in the current action layer
        You might want to use this function:
        self.actionLayer.addMutexActions(action1, action2)
        adds the pair (action1, action2) to the mutex list in the current action layer
        Note that action is *not* mutex with itself
        """
        current_layer_actions = self.actionLayer.getActions()
        for a1, a2 in product(current_layer_actions, current_layer_actions):
            if a1 != a2 and mutexActions(a1, a2, previousLayerMutexProposition):
                self.actionLayer.addMutexActions(a1, a2)

    def updatePropositionLayer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        currentLayerActions is the list of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer

        """
        added_props = {}  # this dictionary will see what propositions were added
        # for all actions do the test
        for action in self.actionLayer.getActions():
            for prop in action.getAdd():
                if prop.getName() not in added_props:  # we don't wan't to add proposition twice
                    added_props[prop.getName()] = prop
                    self.getPropositionLayer().addProposition(prop)
                else:
                    # in case action not in producers, not see that issue but was asked to add that line in description
                    if action not in prop.getProducers():
                        prop.addProducer(action)

    def updateMutexProposition(self):
        """
        updates the mutex propositions in the current proposition layer
        You might want to use those functions:
        mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
        self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer
        """
        currentLayerPropositions = self.propositionLayer.getPropositions()
        currentLayerMutexActions = self.actionLayer.getMutexActions()

        for p1, p2 in product(currentLayerPropositions, currentLayerPropositions):
            if p1 != p2 and mutexPropositions(p1, p2, currentLayerMutexActions):
                self.getPropositionLayer().addMutexProp(p1, p2)

    def expand(self, previousLayer):
        """
        Your algorithm should work as follows:
        First, given the propositions and the list of mutex propositions from the previous layer,
        set the actions in the action layer.
        Then, set the mutex action in the action layer.
        Finally, given all the actions in the current layer,
        set the propositions and their mutex relations in the proposition layer.
        """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        previousLayerMutexProposition = previousPropositionLayer.getMutexProps()

        self.updateActionLayer(previousPropositionLayer)
        self.updateMutexActions(previousLayerMutexProposition)
        self.updatePropositionLayer()
        self.updateMutexProposition()

    def expandWithoutMutex(self, previousLayer):
        """
        Questions 11 and 12
        You don't have to use this function
        """
        previousLayerProposition = previousLayer.getPropositionLayer()
        self.updateActionLayer(previousLayerProposition)
        self.updatePropositionLayer()
예제 #32
0
class PlanGraphLevel(object):
    """
    A class for representing a level in the plan graph.
    For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
    """
    independentActions = [
    ]  # updated to the independentActions of the propblem GraphPlan.py line 31
    # updated to the actions of the problem GraphPlan.py line 32 and
    # planningProblem.py line 25
    actions = []
    # updated to the propositions of the problem GraphPlan.py line 33 and
    # planningProblem.py line 26
    props = []

    @staticmethod
    def setIndependentActions(independentActions):
        PlanGraphLevel.independentActions = independentActions

    @staticmethod
    def setActions(actions):
        PlanGraphLevel.actions = actions

    @staticmethod
    def setProps(props):
        PlanGraphLevel.props = props

    def __init__(self):
        """
        Constructor
        """
        self.actionLayer = ActionLayer()  # see actionLayer.py
        self.propositionLayer = PropositionLayer()  # see propositionLayer.py

    def getPropositionLayer(self):
        # returns the proposition layer
        return self.propositionLayer

    def setPropositionLayer(self, propLayer):
        # sets the proposition layer
        self.propositionLayer = propLayer

    def getActionLayer(self):
        # returns the action layer
        return self.actionLayer

    def setActionLayer(self, actionLayer):
        # sets the action layer
        self.actionLayer = actionLayer

    def updateActionLayer(self, previousPropositionLayer):
        """
        Updates the action layer given the previous proposition layer (see propositionLayer.py)
        You should add an action to the layer if its preconditions are in the previous propositions layer,
        and the preconditions are not pairwise mutex.
        allAction is the list of all the action (include noOp) in the domain
        You might want to use those functions:
        previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
        previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
        self.actionLayer.addAction(action) adds action to the current action layer
        """
        allActions = PlanGraphLevel.actions

        def isAllPrecondsInLayer(action):
            return previousPropositionLayer.allPrecondsInLayer(action)

        def isPropositionMutex(action):
            # create all the combinations of the actions preconditions
            all_combinations = [(cond1, cond2) for cond1 in action.getPre()
                                for cond2 in action.getPre()]
            # check if exists at least one mutex
            are_any_mutex = any(
                lmap(
                    lambda conds: previousPropositionLayer.isMutex(
                        conds[0], conds[1]),
                    lfilter(isDifferent, all_combinations)))
            # retun if rthere are no mutex
            return not are_any_mutex

        addActionToLayer = self.actionLayer.addAction

        lmap(
            addActionToLayer,
            lfilter(isPropositionMutex,
                    lfilter(isAllPrecondsInLayer, allActions)))

    def updateMutexActions(self, previousLayerMutexProposition):
        """
        Updates the mutex list in self.actionLayer,
        given the mutex proposition from the previous layer.
        currentLayerActions are the actions in the current action layer
        You might want to use this function:
        self.actionLayer.addMutexActions(action1, action2)
        adds the pair (action1, action2) to the mutex list in the current action layer
        Note that action is *not* mutex with itself
        """
        currentLayerActions = self.actionLayer.getActions()
        # create all the combinations of the actions
        all_combinations = [(action1, action2)
                            for action1 in currentLayerActions
                            for action2 in currentLayerActions]

        def isMutexActions(actions):
            a1, a2 = actions
            return mutexActions(a1, a2, previousLayerMutexProposition)

        def isNotIn(actions):
            a1, a2 = actions
            return Pair(a1, a2) not in self.actionLayer.mutexActions

        def addMutexActions(actions):
            a1, a2 = actions
            self.actionLayer.addMutexActions(a1, a2)

        lmap(
            addMutexActions,
            lfilter(
                isNotIn,
                lfilter(isMutexActions, lfilter(isDifferent,
                                                all_combinations))))

    def updatePropositionLayer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        currentLayerActions is the list of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer

        """
        currentLayerActions = self.actionLayer.getActions()

        def isNotIn(prop):
            return prop not in self.propositionLayer.getPropositions()

        addProposition = self.propositionLayer.addProposition

        for action in currentLayerActions:
            for prop in action.getAdd():
                if isNotIn(prop):
                    addProposition(prop)
                prop.addProducer(action)

    def updateMutexProposition(self):
        """
        updates the mutex propositions in the current proposition layer
        You might want to use those functions:
        mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
        self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer
        """
        currentLayerPropositions = self.propositionLayer.getPropositions()
        currentLayerMutexActions = self.actionLayer.getMutexActions()

        # create all the combinations of the propositions
        all_combinations = [(prop1, prop2)
                            for prop1 in currentLayerPropositions
                            for prop2 in currentLayerPropositions]

        def isMutexPropositions(propositions):
            p1, p2 = propositions
            return mutexPropositions(p1, p2, currentLayerMutexActions)

        def isNotIn(propositions):
            p1, p2 = propositions
            return Pair(p1, p2) not in self.propositionLayer.mutexPropositions

        def addMutexPropositions(propositions):
            p1, p2 = propositions
            return self.propositionLayer.mutexPropositions.append(Pair(p1, p2))

        lmap(
            addMutexPropositions,
            lfilter(
                isNotIn,
                lfilter(isMutexPropositions,
                        lfilter(isDifferent, all_combinations))))

    def expand(self, previousLayer):
        """
        Your algorithm should work as follows:
        First, given the propositions and the list of mutex propositions from the previous layer,
        set the actions in the action layer.
        Then, set the mutex action in the action layer.
        Finally, given all the actions in the current layer,
        set the propositions and their mutex relations in the proposition layer.
        """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        previousLayerMutexProposition = previousPropositionLayer.getMutexProps(
        )

        self.updateActionLayer(previousPropositionLayer)
        self.updateMutexActions(previousLayerMutexProposition)

        self.updatePropositionLayer()
        self.updateMutexProposition()

    def expandWithoutMutex(self, previousLayer):
        """
        Questions 11 and 12
        You don't have to use this function
        """
        previousLayerProposition = previousLayer.getPropositionLayer()
        self.updateActionLayer(previousLayerProposition)
        self.updatePropositionLayer()
예제 #33
0
 def __init__(self):
 
     self.actionLayer = ActionLayer()    		                            # see actionLayer.py
     self.propositionLayer = PropositionLayer()	                        # see propositionLayer.py
예제 #34
0
class PlanGraphLevel(object):
    """
    A class for representing a level in the plan graph.
    For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
    """
    independentActions = []  # updated to the independentActions of the propblem GraphPlan.py line 31
    actions = []             # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
    props = []               # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26

    @staticmethod
    def setIndependentActions(independentActions):
        PlanGraphLevel.independentActions = independentActions

    @staticmethod
    def setActions(actions):
        PlanGraphLevel.actions = actions

    @staticmethod
    def setProps(props):
        PlanGraphLevel.props = props

    def __init__(self):
        """
        Constructor
        """
        self.actionLayer = ActionLayer()                        # see actionLayer.py
        self.propositionLayer = PropositionLayer()  # see propositionLayer.py

    def getPropositionLayer(self):
    # returns the proposition layer
        return self.propositionLayer

    def setPropositionLayer(self, propLayer):
    # sets the proposition layer
        self.propositionLayer = propLayer

    def getActionLayer(self):
    # returns the action layer
        return self.actionLayer

    def setActionLayer(self, actionLayer):
    # sets the action layer
        self.actionLayer = actionLayer

    def updateActionLayer(self, previousPropositionLayer):
        """
        Updates the action layer given the previous proposition layer (see propositionLayer.py)
        You should add an action to the layer if its preconditions are in the previous propositions layer,
        and the preconditions are not pairwise mutex.
        allAction is the list of all the action (include noOp) in the domain
        You might want to use those functions:
        previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
        previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
        self.actionLayer.addAction(action) adds action to the current action layer
        """
        allActions = PlanGraphLevel.actions        
        for action in allActions:
            if previousPropositionLayer.allPrecondsInLayer(action) \
                and True not in [previousPropositionLayer.isMutex(p1, p2) for p1, p2 in exclusiveProduct(action.getPre(), action.getPre())]:
                self.actionLayer.addAction(action)
            
    def updateMutexActions(self, previousLayerMutexProposition):
        """
        Updates the mutex list in self.actionLayer,
        given the mutex proposition from the previous layer.
        currentLayerActions are the actions in the current action layer
        You might want to use this function:
        self.actionLayer.addMutexActions(action1, action2)
        adds the pair (action1, action2) to the mutex list in the current action layer
        Note that action is *not* mutex with itself
        """
        currentLayerActions = self.actionLayer.getActions()        
        actionPairs = exclusiveProduct(currentLayerActions, currentLayerActions)
        for a1, a2 in actionPairs:
            if mutexActions(a1, a2, previousLayerMutexProposition):
                self.actionLayer.addMutexActions(a1, a2)
        
    def updatePropositionLayer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        currentLayerActions is the list of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer
        """
        
        currentLayerActions = self.actionLayer.getActions()
        props = dict()
                        
        for a in currentLayerActions:
            for prop in a.getAdd():
                name = prop.getName()
                if name not in props.keys():
                    props[name] = Proposition(name)
                props[name].addProducer(a)
        
        for prop in props.values():
            self.propositionLayer.addProposition(prop)

    def updateMutexProposition(self):
        """
        updates the mutex propositions in the current proposition layer
        You might want to use those functions:
        mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
        self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer
        """
        currentLayerPropositions = self.propositionLayer.getPropositions()
        currentLayerMutexActions = self.actionLayer.getMutexActions()
        for p1, p2 in exclusiveProduct(currentLayerPropositions, currentLayerPropositions):
            if mutexPropositions(p1, p2, currentLayerMutexActions):
                self.propositionLayer.addMutexProp(p1, p2)

    def expand(self, previousLayer):
        """
        Your algorithm should work as follows:
        First, given the propositions and the list of mutex propositions from the previous layer,
        set the actions in the action layer.
        Then, set the mutex action in the action layer.
        Finally, given all the actions in the current layer,
        set the propositions and their mutex relations in the proposition layer.
        """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        previousLayerMutexProposition = previousPropositionLayer.getMutexProps()
        self.updateActionLayer(previousPropositionLayer)
        self.updateMutexActions(previousLayerMutexProposition)
        self.updatePropositionLayer()
        self.updateMutexProposition()

    def expandWithoutMutex(self, previousLayer):
        """
        Expand the graph without updating the mutex relations
        """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        self.updateActionLayer(previousPropositionLayer)
        self.updatePropositionLayer()
예제 #35
0
class PlanGraphLevel(object):
  
    """
    A class for representing a level in the plan graph.
    For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
    """
    independentActions = None                                               # updated to the independentActions of the propblem GraphPlan.py line 25
    actions = None                                                          # updated to the actions of the problem GraphPlan.py line 26 and planningProblem.py line 25
    props = None                                                            # updated to the propositions of the problem GraphPlan.py line 27 and planningProblem.py line 26
  
    @staticmethod
    def setIndependentActions(independentActions):
        PlanGraphLevel.independentActions = independentActions
  
    @staticmethod  
    def setActions(actions):
        PlanGraphLevel.actions = actions
  
    @staticmethod    
    def setProps(props):
        PlanGraphLevel.props = props   
  
    def __init__(self):
    
        self.actionLayer = ActionLayer()    		                            # see actionLayer.py
        self.propositionLayer = PropositionLayer()	                        # see propositionLayer.py
  
    def getPropositionLayer(self):                                        # returns the proposition layer

        return self.propositionLayer
  
    def setPropositionLayer(self, propLayer):                             # sets the proposition layer
        self.propositionLayer = propLayer  
  
    def getActionLayer(self):                                             # returns the action layer
        return self.actionLayer
    
    def setActionLayer(self, actionLayer):                                # sets the action layer
        self.actionLayer = actionLayer

    def updateActionLayer(self, previousPropositionLayer):
        """ 
        Updates the action layer given the previous proposition layer (see propositionLayer.py)
        You should add an action to the layer if its preconditions are in the previous propositions layer,
        and the preconditions are not pairwise mutex. 
        allAction is the set of all the action (include noOp) in the domain
        You might want to use those functions:
        previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
        previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
        self.actionLayer.addAction(action) adds action to the current action layer
        """ 
        allActions = PlanGraphLevel.actions
        "*** YOUR CODE HERE ***"
        for a in allActions:
            if previousPropositionLayer.allPrecondsInLayer(a):
                self.actionLayer.addAction(a)
    
    def updateMutexActions(self, previousLayerMutexProposition):
        """
        Updates the mutex set in self.actionLayer,
        given the mutex proposition from the previous layer.
        currentLayerActions are the actions in the current action layer
        You might want to use this function:
        self.actionLayer.addMutexActions(action1, action2)
        adds the pair (action1, action2) to the mutex set in the current action layer
        Note that action is *not* mutex with itself
        """
        currentLayerActions = self.actionLayer.getActions()
        "*** YOUR CODE HERE ***"
        for a_i in currentLayerActions:
            for a_j in currentLayerActions:
                if a_i != a_j and mutexActions(a_i, a_j, previousLayerMutexProposition):
                    if Pair(a_i,a_j) not in self.actionLayer.mutexActions:
                        self.actionLayer.addMutexActions(a_i,a_j)
    
    def updatePropositionLayer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        currentLayerActions is the set of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
           already added to the layer
        self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer
        """
        currentLayerActions = self.actionLayer.getActions()
        "*** YOUR CODE HERE ***"
        for a in currentLayerActions:
          for p in a.getAdd():
            if p not in self.propositionLayer.getPropositions():
              self.propositionLayer.addProposition(p)
            p.addProducer(a)
    
    def updateMutexProposition(self):
        """
        updates the mutex propositions in the current proposition layer
        You might want to use those functions:
        mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
        self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex set of the current layer
        """
    
        currentLayerPropositions = self.propositionLayer.getPropositions()
        currentLayerMutexActions =  self.actionLayer.getMutexActions()
        "*** YOUR CODE HERE ***"
        for p_i in currentLayerPropositions:
          for p_j in currentLayerPropositions:
            if p_i != p_j and mutexPropositions(p_i,p_j,currentLayerMutexActions):
              if Pair(p_i,p_j) not in self.propositionLayer.mutexPropositions:
                self.propositionLayer.mutexPropositions.append(Pair(p_i,p_j))
    
    
    def expand(self, previousLayer):
        """
        Your algorithm should work as follows:
        First, given the propositions and the list of mutex propositions from the previous layer,
        set the actions in the action layer. 
        Then, set the mutex action in the action layer.
        Finally, given all the actions in the current layer,
        set the propositions and their mutex relations in the proposition layer.
        """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        previousLayerMutexProposition = previousPropositionLayer.getMutexProps()

        "*** YOUR CODE HERE ***"
        self.updateActionLayer(previousPropositionLayer) 
        self.updateMutexActions(previousLayerMutexProposition)

        self.updatePropositionLayer()
        self.updateMutexProposition()
            
    def expandWithoutMutex(self, previousLayer):
        """
        Questions 11 and 12
        You don't have to use this function
        """
        previousLayerProposition = previousLayer.getPropositionLayer()
        "*** YOUR CODE HERE ***"
    
        self.updateActionLayer(previousLayerProposition)
        self.updatePropositionLayer()
		
    def mutexActions(a1, a2, mutexProps):
        """
        This function returns true if a1 and a2 are mutex actions.
        We first check whether a1 and a2 are in PlanGraphLevel.independentActions,
        this is the list of all the independent pair of actions (according to your implementation in question 1).
        If not, we check whether a1 and a2 have competing needs
        """ 
        if Pair(a1,a2) not in PlanGraphLevel.independentActions:
            return True
        return haveCompetingNeeds(a1, a2, mutexProps)

    def haveCompetingNeeds(a1, a2, mutexProps):
        pre1 = a1.getPre()
        pre2 = a2.getPre()
        """
        Complete code for deciding whether actions a1 and a2 have competing needs,
        given the mutex proposition from previous level (list of pairs of propositions).
        Hint: for propositions p  and q, the command  "Pair(p, q) in mutexProps"
        returns true if p and q are mutex in the previous level
        """
        "*** YOUR CODE HERE ***" 
        # Get preconditions of both actions


    # Competing needs: Check if a1 and a2 have preconditions that are mutex
        for p1 in pre1:
            for p2 in pre2:
                if Pair(p1, p2) in mutexProps:
                    return True 
        return False
    
    
		
    def mutexPropositions(prop1, prop2, mutexActions):
        prod1 = prop1.getProducers()
        prod2 = prop2.getProducers()
        """
      complete code for deciding whether two propositions are mutex,
      given the mutex action from the current level (set of pairs of actions).
      Your updateMutexProposition function should call this function
      You might want to use this function:
      prop1.getProducers() returns the set of all the possible actions in the layer that have prop1 on their add list
      """
        "*** YOUR CODE HERE ***"
        for a1 in prod1:
            for a2 in prod2:
          # Check if all actions are pairwise mutex
              if Pair(a1,a2) not in mutexActions:
                return False
        return True
예제 #36
0
class PlanGraphLevel(object):
    """
    A class for representing a level in the plan graph.
    For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
    """
    independentActions = []  # updated to the independentActions of the propblem GraphPlan.py line 31
    actions = []  # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
    props = []  # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26

    @staticmethod
    def setIndependentActions(independentActions):
        PlanGraphLevel.independentActions = independentActions

    @staticmethod
    def setActions(actions):
        PlanGraphLevel.actions = actions

    @staticmethod
    def setProps(props):
        PlanGraphLevel.props = props

    def __init__(self):
        """
        Constructor
        """
        self.actionLayer = ActionLayer()  # see actionLayer.py
        self.propositionLayer = PropositionLayer()  # see propositionLayer.py

    def getPropositionLayer(self):
        # returns the proposition layer
        return self.propositionLayer

    def setPropositionLayer(self, propLayer):
        # sets the proposition layer
        self.propositionLayer = propLayer

    def getActionLayer(self):
        # returns the action layer
        return self.actionLayer

    def setActionLayer(self, actionLayer):
        # sets the action layer
        self.actionLayer = actionLayer

    def updateActionLayer(self, previousPropositionLayer):
        """
        Updates the action layer given the previous proposition layer (see propositionLayer.py)
        You should add an action to the layer if its preconditions are in the previous propositions layer,
        and the preconditions are not pairwise mutex.
        allAction is the list of all the action (include noOp) in the domain
        You might want to use those functions:
        previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
        previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
        self.actionLayer.addAction(action) adds action to the current action layer
        """
        allActions = PlanGraphLevel.actions

        for action in allActions:
            if previousPropositionLayer.allPrecondsInLayer(action) and\
                    not pre_contains_pairwise_mutex(action, previousPropositionLayer):
                self.actionLayer.addAction(action)

    def updateMutexActions(self, previousLayerMutexProposition):
        """
        Updates the mutex list in self.actionLayer,
        given the mutex proposition from the previous layer.
        currentLayerActions are the actions in the current action layer
        You might want to use this function:
        self.actionLayer.addMutexActions(action1, action2)
        adds the pair (action1, action2) to the mutex list in the current action layer
        Note that action is *not* mutex with itself
        """
        currentLayerActions = self.actionLayer.getActions()

        for action1 in currentLayerActions:
                for action2 in currentLayerActions:
                    if action1 != action2 and\
                            mutexActions(action1, action2, previousLayerMutexProposition):
                        self.actionLayer.addMutexActions(action1, action2)



    def updatePropositionLayer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        currentLayerActions is the list of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer

        """
        currentLayerActions = self.actionLayer.getActions()
        props_dict = dict()
        for action in currentLayerActions:
            for add_props in action.getAdd():
                if add_props.getName() not in props_dict:
                    props_dict[add_props.getName()] = [action]
                else:
                    props_dict[add_props.getName()] = \
                        props_dict[add_props.getName()]+[action]

        for key in props_dict:
            prop_temp = Proposition(key)
            prop_temp.setProducers(props_dict[key])    #takes care of adding procedure
            self.propositionLayer.addProposition(prop_temp)
        #notice there is no mention of mutexes. is this ok?


    def updateMutexProposition(self):
        """
        updates the mutex propositions in the current proposition layer
        You might want to use those functions:
        mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
        self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer
        """
        currentLayerPropositions = self.propositionLayer.getPropositions()
        currentLayerMutexActions = self.actionLayer.getMutexActions()
        for prop1 in currentLayerPropositions:
            for prop2 in currentLayerPropositions:
                if mutexPropositions(prop1, prop2, currentLayerMutexActions) \
                        and prop1 != prop2:
                    self.propositionLayer.addMutexProp(prop1, prop2)



    def expand(self, previousLayer):
        """
        Your algorithm should work as follows:
        First, given the propositions and the list of mutex propositions from the previous layer,
        set the actions in the action layer.
        Then, set the mutex action in the action layer.
        Finally, given all the actions in the current layer,
        set the propositions and their mutex relations in the proposition layer.
        """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        previousLayerMutexProposition = previousPropositionLayer.getMutexProps()

        self.updateActionLayer(previousPropositionLayer)
        self.updateMutexActions(previousLayerMutexProposition)

        self.updatePropositionLayer()
        self.updateMutexProposition()

    def expandWithoutMutex(self, previousLayer):
        """
        Questions 11 and 12
        You don't have to use this function
        """

        previousPropositionLayer = previousLayer.getPropositionLayer()

        self.updateActionLayer(previousPropositionLayer)

        self.updatePropositionLayer()
예제 #37
0
 def __init__(self):
   """
   Constructor
   """
   self.actionLayer = ActionLayer()    		    # see actionLayer.py
   self.propositionLayer = PropositionLayer()	# see propositionLayer.py
예제 #38
0
파일: planGraphLevel.py 프로젝트: odedm/ai
class PlanGraphLevel(object):
    """
    A class for representing a level in the plan graph.
    For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
    """
    independentActions = [
    ]  # updated to the independentActions of the propblem GraphPlan.py line 31
    actions = [
    ]  # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
    props = [
    ]  # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26

    @staticmethod
    def setIndependentActions(independentActions):
        PlanGraphLevel.independentActions = independentActions

    @staticmethod
    def setActions(actions):
        PlanGraphLevel.actions = actions

    @staticmethod
    def setProps(props):
        PlanGraphLevel.props = props

    def __init__(self):
        """
        Constructor
        """
        self.actionLayer = ActionLayer()  # see actionLayer.py
        self.propositionLayer = PropositionLayer()  # see propositionLayer.py

    def getPropositionLayer(self):
        # returns the proposition layer
        return self.propositionLayer

    def setPropositionLayer(self, propLayer):
        # sets the proposition layer
        self.propositionLayer = propLayer

    def getActionLayer(self):
        # returns the action layer
        return self.actionLayer

    def setActionLayer(self, actionLayer):
        # sets the action layer
        self.actionLayer = actionLayer

    def updateActionLayer(self, previousPropositionLayer):
        """
        Updates the action layer given the previous proposition layer (see propositionLayer.py)
        You should add an action to the layer if its preconditions are in the previous propositions layer,
        and the preconditions are not pairwise mutex.
        allAction is the list of all the action (include noOp) in the domain
        You might want to use those functions:
        previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
        previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
        self.actionLayer.addAction(action) adds action to the current action layer
        """
        allActions = PlanGraphLevel.actions
        for action in allActions:
            if previousPropositionLayer.allPrecondsInLayer(action) \
                and True not in [previousPropositionLayer.isMutex(p1, p2) for p1, p2 in exclusiveProduct(action.getPre(), action.getPre())]:
                self.actionLayer.addAction(action)

    def updateMutexActions(self, previousLayerMutexProposition):
        """
        Updates the mutex list in self.actionLayer,
        given the mutex proposition from the previous layer.
        currentLayerActions are the actions in the current action layer
        You might want to use this function:
        self.actionLayer.addMutexActions(action1, action2)
        adds the pair (action1, action2) to the mutex list in the current action layer
        Note that action is *not* mutex with itself
        """
        currentLayerActions = self.actionLayer.getActions()
        actionPairs = exclusiveProduct(currentLayerActions,
                                       currentLayerActions)
        for a1, a2 in actionPairs:
            if mutexActions(a1, a2, previousLayerMutexProposition):
                self.actionLayer.addMutexActions(a1, a2)

    def updatePropositionLayer(self):
        """
        Updates the propositions in the current proposition layer,
        given the current action layer.
        don't forget to update the producers list!
        Note that same proposition in different layers might have different producers lists,
        hence you should create two different instances.
        currentLayerActions is the list of all the actions in the current layer.
        You might want to use those functions:
        dict() creates a new dictionary that might help to keep track on the propositions that you've
               already added to the layer
        self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer
        """

        currentLayerActions = self.actionLayer.getActions()
        props = dict()

        for a in currentLayerActions:
            for prop in a.getAdd():
                name = prop.getName()
                if name not in props.keys():
                    props[name] = Proposition(name)
                props[name].addProducer(a)

        for prop in props.values():
            self.propositionLayer.addProposition(prop)

    def updateMutexProposition(self):
        """
        updates the mutex propositions in the current proposition layer
        You might want to use those functions:
        mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
        self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer
        """
        currentLayerPropositions = self.propositionLayer.getPropositions()
        currentLayerMutexActions = self.actionLayer.getMutexActions()
        for p1, p2 in exclusiveProduct(currentLayerPropositions,
                                       currentLayerPropositions):
            if mutexPropositions(p1, p2, currentLayerMutexActions):
                self.propositionLayer.addMutexProp(p1, p2)

    def expand(self, previousLayer):
        """
        Your algorithm should work as follows:
        First, given the propositions and the list of mutex propositions from the previous layer,
        set the actions in the action layer.
        Then, set the mutex action in the action layer.
        Finally, given all the actions in the current layer,
        set the propositions and their mutex relations in the proposition layer.
        """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        previousLayerMutexProposition = previousPropositionLayer.getMutexProps(
        )
        self.updateActionLayer(previousPropositionLayer)
        self.updateMutexActions(previousLayerMutexProposition)
        self.updatePropositionLayer()
        self.updateMutexProposition()

    def expandWithoutMutex(self, previousLayer):
        """
        Expand the graph without updating the mutex relations
        """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        self.updateActionLayer(previousPropositionLayer)
        self.updatePropositionLayer()
예제 #39
0
class PlanGraphLevel(object):
  """
  A class for representing a level in the plan graph.
  For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
  """
  independentActions = []  # updated to the independentActions of the propblem GraphPlan.py line 31
  actions = []             # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
  props = []               # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26

  @staticmethod
  def setIndependentActions(independentActions):
    PlanGraphLevel.independentActions = independentActions

  @staticmethod
  def setActions(actions):
    PlanGraphLevel.actions = actions

  @staticmethod
  def setProps(props):
    PlanGraphLevel.props = props

  def __init__(self):
    """
    Constructor
    """
    self.actionLayer = ActionLayer()    		# see actionLayer.py
    self.propositionLayer = PropositionLayer()	# see propositionLayer.py


  def getPropositionLayer(self):
    return self.propositionLayer

  def setPropositionLayer(self, propLayer):
    self.propositionLayer = propLayer

  def getActionLayer(self):
    return self.actionLayer

  def setActionLayer(self, actionLayer):
    self.actionLayer = actionLayer

  def updateActionLayer(self, previousPropositionLayer):
    """
    Updates the action layer given the previous proposition layer (see propositionLayer.py)
    allAction is the list of all the action (include noOp in the domain)
    """
    allActions = PlanGraphLevel.actions
    for action in allActions:
      if previousPropositionLayer.allPrecondsInLayer(action):
        self.actionLayer.addAction(action)
        for p1 in action.getPre():
          for p2 in action.getPre():
            if previousPropositionLayer.isMutex(p1, p2):
              self.actionLayer.removeActions(action)

  def updateMutexActions(self, previousLayerMutexProposition):
    """
    Updates the mutex list in self.actionLayer,
    given the mutex proposition from the previous layer.
    currentLayerActions are the actions in the current action layer
    """
    currentLayerActions = self.actionLayer.getActions()
    for a1 in currentLayerActions:
      for a2 in currentLayerActions:
        if a1 == a2:
          continue
        if mutexActions(a1, a2, previousLayerMutexProposition):
          self.actionLayer.addMutexActions(a1, a2)


  def updatePropositionLayer(self):
    """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    """
    currentLayerActions = self.actionLayer.getActions()
    propsToAdd = dict()
    for action in currentLayerActions:
      for prop in action.getAdd():
        if prop.getName() not in propsToAdd:
          propsToAdd[prop.getName()] = Proposition(prop.getName())
        temp = propsToAdd[prop.getName()]
        if action not in temp.getProducers():
          temp.addProducer(action)
    for prop in propsToAdd.values():
      self.propositionLayer.addProposition(prop)

  def updateMutexProposition(self):
    """
    updates the mutex propositions in the current proposition layer
    """
    currentLayerPropositions = self.propositionLayer.getPropositions()
    currentLayerMutexActions =  self.actionLayer.getMutexActions()
    for prop1 in currentLayerPropositions:
      for prop2 in currentLayerPropositions:
        if prop1 == prop2:
          continue
        if mutexPropositions(prop1, prop2, currentLayerMutexActions):
          self.propositionLayer.addMutexProp(prop1, prop2)


  def expand(self, previousLayer):
    """
    Your algorithm should work as follows:
    First, given the propositions and the list of mutex propositions from the previous layer,
    set the actions in the action layer.
    Then, set the mutex action in the action layer.
    Finally, given all the actions in the current layer, set the propositions and their mutex relations in the proposition layer.
    """
    previousPropositionLayer = previousLayer.getPropositionLayer()
    previousLayerMutexProposition = previousPropositionLayer.getMutexProps()

    self.updateActionLayer(previousPropositionLayer)
    self.updateMutexActions(previousLayerMutexProposition)
    self.updatePropositionLayer()
    self.updateMutexProposition()


  def expandWithoutMutex(self, previousLayer):
    """
    Questions 11 and 12
    You don't have to use this function
    """
    previousLayerProposition = previousLayer.getPropositionLayer()
    "*** YOUR CODE HERE ***"
예제 #40
0
파일: planGraphLevel.py 프로젝트: omerzk/AI
class PlanGraphLevel(object):
  """
  A class for representing a level in the plan graph.
  For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
  """
  independentActions = None                                               # updated to the independentActions of the propblem GraphPlan.py line 25
  actions = None                                                          # updated to the actions of the problem GraphPlan.py line 26 and planningProblem.py line 25
  props = None                                                            # updated to the propositions of the problem GraphPlan.py line 27 and planningProblem.py line 26
  
  @staticmethod
  def setIndependentActions(independentActions):
    PlanGraphLevel.independentActions = independentActions
  
  @staticmethod  
  def setActions(actions):
    PlanGraphLevel.actions = actions
  
  @staticmethod    
  def setProps(props):
    PlanGraphLevel.props = props   
  
  def __init__(self):
    """
    Constructor
    """
    self.actionLayer = ActionLayer()    		                            # see actionLayer.py
    self.propositionLayer = PropositionLayer()	                        # see propositionLayer.py
  
  def getPropositionLayer(self):                                        # returns the proposition layer

    return self.propositionLayer
  
  def setPropositionLayer(self, propLayer):                             # sets the proposition layer
    self.propositionLayer = propLayer  
  
  def getActionLayer(self):                                             # returns the action layer
    return self.actionLayer
    
  def setActionLayer(self, actionLayer):                                # sets the action layer
    self.actionLayer = actionLayer

  def updateActionLayer(self, previousPropositionLayer):
    """ 
    Updates the action layer given the previous proposition layer (see propositionLayer.py)
    You should add an action to the layer if its preconditions are in the previous propositions layer,
    and the preconditions are not pairwise mutex. 
    allAction is the set of all the action (include noOp) in the domain
    You might want to use those functions:
    previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
    previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
    self.actionLayer.addAction(action) adds action to the current action layer
    """ 
    allActions = PlanGraphLevel.actions
    for action in allActions:
      if previousPropositionLayer.allPrecondsInLayer(action):
        pre = action.getPre()
        add = True
        for prop1 in pre:
          for prop2 in pre:
            if prop1 != prop2 and previousPropositionLayer.isMutex(prop1, prop2):
              add = False
              break
          if not add:
            break
        if add:
          self.actionLayer.addAction(action)

              


    
  def updateMutexActions(self, previousLayerMutexProposition):
    """
    Updates the mutex set in self.actionLayer,
    given the mutex proposition from the previous layer.
    currentLayerActions are the actions in the current action layer
    You might want to use this function:
    self.actionLayer.addMutexActions(action1, action2)
    adds the pair (action1, action2) to the mutex set in the current action layer
    Note that action is *not* mutex with itself
    """
    currentLayerActions = self.actionLayer.getActions()
    for a1 in  currentLayerActions:
      for a2 in currentLayerActions:
        if(a1 == a2):
          continue
        elif haveCompetingNeeds(a1, a2, previousLayerMutexProposition):
           self.actionLayer.addMutexActions(a1, a2)
    
    
  def updatePropositionLayer(self):
    """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    Note that same proposition in different layers might have different producers lists,
    hence you should create two different instances.
    currentLayerActions is the set of all the actions in the current layer.
    You might want to use those functions:
    dict() creates a new dictionary that might help to keep track on the propositions that you've
           already added to the layer
    self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer       
    
    """
    currentLayerActions = self.actionLayer.getActions()
    propostions = {}
    for action in currentLayerActions:
      for prop in action.getAdd():
        if not prop.getName() in propostions:
          newProp = Proposition(prop.getName())
          propostions[prop.getName()] = newProp

        propostions[prop.getName()].addProducer(action)

    for prop in propostions:
      self.propositionLayer.addProposition(propostions[prop])
    
  def updateMutexProposition(self):
    """
    updates the mutex propositions in the current proposition layer
    You might want to use those functions:
    mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
    self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex set of the current layer
    """
    currentLayerPropositions = self.propositionLayer.getPropositions()
    currentLayerMutexActions =  self.actionLayer.getMutexActions()

    for prop1 in currentLayerPropositions:
      for prop2 in currentLayerPropositions:
        if prop1 == prop2:
          continue
        elif mutexPropositions(prop1, prop2, currentLayerMutexActions):
          self.propositionLayer.addMutexProp(prop1, prop2)


  def expand(self, previousLayer):
    """
    Your algorithm should work as follows:
    First, given the propositions and the list of mutex propositions from the previous layer,
    set the actions in the action layer. 
    Then, set the mutex action in the action layer.
    Finally, given all the actions in the current layer,
    set the propositions and their mutex relations in the proposition layer.   
    """
    previousPropositionLayer = previousLayer.getPropositionLayer()
    previousLayerMutexProposition = previousPropositionLayer.getMutexProps()
    self.updateActionLayer(previousPropositionLayer)
    self.updateMutexActions(previousLayerMutexProposition)
    self.updatePropositionLayer()
    self.updateMutexProposition()
            
  def expandWithoutMutex(self, previousLayer):
    """
    Questions 11 and 12
    You don't have to use this function
    """
    previousLayerProposition = previousLayer.getPropositionLayer()
    self.updateActionLayer(previousLayerProposition)
    self.updatePropositionLayer()
예제 #41
0
class PlanGraphLevel(object):
    """
  A class for representing a level in the plan graph.
  For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
  """
    independentActions = [
    ]  # updated to the independentActions of the propblem GraphPlan.py line 31
    actions = [
    ]  # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
    props = [
    ]  # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26

    @staticmethod
    def setIndependentActions(independentActions):
        PlanGraphLevel.independentActions = independentActions

    @staticmethod
    def setActions(actions):
        PlanGraphLevel.actions = actions

    @staticmethod
    def setProps(props):
        PlanGraphLevel.props = props

    def __init__(self):
        """
    Constructor
    """
        self.actionLayer = ActionLayer()  # see actionLayer.py
        self.propositionLayer = PropositionLayer()  # see propositionLayer.py

    def getPropositionLayer(self):
        # returns the proposition layer
        return self.propositionLayer

    def setPropositionLayer(self, propLayer):
        # sets the proposition layer
        self.propositionLayer = propLayer

    def getActionLayer(self):
        # returns the action layer
        return self.actionLayer

    def setActionLayer(self, actionLayer):
        # sets the action layer
        self.actionLayer = actionLayer

    def updateActionLayer(self, previousPropositionLayer):
        """ 
    Updates the action layer given the previous proposition layer (see propositionLayer.py)
    You should add an action to the layer if its preconditions are in the previous propositions layer,
    and the preconditions are not pairwise mutex. 
    allAction is the list of all the action (include noOp) in the domain
    You might want to use those functions:
    previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
    previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
    self.actionLayer.addAction(action) adds action to the current action layer
    """
        allActions = PlanGraphLevel.actions
        for action in allActions:
            if previousPropositionLayer.allPrecondsInLayer(
                    action) and action not in self.actionLayer.getActions():
                self.actionLayer.addAction(action)

    def updateMutexActions(self, previousLayerMutexProposition):
        """
    Updates the mutex list in self.actionLayer,
    given the mutex proposition from the previous layer.
    currentLayerActions are the actions in the current action layer
    You might want to use this function:
    self.actionLayer.addMutexActions(action1, action2)
    adds the pair (action1, action2) to the mutex list in the current action layer
    Note that action is *not* mutex with itself
    """
        currentLayerActions = self.actionLayer.getActions()
        for a1 in currentLayerActions:
            for a2 in currentLayerActions:
                if a1 == a2:
                    continue  # an action is not mutex with itself

                if Pair(a1, a2) not in self.actionLayer.getMutexActions(
                ) and mutexActions(a1, a2, previousLayerMutexProposition):
                    # if a1 and a2 are mutex actions, and we didn't add them before, we should do so now
                    self.actionLayer.addMutexActions(a1, a2)

    def updatePropositionLayer(self):
        """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    Note that same proposition in different layers might have different producers lists,
    hence you should create two different instances.
    currentLayerActions is the list of all the actions in the current layer.
    You might want to use those functions:
    dict() creates a new dictionary that might help to keep track on the propositions that you've
           already added to the layer
    self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer       
    
    """
        currentLayerActions = self.actionLayer.getActions()
        for action in currentLayerActions:
            for p in action.getAdd():
                # add the proposition if it doesn't already exist
                if p not in self.propositionLayer.getPropositions():
                    self.propositionLayer.addProposition(p)

                # add the action as a producer if it doesn't already exist
                if action not in p.getProducers():
                    p.addProducer(action)

    def updateMutexProposition(self):
        """
    updates the mutex propositions in the current proposition layer
    You might want to use those functions:
    mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
    self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer
    """
        currentLayerPropositions = self.propositionLayer.getPropositions()
        currentLayerMutexActions = self.actionLayer.getMutexActions()
        for p in currentLayerPropositions:
            for q in currentLayerPropositions:
                if p == q:
                    continue  # a proposition is not mutex with itself

                if Pair(p, q) not in self.propositionLayer.getMutexProps(
                ) and mutexPropositions(p, q, currentLayerMutexActions):
                    # if p and q are mutex props, and we didn't add them before, we should do so now
                    self.propositionLayer.addMutexProp(p, q)

    def expand(self, previousLayer):
        """
    Your algorithm should work as follows:
    First, given the propositions and the list of mutex propositions from the previous layer,
    set the actions in the action layer. 
    Then, set the mutex action in the action layer.
    Finally, given all the actions in the current layer,
    set the propositions and their mutex relations in the proposition layer.   
    """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        previousLayerMutexProposition = previousPropositionLayer.getMutexProps(
        )

        self.updateActionLayer(previousPropositionLayer)
        self.updateMutexActions(previousLayerMutexProposition)
        self.updatePropositionLayer()
        self.updateMutexProposition()

    def expandWithoutMutex(self, previousLayer):
        """
    Questions 11 and 12
    You don't have to use this function
    """
        previousLayerProposition = previousLayer.getPropositionLayer()
        self.updateActionLayer(previousLayerProposition)
        self.updatePropositionLayer()
예제 #42
0
class PlanGraphLevel(object):
  """
  A class for representing a level in the plan graph.
  For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
  """
  independentActions = []  # updated to the independentActions of the propblem GraphPlan.py line 31 
  actions = []             # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
  props = []               # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26
  
  @staticmethod
  def setIndependentActions(independentActions):
    PlanGraphLevel.independentActions = independentActions
  
  @staticmethod  
  def setActions(actions):
    PlanGraphLevel.actions = actions
  
  @staticmethod    
  def setProps(props):
    PlanGraphLevel.props = props   
  
  def __init__(self):
    """
    Constructor
    """
    self.actionLayer = ActionLayer()    		    # see actionLayer.py
    self.propositionLayer = PropositionLayer()	# see propositionLayer.py
  
  def getPropositionLayer(self):
  # returns the proposition layer
    return self.propositionLayer
  
  def setPropositionLayer(self, propLayer):
  # sets the proposition layer
    self.propositionLayer = propLayer  
  
  def getActionLayer(self):
  # returns the action layer
    return self.actionLayer
    
  def setActionLayer(self, actionLayer):
  # sets the action layer
    self.actionLayer = actionLayer

  def updateActionLayer(self, previousPropositionLayer):
    """ 
    Updates the action layer given the previous proposition layer (see propositionLayer.py)
    You should add an action to the layer if its preconditions are in the previous propositions layer,
    and the preconditions are not pairwise mutex. 
    allAction is the list of all the action (include noOp) in the domain
    You might want to use those functions:
    previousPropositionLayer.isMutex(prop1, prop2) returns true if prop1 and prop2 are mutex at the previous propositions layer
    previousPropositionLayer.allPrecondsInLayer(action) returns true if all the preconditions of action are in the previous propositions layer
    self.actionLayer.addAction(action) adds action to the current action layer
    """ 
    allActions = PlanGraphLevel.actions
    "*** YOUR CODE HERE ***"
    for action in allActions:
      #check the preconditions
      if previousPropositionLayer.allPrecondsInLayer(action):
        actionPropositions = action.getPre()
        #check the mutex
        isNotMutex = True
        for prop1 in actionPropositions:
          for prop2 in actionPropositions:
            if not prop1 == prop2:
              if previousPropositionLayer.isMutex(prop1, prop2):
                isNotMutex = False
        if isNotMutex:
          self.actionLayer.addAction(action)


    
  def updateMutexActions(self, previousLayerMutexProposition):
    """
    Updates the mutex list in self.actionLayer,
    given the mutex proposition from the previous layer.
    currentLayerActions are the actions in the current action layer
    You might want to use this function:
    self.actionLayer.addMutexActions(action1, action2)
    adds the pair (action1, action2) to the mutex list in the current action layer
    Note that action is *not* mutex with itself
    """
    currentLayerActions = self.actionLayer.getActions()
    "*** YOUR CODE HERE ***"
    for action1 in currentLayerActions:
      for action2 in currentLayerActions:
        if (not action1 == action2) and\
            (Pair(action1,action2) not in self.actionLayer.getMutexActions()):
          if mutexActions(action1, action2, previousLayerMutexProposition):
            self.actionLayer.addMutexActions(action1, action2)
    
  def updatePropositionLayer(self):
    """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    Note that same proposition in different layers might have different producers lists,
    hence you should create two different instances.
    currentLayerActions is the list of all the actions in the current layer.
    You might want to use those functions:
    dict() creates a new dictionary that might help to keep track on the propositions that you've
           already added to the layer
    self.propositionLayer.addProposition(prop) adds the proposition prop to the current layer       
    
    """
    currentLayerActions = self.actionLayer.getActions()
    "*** YOUR CODE HERE ***"
    # create dictionary with proposition and actions
    # leading to it
    propositionDict = dict()
    for action in currentLayerActions:
      for prop in action.getAdd():
        if prop.getName() in propositionDict.keys():
          propositionDict[prop.getName()].append(action)
        else:
          actionList = list()
          actionList.append(action)
          propositionDict.update({prop.getName(): actionList})

    #create proposition object and update their actions
    for key in propositionDict.keys():
      p = Proposition(key)
      p.setProducers(propositionDict[key])
      self.propositionLayer.addProposition(p)

    
  def updateMutexProposition(self):
    """
    updates the mutex propositions in the current proposition layer
    You might want to use those functions:
    mutexPropositions(prop1, prop2, currentLayerMutexActions) returns true if prop1 and prop2 are mutex in the current layer
    self.propositionLayer.addMutexProp(prop1, prop2) adds the pair (prop1, prop2) to the mutex list of the current layer
    """
    currentLayerPropositions = self.propositionLayer.getPropositions()
    currentLayerMutexActions =  self.actionLayer.getMutexActions()
    "*** YOUR CODE HERE ***"
    for prop1 in currentLayerPropositions:
      for prop2 in currentLayerPropositions:
        if not prop1 == prop2 and \
            (Pair(prop1, prop2) not in self.propositionLayer.getMutexProps()):
          if mutexPropositions(prop1, prop2, currentLayerMutexActions):
            self.propositionLayer.addMutexProp(prop1, prop2)

    
  def expand(self, previousLayer):
    """
    Your algorithm should work as follows:
    First, given the propositions and the list of mutex propositions from the previous layer,
    set the actions in the action layer. 
    Then, set the mutex action in the action layer.
    Finally, given all the actions in the current layer,
    set the propositions and their mutex relations in the proposition layer.   
    """
    previousPropositionLayer = previousLayer.getPropositionLayer()
    previousLayerMutexProposition = previousPropositionLayer.getMutexProps()

    "*** YOUR CODE HERE ***"
    self.updateActionLayer(previousPropositionLayer)
    self.updateMutexActions(previousLayerMutexProposition)
    self.updatePropositionLayer()
    self.updateMutexProposition()



  def expandWithoutMutex(self, previousLayer):
    """
    Questions 11 and 12
    You don't have to use this function
    """
    previousLayerProposition = previousLayer.getPropositionLayer()
    "*** YOUR CODE HERE ***"
    self.updateActionLayer(previousLayerProposition)
    self.updatePropositionLayer()
예제 #43
0
class PlanGraphLevel(object):
    """
  A class for representing a level in the plan graph.
  For each level i, the PlanGraphLevel consists of the actionLayer and propositionLayer at this level in this order!
  """
    independentActions = [
    ]  # updated to the independentActions of the propblem GraphPlan.py line 31
    actions = [
    ]  # updated to the actions of the problem GraphPlan.py line 32 and planningProblem.py line 25
    props = [
    ]  # updated to the propositions of the problem GraphPlan.py line 33 and planningProblem.py line 26

    @staticmethod
    def setIndependentActions(independentActions):
        PlanGraphLevel.independentActions = independentActions

    @staticmethod
    def setActions(actions):
        PlanGraphLevel.actions = actions

    @staticmethod
    def setProps(props):
        PlanGraphLevel.props = props

    def __init__(self):
        """
    Constructor
    """
        self.actionLayer = ActionLayer()  # see actionLayer.py
        self.propositionLayer = PropositionLayer()  # see propositionLayer.py

    def getPropositionLayer(self):
        return self.propositionLayer

    def setPropositionLayer(self, propLayer):
        self.propositionLayer = propLayer

    def getActionLayer(self):
        return self.actionLayer

    def setActionLayer(self, actionLayer):
        self.actionLayer = actionLayer

    def updateActionLayer(self, previousPropositionLayer):
        """ 
    Updates the action layer given the previous proposition layer (see propositionLayer.py)
    allAction is the list of all the actions (include noOp in the domain)
    """
        allActions = PlanGraphLevel.actions
        for a in allActions:
            if previousPropositionLayer.allPrecondsInLayer(a):
                self.actionLayer.addAction(a)

    def updateMutexActions(self, previousLayerMutexProposition):
        """
    Updates the mutex list in self.actionLayer,
    given the mutex proposition from the previous layer.
    currentLayerActions are the actions in the current action layer
    """
        currentLayerActions = self.actionLayer.getActions()
        for a_i in currentLayerActions:
            for a_j in currentLayerActions:
                if a_i != a_j and mutexActions(a_i, a_j,
                                               previousLayerMutexProposition):
                    if Pair(a_i, a_j) not in self.actionLayer.mutexActions:
                        self.actionLayer.addMutexActions(a_i, a_j)

    def updatePropositionLayer(self):
        """
    Updates the propositions in the current proposition layer,
    given the current action layer.
    don't forget to update the producers list!
    """
        currentLayerActions = self.actionLayer.getActions()
        for a in currentLayerActions:
            for p in a.getAdd():
                if p not in self.propositionLayer.getPropositions():
                    self.propositionLayer.addProposition(p)
                p.addProducer(a)

    def updateMutexProposition(self):
        """
    updates the mutex propositions in the current proposition layer
    """
        currentLayerPropositions = self.propositionLayer.getPropositions()
        currentLayerMutexActions = self.actionLayer.getMutexActions()
        for p_i in currentLayerPropositions:
            for p_j in currentLayerPropositions:
                if p_i != p_j and mutexPropositions(p_i, p_j,
                                                    currentLayerMutexActions):
                    if Pair(p_i, p_j
                            ) not in self.propositionLayer.mutexPropositions:
                        self.propositionLayer.mutexPropositions.append(
                            Pair(p_i, p_j))

    def expand(self, previousLayer):
        """
    Your algorithm should work as follows:
    First, given the propositions and the list of mutex propositions from the previous layer,
    set the actions in the action layer. 
    Then, set the mutex action in the action layer.
    Finally, given all the actions in the current layer, set the propositions and their mutex relations in the proposition layer.   
    """
        previousPropositionLayer = previousLayer.getPropositionLayer()
        previousLayerMutexProposition = previousPropositionLayer.getMutexProps(
        )

        self.updateActionLayer(previousPropositionLayer)
        self.updateMutexActions(previousLayerMutexProposition)

        self.updatePropositionLayer()
        self.updateMutexProposition()

    def expandWithoutMutex(self, previousLayer):
        """
    Questions 11 and 12
    You don't have to use this function
    """
        previousLayerProposition = previousLayer.getPropositionLayer()
        self.updateActionLayer(previousLayerProposition)
        self.updatePropositionLayer()