Table of Contents

Class TimeLimit<TObs, TAct>

Namespace
Gym.Wrappers
Assembly
Gym.dll

Truncates the episode once MaxEpisodeSteps steps have run since the last reset. This is where the truncated signal comes from for envs that have no limit of their own, as opposed to terminated, which the MDP decides. make adds it when the spec has max_episode_steps.

public class TimeLimit<TObs, TAct> : Wrapper<TObs, TAct>, IDynamicAttributes, IWrapper, IEnv, IDisposable, IRecordConstructorArgs

Type Parameters

TObs

The observation type, passed through.

TAct

The action type, passed through.

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

Remarks

Stepping before the first reset steps the env and then raises Python's TypeError (the step counter is still None), exactly as Gymnasium does; OrderEnforcing beneath it catches that mistake first in a make-built stack. The step that reaches the limit returns truncated=True even when it also terminates. Reference: gymnasium/wrappers/common.py.

Constructors

TimeLimit(Env<TObs, TAct>, TimeLimitOptions)

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

public TimeLimit(Env<TObs, TAct> env, TimeLimitOptions options)

Parameters

env Env<TObs, TAct>

The env to wrap.

options TimeLimitOptions

The options.

Exceptions

ArgumentNullException

Thrown when options is null.

TypeError

Thrown when a required argument is unset, with Python's missing-argument message.

TimeLimit(Env<TObs, TAct>, long?)

Wraps env with a step limit.

public TimeLimit(Env<TObs, TAct> env, long? maxEpisodeSteps)

Parameters

env Env<TObs, TAct>

The env to limit.

maxEpisodeSteps long?

The step at which the episode is truncated (elapsed >= max_episode_steps). Typed as nullable so that a spec's None reaches Gymnasium's check, as TimeLimit(env, max_episode_steps=None) does.

Remarks

The arguments are checked before the env, as in Python, so a bad limit is reported even for a null env.

Exceptions

TypeError

Thrown when maxEpisodeSteps is null: Expect the max_episode_steps to be an integer, actually: <class 'NoneType'>. Python's other non-integers cannot be passed in C#.

ValueError

Thrown when it is not positive: Expect the max_episode_steps to be positive, actually: -1.

Properties

ElapsedSteps

The steps since the last reset (Python's _elapsed_steps), null until the first reset.

[PyName("_elapsed_steps")]
public long? ElapsedSteps { get; set; }

Property Value

long?

MaxEpisodeSteps

The step limit (Python's _max_episode_steps); assigning it changes the limit of the running episode too.

[PyName("_max_episode_steps")]
public long MaxEpisodeSteps { get; set; }

Property Value

long

SavedKwargs

The recorded constructor arguments, {'max_episode_steps': n} (Python's _saved_kwargs).

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

Property Value

Info

Spec

The env's spec with MaxEpisodeSteps set to this wrapper's limit (a deep copy, cached), or null when the env has none. Unlike other wrappers, no WrapperSpec is added: the limit is part of the spec itself.

public override EnvSpec? Spec { get; set; }

Property Value

EnvSpec

Exceptions

AttributeError

Thrown when assigned, as for every wrapper.

Methods

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

Resets the step count to zero, then resets the env.

public override ResetResult<TObs> 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<TObs>

The env's first observation and info.

Exceptions

Error

Thrown for a negative seed.

Step(TAct)

Steps the env, counts the step, and sets truncated once the count reaches MaxEpisodeSteps.

public override StepResult<TObs> Step(TAct action)

Parameters

action TAct

The action, passed on.

Returns

StepResult<TObs>

The env's transition, truncated at the limit.

Exceptions

TypeError

Thrown after the env stepped when the wrapper was never reset: unsupported operand type(s) for +=: 'NoneType' and 'int', Python's error for None += 1.