def getGhostsAndStates(self, state, distance): ''' Get ghosts within a certain distance, will return an array of ghost locations with its status (Whether it is active or not)''' # Get List of Ghosts within certain area. ghosts = api.distanceLimited(self.ghosts, state, distance) # Get List of Ghosts with States in whole map. ghostStates = api.ghostStates(state) # Temporary List to Return Dangerous Ghosts in specified area. dangerousGhosts = [] # Find dangerous ghosts and add to dangerous ghosts list if (len(ghosts) != 0): for ghost in ghosts: for ghostState in ghostStates: if (ghost == ghostState[0]): dangerousGhosts.append(ghostState) return dangerousGhosts
def getAction(self, state): # Update State of World self.updateState(state) # Update Map self.updateMap() # Update utilities self.updateUtilities(state) # Converge while (not self.isEqual()): self.copy() self.updateUtilities(state) ghostlist = api.distanceLimited(self.ghosts, state, 3) myvalue = self.getMap(self.currentLocation[0], self.currentLocation[1])['utility'] mystate = self.currentLocation if (ghostlist): for i in range(len(ghostlist)): if ghostlist[i][1] > self.currentLocation[1] : if Directions.NORTH in self.legal: self.legal.remove(Directions.NORTH) if ghostlist[i][1] < mystate[1] : if Directions.SOUTH in self.legal: self.legal.remove(Directions.SOUTH) if ghostlist[i][0] < mystate[0] : if Directions.WEST in self.legal: self.legal.remove(Directions.WEST) if ghostlist[i][0] > mystate[0]: if Directions.EAST in self.legal: self.legal.remove(Directions.EAST) if len(self.legal) == 2 and Directions.STOP in self.legal: self.legal.remove(Directions.STOP) goto = random.choice(self.legal) else: if (self.getMap(mystate[0]+1, mystate[1])['utility'] != 'W'): myeast = self.getMap(mystate[0]+1, mystate[1]) else: myeast = myvalue if (self.getMap(mystate[0]-1, mystate[1])['utility'] != 'W'): mywest = self.getMap(mystate[0]-1, mystate[1]) else: mywest = myvalue if (self.getMap(mystate[0], mystate[1] + 1)['utility'] != 'W'): mynorth = self.getMap(mystate[0], mystate[1] + 1) else: mynorth = myvalue if (self.getMap(mystate[0], mystate[1] - 1)['utility'] != 'W'): mysouth = self.getMap(mystate[0], mystate[1] - 1) else: mysouth = myvalue mylist = [] if Directions.EAST in self.legal: mylist.append(myeast) if Directions.SOUTH in self.legal: mylist.append(mysouth) if Directions.NORTH in self.legal: mylist.append(mynorth) if Directions.WEST in self.legal: mylist.append(mywest) if len(mylist) == 0: goto = Directions.STOP elif max(mylist) == myeast and Directions.EAST in self.legal: goto = Directions.EAST elif max(mylist) == mysouth and Directions.SOUTH in self.legal: goto = Directions.SOUTH elif max(mylist) == mywest and Directions.WEST in self.legal: goto = Directions.WEST elif max(mylist) == mynorth and Directions.NORTH in self.legal: goto = Directions.NORTH # if pacman cannot make decision, just make a random choice else: goto = random.choice(api.legalActions(state)) return api.makeMove(goto, api.legalActions(state))
def getAction(self, state): #compare previous number of food and current number of food global numoffood if numoffood != len(api.food(state)): numoffood = len(api.food(state)) # print "RRRRRRemakemap,just eat a food!" #Remake maps self.makeMap(state) self.addWallsFoodToMap(state) #self.updateMap(state) # #the new utilitis saved in the map2 in the FUNCTION self.updateMap(state) self.updateMap(state) #calculate the utility for map1, until the utilitis does not change anymore while (self.map1 != self.map2): #print "while map1" #self.map1.prettyDisplay() #print "while map2" #self.map2.prettyDisplay() # so updated utilitis in map1 self.map1 = self.map2 self.updateMap(state) #print "while new map2" #self.map2.prettyDisplay() #print "while map11" #self.map1.prettyDisplay() # get current pacman state and value mystate = api.whereAmI(state) #get my value myvalue = self.map1.getValue(mystate[0], mystate[1]) #Get the actions we can try legal = api.legalActions(state) #get all state of ghosts ghosts = api.ghosts(state) #check if any ghost in my danger area(within 3 step to me) ghostlist = api.distanceLimited(ghosts, state, danger) # if there are ghosts in my danger area if ghostlist: #print "Ghost ARound!!",ghostlist #Check directions of these ghost in the danger area #Remove all direction of these ghost in legal-move-directions of pacman for i in range(len(ghostlist)): if ghostlist[i][1] > mystate[1] : if Directions.NORTH in legal: legal.remove(Directions.NORTH) #print "hear the ghost n" if ghostlist[i][1] < mystate[1] : if Directions.SOUTH in legal: legal.remove(Directions.SOUTH) #print "hear the ghost s" if ghostlist[i][0] < mystate[0] : if Directions.WEST in legal: legal.remove(Directions.WEST) #print "hear the ghost w" if ghostlist[i][0] > mystate[0]: if Directions.EAST in legal: legal.remove(Directions.EAST) #print "hear the ghost e" #check if pacman only have one direction can go # If it is, just go this direction. if len(legal) == 2 and Directions.STOP in legal: legal.remove(Directions.STOP) goto = random.choice(legal) # If pacman have more than one direction can go # let pacman make correct choice where pacman should go else: # if the direction of east is a wall and pacman go east, # but pacman will not move. # therefor the new utilitiy is same with the utilitiy of current(this) position if (self.map1.getValue(mystate[0]+1, mystate[1]) != '%'): myeast = self.map1.getValue(mystate[0]+1, mystate[1]) else: myeast = myvalue if (self.map1.getValue(mystate[0]-1, mystate[1]) != '%'): mywest = self.map1.getValue(mystate[0]-1, mystate[1]) else: mywest = myvalue if(self.map1.getValue(mystate[0], mystate[1]+1) != '%'): mynorth = self.map1.getValue(mystate[0], mystate[1]+1) else: mynorth = myvalue if(self.map1.getValue(mystate[0], mystate[1]-1) != '%'): mysouth = self.map1.getValue(mystate[0], mystate[1]-1) else: mysouth = myvalue #print"Second legal",legal #print "myeast",myeast,"mywest",mywest,"mynorth",mynorth,"mysouth",mysouth #make a list to save all the payoff of legal directions mylist = [] if Directions.EAST in legal: mylist.append(myeast) if Directions.SOUTH in legal: mylist.append(mysouth) if Directions.NORTH in legal: mylist.append(mynorth) if Directions.WEST in legal: mylist.append(mywest) #print "mylist",mylist #if no legal directions, ask pacman stay the same position if len(mylist) == 0: goto = Directions.STOP #find the max payoff is from which legal directions #ask pacman going the direction with biggest payoff elif max(mylist) == myeast and Directions.EAST in legal: goto = Directions.EAST #print"max E" elif max(mylist) == mysouth and Directions.SOUTH in legal: goto = Directions.SOUTH #print"max S" elif max(mylist) == mywest and Directions.WEST in legal: goto = Directions.WEST #print"max W" elif max(mylist) == mynorth and Directions.NORTH in legal: goto = Directions.NORTH #print"max N" # if pacman cannot make decision, just make a random choice else: goto = random.choice(api.legalActions(state)) #print "GGGGGoto",goto return api.makeMove(goto, api.legalActions(state))
def __init__(self, state): # Required information about the world pacmanOrg = api.whereAmI(state) self.walls = api.walls(state) self.caps = api.capsules(state) self.reward = api.food(state) self.loss = api.ghosts(state) if (len(self.loss) > 0): self.loss[0] = (int(self.loss[0][0]), int(self.loss[0][1])) # Ignore scared ghosts for ghost in api.ghostStates(state): if (ghost[0] in self.loss and ghost[1] == 1): self.loss.remove(ghost[0]) # Grid dimentions based on last wall co-ordinate self.x1 = self.walls[len(self.walls) - 1][0] + 1 self.y1 = self.walls[len(self.walls) - 1][1] + 1 self.grid = [[0 for y in range(self.y1)] for x in range(self.x1)] #Intialize default utilities will be used as rewards for x in range(self.x1): for y in range(self.y1): closeGhosts = api.distanceLimited(self.loss, state, 4) if (x, y) in self.walls: self.grid[x][y] = None elif (x, y) in self.loss: self.grid[x][y] = -20 elif (x, y) in self.caps: self.grid[x][y] = 10 # Larger grids elif (self.x1 > 7): # If there are nearby ghosts subtract ghost score divided by distance if (len(closeGhosts) >= 1): if (x, y) in self.reward: self.grid[x][y] = 1 else: self.grid[x][y] = 0 else: if (x, y) in self.reward: self.grid[x][y] = 1 else: self.grid[x][y] = 0 #Smaller Grids else: if (x, y) in self.reward: if (len(self.reward) > 1): if ((x, y) == (1, 1)): self.grid[x][y] = 5 else: self.grid[x][y] = 1 else: self.grid[x][y] = 10 else: self.grid[x][y] = 0 # Override rewards for legal spaces near ghosts if (self.x1 > 7): for y in range(self.y1): for x in range(self.x1): if (x, y) in self.loss: if (x + 1, y) not in self.walls: self.grid[x + 1][y] = -15 if (x + 2, y) not in self.walls and (x + 2) < self.x1: self.grid[x + 2][y] = -10 if (x - 1, y) not in self.walls: self.grid[x - 1][y] = -15 if (x - 2, y) not in self.walls and (x - 2) > 0: self.grid[x - 2][y] = -10 if (x, y + 1) not in self.walls: self.grid[x][y + 1] = -15 if (x, y + 2) not in self.walls and (y + 2) < self.y1: self.grid[x][y + 2] = -10 if (x, y - 1) not in self.walls: self.grid[x][y - 1] = -15 if (x, y - 2) not in self.walls and (y - 2) > 0: self.grid[x][y - 2] = -10 if (x + 1, y + 1) not in self.walls: self.grid[x + 1][y + 1] = -10 if (x + 1, y - 1) not in self.walls: self.grid[x + 1][y - 1] = -10 if (x - 1, y + 1) not in self.walls: self.grid[x - 1][y + 1] = -10 if (x - 1, y - 1) not in self.walls: self.grid[x - 1][y - 1] = -10