Table of Contents

Class TimeAwareObservation<TObs, TAct>

Namespace
Gym.Wrappers
Assembly
Gym.dll

Adds the episode's elapsed steps to every observation, as Gymnasium's TimeAwareObservation does: a Dict observation gets a time key, a Tuple one a last item, anything else becomes {"obs": obs, "time": time}, and by default the result is flattened into one array.

public class TimeAwareObservation<TObs, TAct> : ObservationWrapper<object?, TObs, TAct>, IDynamicAttributes, IWrapper, IEnv, IDisposable, IRecordConstructorArgs

Type Parameters

TObs

The env's observation type.

TAct

The action type, passed through.

Inheritance
Env<object, TAct>
Wrapper<object, TAct, TObs, TAct>
TimeAwareObservation<TObs, TAct>
Implements
Inherited Members
Extension Methods

Remarks

The time is the step count since the last reset, as an int32 array [t], or with normalize_time a float32 [t / max_episode_steps]; the limit comes from the env's spec or, failing that, a TimeLimit in the stack. The observation type depends on flatten at run time (an array when flattened, a dict or tuple otherwise), so the wrapper's observations are objects.

Reference: gymnasium/wrappers/stateful_observation.py.

Constructors

TimeAwareObservation(Env<TObs, TAct>, TimeAwareObservationOptions)

Builds the instance from its options record: the properties set, the constructor's defaults for the rest.

public TimeAwareObservation(Env<TObs, TAct> env, TimeAwareObservationOptions options)

Parameters

env Env<TObs, TAct>

The env to wrap.

options TimeAwareObservationOptions

The options.

Exceptions

ArgumentNullException

Thrown when options is null.

TimeAwareObservation(Env<TObs, TAct>, bool, bool, string)

Wraps env.

public TimeAwareObservation(Env<TObs, TAct> env, bool flatten = true, bool normalizeTime = false, string dictTimeKey = "time")

Parameters

env Env<TObs, TAct>

The env; its spec or a TimeLimit in its stack must give the step limit.

flatten bool

Whether the observation and the time are flattened into one array (Gymnasium's default).

normalizeTime bool

Whether the time is the fraction of the limit elapsed (float32) instead of the step count (int32).

dictTimeKey string

The time's key in a Dict observation.

Exceptions

TypeError

Thrown when env is null.

ValueError

Thrown when no step limit is known, or dictTimeKey already is a key of the env's Dict space, with Gymnasium's messages.

Properties

DictTimeKey

The time's key in a Dict observation.

public string DictTimeKey { get; }

Property Value

string

Flatten

Whether observations are flattened (flatten).

public bool Flatten { get; }

Property Value

bool

MaxTimesteps

The step limit the time is measured against (max_timesteps).

public long MaxTimesteps { get; }

Property Value

long

NormalizeTime

Whether the time is normalized by the limit (normalize_time).

public bool NormalizeTime { get; }

Property Value

bool

SavedKwargs

The recorded constructor arguments (Python's _saved_kwargs).

[PyName("_saved_kwargs")]
public Info SavedKwargs { get; }

Property Value

Info

Timesteps

The steps since the last reset (timesteps).

public long Timesteps { get; set; }

Property Value

long

Methods

Dispose(bool)

Disposes the unflattened space with the stack.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true from Dispose().

Observation(TObs)

Adds the time to an observation (observation(observation)), then flattens when Flatten is set.

public override object? Observation(TObs observation)

Parameters

observation TObs

The env's observation.

Returns

object

A new array when flattening; otherwise a new dict or tuple holding the observation and the time array.

Reset(BigInteger?, IReadOnlyDictionary<string, object?>?)

Resets the env and the step count.

public override ResetResult<object?> Reset(BigInteger? seed = null, IReadOnlyDictionary<string, object?>? options = null)

Parameters

seed BigInteger?

The seed, passed on.

options IReadOnlyDictionary<string, object>

The options, passed on.

Returns

ResetResult<object>

The first observation with time 0.

Exceptions

Error

Thrown for a negative seed.

Step(TAct)

Steps the env and counts the step before the observation is built.

public override StepResult<object?> Step(TAct action)

Parameters

action TAct

The action, passed on.

Returns

StepResult<object>

The transition with the time added to its observation.