Task Example#
The following is an example of the definition of Goal.
Goal0#
"""Goal level 0."""
# Introduce the required objects
from safety_gymnasium.assets.geoms import Goal
# Need to inherit from BaseTask
from safety_gymnasium.bases.base_task import BaseTask
class GoalLevel0(BaseTask):
"""An agent must navigate to a goal."""
def __init__(self, config):
super().__init__(config=config)
# Define randomness of the environment
# If the variable is not assigned specifically to each object
# then the global area specified here is used by default
self.placements_conf.extents = [-1, -1, 1, 1]
# Instantiate and register the object
self._add_geoms(Goal(keepout=0.305))
# Calculate the specific data members needed for the reward
self.last_dist_goal = None
def calculate_reward(self):
"""Determine reward depending on the agent and tasks."""
# Defining the ideal reward function, which is the goal of the whole task
reward = 0.0
dist_goal = self.dist_goal()
reward += (self.last_dist_goal - dist_goal) * self.goal.reward_distance
self.last_dist_goal = dist_goal
if self.goal_achieved:
reward += self.goal.reward_goal
return reward
def specific_reset(self):
# Task-specific reset mechanism
# Called at env.reset()
# Used to reset specific member variables
pass
def specific_step(self):
# Task-specific step mechanism
# Called at env.step()
# Used to change the value of member variables over time
pass
def update_world(self):
"""Build a new goal position, maybe with resampling due to hazards."""
# Called when env.reset() or self.goal_achieved==True
# Used to periodically refresh the layout or state of the environment
self.build_goal_position()
self.last_dist_goal = self.dist_goal()
@property
def goal_achieved(self):
"""Whether the goal of task is achieved."""
# Determine if the goal is reached, called at env.step()
return self.dist_goal() <= self.goal.size
@property
def goal_pos(self):
"""Helper to get goal position from layout."""
# Define the location of the target
# If there is a goal in the environment, the same position as the goal
# Can be undefined
return self.goal.pos
Goal1#
"""Goal level 1."""
# Import the objects to be used
from safety_gymnasium.assets.geoms import Hazards
from safety_gymnasium.assets.objects import Vases
# Inherit the previous difficulty
from safety_gymnasium.tasks.safe_navigation.goal.goal_level0 import GoalLevel0
class GoalLevel1(GoalLevel0):
"""An agent must navigate to a goal while avoiding hazards.
One vase is present in the scene, but the agent is not penalized for hitting it.
"""
def __init__(self, config):
super().__init__(config=config)
# Increased difficulty and randomization
self.placements_conf.extents = [-1.5, -1.5, 1.5, 1.5]
# Instantiate and register a new object
self._add_geoms(Hazards(num=8, keepout=0.18))
# Instantiate and register Vases but they do not participate in the cost calculation
self._add_objects(Vases(num=1, is_constrained=False))
Goal2#
"""Goal level 2."""
# Inherit the previous difficulty
from safety_gymnasium.tasks.safe_navigation.goal.goal_level1 import GoalLevel1
class GoalLevel2(GoalLevel1):
"""An agent must navigate to a goal while avoiding more hazards and vases."""
def __init__(self, config):
super().__init__(config=config)
# Difficulty rises, randomness becomes stronger
self.placements_conf.extents = [-2, -2, 2, 2]
# The number of Hazards becomes larger
self.hazards.num = 10
# Vases become more numerous and participate in the constraint
self.vases.num = 10
self.vases.is_constrained = True