def findPath(self, state): #print "findPath" #initialize the queue for a Depth First Seach bfsQueue = self.getBFSQueue(state) #copy of the current state of internal map to mark searched nodes copyMap = deepcopy(self.map) #conducts bfs search while len(bfsQueue) != 0: #pop the element from the queue nextCheck = bfsQueue.pop(0) #get the position stored at element nextCheckPosition = nextCheck[0] #extract x position from position possibleX = nextCheckPosition[0] #extract y position from position possibleY = nextCheckPosition[1] #if the position contains food, a capsule, or is unknown, pacman will visit it if self.map[possibleX][possibleY] == "F" or self.map[possibleX][possibleY] == "?" or self.map[possibleX][possibleY] == "C": #print "next move" #pop the first step of the path stored in the currently searching element nextMove = nextCheck[1].pop(0) #set the internal path of pacman to the one found by the BFS self.path = nextCheck[1] #mark the position on the map as "P" to mark visited positions self.map[nextMove[0][0]][nextMove[0][1]] = "P" #set lastDir as the next move self.lastDir = nextMove[1] #return the popped direction stored at the frist step above as the next move return api.makeMove(self.lastDir, api.legalActions(state)) else: #if position does not contain food, capsule, or unknown information, continue the BFS search #print "searching bfs" #check all possibleMoves from the current popped from the queue in the current iteration of the BFS algorithm for move in self.possibleMoves: #get the next position in the search nextPosition = self.sumPair(move[0], nextCheckPosition) #if this postion is neither a wall nor a location already searched by the algorithm, add it to the search if self.map[nextPosition[0]][nextPosition[1]] != "W" and copyMap[nextPosition[0]][nextPosition[1]] != "X": #mark this location as searched by the BFS algorithm copyMap[nextPosition[0]][nextPosition[1]] = "X" #copy the existing path from the search into a new variable path = deepcopy(nextCheck[1]) #add in the new loation and the direction to move to said location to the path path.append((nextPosition, move[1])) #add the path to the bfsQueue bfsQueue.append((nextPosition, path)) #if no moves are available, pacman will not move self.lastDir = Directions.STOP return api.makeMove(Directions.STOP, api.legalActions(state))
def getAction(self, state): legal = api.legalActions(state) corners = api.corners(state) print(corners) return api.makeMove(Directions.STOP, legal)
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 getAction(self, state): # How we access the features. features = api.getFeatureVector( state ) # first 4: walls, next 4: food, next 8: ghosts, next 1: ghost infront, next 1: class # ***************************************************** # # Here you should insert code to call the classifier to # decide what to do based on features and use it to decide # what action to take. # # ******************************************************* # from collected 'features' vector, classify as any of 0-3. # this gives your move # Get class from model and convert to action number = self.model.predict(features) action = self.convertNumberToMove(number) # Get the actions we can try. legal = api.legalActions(state) return api.makeMove(action, legal)
def getAction(self, state): """Get Action of pacman Args: state: The state of an agent (configuration, speed, scared, etc). """ # 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) cols,rows = self.mapSize(state) # Get the layout size # Judge the layout type medium or small map_type = 'medium' if cols >= 10 and rows >= 10: map_type = 'medium' else: map_type = 'small' # Update value map after pacman moved updated_map = self.mapUpdate(state) # Update value map with iteration updated_map = self.Iteration(state,updated_map,map_type) # Make move return api.makeMove(self.whichToMove(state,updated_map), legal)
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 getAction(self, state): self.updateMap(state) self.policyIteration() pacman = api.whereAmI(state) legal = api.legalActions(state) move = self.policy[pacman] return api.makeMove(move, legal)
def getAction(self, state): start = api.whereAmI(state) # generate rewards for map locations in each game cycle to reflect changing conditions self.rewardMapping(state) # create the dictionary of states at the start of the game if not self.stateDict: self.stateMapping(state) # get converged utilities from value iteration gridVals = self.valueIteration(state) # get and assign the utility values and their associated locations to the Grid object, then print this map # representation to the console valKeys = gridVals.keys() valVals = gridVals.values() for i in range(len(gridVals)): y = valKeys[i][0] x = valKeys[i][1] val = '{:3.2f}'.format(valVals[i]) self.map.setValue(y, x, val) self.map.prettyDisplay() # optimal policy for Pacman: get the location of the optimal utility from his current position sPrime = self.bestMove(state, gridVals) # required argument for makeMove() legal = api.legalActions(state) # return a move based on the direction returned by singleMove() and sPrime return api.makeMove(self.singleMove(start, sPrime), legal)
def getAction(self, state): """ This function is used to make pacman move. Parameter: state: the state of pacman. Returns the move of pacman. """ # 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 the size of layout rows,columns = self.map_size(state) # Judge the map_type is mediumClass or smallGrid map_type = 'mediumClass' if rows >= 10 and columns >= 10: map_type = 'mediumClass' else: map_type = 'smallGrid' # Update the value map once pacman moved value_map = self.map_update(state) # Update the value map with value_iteration value_map = self.value_iteration(state, value_map, map_type) # Make move return api.makeMove(self.where_to_move(state,value_map), legal)
def getAction(self, state): # How we access the features. features = api.getFeatureVector(state) # ***************************************************** # # Here you should insert code to call the classifier to # decide what to do based on features and use it to decide # what action to take. # # ******************************************************* nn = self.nn # use the trained neural network to predict y_pred = nn.predict(features) # transform the output value from a list of 0 and 1 to a single value n = 0 for i in range(len(y_pred)): if y_pred[i] > y_pred[n]: n = i # Get the actions we can try. legal = api.legalActions(state) # getAction has to return a move. Here we pass "STOP" to the # API to ask Pacman to stay where they are. We need to pass # the set of legal moves to teh API so it can do some safety # checking. return api.makeMove(self.convertNumberToMove(n), legal)
def getAction(self, state): # How we access the features. features = api.getFeatureVector(state) # ***************************************************** # # Here you should insert code to call the classifier to # decide what to do based on features and use it to decide # what action to take. features = np.append(features, 0) number = self.PredictMove(features) move = self.convertNumberToMove(number) # # ******************************************************* # Get the actions we can try. legal = api.legalActions(state) # Add some randomness to not get pacman completely stuck in the corner. if move not in legal: move = random.choice(legal) # getAction has to return a move. Here we pass "STOP" to the # API to ask Pacman to stay where they are. We need to pass # the set of legal moves to teh API so it can do some safety # checking. return api.makeMove(move, legal)
def getAction(self, state): self.pacman = api.whereAmI(state) self.legal = api.legalActions(state) self.ghosts = api.ghosts(state) if not self.init: self.initialize(state) else: # if self.reward[self.pacman[0]][self.pacman[1]] < 10: # self.reward[self.pacman[0]][self.pacman[1]] = self.reward[self.pacman[0]][self.pacman[1]] - 1 # else: self.reward[self.pacman[0]][self.pacman[1]] = self.baseReward reward = self.updateMap(state) self.bellman(state, reward) print '' for row in reward: print row print '' for row in self.utility: print row #return self.getMove(state) return api.makeMove(Directions.STOP, self.legal)
def getAction(self, state): # find the facing direction of ghost self.current_ghosts_states = api.ghostStatesWithTimes(state) valid_facing = [(1, 0), (-1, 0), (0, 1), (0, -1), (0, 0)] self.ghosts_facing = [] for i in range(len(self.current_ghosts_states)): facing_of_ghost = (int( round(self.current_ghosts_states[i][0][0] - self.last_ghosts_states[i][0][0])), int( round(self.current_ghosts_states[i][0][1] - self.last_ghosts_states[i][0][1]))) # elated by pacman if facing_of_ghost not in valid_facing: facing_of_ghost = (0, 0) self.ghosts_facing.append(facing_of_ghost) self.last_ghosts_states = self.current_ghosts_states # search optimal policy and do an optimal action self.initialRewardMap(state) pacman = api.whereAmI(state) utilities_map = self.updateUtilities() legal = api.legalActions(state) action_vectors = [Actions.directionToVector(a, 1) for a in legal] optic_action = max( map( lambda x: (float( utilities_map.getValue(x[0] + pacman[0], x[1] + pacman[1]) ), x), action_vectors)) return api.makeMove(Actions.vectorToDirection(optic_action[1]), legal)
def deGhost(self, state): # Avoid ghosts # # When running from a ghost, if pacman turns a corner, # pacman can no longer see ghost, and thus may backtrack # towards the ghost, causing him to get caught. # Avoid this by going straight and never backtracking # for 2 steps. Allows pacman to turn corners without losing # track of ghost and backtracking towards it self.update(state) # print "Avoiding ghosts" cur = api.whereAmI(state) legal = api.legalActions(state) legal.remove(Directions.STOP) if len(legal) > 1: #Remove option to backtrack legal.remove(self.oppositeDirection(state, self.last)) #Go straight if possible if self.last in legal: return self.last self.last = random.choice(legal) return self.last
def getAction(self, state): self.pacman = api.whereAmI(state) self.legal = api.legalActions(state) if not self.init: self.initialize(state) else: # if self.reward[self.pacman[0]][self.pacman[1]] < 10: # self.reward[self.pacman[0]][self.pacman[1]] = self.reward[self.pacman[0]][self.pacman[1]] - 1 # else: self.reward[self.pacman[0]][self.pacman[1]] = -1 print "reward" for row in self.reward: print row self.updateMap(state) #print "\nreward" #for row in self.reward: # print row self.bellman(state) #print "utility" #for row in self.utility: # print row return self.getMove(state) return api.makeMove(Directions.STOP, self.legal)
def getAction(self, state): # Get legal actions legal = api.legalActions(state) # Get location of Pacman pacman = api.whereAmI(state) # Get location of Ghosts locGhosts = api.ghosts(state) #print "locGhosts: ", locGhosts # Get distance between pacman and the ghosts for i in locGhosts: p_g_dist = util.manhattanDistance(pacman, i) # Get distance between ghosts g_g_dist = util.manhattanDistance(locGhosts[0], locGhosts[1]) #print "g_g_dist:", g_g_dist # Get distance between pacman and first Ghost dist = [] dist.append(locGhosts[0][0] - pacman[0]) dist.append(locGhosts[0][1] - pacman[1]) return api.makeMove(Directions.STOP, legal)
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) # Random choice between the legal options. return api.makeMove(random.choice(legal), legal)
def getAction(self, state): self.makeMap(state) self.addWallsToMap(state) self.addConsumablesToMap(state) self.updateGhosts(state) # Get the actions we can try, and remove "STOP" if that is one of them. pacman = api.whereAmI(state) legal = api.legalActions(state) ghosts = api.ghosts(state) corners = api.corners(state) layoutHeight = self.getLayoutHeight(corners) layoutWidth = self.getLayoutWidth(corners) if (layoutHeight-1)<8 and (layoutWidth-1)<8: for i in range (100): self.valIterS(state,0.68,-0.1) else: for i in range (50): self.valIterM(state,0.8,-0.1) plannedMove = self.plannedMove(pacman[0],pacman[1]) #self.dankMap.prettyDisplay() #Feel free to uncomment this if you like to see the values generated if Directions.STOP in legal: legal.remove(Directions.STOP) #Input the calculated move for our next move return api.makeMove(plannedMove, legal)
def getAction(self, state): value_list = [] legal = api.legalActions(state) corner = api.corners(state) pacman = api.whereAmI(state) pacman_x = pacman[0] pacman_y = pacman[1] legal_width = corner[3][0] legal_height = corner[3][1] # policy iteration and evaluation # get the value of four directions and select the direction corresponding to # the maximum value as Pacman's decision. map_effect = gridworld().map_valuegeneration(state, (legal_width, legal_height)) value_list.append(map_effect[(pacman_x-1, pacman_y)]) value_list.append(map_effect[(pacman_x+1, pacman_y)]) value_list.append(map_effect[(pacman_x, pacman_y + 1)]) value_list.append(map_effect[(pacman_x, pacman_y - 1)]) max_value = value_list.index(max(value_list)) # print 'map_effect' # print map_effect # print 'value_list' # print value_list # print 'max_value' # print max_value if max_value == 0: return api.makeMove(Directions.WEST, legal) if max_value == 1: return api.makeMove(Directions.EAST, legal) if max_value == 2: return api.makeMove(Directions.NORTH, legal) if max_value == 3: return api.makeMove(Directions.SOUTH, legal)
def __maximum_expected_utility(self, state, debug_mode): # the location of agent agent_location = api.whereAmI(state) if debug_mode: print("\tagent_location=" + str(agent_location)) # discover the legal actions legal = api.legalActions(state) # remove STOP to increase mobility legal.remove(Directions.STOP) # decide next move based on maximum expected utility action, maximum_expected_utility = None, None for direction in legal: utility = self.__utilities[self.__neighbors[agent_location] [direction]][1] if action == None or maximum_expected_utility == None: action = direction maximum_expected_utility = utility expected_utility = utility if debug_mode: print("\tdirection=" + str(direction) + "\texpected_utility=" + str(expected_utility)) if expected_utility > maximum_expected_utility: action = direction maximum_expected_utility = expected_utility if debug_mode: print("\taction=" + str(action)) return action
def getAction(self, state): legal = api.legalActions(state) if not Directions.WEST in legal: if Directions.EAST in legal: legal.remove(Directions.EAST) return api.makeMove(random.choice(legal), legal) return api.makeMove(Directions.WEST, legal)
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): 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 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): self.updateMap(state) pacman = api.whereAmI(state) legal = api.legalActions(state) move = self.policy[pacman] print self.values print move return api.makeMove(move, 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 runFromGhost(self, state): # Runs away from ghosts # # Returns any direction that moves pacman away from ghost # Removes option of going towards ghost # Makes random choice of remaining options self.update(state) # print "Running from ghosts" cur = api.whereAmI(state) ghosts = api.ghosts(state) legal = api.legalActions(state) legal.remove(Directions.STOP) for x in range(1, 4): #Ghost seen south of pacman if (cur[0], cur[1] - x) in ghosts: if Directions.SOUTH in legal: #Stop pacman from going south towards ghost if len(legal) > 1: legal.remove(Directions.SOUTH) #Let pacman go in any direction except south self.last = random.choice(legal) return self.last #Ghost seen west of pacman if (cur[0] - x, cur[1]) in ghosts: if Directions.WEST in legal: #Stop pacman from going west towards ghost if len(legal) > 1: legal.remove(Directions.WEST) #Let pacman go in any direction except west self.last = random.choice(legal) return self.last #Ghost seen north of pacman if (cur[0], cur[1] + x) in ghosts: if Directions.NORTH in legal: #Stop pacman going north towards ghost if len(legal) > 1: legal.remove(Directions.NORTH) #Let pacman go in any direction except south self.last = random.choice(legal) return self.last #Ghost seen east of pacman if (cur[0] + x, cur[1]) in ghosts: if Directions.EAST in legal: #Stop pacman going east towards ghost if len(legal) > 1: legal.remove(Directions.EAST) #Let pacman go in any direction except east self.last = random.choice(legal) return self.last self.last = random.choice(legal) return self.last
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) # Always go West if it is an option if Directions.WEST in legal: return api.makeMove(Directions.WEST, legal) # Otherwise pick any other legal action return api.makeMove(random.choice(legal), legal)
def getAction(self, state): features = api.getFeatureVector(state) # Return majority classification from ensemble moveNumber = majorityVote(features, self.classifier) legal = api.legalActions(state) return api.makeMove(self.convertNumberToMove(moveNumber), legal)
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)