Spaces:
Running
Running
File size: 1,415 Bytes
375a1cf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
"""
Utility functions used for classic control environments.
"""
from typing import Optional, SupportsFloat, Tuple
def verify_number_and_cast(x: SupportsFloat) -> float:
"""Verify parameter is a single number and cast to a float."""
try:
x = float(x)
except (ValueError, TypeError):
raise ValueError(f"An option ({x}) could not be converted to a float.")
return x
def maybe_parse_reset_bounds(
options: Optional[dict], default_low: float, default_high: float
) -> Tuple[float, float]:
"""
This function can be called during a reset() to customize the sampling
ranges for setting the initial state distributions.
Args:
options: Options passed in to reset().
default_low: Default lower limit to use, if none specified in options.
default_high: Default upper limit to use, if none specified in options.
Returns:
Tuple of the lower and upper limits.
"""
if options is None:
return default_low, default_high
low = options.get("low") if "low" in options else default_low
high = options.get("high") if "high" in options else default_high
# We expect only numerical inputs.
low = verify_number_and_cast(low)
high = verify_number_and_cast(high)
if low > high:
raise ValueError(
f"Lower bound ({low}) must be lower than higher bound ({high})."
)
return low, high
|