- class zfit.minimize.NLoptMLSLV1(tol=None, population=None, randomized=None, local_minimizer=None, verbosity=None, maxiter=None, strategy=None, criterion=None, name='NLopt MLSL')#
Global minimizer using local optimization by randomly selecting points.
“Multi-Level Single-Linkage” (MLSL) is an algorithm for global optimization by a sequence of local optimizations from random starting points, proposed by:
A. H. G. Rinnooy Kan and G. T. Timmer, “Stochastic global optimization methods,” Mathematical Programming, vol. 39, p. 27-78 (1987). (Actually 2 papers — part I: clustering methods, p. 27, then part II: multilevel methods, p. 57.)
We also include a modification of MLSL use a Sobol’ low-discrepancy sequence (LDS), also used in so-called quasi Monte Carlo methods that can be invoked by setting randomized to False (as it is now *quasi*randomized) instead of pseudorandom numbers, which was argued to improve the convergence rate by:
Sergei Kucherenko and Yury Sytsko, “Application of deterministic low-discrepancy sequences in global optimization,” Computational Optimization and Applications, vol. 30, p. 297-318 (2005).
In either case, MLSL is a “multistart” algorithm: it works by doing a sequence of local optimizations (using some other local optimization algorithm) from random or low-discrepancy starting points. MLSL is distinguished, however by a “clustering” heuristic that helps it to avoid repeated searches of the same local optima, and has some theoretical guarantees of finding all local optima in a finite number of local minimizations.
The local-search portion of MLSL can use any of the other algorithms in NLopt. The local search uses the derivative/nonderivative algorithm set by the local_minimizer argument.
More information on the algorithm can be found here.
This implenemtation uses internally the NLopt library. It is a free/open-source library for nonlinear optimization, providing a common interface for a number of different free optimization routines available online as well as original implementations of various other algorithms.
population (int | None) – The population size for the evolutionary algorithm.By default, each iteration of MLSL samples 4 random new trial points.
randomized (bool | None) – If True, uses the randomized version ‘GD_MLSL_LDS’ instead of ‘GD_MLSL’
local_minimizer (int | Mapping[str, object] | None) – Configuration for the local minimizer. Defaults to L-BFGS.
verbosity (int | None) –
Verbosity of the minimizer. Has to be between 0 and 10. The verbosity has the meaning:
a value of 0 means quiet and no output
A value above 5 starts printing out considerably more and is used more for debugging purposes.
Setting the verbosity to 10 will print out every evaluation of the loss function and gradient.
value, ‘gradient`` or
ZfitStrategythat takes no input arguments in the init. Determines the behavior of the minimizer in certain situations, most notably when encountering NaNs. It can also implement a callback function.
loss.errordef * tol, the algorithm stopps and it is assumed that the minimum has been found.
name (str) – Human-readable name of the minimizer.
- create_criterion(loss=None, params=None)#
Create a criterion instance for the given loss and parameters.
loss (ZfitLoss | None) – Loss that is used for the criterion. Can be None if called inside
- Return type
ConvergenceCriterion to check if the function converged.
- create_evaluator(loss=None, params=None, numpy_converter=None, strategy=None)#
Make a loss evaluator using the strategy and more from the minimizer.
Convenience factory for the loss evaluator. This wraps the loss to return a numpy array, to catch NaNs, stop on maxiter and evaluate the gradient and hessian without the need to specify the order every time.
loss (ZfitLoss | None) – Loss to be wrapped. Can be None if called inside
strategy (ZfitStrategy | None) – Instance of a Strategy that will be used during the evaluation.
The evaluator that wraps the Loss ant Strategy with the current parameters.
- Return type
- minimize(loss, params=None, init=None)#
Fully minimize the
losswith respect to
params, optionally using information from
The minimizer changes the parameter values in order to minimize the loss function until the convergence criterion value is less than the tolerance. This is a stateless function that can take a
FitResultin order to initialize the minimization.
loss (ZfitLoss | Callable) – Loss to be minimized until convergence is reached. Usually a
can be set to any arbitrary function like
def loss(x): return - x ** 2 loss.errordef = 0.5 # as an example minimizer.minimize(loss, [2, 5])
params (ztyping.ParamsTypeOpt | None) –
None, the parameters will be taken from the
lossis a callable,
paramscan also be (instead of
an array of initial values
for more control, a
dictwith the keys:
value(required): array-like initial values.
name: list of unique names of the parameters.
lower: array-like lower limits of the parameters,
upper: array-like upper limits of the parameters,
step_size: array-like initial step size of the parameters (approximately the expected uncertainty)
FitResultvia params. Repeated calls can therefore (in the current implement) cause a memory increase. The recommended way is to re-use parameters (just taken from the
init (ZfitResult | None) –
In general, the assumption is that the loss provided is similar enough to the one provided in
What is assumed to be close:
the parameters at the minimum of loss will be close to the parameter values at the minimum of init.
Covariance matrix, or in general the shape, of init to the loss at its minimum.
What is explicitly _not_ assumed to be the same:
losswill have the same/similar value at x.
parameters that are used in the minimization may differ in order or which are fixed.
- Return type
The fit result containing all information about the minimization.
Using the ability to restart a minimization with a previous result allows to use a more global search algorithm with a high tolerance and an additional local minimization to polish the found minimum.
result_approx = minimizer_global.minimize(loss, params) result = minimizer_local.minimize(result_approx)
For a simple usage with a callable only, the parameters can be given as an array of initial values.
def func(x): return np.log(np.sum(x ** 2)) func.errordef = 0.5 params = [1.1, 3.5, 8.35] # initial values result = minimizer.minimize(func, param)