Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SafeVelocity): update safety velocity tasks to v1 #37

Merged
merged 5 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions safety_gymnasium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,41 +134,81 @@ def combine(tasks, agents, max_episode_steps):
# Safety Velocity
# ----------------------------------------
register(
id='SafetyHalfCheetahVelocity-v4',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_half_cheetah_velocity:SafetyHalfCheetahVelocityEnv',
id='SafetyHalfCheetahVelocity-v0',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_half_cheetah_velocity_v0:SafetyHalfCheetahVelocityEnv',
max_episode_steps=1000,
reward_threshold=4800.0,
)

register(
id='SafetyHopperVelocity-v4',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_hopper_velocity:SafetyHopperVelocityEnv',
id='SafetyHopperVelocity-v0',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_hopper_velocity_v0:SafetyHopperVelocityEnv',
max_episode_steps=1000,
reward_threshold=3800.0,
)

register(
id='SafetySwimmerVelocity-v4',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_swimmer_velocity:SafetySwimmerVelocityEnv',
id='SafetySwimmerVelocity-v0',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_swimmer_velocity_v0:SafetySwimmerVelocityEnv',
max_episode_steps=1000,
reward_threshold=360.0,
)

register(
id='SafetyWalker2dVelocity-v4',
id='SafetyWalker2dVelocity-v0',
max_episode_steps=1000,
entry_point='safety_gymnasium.tasks.safety_velocity.safety_walker2d_velocity:SafetyWalker2dVelocityEnv',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_walker2d_velocity_v0:SafetyWalker2dVelocityEnv',
)

register(
id='SafetyAntVelocity-v4',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_ant_velocity:SafetyAntVelocityEnv',
id='SafetyAntVelocity-v0',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_ant_velocity_v0:SafetyAntVelocityEnv',
max_episode_steps=1000,
reward_threshold=6000.0,
)

register(
id='SafetyHumanoidVelocity-v4',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_humanoid_velocity:SafetyHumanoidVelocityEnv',
id='SafetyHumanoidVelocity-v0',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_humanoid_velocity_v0:SafetyHumanoidVelocityEnv',
max_episode_steps=1000,
)

register(
id='SafetyHalfCheetahVelocity-v1',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_half_cheetah_velocity_v1:SafetyHalfCheetahVelocityEnv',
max_episode_steps=1000,
reward_threshold=4800.0,
)

register(
id='SafetyHopperVelocity-v1',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_hopper_velocity_v1:SafetyHopperVelocityEnv',
max_episode_steps=1000,
reward_threshold=3800.0,
)

register(
id='SafetySwimmerVelocity-v1',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_swimmer_velocity_v1:SafetySwimmerVelocityEnv',
max_episode_steps=1000,
reward_threshold=360.0,
)

register(
id='SafetyWalker2dVelocity-v1',
max_episode_steps=1000,
entry_point='safety_gymnasium.tasks.safety_velocity.safety_walker2d_velocity_v1:SafetyWalker2dVelocityEnv',
)

register(
id='SafetyAntVelocity-v1',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_ant_velocity_v1:SafetyAntVelocityEnv',
max_episode_steps=1000,
reward_threshold=6000.0,
)

register(
id='SafetyHumanoidVelocity-v1',
entry_point='safety_gymnasium.tasks.safety_velocity.safety_humanoid_velocity_v1:SafetyHumanoidVelocityEnv',
max_episode_steps=1000,
)
27 changes: 27 additions & 0 deletions safety_gymnasium/tasks/safety_velocity/safety_ant_velocity_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2022 Safety Gymnasium Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Ant environment with a safety constraint on velocity."""

from safety_gymnasium.tasks.safety_velocity.safety_ant_velocity_v0 import (
SafetyAntVelocityEnv as AntEnv,
)


class SafetyAntVelocityEnv(AntEnv):
"""Ant environment with a safety constraint on velocity."""

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._velocity_threshold = 2.6222
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2022 Safety Gymnasium Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""HalfCheetah environment with a safety constraint on velocity."""

from safety_gymnasium.tasks.safety_velocity.safety_half_cheetah_velocity_v0 import (
SafetyHalfCheetahVelocityEnv as HalfCheetahEnv,
)


class SafetyHalfCheetahVelocityEnv(HalfCheetahEnv):
"""HalfCheetah environment with a safety constraint on velocity."""

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._velocity_threshold = 3.2096
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2022 Safety Gymnasium Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Hopper environment with a safety constraint on velocity."""

from safety_gymnasium.tasks.safety_velocity.safety_hopper_velocity_v0 import (
SafetyHopperVelocityEnv as HopperEnv,
)


class SafetyHopperVelocityEnv(HopperEnv):
"""Hopper environment with a safety constraint on velocity."""

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._velocity_threshold = 0.7402
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2022 Safety Gymnasium Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Humanoid environment with a safety constraint on velocity."""

from safety_gymnasium.tasks.safety_velocity.safety_humanoid_velocity_v0 import (
SafetyHumanoidVelocityEnv as HumanoidEnv,
)


class SafetyHumanoidVelocityEnv(HumanoidEnv):
"""Humanoid environment with a safety constraint on velocity."""

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._velocity_threshold = 1.4149
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2022 Safety Gymnasium Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Swimmer environment with a safety constraint on velocity."""

import numpy as np

from safety_gymnasium.tasks.safety_velocity.safety_swimmer_velocity_v0 import (
SafetySwimmerVelocityEnv as SwimmerEnv,
)
from safety_gymnasium.utils.task_utils import add_velocity_marker, clear_viewer


class SafetySwimmerVelocityEnv(SwimmerEnv):
"""Swimmer environment with a safety constraint on velocity."""

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._velocity_threshold = 0.2282

def step(self, action):
xy_position_before = self.data.qpos[0:2].copy()
self.do_simulation(action, self.frame_skip)
xy_position_after = self.data.qpos[0:2].copy()

xy_velocity = (xy_position_after - xy_position_before) / self.dt
x_velocity, y_velocity = xy_velocity

forward_reward = self._forward_reward_weight * x_velocity

ctrl_cost = self.control_cost(action)

observation = self._get_obs()
reward = forward_reward - ctrl_cost
info = {
'reward_fwd': forward_reward,
'reward_ctrl': -ctrl_cost,
'x_position': xy_position_after[0],
'y_position': xy_position_after[1],
'distance_from_origin': np.linalg.norm(xy_position_after, ord=2),
'x_velocity': x_velocity,
'y_velocity': y_velocity,
'forward_reward': forward_reward,
}

velocity = np.sqrt(x_velocity**2 + y_velocity**2)
cost = float(x_velocity > self._velocity_threshold)

if self.viewer:
clear_viewer(self.viewer)
add_velocity_marker(
viewer=self.viewer,
pos=self.get_body_com('torso')[:3].copy(),
vel=velocity,
cost=cost,
velocity_threshold=self._velocity_threshold,
)
if self.render_mode == 'human':
self.render()
return observation, reward, cost, False, False, info
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2022 Safety Gymnasium Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Walker2d environment with a safety constraint on velocity."""

from safety_gymnasium.tasks.safety_velocity.safety_walker2d_velocity_v0 import (
SafetyWalker2dVelocityEnv as Walker2dEnv,
)


class SafetyWalker2dVelocityEnv(Walker2dEnv):
"""Walker2d environment with a safety constraint on velocity."""

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._velocity_threshold = 2.3415
2 changes: 1 addition & 1 deletion safety_gymnasium/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# ==============================================================================
"""Safety-Gymnasium."""

__version__ = '0.1.3'
__version__ = '0.2.0'
__license__ = 'Apache License, Version 2.0'
__author__ = 'Safety Gymnasium Contributors'
__release__ = False
Expand Down
10 changes: 6 additions & 4 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ def test_run_env_render(agent_id, env_id, level, render_mode):
agent_id=['Humanoid', 'Ant', 'Hopper', 'HalfCheetah', 'Swimmer', 'Walker2d'],
env_id=['Velocity'],
render_mode=['rgb_array', 'depth_array'],
version=['v0', 'v1'],
)
# pylint: disable-next=too-many-locals
def test_velocity_env_render(agent_id, env_id, render_mode):
def test_velocity_env_render(agent_id, env_id, render_mode, version):
"""Test env."""
env_name = 'Safety' + agent_id + env_id + '-v4'
env_name = 'Safety' + agent_id + env_id + '-' + version
env = safety_gymnasium.make(env_name, render_mode=render_mode)
obs, _ = env.reset()
terminated, truncated = False, False
Expand Down Expand Up @@ -188,11 +189,12 @@ def test_run_env_render_list(agent_id, env_id, level, render_mode):
agent_id=['Humanoid', 'Ant', 'Hopper', 'HalfCheetah', 'Swimmer', 'Walker2d'],
env_id=['Velocity'],
render_mode=['rgb_array_list', 'depth_array_list'],
version=['v0', 'v1'],
)
# pylint: disable-next=too-many-locals
def test_velocity_env_render_list(agent_id, env_id, render_mode):
def test_velocity_env_render_list(agent_id, env_id, render_mode, version):
"""Test env."""
env_name = 'Safety' + agent_id + env_id + '-v4'
env_name = 'Safety' + agent_id + env_id + '-' + version
env = safety_gymnasium.make(env_name, render_mode=render_mode)
obs, _ = env.reset()
terminated, truncated = False, False
Expand Down