Skip to content

Commit

Permalink
[gym/common] Remove 'enforce_bounded_spaces' BaseJiminyEnv option by …
Browse files Browse the repository at this point in the history
…lack of use-case.
  • Loading branch information
duburcqa committed May 2, 2024
1 parent 0fcecd9 commit bf67b37
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 72 deletions.
15 changes: 13 additions & 2 deletions python/gym_jiminy/common/gym_jiminy/common/bases/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
quantities only once per step, and gathering similar quantities in a large
batch to leverage vectorization of math instructions.
"""
import re
import weakref
from weakref import ReferenceType
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -242,6 +243,12 @@ class and all their constructor keyword-arguments
self.parent = parent
self.auto_refresh = auto_refresh

# Make sure that all requirement names would be valid as property
requirement_names = requirements.keys()
if any(re.match('[^A-Za-z0-9_]', name) for name in requirement_names):
raise ValueError("The name of all quantity requirements should be "
"ASCII alphanumeric characters plus underscore.")

# Instantiate intermediary quantities if any
self.requirements: Dict[str, AbstractQuantity] = {
name: cls(env, self, **kwargs)
Expand All @@ -268,7 +275,7 @@ class and all their constructor keyword-arguments
def get_value(name: str, quantity: AbstractQuantity) -> Any:
return quantity.requirements[name].get()

for name in self.requirements.keys():
for name in requirement_names:
setattr(type(self), name, property(partial(get_value, name)))

def __getattr__(self, name: str) -> Any:
Expand All @@ -283,7 +290,11 @@ def __getattr__(self, name: str) -> Any:
:param name: Name of the requested quantity.
"""
return self.__getattribute__('requirements')[name].get()
try:
return self.__getattribute__('requirements')[name].get()
except KeyError as e:
raise AttributeError(
f"'{type(self)}' object has no attribute '{name}'") from e

