2023-05-23 15:46:04 +01:00
|
|
|
"""Vector wrapper for converting between NumPy and Jax."""
|
2024-06-10 17:07:47 +01:00
|
|
|
|
2023-05-23 15:46:04 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import jax.numpy as jnp
|
2025-05-12 00:10:06 +02:00
|
|
|
import numpy as np
|
2023-05-23 15:46:04 +01:00
|
|
|
|
|
|
|
from gymnasium.error import DependencyNotInstalled
|
2025-05-12 00:10:06 +02:00
|
|
|
from gymnasium.vector import VectorEnv
|
|
|
|
from gymnasium.wrappers.vector.array_conversion import ArrayConversion
|
2023-05-23 15:46:04 +01:00
|
|
|
|
|
|
|
|
2023-11-07 13:27:25 +00:00
|
|
|
__all__ = ["JaxToNumpy"]
|
2023-06-21 17:04:11 +01:00
|
|
|
|
|
|
|
|
2025-05-12 00:10:06 +02:00
|
|
|
class JaxToNumpy(ArrayConversion):
|
2023-05-23 15:46:04 +01:00
|
|
|
"""Wraps a jax vector environment so that it can be interacted with through numpy arrays.
|
|
|
|
|
|
|
|
Notes:
|
2023-11-07 13:27:25 +00:00
|
|
|
A vectorized version of ``gymnasium.wrappers.JaxToNumpy``
|
2023-05-23 15:46:04 +01:00
|
|
|
|
|
|
|
Actions must be provided as numpy arrays and observations, rewards, terminations and truncations will be returned as numpy arrays.
|
2023-11-07 13:27:25 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
>>> import gymnasium as gym # doctest: +SKIP
|
|
|
|
>>> envs = gym.make_vec("JaxEnv-vx", 3) # doctest: +SKIP
|
|
|
|
>>> envs = JaxToNumpy(envs) # doctest: +SKIP
|
2023-05-23 15:46:04 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, env: VectorEnv):
|
|
|
|
"""Wraps an environment such that the input and outputs are numpy arrays.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
env: the vector jax environment to wrap
|
|
|
|
"""
|
|
|
|
if jnp is None:
|
|
|
|
raise DependencyNotInstalled(
|
2024-04-06 15:44:09 +01:00
|
|
|
'Jax is not installed, run `pip install "gymnasium[jax]"`'
|
2023-05-23 15:46:04 +01:00
|
|
|
)
|
2025-05-12 00:10:06 +02:00
|
|
|
super().__init__(env, env_xp=jnp, target_xp=np)
|