Beispiel #1
0
    def getFeatures(self, state, action):
        # extract the grid of food and wall locations and get the ghost locations
        food = state.getFood()
        walls = state.getWalls()
        ghosts = state.getGhostPositions()

        features = CustomCounter()

        features["bias"] = 1.0

        # compute the location of pacman after he takes the action
        x, y = state.getPacmanPosition()
        dx, dy = Actions.directionToVector(action)
        next_x, next_y = int(x + dx), int(y + dy)

        # count the number of ghosts 1-step away
        features["#-of-ghosts-1-step-away"] = sum(
            (next_x, next_y) in Actions.getLegalNeighbors(g, walls)
            for g in ghosts)

        # if there is no danger of ghosts then add the food feature
        if not features["#-of-ghosts-1-step-away"] and food[next_x][next_y]:
            features["eats-food"] = 1.0

        dist = closestFood((next_x, next_y), food, walls)
        if dist is not None:
            # make the distance a number less than one otherwise the update
            # will diverge wildly
            features["closest-food"] = float(dist) / (walls.width *
                                                      walls.height)
        features.divideAll(10.0)
        return features
Beispiel #2
0
    def __init__(self,
                 epsilon=0.05,
                 gamma=0.8,
                 alpha=0.2,
                 numTraining=900,
                 extractor=SimpleExtractor(),
                 **args):
        "You can initialize Q-values here..."

        args['epsilon'] = epsilon
        args['gamma'] = gamma
        args['alpha'] = alpha
        args['numTraining'] = numTraining
        self.featExtractor = extractor
        self.index = 0  # This is always Pacman
        self.weights = CustomCounter()
        self.q_values = CustomCounter()
        self.lastAction = None
        ReinforcementAgent.__init__(self,
                                    epsilon=epsilon,
                                    gamma=gamma,
                                    alpha=alpha,
                                    numTraining=numTraining)

        "*** YOUR CODE HERE ***"
Beispiel #3
0
 def getFeatures(self, state, action):
     feats = CustomCounter()
     feats[state] = 1.0
     feats['x=%d' % state[0]] = 1.0
     feats['y=%d' % state[0]] = 1.0
     feats['action=%s' % action] = 1.0
     return feats
Beispiel #4
0
    def __init__(self, **args):
        "You can initialize Q-values here..."
        ReinforcementAgent.__init__(self, **args)

        self.last_move = None
        self.last_index = -1
        self.q_values = CustomCounter()
        self.best_state_action = None
Beispiel #5
0
    def __init__(self, extractor='IdentityExtractor', **args):
        self.featExtractor = MyExtractor()
        # self.featExtractor = lookup(extractor, globals())()
        PacmanQAgent.__init__(self, **args)
        self.train = True
        self.start_with_trained_weights = True

        if self.train:
            self.weights = CustomCounter()
        if self.start_with_trained_weights:
           self.weights = {'eats-food': 3.744196232507296, 'closest-food': -0.04577473069235167,
                            '#-of-ghosts-1-step-away': -10.027445395331362, 'bias': -6.304746841317069}
Beispiel #6
0
    def __init__(self,
                 extractor='IdentityExtractor',
                 train=False,
                 optilio=False,
                 weights_values=[0, 0, 0, 0],
                 **args):
        self.featExtractor = lookup(extractor, globals())()
        PacmanQAgent.__init__(self, **args)
        self.weights = CustomCounter()
        self.weight = 0
        self.optilio = optilio
        self.train = train

        if self.train:
            for number, feature in zip(weights_values, self.weights):
                self.weights[feature] = float(number)
Beispiel #7
0
    def __init__(self, extractor='IdentityExtractor', **args):
        self.featExtractor = MyExtractor()
        # self.featExtractor = lookup(extractor, globals())()
        PacmanQAgent.__init__(self, **args)
        self.train = False
        self.start_with_trained_weights = True

        if self.train:
            self.weights = CustomCounter()
        if self.start_with_trained_weights:
            # self.weights = {'capsule-is-nearby': 0.0, 'bias': -5.848186678365304, 'ghost-is-nearby': -47.767678591296, 'eats-food': 4.2052171620675125, '#-of-ghosts-1-step-away': -10.053403840432816, 'closest-food': -0.09139458036757105}
            self.weights = {
                'ghost-is-nearby': -19.71752071573443,
                'bias': -5.853012565325543,
                'eats-food': 4.2186468885699115,
                '#-of-ghosts-1-step-away': -10.029721097702556,
                'closest-food': -0.09146973122692789
            }
Beispiel #8
0
    def __init__(self,
                 epsilon=0.25,
                 gamma=0.8,
                 alpha=0.2,
                 numTraining=0,
                 extractor=IdentityExtractor(),
                 **args):
        "You can initialize Q-values here..."

        args['epsilon'] = epsilon
        args['gamma'] = gamma
        args['alpha'] = alpha
        args['numTraining'] = numTraining
        self.featExtractor = extractor
        self.index = 0  # This is always Pacman
        self.weights = CustomCounter()
        ReinforcementAgent.__init__(self, **args)

        "*** YOUR CODE HERE ***"
Beispiel #9
0
    def __init__(self,
                 extractor='IdentityExtractor',
                 train=False,
                 optilio=False,
                 weights_values=[0, 0, 0, 0, 0],
                 **args):
        self.featExtractor = lookup(extractor, globals())()
        PacmanQAgent.__init__(self, **args)
        self.weights = CustomCounter()
        self.weight = 0
        self.optilio = optilio
        self.train = train

        if (not self.train
            ) or self.optilio or weights_values != [0, 0, 0, 0, 0]:
            for number, feature in zip(weights_values, [
                    "bias", "#-of-ghosts-1-step-away", "eats-food",
                    "closest-food", "capsules"
            ]):
                self.weights[feature] = float(number)

        if not self.optilio:
            # print(self.weights)
            pass
Beispiel #10
0
 def getFeatures(self, state, action):
     feats = CustomCounter()
     feats[(state, action)] = 1.0
     return feats
Beispiel #11
0
    def getFeatures(self, state, action):
        # extract the grid of food and wall locations and get the ghost locations
        food = state.getFood()
        walls = state.getWalls()
        ghosts = state.getGhostPositions()

        features = CustomCounter()

        features["bias"] = 1.0

        # compute the location of pacman after he takes the action
        x, y = state.getPacmanPosition()
        dx, dy = Actions.directionToVector(action)
        next_x, next_y = int(x + dx), int(y + dy)

        not_scared_ghosts_positions = [
            g_s.getPosition() for g_s in state.getGhostStates()
            if not g_s.scaredTimer
        ]

        # count the number of ghosts 1-step away
        # get position of ghost only when it is not scared
        # in this way pacman may learn to eat scared ghost
        features["#-of-ghosts-1-step-away"] = sum(
            (next_x, next_y) in Actions.getLegalNeighbors(g_s, walls)
            for g_s in not_scared_ghosts_positions)

        # ghost_quarters = [get_quarter_from_position(ghost_position, walls)
        #                   for ghost_position in state.getGhostPositions()]
        #
        # if all(ghost_quarters) == get_quarter_from_position((x, y), walls):
        #     features['pacman-and-ghosts-in-the-same-region'] = 1.0
        # else:
        #     features['pacman-and-ghosts-in-the-same-region'] = 0.0

        ghost_distance_limit = 3
        nearest_ghost = distance_to_the_nearest_item_from_list(
            (next_x, next_y), not_scared_ghosts_positions, walls, cutout=3)
        if nearest_ghost is not None:
            nearest_ghost = max(nearest_ghost - 1, 0)
            if nearest_ghost < 1:
                features['ghost-is-nearby'] = 1
            else:
                features['ghost-is-nearby'] = (
                    ghost_distance_limit -
                    nearest_ghost) / ghost_distance_limit
        else:
            features['ghost-is-nearby'] = 0

        # nearest_capsule = distance_to_the_nearest_item_from_list((next_x, next_y), state.getCapsules(), walls, cutout=0)

        ## set 1.0 if the nearest capsule is closer than the nearest ghost
        # if nearest_capsule is not None and nearest_ghost is None or \
        #         nearest_capsule is not None and nearest_ghost is not None and nearest_capsule < nearest_ghost:
        #     features["capsule-is-nearby"] = 1.0
        # features["capsule-is-nearby"] = 1.0 if nearest_capsule is not None else 0.0

        # if there is no danger of ghosts then add the food feature
        if not features["#-of-ghosts-1-step-away"] and food[next_x][next_y]:
            features["eats-food"] = 1.0
        else:
            features["eats-food"] = 0.0

        dist = closestFood((next_x, next_y), food, walls)
        if dist is not None:
            # make the distance a number less than one otherwise the update
            # will diverge wildly
            features["closest-food"] = float(dist) / (walls.width *
                                                      walls.height)
        features.divideAll(10.0)
        return features
