Fix search algorithm and finish project.

This commit is contained in:
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) fringe.push((startState, [], 0), 0)
visited = {} visited = {}
while not fringe.isEmpty(): while True:
if fringe.isEmpty():
raise Exception("No path found.")
state, actions, cost = fringe.pop() state, actions, cost = fringe.pop()
if problem.isGoalState(state): if problem.isGoalState(state):
return actions return actions
if state in visited and cost >= visited[state]:
continue
visited[state] = cost visited[state] = cost
for successor, action, stepCost in problem.getSuccessors(state): for successor, action, stepCost in problem.getSuccessors(state):
newCost, priority = getNewCostAndPriority(cost, stepCost, successor) newCost, priority = getNewCostAndPriority(cost, stepCost, successor)
if successor in visited and visited[successor] <= newCost:
continue
newActions = list(actions) + [action] newActions = list(actions) + [action]
fringe.push((successor, newActions, newCost), priority) fringe.push((successor, newActions, newCost), priority)
print("No path found.")
raise Exception()
def depthFirstSearch(problem): def depthFirstSearch(problem):

View File

@@ -394,11 +394,24 @@ def cornersHeuristic(state, problem):
admissible (as well as consistent). admissible (as well as consistent).
""" """
corners = problem.corners # These are the corner coordinates corners = problem.corners # These are the corner coordinates
# These are the walls of the maze, as a Grid (game.py) position, visitedCorners = state
walls = problem.walls
"*** YOUR CODE HERE ***" # self.corners = ((1, 1), (1, top), (right, 1), (right, top))
return 0 # Default to trivial solution 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): class AStarCornersAgent(SearchAgent):
@@ -501,9 +514,25 @@ def foodHeuristic(state, problem):
problem.heuristicInfo['wallCount'] problem.heuristicInfo['wallCount']
""" """
position, foodGrid = state position, foodGrid = state
"*** YOUR CODE HERE ***" foodPositions = foodGrid.asList()
if not foodPositions:
return 0 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): class ClosestDotSearchAgent(SearchAgent):
"Search for all food using a sequence of searches" "Search for all food using a sequence of searches"
@@ -534,9 +563,7 @@ class ClosestDotSearchAgent(SearchAgent):
food = gameState.getFood() food = gameState.getFood()
walls = gameState.getWalls() walls = gameState.getWalls()
problem = AnyFoodSearchProblem(gameState) problem = AnyFoodSearchProblem(gameState)
return search.ucs(problem)
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
class AnyFoodSearchProblem(PositionSearchProblem): class AnyFoodSearchProblem(PositionSearchProblem):
@@ -571,9 +598,9 @@ class AnyFoodSearchProblem(PositionSearchProblem):
complete the problem definition. complete the problem definition.
""" """
x, y = state x, y = state
if (x, y) in self.food.asList():
"*** YOUR CODE HERE ***" return True
util.raiseNotDefined() return False
def mazeDistance(point1, point2, gameState): def mazeDistance(point1, point2, gameState):