Sei sulla pagina 1di 11


This contribution provides functions for finding an optimum parameter set using the
Evolutionary Algorithm of Differential Evolution. Simply speaking: If you have
some complicated function of which you are unable to compute a derivative, and
you want to find the parameter set minimizing the output of the function, using this
package is one possible way to go.

The core of the optimization is the Differential Evolution algorithm. For an

introduction to the algorithm, see the basics section on the Differential Evolution
homepage of Rainer Storn. You will also find a demo applet and code for different
programming languages there. For in-depth documentation and publications, check
out the Literature Section. Even several books about Differential Evolution are

This package provides much more than the code available on the referenced
homepage. Here is a list of some features:

Optimization can run in parallel on multiple cores/computers.

Extensive and configurable progress information during optimization.

Intermediate results are stored for later review of optimization progress.

Progress information can be sent by E-mail.

Optimization toolbox is not needed.

Quick start with demo functions.

Intermediate results are displayed after the optimization.

Different end conditions can be chosen (maximum time, value to reach etc.).

Each parameter value can be constrained to an interval.

Each parameter value can be quantized (for example for parameters of

integer nature).

Code can easily be extended to use the evolutionary algorithm of your


I have developed this package for an own project. A single evaluation of my

objective function took between 30 and 60 seconds and the parameter space was
galactically large. If your objective function needs only milliseconds to evaluate and
your optimization is expected to finish in seconds or minutes, you can still use this
package. However, much of its power (parallel processing, progress notifications)
will not be of much use.

In addition to this documentation, I have summarized E-mail conversations in a list

of Frequently Asked Questions.
Quick start
To get into the usage of the package quickly, check out the demo functions
demo1.m, demo2.m and demo3.m. Modify those files to start your first optimization.
Essentially you only have to define which parameters to optimize and provide a
handle to your objective function. You can learn about everything else later. Your
objective function can be called in different ways:

With a scalar or column vector as the only input argument.

With a structure containing the current parameters as only input argument.

With one or more user-defined fixed arguments first, then a parameter

vector/structure as last argument.

