Esempio n. 1
0
 def __init__(self,
              num_articles,
              dim,
              theta_dist=None,
              arm_dist=None,
              seed=None,
              verbosity=0):
     #theta_mean=0,theta_std=1):
     # dim is actually (dimension of feature space) + 1 because it includes bias term
     """Args:
   num_articles - number of arms
   dim - dimension of the problem
   theta_dist - distribution of theta
   arm_dist - distribution of arms
   """
     Environment.__init__(self, verbosity)
     if seed is not None:
         np.random.seed(seed)  #todo? upgrade this to its own RNG
     self.num_articles = num_articles
     self.dim = dim
     self.theta_dist = theta_dist if theta_dist != None else NormalDist(
         0, 1, dim=dim)
     self.arm_dist = arm_dist if arm_dist != None else DistributionWithConstant(
         BernoulliDist(1.0 / (dim - 1), dim - 1))
     #(lambda: np.random.binomial(1,max(0,1/(self.dim-1)),self.dim))
     #print(self.theta_dist)
     #print(type(self.theta_dist))
     self.theta = self.theta_dist()
     print('theta', self.theta)
     # keeping current rewards
     self.current_rewards = [0] * self.num_articles
     self.stochastic_rewards = [0] * self.num_articles
Esempio n. 2
0
async def init_db():
    db_host = db_config.get('host', '127.0.0.1')
    database = db_config.get('database')
    db_user = db_config.get('user')
    db_pwd = db_config.get('password')
    db_pool = await create_pool(max_size=50,
                                host=db_host,
                                database=database,
                                user=db_user,
                                password=db_pwd,
                                loop=loop)
    redis_host = redis_config['redis'].get('host')
    redis_port = redis_config['redis'].get('port')
    redis_db = redis_config['redis'].get('db')
    redis_pool = await create_redis_pool((redis_host, redis_port),
                                         db=redis_db,
                                         loop=loop)
    redis_cache_host = redis_config['redis_cache'].get('host')
    redis_cache_port = redis_config['redis_cache'].get('port')
    redis_cache_db = redis_config['redis_cache'].get('db')
    redis_cache_pool = await create_redis_pool(
        (redis_cache_host, redis_cache_port), db=redis_cache_db, loop=loop)
    global env
    env = Environment(loop=loop,
                      db_pool=db_pool,
                      redis_pool=redis_pool,
                      redis_cache_pool=redis_cache_pool)
Esempio n. 3
0
    def run(self):
        self.env.reset()
        done = False
        with torch.no_grad():
            while not done:
                long_term_xs, long_term_ys = self.env.calc_long_term_targets()

                rel_long_term_waypoints = self._to_relative_coords(
                    long_term_xs, long_term_ys)
                rel_nn_short_term_waypoints = self.policy_layer(
                    torch.FloatTensor(rel_long_term_waypoints).cuda()).detach(
                    ).cpu().numpy()
                abs_nn_planned_xs, abs_nn_planned_ys = self._to_absolute_coords(
                    rel_nn_short_term_waypoints[:self.short_term_planning_length], \
                    rel_nn_short_term_waypoints[self.short_term_planning_length: 2 * self.short_term_planning_length],
                    combine=False)
                action = self.execution_layer(self.env.car, abs_nn_planned_xs,
                                              abs_nn_planned_ys, self.dt,
                                              self.short_term_planning_length)
                _, _, done, _ = self.env.step(action)
                plot_waypoints(rel_long_term_waypoints,
                               rel_nn_short_term_waypoints)
                # self.env.render()


if __name__ == '__main__':
    env = gym.make('CarRacing-v0')
    env = Environment(env=env, FPS=50.0)
    test = Test(env=env, model_dict="saved/nn_policy_15.h5")
    test.run()
Esempio n. 4
0
        return tmp_words 

    def writeFiles( self, bundle ):
        try:
            os.makedirs( bundle.getPath() )
            os.chdir( bundle.getPath() )
            files = Files( bundle.getPath() , self.env )
            comment( '- Writing content for subdirectories.' )
            files.writeWords( 'words', bundle.words, bundle.folder_name )
            files.writeLines( 'lines', bundle.lines, bundle.folder_name )
        except:
            error( 'Directory already exists.' )
            rm_file = input( 'Do you want to remove the directory [Y/n]?' )
            if( rm_file.upper() == 'Y' or rm_file == '' ):
                os.system( f'rm { bundle.getPath() } -r')
                os.makedirs( bundle.getPath() )
                os.chdir( bundle.getPath() )
                files = Files( bundle.getPath(), self.env )
                comment( '- Writing content for subdirectories.' )
                files.writeWords( 'words', bundle.words, bundle.folder_name )
                files.writeLines( 'lines', bundle.lines, bundle.folder_name )

if __name__ == "__main__":
    from base import console_message as out
    from base.environment import Environment

    e = Environment( out, os.getcwd() )

    choice = ask( 'What dataset would you like to use?' )
    ImagePipeline( e, f'{os.getcwd()}/images/', choice.payload )