def foodWithin2(self, state, currentDirection, legalMoves): cur = api.whereAmI(state) #north ##can see food within 2 units if (cur[0], cur[1] + 1) in api.food(state) or (cur[0], cur[1] + 2) in api.food(state): print "n" return Directions.NORTH #east ##can see food within 2 units if (cur[0] + 1, cur[1]) in api.food(state) or (cur[0] + 2, cur[1]) in api.food(state): print "e" return Directions.EAST #south ##can see food within 2 units if (cur[0], cur[1] - 1) in api.food(state) or (cur[0], cur[1] - 2) in api.food(state): print "s" return Directions.SOUTH #west ##can see food within 2 units if (cur[0] - 1, cur[1]) in api.food(state) or (cur[0] + 2, cur[1]) in api.food(state): print "w" return Directions.WEST legalMoves.remove(Directions.STOP) return random.choice(legalMoves)
def getAction(self, state): print "-" * 30 #divider ghosts = api.ghosts(state) #get state of ghosts legal = state.getLegalPacmanActions() #Again, get a list of pacman's legal actions last = state.getPacmanState().configuration.direction #store last move pacman = api.whereAmI(state) #retrieve location of pacman food = api.food(state) #retrieve location of food walls = api.walls(state) #how to call getfoodvalmap method. #In reality, the reward should be the final value-iteration of the grid. foodVal = self.getValueMap(state, 10) print foodVal #example on how to use getPacMEU function currentUtil = self.getPacMEU(pacman[0], pacman[1], foodVal, legal) print "Utility values: " print currentUtil print max(currentUtil.values()) #example on how to use getMEU function foodUtil = self.getMEU((18, 3), foodVal, walls) print "max utility for (18, 3) is: " print foodUtil if Directions.STOP in legal: legal.remove(Directions.STOP) # Random choice between the legal options. return api.makeMove(random.choice(legal), legal) """
def getAction(self, state): #current possible moves legal = state.getLegalPacmanActions() #get current position pacman = api.whereAmI(state) pacmanX = pacman[0] pacmanY = pacman[1] #get food locations food = api.food(state) foodLoc = [] # get Distance for loc in food: foodLoc.append((abs(loc[0]-pacmanX + loc[1]-pacmanY),(loc[0]-pacmanX, loc[1]-pacmanY))) print foodLoc #Prevent it from stopping if Directions.STOP in legal: legal.remove(Directions.STOP) pick = random.choice(legal) return api.makeMove(pick, legal)
def registerInitialState(self, state): global numoffood numoffood = len(api.food(state)) self.makeMap(state) self.addWallsFoodToMap(state)
def getAction(self, state): legal = api.legalActions(state) if Directions.STOP in legal: legal.remove(Directions.STOP) target = (1, 1) print target print "Food locations: " print len(api.food(state)) pacman = api.whereAmI(state) print "Pacman position: ", pacman if self.backsteps == 0: if pacman[0] >= target[0]: if Directions.WEST in legal: return api.makeMove(Directions.WEST, legal) else: if Directions.EAST in legal: return api.makeMove(Directions.EAST, legal) if pacman[1] >= target[1]: if Directions.SOUTH in legal: return api.makeMove(Directions.SOUTH, legal) else: if Directions.NORTH in legal: return api.makeMove(Directions.NORTH, legal) self.backsteps = 2 self.backstep_direction = random.choice(legal) self.backsteps -= 1 return api.makeMove(self.backstep_direction, legal)
def valueIterationSmall(self, state, reward, gamma, V1): # Similar to valueIteration function # does not calculate buffers around ghosts (cause it would be too small) # meant for maps smaller than 10 x 10 corners = api.corners(state) walls = api.walls(state) food = api.food(state) ghosts = api.ghosts(state) capsules = api.capsules(state) maxWidth = self.getLayoutWidth(corners) - 1 maxHeight = self.getLayoutHeight(corners) - 1 if not (0 < gamma <= 1): raise ValueError("MDP must have a gamma between 0 and 1.") # Implement Bellman equation with 10-loop iteration # Since smaller maps do not require as big of a value iteration loop loops = 100 while loops > 0: V = V1.copy() # This will store the old values for i in range(maxWidth): for j in range(maxHeight): # Exclude any food because in this case it is the terminal state if (i, j) not in walls and (i, j) not in food and ( i, j) not in ghosts and (i, j) not in capsules: V1[(i, j)] = reward + gamma * self.getTransition(i, j, V) loops -= 1
def food_update(self, state): """ Update food to the map """ foods = api.food(state) for food in foods: self.map.set_value(food[0], food[1], self.food_reward)
def registerInitialState(self, state): self.initial_num_food = len(api.food(state)) self.corners = api.corners(state) self.width = max(self.corners)[0] + 1 # max x coordinate + 1 self.height = max(self.corners, key=itemgetter(1))[1] + 1 # max y coordinate + 1 self.walls = api.walls(state)
def getAction(self,state): walls = api.walls(state) width,height = api.corners(state)[-1] legal = api.legalActions(state) me = api.whereAmI(state) food = api.food(state) ghosts = api.ghosts(state) capsules = api.capsules(state) direction = Directions.STOP x, y = me if not hasattr(self, 'map'): self.createMap(walls, width + 1, height + 1) self.checkForCapsules(capsules, legal, ghosts) legal = self.solveLoop(ghosts, legal) if len(ghosts): self.memorizeGhosts(ghosts) if self.counter < 0: for ghost in ghosts: legal = self.checkForGhosts(ghost, me, legal) direction = self.pickMove(me, legal, width + 1, height + 1, food) self.updatePosition(me, 1, self.map) self.printMap(self.map) self.last = direction return direction
def initialize(self, state): # get location of all visible food foods = api.food(state) #get location of all corners corners = api.corners(state) #get location of all visible capsules capsules = api.capsules(state) # Get the actions we can try, and remove "STOP" if that is one of them. legal = api.legalActions(state) #get location of all visible walls walls = api.walls(state) #get pacmans position pacman = api.whereAmI(state) x = pacman[0] y = pacman[1] if self.map == None: width = 0 height = 0 for corner in corners: if corner[0] > width: width = corner[0] if corner[1] > height: height = corner[1] self.map = [["?" for y in range(height)] for x in range(width)] for wall in walls: self.map[wall[0]][wall[1]] = "W" for food in foods: self.map[food[0]][food[1]] = "F" for capsule in capsules: self.map[capsule[0]][capsule[1]] = "F" self.map[x][y] = "0" self.init = True
def generateRewardGrid(self, state): # a negative incentive for non-terminal states # this is an incentive for taking the shortest route initialValue = -5 # initialize 2d array with correct dimensions (w, h) = api.corners(state)[3] rewardGrid = [[initialValue for x in range(w + 1)] for y in range(h + 1)] ghosts = api.ghosts(state) foods = api.food(state) walls = api.walls(state) for (x, y) in foods: rewardGrid[y][x] = 100 # fill a radius around each ghost with negative reward # size of radius dependent on number of foods remaining # pacman feels no fear when almost winning radius = 5 if len(foods) > 3 else 2 for (x, y) in ghosts: self.floodFill(rewardGrid, int(x), int(y), radius) for (x, y) in walls: rewardGrid[y][x] = 0 return rewardGrid
def getAction(self, state): legal = api.legalActions(state) if Directions.STOP in legal: legal.remove(Directions.STOP) #SELECT TARGET target = api.food(state)[0] print target pacman = api.whereAmI(state) print "Pacman position: ", pacman if self.backsteps == 0: if pacman[0] >= target[0]: if Directions.WEST in legal: return api.makeMove(Directions.WEST, legal) else: if Directions.EAST in legal: return api.makeMove(Directions.EAST, legal) if pacman[1] >= target[1]: if Directions.SOUTH in legal: return api.makeMove(Directions.SOUTH, legal) else: if Directions.NORTH in legal: return api.makeMove(Directions.NORTH, legal) self.backsteps = 2 #IT REACHES HERE ONLY ONCE BOTH DIRECTIONS IT WANTS TO GO ARE ILLEGAL, SO: BACKSTEP 2 STOPS TOWARDS RANDOM LEGAL DIRECTION self.backstep_direction = random.choice(legal) self.backsteps -= 1 return api.makeMove(self.backstep_direction, legal)
def getAction(self, state): """ The function to work out next intended action carried out. Parameters: None Returns: Directions: Intended action that Pacman will carry out. """ current_pos = api.whereAmI(state) corners = api.corners(state) food = api.food(state) ghosts = api.ghosts(state) ghost_scared_time = api.ghostStatesWithTimes(state)[0][1] walls = api.walls(state) legal = api.legalActions(state) capsules = api.capsules(state) protected_coords = walls + ghosts + [current_pos] width = max(corners)[0] + 1 height = max(corners, key=itemgetter(1))[1] + 1 board = self.create_board(width, height, -0.04) board.set_position_values(food, 1) board.set_position_values(walls, 'x') board.set_position_values(capsules, 2) if ghost_scared_time < 5: board.set_position_values(ghosts, -3) # for i in range(height): # for j in range(width): # print board[i, j], # print # print print "GHOST LIST: ", ghosts for x, y in ghosts: # set the surrounding area around the ghost to half the reward of the ghost # avoids changing the reward of the ghost itself, the pacman and the walls # print "GHOST Coordinates: " + str(x) + " " + str(y) x_coordinates = [x - 1, x, x + 1] y_coordinates = [y - 1, y, y + 1] # print "X/Y Coordinates: " + str(x_coordinates) + " " + str(y_coordinates) for x_coord in x_coordinates: for y_coord in y_coordinates: if (x_coord, y_coord) not in protected_coords: # print("index: " + str((board.convert_y(y_coord), x_coord))) converted_y = board.convert_y(y_coord) # print "VALUE: " + str(board[board.convert_y(y), x]) board[converted_y, x_coord] = board[board.convert_y(y), x] / 2 # print "VALUE PART 2: " + str(board[converted_y, x_coord]) board = self.value_iteration(state, board) expected_utility = self.calculate_expected_utility( state, board, abs(current_pos[1] - (height - 1)), current_pos[0]) return max([(utility, action) for utility, action in expected_utility if action in legal])[1]
def getAction(self, state): legal = state.getLegalPacmanActions() #Again, get a list of pacman's legal actions if Directions.STOP in legal: legal.remove(Directions.STOP) pacman = api.whereAmI(state) #retrieve location of pacman food = api.food(state) #retrieve location of food #Distance of food dist = [] # initiate list of distances for i in range(len(food)): dist.append(util.manhattanDistance(pacman, food[i])) minIndex = dist.index(min(dist)) #get index of min dist value (assuming the array remains ordered) closestFood = food[minIndex] #current position coordinates x1, y1 = pacman[0], pacman[1] x2, y2 = closestFood print "closest food is: " print closestFood print "pacman's location is: " print pacman print "list of distances: " print dist #if pacman is to the West of closest food, then goEast = True and so on... goEast = x1 < x2 and y1 == y2 goWest = x1 > x2 and y1 == y2 goNorth = x1 == x2 and y1 < y2 goSouth = x1 == x2 and y1 > y2 last = state.getPacmanState().configuration.direction if x1 == 9 and y1 == 1: return api.makeMove(random.choice(legal), legal) else: pass if Directions.EAST in legal and (goEast): return api.makeMove('East', legal) elif Directions.WEST in legal and (goWest): return api.makeMove('West', legal) elif Directions.NORTH in legal and (goNorth): return api.makeMove('North', legal) elif Directions.SOUTH in legal and (goSouth): return api.makeMove('South', legal) elif last in legal: #if pacman doesnt find a move he can do, he just repeats the last move. return api.makeMove(last, legal) #this makes it so that the closest food isn't across the wall from him next else: return api.makeMove(random.choice(legal), legal) #just return a random move when he's out of moves
def updateFoodInMap(self, state): # First, make all grid elements thataren't walls with a reward - 0.02. map(lambda s: self.map.setValue(s[0], s[1], ('-0.02', 0)), self.states) food = api.food(state) food += api.capsules(state) map( lambda i: self.map.setValue(food[i][0], food[i][1], (self.initialFood / len(food), 0)), range(len(food)))
def registerInitialState(self, state): #print "Running registerInitialState for MDPAgent!" #print "I'm at:" #print api.whereAmI(state) global numoffood numoffood = len(api.food(state)) self.makeMap(state) self.addWallsFoodToMap(state)
def final(self, state): walls = api.walls(state) width, height = api.corners(state)[-1] self.last = None self.createMap(walls, width + 1, height + 1) self.pos = (0,0) self.capsule = (0,0) self.ghosts = [(0,0)] food = api.food(state)
def updateFoodInMap(self, state): # First, make all grid elements that aren't walls blank. for i in range(self.map.getWidth()): for j in range(self.map.getHeight()): if self.map.getValue(i, j) != '%': self.map.setValue(i, j, ' ') food = api.food(state) for i in range(len(food)): self.map.setValue(food[i][0], food[i][1], '*')
def registerInitialState(self, state): print "Running registerInitialState!" # Make a map of the right size self.makeMap(state) self.addWallsToMap(state) self.updateFoodInMap(state) self.map.display() self.updateUtilities(api.walls(state), api.food(state), api.ghosts(state), 1000) self.counter = 0
def getAction(self, state): # Demonstrates the information that Pacman can access about the state # of the game. # What are the current moves available legal = api.legalActions(state) print "Legal moves: ", legal # Where is Pacman? pacman = api.whereAmI(state) print "Pacman position: ", pacman # Where are the ghosts? print "Ghost positions:" theGhosts = api.ghosts(state) for i in range(len(theGhosts)): print theGhosts[i] print "timer" moreGhosts = api.ghostStatesWithTimes(state) for i in range(len(moreGhosts)): print moreGhosts[i] # How far away are the ghosts? print "Distance to ghosts:" for i in range(len(theGhosts)): print util.manhattanDistance(pacman, theGhosts[i]) # Where are the capsules? print "Capsule locations:" print api.capsules(state) # Where is the food? print "Food locations: " print api.food(state) # Where are the walls? print "Wall locations: " print api.walls(state) # getAction has to return a move. Here we pass "STOP" to the # API to ask Pacman to stay where they are. return api.makeMove(Directions.STOP, legal)
def getAction(self, state): """ The function to work out next intended action carried out. Parameters: None Returns: Directions: Intended action that Pacman will carry out. """ current_pos = api.whereAmI(state) food = api.food(state) # make sure all ghost coordinates are ints rather than floats ghosts = [(int(x), int(y)) for x, y in api.ghosts(state)] legal = api.legalActions(state) capsules = api.capsules(state) food_multiplier = ( (0.8 * len(food) / float(self.initial_num_food))**2) + 6 ghost_multiplier = ( (0.2 * len(food) / float(self.initial_num_food))**2) + 3 board = Board(self.width, self.height, -0.04) board.set_position_values(self.walls, 'x') board.set_position_values(capsules, 2 * food_multiplier) board.set_position_values(food, 1 * food_multiplier) board.set_position_values(ghosts, -7 * ghost_multiplier) # rewards of ghosts, walls and current position cannot be overridden protected_pos = set(ghosts + self.walls + [current_pos]) # setting a much more negative reward for potential positions ghosts can occupy # in two moves. for ghost in ghosts: # loop through potential positions that the ghost can occupy if it were # to move now for pos in self.get_next_pos(ghost): if pos not in protected_pos: # set the reward value of surrounding positions of ghosts to -6 * # ghost multiplier. board[int(board.convert_y(pos[1])), int(pos[0])] = -6 * ghost_multiplier for position in self.get_next_pos(pos): # loop through potential positions that the ghost can occupy if # it were to move two times. if position not in protected_pos: board[int(board.convert_y(position[1])), int(position[0])] = -6 * ghost_multiplier board = self.value_iteration(state, board) # call value iteration expected_utility = self.calculate_expected_utility( state, board, board.convert_y(current_pos[1]), current_pos[0]) # returns action associated to the max utility out of all the legal actions. return api.makeMove( max([(utility, action) for utility, action in expected_utility if action in legal])[1], legal)
def registerInitialState(self, state): print "Running registerInitialState!" # Make a map of the right size self.makeMap(state) self.addWallsToMap(state) self.updateFoodInMap(state) self.map.display() #Run the value iteration 1000 times at the start as it will stabilise the values for later on self.updateUtilities(api.walls(state), api.food(state), api.ghosts(state), 1000, state) self.counter = 0
def updateFoodInMap(self, state): # First, make all grid elements that aren't walls blank. for i in range(self.map.getWidth()): for j in range(self.map.getHeight()): if self.map.getValue(i, j) != self.WALL: self.map.setValue(i, j, self.EMPTY) food = api.food(state) for i in range(len(food)): self.map.setValue(food[i][0], food[i][1], self.FOOD) self.updateGhostsInMap(state)
def addWallsFoodToMap(self, state): walls = api.walls(state) for i in range(len(walls)): self.map1.setValue(walls[i][0], walls[i][1], '%') self.map2.setValue(walls[i][0], walls[i][1], '%') food = api.food(state) #print food for i in range(len(food)): self.map1.setValue(food[i][0], food[i][1],1) self.map2.setValue(food[i][0], food[i][1],1)
def __update_positions(self, state): ''' Repaints the grid according to the passed in state. Updating the position of pacman, the ghosts, the food, the capsules, and the blank spaces. In addition calculates the number of filled spaces and stores this value statically on Point, for later use in the reward function. Args: state: Current game state. ''' Grid.FILL_COUNT = 0 points = { Dispositions.FOOD: api.food(state), Dispositions.CAPSULE: api.capsules(state), Dispositions.GHOST_HOSTILE: [ ghost for ghost, time in api.ghost_states_with_times(state) if time <= Grid.GHOST_SAFE_TIME ], Dispositions.GHOST_EDIBLE: [ ghost for ghost, time in api.ghost_states_with_times(state) if time > Grid.GHOST_SAFE_TIME ], } for disposition, coordinates in points.iteritems(): for x, y in coordinates: if disposition in {Dispositions.FOOD, Dispositions.CAPSULE}: Grid.FILL_COUNT += 1 coordinate = Coordinate(x, y) # because ghost x, y are floats self[coordinate].disposition = disposition for coordinate, point in self: point.min_ghost_distance = Point.min_distance( coordinate, points[Dispositions.GHOST_HOSTILE] ) MDPAgent.set_gamma(len(api.food(state) + api.capsules(state)))
def getAction(self, state): pacman = api.whereAmI(state) x = pacman[0] y = pacman[1] food = api.food(state) capsule = api.capsules(state) # Get the actions we can try, and remove "STOP" if that is one of them. legal = state.getLegalPacmanActions() walls = api.walls(state) q = Queue.Queue() if Directions.STOP in legal: legal.remove(Directions.STOP) if Directions.WEST in legal: #print "West" q.put(((x-1,y), Directions.WEST)) if Directions.EAST in legal: #print "East" q.put(((x+1,y), Directions.EAST)) if Directions.NORTH in legal: #print "North" q.put(((x,y+1), Directions.NORTH)) if Directions.SOUTH in legal: #print "South" q.put(((x,y-1), Directions.SOUTH)) #print food #print q.toString() while not q.empty(): possible = q.get() position = possible[0] x = position[0] y = position[1] print position if position in (food or capsules): print "moving" return api.makeMove(possible[1], legal) else: #print "searching" if (x-1,y) not in walls: q.put(((x-1,y), possible[1])) if (x+1,y) not in walls: q.put(((x+1,y), possible[1])) if (x,y+1) not in walls: q.put(((x,y+1), possible[1])) if (x,y-1) not in walls: q.put(((x,y-1), possible[1]))
def _findNearestFood(self, state): pacman = api.whereAmI(state) foodMap = api.food(state) if foodMap: distances = [ abs((x - pacman[0])) + abs((y - pacman[1])) for (x, y) in foodMap ] return min(zip(distances, foodMap)) else: return (0, (0, 0))
def getAction(self, state): # Get the actions we can try, and remove "STOP" if that is one of them. legal = api.legalActions(state) if Directions.STOP in legal: legal.remove(Directions.STOP) # Get current location of pacman pacman = api.whereAmI(state) # Get list of food locations food = api.food(state) # Compute manhattan distance to each food location foodDistances = [] for i in range(len(food)): foodDistances.append(util.manhattanDistance(pacman, food[i])) # print foodDistances minDistance = min(foodDistances) # print "Min Distance: ", minDistance minDistanceIndex = foodDistances.index(minDistance) # print "Min Food index: ", minDistanceIndex nearestFood = food[minDistanceIndex] # print "Pacman: ", pacman # print "Nearest Food: ", nearestFood diffX = pacman[0] - nearestFood[0] diffY = pacman[1] - nearestFood[1] # print "legal", legal # print diffX, diffY moveX = Directions.STOP moveY = Directions.STOP # Determine whether to move east or west if diffX >= 0: # print "Go left" moveX = Directions.WEST elif diffX < 0: # print "Go right" moveX = Directions.EAST # Determine whether to move north or south if diffY >= 0: # print "Go down" moveY = Directions.SOUTH elif diffY < 0: # print "Go up" moveY = Directions.NORTH # Determine whether to move in X or Y # print "diffX: ", diffX, " diffY", diffY if abs(diffX) >= abs(diffY) and moveX in legal: # print moveX return api.makeMove(moveX, legal) elif abs(diffY) >= 0 and moveY in legal: # print moveY return api.makeMove(moveY, legal) elif abs(diffX) >= 0 and moveX in legal: return api.makeMove(moveX, legal) else: # print "Random" return api.makeMove(random.choice(legal), legal)
def updateFoodInMap(self, state): # First, make all grid elements that aren't walls blank. # Set the blank spaces to -3 and walls are set to -5 for i in range(self.map.getWidth()): for j in range(self.map.getHeight()): if self.map.getValue(i, j) != -5: self.map.setValue(i, j, -3) #Get the food and for each food coordinate, add it to the map and set the value to 10 food = api.food(state) for i in range(len(food)): self.map.setValue(food[i][0], food[i][1], 10)
def updateMap(self, state): ghost = api.ghosts(state) ghostStates = api.ghostStates(state) capsule = api.capsules(state) food = api.food(state) wall = api.walls(state) for node in self.mapDictionary: if node in wall: self.mapDictionary.update({node: 'X'}) return self.mapDictionary