Exemple #1
0
def foodHeuristic(state, problem):
    """
    Your heuristic for the FoodSearchProblem goes here.

    This heuristic must be consistent to ensure correctness.  First, try to come up
    with an admissible heuristic; almost all admissible heuristics will be consistent
    as well.

    If using A* ever finds a solution that is worse uniform cost search finds,
    your heuristic is *not* consistent, and probably not admissible!  On the other hand,
    inadmissible or inconsistent heuristics may find optimal solutions, so be careful.

    The state is a tuple ( pacmanPosition, foodGrid ) where foodGrid is a
    Grid (see game.py) of either True or False. You can call foodGrid.asList()
    to get a list of food coordinates instead.

    If you want access to info like walls, capsules, etc., you can query the problem.
    For example, problem.walls gives you a Grid of where the walls are.

    If you want to *store* information to be reused in other calls to the heuristic,
    there is a dictionary called problem.heuristicInfo that you can use. For example,
    if you only want to count the walls once and store that value, try:
      problem.heuristicInfo['wallCount'] = problem.walls.count()
    Subsequent calls to this heuristic can access problem.heuristicInfo['wallCount']
    """
    position, foodGrid = state
    "*** YOUR CODE HERE ***"
    h=0.0
    x,y=position
    food_state=foodGrid.data
    walls=problem.walls
    nrows=len(food_state)
    ncols=len(food_state[0])
    graph=util.PriorityQueue()
    pacman_distances=[]
    food_position=[]
    for i in xrange(nrows):
        for j in xrange(ncols):
            if food_state[i][j]==True:
                food_position.append((i,j))
                manhattan=abs(x-i)+abs(y-j)
                distance=manhattan
                pacman_distances.append(distance)
    food_count=len(food_position)
    for i in xrange(food_count):
        for j in xrange(i+1,food_count):
            x1,y1=food_position[i]
            x2,y2=food_position[j]
            manhattan=abs(x1-x2)+abs(y1-y2)
            distance=manhattan
            graph.push(((x1,y1),(x2,y2),distance),distance)
    mst_weight=mst.getWeightFromMST(graph,food_count)
    if pacman_distances:
        h+=min(pacman_distances)
        h+=mst_weight
    return h
Exemple #2
0
def cornersHeuristic(state, problem):
    """
    A heuristic for the CornersProblem that you defined.

      state:   The current search state
               (a data structure you chose in your search problem)

      problem: The CornersProblem instance for this layout.

    This function should always return a number that is a lower bound
    on the shortest path from the state to a goal of the problem; i.e.
    it should be admissible (as well as consistent).
    """
    "*** YOUR CODE HERE ***"
    h=0
    corners = problem.corners # These are the corner coordinates
    walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
    graph=util.PriorityQueue()
    pacman_distances=[]
    position,food_state=state
    x,y=position
    food_position=[]
    for i in xrange(len(food_state)):
        if food_state[i]=="0":
            food_position.append(corners[i])
            food_x,food_y=corners[i]
            manhattan=abs(x-food_x)+abs(y-food_y)
            distance=manhattan
            pacman_distances.append(distance)

    food_count=len(food_position)
    for i in xrange(food_count):
        for j in xrange(i+1,food_count):
            x1,y1=food_position[i]
            x2,y2=food_position[j]
            #manhattan=abs(x1-x2)+abs(y1-y2)
            distance=manhattan
            graph.push(((x1,y1),(x2,y2),distance),distance)
    mst_weight=mst.getWeightFromMST(graph,food_count)
    if pacman_distances:
        h+=min(pacman_distances)
        h+=mst_weight
    return h