Esempio n. 1
0
    def getAction(self,state):
        walls = api.walls(state)
        width,height = api.corners(state)[-1]
        legal = api.legalActions(state)
        me = api.whereAmI(state)
        food = api.food(state)
        ghosts = api.ghosts(state)
        capsules = api.capsules(state)
        direction = Directions.STOP
        x, y = me

        if not hasattr(self, 'map'):
            self.createMap(walls, width + 1, height + 1)

        self.checkForCapsules(capsules, legal, ghosts)
        legal = self.solveLoop(ghosts, legal)

        if len(ghosts):
            self.memorizeGhosts(ghosts)
            if self.counter < 0:
                for ghost in ghosts:
                    legal = self.checkForGhosts(ghost, me, legal)

        direction = self.pickMove(me, legal, width  + 1, height + 1, food)
        self.updatePosition(me, 1, self.map)


        self.printMap(self.map)


        self.last = direction
        return direction
Esempio n. 2
0
    def initialize(self, state):

        # get location of all visible food
        foods = api.food(state)
        #get location of all corners
        corners = api.corners(state)
        #get location of all visible capsules
        capsules = api.capsules(state)
        # Get the actions we can try, and remove "STOP" if that is one of them.
        legal = api.legalActions(state)
        #get location of all visible walls
        walls = api.walls(state)
        #get pacmans position
        pacman = api.whereAmI(state)
        x = pacman[0]
        y = pacman[1]

        if self.map == None:
            width = 0
            height = 0
            for corner in corners:
                if corner[0] > width:
                    width = corner[0]
                if corner[1] > height:
                    height = corner[1]
            self.map = [["?" for y in range(height)] for x in range(width)]
            for wall in walls:
                self.map[wall[0]][wall[1]] = "W"
            for food in foods:
                self.map[food[0]][food[1]] = "F"
            for capsule in capsules:
                self.map[capsule[0]][capsule[1]] = "F"
            self.map[x][y] = "0"

        self.init = True
    def valueIterationSmall(self, state, reward, gamma, V1):
        # Similar to valueIteration function
        # does not calculate buffers around ghosts (cause it would be too small)
        # meant for maps smaller than 10 x 10

        corners = api.corners(state)
        walls = api.walls(state)
        food = api.food(state)
        ghosts = api.ghosts(state)
        capsules = api.capsules(state)

        maxWidth = self.getLayoutWidth(corners) - 1
        maxHeight = self.getLayoutHeight(corners) - 1

        if not (0 < gamma <= 1):
            raise ValueError("MDP must have a gamma between 0 and 1.")

        # Implement Bellman equation with 10-loop iteration
        # Since smaller maps do not require as big of a value iteration loop
        loops = 100
        while loops > 0:
            V = V1.copy()  # This will store the old values
            for i in range(maxWidth):
                for j in range(maxHeight):
                    # Exclude any food because in this case it is the terminal state
                    if (i, j) not in walls and (i, j) not in food and (
                            i, j) not in ghosts and (i, j) not in capsules:
                        V1[(i,
                            j)] = reward + gamma * self.getTransition(i, j, V)
            loops -= 1
Esempio n. 4
0
    def getAction(self, state):
        """
            The function to work out next intended action carried out.

            Parameters:
                None

            Returns:
                Directions: Intended action that Pacman will carry out.
        """
        current_pos = api.whereAmI(state)
        corners = api.corners(state)
        food = api.food(state)
        ghosts = api.ghosts(state)
        ghost_scared_time = api.ghostStatesWithTimes(state)[0][1]
        walls = api.walls(state)
        legal = api.legalActions(state)
        capsules = api.capsules(state)
        protected_coords = walls + ghosts + [current_pos]

        width = max(corners)[0] + 1
        height = max(corners, key=itemgetter(1))[1] + 1

        board = self.create_board(width, height, -0.04)
        board.set_position_values(food, 1)
        board.set_position_values(walls, 'x')
        board.set_position_values(capsules, 2)

        if ghost_scared_time < 5:
            board.set_position_values(ghosts, -3)

            # for i in range(height):
            #     for j in range(width):
            #         print board[i, j],
            #     print
            # print
            print "GHOST LIST: ", ghosts
            for x, y in ghosts:
                # set the surrounding area around the ghost to half the reward of the ghost
                # avoids changing the reward of the ghost itself, the pacman and the walls
                # print "GHOST Coordinates: " + str(x) + " " + str(y)
                x_coordinates = [x - 1, x, x + 1]
                y_coordinates = [y - 1, y, y + 1]
                # print "X/Y Coordinates: " + str(x_coordinates) + " " + str(y_coordinates)
                for x_coord in x_coordinates:
                    for y_coord in y_coordinates:
                        if (x_coord, y_coord) not in protected_coords:
                            # print("index: " + str((board.convert_y(y_coord), x_coord)))
                            converted_y = board.convert_y(y_coord)
                            # print "VALUE: " + str(board[board.convert_y(y), x])
                            board[converted_y,
                                  x_coord] = board[board.convert_y(y), x] / 2
                            # print "VALUE PART 2: " + str(board[converted_y, x_coord])

        board = self.value_iteration(state, board)
        expected_utility = self.calculate_expected_utility(
            state, board, abs(current_pos[1] - (height - 1)), current_pos[0])
        return max([(utility, action) for utility, action in expected_utility
                    if action in legal])[1]
