{ } Raw JSON

bundles / scipy latest / scipy / optimize / _differentialevolution / DifferentialEvolutionSolver

class

scipy.optimize._differentialevolution:DifferentialEvolutionSolver

source: /scipy/optimize/_differentialevolution.py :538

Signature

class   DifferentialEvolutionSolver ( func bounds args = () strategy = best1bin maxiter = 1000 popsize = 15 tol = 0.01 mutation = (0.5, 1) recombination = 0.7 rng = None maxfun = inf callback = None disp = False polish = True init = latinhypercube atol = 0 updating = immediate workers = 1 constraints = () x0 = None * integrality = None vectorized = False )

Members

Summary

This class implements the differential evolution solver

Parameters

func : callable

The objective function to be minimized. Must be in the form f(x, *args), where x is the argument in the form of a 1-D array and args is a tuple of any additional fixed parameters needed to completely specify the function. The number of parameters, N, is equal to len(x).

bounds : sequence or `Bounds`

Bounds for variables. There are two ways to specify the bounds:

  • Instance of Bounds class.

  • (min, max) pairs for each element in x, defining the finite lower and upper bounds for the optimizing argument of func.

The total number of bounds is used to determine the number of parameters, N. If there are parameters whose bounds are equal the total number of free parameters is N - N_equal.

args : tuple, optional

Any additional fixed parameters needed to completely specify the objective function.

strategy : {str, callable}, optional

The differential evolution strategy to use. Should be one of:

  • 'best1bin'

  • 'best1exp'

  • 'rand1bin'

  • 'rand1exp'

  • 'rand2bin'

  • 'rand2exp'

  • 'randtobest1bin'

  • 'randtobest1exp'

  • 'currenttobest1bin'

  • 'currenttobest1exp'

  • 'best2exp'

  • 'best2bin'

The default is 'best1bin'. Strategies that may be implemented are outlined in 'Notes'.

Alternatively the differential evolution strategy can be customized by providing a callable that constructs a trial vector. The callable must have the form strategy(candidate: int, population: np.ndarray, rng=None), where candidate is an integer specifying which entry of the population is being evolved, population is an array of shape (S, N) containing all the population members (where S is the total population size), and rng is the random number generator being used within the solver. candidate will be in the range [0, S). strategy must return a trial vector with shape (N,). The fitness of this trial vector is compared against the fitness of population[candidate].

maxiter : int, optional

The maximum number of generations over which the entire population is evolved. The maximum number of function evaluations (with no polishing) is: (maxiter + 1) * popsize * (N - N_equal)

popsize : int, optional

A multiplier for setting the total population size. The population has popsize * (N - N_equal) individuals. This keyword is overridden if an initial population is supplied via the init keyword. When using init='sobol' the population size is calculated as the next power of 2 after popsize * (N - N_equal).

tol : float, optional

Relative tolerance for convergence, the solving stops when np.std(population_energies) <= atol + tol * np.abs(np.mean(population_energies)), where and atol and tol are the absolute and relative tolerance respectively.

mutation : float or tuple(float, float), optional

The mutation constant. In the literature this is also known as differential weight, being denoted by F. If specified as a float it should be in the range [0, 2]. If specified as a tuple (min, max) dithering is employed. Dithering randomly changes the mutation constant on a generation by generation basis. The mutation constant for that generation is taken from U[min, max). Dithering can help speed convergence significantly. Increasing the mutation constant increases the search radius, but will slow down convergence.

recombination : float, optional

The recombination constant, should be in the range [0, 1]. In the literature this is also known as the crossover probability, being denoted by CR. Increasing this value allows a larger number of mutants to progress into the next generation, but at the risk of population stability.

rng : {None, int, `numpy.random.Generator`}, optional

..versionchanged:: 1.15.0

As part of the SPEC-007 transition from use of numpy.random.RandomState to numpy.random.Generator this keyword was changed from seed to rng. For an interim period both keywords will continue to work (only specify one of them). After the interim period using the seed keyword will emit warnings. The behavior of the seed and rng keywords is outlined below.

If rng is passed by keyword, types other than numpy.random.Generator are passed to numpy.random.default_rng to instantiate a Generator. If rng is already a Generator instance, then the provided instance is used.

If this argument is passed by position or seed is passed by keyword, the behavior is:

  • If seed is None (or np.random), the numpy.random.RandomState singleton is used.

  • If seed is an int, a new RandomState instance is used, seeded with seed.

  • If seed is already a Generator or RandomState instance then that instance is used.

Specify seed/rng for repeatable minimizations.

disp : bool, optional

Prints the evaluated func at every iteration.

callback : callable, optional

