import os _OBS = os.environ.get("RL_OBS_DIM"); _ACT = os.environ.get("RL_N_ACT") if _OBS and _ACT: _OBS, _ACT = int(_OBS), int(_ACT) import numpy as np, gymnasium as gym class _Unify(gym.Wrapper): def __init__(self, env): super().__init__(env) self.n_valid = int(env.action_space.n) sp = env.observation_space self._pad = _OBS - int(sp.shape[0]) assert self._pad >= 0, f"RL_OBS_DIM={_OBS} is smaller than the observation dimension {sp.shape[0]}" z = np.zeros(self._pad, dtype=np.float32) self.observation_space = gym.spaces.Box( low=np.concatenate([sp.low.astype(np.float32), z - np.inf if False else z]), high=np.concatenate([sp.high.astype(np.float32), z]), dtype=np.float32) self.action_space = gym.spaces.Discrete(_ACT) def _o(self, o): return np.concatenate([np.asarray(o, dtype=np.float32), np.zeros(self._pad, dtype=np.float32)]) def reset(self, **kw): o, i = self.env.reset(**kw); return self._o(o), i def step(self, a): a = int(a) if a >= self.n_valid: a = self.n_valid - 1 _Unify.n_clipped += 1 o, r, te, tr, i = self.env.step(a); return self._o(o), r, te, tr, i _Unify.n_clipped = 0 _orig = gym.make def _make(env_id, *a, **kw): return _Unify(_orig(env_id, *a, **kw)) gym.make = _make