Esempio n. 5
0
 def updateFoodInMap(self, state):
     # First, make all grid elements thataren't walls with a reward - 0.02.
     map(lambda s: self.map.setValue(s[0], s[1], ('-0.02', 0)), self.states)
     food = api.food(state)
     food += api.capsules(state)
     map(
         lambda i: self.map.setValue(food[i][0], food[i][1],
                                     (self.initialFood / len(food), 0)),
         range(len(food)))
Esempio n. 6
0
    def getAction(self, state):

        # Demonstrates the information that Pacman can access about the state
        # of the game.

        # What are the current moves available
        legal = api.legalActions(state)
        print "Legal moves: ", legal

        # Where is Pacman?
        pacman = api.whereAmI(state)
        print "Pacman position: ", pacman

        # Where are the ghosts?
        print "Ghost positions:"
        theGhosts = api.ghosts(state)
        for i in range(len(theGhosts)):
            print theGhosts[i]

        print "timer"
        moreGhosts = api.ghostStatesWithTimes(state)
        for i in range(len(moreGhosts)):
            print moreGhosts[i]

        # How far away are the ghosts?
        print "Distance to ghosts:"
        for i in range(len(theGhosts)):
            print util.manhattanDistance(pacman, theGhosts[i])

        # Where are the capsules?
        print "Capsule locations:"
        print api.capsules(state)

        # Where is the food?
        print "Food locations: "
        print api.food(state)

        # Where are the walls?
        print "Wall locations: "
        print api.walls(state)

        # getAction has to return a move. Here we pass "STOP" to the
        # API to ask Pacman to stay where they are.
        return api.makeMove(Directions.STOP, legal)
Esempio n. 7
0
    def getAction(self, state):
        """
            The function to work out next intended action carried out.
            Parameters:
                None
            Returns:
                Directions: Intended action that Pacman will carry out.
        """
        current_pos = api.whereAmI(state)
        food = api.food(state)
        # make sure all ghost coordinates are ints rather than floats
        ghosts = [(int(x), int(y)) for x, y in api.ghosts(state)]
        legal = api.legalActions(state)
        capsules = api.capsules(state)

        food_multiplier = (
            (0.8 * len(food) / float(self.initial_num_food))**2) + 6
        ghost_multiplier = (
            (0.2 * len(food) / float(self.initial_num_food))**2) + 3

        board = Board(self.width, self.height, -0.04)
        board.set_position_values(self.walls, 'x')
        board.set_position_values(capsules, 2 * food_multiplier)
        board.set_position_values(food, 1 * food_multiplier)
        board.set_position_values(ghosts, -7 * ghost_multiplier)

        # rewards of ghosts, walls and current position cannot be overridden
        protected_pos = set(ghosts + self.walls + [current_pos])

        # setting a much more negative reward for potential positions ghosts can occupy
        # in two moves.
        for ghost in ghosts:
            # loop through potential positions that the ghost can occupy if it were
            # to move now
            for pos in self.get_next_pos(ghost):
                if pos not in protected_pos:
                    # set the reward value of surrounding positions of ghosts to -6 *
                    # ghost multiplier.
                    board[int(board.convert_y(pos[1])),
                          int(pos[0])] = -6 * ghost_multiplier
                    for position in self.get_next_pos(pos):
                        # loop through potential positions that the ghost can occupy if
                        # it were to move two times.
                        if position not in protected_pos:
                            board[int(board.convert_y(position[1])),
                                  int(position[0])] = -6 * ghost_multiplier

        board = self.value_iteration(state, board)  # call value iteration

        expected_utility = self.calculate_expected_utility(
            state, board, board.convert_y(current_pos[1]), current_pos[0])

        # returns action associated to the max utility out of all the legal actions.
        return api.makeMove(
            max([(utility, action) for utility, action in expected_utility
                 if action in legal])[1], legal)
