def mstHeuristic(state, problem): if "calc" not in problem.heuristicInfo: problem.heuristicInfo["calc"] = DistanceCalculator(problem.startingGameState.data.layout) calculator = problem.heuristicInfo["calc"] BIG_COST = 1000 totalCost = -BIG_COST foodList = state[1].asList() # only works for integral pacman positions foodList.append(util.nearestPoint(state[0])) treeNodes = {} nonTreeNodes = util.PriorityQueue() nodeCosts = {} for pos in foodList: nonTreeNodes.push(pos, BIG_COST) nodeCosts[pos] = BIG_COST while not nonTreeNodes.isEmpty(): newNode = nonTreeNodes.pop() cost = nodeCosts[newNode] totalCost += cost treeNodes[newNode] = 1 for nonTreeNode in foodList: if treeNodes.has_key(nonTreeNode): continue newDist = calculator.getDistance(newNode, nonTreeNode) # newDist = manhattanDist(newNode, nonTreeNode) oldDist = nodeCosts[nonTreeNode] if newDist < oldDist: nodeCosts[nonTreeNode] = newDist nonTreeNodes.push(nonTreeNode, newDist) return totalCost
def getAction(self, gameState): """ Calls chooseAction on a grid position, but continues on half positions. This method also cedes some processing time to the distance calculator, which computes the shortest path distance between all pairs of points. If you subclass CaptureAgent, you shouldn't need to override this method. It takes care of appending the current gameState on to your observation history (so you have a record of the game states of the game) and will call your choose action method if you're in a state (rather than halfway through your last move - this occurs because Pacman agents move half as quickly as ghost agents). If you aren't going to be using the distance calculator we provide, you can comment out the line beginning "distanceCalculator" so as not to lose computing time to the calculating distances you're not using. """ # Give some time to the distance calculator thread distanceCalculator.waitOnDistanceCalculator(self.timeForComputing) self.observationHistory.append(gameState) myState = gameState.getAgentState(self.index) myPos = myState.getPosition() if myPos != nearestPoint(myPos): # We're halfway from one position to the next return gameState.getLegalActions(self.index)[0] else: return self.chooseAction(gameState)
def getSuccessor(self, index, gameState, action): """ Finds the next successor which is a grid position (location tuple). """ team = self.getTeam(gameState) for agent in team: if index == agent: temp = gameState.generateSuccessor(self.index, action) successor = temp.deepCopy() successor.data.agentStates = copy.deepcopy(temp.data.agentStates) pos = successor.getAgentState(self.index).getPosition() if pos != nearestPoint(pos): # Only half a grid position was covered return successor.generateSuccessor(self.index, action) else: return successor successor = gameState.deepCopy() successor.data.agentStates = copy.deepcopy(gameState.data.agentStates) #successor.data.agentStates = gameState.copyAgentStates(successor.data.agentStates) position = self.getNextPosition(successor, successor.data.agentStates[index].configuration.pos, action) successor.data.agentStates[index].configuration.pos = position foodList = self.getFoodYouAreDefending(gameState).asList() for food in foodList: if food == position: x,y = position successor.data.food[x][y] = False successor.data.score += 1 successor.data.scoreChange = 1 return successor
def __str__(self): width, height = self.layout.width, self.layout.height map = Grid(width, height) if ut.isInstance(self.food, (1, 2)): self.food = ut.reconstituteGrid(self.food) for x in range(width): for y in range(height): food, walls = self.food, self.layout.walls map[x][y] = self._foodWallStr(food[x][y], walls[x][y]) for agentState in self.agentStates: if agentState is None: continue if agentState.configuration is None: continue x, y = [int(i) for i in ut.nearestPoint(agentState.configuration.pos)] agent_dir = agentState.configuration.direction if agentState.isPacman: map[x][y] = self._pacStr(agent_dir) else: map[x][y] = self._ghostStr(agent_dir) for x, y in self.capsules: map[x][y] = 'o' return str(map) + ("\nScore: %d\n" % self.score)
def getAction(self, state): "The agent receives a GameState (defined in pacman.py)." "[Project 1] YOUR CODE HERE" vector = {Directions.EAST:(1, 0), Directions.WEST:(-1, 0), Directions.SOUTH:(0, -1), Directions.NORTH:(0, 1), Directions.STOP:(0, 0)} pacman_pos = state.getPacmanPosition() ghost_pos_all = [] for ghost_pos in state.getGhostPositions(): ghost_pos = util.nearestPoint(ghost_pos) for key in vector: x = ghost_pos[0] + vector[key][0] y = ghost_pos[1] + vector[key][1] ghost_pos_all.append((x, y)) print ghost_pos_all next_pos_all = [] for action in state.getLegalActions(): next_pos_all.append((pacman_pos[0] + vector[action][0], pacman_pos[1] + vector[action][1])) for next_pos in next_pos_all: if next_pos in ghost_pos_all: for action in [Directions.NORTH, Directions.SOUTH]: if action in state.getLegalActions(): return action for action in [Directions.EAST, Directions.WEST]: if (pacman_pos[0] + vector[action][0], pacman_pos[1] + vector[action][1]) in ghost_pos_all: return Directions.REVERSE[action] return Directions.STOP
def applyAction( state, action, index ): """ Edits the state to reflect the results of the action. """ legal = BombermanRules.getLegalActions( state, index) if action not in legal: print ("Illegal action " + str(action) + " with agentIndex " + str(index) + ' and legals:' + str(legal)) #action = random.choice(legal) if Directions.STOP in legal: action = Directions.STOP else : action =random.choice(legal) agentState = state.data.agentStates[index] # Update Configuration vector = Actions.directionToVector( action, agentState.getSpeed() ) agentState.configuration = agentState.configuration.generateSuccessor( vector ) # Eat next = agentState.configuration.getPosition() nearest = nearestPoint( next ) if manhattanDistance( nearest, next ) <= 0.5 : # consume item BombermanRules.consume( nearest, state, index ) # Lay bomb if action is Actions.LAY: state.layABomb(index,nearest)
def isGoalState(self, state): pos = state.getAgentState(self.agentIndex).getPosition() x,y = nearestPoint(pos) if pos != (x,y): return False if self.targets[x][y]: pass return self.targets[x][y]
def getSuccessor(self, gameState, action): successor = gameState.generateSuccessor(self.index, action) pos = successor.getAgentState(self.index).getPosition() if pos != nearestPoint(pos): return successor.generateSuccessor(self.index, action) else: return successor
def applyAction(state, action, agentIndex): """ Edits the state to reflect the results of the action. """ legal = AgentRules.getLegalActions(state, agentIndex) if action not in legal: raise Exception("Illegal action " + str(action)) # Update Configuration agentState = state.data.agentStates[agentIndex] speed = 1.0 # if agentState.isPacman: speed = 0.5 vector = Actions.directionToVector(action, speed) oldConfig = agentState.configuration agentState.configuration = oldConfig.generateSuccessor(vector) # Eat next = agentState.configuration.getPosition() nearest = nearestPoint(next) if agentState.isPacman and manhattanDistance(nearest, next) <= 0.9 : AgentRules.consume(nearest, state, state.isOnRedTeam(agentIndex)) # Change agent type if next == nearest: agentState.isPacman = [state.isOnRedTeam(agentIndex), state.isRed(agentState.configuration)].count(True) == 1
def getPathToNearestDot(self, gameState, myState): """ A path takes the form [(whereYouAre, whatYouDo), ...] """ food = self.getFood(gameState) problem = AnyDotOnSideWillDo(nearestPoint(myState.getPosition()), food, gameState.getWalls()) states, actions, cost = search.breadthFirstSearch(problem) return zip(states, actions)
def evalFn(self, successor, action): if self.getFood(successor).count() == 0: return 1000 myState = successor.getAgentState(self.index) pos = myState.getPosition() if pos != nearestPoint(pos): return self.evalFn(successor.generateSuccessor(self.index, action), action) food = self.getFood(successor) problem = AnyDotOnSideWillDo(nearestPoint(pos), food, successor.getWalls()) distanceToFood = search.breadthFirstSearch(problem)[2] distanceToOpponent = 100 for enemyIndex in self.getOpponents(successor): opp = successor.getAgentState(enemyIndex) if not opp.isPacman: distanceToOpponent = min(distanceToOpponent, manhattanDistance(myState.getPosition(), opp.getPosition())) return -0.3 * distanceToFood + self.getScore(successor) + 2 * math.log(distanceToOpponent)
def getSuccessor(self, gameState, action): # Finds the next successor which is a grid position (location tuple). successor = gameState.generateSuccessor(self.index, action) pos = successor.getAgentState(self.index).getPosition() if pos != nearestPoint(pos): # Only half a grid position was covered return successor.generateSuccessor(self.index, action) else: return successor
def getAction(self, gameState): self.observationHistory.append(gameState) myState = gameState.getAgentState(self.index) myPos = myState.getPosition() if myPos != util.nearestPoint(myPos): # We're halfway from one position to the next return gameState.getLegalActions(self.index)[0] else: return self.chooseAction(gameState)
def getAction(self, state): myState = state.getAgentState(self.index) myPos = myState.getPosition() if myPos != nearestPoint(myPos): return self.lastAction action = self.chooseAction(state, myState) if action == Directions.STOP: print 'stop' self.lastAction = action return action
def capsuleEdible(self, pacmanPos, capsulePos): """ arguments: pacmanPos is an x,y position on the board (representing pacman's location) capsulePos is an x,y position on the board (representing a capsule's location) returns: True if pacman can eat the capsule at capsulePos from pacmanPos, else False """ nearest = nearestPoint(pacmanPos) return nearest == capsulePos and manhattanDistance(nearest, pacmanPos) <= 0.5
def getAction(self, state): "The agent receives a GameState (defined in pacman.py)." "[Project 1] YOUR CODE HERE" INTMAX = 100000 vector = {Directions.EAST:(1, 0), Directions.WEST:(-1, 0), Directions.SOUTH:(0, -1), Directions.NORTH:(0, 1)} grid = list(state.getWalls()) pacman_pos = state.getPacmanPosition() food = list(state.getFood()) near_food_depth = INTMAX for x in range(len(food)): for y in range(len(food[x])): food_depth = util.manhattanDistance(pacman_pos, (x, y)) if food[x][y] and near_food_depth > food_depth: near_food_depth = food_depth food_pos = (x, y) bfs_map = [[0 for y in range(len(grid[x]))] for x in range(len(grid))] bfs_map[food_pos[0]][food_pos[1]] = 1 for x in range(len(grid)): #grid = [[not y for y in x] for x in grid] for y in range(len(grid[x])): grid[x][y] = not grid[x][y] ghost_pos_all = [] for ghost_pos in state.getGhostPositions(): ghost_pos = util.nearestPoint(ghost_pos) ghost_pos_all.append(ghost_pos) bfs_map[ghost_pos[0]][ghost_pos[1]] = 0 for key in vector: x = ghost_pos[0] + vector[key][0] y = ghost_pos[1] + vector[key][1] ghost_pos_all.append((x, y)) bfs_map[x][y] = 0 q = util.Queue() q.push(Node(food_pos, 1)) grid[food_pos[0]][food_pos[1]] = False while not q.isEmpty(): node = q.pop() for key in vector: x = node.pos[0] + vector[key][0] y = node.pos[1] + vector[key][1] if grid[x][y]: if (x, y) not in ghost_pos_all: bfs_map[x][y] = node.depth + 1 q.push(Node((x, y), node.depth + 1)) grid[x][y] = False mn = INTMAX move = Directions.STOP for key in vector: x = pacman_pos[0] + vector[key][0] y = pacman_pos[1] + vector[key][1] if bfs_map[x][y] != 0 and mn > bfs_map[x][y]: move = key mn = bfs_map[x][y] return move return Directions.STOP
def cumGhostsDistance(position, ghostPositions, state): ghostStates = state.getGhostStates() scaredTimes = [ghostState.scaredTimer for ghostState in ghostStates] total = 0 for i in range(len(scaredTimes)): d = math.sqrt(mazeDistance(position, util.nearestPoint(state.getGhostPosition(i+1)), state)) if scaredTimes[i] > 0: d = d * math.sqrt(scaredTimes[i]) else: d *= -1 total += int(d) return total
def applyAction( state, action, agentIndex ): """ Edits the state to reflect the results of the action. """ legal = AgentRules.getLegalActions( state, agentIndex ) if action not in legal: raise Exception("Illegal action " + str(action)) # Update Configuration agentState = state.data.agentStates[agentIndex] speed = 1.0 # if agentState.isPacman: speed = 0.5 vector = Actions.directionToVector( action, speed ) oldConfig = agentState.configuration agentState.configuration = oldConfig.generateSuccessor( vector ) # Eat next = agentState.configuration.getPosition() nearest = nearestPoint( next ) # TODO limit change agent type(in our case always 1 pacman two ghost) # TODO or just say we think in this way it's fun... if next == nearest: isRed = state.isOnRedTeam(agentIndex) # Change agent type # important for change agent type once on opponent's land agentState.isPacman = [isRed, state.isRed(agentState.configuration)].count(True) == 1 # if he's no longer pacman, he's on his own side, so reset the num carrying timer #agentState.numCarrying *= int(agentState.isPacman) if agentState.numCarrying > 0 and not agentState.isPacman: # TODO score = agentState.numCarrying if isRed else -1*agentState.numCarrying # TODO temp disable one point score per numcarrying score = 0 state.data.scoreChange += score agentState.numReturned += agentState.numCarrying agentState.numCarrying = 0 redCount = 0 blueCount = 0 for index in range(state.getNumAgents()): agentState = state.data.agentStates[index] if index in state.getRedTeamIndices(): redCount += agentState.numReturned else: blueCount += agentState.numReturned #if redCount >= (TOTAL_FOOD/2) - MIN_FOOD or blueCount >= (TOTAL_FOOD/2) - MIN_FOOD: #state.data._win = True if agentState.isPacman and manhattanDistance( nearest, next ) <= 0.9 : AgentRules.consume( nearest, state, state.isOnRedTeam(agentIndex) )
def scoreEvaluation(state): score = state.getScore() position = state.getPacmanPosition() nearest = nearestPoint(position) for index in range(1,len(state.data.agentStates)): if not state.data.agentStates[index].scaredTimer > 0: gstate = state.data.agentStates[index].configuration.getPosition() dis = manhattanDistance(gstate,nearest) if dis <= 3: score -= (20-5*dis) elif dis <= 5: score -= (11-2*dis) return score
def getSuccessor(self, gameState, action, alternateId = None): """ Finds the next successor which is a grid position (location tuple). """ if alternateId: id = alternateId else: id = self.index successor = gameState.generateSuccessor(id, action) pos = successor.getAgentState(id).getPosition() if pos != nearestPoint(pos): # Only half a grid position was covered return successor.generateSuccessor(id, action) else: return successor
def evalState(state): position = state.getPacmanPosition() capsules = state.getCapsules() foodGrid = state.getFood() foodState = position, foodGrid, tuple(capsules) closestFood = None if foodState in closestFoodCache: closestFood = closestFoodCache[foodState] else: nodes = util.Queue() node = position, 0 nodes.push(node) closestFood = findClosestFood(nodes, foodGrid, capsules, state.getWalls()) closestFoodCache[foodState] = closestFood ghostState = position, tuple(ghostPositions) closestGhost = None if ghostState in evalCache: closestGhost = evalCache[ghostState] else: closestGhost = min(mazeDistance(position, util.nearestPoint(ghostPosition), state) for ghostPosition in ghostPositions) evalCache[ghostState] = closestGhost foodLeft = state.getNumFood() score = state.getScore() #scared = newScaredTimes[0] > 0 #cgd = cumGhostsDistance(position, ghostPositions, state) #numFoodEaten = startNumFood - foodLeft # a = 0 # if score < 0: # a = -1 * math.sqrt(abs(score)) # elif score > 0: # a = math.sqrt(score) a = score b = -1 * closestFood c = -1 * foodLeft**2 #d = 0.5 * (( closestGhost**(1.0/4)) + ( 10 * scared * closestGhost)) d = -1 * closestGhost #e = cgd f = 100 * len(capsules) #print 'newScaredTimes', newScaredTimes #print 'score, closestFood, cgd', score, b, e result = a + b + d + f return result
def applyAction(state, action): '''Edits the state to reflect the results of the action.''' legal = PacmanRules.getLegalActions( state ) if action not in legal: raise Exception('Illegal action ' + str(action)) pacmanState = state.data.agentStates[0] # Update Configuration vector = Actions.directionToVector(action, PacmanRules.PACMAN_SPEED) pacmanState.configuration = pacmanState.configuration.generateSuccessor(vector) # Eat next = pacmanState.configuration.getPosition() nearest = nearestPoint(next) if manhattanDistance(nearest, next) <= 0.5: # Remove food PacmanRules.consume(nearest, state)
def getAction(self, gameState): """ Calls chooseAction on a grid position, but continues on half positions. If you subclass CaptureAgent, you shouldn't need to override this method. It takes care of appending the current gameState on to your observation history (so you have a record of the game states of the game) and will call your choose action method if you're in a state (rather than halfway through your last move - this occurs because Pacman agents move half as quickly as ghost agents). """ myState = gameState.getAgentState(self.index) myPos = myState.getPosition() if myPos != nearestPoint(myPos): # We're halfway from one position to the next return gameState.getLegalActions(self.index)[0] else: return self.chooseAction(gameState)
def applyAction( state, action ): """ Edits the state to reflect the results of the action. """ legal = PacmanRules.getLegalActions( state ) if action not in legal: raise Exception("Illegal action " + str(action)) pacmanState = state.data.agentStates[0] # Update Configuration vector = Actions.directionToVector( action, PacmanRules.PACMAN_SPEED ) pacmanState.configuration = pacmanState.configuration.generateSuccessor( vector ) # Eat next = pacmanState.configuration.getPosition() nearest = nearestPoint( next ) PacmanRules.lightSwitches(state)
def getAction(self, gameState): """ Calls chooseAction on a grid position, but continues on half positions. This method also cedes some processing time to the distance calculator, which computes the shortest path distance between all pairs of points. """ # Give some time to the distance calculator thread distanceCalculator.waitOnDistanceCalculator(self.timeForComputing) self.observationHistory.append(gameState) myState = gameState.getAgentState(self.index) myPos = myState.getPosition() if myPos != nearestPoint(myPos): # We're halfway from one position to the next return gameState.getLegalActions(self.index)[0] else: return self.chooseAction(gameState)
def applyAction( state, action ): """ Edits the state to reflect the results of the action. """ legal = ghWorkerRules.getLegalActions( state ) if action not in legal: raise Exception("Illegal action " + str(action)) ghWorkerState = state.data.agentStates[0] # Update Configuration vector = Actions.directionToVector( action, ghWorkerRules.GHWORKER_SPEED ) ghWorkerState.configuration = ghWorkerState.configuration.generateSuccessor( vector ) # Eat next = ghWorkerState.configuration.getPosition() nearest = nearestPoint( next ) if manhattanDistance( nearest, next ) <= 0.5 : # Remove food ghWorkerRules.consume( nearest, state )
def getAveragedEnemyLocation(self, enemyIndex): xavg = 0 yavg = 0 for pos, prob in self.tracking[enemyIndex].getBeliefDistribution().items(): x, y = pos xavg += x * prob yavg += y * prob avgPoint = util.nearestPoint((xavg, yavg)) # annoying thing because mazeDistance doesn't work if one point is a wall if avgPoint in self.walls: neighbors = list(getNeighbors(avgPoint)) neighbors = [n for n in neighbors if n in self.legalPositions] if len(neighbors) > 0: avgPoint = neighbors[0] else: raise Exception("avg enemy location is wall surrounded by walls") return avgPoint
def actualGhostDistance(gameState, ghostPosition): from game import Directions from game import Actions visited = {} ghostPosition=util.nearestPoint(ghostPosition) curState=startPosition = gameState.getPacmanPosition() fringe = util.FasterPriorityQueue() Hvalue=util.manhattanDistance(startPosition,ghostPosition) curDist=0; priorityVal=Hvalue+curDist fringe.push(tuple((startPosition,curDist)), priorityVal) visited[startPosition] = True walls = gameState.getWalls() foodGrid = gameState.getFood() isFood = lambda(x, y): foodGrid[x][y] #isGhost = lambda(x, y): (x, y) in ghostPositions while not fringe.isEmpty(): curState,curDist = fringe.pop() # if goal state is found return the distance if (curState==ghostPosition): #print "returned: %d" % curDist return (curState, curDist) break # if you find a ghost before you find your closest food!! you are screwed =( #if (isGhost(curState)): # ghostInRange = True for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: x,y = curState dx, dy = Actions.directionToVector(action) nextx, nexty = int(x + dx), int(y + dy) nextState = (nextx, nexty) if ((not walls[nextx][nexty]) and (nextState not in visited)): newcurDist = curDist + 1 priorityVal=util.manhattanDistance(curState,ghostPosition)+newcurDist visited[nextState] = True fringe.push(tuple((nextState,newcurDist)), priorityVal) return (curState,curDist)
def actualGhostDistance(gameState, ghostPosition): from game import Directions from game import Actions visited = {} ghostPosition = util.nearestPoint(ghostPosition) curState = startPosition = gameState.getPacmanPosition() fringe = util.FasterPriorityQueue() Hvalue = util.manhattanDistance(startPosition, ghostPosition) curDist = 0 priorityVal = Hvalue + curDist fringe.push(tuple((startPosition, curDist)), priorityVal) visited[startPosition] = True walls = gameState.getWalls() foodGrid = gameState.getFood() isFood = lambda (x, y): foodGrid[x][y] while not fringe.isEmpty(): curState, curDist = fringe.pop() # terminal test: if the position has a ghost if curState == ghostPosition: return (curState, curDist) break for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: x, y = curState dx, dy = Actions.directionToVector(action) nextx, nexty = int(x + dx), int(y + dy) nextState = (nextx, nexty) if (not walls[nextx][nexty]) and (nextState not in visited): newcurDist = curDist + 1 priorityVal = util.manhattanDistance(nextState, ghostPosition) + newcurDist visited[nextState] = True fringe.push(tuple((nextState, newcurDist)), priorityVal) return (curState, curDist)
def decrementTimer( ghostState): timer = ghostState.scaredTimer if timer == 1: ghostState.configuration.pos = nearestPoint( ghostState.configuration.pos ) ghostState.scaredTimer = max( 0, timer - 1 )
def chooseAction(self, gameState): ''' Choose action according to the state whether the agent is chased. If it is chased, use WA* back to safe points Otherwise, eating food until getting 18 food Specially, if the capsule is eaten within safe time, eat food normally ''' currentPosition = util.nearestPoint( gameState.getAgentPosition(self.index)) enemies = [ gameState.getAgentState(i) for i in self.getOpponents(gameState) ] currentObserveDefenders = [ util.nearestPoint(a.getPosition()) for a in enemies if not a.isPacman and a.getPosition() != None ] ''' Expected defenders position ''' for opponentPosition in self.observedDefender.copyList(): if opponentPosition not in currentObserveDefenders: self.observedDefender.remove(opponentPosition) for cod in currentObserveDefenders: self.observedDefender.push(cod) expectedDefenders = self.observedDefender.copyList() self.opponents = set( self.getDefenderBlockingPosition(gameState, expectedDefenders)) ''' Observed defenders position and state within safe distance ''' distanceToDefenders = [ self.getMazeDistance(currentPosition, d) for d in currentObserveDefenders ] cloestDefender, cloestDistanceToDefender = self.findCloestPositionAndDistance( currentObserveDefenders, currentPosition) if cloestDistanceToDefender > self.safeDistance: cloestDefender = None cloestDefenderState = None for a in enemies: if cloestDefender is not None and a.getPosition( ) == cloestDefender: cloestDefenderState = a break ''' Record current position in history ''' self.observeCurrentPosition.push(currentPosition) if self.beingChased and cloestDefender is None or \ cloestDefenderState is not None and cloestDefenderState.scaredTimer > self.safeTimer: ''' In capsule power time ''' self.beingChased = False self.opponents = set() elif (not self.beingChased and self.isReadyOffensing(gameState.getAgentPosition(self.index)) and cloestDefender is not None and min(distanceToDefenders) < self.safeDistance): self.beingChased = True #print(self.opponents) ''' counting food I ate ''' myScore = gameState.getAgentState(self.index).numCarrying ''' Initialize returnFlag ''' if currentPosition in self.safePoints.asList(): self.returnFlag = False ''' Number of food left ''' numOfFood = len(self.getFood(gameState).asList()) ''' Adjust my food ''' if currentPosition == self.start: distanceToFoods = self.getFoodSortedByDistance( currentPosition, gameState) self.myFood = [ foods[0] for i, foods in enumerate(distanceToFoods) if i >= self.minDistanceFoodStart ] self.myFood = [ food for food in self.myFood if food in self.getFood(gameState).asList() ] if len(self.myFood) == 0: self.myFood = self.getFood(gameState).asList() ''' Action ''' if numOfFood < 3: ''' Number of food is smaller than 3 ''' self.goal = self.borderPoints.asList() elif self.beingChased or self.returnFlag: ''' Chased by defender ''' self.goal = self.safePoints.asList() self.goal.extend(self.getCapsules(gameState)) elif self.getScore(gameState) < 0 and myScore + self.getScore( gameState) > self.catchup: #or myScore >= self.returnFood: ''' Come back if I ate a lot or I can reverse score ''' self.goal = self.borderPoints.asList() else: positionCounter = Counter(self.observeCurrentPosition.list) numRepeat = positionCounter[currentPosition] - self.repeatLimit if numRepeat <= 0: self.goal = self.myFood elif numRepeat >= self.numHistoryObserve // 2 - self.repeatLimit or numRepeat >= numOfFood - 1: ''' Repeat too much and return to safe points ''' self.goal = self.safePoints.asList() self.returnFlag = True else: ''' Pick another goal ''' minStart = (self.minDistanceFoodStart + random.randint( numRepeat, numOfFood - 1)) % (numOfFood) distanceToFoods = self.getFoodSortedByDistance( currentPosition, gameState) self.goal = [ foods[0] for i, foods in enumerate(distanceToFoods) if i >= minStart ] if len(self.goal) == 0: self.goal = self.safePoints.asList() actions = self.waStarSearch(gameState, self.heuristicMin, 10) if actions is None or len(actions) == 0: randomActions = self.getLegalActions(gameState) if len(randomActions) > 1 and Directions.STOP in randomActions: randomActions.remove(Directions.STOP) return random.choice(self.getLegalActions(gameState)) return actions[0]
def getSuccessor(self, gameState, action): successor = gameState.generateSuccessor(action) pos = successor.getAgentState(self.index).getPosition() if pos != util.nearestPoint(pos): return successor.generateSuccessor(action) return successor
def betterEvaluationFunction(currentGameState): """ Your extreme, unstoppable evaluation function (problem 4). DESCRIPTION: The idea behind the below is as follows. Losing is always avoided, significantly, by heavily penalizing all such states. Winning is heavily rewarded (above just a simple score), so that PacMan always tries to win. For everything else, we use BFS to compute distances to food items, scared ghosts, and normal ghosts from pacman. We then use a simply formula (arrived at after experimentation) to combine these features into a single store. Intuitively, here are the properties we're generally looking for: 1. A higher game score implies a higher evaluation score. 2. Closer food items imply higher scores. 3. A lower number of capsules implies a higher score. 4. A lower number of food items left implies a higher score. 5. Closer scared ghosts imply a higher score (because we can eat them) 6. Further active ghosts imply a higher score (because they won't eat us) """ # BEGIN_YOUR_CODE (our solution is 26 lines of code, but don't worry if you deviate from this) SCARED_TIMER = 40 walls = currentGameState.getWalls() food = currentGameState.getFood() capsules = set(currentGameState.getCapsules()) scaredGhosts = {util.nearestPoint(ghost.getPosition()) : ghost.scaredTimer for ghost in currentGameState.getGhostStates() if ghost.scaredTimer} adversarialGhosts = {ghost.getPosition() for ghost in currentGameState.getGhostStates() if not ghost.scaredTimer} def greedyApproachCost(pacmanPos, eaten, reward=10, is_edible=lambda x,y: food[x][y], maxRestarts=None): """If we were to take a greedy route, ignoring ghosts, what is our cost""" def end(): if len(eaten.values()) > 0: costs, rewards = zip(*eaten.values()) return sum(costs) - sum(rewards) return 0 if maxRestarts is not None and maxRestarts == 0: return end() queue = util.Queue() queue.push((pacmanPos, 0)) visited = {} while not queue.isEmpty(): (x,y), distance = queue.pop() if (x,y) not in eaten and is_edible(x,y): eaten[(x,y)] = (distance, reward) return greedyApproachCost(pacmanPos=(x,y), eaten=eaten, reward=reward, is_edible=is_edible, maxRestarts=maxRestarts if maxRestarts is None else maxRestarts - 1) visited[(x,y)] = distance actions = Actions.getLegalNeighbors((x,y), walls) random.shuffle(actions) for neighbor in actions: if neighbor not in visited: queue.push((neighbor, distance + 1)) return end() def minDistanceToAllReachablePoints(pos): """Basically BFS to find how many steps it'll take to get places. Returns a function that given a loc returns the minimum distance from pos to loc. """ queue = util.Queue() queue.push((pos, 0)) visited = {} while not queue.isEmpty(): curr, distance = queue.pop() visited[curr] = distance for neighbor in Actions.getLegalNeighbors(curr, walls): if neighbor not in visited: queue.push((neighbor, distance + 1)) return lambda loc: visited[util.nearestPoint(loc)] # distanceFunction = lambda loc: util.manhattanDistance(pos, loc) distanceFunction = minDistanceToAllReachablePoints(currentGameState.getPacmanPosition()) def loseEvaluationScore(): if not adversarialGhosts: return -1000 closestGhost = min([distanceFunction(pos) for pos in adversarialGhosts]) # The closer the ghost, the more we want this state. return -closestGhost def winEvaluationScore(): greedyScore = scoreEvaluationFunction(currentGameState) greedyScore += sum({200 - distanceFunction(pos) for pos in scaredGhosts}) # greedyScore += 30*len(currentGameState.getCapsules()) #if len(adversarialGhosts) == 2: # Punish if the ghosts get too close to each other. # ghosts = list(adversarialGhosts) # greedyScore -= (greedyScore / 10.0) / (1 + minDistanceToAllReachablePoints(ghosts[0])(ghosts[1])) greedyScore -= greedyApproachCost(pacmanPos=currentGameState.getPacmanPosition(), eaten={}, reward=10, maxRestarts=None) # Look ahead one level to see if we'll win. score = 0 if greedyScore > 1000: for action in currentGameState.getLegalActions(0): nextState = currentGameState.generateSuccessor(0, action) if nextState.isWin(): score += 500 return greedyScore + score # Compute an upperbound on our score. maxFromFood = 10 * food.count() maxForWin = 500 maxForKillingGhosts = 400*len(currentGameState.getCapsules()) + 200*len(scaredGhosts) maxPossibleScore = scoreEvaluationFunction(currentGameState) + maxFromFood + maxForWin + maxForKillingGhosts # We know we can normally do well. Decide if we want to win our lose at this point. return winEvaluationScore()
def makeObservation(self, gameState): DIFFERENCE = 32 MY_FOOD_SIGN = 10 FOOD_SIGN = MY_FOOD_SIGN + DIFFERENCE MY_CAPSULE_SIGN = 20 CAPSULE_SIGN = MY_CAPSULE_SIGN + DIFFERENCE GHOST_ME = 71 GHOST_TEAMMATE = GHOST_ME + DIFFERENCE GHOST_ENEMY = GHOST_TEAMMATE + DIFFERENCE PACMAN_ME = 80 PACMAN_TEAMATE = PACMAN_ME + DIFFERENCE PACMAN_ENEMY = PACMAN_TEAMATE + DIFFERENCE SCARED_ME = 87 SCARED_TEAMATE = SCARED_ME + DIFFERENCE SCARED_DEFENDER = SCARED_TEAMATE + DIFFERENCE gameStateStr = str(gameState.deepCopy()) gameStateStr = gameStateStr[:gameStateStr. rfind('\n', 0, len(gameStateStr) - 1)] gameStateStr = gameStateStr.replace("\n", "") gameStateStr = gameStateStr.replace(' ', chr(0)) gameStateList = list(gameStateStr) gameStateList = [ord(c) for c in gameStateList] gameStateList = np.array(gameStateList, dtype=np.float32) gameStateList = gameStateList.reshape(gameState.data.layout.height, gameState.data.layout.width) myFoodList = self.getFoodYouAreDefending(gameState).asList() for x, y in myFoodList: gameStateList[gameState.data.layout.height - 1 - y][x] = MY_FOOD_SIGN foodList = self.getFood(gameState).asList() for x, y in foodList: gameStateList[gameState.data.layout.height - 1 - y][x] = FOOD_SIGN myCapsuleList = self.getCapsulesYouAreDefending(gameState) for x, y in myCapsuleList: gameStateList[gameState.data.layout.height - 1 - y][x] = MY_CAPSULE_SIGN capsuleList = self.getCapsules(gameState) for x, y in capsuleList: gameStateList[gameState.data.layout.height - 1 - y][x] = CAPSULE_SIGN enemies = [ gameState.getAgentState(i) for i in self.getOpponents(gameState) ] for a in enemies: if not a.isPacman and a.getPosition() != None: if a.scaredTimer > 0: x, y = util.nearestPoint(a.getPosition()) gameStateList[gameState.data.layout.height - 1 - y][x] = SCARED_DEFENDER else: x, y = util.nearestPoint(a.getPosition()) gameStateList[gameState.data.layout.height - 1 - y][x] = GHOST_ENEMY elif a.isPacman and a.getPosition() != None: x, y = util.nearestPoint(a.getPosition()) gameStateList[gameState.data.layout.height - 1 - y][x] = PACMAN_ENEMY teams = [ gameState.getAgentState(i) for i in self.getTeam(gameState) if i != self.index ] teammate = teams[0] me = gameState.getAgentState(self.index) if not me.isPacman and me.getPosition() != None: if me.scaredTimer > 0: x, y = util.nearestPoint(me.getPosition()) gameStateList[gameState.data.layout.height - 1 - y][x] = SCARED_ME else: x, y = util.nearestPoint(me.getPosition()) gameStateList[gameState.data.layout.height - 1 - y][x] = GHOST_ME elif me.isPacman and me.getPosition() != None: x, y = util.nearestPoint(me.getPosition()) gameStateList[gameState.data.layout.height - 1 - y][x] = PACMAN_ME if not teammate.isPacman and teammate.getPosition() != None: if teammate.scaredTimer > 0: x, y = util.nearestPoint(teammate.getPosition()) gameStateList[gameState.data.layout.height - 1 - y][x] = SCARED_TEAMATE else: x, y = util.nearestPoint(teammate.getPosition()) gameStateList[gameState.data.layout.height - 1 - y][x] = GHOST_TEAMMATE elif teammate.isPacman and teammate.getPosition() != None: x, y = util.nearestPoint(teammate.getPosition()) gameStateList[gameState.data.layout.height - 1 - y][x] = PACMAN_TEAMATE gameStateList = np.reshape(gameStateList, (1, ) + gameStateList.shape + (1, )) return gameStateList
def value(self, pos): (x, y) = util.nearestPoint(pos) return self.map[x][y]
def decrementTimer(state): timer = state.scaredTimer if timer == 1: state.configuration.pos = nearestPoint(state.configuration.pos) state.scaredTimer = max(0, timer - 1)