def __init__(self, extractor='IdentityExtractor', **args):
        self.featExtractor = util.lookup(extractor, globals())()
        PacmanQAgent.__init__(self, **args)
        numpy.set_printoptions(precision=2, suppress=True)

        # You might want to initialize weights here.
        self.all_directions = [Directions.NORTH,
                               Directions.SOUTH,
                               Directions.EAST,
                               Directions.WEST,
                               Directions.STOP]
        self.alpha = 0.01
        self.areaRad = 5
        self.biais = 1.0
        self.features = ['food', 'ghost', 'wall']
        self.out_of_map = {'ghost': 0, 'wall': 0, 'food': 0}
        self.w = []
        for feature in self.features:
            temp = []
            for i in range(0, 2 * self.areaRad + 1):
                temp.append([])
                for j in range(0, 2 * self.areaRad + 1):
                    temp[i].append(1.0)
            self.w.append(temp)

        # Try to load the weights
        self.load_weights()
  def registerInitialState(self, gameState):
    """
    This method handles the initial setup of the
    agent to populate useful fields (such as what team
    we're on).

    A distanceCalculator instance caches the maze distances
    between each pair of positions, so your agents can use:
    self.distancer.getDistance(p1, p2)
    """
    self.red = gameState.isOnRedTeam(self.index)
    self.distancer = distanceCalculator.Distancer(gameState.data.layout)

    # comment this out to forgo maze distance computation and use manhattan distances
    self.distancer.getMazeDistances()

    import __main__
    if '_display' in dir(__main__):
      self.display = __main__._display
  
    inferenceType = util.lookup('ParticleFilter', globals())
    self.inferenceModules = [inferenceType(o,self.index) for o in self.getOpponents(gameState)]
    for inference in self.inferenceModules: inference.initialize(gameState)
    self.ghostBeliefs = [inf.getBeliefDistribution() for inf in self.inferenceModules]
    self.firstMove = True
Example #3
0
 def __init__(
     self, index=0, inference="ExactInference", ghostAgents=None, observeEnable=True, elapseTimeEnable=True
 ):
     inferenceType = util.lookup(inference, globals())
     self.inferenceModules = [inferenceType(a) for a in ghostAgents]
     self.observeEnable = observeEnable
     self.elapseTimeEnable = elapseTimeEnable
  def __init__(self, extractor='IdentityExtractor', **args):
    self.featExtractor = util.lookup(extractor, globals())()
    PacmanQAgent.__init__(self, **args)

    # You might want to initialize weights here.
    "*** YOUR CODE HERE ***"
    self.weights = util.Counter()

    self.globalvar = 1

    # Define NN
    self.model = Sequential()
    self.model.add(Dense(300, init='lecun_uniform', input_shape=(5,)))
    self.model.add(Activation('relu'))

    self.model.add(Dense(300, init='lecun_uniform'))
    self.model.add(Activation('relu'))

    self.model.add(Dense(300, init='lecun_uniform'))
    self.model.add(Activation('relu'))

    self.model.add(Dense(5, init='lecun_uniform'))
    self.model.add(Activation('linear'))

    rms = RMSprop()
    self.model.compile(loss='mse', optimizer=rms)

    self.atoi = {'North': 0, 'South': 1, 'East': 2, 'West': 3, 'Stop': 4}
  def __init__(self, extractor='IdentityExtractor', **args):
    self.featExtractor = util.lookup(extractor, globals())()
    PacmanQAgent.__init__(self, **args)

    # You might want to initialize weights here.
    "*** YOUR CODE HERE ***"
    self.weights = util.Counter()
    def __init__(self, extractor='IdentityExtractor', **args):
        self.featExtractor = util.lookup(extractor, globals())()
        PacmanQAgent.__init__(self, **args)
        #self.epsilon = 0.05
        #self.gamma = 0.8
        #self.alpha = 0.2

        # You might want to initialize weights here.
        self.all_directions = [Directions.NORTH,
                               Directions.SOUTH,
                               Directions.EAST,
                               Directions.WEST,
                               Directions.STOP]
        self.nb_states = 0
        self.closest_food = None
        self.featureQ = [self.ghost_west, self.ghost_east, self.ghost_north, self.ghost_south,
                         self.no_wall_west, self.no_wall_east, self.no_wall_north, self.no_wall_south,
                         #self.posx, self.posy, self.biais,
                         self.close_dot_west, self.close_dot_east, self.close_dot_north, self.close_dot_south]
        self.w = {}
        for action in self.all_directions:
            temp = []
            for i in range(0, len(self.featureQ)):
                temp.append(1.0)
            self.w[action] = temp
Example #7
0
 def __init__(self, index, fnStrategy='defaultstrategy'):
   self.index=index
   strategies = fnStrategy.split(';')
   try:
     self.strategy = util.lookup(strategies[index%len(strategies)], globals())
   except:
     print "Function "+strategies[index%len(strategies)]+" not defined!"
     print "Loading defaultstrategy..."
     self.strategy = defaultstrategy
Example #8
0
 def getattr(self, name):
     ret = None
     if self.path is not None:
         ret = findOnPath(name, self.path)
     if ret is not None:
         ret.name = self.name+'.'+ret.name
         return ret
     if self.mod is None:
         self.mod = lookup(self.name)
     return Namespace.getattr(self, name)
Example #9
0
def _process_batch(session, posts, dests, modes, **kwargs):
    """
    Request commute distances and durations for a batch of posts.
    """
    
    posts_latlon = [(_.latitude, _.longitude) for _ in posts]
    
    for mode in modes:
        
        response = gmaps.distance_matrix(posts_latlon, dests, mode, **kwargs)
        
        for post, row in zip(posts, response['rows']):
            post.commutes.extend([
                Commute(dest=lookup(session, Destination, dest),
                        mode=lookup(session, Mode, mode),
                        distance=e['distance']['value'],
                        duration=e['duration']['value'])
                for dest, e in zip(dests, row['elements'])
                if e['status'] == 'OK'
            ])
 def __init__(self, evalFn = 'scoreEvaluationFunction', depth = '2', a=0,b=0,c=0,d=0,e=0,f=0):
   self.index = 0 # Pacman is always agent index 0
   self.depth = int(depth)
   storeConstants(a, b, c, d, e, f)
   # self.a = a
   # print "a: ", a
   # b = b
   # c = c
   # d = d
   # e = e
   # f = f
   self.evaluationFunction = util.lookup(evalFn, globals())
Example #11
0
  def __init__(self, extractor='IdentityExtractor', **args):
    if extractor in args:
      extractor = args['extractor']

    self.featExtractor = util.lookup(extractor, globals())()
    SarsaLambdaAgent.__init__(self, **args)

    # You might want to initialize weights here.
    "*** YOUR CODE HERE ***"
    # we are not using values
    self.weights = util.Counter()
    self.workingWeights = util.Counter()
    self.times = 0
Example #12
0
 def addPackage(self, package, skiplist=[]):
     pkg = lookup(package)
     base = package.replace(DOT, SLASH)
     for cl in listAllClasses(pkg):
         name = package + "." + cl
         if name in skiplist:
             # print 'skipping',name # ?? dbg
             continue
         entryname = base + "/" + cl + ".class"
         self.zipfile.putNextEntry(ZipEntry(entryname))
         instream = openResource(entryname)
         copy(instream, self.zipfile)
         instream.close()
	def __init__(self, index, fnStrategy='defaultstrategy'):
		self.index=index
		self.xCapsule = self.yCapsule = -1
		strategies = fnStrategy.split(';')

		global ghostN
		ghostN = "all" if len(strategies) > 1 else strategies[0]

		try:
			self.strategy = util.lookup(strategies[index%len(strategies)], globals())
		except:
			print "Function "+strategies[index%len(strategies)]+" not defined!"
			self.strategy = default
  def __init__(self, extractor='FeatureLearner', processFreq=processingFrequency, **args):
    self.featExtractor = util.lookup(extractor, globals())()
    
    PacmanQAgent.__init__(self, **args)

    # Weight Initialization
    self.featuresWeights = util.Counter()
    self.rewardCounter = util.Counter()
    self.rewardDict = util.Counter()

    # We use the processingFrequency only if we are in 
    # the featExtractor is a FeatureLearner (= we have a deep learning agent)    
    self.processingFrequency = processingFrequency #if self.featExtractor==FeatureLearner else 10e9
Example #15
0
 def __init__(self, numDecks=1, qvalues=util.Counter(), epsilon=0.05, gamma=0.8, alpha=0.2, numTraining=1000, extractor='SimpleExtractor'):
     """
     Init appropriated from qlearningAgents.py from
     pacman reinforcement assignment.
     """
     self.epsilon = epsilon
     self.discount = gamma
     self.alpha = alpha
     self.numTraining = numTraining
 
     self.dealer = Dealer.Dealer(numDecks, True)     # True = Silent Mode; Agent must be modified later to actually accept a dealer
     self.qvalues = qvalues
     self.weights = util.Counter()
     self.featExtractor = util.lookup(extractor, globals())()
