Wrappers
AutoResetWrapper(env, next_obs_in_extras=False)
#
Bases: Wrapper[State, ActionSpec, Observation], Generic[State, ActionSpec, Observation]
Automatically resets environments that are done. Once the terminal state is reached, the state, observation, and step_type are reset. The observation and step_type of the terminal TimeStep is reset to the reset observation and StepType.LAST, respectively. The reward, discount, and extras retrieved from the transition to the terminal state. NOTE: The observation from the terminal TimeStep is stored in timestep.extras["next_obs"].
Wrap an environment to automatically reset it when the episode terminates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment[State, ActionSpec, Observation]
|
the environment to wrap. |
required |
next_obs_in_extras
|
bool
|
whether to store the next observation in the extras of the terminal timestep. This is useful for e.g. truncation. |
False
|
Source code in jumanji/wrappers.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |
step(state, action)
#
Step the environment, with automatic resetting if the episode terminates.
Source code in jumanji/wrappers.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
JumanjiToDMEnvWrapper(env, key=None)
#
Bases: Environment, Generic[State, ActionSpec, Observation]
A wrapper that converts Environment to dm_env.Environment.
Create the wrapped environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment[State, ActionSpec, Observation]
|
|
required |
key
|
Optional[PRNGKey]
|
optional key to initialize the |
None
|
Source code in jumanji/wrappers.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
action_spec()
#
Returns the dm_env action spec.
Source code in jumanji/wrappers.py
216 217 218 | |
observation_spec()
#
Returns the dm_env observation spec.
Source code in jumanji/wrappers.py
212 213 214 | |
reset()
#
Starts a new sequence and returns the first TimeStep of this sequence.
Returns:
| Type | Description |
|---|---|
TimeStep
|
A |
Source code in jumanji/wrappers.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
step(action)
#
Updates the environment according to the action and returns a TimeStep.
If the environment returned a TimeStep with StepType.LAST at the
previous step, this call to step will start a new sequence and action
will be ignored.
This method will also start a new sequence if called after the environment
has been constructed and reset has not been called. Again, in this case
action will be ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
ArrayNumpy
|
A NumPy array, or a nested dict, list or tuple of arrays
corresponding to |
required |
Returns:
| Type | Description |
|---|---|
TimeStep
|
A |
Source code in jumanji/wrappers.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
JumanjiToGymWrapper(env, seed=0, backend=None)
#
Bases: Env, Generic[State, ActionSpec, Observation]
A wrapper that converts a Jumanji Environment to one that follows the gym.Env API.
Create the Gym environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment[State, ActionSpec, Observation]
|
|
required |
seed
|
int
|
the seed that is used to initialize the environment's PRNG. |
0
|
backend
|
Optional[str]
|
the XLA backend. |
None
|
Source code in jumanji/wrappers.py
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
close()
#
Closes the environment, important for rendering where pygame is imported.
Source code in jumanji/wrappers.py
616 617 618 | |
render(mode='human')
#
Renders the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
currently not used since Jumanji does not currently support modes. |
'human'
|
Source code in jumanji/wrappers.py
605 606 607 608 609 610 611 612 613 614 | |
reset(*, seed=None, options=None)
#
Resets the environment to an initial state by starting a new sequence
and returns the first Observation of this sequence.
Returns:
| Name | Type | Description |
|---|---|---|
obs |
GymObservation
|
an element of the environment's observation_space. |
info |
optional
|
contains supplementary information such as metrics. |
Source code in jumanji/wrappers.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
seed(seed=0)
#
Function which sets the seed for the environment's random number generator(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
the seed value for the random number generator(s). |
0
|
Source code in jumanji/wrappers.py
597 598 599 600 601 602 603 | |
step(action)
#
Updates the environment according to the action and returns an Observation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
ArrayNumpy
|
A NumPy array representing the action provided by the agent. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
observation |
GymObservation
|
an element of the environment's observation_space. |
reward |
float
|
the amount of reward returned as a result of taking the action. |
terminated |
bool
|
whether a terminal state is reached. |
info |
bool
|
contains supplementary information such as metrics. |
Source code in jumanji/wrappers.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |
MultiToSingleWrapper(env, reward_aggregator=jnp.sum, discount_aggregator=jnp.max)
#
Bases: Wrapper[State, ActionSpec, Observation], Generic[State, ActionSpec, Observation]
A wrapper that converts a multi-agent Environment to a single-agent Environment.
Create the wrapped environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment[State, ActionSpec, Observation]
|
|
required |
reward_aggregator
|
Callable
|
a function to aggregate all agents rewards into a single scalar value, e.g. sum. |
sum
|
discount_aggregator
|
Callable
|
a function to aggregate all agents discounts into a single scalar value, e.g. max. |
max
|
Source code in jumanji/wrappers.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
discount_spec
cached
property
#
Scalar discount spec matching the aggregated output.
reward_spec
cached
property
#
Scalar reward spec matching the aggregated output.
reset(key)
#
Resets the environment to an initial state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNGKey
|
random key used to reset the environment. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
state |
State
|
State object corresponding to the new state of the environment, |
timestep |
TimeStep[Observation]
|
TimeStep object corresponding the first timestep returned by the environment, |
Source code in jumanji/wrappers.py
268 269 270 271 272 273 274 275 276 277 278 279 280 | |
step(state, action)
#
Run one timestep of the environment's dynamics.
The rewards are aggregated into a single value based on the given reward aggregator. The discount value is set to the largest discount of all the agents. This essentially means that if any single agent is alive, the discount value won't be zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
State object containing the dynamics of the environment. |
required |
action
|
Array
|
Array containing the action to take. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
state |
State
|
State object corresponding to the next state of the environment, |
timestep |
TimeStep[Observation]
|
TimeStep object corresponding the timestep returned by the environment, |
Source code in jumanji/wrappers.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
VmapAutoResetWrapper(env, next_obs_in_extras=False)
#
Bases: VmapWrapper[State, ActionSpec, Observation], Generic[State, ActionSpec, Observation]
Combination of VmapWrapper and AutoResetWrapper.
env = VmapAutoResetWrapper(env) is completely equivalent to
env = VmapWrapper(AutoResetWrapper(env)). This class only exists for backwards
compatibility. New users should use env = VmapWrapper(AutoResetWrapper(env)).
NOTE: The observation from the terminal TimeStep is stored in timestep.extras["next_obs"].
Wrap an environment to vmap it and automatically reset it when the episode terminates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment[State, ActionSpec, Observation]
|
the environment to wrap. |
required |
next_obs_in_extras
|
bool
|
whether to store the next observation in the extras of the terminal timestep. This is useful for e.g. truncation. |
False
|
Source code in jumanji/wrappers.py
490 491 492 493 494 495 496 497 498 499 500 501 502 | |
VmapWrapper(env)
#
Bases: Wrapper[State, ActionSpec, Observation], Generic[State, ActionSpec, Observation]
Vectorized Jax env. Please note that all methods that return arrays do not return a batch dimension because the batch size is not known to the VmapWrapper. Methods that omit the batch dimension include: - observation_spec - action_spec - reward_spec - discount_spec
Source code in jumanji/wrappers.py
39 40 41 | |
observe(state)
#
Create observations from a batch of environment states.
Source code in jumanji/wrappers.py
369 370 371 | |
render(state)
#
Render the first environment state of the given batch. The remaining elements of the batched state are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
State object containing the current dynamics of the environment. |
required |
Source code in jumanji/wrappers.py
373 374 375 376 377 378 379 380 381 | |
reset(key)
#
Resets the environment to an initial state.
The first dimension of the key will dictate the number of concurrent environments.
To obtain a key with the right first dimension, you may call jax.random.split on key
with the parameter num representing the number of concurrent environments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNGKey
|
random keys used to reset the environments where the first dimension is the number of desired environments. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
state |
State
|
State object corresponding to the new state of the environments, |
timestep |
TimeStep[Observation]
|
TimeStep object corresponding the first timesteps returned by the environments, |
Source code in jumanji/wrappers.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
step(state, action)
#
Run one timestep of the environment's dynamics.
The first dimension of the state will dictate the number of concurrent environments.
See VmapWrapper.reset for more details on how to get a state of concurrent
environments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
State object containing the dynamics of the environments. |
required |
action
|
Array
|
Array containing the actions to take. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
state |
State
|
State object corresponding to the next states of the environments, |
timestep |
TimeStep[Observation]
|
TimeStep object corresponding the timesteps returned by the environments, |
Source code in jumanji/wrappers.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
Wrapper(env)
#
Bases: Environment[State, ActionSpec, Observation], Generic[State, ActionSpec, Observation]
Wraps the environment to allow modular transformations. Source: https://github.com/google/brax/blob/main/brax/envs/env.py#L72
Source code in jumanji/wrappers.py
39 40 41 | |
action_spec
cached
property
#
Returns the action spec.
discount_spec
cached
property
#
Returns the discount spec.
observation_spec
cached
property
#
Returns the observation spec.
reward_spec
cached
property
#
Returns the reward spec.
unwrapped
property
#
Returns the wrapped env.
close()
#
Perform any necessary cleanup.
Environments will automatically :meth:close() themselves when
garbage collected or when the program exits.
Source code in jumanji/wrappers.py
113 114 115 116 117 118 119 | |
observe(state)
#
Create an observation from an environment state.
Source code in jumanji/wrappers.py
81 82 83 | |
render(state)
#
Compute render frames during initialisation of the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
State object containing the dynamics of the environment. |
required |
Source code in jumanji/wrappers.py
105 106 107 108 109 110 111 | |
reset(key)
#
Resets the environment to an initial state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNGKey
|
random key used to reset the environment. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
state |
State
|
State object corresponding to the new state of the environment, |
timestep |
TimeStep[Observation]
|
TimeStep object corresponding the first timestep returned by the environment, |
Source code in jumanji/wrappers.py
56 57 58 59 60 61 62 63 64 65 66 | |
step(state, action)
#
Run one timestep of the environment's dynamics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
State object containing the dynamics of the environment. |
required |
action
|
Array
|
Array containing the action to take. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
state |
State
|
State object corresponding to the next state of the environment, |
timestep |
TimeStep[Observation]
|
TimeStep object corresponding the timestep returned by the environment, |
Source code in jumanji/wrappers.py
68 69 70 71 72 73 74 75 76 77 78 79 | |
add_obs_to_extras(timestep)
#
Place the observation in timestep.extras[NEXT_OBS_KEY_IN_EXTRAS]. Used when auto-resetting to store the observation from the terminal TimeStep (useful for e.g. truncation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestep
|
TimeStep[Observation]
|
TimeStep object containing the timestep returned by the environment. |
required |
Returns:
| Type | Description |
|---|---|
TimeStep[Observation]
|
timestep where the observation is placed in timestep.extras["next_obs"]. |
Source code in jumanji/wrappers.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
jumanji_to_gym_obs(observation)
#
Convert a Jumanji observation into a gym observation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observation
|
Observation
|
JAX pytree with (possibly nested) containers that
either have the |
required |
Returns:
| Type | Description |
|---|---|
GymObservation
|
Numpy array or nested dictionary of numpy arrays. |
Source code in jumanji/wrappers.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |