RandomGenerator

safety_gymnasium2.utils.random_generator

class safety_gymnasium2.utils.random_generator.RandomGenerator

A random number generator that can be seeded and reset.

Used to generate random numbers for placement of objects. And there is only one instance in a single environment which is in charge of all randomness.

Methods:

Attributes:

  • random_generator (np.random.RandomState): Random number generator.

  • placements (dict): Potential placement areas.

  • placements_extents (list): Extents of potential placement areas.

  • placements_margin (float): Margin of potential placement areas.

  • layout (Dict[str, dict]): Layout of objects which is generated by this class.

Note

Information about placements is set by set_placements_info() method in the instance of specific environment, and we just utilize these to generate randomness here.

Initialize the random number generator.

Methods

safety_gymnasium2.utils.random_generator.RandomGenerator.__init__(self) → None

Initialize the random number generator.

safety_gymnasium2.utils.random_generator.RandomGenerator.set_placements_info(self, placements: dict, placements_extents: list, placements_margin: float) → None

Set the placements information from task for each type of objects.

safety_gymnasium2.utils.random_generator.RandomGenerator.set_random_seed(self, seed: int) → None

Instantiate a np.random.RandomState object using given seed.

safety_gymnasium2.utils.random_generator.RandomGenerator.build_layout(self) → dict

Try to Sample within placement area of objects to find a layout.

safety_gymnasium2.utils.random_generator.RandomGenerator.draw_placement(self, placements: dict, keepout: float) → ndarray

Sample an (x,y) location, based on potential placement areas.

Parameters:
  • placements (dict) – A list of (xmin, xmax, ymin, ymax) tuples that specify rectangles in the XY-plane where an object could be placed.

  • keepout (float) – Describes how much space an object is required to have around it, where that keepout space overlaps with the placement rectangle.

Note

To sample an (x,y) pair, first randomly select which placement rectangle to sample from, where the probability of a rectangle is weighted by its area. If the rectangles are disjoint, there’s an equal chance the (x,y) location will wind up anywhere in the placement space. If they overlap, then overlap areas are double-counted and will have higher density. This allows the user some flexibility in building placement distributions. Finally, randomly draw a uniform point within the selected rectangle.

safety_gymnasium2.utils.random_generator.RandomGenerator.sample_layout(self) → bool

Sample once within placement area of objects to find a layout.

returning True if successful, else False.

safety_gymnasium2.utils.random_generator.RandomGenerator.sample_goal_position(self) → bool

Sample a new goal position and return True, else False if sample rejected.

safety_gymnasium2.utils.random_generator.RandomGenerator.constrain_placement(self, placement: dict, keepout: float) → tuple[float]

Helper function to constrain a single placement by the keepout radius.

safety_gymnasium2.utils.random_generator.RandomGenerator.generate_rots(self, num: int = 1) → list[float]

Generate the rotations of the obstacle.

safety_gymnasium2.utils.random_generator.RandomGenerator.randn(self, *args, **kwargs) → ndarray

Wrapper for np.random.RandomState.randn().

safety_gymnasium2.utils.random_generator.RandomGenerator.binomial(self, *args, **kwargs) → ndarray

Wrapper for np.random.RandomState.binomial().

safety_gymnasium2.utils.random_generator.RandomGenerator.random_rot(self) → float

Use internal random state to get a random rotation in radians.

safety_gymnasium2.utils.random_generator.RandomGenerator.choice(self, *args, **kwargs) → ndarray

Wrapper for np.random.RandomState.choice().

safety_gymnasium2.utils.random_generator.RandomGenerator.uniform(self, *args, **kwargs) → ndarray

Wrapper for np.random.RandomState.uniform().