Fix search algorithm and finish project.

This commit is contained in:
Felix Martin 2021-10-23 18:38:08 -04:00
parent 7e64e723eb
commit fd8dd8ae35
2 changed files with 48 additions and 17 deletions

View File

@ -80,19 +80,23 @@ def genericSearch(problem, getNewCostAndPriority):
fringe.push((startState, [], 0), 0)
visited = {}
while not fringe.isEmpty():
while True:
if fringe.isEmpty():
raise Exception("No path found.")
state, actions, cost = fringe.pop()
if problem.isGoalState(state):
return actions
if state in visited and cost >= visited[state]:
continue
visited[state] = cost
for successor, action, stepCost in problem.getSuccessors(state):
newCost, priority = getNewCostAndPriority(cost, stepCost, successor)
if successor in visited and visited[successor] <= newCost:
continue
newActions = list(actions) + [action]
fringe.push((successor, newActions, newCost), priority)
print("No path found.")
raise Exception()
def depthFirstSearch(problem):

View File

@ -394,11 +394,24 @@ def cornersHeuristic(state, problem):
admissible (as well as consistent).
"""
corners = problem.corners # These are the corner coordinates
# These are the walls of the maze, as a Grid (game.py)
walls = problem.walls
position, visitedCorners = state
"*** YOUR CODE HERE ***"
return 0 # Default to trivial solution
# self.corners = ((1, 1), (1, top), (right, 1), (right, top))
minDist = min(corners[2][0] - 1, corners[1][1] - 1)
# Okay, I am having a way harder time with this than I should.
# First, get only the corners Pacman hasn't visited yet.
distToCorners = [util.manhattanDistance(position, corner)
for corner, visited in zip(corners, visitedCorners)
if visited == 0]
# If there are no corners left, we are done.
if not distToCorners:
return 0
distanceClosestCorner = min(distToCorners)
cost = distanceClosestCorner + (len(distToCorners) - 1) * minDist
return cost
class AStarCornersAgent(SearchAgent):
@ -501,8 +514,24 @@ def foodHeuristic(state, problem):
problem.heuristicInfo['wallCount']
"""
position, foodGrid = state
"*** YOUR CODE HERE ***"
return 0
foodPositions = foodGrid.asList()
if not foodPositions:
return 0
# We have to travel at least from x_min to x_max and y_min to y_max.
foodX = [x for (x, y) in foodPositions]
foodY = [y for (x, y) in foodPositions]
cost = (max(foodX) - min(foodX)) + (max(foodY) - min(foodY))
# The previous gave over 9000 for trickySearch. We can improve by adding
# the distance to the closest food position which gives over 7000 points.
cost += min([util.manhattanDistance(position, foodPosition)
for foodPosition in foodPositions])
# If I wanted to get full score, I would use the cost to the closest food,
# plus a TSP from there. That would give us less than 7000 for sure.
return cost
class ClosestDotSearchAgent(SearchAgent):
@ -534,9 +563,7 @@ class ClosestDotSearchAgent(SearchAgent):
food = gameState.getFood()
walls = gameState.getWalls()
problem = AnyFoodSearchProblem(gameState)
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
return search.ucs(problem)
class AnyFoodSearchProblem(PositionSearchProblem):
@ -571,9 +598,9 @@ class AnyFoodSearchProblem(PositionSearchProblem):
complete the problem definition.
"""
x, y = state
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
if (x, y) in self.food.asList():
return True
return False
def mazeDistance(point1, point2, gameState):