garage.sampler.local_sampler

Sampler that runs workers in the main process.

class LocalSampler(agents, envs, *, worker_factory=None, max_episode_length=None, is_tf_worker=False, seed=get_seed(), n_workers=psutil.cpu_count(logical=False), worker_class=DefaultWorker, worker_args=None)[source]

Bases: garage.sampler.sampler.Sampler

Inheritance diagram of garage.sampler.local_sampler.LocalSampler

Sampler that runs workers in the main process.

This is probably the simplest possible sampler. It’s called the “Local” sampler because it runs everything in the same process and thread as where it was called from.

The sampler need to be created either from a worker factory or from parameters which can construct a worker factory. See the __init__ method of WorkerFactory for the detail of these parameters.

Parameters
  • agents (Policy or List[Policy]) – Agent(s) to use to sample episodes. If a list is passed in, it must have length exactly worker_factory.n_workers, and will be spread across the workers.

  • envs (Environment or List[Environment]) – Environment from which episodes are sampled. If a list is passed in, it must have length exactly worker_factory.n_workers, and will be spread across the workers.

  • worker_factory (WorkerFactory) – Pickleable factory for creating workers. Should be transmitted to other processes / nodes where work needs to be done, then workers should be constructed there. Either this param or params after this are required to construct a sampler.

  • max_episode_length (int) – Params used to construct a worker factory. The maximum length episodes which will be sampled.

  • is_tf_worker (bool) – Whether it is workers for TFTrainer.

  • seed (int) – The seed to use to initialize random number generators.

  • n_workers (int) – The number of workers to use.

  • worker_class (type) – Class of the workers. Instances should implement the Worker interface.

  • worker_args (dict or None) – Additional arguments that should be passed to the worker.

classmethod from_worker_factory(cls, worker_factory, agents, envs)[source]

Construct this sampler.

Parameters
  • worker_factory (WorkerFactory) – Pickleable factory for creating workers. Should be transmitted to other processes / nodes where work needs to be done, then workers should be constructed there.

  • agents (Agent or List[Agent]) – Agent(s) to use to sample episodes. If a list is passed in, it must have length exactly worker_factory.n_workers, and will be spread across the workers.

  • envs (Environment or List[Environment]) – Environment from which episodes are sampled. If a list is passed in, it must have length exactly worker_factory.n_workers, and will be spread across the workers.

Returns

An instance of cls.

Return type

Sampler

obtain_samples(self, itr, num_samples, agent_update, env_update=None)[source]

Collect at least a given number transitions (timesteps).

Parameters
  • itr (int) – The current iteration number. Using this argument is deprecated.

  • num_samples (int) – Minimum number of transitions / timesteps to sample.

  • agent_update (object) – Value which will be passed into the agent_update_fn before sampling episodes. If a list is passed in, it must have length exactly factory.n_workers, and will be spread across the workers.

  • env_update (object) – Value which will be passed into the env_update_fn before sampling episodes. If a list is passed in, it must have length exactly factory.n_workers, and will be spread across the workers.

Returns

The batch of collected episodes.

Return type

EpisodeBatch

obtain_exact_episodes(self, n_eps_per_worker, agent_update, env_update=None)[source]

Sample an exact number of episodes per worker.

Parameters
  • n_eps_per_worker (int) – Exact number of episodes to gather for each worker.

  • agent_update (object) – Value which will be passed into the agent_update_fn before sampling episodes. If a list is passed in, it must have length exactly factory.n_workers, and will be spread across the workers.

  • env_update (object) – Value which will be passed into the env_update_fn before samplin episodes. If a list is passed in, it must have length exactly factory.n_workers, and will be spread across the workers.

Returns

Batch of gathered episodes. Always in worker

order. In other words, first all episodes from worker 0, then all episodes from worker 1, etc.

Return type

EpisodeBatch

shutdown_worker(self)[source]

Shutdown the workers.

start_worker(self)

Initialize the sampler.

i.e. launching parallel workers if necessary.

This method is deprecated, please launch workers in construct instead.