import pacman from ex03 import astar_search from ex04 import CornersSearchProblem 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). """ corners = problem.corners # These are the corner coordinates walls = problem.walls # These are the walls of the maze, as a Grid (game.py) "*** YOUR CODE HERE ***" return 0 if __name__ == '__main__': pacman.run(astar_search, problem=CornersSearchProblem, heuristic=cornersHeuristic)
import pacman def my_first_plan(problem): """ Returns a sequence of moves that solves tinyMaze. For any other maze, the sequence of moves will be incorrect, so only use this for tinyMaze. """ n = 'North' s = 'South' w = 'West' e = 'East' # *** YOUR CODE HERE *** # return [] if __name__ == '__main__': pacman.run(my_first_plan)
inherited), but has a different goal test, which you need to fill in below. The state space and successor function do not need to be changed. Look at ClosestDotSearchAgent in agents.py to understand how to implement the goal test. """ def __init__(self, gameState, costFn=lambda x: 1): """Stores information from the gameState. You don't need to change this.""" # Store the food for later reference self.food = gameState.getFood() # Store info for the PositionSearchProblem (no need to change this) self.walls = gameState.getWalls() self.startState = gameState.getPacmanPosition() self.costFn = costFn self._visited, self._visitedlist, self._expanded = {}, [], 0 # DO NOT CHANGE def isGoalState(self, state): """ The state is Pacman's position. Fill this in with a goal test that will complete the problem definition. Hint: A goal state is any state resulting in some food being eaten. """ x, y = state "*** YOUR CODE HERE ***" util.raiseNotDefined() if __name__ == '__main__': pacman.run(astar_search, problem=AnyFoodSearchProblem, agent=ClosestDotSearchAgent)
def astar_search(problem, heuristic): """ Search the node that has the lowest total cost (past + future) first. The heuristic function estimates the cost from the current state to the nearest goal in the provided SearchProblem. It takes two inputs: a state and a problem. >>> heuristic(state, problem) 11.2 These are the functions to interact with the Pacman world: >>> state = problem.getStartState() >>> state (5, 5) >>> problem.getSuccessors(state) [((5, 4), 'South', 1), ((4, 5), 'West', 1)] >>> problem.isGoalState(state) False """ # *** YOUR CODE HERE *** # return [] if __name__ == '__main__': pacman.run(astar_search)
def breadth_first_search(problem): """ Search the shallowest nodes in the search tree first. These are the functions to interact with the Pacman world: >>> state = problem.getStartState() >>> state (5, 5) >>> problem.getSuccessors(state) [((5, 4), 'South', 1), ((4, 5), 'West', 1)] >>> problem.isGoalState(state) False """ # *** YOUR CODE HERE *** # return [] def path_reconstruction(start, goal, explored): # *** YOUR CODE HERE *** # return [] if __name__ == '__main__': pacman.run(breadth_first_search)
import pacman from pacman.util import PriorityQueue from ex01 import path_reconstruction def uniform_cost_search(problem): """ Search the node of least total cost first. These are the functions to interact with the Pacman world: >>> state = problem.getStartState() >>> state (5, 5) >>> problem.getSuccessors(state) [((5, 4), 'South', 1), ((4, 5), 'West', 1)] >>> problem.isGoalState(state) False """ # *** YOUR CODE HERE *** # return [] if __name__ == '__main__': pacman.run(uniform_cost_search)
Directions.WEST ]: # Add a successor state to the successor list if the action is legal # Here's a code snippet for figuring out whether a new position hits a wall: # x,y = currentPosition # dx, dy = Actions.directionToVector(action) # nextx, nexty = int(x + dx), int(y + dy) # hitsWall = self.walls[nextx][nexty] "*** YOUR CODE HERE ***" self._expanded += 1 # DO NOT CHANGE return successors def getCostOfActions(self, actions): """ Returns the cost of a particular sequence of actions. If those actions include an illegal move, return 999999. This is implemented for you. """ if actions == None: return 999999 x, y = self.startingPosition for action in actions: dx, dy = Actions.directionToVector(action) x, y = int(x + dx), int(y + dy) if self.walls[x][y]: return 999999 return len(actions) if __name__ == '__main__': pacman.run(breadth_first_search, problem=CornersSearchProblem)