optimize._differentialevolution

differential_evolution: The differential evolution global optimization algorithm Added by Andrew Nelson 2014

Module Contents

Classes

DifferentialEvolutionSolver(self,func,bounds,args=tuple,strategy=”best1bin”,maxiter=1000,popsize=15,tol=0.01,mutation=tuple,recombination=0.7,seed=None,maxfun=None,callback=None,disp=False,polish=True,init=”latinhypercube”,atol=0) This class implements the differential evolution solver

Functions

differential_evolution(func,bounds,args=tuple,strategy=”best1bin”,maxiter=1000,popsize=15,tol=0.01,mutation=tuple,recombination=0.7,seed=None,callback=None,disp=False,polish=True,init=”latinhypercube”,atol=0) Finds the global minimum of a multivariate function.
differential_evolution(func, bounds, args=tuple, strategy="best1bin", maxiter=1000, popsize=15, tol=0.01, mutation=tuple, recombination=0.7, seed=None, callback=None, disp=False, polish=True, init="latinhypercube", atol=0)

Finds the global minimum of a multivariate function. Differential Evolution is stochastic in nature (does not use gradient methods) to find the minimium, and can search large areas of candidate space, but often requires larger numbers of function evaluations than conventional gradient based techniques.

The algorithm is due to Storn and Price [1].

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.
bounds : sequence
Bounds for variables. (min, max) pairs for each element in x, defining the lower and upper bounds for the optimizing argument of func. It is required to have len(bounds) == len(x). len(bounds) is used to determine the number of parameters in x.
args : tuple, optional
Any additional fixed parameters needed to completely specify the objective function.
strategy : str, optional

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

  • ‘best1bin’
  • ‘best1exp’
  • ‘rand1exp’
  • ‘randtobest1exp’
  • ‘best2exp’
  • ‘rand2exp’
  • ‘randtobest1bin’
  • ‘best2bin’
  • ‘rand2bin’
  • ‘rand1bin’

The default is ‘best1bin’.

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 * len(x)
popsize : int, optional
A multiplier for setting the total population size. The population has popsize * len(x) individuals.
tol : float, optional
Relative 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.
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.
seed : int or np.random.RandomState, optional
If seed is not specified the np.RandomState singleton is used. If seed is an int, a new np.random.RandomState instance is used, seeded with seed. If seed is already a np.random.RandomState instance, then that np.random.RandomState instance is used. Specify seed for repeatable minimizations.
disp : bool, optional
Display status messages
callback : callable, callback(xk, convergence=val), optional
A function to follow the progress of the minimization. xk is the current value of x0. val represents the fractional value of the population convergence. When val is greater than one the function halts. If callback returns True, then the minimization is halted (any polishing is still carried out).
polish : bool, 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.
init : string, optional

Specify how the population initialization is performed. Should be one of:

  • ‘latinhypercube’
  • ‘random’

The default is ‘latinhypercube’. Latin Hypercube sampling tries to maximize coverage of the available parameter space. ‘random’ initializes the population randomly - this has the drawback that clustering can occur, preventing the whole of parameter space being covered.

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.
res : OptimizeResult
The optimization result represented as a OptimizeResult object. Important attributes are: x the solution array, success a Boolean flag indicating if the optimizer exited successfully and message which describes the cause of the termination. See OptimizeResult for a description of other attributes. If polish was employed, and a lower minimum was obtained by the polishing, then OptimizeResult also contains the jac attribute.

Differential evolution is a stochastic population based method that is useful for global optimization problems. At each pass through the population the algorithm mutates each candidate solution by mixing with other candidate solutions to create a trial candidate. There are several strategies [2] for creating trial candidates, which suit some problems more than others. The ‘best1bin’ strategy is a good starting point for many systems. In this strategy two members of the population are randomly chosen. Their difference is used to mutate the best member (the best in best1bin), , so far:

A trial vector is then constructed. Starting with a randomly chosen ‘i’th parameter the trial is sequentially filled (in modulo) with parameters from b’ or the original candidate. The choice of whether to use b’ or the original candidate is made with a binomial distribution (the ‘bin’ in ‘best1bin’) - a random number in [0, 1) is generated. If this number is less than the recombination constant then the parameter is loaded from b’, otherwise it is loaded from the original candidate. The final parameter is always loaded from b’. Once the trial candidate is built its fitness is assessed. If the trial is better than the original candidate then it takes its place. If it is also better than the best overall candidate it also replaces that. To improve your chances of finding a global minimum use higher popsize values, with higher mutation and (dithering), but lower recombination values. This has the effect of widening the search radius, but slowing convergence.

New in version 0.15.0.

Let us consider the problem of minimizing the Rosenbrock function. This function is implemented in rosen in scipy.optimize.

>>> from scipy.optimize import rosen, differential_evolution
>>> bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)]
>>> result = differential_evolution(rosen, bounds)
>>> result.x, result.fun
(array([1., 1., 1., 1., 1.]), 1.9216496320061384e-19)