A callable called after each iteration. Has the signature:

callback(intermediate_result: OptimizeResult)

where intermediate_result is a keyword parameter containing an OptimizeResult with attributes x and fun, the best solution found so far and the objective function. Note that the name of the parameter must be intermediate_result for the callback to be passed an OptimizeResult.

The callback also supports a signature like:

callback(x, convergence: float=val)

val represents the fractional value of the population convergence.

When val is greater than 1.0, the function halts.

Introspection is used to determine which of the signatures is invoked.

Global minimization will halt if the callback raises StopIteration or returns True; any polishing is still carried out.

polish : {bool, callable}, optional

If True (default), then scipy.optimize.minimize with the L-BFGS-B method is used to polish the best population member at the end, which can improve the minimization slightly. If a constrained problem is being studied then the trust-constr method is used instead. For large problems with many constraints, polishing can take a long time due to the Jacobian computations. Alternatively supply a callable that has a minimize-like signature, polish_func(func, x0, **kwds) and returns an OptimizeResult. This allows the user to have fine control over how the polishing occurs. bounds and constraints will be present in kwds. Extra keywords could be supplied to polish_func using functools.partial. It is the user's responsibility to ensure that the polishing function obeys bounds, any constraints (including integrality constraints), and that appropriate attributes are set in the OptimizeResult, such as fun, `x, nfev, jac.

maxfun : int, optional

Set the maximum number of function evaluations. However, it probably makes more sense to set maxiter instead.

init : str or array-like, optional

Specify which type of population initialization is performed. Should be one of:

  • 'latinhypercube'

  • 'sobol'

  • 'halton'

  • 'random'

  • array specifying the initial population. The array should have shape (S, N), where S is the total population size and N is the number of parameters. init is clipped to bounds before use.

The default is 'latinhypercube'. Latin Hypercube sampling tries to maximize coverage of the available parameter space.

'sobol' and 'halton' are superior alternatives and maximize even more the parameter space. 'sobol' will enforce an initial population size which is calculated as the next power of 2 after popsize * (N - N_equal). 'halton' has no requirements but is a bit less efficient. See scipy.stats.qmc for more details.

'random' initializes the population randomly - this has the drawback that clustering can occur, preventing the whole of parameter space being covered. Use of an array to specify a population could be used, for example, to create a tight bunch of initial guesses in an location where the solution is known to exist, thereby reducing time for convergence.

atol : float, optional

Absolute tolerance for convergence, the solving stops when np.std(pop) <= atol + tol * np.abs(np.mean(population_energies)), where and atol and tol are the absolute and relative tolerance respectively.

updating : {'immediate', 'deferred'}, optional

If 'immediate', the best solution vector is continuously updated within a single generation [4]. This can lead to faster convergence as trial vectors can take advantage of continuous improvements in the best solution. With 'deferred', the best solution vector is updated once per generation. Only 'deferred' is compatible with parallelization or vectorization, and the workers and vectorized keywords can over-ride this option.

workers : int or map-like callable, optional

If workers is an int the population is subdivided into workers sections and evaluated in parallel (uses multiprocessing.Pool <multiprocessing>). Supply -1 to use all cores available to the Process. Alternatively supply a map-like callable, such as multiprocessing.Pool.map for evaluating the population in parallel. This evaluation is carried out as workers(func, iterable). This option will override the updating keyword to updating='deferred' if workers != 1. Requires that func be pickleable.

constraints : {NonLinearConstraint, LinearConstraint, Bounds}

Constraints on the solver, over and above those applied by the bounds kwd. Uses the approach by Lampinen.

x0 : None or array-like, optional

Provides an initial guess to the minimization. Once the population has been initialized this vector replaces the first (best) member. This replacement is done even if init is given an initial population. x0.shape == (N,).

integrality : 1-D array, optional

For each decision variable, a boolean value indicating whether the decision variable is constrained to integer values. The array is broadcast to (N,). If any decision variables are constrained to be integral, they will not be changed during polishing. Only integer values lying between the lower and upper bounds are used. If there are no integer values lying between the bounds then a ValueError is raised.

vectorized : bool, optional

If vectorized is True, func is sent an x array with x.shape == (N, S), and is expected to return an array of shape (S,), where S is the number of solution vectors to be calculated. If constraints are applied, each of the functions used to construct a Constraint object should accept an x array with x.shape == (N, S), and return an array of shape (M, S), where M is the number of constraint components. This option is an alternative to the parallelization offered by workers, and may help in optimization speed. This keyword is ignored if workers != 1. This option will override the updating keyword to updating='deferred'.

Aliases

  • scipy.optimize._differentialevolution.DifferentialEvolutionSolver