Esempio n. 8
0
    def __update_positions(self, state):
        '''
        Repaints the grid according to the passed in state. Updating the 
        position of pacman, the ghosts, the food, the capsules, and the blank
        spaces.

        In addition calculates the number of filled spaces and stores this value
        statically on Point, for later use in the reward function.

        Args:
            state: Current game state.
        '''
        Grid.FILL_COUNT = 0

        points = {
            Dispositions.FOOD: api.food(state),
            Dispositions.CAPSULE: api.capsules(state),
            Dispositions.GHOST_HOSTILE: [
                ghost
                for ghost, time in api.ghost_states_with_times(state)
                if time <= Grid.GHOST_SAFE_TIME
            ],
            Dispositions.GHOST_EDIBLE: [
                ghost
                for ghost, time in api.ghost_states_with_times(state)
                if time > Grid.GHOST_SAFE_TIME
            ],
        }

        for disposition, coordinates in points.iteritems():
            for x, y in coordinates:
                if disposition in {Dispositions.FOOD, Dispositions.CAPSULE}:
                    Grid.FILL_COUNT += 1
                coordinate = Coordinate(x, y)  # because ghost x, y are floats
                self[coordinate].disposition = disposition

        for coordinate, point in self:
            point.min_ghost_distance = Point.min_distance(
                coordinate, points[Dispositions.GHOST_HOSTILE]
            )

        MDPAgent.set_gamma(len(api.food(state) + api.capsules(state)))
Esempio n. 9
0
    def getAction(self, state):

        pacman = api.whereAmI(state)
        x = pacman[0]
        y = pacman[1]

        food = api.food(state)

        capsule = api.capsules(state)
        # Get the actions we can try, and remove "STOP" if that is one of them.
        legal = state.getLegalPacmanActions()

        walls = api.walls(state)

        q = Queue.Queue()

        if Directions.STOP in legal:
            legal.remove(Directions.STOP)

        if Directions.WEST in legal:
            #print "West"
            q.put(((x-1,y), Directions.WEST))
        if Directions.EAST in legal:
            #print "East"
            q.put(((x+1,y), Directions.EAST))
        if Directions.NORTH in legal:
            #print "North"
            q.put(((x,y+1), Directions.NORTH))
        if Directions.SOUTH in legal:
            #print "South"
            q.put(((x,y-1), Directions.SOUTH))

        #print food
        #print q.toString()

        while not q.empty():
            possible = q.get()
            position = possible[0]
            x = position[0]
            y = position[1]
            print position
            if position in (food or capsules):
                print "moving"
                return api.makeMove(possible[1], legal)
            else:
                #print "searching"
                if (x-1,y) not in walls:
                    q.put(((x-1,y), possible[1]))
                if (x+1,y) not in walls:
                    q.put(((x+1,y), possible[1]))
                if (x,y+1) not in walls:
                    q.put(((x,y+1), possible[1]))
                if (x,y-1) not in walls:
                    q.put(((x,y-1), possible[1]))
Esempio n. 10
0
    def updateMap(self, state):
        ghost = api.ghosts(state)
        ghostStates = api.ghostStates(state)
        capsule = api.capsules(state)
        food = api.food(state)
        wall = api.walls(state)

        for node in self.mapDictionary:
            if node in wall:
                self.mapDictionary.update({node: 'X'})

        return self.mapDictionary
Esempio n. 11
0
    def initialize(self, state):
        #sets the reward of each grid
        self.reward = None
        #set the utility of each grid
        self.utility = None
        # get location of all visible food
        foods = api.food(state)
        #get location of all corners
        corners = api.corners(state)
        #get location of all visible capsules
        capsules = api.capsules(state)
        #get location of all visible walls
        walls = api.walls(state)
        #get pacmans position
        pacman = api.whereAmI(state)
        #pacman's x position
        pacmanX = pacman[0]
        #pacman's y position
        pacmanY = pacman[1]

        #if the internal map has not been initialized
        if self.reward == None and self.utility == None:
            #finds the dimension of the map by location the extremes, in this case the corners
            width = 0
            height = 0
            for corner in corners:
                if corner[0] > width:
                    width = corner[0]
                if corner[1] > height:
                    height = corner[1]

            #once the size of the map has been identified, initialize the rewards of each position with the approriate value
            self.reward = [[self.baseReward for y in range(height+1)] for x in range(width+1)]
            #do the same with the utility, however with random values between 0 and 1
            self.utility = [[random() for y in range(height+1)] for x in range(width+1)]
            #now add in all the information pacman knows initially. starting with all known locations of food
            for food in foods:
                #set the reward of food to the value defined above
                self.reward[food[0]][food[1]] = self.foodReward
                #self.utility[food[0]][food[1]] = self.foodReward
            #set the reward of capsules with the reward defined above
            for capsule in capsules:
                self.reward[capsule[0]][capsule[1]] = self.capsuleReward
                #self.utility[capsule[0]][capsule[1]] = self.capsuleReward

            #now mark the location of the walls on the map, using "W"
            for wall in walls:
                self.reward[wall[0]][wall[1]] = "W"
                self.utility[wall[0]][wall[1]] = "W"

        #set init to true as the map has been initialized
        self.init = True
