Skip to content

Commit bb0cc50

Browse files
committed
fix(sim): env to use correct seeding form gym np random
1 parent 0d642b3 commit bb0cc50

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

python/rcs/envs/base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import gymnasium as gym
99
import numpy as np
1010
from greenlet import getcurrent, greenlet
11+
from gymnasium.utils import seeding
1112
from rcs._core.common import Hand, RobotPlatform
1213
from rcs.camera.interface import BaseCameraSet
1314
from rcs.envs.space_utils import (
@@ -242,16 +243,18 @@ def step_sim(self):
242243
self.sim.step_until_convergence()
243244

244245
def apply_sim_state(self):
246+
# TODO: would a mj.forward be good enough here or even cleaner?
245247
self.sim.step(2)
246248

247249
def reset(
248250
self, *, seed: int | None = None, options: dict[str, Any] | None = None
249251
) -> tuple[dict[str, Any], dict[str, Any]]:
252+
assert seed is None, "seed should never arrive here, did you forget to add the CoverWrapper?"
250253
if self.main_greenlet is not None:
251254
self.main_greenlet.switch()
252255
else:
253256
self.apply_sim_state()
254-
return super().reset(seed=seed, options=options)
257+
return super().reset(seed=None, options=options)
255258

256259
def observation(self, observation: dict[str, Any], info: dict[str, Any]) -> tuple[dict[str, Any], dict[str, Any]]:
257260
sim_state = self.sim.get_state()
@@ -274,7 +277,10 @@ def reset(
274277
if self.env.get_wrapper_attr("PLATFORM") == RobotPlatform.SIMULATION:
275278
sim = cast(simulation.Sim, self.get_wrapper_attr("sim"))
276279
sim.reset()
277-
return super().reset(seed=seed, options=options)
280+
if seed is not None:
281+
# seed only once at the top of the stack
282+
self.np_random, _ = seeding.np_random(seed)
283+
return super().reset(seed=None, options=options)
278284

279285

280286
class RobotWrapper(ActObsInfoWrapper):

python/rcs/envs/tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ def reset(
111111
) -> tuple[dict[str, Any], dict[str, Any]]:
112112

113113
# place randomly in square
114-
pos_x = np.random.uniform(-self.x_width / 2, self.x_width / 2)
115-
pos_y = np.random.uniform(-self.y_width / 2, self.y_width / 2)
114+
pos_x = self.np_random.uniform(-self.x_width / 2, self.x_width / 2)
115+
pos_y = self.np_random.uniform(-self.y_width / 2, self.y_width / 2)
116116

117117
if self.include_rotation:
118118
# 1. Sample a random angle between 0 and 2*pi (360 degrees)
119-
theta = np.random.uniform(0, 2 * np.pi)
119+
theta = self.np_random.uniform(0, 2 * np.pi)
120120

121121
# 2. Convert the angle to a unit quaternion for the Z-axis
122122
qw = np.cos(theta / 2)

0 commit comments

Comments
 (0)