Beispiel #12
0
    def getFeatures(self, state, action):
        # extract the grid of food and wall locations and get the ghost locations
        food = state.getFood()
        walls = state.getWalls()
        ghosts = state.getGhostPositions()
        capsulesLeft = len(state.getCapsules())
        scaredGhost = []
        activeGhost = []
        features = CustomCounter()

        for ghost in state.getGhostStates():
            if not ghost.scaredTimer:
                activeGhost.append(ghost)
            else:
                #print (ghost.scaredTimer)
                scaredGhost.append(ghost)

        pos = state.getPacmanPosition()

        def getManhattanDistances(ghosts):
            return map(lambda g: manhattan_distance(pos, g.getPosition()),
                       ghosts)

        distanceToClosestActiveGhost = distanceToClosestScaredGhost = 0

        features["bias"] = 1.0

        # compute the location of pacman after he takes the action
        x, y = state.getPacmanPosition()
        dx, dy = Actions.directionToVector(action)
        next_x, next_y = int(x + dx), int(y + dy)

        # count the number of ghosts 1-step away
        features["#-of-ghosts-1-step-away"] = sum(
            (next_x, next_y) in Actions.getLegalNeighbors(g, walls)
            for g in ghosts)

        # if there is no danger of ghosts then add the food feature
        if not features["#-of-ghosts-1-step-away"] and food[next_x][next_y]:
            features["eats-food"] = 1.0

        dist = closestFood((next_x, next_y), food, walls)
        if dist is not None:
            # make the distance a number less than one otherwise the update
            # will diverge wildly
            features["closest-food"] = float(dist) / (walls.width *
                                                      walls.height)

        if scaredGhost:  # and not activeGhost:
            distanceToClosestScaredGhost = min(
                getManhattanDistances(scaredGhost))
            if activeGhost:
                distanceToClosestActiveGhost = min(
                    getManhattanDistances(activeGhost))
            else:
                distanceToClosestActiveGhost = 10
            features["capsules"] = capsulesLeft
            #features["dist-to-closest-active-ghost"] = 2*(1./distanceToClosestActiveGhost)
            if distanceToClosestScaredGhost <= 8 and distanceToClosestActiveGhost >= 2:  #features["#-of-ghosts-1-step-away"] >= 1:
                features["#-of-ghosts-1-step-away"] = 0
                features["eats-food"] = 0.0
                #features["closest-food"] = 0

        features.divideAll(10.0)
        return features
Beispiel #13
0
 def __init__(self, extractor='IdentityExtractor', **args):
     self.featExtractor = lookup(extractor, globals())()
     PacmanQAgent.__init__(self, **args)
     self.weights = CustomCounter()