Esempio n. 12
0
 def addConsumablesToMap(self, state):
     # First, make all grid elements that aren't walls blank.
     for i in range(self.map.getWidth()):
         for j in range(self.map.getHeight()):
             if self.map.getValue(i, j) != '%':
                 self.map.setValue(i, j, 0)
     food = api.food(state)
     for i in range(len(food)):
         self.map.setValue(food[i][0], food[i][1], 5)
     bigPillEnergy = api.capsules(state)
     for i in range(len(bigPillEnergy)):
         self.map.setValue(bigPillEnergy[i][0],bigPillEnergy[i][1],15)
     self.dankMap = self.map
Esempio n. 13
0
    def updateMap(self, state):

        corners = api.corners(state)
        ghosts = api.ghosts(state)
        me = api.whereAmI(state)
        walls = api.walls(state)
        capsules = api.capsules(state)
        foods = api.food(state)

        # Width and Height of Map
        width, height = corners[3][0] + 1, corners[3][1] + 1

        # Generate empty world Map
        if (self.worldMap is None):
            self.worldMap = [[[' ', self.emptyReward, 0, Directions.STOP]
                              for x in range(width)] for y in range(height)]

            self.setMap(me[0], me[1], ['M', self.meReward, 0, Directions.STOP])
            for food in foods:
                self.setMap(food[0], food[1],
                            ['F', self.foodReward, 0, Directions.STOP])
            for capsule in capsules:
                self.setMap(capsule[0], capsule[1],
                            ['C', self.capsuleReward, 0, Directions.STOP])
            for wall in walls:
                self.setMap(wall[0], wall[1],
                            ['W', self.wallReward, 0, Directions.STOP])
            for ghost in ghosts:
                self.setMap(ghost[0], ghost[1],
                            ['G', self.ghostReward, 0, Directions.STOP])

        else:
            self.clearMapKeepState(state)
            self.setThing(me[1], me[0], 'M')
            for food in foods:
                self.setThing(food[1], food[0], 'F')
            for capsule in capsules:
                self.setThing(capsule[1], capsule[0], 'C')
            for wall in walls:
                self.setThing(wall[1], wall[0], 'W')
            for ghost in ghosts:
                self.setThing(ghost[1], ghost[0], 'G')
            self.setReward(me[0], me[1], self.meReward)
            for food in foods:
                self.setReward(food[0], food[1], self.foodReward)
            for capsule in capsules:
                self.setReward(capsule[0], capsule[1], self.capsuleReward)
            for wall in walls:
                self.setReward(wall[0], wall[1], self.wallReward)
            for ghost in ghosts:
                self.setReward(ghost[0], ghost[1], self.ghostReward)
Esempio n. 14
0
    def getAction(self, state):
        legal = api.legalActions(state)

        if Directions.STOP in legal:
            legal.remove(Directions.STOP)

        coners = api.corners(state)

        pacman = api.whereAmI(state)

        food = api.food(state)

        Capsules = api.capsules(state)

        ghosts = api.ghosts(state)

        if len(self.detected) == 0:
            self.states.push((pacman, Directions.STOP))

        if not self.states.isEmpty():
            self.detected.append(pacman)
            success_node = []
            for directs in legal:
                if directs == Directions.WEST:
                    success_node.append((pacman[0] - 1, pacman[1]))
                if directs == Directions.EAST:
                    success_node.append((pacman[0] + 1, pacman[1]))
                if directs == Directions.NORTH:
                    success_node.append((pacman[0], pacman[1] + 1))
                if directs == Directions.SOUTH:
                    success_node.append((pacman[0], pacman[1] - 1))
            for index in range(len(success_node)):
                if not success_node[index] in self.detected and (
                        success_node[index] in food
                        or success_node[index] in Capsules):
                    self.states.push((success_node[index], legal[index]))
                    return (api.makeMove(legal[index], legal))

            last, acted = self.states.pop()
            if acted == Directions.NORTH:
                return (api.makeMove(Directions.SOUTH, legal))
            if acted == Directions.SOUTH:
                return (api.makeMove(Directions.NORTH, legal))
            if acted == Directions.WEST:
                return (api.makeMove(Directions.EAST, legal))
            if acted == Directions.EAST:
                return (api.makeMove(Directions.WEST, legal))

        return (api.makeMove(Directions.STOP, legal))