Example #16
0
  def __init__(self, extractor='IdentityExtractor', **args):
    if extractor in args:
      extractor = args['extractor']

    self.featExtractor = util.lookup(extractor, globals())()

    # You might want to initialize weights here.
    "*** YOUR CODE HERE ***"
    self.mdp = args['mdp']
    self.discount = args['gamma']
    self.iterations = args['iterations']
    self.alpha = args['alpha']

    self.weights = util.Counter()
    self.times = 0

    if False: #extractor == 'BairdsExtractor':
      # doing evil thing here
      self.weights[0] = 1
      self.weights[1] = 1
      self.weights[2] = 1
      self.weights[3] = 1
      self.weights[4] = 1
      self.weights[5] = 1
      self.weights[6] = 1
    
    # do update, full backup (sweep every state)
    for time in range(self.iterations):
      for state in self.mdp.getStates():
        if not self.mdp.isTerminal(state): 
	  # find the best action
          maxValue = None
	  bestAction = None

	  for action in self.mdp.getPossibleActions(state):
	    thisValue = self.getQValue(state, action)
	    if bestAction == None or thisValue > maxValue:
	      maxValue = thisValue
	      bestAction = action

          for nextState, prob in self.mdp.getTransitionStatesAndProbs(state, bestAction):
	    self.update(state, action, nextState, self.mdp.getReward(state, action, nextState), prob)

      self.outputWeights(time)
      self.outputValues(time)
      self.outputMSE(time)
def getFile(name):
    dot = name.rfind('.')
    if dot == -1:
        return PkgEntry(name)
##        return topFile(name)

    package = lookup(name[:dot])
    if isinstance(package, PyJavaPackage):
        return PkgEntry(name)
##    if hasattr(package, '__file__'):
##        return ZipEntry(package.__file__, name)
##    elif hasattr(package, '__path__') and len(package.__path__) == 1:
##        return DirEntry(package.__path__[0], name)
    elif isinstance(package, TypeType):
        # this 'package' is a java class
        f = getFile(name[:dot])
        if f:
            return f
Example #18
0
    def __init__(self, extractor='StateExtractor'):
        # Feature extractor
        self.featExtractor = util.lookup(extractor, globals())()

        # Transition function (data structure required for the transition function)
        #*** YOUR CODE STARTS HERE ***"
        # Code to remove ---------- from here
        self.frequencies = util.Counter()
        # Code to remove ---------- to here
        #"*** YOUR CODE FINISHES HERE ***"


        # Dictionary with examples of a Low state for each High state: it serves to get possible actions
        # and to check terminal states (though it is not required if the high level representation
        # capture them)
        self.states = util.Counter()

        # Reward for each state at the high level representation
        self.reward = util.Counter()
Example #19
0
 def __init__(self, **args):
   """Uses the SimpleExtractor feature extractor with a 2-layer network
   (affine-relu-affine-relu) with an L2 loss to predict Q values."""
   self.featExtractor = util.lookup("SimpleExtractor", globals())()
   PacmanQAgent.__init__(self, **args)
   self.sess = tf.Session()
   self.allFeats = list(self.featExtractor.getFeatures(None, None))
   numDims = len(self.allFeats)
   self.x = tf.placeholder("float", shape=[None, numDims])
   self.y_ = tf.placeholder("float", shape=[None, 1])
   self.W1 = tf.Variable(tf.truncated_normal([numDims, 5], stddev=0.01))
   self.b1 = tf.Variable(tf.constant(0.01, shape=[5]))
   self.W2 = tf.Variable(tf.truncated_normal([5, 1]))
   self.b2 = tf.Variable(tf.constant(0.01, shape=[1]))
   self.h = tf.nn.relu(tf.matmul(self.x, self.W1) + self.b1)
   self.out = tf.nn.relu(tf.matmul(self.h, self.W2) + self.b2)
   self.l2_loss = tf.reduce_sum(tf.square(self.y_ - self.out))
   self.train_step = tf.train.AdamOptimizer(1e-4).minimize(self.l2_loss)
   self.sess.run(tf.initialize_all_variables())