Next find the minimum of the Ackley function (http://en.wikipedia.org/wiki/Test_functions_for_optimization).

>>> from scipy.optimize import differential_evolution
>>> import numpy as np
>>> def ackley(x):
...     arg1 = -0.2 * np.sqrt(0.5 * (x[0] ** 2 + x[1] ** 2))
...     arg2 = 0.5 * (np.cos(2. * np.pi * x[0]) + np.cos(2. * np.pi * x[1]))
...     return -20. * np.exp(arg1) - np.exp(arg2) + 20. + np.e
>>> bounds = [(-5, 5), (-5, 5)]
>>> result = differential_evolution(ackley, bounds)
>>> result.x, result.fun
(array([ 0.,  0.]), 4.4408920985006262e-16)
[1]Storn, R and Price, K, Differential Evolution - a Simple and Efficient Heuristic for Global Optimization over Continuous Spaces, Journal of Global Optimization, 1997, 11, 341 - 359.
[2]http://www1.icsi.berkeley.edu/~storn/code.html
[3]http://en.wikipedia.org/wiki/Differential_evolution
class DifferentialEvolutionSolver(func, bounds, args=tuple, strategy="best1bin", maxiter=1000, popsize=15, tol=0.01, mutation=tuple, recombination=0.7, seed=None, maxfun=None, callback=None, disp=False, polish=True, init="latinhypercube", atol=0)

This class implements the differential evolution solver

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.
bounds : sequence
Bounds for variables. (min, max) pairs for each element in x, defining the lower and upper bounds for the optimizing argument of func. It is required to have len(bounds) == len(x). len(bounds) is used to determine the number of parameters in x.
args : tuple, optional
Any additional fixed parameters needed to completely specify the objective function.
strategy : str, optional

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

  • ‘best1bin’
  • ‘best1exp’
  • ‘rand1exp’
  • ‘randtobest1exp’
  • ‘best2exp’
  • ‘rand2exp’
  • ‘randtobest1bin’
  • ‘best2bin’
  • ‘rand2bin’
  • ‘rand1bin’

The default is ‘best1bin’

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 * len(x)
popsize : int, optional
A multiplier for setting the total population size. The population has popsize * len(x) individuals.
tol : float, optional
Relative 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.
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.
seed : int or np.random.RandomState, optional
If seed is not specified the np.random.RandomState singleton is used. If seed is an int, a new np.random.RandomState instance is used, seeded with seed. If seed is already a np.random.RandomState instance, then that np.random.RandomState instance is used. Specify seed for repeatable minimizations.
disp : bool, optional
Display status messages
callback : callable, callback(xk, convergence=val), optional
A function to follow the progress of the minimization. xk is the current value of x0. val represents the fractional value of the population convergence. When val is greater than one the function halts. If callback returns True, then the minimization is halted (any polishing is still carried out).
polish : bool, optional
If True, then scipy.optimize.minimize with the L-BFGS-B method is used to polish the best population member at the end. This requires a few more function evaluations.
maxfun : int, optional
Set the maximum number of function evaluations. However, it probably makes more sense to set maxiter instead.
init : string, optional

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

  • ‘latinhypercube’
  • ‘random’
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.
__init__(func, bounds, args=tuple, strategy="best1bin", maxiter=1000, popsize=15, tol=0.01, mutation=tuple, recombination=0.7, seed=None, maxfun=None, callback=None, disp=False, polish=True, init="latinhypercube", atol=0)
init_population_lhs()

Initializes the population with Latin Hypercube Sampling. Latin Hypercube Sampling ensures that each parameter is uniformly sampled over its range.

init_population_random()

Initialises the population at random. This type of initialization can possess clustering, Latin Hypercube sampling is generally better.

x()

The best solution from the solver

x : ndarray
The best solution from the solver.
convergence()

The standard deviation of the population energies divided by their mean.

solve()

Runs the DifferentialEvolutionSolver.

res : OptimizeResult
The optimization result represented as a OptimizeResult object. Important attributes are: x the solution array, success a Boolean flag indicating if the optimizer exited successfully and message which describes the cause of the termination. See OptimizeResult for a description of other attributes. If polish was employed, and a lower minimum was obtained by the polishing, then OptimizeResult also contains the jac attribute.
_calculate_population_energies()

Calculate the energies of all the population members at the same time. Puts the best member in first place. Useful if the population has just been initialised.

__iter__()
__next__()

Evolve the population by a single generation

x : ndarray
The best solution from the solver.
fun : float
Value of objective function obtained from the best solution.
next()

Evolve the population by a single generation

x : ndarray
The best solution from the solver.
fun : float
Value of objective function obtained from the best solution.
_scale_parameters(trial)

scale from a number between 0 and 1 to parameters.

_unscale_parameters(parameters)

scale from parameters to a number between 0 and 1.

_ensure_constraint(trial)

make sure the parameters lie between the limits

_mutate(candidate)

create a trial vector based on a mutation strategy

_best1(samples)

best1bin, best1exp

_rand1(samples)

rand1bin, rand1exp

_randtobest1(candidate, samples)

randtobest1bin, randtobest1exp

_best2(samples)

best2bin, best2exp

_rand2(samples)

rand2bin, rand2exp

_select_samples(candidate, number_samples)

obtain random integers from range(self.num_population_members), without replacement. You can’t have the original candidate either.