Environments
Contents
Environments#
Function
#
- class fragile.optimize.env.Function(function, bounds, custom_domain_check=None, actions_as_perturbations=True, start_same_pos=False, x0=None)[source]#
Environment that represents an arbitrary mathematical function bounded in a given interval.
- Parameters
- __init__(function, bounds, custom_domain_check=None, actions_as_perturbations=True, start_same_pos=False, x0=None)[source]#
Initialize a
Function
.- Parameters
function (Callable[[numpy.ndarray], numpy.ndarray]) – Callable that takes a batch of vectors (batched across the first dimension of the array) and returns a vector of typing.Scalar. This function is applied to a batch of walker observations.
bounds (Union[judo.data_structures.bounds.Bounds, gym.spaces.box.Box]) –
Bounds
that defines the domain of the function.custom_domain_check (Callable[[numpy.ndarray, numpy.ndarray, int], numpy.ndarray]) – Callable that checks points inside the bounds to know if they are in a custom domain when it is not just a set of rectangular bounds. It takes a batch of points as input and returns an array of booleans. Each
True
value indicates that the corresponding point is outside thecustom_domain_check
.actions_as_perturbations (bool) – If
True
the actions are interpreted as perturbations that will be applied to the past states. IfFalse
the actions are interpreted as the new states to be evaluated.start_same_pos (bool) –
x0 (numpy.ndarray) –
- property action_space: gym.spaces.box.Box#
Action space with the same characteristics as self.bounds.
- Return type
gym.spaces.box.Box
- calculate_oobs(points, rewards)[source]#
Determine if a given batch of vectors lie inside the function domain.
- Parameters
points (numpy.ndarray) – Array of batched vectors that will be checked to lie inside the
Function
bounds.rewards (numpy.ndarray) – Array containing the rewards of the current walkers.
- Returns
Array of booleans of length batch_size (points.shape[0]) that will be
True
if a given point of the batch lies outside the bounds, andFalse
otherwise.- Return type
numpy.ndarray
- classmethod from_bounds_params(function, shape=None, high=inf, low=- inf, custom_domain_check=None)[source]#
Initialize a function defining its shape and bounds without using a
Bounds
.- Parameters
function (Callable) – Callable that takes a batch of vectors (batched across the first dimension of the array) and returns a vector of typing.Scalar. This function is applied to a batch of walker observations.
shape (Optional[tuple]) – Input shape of the solution vector without taking into account the batch dimension. For example, a two-dimensional function applied to a batch of 5 walkers will have shape=(2,), even though the observations will have shape (5, 2)
high (Union[int, float, numpy.ndarray]) – Upper bound of the function domain. If it’s a typing.Scalar it will be the same for all dimensions. If it’s a numpy array it will be the upper bound for each dimension.
low (Union[int, float, numpy.ndarray]) – Lower bound of the function domain. If it’s a typing.Scalar it will be the same for all dimensions. If it’s a numpy array it will be the lower bound for each dimension.
custom_domain_check (Optional[Callable[[numpy.ndarray], numpy.ndarray]]) – Callable that checks points inside the bounds to know if they are in a custom domain when it is not just a set of rectangular bounds.
- Returns
Function
with itsBounds
created from the provided arguments.- Return type
- reset(inplace=True, root_walker=None, states=None, **kwargs)[source]#
Reset the
Function
to the start of a new episode and returns anStatesEnv
instance describing its internal state.
- sample_action(batch_size)[source]#
Return a matrix of points sampled uniformly from the
Function
domain.
- property shape: Tuple[int, ...]#
Return the shape of the environment.
- Return type
Tuple[int, Ellipsis]
Minimizer
#
- class fragile.optimize.env.Minimizer(function, bounds=None, **kwargs)[source]#
Apply
scipy.optimize.minimize
to aFunction
.- Parameters
function (fragile.core.env.Function) –
- __init__(function, bounds=None, **kwargs)[source]#
Initialize a
Minimizer
.- Parameters
function (fragile.core.env.Function) –
Function
that will be minimized.bounds –
Bounds
defining the domain of the minimization process. If it isNone
theFunction
Bounds
will be used.*args – Passed to
scipy.optimize.minimize
.**kwargs – Passed to
scipy.optimize.minimize
.
- minimize(x)[source]#
Apply
scipy.optimize.minimize
to a single point.- Parameters
x (numpy.ndarray) – Array representing a single point of the function to be minimized.
- Returns
Optimization result object returned by
scipy.optimize.minimize
.
- minimize_batch(x)[source]#
Minimize a batch of points.
- Parameters
x (numpy.ndarray) – Array representing a batch of points to be optimized, stacked across the first dimension.
- Returns
Tuple of arrays containing the local optimum found for each point, and an array with the values assigned to each of the points found.
- Return type
Tuple[numpy.ndarray, numpy.ndarray]
MinimizerWrapper
#
- class fragile.optimize.env.MinimizerWrapper(function, **kwargs)[source]#
Wrapper that applies a local minimization process to the observations returned by a
Function
.- Parameters
function (fragile.core.env.Function) –
- __init__(function, **kwargs)[source]#
Initialize a
MinimizerWrapper
.- Parameters
function (fragile.core.env.Function) –
Function
to be minimized after each step.*args – Passed to the internal
Optimizer
.**kwargs – Passed to the internal
Optimizer
.
- property bounds: judo.data_structures.bounds.Bounds#
Return the bounds of the wrapped environment.
- Return type
judo.Bounds
- property custom_domain_check: Callable#
Return the custom_domain_check of the wrapped environment.
- Return type
Callable
- property function: Callable#
Return the function of the wrapped environment.
- Return type
Callable