Utilities

Samplers

Samplers are designed to be used with supertypes. See the Supertypes page for examples on how they are used.

Interface

class phantom.utils.samplers.Sampler[source]

Samplers are used in Agent/Environment Supertypes to define how they are sampled.

Samplers are designed to be used when training policies and a stochastic distribution of values is required for the Supertype sampling.

Samplers return an unbounded number of total values with one value being returned at a time with the sample() method.

abstract sample()[source]

Returns a single value defined by the Sampler’s internal distribution.

Implementations of this function should also update the instance’s _value property.

Return type:

TypeVar(T)

class phantom.utils.samplers.ComparableSampler[source]

Extension of the Sampler for ComparableTypes in order to treat the ComparableSampler like its actual internal value.

Example: >>> s = UniformFloatSampler() >>> s.value = s.sample() >>> s <= 1.0 # True >>> s == 1.5 # False

abstract sample()

Returns a single value defined by the Sampler’s internal distribution.

Implementations of this function should also update the instance’s _value property.

Return type:

TypeVar(T)

Implementations

class phantom.utils.samplers.UniformFloatSampler(low=0.0, high=1.0, clip_low=None, clip_high=None)[source]

Samples a single float value from a uniform distribution.

Uses np.random.uniform() internally.

sample()[source]

Returns a single value defined by the Sampler’s internal distribution.

Implementations of this function should also update the instance’s _value property.

Return type:

float

class phantom.utils.samplers.UniformIntSampler(low=0, high=1, clip_low=None, clip_high=None)[source]

Samples a single int value from a uniform distribution.

Uses np.random.randint() internally.

sample()[source]

Returns a single value defined by the Sampler’s internal distribution.

Implementations of this function should also update the instance’s _value property.

Return type:

float

class phantom.utils.samplers.UniformArraySampler(low=0.0, high=1.0, shape=(1,), clip_low=None, clip_high=None)[source]

Samples an array of float values from a uniform distribution.

Uses np.random.uniform() internally.

sample()[source]

Returns a single value defined by the Sampler’s internal distribution.

Implementations of this function should also update the instance’s _value property.

Return type:

ndarray

class phantom.utils.samplers.NormalSampler(mu, sigma, clip_low=None, clip_high=None)[source]

Samples a single float value from a normal distribution.

Uses np.random.normal() internally.

sample()[source]

Returns a single value defined by the Sampler’s internal distribution.

Implementations of this function should also update the instance’s _value property.

Return type:

float

class phantom.utils.samplers.NormalArraySampler(mu, sigma, shape=(1,), clip_low=None, clip_high=None)[source]

Samples an array of float values from a normal distribution.

Uses np.random.normal() internally.

sample()[source]

Returns a single value defined by the Sampler’s internal distribution.

Implementations of this function should also update the instance’s _value property.

Return type:

ndarray

class phantom.utils.samplers.LambdaSampler(func, *args, **kwargs)[source]

Samples using an arbitrary lambda function.

sample()[source]

Returns a single value defined by the Sampler’s internal distribution.

Implementations of this function should also update the instance’s _value property.

Return type:

TypeVar(T)

Ranges

Ranges are designed to be used with supertypes. See the Supertypes page for examples on how they are used.

Interface

class phantom.utils.ranges.Range(name=None)[source]

Ranges are used in Agent/Environment Supertypes to define how they are sampled.

Ranges are designed to be used when generating rollouts post-training and a non-stochastic distribution of values is required for the Supertype sampling.

Ranges return a fixed number of total values and as such all values must be returned in one go with the values() method.

abstract values()[source]

Returns the complete set of values defined by the Range.

Return type:

Sequence[TypeVar(T)]

Implementations

class phantom.utils.ranges.UniformRange(start, end, step=1.0, name=None, dtype=None)[source]

Returns an array of values spaced by a step between a start and end value.

Uses np.arange() internally.

values()[source]

Returns the complete set of values defined by the Range.

Return type:

ndarray

class phantom.utils.ranges.LinspaceRange(start, end, n, name=None, dtype=None)[source]

Returns an array of n values evenly distributed between a start and end value.

Uses np.linspace() internally.

values()[source]

Returns the complete set of values defined by the Range.

Return type:

ndarray

class phantom.utils.ranges.UnitArrayUniformRange(start, end, step=1.0, name=None, dtype=None)[source]

Returns a list of n shape (1,) numpy arrays with values spaced by a step between a start and end value. Useful for encoding observation spaces with single element boxes.

Uses np.arange() internally.

values()[source]

Returns the complete set of values defined by the Range.

Return type:

List[ndarray]

class phantom.utils.ranges.UnitArrayLinspaceRange(start, end, n, name=None, dtype=None)[source]

Returns a list of n shape (1,) numpy arrays with values evenly distributed between a start and end value. Useful for encoding observation spaces with single element boxes.

Uses np.linspace() internally.

values()[source]

Returns the complete set of values defined by the Range.

Return type:

List[ndarray]