我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用util.manhattanDistance()。
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 canKill( pacmanPosition, ghostPosition ): return manhattanDistance( ghostPosition, pacmanPosition ) <= COLLISION_TOLERANCE
def getFurthestCorner(self, pacPos): poses = [(1,1), (1, self.height - 2), (self.width - 2, 1), (self.width - 2, self.height - 2)] dist, pos = max([(manhattanDistance(p, pacPos), p) for p in poses]) return pos
def getDistribution( self, state ): # Read variables from state ghostState = state.getGhostState( self.index ) legalActions = state.getLegalActions( self.index ) pos = state.getGhostPosition( self.index ) isScared = ghostState.scaredTimer > 0 speed = 1 if isScared: speed = 0.5 actionVectors = [Actions.directionToVector( a, speed ) for a in legalActions] newPositions = [( pos[0]+a[0], pos[1]+a[1] ) for a in actionVectors] pacmanPosition = state.getPacmanPosition() # Select best actions given the state distancesToPacman = [manhattanDistance( pos, pacmanPosition ) for pos in newPositions] if isScared: bestScore = max( distancesToPacman ) bestProb = self.prob_scaredFlee else: bestScore = min( distancesToPacman ) bestProb = self.prob_attack bestActions = [action for action, distance in zip( legalActions, distancesToPacman ) if distance == bestScore] # Construct distribution dist = util.Counter() for a in bestActions: dist[a] = bestProb / len(bestActions) for a in legalActions: dist[a] += ( 1-bestProb ) / len(legalActions) dist.normalize() return dist
def noisyDistance(pos1, pos2): return int(util.manhattanDistance(pos1, pos2) + random.choice(SONAR_NOISE_VALUES)) ################################################### # YOUR INTERFACE TO THE PACMAN WORLD: A GameState # ###################################################
def getAction(self, state, agentIndex): # print len(state.data.agentStates) neighbors = Actions.getPossibleNeighborActions(state.data.agentStates[0].getPosition(), 1.0, state.data.layout.obstacles) #print "neighbors", neighbors nearestPursuer = None distanceToPursuer = 999 maxDistance = 0 maxNeighbors = [] for j in range(1, len(state.data.agentStates)): distance = manhattanDistance(state.data.agentStates[0].getPosition(), state.data.agentStates[j].getPosition()) if distance < distanceToPursuer: nearestPursuer = state.data.agentStates[j].getPosition() distanceToPursuer = distance """ for i in range(len(neighbors)): distance = manhattanDistance(neighbors[i], nearestPursuer) if distance > maxDistance: maxNeighbors = [] maxNeighbors.append(neighbors[i]) maxDistance = distance if distance == maxDistance: maxNeighbors.append(neighbors[i]) import random random.shuffle(maxNeighbors) return maxNeighbors[0] """ for i in range(len(neighbors)): distance = manhattanDistance(neighbors[i], nearestPursuer) if distance > maxDistance: maxNeighbors = [] maxNeighbors.append(neighbors[i]) maxDistance = distance if distance == maxDistance: maxNeighbors.append(neighbors[i]) return maxNeighbors[0]
def isWumpusClose(self, state): """ You SHOULD use this function in your implementation. This function checks if the stench of the wumpus can be sensed from the square you are at. """ return util.manhattanDistance(self.wumpus, state) == 1
def isPoisonCapsuleClose(self, state): """ You SHOULD use this function in your implementation. This function checks if the poison from the pills can be sensed from the square you are at. """ return any([util.manhattanDistance(capsule, state) == 1 for capsule in self.capsules])