Esempio n. 15
0
    def addFoodInMap(self, state):
        # First, make all grid elements that aren't walls with a reward - 0.02.
        [
            self.map.setValue(i, j, ('-0.02', 0))
            for i in range(self.map.getWidth())
            for j in range(self.map.getHeight())
        ]

        food = api.food(state)
        food += api.capsules(state)
        self.initialFood = len(food)
        map(
            lambda i: self.map.setValue(food[i][0], food[i][1],
                                        (self.initialFood, 0)),
            range(len(food)))
Esempio n. 16
0
    def initialize(self, state):
        #print "initializing map"
        #sets the path of pacman to be empty
        self.path = []
        #sets the internal map of pacman to be empty
        self.map = None
        # get location of all visible food
        foods = api.food(state)
        #get location of all corners
        corners = api.corners(state)
        #get location of all visible capsules
        capsules = api.capsules(state)
        # Get the actions we can try, and remove "STOP" if that is one of them.
        #get location of all visible walls
        walls = api.walls(state)
        #get pacmans position
        pacman = api.whereAmI(state)
        pacmanX = pacman[0]
        pacmanY = pacman[1]

        #if the internal map has not been initialized
        if self.map == None:
            #finds the dimension of the map by location the extremes, in this case the corners
            width = 0
            height = 0
            for corner in corners:
                if corner[0] > width:
                    width = corner[0]
                if corner[1] > height:
                    height = corner[1]

            #once the size of the map has been identified, fill it up with "?", as pacman does not know what is in there
            self.map = [["?" for y in range(height+1)] for x in range(width+1)]
            #now add in all the information pacman knows initially. starting with all known locations of food
            for food in foods:
                #use "F" to mark food on the map
                self.map[food[0]][food[1]] = "F"
            #now mark the location of capsules on the map, this time using "C"
            for capsule in capsules:
                self.map[capsule[0]][capsule[1]] = "C"
            #now mark the location of the walls on the map, using "W"
            for wall in walls:
                self.map[wall[0]][wall[1]] = "W"
            #last pacman knows where it is, so mark that as "P"
            self.map[pacmanX][pacmanY] = "P"

        #set init to true as the map has been initialized
        self.init = True
Esempio n. 17
0
    def bellmanUpdate(
        self, state
    ):  #preforms bellmans over the entire map using bellmans equation on each cell
        ghostList = api.ghosts(state)
        foodList = api.food(state)
        wallList = api.walls(state)
        scaredTime = api.ghostStatesWithTimes(state)[0][1]
        capsuleList = api.capsules(state)
        width = self.map.width
        height = self.map.height
        done = False

        while not done:  #loops over map preforming bellmans until previous map from the last iteration is equal to the map at the end of the new iteration
            oldMap = deepcopy(self.map)

            for x in range(0, width):
                for y in range(0, height):
                    if oldMap[x][y] != None:
                        bestUtil = -1000
                        moves = [
                            oldMap[x][y + 1], oldMap[x + 1][y],
                            oldMap[x][y - 1], oldMap[x - 1][y]
                        ]  # list of all possible moves from the current cell

                        for i in range(
                                len(moves)
                        ):  # finds the best util possible based on all legal moves to uses in value iteration
                            if moves[i] != None:
                                tutil = moves[i] * 0.8
                                if moves[i - 1] != None:
                                    tutil += moves[i - 1] * 0.1
                                else:
                                    tutil += oldMap[x][y] * 0.1
                                if moves[(i + 1) % 4] != None:
                                    tutil += moves[(i + 1) % 4] * 0.1
                                else:
                                    tutil += oldMap[x][y] * 0.1
                                if tutil > bestUtil:
                                    bestUtil = deepcopy(tutil)

                        self.map[x][y] = (bestUtil * 0.9) + self.reward(
                            x, y, state, ghostList, foodList, capsuleList,
                            scaredTime,
                            wallList)  #bellmans equation using rewards functon

            done = self.checkSame(
                oldMap, self.map
            )  #checks to see whether old map is the same as new map
