Ejemplo n.º 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
  """
  "*** 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
Ejemplo n.º 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')
  """
  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
Ejemplo n.º 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 ***"

    # explain: implement max level heuristic
    #   expand the graph with out mutexs until the goal is reached,
    #   the heuristic value is the number of levels need to reach the goal

    pg = state

    Graph = []
    Graph.append(pg)
    Level = 0

    pgNext = pg
    isRepeat = False
    isGoal = False
    while not isGoal and not isRepeat:

        # expand next level without mutexs
        pgPrev = pgNext
        pgNext = PlanGraphLevel()
        pgNext.expandWithoutMutex(pgPrev)

        Graph.append(pgNext)
        Level += 1

        # check if level expansion is in 'levels-off' state
        #  if isFixed() function return true' check if the current level was aread reached in a previous graph history
        #  if so, we are in a loop state and the heuristic value should be 'inf'

        if isFixed(Graph, Level):

            pgNextPropositions = pgNext.getPropositionLayer().getPropositions()
            isRepeat = True
            for Hist in range(Level - 1):
                HistLevelPropositions = Graph[Hist].getPropositionLayer(
                ).getPropositions()
                for prop in pgNextPropositions:
                    if prop not in HistLevelPropositions:
                        isRepeat = False

        isGoal = not problem.goalStateNotInPropLayer(
            pgNext.propositionLayer.propositions)

    h = Level
    if isRepeat and not isGoal:
        h = float('inf')

    return h
Ejemplo n.º 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')
    """
    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())
Ejemplo n.º 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')
    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
Ejemplo n.º 6
0
 def nextPlan(plan):
     next_plan = PlanGraphLevel()
     next_plan.expandWithoutMutex(plan)
     return next_plan, next_plan.getPropositionLayer().getPropositions()
Ejemplo n.º 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')
  """
    "*** YOUR CODE HERE ***"

    # explain: implement max level heuristic
    #   expand the graph with out mutexs until the goal is reached,
    #   the heuristic value is the sum of the levels reached for each goal proposition

    pg = state

    Graph = []
    Graph.append(pg)
    Level = 0
    Sum = 0
    goal = copy.deepcopy(problem.goal)

    pgNext = pg
    isRepeat = False
    isGoal = False
    while not isGoal and not isRepeat:

        # expand next level without mutexs
        pgPrev = pgNext
        pgNext = PlanGraphLevel()
        pgNext.expandWithoutMutex(pgPrev)

        Graph.append(pgNext)
        Level += 1

        # check if level expansion is in 'levels-off' state
        #  if isFixed() function return true' check if the current level was aread reached in a previous graph history
        #  if so, we are in a loop state and the heuristic value should be 'inf'

        if isFixed(Graph, Level):
            pgNextPropositions = pgNext.getPropositionLayer().getPropositions()
            isRepeat = True
            for Hist in range(Level - 1):
                HistLevelPropositions = Graph[Hist].getPropositionLayer(
                ).getPropositions()
                for prop in pgNextPropositions:
                    if prop not in HistLevelPropositions:
                        isRepeat = False

        to_delete = []
        for prop in goal:
            if prop in pgNext.propositionLayer.propositions:
                # add each goal level, and delete it from goal list
                Sum += Level
                to_delete.append(prop)
        for prop in to_delete:
            goal.remove(prop)
        isGoal = len(goal) == 0

    h = Sum
    if isRepeat and not isGoal:
        h = float('inf')

    return h