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
  • function (Callable[[numpy.ndarray], numpy.ndarray]) –

  • bounds (Union[judo.data_structures.bounds.Bounds, gym.spaces.box.Box]) –

  • custom_domain_check (Callable[[numpy.ndarray, numpy.ndarray, int], numpy.ndarray]) –

  • actions_as_perturbations (bool) –

  • start_same_pos (bool) –

  • x0 (numpy.ndarray) –

__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 the custom_domain_check.

  • actions_as_perturbations (bool) – If True the actions are interpreted as perturbations that will be applied to the past states. If False 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, and False 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 its Bounds created from the provided arguments.

Return type

fragile.core.env.Function

property n_dims: int#

Return the number of dimensions of the function to be optimized.

Return type

int

reset(inplace=True, root_walker=None, states=None, **kwargs)[source]#

Reset the Function to the start of a new episode and returns an StatesEnv instance describing its internal state.

Parameters
Return type

Union[None, Dict[str, Union[list, numpy.ndarray]]]

sample_action(batch_size)[source]#

Return a matrix of points sampled uniformly from the Function domain.

Parameters

batch_size (int) – Number of points that will be sampled.

Returns

Array containing batch_size points that lie inside the Function domain, stacked across the first dimension.

Return type

numpy.ndarray

property shape: Tuple[int, ...]#

Return the shape of the environment.

Return type

Tuple[int, Ellipsis]

step(actions, observs, **kwargs)[source]#

Sum the target action to the observations to obtain the new points, and evaluate the reward and boundary conditions.

Returns

Dictionary containing the information of the new points evaluated.

{"states": new_points, "observs": new_points, "rewards": typing.Scalar array,              "oobs": boolean array}

Return type

Dict[str, Union[list, numpy.ndarray]]

Minimizer#

class fragile.optimize.env.Minimizer(function, bounds=None, **kwargs)[source]#

Apply scipy.optimize.minimize to a Function.

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.

  • boundsBounds defining the domain of the minimization process. If it is None the Function 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]

minimize_point(x)[source]#

Minimize the target function passing one starting point.

Parameters

x (numpy.ndarray) – Array representing a single point of the function to be minimized.

Returns

Tuple containing a numpy array representing the best solution found, and the numerical value of the function at that point.

Return type

Tuple[numpy.ndarray, Union[int, float]]

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

make_transitions(inplace=True, **kwargs)[source]#

Perform a local optimization process to the observations returned after calling make_transitions on the wrapped Function.

Parameters

inplace (bool) –

setup(swarm)[source]#

Prepare the component during the setup phase of the Swarm.

property shape: Tuple[int, ...]#

Return the shape of the wrapped environment.

Return type

Tuple[int, Ellipsis]

Models#

Evolutionary strategy (ESModel)#

Swarm#

FunctionMapper#