def __dir__(self) -> List[str]:
"""Attribute lookup.
Expand Down
59 changes: 0 additions & 59 deletions python/gym_jiminy/common/gym_jiminy/common/envs/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class BaseJiminyEnv(InterfaceJiminyEnv[ObsT, ActT],
def __init__(self,
simulator: Simulator,
step_dt: float,
enforce_bounded_spaces: bool = False,
debug: bool = False,
render_mode: Optional[str] = None,
**kwargs: Any) -> None:
Expand All @@ -136,11 +135,6 @@ def __init__(self,
snapshot as an RGB array without showing it on the screen.
Optional: 'human' by default if available with the current
backend (or default if none), 'rgb_array' otherwise.
:param enforce_bounded_spaces:
Whether to enforce finite bounds for the observation and action
spaces. If so, then '\*_MAX' are used whenever it is necessary.
Note that whose bounds are very spread to make sure it is suitable
for the vast majority of systems.
:param debug: Whether the debug mode must be enabled. Doing it enables
telemetry recording.
:param render_mode: Desired rendering mode, ie "human" or "rgb_array".
Expand Down Expand Up @@ -194,7 +188,6 @@ def __init__(self,
self.simulator: Simulator = simulator
self._step_dt = step_dt
self.render_mode = render_mode
self.enforce_bounded_spaces = enforce_bounded_spaces
self.debug = debug

# Define some proxies for fast access.
Expand Down Expand Up @@ -352,23 +345,6 @@ def _get_agent_state_space(
position_limit_lower = self.robot.position_limit_lower
velocity_limit = self.robot.velocity_limit

# Replace inf bounds of the state space if requested
if self.enforce_bounded_spaces:
if self.robot.has_freeflyer:
position_limit_lower[:3] = -FREEFLYER_POS_TRANS_MAX
position_limit_upper[:3] = +FREEFLYER_POS_TRANS_MAX
velocity_limit[:3] = FREEFLYER_VEL_LIN_MAX
velocity_limit[3:6] = FREEFLYER_VEL_ANG_MAX

for joint_index in self.robot.flexibility_joint_indices:
joint_velocity_index = (
self.robot.pinocchio_model.joints[joint_index].idx_v)
velocity_limit[
joint_velocity_index + np.arange(3)] = FLEX_VEL_ANG_MAX

if not model_options['joints']['enableVelocityLimit']:
velocity_limit[joint_velocity_indices] = JOINT_VEL_MAX

# Deduce bounds associated the theoretical model from the extended one
if use_theoretical_model:
position_limit_lower, position_limit_upper = map(
Expand Down Expand Up @@ -479,32 +455,6 @@ def _get_measurements_space(self) -> spaces.Dict:
sensor_space_upper[effort.type][0, sensor_index] = (
command_limit[motor_velocity_index])

# Replace inf bounds of the imu sensor space
if self.enforce_bounded_spaces:
# Replace inf bounds of the contact sensor space
if contact.type in sensor_measurements.keys():
sensor_space_lower[contact.type][:] = -SENSOR_FORCE_MAX
sensor_space_upper[contact.type][:] = SENSOR_FORCE_MAX

# Replace inf bounds of the force sensor space
if force.type in sensor_measurements.keys():
sensor_space_lower[force.type][:3] = -SENSOR_FORCE_MAX
sensor_space_upper[force.type][:3] = SENSOR_FORCE_MAX
sensor_space_lower[force.type][3:] = -SENSOR_MOMENT_MAX
sensor_space_upper[force.type][3:] = SENSOR_MOMENT_MAX

# Replace inf bounds of the imu sensor space
if imu.type in sensor_measurements.keys():
gyro_index = [
field.startswith('Gyro') for field in imu.fieldnames]
sensor_space_lower[imu.type][gyro_index] = -SENSOR_GYRO_MAX
sensor_space_upper[imu.type][gyro_index] = SENSOR_GYRO_MAX

accel_index = [
field.startswith('Accel') for field in imu.fieldnames]
sensor_space_lower[imu.type][accel_index] = -SENSOR_ACCEL_MAX
sensor_space_upper[imu.type][accel_index] = SENSOR_ACCEL_MAX

return spaces.Dict(OrderedDict(
(key, spaces.Box(low=min_val, high=max_val, dtype=np.float64))
for (key, min_val), max_val in zip(
Expand All @@ -524,15 +474,6 @@ def _initialize_action_space(self) -> None:
# Get effort limit
command_limit = self.robot.command_limit

# Replace inf bounds of the effort limit if requested
if self.enforce_bounded_spaces:
for motor_name in self.robot.motor_names:
motor = self.robot.get_motor(motor_name)
motor_options = motor.get_options()
if not motor_options["enableCommandLimit"]:
command_limit[motor.joint_velocity_index] = \
MOTOR_EFFORT_MAX

# Set the action space
action_scale = command_limit[self.robot.motor_velocity_indices]
self.action_space = spaces.Box(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def __init__(self,
mesh_path_dir: Optional[str] = None,
simulation_duration_max: float = DEFAULT_SIMULATION_DURATION,
step_dt: float = DEFAULT_STEP_DT,
enforce_bounded_spaces: bool = False,
reward_mixture: Optional[dict] = None,
std_ratio: Optional[dict] = None,
config_path: Optional[str] = None,
Expand All @@ -101,9 +100,6 @@ def __init__(self,
:param simulation_duration_max: Maximum duration of a simulation before
returning done.
:param step_dt: Simulation timestep for learning.
:param enforce_bounded_spaces:
Whether to enforce finite bounds for the observation and action
spaces. If so, '\*_MAX' are used whenever it is necessary.
:param reward_mixture: Weighting factors of selected contributions to
total reward.
:param std_ratio: Relative standard deviation of selected contributions
Expand Down Expand Up @@ -206,8 +202,7 @@ def __init__(self,
simulator.import_options(config_path)

# Initialize base class
super().__init__(
simulator, step_dt, enforce_bounded_spaces, debug, **kwargs)
super().__init__(simulator, step_dt, debug, **kwargs)

def _setup(self) -> None:
"""Configure the environment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ def __init__(self,
# Make sure that a parent has been specified
assert isinstance(parent, (FrameQuantity, MultiFrameQuantity))

# Call base implementation
super().__init__(env, parent, requirements={}, auto_refresh=False)

# Set unique identifier based on parent type
self.identifier = hash(type(parent))

# Call base implementation
super().__init__(env, parent, requirements={}, auto_refresh=False)

# Initialize the ordered list of frame names
self.frame_names: Set[str] = set()

Expand Down
3 changes: 1 addition & 2 deletions python/gym_jiminy/envs/gym_jiminy/envs/ant.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ def __init__(self, debug: bool = False, **kwargs: Any) -> None:
simulator=simulator,
debug=debug,
**{**dict(
step_dt=STEP_DT,
enforce_bounded_spaces=False),
step_dt=STEP_DT),
**kwargs})

# Define observation slices proxy for fast access.
Expand Down

0 comments on commit bf67b37

Please sign in to comment.