Esempio n. 18
0
    def initialize(self, state):
        #sets the reward of each grid
        self.reward = None
        #set the utility of each grid
        self.utility = None
        # get location of all visible food
        foods = api.food(state)
        #get location of all corners
        corners = api.corners(state)
        #get location of all visible capsules
        capsules = api.capsules(state)
        #get location of all visible walls
        walls = api.walls(state)
        #get pacmans position
        pacman = api.whereAmI(state)
        pacmanX = pacman[0]
        pacmanY = pacman[1]

        #if the internal map has not been initialized
        if self.reward == None and self.utility == None:
            #finds the dimension of the map by location the extremes, in this case the corners
            width = 0
            height = 0
            for corner in corners:
                if corner[0] > width:
                    width = corner[0]
                if corner[1] > height:
                    height = corner[1]

            #once the size of the map has been identified, fill it up with "?", as pacman does not know what is in there
            self.reward = [[-1 for y in range(height + 1)]
                           for x in range(width + 1)]
            self.utility = [[random() for y in range(height + 1)]
                            for x in range(width + 1)]
            #now add in all the information pacman knows initially. starting with all known locations of food
            for food in foods:
                #use "F" to mark food on the map
                self.reward[food[0]][food[1]] = 10
            #now mark the location of capsules on the map, this time using "C"
            for capsule in capsules:
                self.reward[capsule[0]][capsule[1]] = 5
            #now mark the location of the walls on the map, using "W"
            for wall in walls:
                self.reward[wall[0]][wall[1]] = "W"
                self.utility[wall[0]][wall[1]] = "W"

        #set init to true as the map has been initialized
        self.init = True
Esempio n. 19
0
    def updatefood(self, state):

        foodloc = tuple(api.food(state))
        capsules = api.capsules(state)

        # initially assigning same values to all places in the map
        for i in range(len(self.pcMap)):
            self.valueMap[self.pcMap[i]] = 0

        # Assign values in the map based on the objects present in the map
        for i in range(len(self.pcMap)):
            # Assign for Food
            if self.pcMap[i] in foodloc:
                self.valueMap[self.pcMap[i]] = 1
            # Assign for Capsules
            if self.pcMap[i] in capsules:
                self.valueMap[self.pcMap[i]] = 2
Esempio n. 20
0
    def initialRewardMap(self, state):

        foods = api.food(state)
        ghosts = map(lambda x: ((int(x[0][0]), int(x[0][1])), x[1]),
                     self.current_ghosts_states)
        capsules = api.capsules(state)
        pacman = api.whereAmI(state)

        # find the real distance for ghosts to every points and take the minimum distance between different ghosts
        if ghosts:
            walkDistanceMapOfGhosts = self.getGhostWalkDistance(
                tuple(map(int, ghosts[-1][0])), self.ghosts_facing[-1],
                ghosts[-1][1])
        else:
            walkDistanceMapOfGhosts = Grid(self.reward_map.getWidth(),
                                           self.reward_map.getHeight())

        for ghost in ghosts[0:-1]:
            walkDistanceMapOfGhosts = walkDistanceMapOfGhosts.minimumValueOnMap(
                self.getGhostWalkDistance(
                    tuple(map(int, ghost[0])),
                    self.ghosts_facing[ghosts.index(ghost)], ghost[1]))

        # the distance between pacman and ghost
        distance_pacman_to_ghost = walkDistanceMapOfGhosts.getValue(
            pacman[0], pacman[1])

        # smallGird
        if self.is_small_grid:
            #  max ghost distance 17
            # if the pacman is too close the ghost the pacman will try the closest food
            # otherwise eat the food in bottom left
            if distance_pacman_to_ghost <= 2:
                self.rewardMapFunction(foods, capsules,
                                       map(lambda x: x[0], ghosts),
                                       walkDistanceMapOfGhosts, 1, 2, 1, 3, -5)
            else:
                self.rewardMapFunction([foods[0]], capsules,
                                       map(lambda x: x[0], ghosts),
                                       walkDistanceMapOfGhosts, 1, 2, 1, 3, -3)
        else:
            self.rewardMapFunction(foods, capsules,
                                   map(lambda x: x[0], ghosts),
                                   walkDistanceMapOfGhosts, 0, 1.5, 2, 12, -5)
        # delete the distance map
        del walkDistanceMapOfGhosts
Esempio n. 21
0
    def createMap(
        self, state
    ):  #initialises the 3 maps that will be used throughout the solver
        ghostList = api.ghosts(state)
        wallList = api.walls(state)
        foodList = api.food(state)
        capsuleList = api.capsules(state)

        #creates three maps, self.map will be the main map that holds utility, grassfireMap1 will be the distance between the first ghost and each food, grassfireMap2 does the samething but with the second ghost
        self.map = deepcopy(state.getFood())
        self.grassfireMap1 = deepcopy(self.map)
        self.grassfireMap2 = deepcopy(self.map)

        width = self.map.width
        height = self.map.height

        #initialises each map wall = None, every other cell is 0 for the main map and -5 for the ghost-food distance map
        for x in range(width):
            for y in range(height):
                if (x, y) in wallList:
                    self.map[x][y] = None
                    self.grassfireMap1[x][y] = None
                    self.grassfireMap2[x][y] = None
                elif (x, y) in foodList:
                    self.map[x][y] = 0
                    self.grassfireMap1[x][y] = -5
                    self.grassfireMap2[x][y] = -5
                elif (x, y) in ghostList:
                    self.map[x][y] = 0
                    self.grassfireMap1[x][y] = -5
                    self.grassfireMap2[x][y] = -5
                elif (x, y) in capsuleList:
                    self.map[x][y] = 0
                    self.grassfireMap1[x][y] = -5
                    self.grassfireMap2[x][y] = -5
                else:
                    self.map[x][y] = 0
                    self.grassfireMap1[x][y] = -5
                    self.grassfireMap2[x][y] = -5

        #iterates over each ghost and updates each map accordingly using grass fire algorithm
        i = 0
        for ghost in ghostList:
            self.grassfire(ghost[0], ghost[1], wallList, i)
            i += 1
Esempio n. 22
0
 def updateMap(self, state):
     # get location of all visible food
     foods = api.food(state)
     #get location of all visible capsules
     capsules = api.capsules(state)
     #get location of all visible walls
     walls = api.walls(state)
     #now add in all the information pacman knows initially. starting with all known locations of food
     for food in foods:
         #use "F" to mark food on the map
         self.reward[food[0]][food[1]] = 10
     #now mark the location of capsules on the map, this time using "C"
     for capsule in capsules:
         self.reward[capsule[0]][capsule[1]] = 5
     #now mark the location of the walls on the map, using "W"
     for wall in walls:
         self.reward[wall[0]][wall[1]] = "W"
         self.utility[wall[0]][wall[1]] = "W"
Esempio n. 23
0
    def __init__(self, state):
        # The constructor identifies the size of the map through the corners, and then calls a number of functions
        # that begin to populate the detail of the map
        TR = self.getCorners(api.corners(state))[
            3]  # Capture the TR coordinate for map building
        width = TR[0] + 2
        height = TR[1] + 2
        self.width = width
        self.height = height
        self.matrix = [[0 for x in range(width)] for y in range(height)]

        # We call these functions to populate the map with walls, food, capsules and ghosts
        self.assignWalls(api.walls(state))
        self.assignFood(api.food(state))
        self.assignCapsules(api.capsules(state))
        self.assignGhosts(api.ghosts(state))

        # We call this function to create a list of locations that should be iterated. (i.e. not walls)
        self.identifyLocationsToIterate()
Esempio n. 24
0
	def getValueMap(self, state, reward):
		"""
		This method helps to map rewards to the state of each grid that contains food
		It gives each coordinate of food an R(s) value
		It also updates each coordinate of food to no reward when it finds
		that Pacman has already eaten it

		This is the initial state that will have to be value-iterated for each change in state
		of ghost and pacman

		returns a dictionary of coordinates and their assigned utility values
		"""
		pacman = api.whereAmI(state)
		food = api.food(state)
		capsules = api.capsules(state)

		self.reward = reward

		if pacman not in self.visited:
			self.visited.append(pacman)

		for i in food:
			if i not in self.foodMap:
				self.foodMap.append(i)

		foodsMap = dict.fromkeys(self.foodMap, self.reward)
		# make a dictionary of capsules and give a reward value of 0 since it's neutral
		capsuleMap = dict.fromkeys(capsules, 0)

		#join these two together to get a complete map of legal squares
		valueMap = {}
		valueMap.update(foodsMap)
		valueMap.update(capsuleMap)

		#Check: If key in food-list is in visited list,
		#Set value of food in valueMap dictionary to 0
		#update value of the dictionary key
		for key in self.foodMap:
			if key in self.visited:
				valueMap[key] = 0

		return valueMap
