Ejemplo n.º 1
0
def make_envs(make_env, env_id, num_env, init_seed, **kwargs):
    """
    Create a list of argument-free make_env() given the settings. 
    
    Each make_env function in the list uses different random seeds generated by Seeder. 
    
    Args:
        make_env (function): A function to create an environment
        env_id (str): environment ID, e.g. 'Pendulum-v0'. 
        num_env (int): Number of environments to create. 
        init_seed (int): Initial seed for Seeder to sample random seeds. 
        **kwargs: keyword aguments used to specify other options for make_env. 
        
    Returns:
        list_make_env (list): A list of argument-free make_env() functions, each with different random
            seed. 
    """
    # Generate different seeds for each environment
    seeder = Seeder(init_seed=init_seed)
    seeds = seeder(size=num_env)

    # Use partial to generate a list of argument-free make_env, each with different seed
    list_make_env = [
        partial(make_env, env_id=env_id, seed=seed, **kwargs) for seed in seeds
    ]

    return list_make_env
 def test_seeder(self):
     seeder = Seeder(init_seed=0)
     
     assert np.random.get_state()[1][0] == 0
     
     assert len(seeder(size=1)) == 1
     
     assert len(seeder(size=20)) == 20
     
     assert np.array(seeder(size=[2, 3, 4])).shape == (2, 3, 4)
Ejemplo n.º 3
0
    def test_seeding(self):
        seeder = Seeder(init_seed=0)

        # Single list of seeds
        seeds = seeder(size=1)
        assert len(seeds) == 1
        seeds = seeder(size=5)
        assert len(seeds) == 5

        # Batch of seeds
        seeds = seeder(size=[1, 3])
        assert np.alltrue(np.array(seeds).shape == (1, 3))
        seeds = seeder(size=[2, 3])
        assert np.alltrue(np.array(seeds).shape == (2, 3))
Ejemplo n.º 4
0
def test_make_envs():
    list_make_env = make_envs(make_env=make_gym_env, env_id='Pendulum-v0', num_env=3, init_seed=1)
    assert len(list_make_env) == 3
    assert list_make_env[0] != list_make_env[1] and list_make_env[0] != list_make_env[2]

    # Test if the seedings are correct
    seeder = Seeder(init_seed=1)
    seeds = seeder(3)
    for make_env, seed in zip(list_make_env, seeds):
        assert make_env.keywords['seed'] == seed
    env = list_make_env[0]()
    raw_env = gym.make('Pendulum-v0')
    raw_env.seed(seeds[0])
    assert np.allclose(env.reset(), raw_env.reset())
Ejemplo n.º 5
0
    def __init__(self,
                 worker_class,
                 num_worker,
                 init_seed=0,
                 daemonic_worker=None):
        """
        Args:
            worker_class (BaseWorker): a callable worker class. Note that it is not recommended to 
                send instantiated object of the worker class, but send class instead. 
            num_worker (int): number of workers. Recommended to be the same as number of CPU cores. 
            init_seed (int): initial seed for the seeder which samples seeds for workers.
            daemonic_worker (bool): If True, then set all workers to be daemonic. 
                Because if main process crashes, we should not cause things to hang.
        """
        self.worker_class = worker_class
        self.num_worker = num_worker
        self.init_seed = init_seed
        self.daemonic_worker = daemonic_worker

        # Create a seeder, sampling different seeds for each task
        self.seeder = Seeder(init_seed=self.init_seed)
Ejemplo n.º 6
0
def make_envs(make_env, env_id, num_env, init_seed, **kwargs):
    r"""Create a list of argument-free make_env() functions based on the given settings. 
    
    .. note::
    
        Each make_env function in the list uses different random seeds generated by :class:`Seeder`. 
    
    Example::
        
        >>> make_envs(make_env=make_gym_env, env_id='CartPole-v1', num_env=3, init_seed=0)
        [functools.partial(<function make_gym_env at 0x7f2127b5ce18>, env_id='CartPole-v1', seed=209652396),
         functools.partial(<function make_gym_env at 0x7f2127b5ce18>, env_id='CartPole-v1', seed=398764591),
         functools.partial(<function make_gym_env at 0x7f2127b5ce18>, env_id='CartPole-v1', seed=924231285)]
    
    Args:
        make_env (function): a function to create an environment
        env_id (str): environment ID, e.g. 'Pendulum-v0', 'Ant-v2'
        num_env (int): number of environments to create. 
        init_seed (int): initial seed for :class:`Seeder` to sample random seeds. 
        **kwargs: keyword aguments used to specify other options for make_env. 
        
    Returns
    -------
    list_make_env : list
        a list of argument-free make_env() functions, each associated with different random seed. 
    """
    # Generate different seeds for each environment
    seeder = Seeder(init_seed=init_seed)
    seeds = seeder(size=num_env)

    # Use partial to generate a list of argument-free make_env, each with different seed
    list_make_env = [
        partial(make_env, env_id=env_id, seed=seed, **kwargs) for seed in seeds
    ]

    return list_make_env