fragile.optimize#

Module for optimizing functions using a Swarm.

Submodules#

Package Contents#

Classes#

FunctionMapper

It is a swarm adapted to minimize mathematical functions.

Function

Environment that represents an arbitrary mathematical function bounded in a given interval.

MinimizerWrapper

Wrapper that applies a local minimization process to the observations returned by a Function.

class fragile.optimize.FunctionMapper(minimize=True, start_same_pos=False, **kwargs)[source]#

Bases: Swarm

It is a swarm adapted to minimize mathematical functions.

Parameters
  • minimize (bool) –

  • start_same_pos (bool) –

classmethod from_function(function, bounds, **kwargs)[source]#

Initialize a FunctionMapper using a python callable and a Bounds instance.

Parameters
  • function (Callable) – Callable representing an arbitrary function to be optimized.

  • bounds (judo.data_structures.bounds.Bounds) – Represents the domain of the function to be optimized.

  • **kwargs – Passed to FunctionMapper __init__.

Returns

Instance of FunctionMapper that optimizes the target function.

Return type

FunctionMapper

__repr__()[source]#

Return repr(self).

reset(root_walker=None, state=None)[source]#

Reset the fragile.Walkers, the Function environment, the Model and clear the internal data to start a new search process.

Parameters
  • root_walker (Optional[OneWalker]) – Walker representing the initial state of the search. The walkers will be reset to this walker, and it will be added to the root of the StateTree if any.

  • state (Optional[fragile.core.state.SwarmState]) – StateData dictionary that define the initial state of the Swarm.

class fragile.optimize.Function(function, bounds, custom_domain_check=None, actions_as_perturbations=True, start_same_pos=False, x0=None)[source]#

Bases: fragile.core.api_classes.EnvironmentAPI

Environment that represents an arbitrary mathematical function bounded in a given interval.

Parameters
  • function (Callable[[judo.typing.Tensor], judo.typing.Tensor]) –

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

  • custom_domain_check (Callable[[judo.typing.Tensor, judo.typing.Tensor, int], judo.typing.Tensor]) –

  • actions_as_perturbations (bool) –

  • start_same_pos (bool) –

  • x0 (fragile.core.typing.Tensor) –

default_inputs#
property n_dims#

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

Return type

int

property shape#

Return the shape of the environment.

Return type

Tuple[int, Ellipsis]

property action_space#

Action space with the same characteristics as self.bounds.

Return type

gym.spaces.box.Box

classmethod from_bounds_params(function, shape=None, high=numpy.inf, low=numpy.NINF, 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 (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, judo.typing.Tensor]) – 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, judo.typing.Tensor]) – 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 (Callable[[judo.typing.Tensor], judo.typing.Tensor]) – 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

Function

__repr__()[source]#

Return repr(self).

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

fragile.core.typing.StateData

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
  • inplace (bool) –

  • root_walker (Optional[fragile.core.typing.StateData]) –

  • states (Optional[fragile.core.typing.StateData]) –

Return type

Union[None, fragile.core.typing.StateData]

calculate_oobs(points, rewards)[source]#

Determine if a given batch of vectors lie inside the function domain.

Parameters
  • points (judo.typing.Tensor) – Array of batched vectors that will be checked to lie inside the Function bounds.

  • rewards (judo.typing.Tensor) – 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

judo.typing.Tensor

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

judo.typing.Tensor

class fragile.optimize.MinimizerWrapper(function, **kwargs)[source]#

Bases: fragile.core.env.Function

Wrapper that applies a local minimization process to the observations returned by a Function.

Parameters

function (fragile.core.env.Function) –

property shape#

Return the shape of the wrapped environment.

Return type

Tuple[int, Ellipsis]

property function#

Return the function of the wrapped environment.

Return type

Callable

property bounds#

Return the bounds of the wrapped environment.

Return type

judo.Bounds

property custom_domain_check#

Return the custom_domain_check of the wrapped environment.

Return type

Callable

__getattr__(item)[source]#
__repr__()[source]#

Return repr(self).

setup(swarm)[source]#

Prepare the component during the setup phase of the Swarm.

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) –