sample#

Sampling methods.

gym_socks.sampling.sample.default_sampler(state_sampler=None, action_sampler=None, env=None)[source]#

Default trajectory sampler.

Parameters
Returns

A generator function that yields system observations as tuples.

gym_socks.sampling.sample.default_trajectory_sampler(state_sampler=None, action_sampler=None, env=None, time_horizon=1)[source]#

Default trajectory sampler.

Parameters
  • state_sampler – The state space sampler.

  • action_sampler – The action space sampler.

  • env (Optional[gym_socks.envs.dynamical_system.DynamicalSystem]) – The system to sample from.

  • time_horizon (int) – The time horizon to simulate over.

Returns

A generator function that yields system observations as tuples.

gym_socks.sampling.sample.grid_sampler(grid)[source]#

Grid sampler.

Returns a sample arranged on a uniformly-spaced grid. Use make_grid_from_ranges() or make_grid_from_space() from gym_socks.utils.grid to generate a grid of points.

Parameters

grid (list) – The grid of points.

Yields

A sample from the sample_space.

gym_socks.sampling.sample.random_sampler(sample_space)[source]#

Random sampler.

Returns a random sample taken from the space. See gym.spaces.Box for more information on the distributions used for sampling.

Parameters

sample_space (gym.Space) – The space to sample from.

Yields

A sample from the sample_space.

gym_socks.sampling.sample.repeat(sampler, num)[source]#

Repeat sampler.

Repeats the output of a sample generator num times.

Parameters
  • sampler – The sample generator function.

  • num (int) – The number of times to repeat a sample.

Yields

A repeated sample.

gym_socks.sampling.sample.sample(sampler=None, sample_size=None, *args, **kwargs)[source]#

Generate a sample using the sample generator.

Parameters
  • sampler – Sample generator function.

  • sample_size (Optional[int]) – Size of the sample.

Returns

list of tuples

gym_socks.sampling.sample.sample_generator(fun)[source]#

Sample generator decorator.

Converts a sample function into a generator function. Any function that returns a single observation (as a tuple) can be converted into a sample generator.

Parameters

fun – Sample function that returns or yields an observation.

Returns

A function that can be used to islice a sample from the sample generator.

Example

>>> from itertools import islice
>>> from gym_socks.envs.sample import sample_generator
>>> @sample_generator
... def custom_sampler(env, policy, sample_space):
...     env.state = sample_space.sample()
...     action = policy(state=state)
...     next_state, *_ = env.step(action)
...     yield (env.state, action, next_state)
>>> S = list(islice(custom_sampler(), 100))