Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. def maxLevel(state, problem):
  2.   """
  3.  The heuristic value is the number of layers required to expand all goal propositions.
  4.  If the goal is not reachable from the state your heuristic should return float('inf')  
  5.  A good place to start would be:
  6.  propLayerInit = PropositionLayer()          #create a new proposition layer
  7.  for prop in state:
  8.    propLayerInit.addProposition(prop)        #update the proposition layer with the propositions of the state
  9.  pgInit = PlanGraphLevel()                   #create a new plan graph level (level is the action layer and the propositions layer)
  10.  pgInit.setPropositionLayer(propLayerInit)   #update the new plan graph level with the the proposition layer
  11.  """
  12.  
  13.   propLayerInit = PropositionLayer()
  14.   for p in state:
  15.     propLayerInit.addProposition(p)    
  16.  
  17.   pgInit = PlanGraphLevel()                  
  18.   pgInit.setPropositionLayer(propLayerInit)
  19.  
  20.   graph = []  # list of PlanGraphLevel objects
  21.   graph.append(pgInit)
  22.   level = 0
  23.  
  24.   # keep expanding as long as we don't hit the goal state
  25.   while problem.goalStateNotInPropLayer(graph[level].getPropositionLayer().getPropositions()):
  26.     if isFixed(graph, level):
  27.       # if the graph is fixed and expansions didn't change in the last level, it means that we can't reach
  28.       # the goal state, and we return infinity
  29.       return float('inf')
  30.  
  31.     pg = PlanGraphLevel()
  32.     # expanding using a easier version of the problem - without mutexes
  33.     pg.expandWithoutMutex(graph[level])
  34.     graph.append(pg)
  35.     level += 1
  36.  
  37.   return level
  38.  
  39.  
  40.  
  41.  
  42. def levelSum(state, problem):
  43.   """
  44.  The heuristic value is the sum of sub-goals level they first appeared.
  45.  If the goal is not reachable from the state your heuristic should return float('inf')
  46.  """
  47.   propLayerInit = PropositionLayer()
  48.   for p in state:
  49.     propLayerInit.addProposition(p)    
  50.  
  51.   pgInit = PlanGraphLevel()                  
  52.   pgInit.setPropositionLayer(propLayerInit)
  53.  
  54.   graph = []  # list of PlanGraphLevel objects
  55.   graph.append(pgInit)
  56.   goals = problem.goal[:]
  57.   level = 0
  58.   sum_ = 0
  59.  
  60.   # keep expanding as long as we still have goal states we didn't see
  61.   while goals:
  62.     if isFixed(graph, level):
  63.       # if the graph is fixed and expansions didn't change in the last level, it means that we can't reach
  64.       # the goal state, and we return infinity
  65.       return float('inf')
  66.  
  67.     props = graph[level].getPropositionLayer().getPropositions()
  68.     for goal in goals:
  69.       if goal in props:
  70.         # each goal state that we run into, we should add to the sum, and remove it from the goals we need to see
  71.         sum_ += level
  72.         goals.remove(goal)
  73.  
  74.     pg = PlanGraphLevel()
  75.     # expanding using a easier version of the problem - without mutexes
  76.     pg.expandWithoutMutex(graph[level])
  77.     graph.append(pg)
  78.     level += 1
  79.  
  80.   sum_ += level
  81.  
  82.   return sum_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement