-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SafeVelocity): update safety velocity tasks to v1 (#37)
- Loading branch information
Showing
15 changed files
with
265 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
safety_gymnasium/tasks/safety_velocity/safety_ant_velocity_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
safety_gymnasium/tasks/safety_velocity/safety_half_cheetah_velocity_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
safety_gymnasium/tasks/safety_velocity/safety_hopper_velocity_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
safety_gymnasium/tasks/safety_velocity/safety_humanoid_velocity_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
File renamed without changes.
71 changes: 71 additions & 0 deletions
71
safety_gymnasium/tasks/safety_velocity/safety_swimmer_velocity_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
safety_gymnasium/tasks/safety_velocity/safety_walker2d_velocity_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters