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 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 move_pacman(self, pac_dir, axis, legal, pacman, target): if pacman[axis] > target[axis]: if pac_dir in legal: return api.makeMove(pac_dir, legal) elif pacman[axis] < target[axis]: if pac_dir in legal: return api.makeMove(pac_dir.REVERSE, legal)
def getToCornersOrFood(): # if all corners are visited then visit the food which was left beforehand if (len(self.visitedCorners) > 4): value = 1000000 for food in self.allFood: distance = util.manhattanDistance(pacman, food) if (distance < value): self.corner = food #find the path toward the corner or food path = breadth_first_search(pacman, self.corner, walls) if (pacman == self.corner): self.visitedCorners.append(self.corner) self.corner = getNearestCorner(pacman, corners) path = breadth_first_search(pacman, self.corner, walls) if path: return path.pop(0) else: pick = random.choice(legal) return api.makeMove(pick, legal) else: if path: return path.pop(0) else: pick = random.choice(legal) return api.makeMove(pick, 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 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): # Get the actions we can try, and remove "STOP" if that is one of them. legal = api.legalActions(state) if Directions.WEST in legal: return api.makeMove(Directions.WEST, legal) 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): # 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): # 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 getAction(self,state): legal = state.getLegalPacmanActions() #get a list of pacman's legal actions if Directions.STOP in legal: legal.remove(Directions.STOP) #make it so that pacman won't stop even though it is legal to #if going west is in the list of legal moves, then go west #else pick a random direction to move if Directions.WEST in legal: return api.makeMove('West', legal) else: pick = random.choice(legal) return api.makeMove(pick, legal)
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): # 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) # If we can repeat the last action, do it. Otherwise make a # random choice. if self.last in legal: return api.makeMove(self.last, legal) else: pick = random.choice(legal) # Since we changed action, record what we did self.last = pick return api.makeMove(pick, 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 ghost locations ghosts = api.ghosts(state) # Compute manhattan distance to each ghost location ghostsDistances = [] for i in range(len(ghosts)): ghostsDistances.append(util.manhattanDistance(pacman, ghosts[i])) minDistance = min(ghostsDistances) print "Min Distance: ", minDistance minDistanceIndex = ghostsDistances.index(minDistance) print "Min Ghosts index: ", minDistanceIndex nearestGhost = ghosts[minDistanceIndex] print "Pacman: ", pacman print "Nearest Ghost: ", nearestGhost diffX = pacman[0] - nearestGhost[0] diffY = pacman[1] - nearestGhost[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 right" moveX = Directions.EAST elif diffX < 0: print "Go left" moveX = Directions.WEST # Determine whether to move north or south if diffY >= 0: print "Go up" moveY = Directions.NORTH elif diffY < 0: print "Go down" moveY = Directions.SOUTH # Determine whether to move in X or Y print "diffX: ", diffX, " diffY", diffY if abs(diffX) >= abs(diffY) and moveX in legal: return api.makeMove(moveX, legal) elif abs(diffY) >= 0 and moveY in legal: return api.makeMove(moveY, legal) elif abs(diffX) >= 0 and moveX in legal: return api.makeMove(moveX, legal) else: return api.makeMove(random.choice(legal), legal)
def getAction(self, state): # Get the actions we can try, and remove "STOP" if that is one of them. legal = state.getLegalPacmanActions() if Directions.STOP in legal: legal.remove(Directions.STOP) # Get the current score current_score = state.getScore() # Get the last action last = state.getPacmanState().configuration.direction # If we can repeat the last action, do it. Otherwise make a # random choice. if last in legal: return api.makeMove(last, legal) else: pick = random.choice(legal) return api.makeMove(pick, 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 getAction(self, state): #current possible moves legal = state.getLegalPacmanActions() #Prevent it from stopping if Directions.STOP in legal: legal.remove(Directions.STOP) #if it can go west, go west if Directions.WEST in legal: return api.makeMove(Directions.WEST, legal) #else pick a random direction else: pick = random.choice(legal) return api.makeMove(pick, legal)
def getAction(self, state): legal = api.legalActions(state) corners = api.corners(state) print(corners) return api.makeMove(Directions.STOP, 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 getMove(self, state): scores = [0, 0, 0, 0] #searches all adjacent squares for i in range(len(self.possibleMoves)): direction = self.possibleMoves[i][1] #see if surrounding locations are legal moves. if so, add it to the search if direction in self.legal: deltaPosition = self.possibleMoves[i][0] nextPosition = self.sumPair(self.pacman, deltaPosition) positionScore = self.map[nextPosition[0]][nextPosition[1]] scores[i] = positionScore else: scores[i] = -1 cumulativeScore = [0, 0, 0, 0] for i in range(len(scores)): if self.possibleMoves[i][1] in self.legal: cumulativeScore[i] = .1 * scores[ (i + 4) % 4] + .8 * scores[i] + .1 * scores[(i + 1) % 4] else: cumulativeScore[i] = -100 max = -100 index = 0 for i in range(len(cumulativeScore)): if cumulativeScore[i] > max: index = i max = cumulativeScore[i] return api.makeMove(self.possibleMoves[index][1], self.legal)
def getMove(self,state): #print "getmove" #calculate the scores from all possible moves scores = self.calcAdjUtility(self.pacman[0], self.pacman[1]) #get the maximum value from calculated utilities maxUtility = max(scores) #get the imdex of the move index = scores.index(maxUtility) #get the direction that orresponds to the highest utility direction = self.possibleMoves[index][1] #if staying still can provide higher utility, then stay still if self.utility[self.pacman[0]][self.pacman[1]] >= maxUtility: #print "dont move" return api.makeMove(Directions.STOP, self.legal) #otherwise move in the previously found direction return api.makeMove(direction, self.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): #if the internal map of the environment has yet to be initialized, initialize it if not self.init: self.initialize(state) #update the legal moves for this move self.setLegal(state) #if pacman can detect a ghost nearby pacman needs to run away if api.ghosts(state): return self.runAway(state) #if a route has been found, pacman will follow it instead of searching again if len(self.path) != 0: #pop off the first move in the path nextMove = self.path.pop(0) #check that the move is legal if nextMove[1] in self.legal: #mark that position as visited with "P" self.map[nextMove[0][0]][nextMove[0][1]] = "P" self.lastDir = nextMove[1] #return the move return api.makeMove(self.lastDir, self.legal) #if the move is not legal, find a new path else: return self.findPath(state) #otherwise find a path else: return self.findPath(state)
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): """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): # 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): 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): 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): 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): # 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): 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)