Example #20
0
  def __init__(self, extractor='IdentityExtractor', **args):
    if extractor in args:
      extractor = args['extractor']

    self.featExtractor = util.lookup(extractor, globals())()
    TGLearningAgent.__init__(self, **args)

    # You might want to initialize weights here.
    "*** YOUR CODE HERE ***"
    self.weights = util.Counter()
    self.times = 0

    if extractor == 'BairdsExtractor':
      # doing evil thing here
      self.weights[0] = 1
      self.weights[1] = 1
      self.weights[2] = 1
      self.weights[3] = 1
      self.weights[4] = 1
      self.weights[5] = 10
      self.weights[6] = 1
    def __init__( self, index = 0, inference = "ExactInference", ghostAgents = None, observeEnable = True, elapseTimeEnable = True, obsOnStopOnly = False,
                  prior = False, rolloutPolicy='Uniform'):
        inferenceType = util.lookup(inference, globals())
        if prior:
            self.inferenceModules = [inferenceType(a, prior) for a in ghostAgents]
        else:
            self.inferenceModules = [inferenceType(a) for a in ghostAgents]

        self.observeEnable = observeEnable
        self.elapseTimeEnable = elapseTimeEnable
        self.ghostAgents = ghostAgents
        self.obsOnStopOnly = obsOnStopOnly
        self.lastAction = None
        self.rolloutPolicy = rolloutPolicy
        
        self.gamma = 0.99
        self.epsilon = 0.01
        self.c = 500.0
        self.numSimulations = 200
        self.depth = 10

        self.searchTree = {}
        self.searchTree[()] = [0,0]
Example #22
0
    def process_item(self, item, spider):
        """
        Save items to the database.
        """

        session = self.Session()
        post = ApartmentPost(**item)
        tags = item['tags'].split('|')

        post.post_tags = [
            ApartmentPostTag(tag=lookup(session, ApartmentTag, tag_name))
            for tag_name in tags
        ]

        try:
            session.add(post)
            session.commit()
        except:
            session.rollback()
            raise
        finally:
            session.close()

        return item
Example #23
0
 def __init__(self, evalFn="scoreEvaluation"):
   self.evaluationFunction = util.lookup(evalFn, globals())
   assert self.evaluationFunction != None
Example #24
0
# multiAgents.py
Example #25
0
    def __init__(self, extractor='IdentityExtractor', **args):
        self.featExtractor = util.lookup(extractor, globals())()
        PacmanQAgent.__init__(self, **args)

        "*** YOUR CODE HERE ***"
        self.W = util.Counter()
Example #26
0
 def __init__(self, index=0, inference="ExactInference", ghostAgents=None):
     inferenceType = util.lookup(inference, globals())
     self.inferenceModules = [inferenceType(a) for a in ghostAgents]
Example #27
0
  def __init__(self, extractor='IdentityExtractor', **args):
    self.featExtractor = util.lookup(extractor, globals())()
    PacmanQAgent.__init__(self, **args)

    "*** YOUR CODE HERE ***"
    self.W = util.Counter()
Example #28
0
 def __init__(self, evalFn = 'scoreEvaluationFunction', depth = '2'):
     self.index = 0 # Pacman is always agent index 0
     self.evaluationFunction = util.lookup(evalFn, globals())
     self.depth = int(depth)
 def __init__( self, index = 0, inference = "ExactInference", ghostAgents = None ):
   inferenceType = util.lookup(inference, globals())
   self.inferenceModules = [inferenceType(a) for a in ghostAgents]
Example #30
0
 def __init__(self, evaluation_function='scoreEvaluationFunction', depth=2):
     self.evaluation_function = util.lookup(evaluation_function, globals())
     self.depth = depth
Example #31
0
 def __init__(self, evalFn="scoreEvaluationFunction", depth="2"):
     self.index = 0  # Pacman is always agent index 0
     self.evaluationFunction = util.lookup(evalFn, globals())
     self.depth = int(depth)
     self.nextagent = "max"
     self.ghostidx = 1
Example #32
0
 def __init__(self, evalFn='scoreEvaluationFunction', depth='2'):
     self.index = 0  # Pacman is always agent index 0
     self.evaluationFunction = util.lookup(evalFn, globals())
     self.depth = int(depth)
     self.notFirstIteration = False
     self.currentPlayer = 0
Example #33
0
 def __init__( self, index = 0, inference = "ExactInference", ghostAgents = None, observeEnable = True, elapseTimeEnable = True):
     inferenceType = util.lookup(inference, globals())
     self.inferenceModules = [inferenceType(a) for a in ghostAgents]
     self.observeEnable = observeEnable
     self.elapseTimeEnable = elapseTimeEnable
Example #34
0
 def __init__(self, extractor='IdentityExtractor', **args):
     self.featExtractor = util.lookup(extractor, globals())()
     PacmanQAgent.__init__(self, **args)
     self.weights = util.Counter()
Example #35
0
 def __init__(self, evalFn='scoreEvaluationFunction', depth='2'):
     self.index = 0  # Pacman is always agent index 0
     self.evaluationFunction = util.lookup(evalFn, globals())
     self.depth = int(depth)
    def __init__(self, extractor='IdentityExtractor', **args):
        self.featExtractor = util.lookup(extractor, globals())()
        PacmanQAgent.__init__(self, **args)

        # You might want to initialize weights here.
        "*** YOUR CODE HERE ***"
Example #37
0
 def __init__(self, index, evalFun='scoreEvaluationFunctionGhost', depth=2):
     self.index = index
     self.evaluationFunction = util.lookup(evalFun, globals())
     self.depth = int(depth)
Example #38
0
 def __init__(self, evalFn="scoreEvaluation"):
     self.evaluationFunction = util.lookup(evalFn, globals())
     assert self.evaluationFunction != None
Example #39
0
 def __init__(self, evalFn = 'betterEvaluationFunction', depth = '2'):
   self.index = 0 # Pacman is always agent index 0
   self.evaluationFunction = util.lookup(evalFn, globals())
   self.depth = int(depth)
   self._turn_durations = []
Example #40
0
 def __init__(self, evalFn='contest'):
     self.index = 0
     self.evaluationFunction = util.lookup(evalFn, globals())
     self.depth = 3
 def __init__(self, extractor="IdentityExtractor", **args):
     self.featExtractor = util.lookup(extractor, globals())()
     PacmanQAgent.__init__(self, **args)
     self.weights = util.Counter()
Example #42
0
def main():
    parser = argparse.ArgumentParser(description='2048 game.')
    parser.add_argument('--random_seed',
                        help='The seed for the random state.',
                        default=numpy.random.randint(100),
                        type=int)
    displays = ['GUI', 'SummaryDisplay']
    agents = [
        'KeyboardAgent', 'ReflexAgent', 'MinmaxAgent', 'AlphaBetaAgent',
        'ExpectimaxAgent'
    ]
    parser.add_argument('--display',
                        choices=displays,
                        help='The game ui.',
                        default=displays[0],
                        type=str)
    parser.add_argument('--agent',
                        choices=agents,
                        help='The agent.',
                        default=agents[0],
                        type=str)
    parser.add_argument(
        '--depth',
        help='The maximum depth for to search in the game tree.',
        default=2,
        type=int)
    parser.add_argument('--sleep_between_actions',
                        help='Should sleep between actions.',
                        default=False,
                        type=bool)
    parser.add_argument('--num_of_games',
                        help='The number of games to run.',
                        default=1,
                        type=int)
    parser.add_argument(
        '--num_of_initial_tiles',
        help='The number non empty tiles when the game started.',
        default=2,
        type=int)
    parser.add_argument('--initial_board',
                        help='Initial board for new games.',
                        default=None,
                        type=str)
    parser.add_argument('--evaluation_function',
                        help='The evaluation function for ai agent.',
                        default='score_evaluation_function',
                        type=str)
    args = parser.parse_args()
    numpy.random.seed(args.random_seed)
    if args.display != displays[0]:
        display = util.lookup('displays.' + args.display, globals())()
    else:
        display = None
    if args.agent != agents[0]:
        agent = create_agent(args)
    else:
        agent = None
    initial_state = None
    if args.initial_board is not None:
        with open(os.path.join('layouts', args.initial_board), 'r') as f:
            lines = f.readlines()
            initial_board = numpy.array([
                list(map(lambda x: int(x), line.split(','))) for line in lines
            ])
            initial_state = GameState(board=initial_board)
    game_runner = GameRunner(display=display,
                             agent=agent,
                             num_of_initial_tiles=args.num_of_initial_tiles,
                             sleep_between_actions=args.sleep_between_actions)
    for i in range(args.num_of_games):
        score = game_runner.new_game(initial_state=initial_state)
    if display is not None:
        display.print_stats()