Class TimeLimit<TObs, TAct>
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
TObsThe observation type, passed through.
TActThe 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
envEnv<TObs, TAct>The env to wrap.
optionsTimeLimitOptionsThe options.
Exceptions
- ArgumentNullException
Thrown when
optionsis 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
envEnv<TObs, TAct>The env to limit.
maxEpisodeStepslong?The step at which the episode is truncated (
elapsed >= max_episode_steps). Typed as nullable so that a spec'sNonereaches Gymnasium's check, asTimeLimit(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
maxEpisodeStepsis null:Expect the. Python's other non-integers cannot be passed in C#.max_episode_stepsto be an integer, actually: <class 'NoneType'>- ValueError
Thrown when it is not positive:
Expect the.max_episode_stepsto 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
SavedKwargs
The recorded constructor arguments, {'max_episode_steps': n} (Python's _saved_kwargs).
[PyName("_saved_kwargs")]
public Info SavedKwargs { get; }
Property Value
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
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
seedBigInteger?The seed, passed on.
optionsIReadOnlyDictionary<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
actionTActThe 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 forNone += 1.