Esempio n. 25
0
 def map_reward(self, state, size):
     weight_reward = {}
     (map_x, map_y) = size
     wall = api.walls(state)
     food = api.food(state)
     ghost = api.ghosts(state)
     capsule = api.capsules(state)
     for i in range(map_x + 1):
         for j in range(map_y + 1):
             if (i, j) in food:
                 weight_reward[(i, j)] = self.weight_food
             elif (i, j) in capsule:
                 weight_reward[(i, j)] = self.weight_capsule
             elif (i, j) in wall:
                 weight_reward[(i, j)] = self.weight_wall
             elif (i, j) in ghost:
                 weight_reward[(i, j)] = self.weight_blank
             else:
                 weight_reward[(i, j)] = self.weight_blank
     return weight_reward
Esempio n. 26
0
    def valIterS (self,state,discount,reward):
        pacman = api.whereAmI(state)
        walls= api.walls(state)
        food = api.food(state)
        ghosts = api.ghosts(state)
        bigPillEnergy = api.capsules(state)
        corners = api.corners(state)
        for i in range(self.getLayoutWidth(corners) - 1):
            for j in range (self.getLayoutHeight(corners) - 1):
                if (i,j) not in walls and (i,j)not in ghosts and (i,j) not in bigPillEnergy and (i,j) not in food:
                    if len(food)<=1:
                        u = self.MaxU(i,j)
                        #when the food is less 1 or 0 (could be pointless when it's 0 but just making sure), 
                        # we want the pacman to eat the last food as soon as possible
                    else:

                        u = self.MinU(i,j)
                        # However, when the food is more than 1. Since it's a small grid, avoiding the ghost(s) is priority
                    bellman = reward + discount* u 
                    self.dankMap.setValue(i,j,bellman) 
Esempio n. 27
0
    def makeMap(self, state):
        grid = []
        self.valueMap = {}

        foodloc = tuple(api.food(state))
        corners = api.corners(state)
        walls = api.walls(state)
        capsules = api.capsules(state)

        tempH = -1
        tempW = -1

        for i in range(len(corners)):
            if corners[i][1] > tempH:
                tempH = corners[i][1]
            if corners[i][0] > tempW:
                tempW = corners[i][0]

        height = tempH + 1
        width = tempW + 1

        for i in range(width):
            for j in range(height):
                grid.append((i, j))

        # creates a map where pacman can move around
        self.pcMap = (tuple(set(grid) - set(walls)))
        # print "pcMap",self.pcMap

        # initially assigning same values to all places in the map
        for i in range(len(self.pcMap)):
            self.valueMap[self.pcMap[i]] = 0

        # Assign values in the map based on the objects present in the map
        for i in range(len(self.pcMap)):
            #Assign for Food
            if self.pcMap[i] in foodloc:
                self.valueMap[self.pcMap[i]] = 1
            #Assign for Capsules
            if self.pcMap[i] in capsules:
                self.valueMap[self.pcMap[i]] = 2
Esempio n. 28
0
    def foodWithinRange(self, state):
        # Determines whether pacman can currently see food

        cur = api.whereAmI(state)
        foodAndCapsules = api.union(api.food(state), api.capsules(state))

        for x in range(1, 6):
            #Food seen south of pacman
            if (cur[0], cur[1] - x) in foodAndCapsules:
                return True
            #Food seen west of pacman
            if (cur[0] - x, cur[1]) in foodAndCapsules:
                return True
            #Food seen north of pacman
            if (cur[0], cur[1] + x) in foodAndCapsules:
                return True
            #Food seen east of pacman
            if (cur[0] + x, cur[1]) in foodAndCapsules:
                return True
        #No food seen
        return False
Esempio n. 29
0
    def updateState(self, state):

        # Update Legal Moves
        self.legal = api.legalActions(state)

        # Update Current Location
        self.currentLocation = api.whereAmI(state)

        # Update Position of Corners
        self.corners = api.corners(state)

        # Update Position of Food
        self.foods = api.food(state)

        # Update Position of Ghosts
        self.ghosts = api.ghosts(state)

        # Update Position of Capsules
        self.capsules = api.capsules(state)

        # Update Position of Walls
        self.walls = api.walls(state)
Esempio n. 30
0
    def generateEntityGrid(self, state):
        # initialize 2d array with correct dimensions from state
        (w, h) = api.corners(state)[3]
        entityGrid = [[" " for x in range(w + 1)] for y in range(h + 1)]

        # populate known information
        (x, y) = api.whereAmI(state)
        entityGrid[y][x] = "p"

        for (x, y) in api.food(state):
            entityGrid[y][x] = "f"

        for (x, y) in api.capsules(state):
            entityGrid[y][x] = "c"

        for (x, y) in api.ghosts(state):
            entityGrid[int(y)][int(x)] = "g"

        for (x, y) in api.walls(state):
            entityGrid[y][x] = "w"

        return entityGrid