RandomGenerator#
safety_gymnasium.utils.random_generator#
- class safety_gymnasium.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:
set_placements_info()
: Set the placements information from task for each type of objects.set_random_seed()
: Instantiate anp.random.RandomState
object using given seed.build_layout()
: Try to sample within placement area of objects to find a layout.draw_placement()
: Sample an (x,y) location, based on potential placement areas.sample_layout()
: Sample a layout of all objects.sample_goal_position()
: Sample a position for goal.constrain_placement()
: Get constrained placement of objects considering keepout.generate_rots()
: Generate rotations of objects.randn()
: Sample a random number from a normal distribution.binomial()
: Sample a random number from a binomial distribution.random_rot()
: Sample a random rotation angle.choice()
: Sample a random element from a list.uniform()
: Sample a random number from a uniform distribution.
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_gymnasium.utils.random_generator.RandomGenerator.__init__(self) None #
Initialize the random number generator.
- safety_gymnasium.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_gymnasium.utils.random_generator.RandomGenerator.set_random_seed(self, seed: int) None #
Instantiate a
np.random.RandomState
object using given seed.
- safety_gymnasium.utils.random_generator.RandomGenerator.build_layout(self) dict #
Try to Sample within placement area of objects to find a layout.
- safety_gymnasium.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_gymnasium.utils.random_generator.RandomGenerator.sample_layout(self) bool #
Sample once within placement area of objects to find a layout.
returning
True
if successful, elseFalse
.
- safety_gymnasium.utils.random_generator.RandomGenerator.sample_goal_position(self) bool #
Sample a new goal position and return True, else False if sample rejected.
- safety_gymnasium.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_gymnasium.utils.random_generator.RandomGenerator.generate_rots(self, num: int = 1) list[float] #
Generate the rotations of the obstacle.
- safety_gymnasium.utils.random_generator.RandomGenerator.randn(self, *args, **kwargs) ndarray #
Wrapper for
np.random.RandomState.randn()
.
- safety_gymnasium.utils.random_generator.RandomGenerator.binomial(self, *args, **kwargs) ndarray #
Wrapper for
np.random.RandomState.binomial()
.
- safety_gymnasium.utils.random_generator.RandomGenerator.random_rot(self) float #
Use internal random state to get a random rotation in radians.
- safety_gymnasium.utils.random_generator.RandomGenerator.choice(self, *args, **kwargs) ndarray #
Wrapper for
np.random.RandomState.choice()
.
- safety_gymnasium.utils.random_generator.RandomGenerator.uniform(self, *args, **kwargs) ndarray #
Wrapper for
np.random.RandomState.uniform()
.