:py:mod:`fragile.optimize` ========================== .. py:module:: fragile.optimize .. autoapi-nested-parse:: Module for optimizing functions using a :class:`Swarm`. Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 benchmarks/index.rst env/index.rst evotorch/index.rst policy/index.rst utils/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: fragile.optimize.FunctionMapper fragile.optimize.Function fragile.optimize.MinimizerWrapper .. py:class:: FunctionMapper(minimize = True, start_same_pos = False, **kwargs) Bases: :py:obj:`Swarm` It is a swarm adapted to minimize mathematical functions. .. py:method:: from_function(function, bounds, **kwargs) :classmethod: Initialize a :class:`FunctionMapper` using a python callable and a :class:`Bounds` instance. :param function: Callable representing an arbitrary function to be optimized. :param bounds: Represents the domain of the function to be optimized. :param \*\*kwargs: Passed to :class:`FunctionMapper` __init__. :returns: Instance of :class:`FunctionMapper` that optimizes the target function. .. py:method:: __repr__() Return repr(self). .. py:method:: reset(root_walker = None, state = None) Reset the :class:`fragile.Walkers`, the :class:`Function` environment, the :class:`Model` and clear the internal data to start a new search process. :param root_walker: 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 :class:`StateTree` if any. :param state: StateData dictionary that define the initial state of the Swarm. .. py:class:: Function(function, bounds, custom_domain_check = None, actions_as_perturbations = True, start_same_pos = False, x0 = None) Bases: :py:obj:`fragile.core.api_classes.EnvironmentAPI` Environment that represents an arbitrary mathematical function bounded in a given interval. .. py:attribute:: default_inputs .. py:method:: n_dims() :property: Return the number of dimensions of the function to be optimized. .. py:method:: shape() :property: Return the shape of the environment. .. py:method:: action_space() :property: Action space with the same characteristics as self.bounds. .. py:method:: from_bounds_params(function, shape = None, high = numpy.inf, low = numpy.NINF, custom_domain_check = None) :classmethod: Initialize a function defining its shape and bounds without using a :class:`Bounds`. :param function: 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. :param shape: 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) :param high: 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. :param low: 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. :param custom_domain_check: 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: :class:`Function` with its :class:`Bounds` created from the provided arguments. .. py:method:: __repr__() Return repr(self). .. py:method:: step(actions, observs, **kwargs) 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}`` .. py:method:: reset(inplace = True, root_walker = None, states = None, **kwargs) Reset the :class:`Function` to the start of a new episode and returns an :class:`StatesEnv` instance describing its internal state. .. py:method:: calculate_oobs(points, rewards) Determine if a given batch of vectors lie inside the function domain. :param points: Array of batched vectors that will be checked to lie inside the :class:`Function` bounds. :param rewards: 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. .. py:method:: sample_action(batch_size) Return a matrix of points sampled uniformly from the :class:`Function` domain. :param batch_size: Number of points that will be sampled. :returns: Array containing ``batch_size`` points that lie inside the :class:`Function` domain, stacked across the first dimension. .. py:class:: MinimizerWrapper(function, **kwargs) Bases: :py:obj:`fragile.core.env.Function` Wrapper that applies a local minimization process to the observations returned by a :class:`Function`. .. py:method:: shape() :property: Return the shape of the wrapped environment. .. py:method:: function() :property: Return the function of the wrapped environment. .. py:method:: bounds() :property: Return the bounds of the wrapped environment. .. py:method:: custom_domain_check() :property: Return the custom_domain_check of the wrapped environment. .. py:method:: __getattr__(item) .. py:method:: __repr__() Return repr(self). .. py:method:: setup(swarm) Prepare the component during the setup phase of the :class:`Swarm`. .. py:method:: make_transitions(inplace = True, **kwargs) Perform a local optimization process to the observations returned after calling ``make_transitions`` on the wrapped :class:`Function`.