If your objective function has an argument list that does not comply with one of
these possibilities, you have to write a small wrapper function which brings the
arguments into the correct order and calls your objective function. The definition of
the parameters to optimize is expected as a cell array. The first column of the cell
array has to contain the parameter names. In the second column, you have to
provide the parameter ranges. For a scalar parameter, the range is expected as a
two-element row vector. For vector-valued parameters, you have to give the ranges
of the elements as a two-column matrix with ranges in rows. The third column
contains the parameter quantization steps as a scalar or a column vector (set to
zero for no quantization). The fourth column (optional) can contain the initial values
of the parameters. If your objective function shall be called with a column
parameter vector instead of a structure as input, define only one single parameter
with an empty string as parameter name. See the help text ("help
differentialevolution") and the demos for more details and examples.
Parallel processing on multiple cores
This package allows to work in parallel on multiple cores in order to increase the
speed of the optimization. One process acts as the master and all other processes
act as slaves. The only requirements for parallel processing are:

All involved processes have to have read- and write-access to a common

directory, for example on a network share.

All involved processes have to have access to identical code versions of the
objective function.

For parallel processing, the main function differentialevolution.m has to be started

in one Matlab process. In one or more other processes, the function
differentialevolutionslave.m has to be started. The master process will save files
including the parameter sets to evaluate into the common directory. The slave
processes load the parameter files, evaluate the objective function and save the
obtainted results into other files. After each iteration, the master process collects
the evaluated results and feeds the slave processes with data files again. If there
are no results found, the master process will evaluate the parameter sets himself.
This way, the slaves can never cause the master to get stuck.

The Multicore package on Matlab Central actually emanated from this function and
works quite similar.
The usage in detail
The details of how to use the package are contained as help lines in the file
differentialevolution.m. I paste the same lines here:
%DIFFERENTIALEVOLUTION Start Differential Evolution optimization.
% [bestmem, bestval, bestFctParams, nrOfIterations] =
% DIFFERENTIALEVOLUTION(...) starts a Differential Evolution (DE)
% to minimize the cost returned by a given function. For a quick start,
% out and modify the functions DEMO1, DEMO2 and DEMO3.
% Output arguments:
% =================
% bestmem:
% Best population member.
% bestval:
% Lowest evaluated cost value.
% bestFctParams:
% Structure like input objFctParams containing the best parameter set.
% nrOfIterations:
% Number of iterations done.
% resultFileName:
% Name of file containing optimization results or empty string (if
% DEParams.saveHistory is set to zero, see below).
% Input arguments:
% ================
% DEParams (required):
% Struct with parameters as returned by function GETDEFAULTPARAMS.
% paramDefCell (required):
% Cell specifying the parameters to optimize (see details below).
% objFctHandle (required):
% Handle to the objective function, which is called as follows:
% value = objFctHandle(objFctSettings, objFctParams) or
% value = objFctHandle(objFctSettings{:}, objFctParams).
% Here, objFctParams is a structure containing the current parameters (see
% input argument objFctParams below).
% The second case is used if objFctSettings is a cell array, thus allowing
% an arbitrary number of additional constant input arguments (see input
% argument objFctSettings below).
% If the objective function handle is empty, the distance to a randomly
% optimal parameter vector is used as cost value (for testing purposes).
% objFctSettings (optional):
% Additional fixed settings to be passed (a cell array will be expanded
% {:}) to the objective function. If no additional settings are needed, set
% objFctSettings to an empty cell: {}. If a cell array is needed as an
% additional input, wrap it in another cell (e.g. objFctSettings =
{ myCell };
% objFctParams (optional):
% Struct with initial parameters in its fields. Each field needs to contain
% numeric scalar or column vector. Example:
% objFctParams.parameter1 = 1;
% objFctParams.parameter2 = 2;
% emailParams (optional):
% Struct with fields serveraddress, fromaddress, toaddress, and, if needed,
% username and password. The parameters are used for sending E-mail
% notifications about the optimization progress.
% optimInfo (optional):
% Info about the current optimization task. The fields 'title' and
% are displayed and included in saved files if existing. No influence on
% optimization.
% resultFileName (optional):
% The result file resultFileName saved during a former optimization by
% differentialevolution.m is loaded and the previous optimization is
% continued. The population of parameter vectors saved in the file is
% restored. The optimization parameters may be changed, but not every change
% is allowed. CAUTION! Changing optimization parameters when continuing an
% optimization may lead to wrong or misleading results! If any value of the
% former input arguments is empty, the value saved in the result file is
% Struct DEParams:
% ================
% The struct DEParams must contain the following fields (use function
% GETDEFAULTPARAMS to generate a struct with default parameters):
% VTR "Value To Reach". The optimization is stopped if a cost
% value <= VTR in a minimization problem or a cost value >=
% in a maximization problem is found. Set to empty matrix or
% NaN for no VTR.
% NP Number of population members (e.g. 10 * dimension).
% F DE-stepsize F from interval [0, 2]. A good initial guess
% is the interval [0.5, 1], e.g. 0.8.
% CR Crossover probability constant from interval [0, 1]. If
% the parameters are correlated, high values of CR work
% The reverse is true for no correlation.
% strategy 1 --> DE/best/1/exp (def.) 6 --> DE/best/1/bin
% 2 --> DE/rand/1/exp 7 --> DE/rand/1/bin
% 3 --> DE/rand-to-best/1/exp 8 --> DE/rand-to-best/1/bin
% 4 --> DE/best/2/exp 9 --> DE/best/2/bin
% 5 --> DE/rand/2/exp else DE/rand/2/bin
% Experiments suggest that /bin likes to have a slightly
% CR than /exp
% maxiter Maximum number of iterations.
% maxtime Maximum time (in seconds) before finishing optimization.
% Set to empty or Inf for no time limit.
% maxclock Time (as returned by function clock.m) when to
% finish optimization. Set to empty for no end time.
% minvalstddev Population is reinitialized if the standard deviation of
% the cost values in the population is lower than
% minparamstddev Population is reinitialized if the maximum parameter
% standard deviation (normalized to the parameter range) is
% lower than minparamstddev.
% nofevaliter Population is reinitialized if there was no function
% evaluation during the last nofevaliter iterations.
% nochangeiter Population is reinitialized if there was no change in
% the population during the last nochangeiter iterations.
% infoIterations Info is displayed and current state is saved every
% infoIterations iterations.
% infoPeriod Progress information is displayed every infoPeriod
% seconds.
% sendMailPeriod Progress information is sent via E-mail every
% sendMailPeriod seconds (usually sendMailPeriod >>
% infoPeriod).
% useInitParams If one, the given parameters in struct objFctParams
% OR those in the fourth column of paramDefCell are used as
% of the initial population members. If two, additionally the
% first twenty percent of the population members are computed
% as the given initial parameter vector plus small random
% noise.
% saveHistory Save intermediate results for reference or for continuing
% an interrupted optimization.
% displayResults Draw graphs for visualization of the optimization
% result.
% feedSlaveProc Use slave process for parallel computation.
% maxMasterEvals Maximum number of function evaluations to be done by the
% master process. Warning: Use this option with caution! If
% maxMasterEvals is set to a number less than the number of
% population members and one of the slave processes is
% interrupted, the optimization will get stuck!
% slaveFileDir Base directory for saving slave files.
% minimizeValue If true, the evaluation value is minimized, otherwise
% maximized.
% validChkHandle Handle to a function which accepts the same arguments as
% the objective function (see input parameter objFctHandle
% above) and decides if a given parameter set is valid (e.g.
% subject to a constraint) or not. The function may only
% 1 (parameter set is valid/constraint is fulfilled) or 0
% (parameter set is invalid/constraint is not fulfilled) if
% not. Set validChkHandle to an empty string if no constraint
% is needed.
% playSound Play a short applause sound whenever a new best member
% was found.
% If DEParams is empty or fields are missing, default parameters are used
% (see function GETDEFAULTPARAMS) but warnings are displayed.
% Cell array paramDefCell:
% ========================
% The cell array paramDefCell has to contain the names of the parameters to
% optimize (first column), its ranges (second column), their quantizations
% (third column) and optionally the initial values (fourth column). Each
% parameter may be a real-valued scalar or column vector. See the examples
% below for details.
% Example 1 (only scalar parameters):
% paramDefCell = {
% 'useSmoothing', [0 1], 1, 0
% 'nrOfCoefficients', [5 20], 1, 10
% 'threshold', [0.01 1], 0.001, 0.5 }
% The first cell in each row contains the name of the parameter, the second
% two-element row vector specifying the allowed range, the third the
% quantization and the fourth the initial values (the fourth column is
% opotional).
% The objective function objFctHandle will be called with a struct like
% follows as last input argument:
% objFctParams =
% useSmoothing: 1
% nrOfCoefficients: 17
% threshold: 0.08
% Provide a non-empty value either in objFctParams or in the fourth column
% paramDefCell as initial value. If both are present, a warning message is
% issued and the value in paramDefCell is used. If objFctParams is empty and
% no initial parameters are given in paramDefCell, the centers of the
% parameter ranges are used as initial parameters.
% Using parameter quantization allows for the use of binary variables like
% 'useSmoothing' above as well for parameters that are of integer nature,
% a number of coefficients. If the quantization of a parameter is set to
% the parameter is not quantized. Using a quantization grid for continuous
% parameters can save computational effort. If DEParams.saveHistory is true,
% all evaluated parameter vectors are saved with the corresponding cost
% and the same parameter value will never be evaluated twice. With
% quantization, it is more likely that a generated parameter vector was
% already evaluated and saved before.
% Example 2 (vector parameter):
% paramDefCell = {
% 'weight_vector', [0 1; 0 2], [0.01; 0.02], [0.5; 0.5] };
% Here, the parameter 'weight_vector' is defined as a two-element column
% vector. The ranges are set to [0, 1] for the first element and [0, 2]
% for the second. The quantizations are 0.01 and 0.02 and the initial
% values are both 0.5.
% The provided structure objFctParams may contain further fixed parameters
% and/or the current parameter values. The fields with the names of the
% parameters given in paramDefCell are overwritten by the values of the
% current parameters.
% Example 3 (vector parameter):
% paramDefCell = { '', [0 1; 0 2], [0.01; 0.02], [0.5; 0.5] };
% In this special case (one single parameter with empty name), the
% objective function is called as
% value = objFctHandle(objFctSettings, paramVec) or
% value = objFctHandle(objFctSettings{:}, paramVec)
% with the current parameters in column vector paramVec.
% Miscellaneous:
% ==============
% If parameter DEParams.saveHistory is set to one, the current optimization
% state including all tested members etc. is saved in the file
% AAA_result_BBB_NN.mat, where AAA is the name of the objective function,
% is the name of the current host and NN is a number between 1 and 99 (to
% avoid overwriting old results). The saved file can be used to continue a
% former optimization run (see special calling modes below).
% A 'time over'-file is saved at the start of the optimization. The
% optimization is stopped if this file is deleted. Using this mechanism to
% stop the simulation avoids interrupting Matlab during saving a file, which
% can make a file unaccessible for the rest of the session and lead to
% repeated warning messages. The name of the file to delete is
% AAA_timeover_BBB.mat, where AAA is the name of the objective function and
% BBB is the hostname. Result- and 'time over'-files are saved in directory
% 'data' if existing, otherwise in the current directory.
% The optimization can be performed in parallel by more than one
% processor/computer. Function DIFFERENTIALEVOLUTION has to be started in
% Matlab session, function DIFFERENTIALEVOLUTIONSLAVE in one or more other
% Matlab sessions. Function DIFFERENTIALEVOLUTION acts as master and saves
% information about which function to evaluate and which parameters to use
% into files in a commonly accessible directory. The Distributed Computing
% toolbox is not used. If input parameter slaveFileDir is empty, the
% differentialevolution is used (or created) below the temporary directory
% returned by function TEMPDIR2 (something like C:\Documents and
% Settings\\Local Settings\ Temp\@\MATLAB).
% Function DIFFERENTIALEVOLUTION was developed for objective functions that
% need relatively long for one function evaluation (several seconds or
% When used with objective functions that evaluate very fast, memory
% could occur. If DEParams.saveHistory is true, every evaluated parameter
% vector is kept in memory. Further, the overhead for checking if a
% vector was already evaluated might be larger than a function evaluation
% itself.
% Special calling modes:
% ======================
% DIFFERENTIALEVOLUTION (without input arguments) or
% A demo optimization of Rosenbrock's saddle is run using default parameters
% or the parameters in struct DEParams.
% This function is based on the differential evolution (DE) algorithm of
% Rainer Storn ( The core
% evolutionary algorithm was taken from function devec3.m.
% differentialevolution.html
% File Exchange
% Donate via PayPal
% Markus Buehren
% Last modified 30.08.2014
Please check the help lines in file differentialevolution.m to be sure to read the
latest version.
Functions contained in this package
In the following, the most important functions contained in this package are listed.
Every file contains its own help comments which you can access by typing "help
functionname" on the Matlab command line.

The main function to call after preparing the input arguments.

When starting to work with this package, you probably do not want to handle with
every existing parameter for function differentialevolution.m. You can get a default
parameter set by calling getdefaultparams.m.

When working in parallel on multiple cores/computers, this function has to be
started in every Matlab process that shall act as slave.

The core Differential Evolution algorithm resides in this functions. If you like to use
your own favorite evolutionary algorithm, you can put the code into this function.

demo1.m, demo2.m, demo3.m

Demo functions you can modify for a quick start.

foxholes.m, rosenbrocksaddle.m
These two functions are used for the demos. They implement two functions that are
often cited in the context of optimization algorithms ("Shekel's Foxholes" and
"Rosenbrock's Saddle"). Parameter quantization

The original Differential Evolution algorithm only knows unbounded, continuous

parameters. In order to include parameters of integer nature into the optimization, I
have extended the algorithm in this way. Internally, all parameters are continuous.
Only before passing them to the objective function, the parameter values are

Hard parameter bounds

In my own project, and I guess in most other optimization problems as well, I always
had an idea about the possible range for each optimal parameter. A hard parameter
range has to be given here for every parameter. Using -Inf or Inf as lower or upper
bound is not possible. A side effect of the hard bounds is that parameter sets
including boundary values can be evaluated with higher probability under certain

Breaking using Ctrl-C

When breaking the optimization using Ctrl-C, it might happen that you catch Matlab
just when writing to a file. When you start the optimization again and the same file
needs to be accessed again, the file is locked until you quit and restart Matlab (at
least on Windows).

To avoid the need for using Ctrl-C, a 'time-over'-file is saved in the current directory.
After each function evaluation, the master process checks if the file is still existing.
If it was deleted, the optimization is finished cheerfully. So instead of pressing Ctrl-C,
delete that file.

Shrinking slave support in parallel processing

When processing in parallel, the master process feeds the slaves at the start of
each iteration and collects the results at the end. Further, as all parameter sets that
have been evaluated before are saved, not all parameter vectors in the current
population might need to be evaluated. If the population has already converged
very well, it can happen that there are less new parameter vectors in the population
than processes are working on the optimization problem. In this case, some or all
slaves are sleeping, as there is no more work to do.

Parameter vectors in rows and columns

I always save parameter vectors as column vectors. However, the core Differential
Evolution algorithm taken from the Differential Evolution homepage expects
parameter vectors to be stored in rows. Unfortunately, the code now contains a
mixture of representations as rows and columns. You will never get involved with
this issue until you edit the code. Parameter vectors are passed to your objective
functions as column vectors. If your objective function needs to get parameter
vectors passed as row vectors, you will have to write a small wrapper function that
transposed the vectors and calls the objective function in the right way.
Differential Evolution homepage.
Wikipedia about Evolutionary Algorithms.
Latest Version of this package on Matlab Central.
The Multicore package on Matlab Central.
Blat homepage (E-mails from the command line on Windows).
Dr.-Ing. Markus Buehren
Stuttgart, Germany
Last modified 30.08.2014
Latest version on Matlab Central.