Class SyncVectorEnv<TObs, TAct>
A vector env that steps its sub-envs one after another on the calling thread: the reference implementation of the
vector API, byte for byte what Gymnasium's SyncVectorEnv returns for the same sub-envs, seeds and actions.
Typed by the batched types (NDArray for array spaces); SyncVectorEnv is the untyped one.
public class SyncVectorEnv<TObs, TAct> : VectorEnv<TObs, TAct>, IVectorEnv, IDisposable
Type Parameters
TObsThe batched observation type: the element type of the single observation space batched.
TActThe batched action type.
- Inheritance
-
VectorEnv<TObs, TAct>SyncVectorEnv<TObs, TAct>
- Implements
- Derived
- Inherited Members
- Extension Methods
Remarks
The sub-envs are built by the constructor from EnvFns, owned by this vector env and closed with it.
When a sub-env's episode ends, AutoresetMode decides: NextStep
resets it at the next step (whose reward is 0 and whose action is ignored), SameStep
resets it at once and moves the last observation and info into final_obs/final_info,
Disabled leaves it to reset(options={"reset_mask": ...}).
Ownership: with Copy (the default) every observation batch returned is a new deep copy the caller owns; without it, the batch is this env's own buffer, overwritten by the next call, which the caller must neither keep nor dispose. Rewards and flags are always new arrays. Not thread-safe.
Deviations: an options dict's reset_mask is not removed from the caller's dict (Python pops it); the
sub-envs get the options without it, as in Gymnasium. Gymnasium's np_random and np_random_seed of a
sync vector env are tuples of the sub-envs' generators and seeds (get_attr); the typed
NpRandom and NpRandomSeed cannot hold a
tuple, so they keep the base's meaning, the vector env's own generator and seed, and GetAttr("np_random")
and GetAttr("np_random_seed") give the sub-envs' values.
Reference: gymnasium/vector/sync_vector_env.py.
Constructors
SyncVectorEnv(IEnumerable<Func<IEnv>>, bool, object?, object?)
Builds the sub-envs and the batched spaces (SyncVectorEnv(env_fns, copy, observation_mode, autoreset_mode)).
public SyncVectorEnv(IEnumerable<Func<IEnv>> envFns, bool copy = true, object? observationMode = null, object? autoresetMode = null)
Parameters
envFnsIEnumerable<Func<IEnv>>One function per sub-env, called once each, in order; the vector env owns what they return.
copyboolWhether every returned observation batch is a deep copy (the default) or the env's own buffer.
observationModeobject"same"(null, the default): every sub-env has the first's observation space, batched;"different": the spaces may differ but share shape and dtype, batched withbatch_differing_spaces; or a tuple (object?[] or a value tuple) of the batched and the single space to use.autoresetModeobjectAn AutoresetMode or its value as a string; null is NextStep.
Exceptions
- ArgumentNullException
Thrown when
envFnsis null.- ValueError
Thrown for an unknown autoreset mode or observation mode, with Gymnasium's message.
- IndexError
Thrown when there are no sub-envs (
list index out of range).- TypeError
Thrown when a tuple observation mode holds something else than spaces, or a batched space's elements are not
TObs/TAct.- RuntimeError
Thrown when a sub-env's spaces do not match the first's, with Gymnasium's message.
Properties
AutoresetMode
When ended sub-envs reset (autoreset_mode), also in metadata["autoreset_mode"].
public AutoresetMode AutoresetMode { get; }
Property Value
Copy
Whether returned observation batches are deep copies (copy).
public bool Copy { get; }
Property Value
EnvFns
The functions the sub-envs were built from (env_fns).
public IReadOnlyList<Func<IEnv>> EnvFns { get; }
Property Value
Envs
The sub-envs (envs), in order; owned by this vector env.
public IReadOnlyList<IEnv> Envs { get; }
Property Value
ObservationMode
The observation mode given (observation_mode): "same", "different", or the tuple of spaces.
public object ObservationMode { get; }
Property Value
Methods
Call(string, params object?[])
Calls a sub-env member by its Python name on every sub-env (call(name, *args)), through the wrappers as
get_wrapper_attr finds it: an attribute's value (a delegate one is called with the arguments), or a method,
called with the arguments in order.
public IReadOnlyList<object?> Call(string name, params object?[] args)
Parameters
namestringThe Python name:
render,np_random, a run-time attribute.argsobject[]The positional arguments for a callable member.
Returns
- IReadOnlyList<object>
One result per sub-env.
Exceptions
- AttributeError
Thrown when a sub-env has no such attribute or method.
- TypeError
Thrown when no method of that name takes the arguments given.
CloseExtras()
Closes every sub-env (close_extras).
protected override void CloseExtras()
Dispose(bool)
Closes, then disposes the sub-envs (which this vector env built and owns) and its buffers.
protected override void Dispose(bool disposing)
Parameters
GetAttr(string)
Reads an attribute of every sub-env (get_attr(name)), as Call(string, params object?[]) does without arguments.
public IReadOnlyList<object?> GetAttr(string name)
Parameters
namestringThe Python name.
Returns
- IReadOnlyList<object>
One value per sub-env.
Exceptions
- AttributeError
Thrown when a sub-env has no such attribute.
Render()
Renders every sub-env (render()), in order.
public override IReadOnlyList<RenderResult>? Render()
Returns
- IReadOnlyList<RenderResult>
One result per sub-env, as each renders by its own mode.
Reset(IReadOnlyList<BigInteger?>, IReadOnlyDictionary<string, object?>?)
Resets the sub-envs with a seed each (reset(seed=[...])); see Reset(BigInteger?, IReadOnlyDictionary<string, object?>?).
public override VectorResetResult<TObs> Reset(IReadOnlyList<BigInteger?> seeds, IReadOnlyDictionary<string, object?>? options = null)
Parameters
seedsIReadOnlyList<BigInteger?>One seed per sub-env, null keeping that sub-env's generator.
optionsIReadOnlyDictionary<string, object>The options passed to every reset sub-env, without
reset_mask.
Returns
- VectorResetResult<TObs>
The batched observations and the merged reset infos.
Exceptions
- ArgumentNullException
Thrown when
seedsis null.- ValueError
Thrown when there is not one seed per sub-env, with Gymnasium's message, or for a bad
reset_mask.- TypeError
Thrown for a
reset_maskthat is not a bool array.
Reset(BigInteger?, IReadOnlyDictionary<string, object?>?)
Resets the sub-envs (reset(seed=seed, options=options)): every one, or with options["reset_mask"]
(a (num_envs,) bool array) only the masked ones. An integer seed seeds sub-env i with seed + i.
public override VectorResetResult<TObs> Reset(BigInteger? seed = null, IReadOnlyDictionary<string, object?>? options = null)
Parameters
seedBigInteger?The first sub-env's seed, or null to keep every generator.
optionsIReadOnlyDictionary<string, object>The options passed to every reset sub-env, without
reset_mask.
Returns
- VectorResetResult<TObs>
The batched observations (a copy unless Copy is off) and the merged reset infos.
Exceptions
- TypeError
Thrown when
reset_maskis not an array or not bool.- ValueError
Thrown when
reset_maskhas another shape or no true.- Error
Thrown for a negative seed, by the sub-env.
SetAttr(string, object?)
Sets an attribute of every sub-env (set_attr(name, values)) through its wrappers
(set_wrapper_attr): a list or tuple gives one value per sub-env, anything else is set on all of them.
public void SetAttr(string name, object? values)
Parameters
namestringThe Python name.
valuesobjectA list or object?[] of one value per sub-env, or one value for all (an array is one value).
Exceptions
- ValueError
Thrown when a list's length is not the number of sub-envs, with Gymnasium's message.
Step(TAct)
Steps every sub-env with its action (step(actions)), applying AutoresetMode to the ones whose
episode ended at the previous step (NEXT_STEP) or at this one (SAME_STEP).
public override VectorStepResult<TObs> Step(TAct actions)
Parameters
actionsTActThe batched actions, one per sub-env, split by the batched action space (
iterate).
Returns
- VectorStepResult<TObs>
The batched observations (a copy unless Copy is off), new reward and flag arrays, and the merged infos.
Exceptions
- ValueError
Thrown when the actions are not one per sub-env (Python's
zip(strict=True)text), after the paired sub-envs stepped.- AssertionError
Thrown in
DISABLEDmode when an ended sub-env is stepped without a reset.