def followActionBasis(self, observedState, action): allowedDirections = observedState.getLegalPacmanActions() pacmanPosition = observedState.getPacmanPosition() if action == 'good': target = classifyGhosts.getNearestGoodGhost(observedState, self.distancer) elif action == 'bad': target = classifyGhosts.getNearestBadGhost(observedState, self.distancer) elif action == 'capsule': target = classifyCapsule.closest(observedState, self.distancer) else: raise Exception("Unknown action '%s' passed to action basis" % action) # find the direction that moves closes to target best_action = Directions.STOP best_dist = np.inf for la in allowedDirections: if la == Directions.STOP: continue successor_pos = Actions.getSuccessor(pacmanPosition,la) new_dist = self.distancer.getDistance(successor_pos,target) if new_dist < best_dist: best_action = la best_dist = new_dist return best_action
def chooseAction(self, observedState): """ Pacman will eat the nearest good capsule if the ghost is not scared, and chase the scared ghost otherwise. """ pacmanPos = observedState.getPacmanPosition() ghost_states = observedState.getGhostStates() # states have getPosition() and getFeatures() methods legalActs = [a for a in observedState.getLegalPacmanActions()] # find the closest ghost by sorting the distances closeGhost = ghosts.closestGhost(observedState, self.distancer, good=False) # find the closest bad ghost by sorting the distances badGhost = ghosts.getNearestBadGhost(observedState, self.distancer) # position of closest good capsule to Pacman closeCapsule = capsule.closest(observedState, self.distancer) best_action = random.choice(legalActs) if observedState.scaredGhostPresent(): # targets capsules and good ghosts on the way to the bad ghost target = mapH.subTarget(pacmanPos, badGhost, closeCapsule) target = mapH.subTarget(pacmanPos, target, closeGhost) for dir in mapH.getDirs(pacmanPos, target): if dir in legalActs: return dir else: # sorts out directions that lead to any close ghost legalActs = [a for a in legalActs if a not in mapH.ghostDirs(pacmanPos, closeGhost)] for dir in mapH.getDirs(pacmanPos, closeCapsule): if dir in legalActs: return dir return best_action
def chooseAction(self, observedState): """ Pacman will eat the nearest good capsule if the ghost is not scared, and eat good ghosts otherwise. Along the way, he will adjust his path to run into good ghosts. """ pacmanPos = observedState.getPacmanPosition() legalActs = [a for a in observedState.getLegalPacmanActions()] # find the closest ghost by sorting the distances goodGhost = ghosts.closestGhost(observedState, self.distancer, good=True) # position of closest good capsule to Pacman closeCapsule = capsule.closest(observedState, self.distancer) best_action = random.choice(legalActs) if observedState.scaredGhostPresent(): # targets capsules along the way target = mapH.subTarget(pacmanPos, goodGhost, closeCapsule) for dir in mapH.getDirs(pacmanPos, target): if dir in legalActs: return dir else: # find the closest bad ghost by sorting the distances badGhost = ghosts.getNearestBadGhost(observedState, self.distancer) # sorts out directions that lead to the bad ghost legalActs = [a for a in legalActs if a not in mapH.ghostDirs(pacmanPos, badGhost)] # targets good ghosts along the way target = mapH.subTarget(pacmanPos, closeCapsule, goodGhost) for dir in mapH.getDirs(pacmanPos, closeCapsule): if dir in legalActs: return dir return best_action
def goodBadGhostCapsuleDistances(self,observedState): pacmanPosition = observedState.getPacmanPosition() goodGhost = classifyGhosts.getNearestGoodGhost(observedState, self.distancer) badGhost = classifyGhosts.getNearestBadGhost(observedState, self.distancer) capsule = classifyCapsule.closest(observedState, self.distancer) distBinner = xbinner( \ (0,math.sqrt(observedState.layout.width**2 + observedState.layout.width**2)), \ goodBadGhostCapsuleDistances.buckets ) goodGhostDistance = distBinner(self.distancer.getDistance(pacmanPosition, goodGhost)) badGhostDistance = distBinner(self.distancer.getDistance(pacmanPosition, badGhost)) capsuleDistance = distBinner(self.distancer.getDistance(pacmanPosition, capsule)) hasCapsule = observedState.scaredGhostPresent() return (goodGhostDistance,badGhostDistance,capsuleDistance,int(hasCapsule))
def chooseAction(self, observedState): "Pacman will eat the nearest bad ghost. For testing purposes." pacmanPos = observedState.getPacmanPosition() ghost_states = observedState.getGhostStates() # states have getPosition() and getFeatures() methods legalActs = [a for a in observedState.getLegalPacmanActions()] # position of closest good capsule to Pacman badGhost = ghosts.getNearestBadGhost(observedState, self.distancer) best_action = random.choice(legalActs) for dir in mapH.getDirs(pacmanPos, badGhost): if dir in legalActs: return dir return best_action
def localNeighborhood(self, observedState): radius = 4 pacmanPosition = observedState.getPacmanPosition() xBinner = xbinner( (pacmanPosition[0]-radius, pacmanPosition[0]+radius), localNeighborhood.buckets ) yBinner = xbinner( (pacmanPosition[1]-radius, pacmanPosition[1]+radius), localNeighborhood.buckets ) goodGhost = classifyGhosts.getNearestGoodGhost(observedState, self.distancer) badGhost = classifyGhosts.getNearestBadGhost(observedState, self.distancer) capsule = classifyCapsule.closest(observedState, self.distancer) hasCapsule = observedState.scaredGhostPresent() print pacmanPosition, goodGhost, badGhost, capsule, hasCapsule # bin the x/y position for each object of interest b = [] for p in [goodGhost, badGhost, capsule]: b += [xBinner(p[0]), yBinner(p[1])] return tuple(b + [int(hasCapsule)])
def ghostPosition(self,observedState): pacmanPosition = observedState.getPacmanPosition() ghost_positions = [ classifyGhosts.getNearestGoodGhost(observedState, self.distancer), classifyGhosts.getNearestBadGhost(observedState, self.distancer)] bins = 5 xBinner = xbinner( (0,observedState.layout.width), bins ) yBinner = xbinner( (0,observedState.layout.height), bins ) state = (xBinner(pacmanPosition[0]), yBinner(pacmanPosition[1])) for pos in ghost_positions: state += (xBinner(pos[0]),yBinner(pos[1])) return state