File Exchange

## Differential Evolution

version 1.16.0.1 (64.1 KB) by Markus Buehren

### Markus Buehren (view profile)

Optimization using the evolutionary algorithm of Differential Evolution.

Updated 13 Nov 2019

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. However, this package provides much more than the code available on the Differential Evolution homepage:
http://www.icsi.berkeley.edu/~storn/code.html
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.
* 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 choice.
I have spent many hours to develop this package. If you would like to let me know that you appreciate my work, you can do so by leaving a donation:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=KAECWD2H7EJFN
Keywords: Optimization, evolutionary algorithm, genetic algorithm, differential evolution, parallel processing, distributed computing, multiple core.

### Cite As

Markus Buehren (2020). Differential Evolution (https://www.mathworks.com/matlabcentral/fileexchange/18593-differential-evolution), MATLAB Central File Exchange. Retrieved .

Markus Buehren

### Markus Buehren (view profile)

There is a new version without the (unnecessary) sound support that doesn't cause any trouble with the function wavread anymore.

Giussepi Mamani

### Giussepi Mamani (view profile)

Hi Markus!
It's a powerfull package, thanks for this contribution, I have a doubt I ran demo2.m and appears this error. I don't know what happened,

Error in differentialevolution>displaybestmember (line 1900)

Error in differentialevolution (line 1163)
displaybestmember(paramDefCell, parameterDimVector, bestval, bestmem, ...

Error in demo2 (line 85)
DEParams, paramDefCell2, objFctHandle, objFctSettings, objFctParams, emailParams,
optimInfo, ...

Regards!

Sanjeev Aggarwal

Stephen Schwaner

### Stephen Schwaner (view profile)

Hi Markus,

Again thanks for the package! I think I found a bug. I believe that the function "generatefilesforslaveprocess" needs to output information about how the variable allmem is updated within itself, or refrain from updating allmem. allmem is updated within the function, but the updated array is not an output. This causes problems when there are two population members whose quantized values are equal. I think it only causes problems when maxMasterEvals = 0. Let's say we have a population of 10 members, with members 2 and 8 being equal. Within "generatefiles...", the loop proceeds in reverse order from member 10 to member 1. When it reaches member 8, member 8 is added to allmem. Later in the loop, member 2 is flagged as a repeat set of parameters that does not need to be evaluated, and no slave file is generated for member 2. However, allmem in the main differentialevolution function is not updated to reflect the addition of member 2. Later on, computeevaluationvalue is used to evaluate each parameter set in the population. It proceeds from member 1 to member 10. When it gets to member 2, it looks for a result file generated by a slave process, but never finds one because no parameters slave file was generated in the first place.

I guess outputting an updated allmem from "generate...." would not solve the issue because then computeevaluationvalue would not look for any results from the current parameter set. As a temporary fix, I think I'll remove the lines that update allmem inside "generate...". It'll obviously cause inefficiencies when I have multiple equal members within the same population set, but at least for my particular problem, I don't think that will be a big deal.

Thanks again for you great work.

Stephen

P.S. In case you're interested to know, I added a bit of code that generates a new member every time a boundary value is reached (with the strategy rand/1/bin). This solved the problem I was having in which the algorithm kept getting stuck at the boundaries. It could be useful to add as an option to your code if others are experiencing the boundary value problem.

GeoMath

Isa Hafidz

Mingjing Xu

### Mingjing Xu (view profile)

I like this toolbox, it's very powerful and useful.

Stephen Schwaner

### Stephen Schwaner (view profile)

Hi Markus,

On your website, you said that in certain situations, the algorithm will continue to guess boundary values. This seems to happen a lot in my problem. Do you have tips for avoiding this?

Also thanks for the great software package.

- Stephen

Stephen Schwaner

### Stephen Schwaner (view profile)

Zachary Jospeh Heins

Markus Buehren

### Markus Buehren (view profile)

Hi Alan,
of course that depends on your optimization problem. If you have parameters that are integer numbers by nature (e.g. the number of neurons in a neural network), set the quantization to one. If you know that a parameter difference less than epsilon (e.g. epsilon = 0.001) does not influence the result, set the quantization to epsilon. If you want to limit the overall number of possible parameter vectors in order to reduce the runtime of the optimization, set some quantization value larger than 0 for each parameter. If nothing of the above applies, you can set the quantization value(s) to zero meaning no quantization.

Alan

### Alan (view profile)

Would you please tell me how to set the value of parameter quantizations in paramDefCell?

Markus Buehren

### Markus Buehren (view profile)

Hi Marcus,
I am glad that you like my code and that it is helping you! Normally the number of slave evaluations should be nearly much as the number of function evaluations. How many function evaluations are executed for each iteration? Maybe some slave processes are idling because there is not enough work for all of the slaves? Do you use a function to check parameter vectors for validity (validChkHandle)? Maybe most of the generated parameter vectors are invalid and there is nothing to evaluate. How long does a function evaluation take? There is some overhead per function evaluation (loading/saving files) that could have something to do with that.

Markus Buehren

### Markus Buehren (view profile)

Hi Marcus,
I am glad that you like my code and that it is helping you! Normally the number of slave evaluations should be nearly much as the number of function evaluations. How many function evaluations are executed for each iteration? Maybe some slave processes are idling because there is not enough work for all of the slaves? Do you use a function to check parameter vectors for validity (validChkHandle)? Maybe most of the generated parameter vectors are invalid and there is nothing to evaluate. How long does a function evaluation take? There is some overhead per function evaluation (loading/saving files) that could have something to do with that.

Marcus Müller

### Marcus Müller (view profile)

Hi Markus,

thank you for sharing your code. It is currently helping me a lot in doing some computationally very expensive optimizations.

One question: The „number of slave evaluations“ is constantly much lower than the „number of function evaluations“, even though I am using 7, 27 or even 47 Matlab instances, running the differentialevolution_slave.m code. Of course the machines have adequate number of cores (8/28/48 cores).
Shouldn‘t the number of slave evaluations be a large number, as 6 out of 7 Matlab instances are generating those?

Ben Petschel

### Ben Petschel (view profile)

In R2017b the program crashes due to undefined function or variable 'wavread' unless you set playSound to false. Also running the program with no input parameters has a crash at line 331 due to undefined function or variable 'setrandomstate' - assuming this is supposed to be 'setrandomseed'.

Nitin Muralidhara Athreya

Hi
Great Work!

Ryan Rosario

Krishanu Nath

### Krishanu Nath (view profile)

Aitor López Hernández

### Aitor López Hernández (view profile)

How are you able to use this function including constraints?

I tried to create a new script with all the constraints and call it through validChkHandle parameter in getDefaultParams.m. But it seems to be wrong somehow. It says that apparently there are too many input parameters. What on Earth could be wrong?

I just did

DEParamsDefault.validChkHandle = @(param)NonLinearConstraints({param,k,N,D,perror,divisions})

And cannot see what is wrong with it.

Thank you all in advance for the attention, and BR!!

2MASS

saw

### saw (view profile)

Hi, thanks for the code.From the demo1, it only pass one number. I'm trying to pass 3 additional vectors to the cost function but it doesnt work. may i know how to do that? Would appreciate much if you could help. Thanks.
Below is how the code.

optimInfo.title = 'Fung model using DE';

objFctHandle = @cost_Fung_v3_brute;
% Define parameter names, ranges and quantization:

paramDefCell = {
'c0', [0.1 10], 0.001
'c1', [0.1 10], 0.001
'c2', [0.1 10], 0.001
};

% Set initial parameter values in struct objFctParams
objFctParams.c0 = 0.1;
objFctParams.c1 = 0.1;
objFctParams.c2 = 0.1;
% Set single additional function parameter
Mycell = {lamda, c_stress11, c_stress22};
objFctSettings = {Mycell};
% objFctSettings.lamda = lamda;
% objFctSettings.c_stress11 = c_stress11;
% objFctSettings.c_stress22 = c_stress22;

DEParams = getdefaultparams;

% Set number of population members (often 10*D is suggested)
DEParams.NP = 30;

% Do not use slave processes here. If you want to, set feedSlaveProc to 1 and
% run startmulticoreslave.m in at least one additional Matlab session.
DEParams.feedSlaveProc = 0;

% Set times
DEParams.maxiter = 20;
DEParams.maxtime = 30; % in seconds
DEParams.maxclock = [];

% Set display options
DEParams.infoIterations = 1;
DEParams.infoPeriod = 10; % in seconds

% Do not send E-mails
emailParams = [];

% Set random state in order to always use the same population members here
setrandomseed(1);

% Start differential evolution
[bestmem, bestval, bestFctParams, nrOfIterations, resultFileName] = differentialevolution(...
DEParams, paramDefCell, objFctHandle, objFctSettings, objFctParams, emailParams, optimInfo);

The cost function:

function P_error_top = cost_Fung_v3_brute(objFctSettings, objFctParams)
% Initialize values

c0=objFctParams.c0(1); %p(1) ;
c1=objFctParams.c1(1); % p(2);
c2=objFctParams.c2(1); %p(3);
c3=c2;
c4=0; %p(5);
c5=0; %p(6);
c6=0; %p(7);

Mycell = objFctSettings{:}
[lamda11, c_stress11, c_stress22] = Mycell{:};
lamda22 = sqrt(1./lamda11);
lamda33 = lamda22;

E11=0.5*(lamda11.^2-1);
E22=0.5*(lamda22.^2-1);
E33=0.5*(lamda33.^2-1);

Q = c1*E22.^2 + c2*E11.^2 + c3*E33.^2;
H = -c0*(1+2*E22).*(c1*E22).*exp(Q);

stress_top_comp = c0*(1+2*E11).*(c2*E11).*exp(Q) + H;

P_error1= stress_top_comp - c_stress11;

P_error_top =sum(sqrt((P_error1).^2))/length(lamda11);

end

Aitor López Hernández

### Aitor López Hernández (view profile)

Hello everyone,

I'd be very pleased if you are so kind to tell me which parts you need to comment out to make the script work. I am also having problems with the wavread declaration, but after I try to comment its corresponding lines, the whole script stops working.

Many thanks in advance and may you have a nice day.

Roshal Fernandes

Samuel Käser

### Samuel Käser (view profile)

Thanks for your work. I also changed the not working parts (addbuttons & wavread), after tat it was alright and workinf fine :)

Halil Bilal

qiang zhang

yukun ding

### yukun ding (view profile)

Thanks very much for your work and sharing, it helps me a lot. Although there seems to be some problems with the new version (addbuttons and wavread), but it works well after I comment them out.

yukun ding

yukun ding

yalan Zhao

Namit Sharma

### Namit Sharma (view profile)

Thank you for this submission.It is working well for my problem.

However, I have noticed that for constrained optimization problems the evaluations of constraint function take place in only one MATLAB session(i.e. sequentially rather than in parallel) while the objective function evaluations are carried out in parallel(i.e. on multiple MATLAB sessions).
So, is there any way that I can also parallelize my constraint function evaluations? (since my constraint function is as computationally expensive as the objective function)

Jakob Sievers

### Jakob Sievers (view profile)

I cannot overstate how pleased I have been with this code. That being said, I have had to make a few relatively simple alterations for use with my specific problem and I feel some of them might be good to add for a future release:
1) I have added an input parameter DEParams.infoOutput which, if set to 0, suppresses ALL visual output. This is handy for serial application after an initial testing phase. I myself am running the code for a very large number of cases and I prefer that the screen is left for more general output like progress reports and estimated time left, etc.
2) I have added a parameter DEParams.saveHistoryFilename which allows the user to save the history to a specific path. Again this is useful for serial application in which one wants to do statistics on the performance of the algorithm.
3) I have added a parameter DEParams.maxiterStablebest which defines the number of allowed iterations with a stable "best solution". That is: if for X iterations the solution has been stable, terminate optimization.
4) Finally, and perhaps most importantly, I have had to add support for vectorized functions. That is: in the current code evaluation of the NP members is done in a for-loop. For functions which can be vectorized (i.e.: calling the function with all NP members at once rather than sequentially) a significant reduction in computation time can be attained. In my case the computation time dropped to ~60% relative to before when using a vectorized function evaluation instead. Support should be added such that users can alternate between sequential and vectorized application depending on their type of problem.

Liqun

### Liqun (view profile)

Trying to figure out how to use this package these days, and had some emails back and forth with the author. But still have two questions.

The fist is: in the demo files, it has the following codes:

% set times
DEParams.maxiter = 20;
DEParams.maxtime = 30; % in seconds
DEParams.maxclock = [];

What is the proper way to set the time parameters (maxiter and maxtime) for DE? or what is the the theory behind this?

The second questions is: how to use this package on a cluster (Linux based)? I can access to a cluster, but have to submit the job to the cluster from within the Matlab on my local laptop or desktop, using job = batch(......)

How to open as many as matlab sessions on the cluster, using job = batch(......)?

Hope can hear some feedback!

Thanks!

Pijian Cheng

gon

### gon (view profile)

Hi Markus, May I enquire if this algorithm works if multi objective optimization? So far, it appears that it can only maximize a function with a single output.

Paulo Branco

### Paulo Branco (view profile)

Hi Markus, thanks for the response. I am sorry to intrude, but I do have another question. There is a test problem in <http://www-optima.amp.i.kyoto-u.ac.jp/member/student/hedar/Hedar_files/TestGO_files/Page506.htm>. I used the constraint function you pointed me to. I defined a matrix Ain(dimension 9x13) and the vector bin(dimension 9x1) to write the function return value: valid all(Ain*x<=bin). However, the optimum parameters stray by a huge amount from the expected results. The problem has hard boundaries, as well as linear inequality constraints (9 of them to be precise). I hope you can briefly walk me through the correct formulation of the problem in your files, as I suspect my constraints are at the core of the problem.

Paulo Branco

### Paulo Branco (view profile)

Hi Markus, does your algorithm allow the inclusion of linear constraints? I am trying to optimize an instance of density evolution, and the parameters must add up to 1. Also, a linear combination of the parameters must yield a given rate. Do you have anything similar to the tools provided by Matlab's global optimization toolbox or do I just have to work around with the parameters, taking the linear constraints into account as I go along?

Joe

### Joe (view profile)

very helpful tool for those who cannot afford toolboxes.

Behnam seyedi

### Behnam seyedi (view profile)

Hi dear Markus Buehren
i just wana ask about the simple Differential Evolution algorithm (single objective ) , how can i use your code for this propose ?

chen

Jakob Sievers

### Jakob Sievers (view profile)

I am very happy with this submission. I have probably misunderstood something though, but isn't it possible to stop optimization before maxiter if the function has been optimized within a user-defined threshold? Currently I am only tuning maxiter to achieve proper results but I suspect I am loosing a lot of processing time and accuracy when a fixed maxiter is insufficient (either too many or too few).
Cheers

Steven Crocker

### Steven Crocker (view profile)

This is a wonderful method and implementation that I have used frequently, and always successfully. If I had one "feature request" it would be the option to restart a prior optimization by reading the optimResult file and starting with the current population in that file. I have, on occasion, had a problem terminate prematurely for reasons having nothing to do with differentialevoluion.m. It would be nice to continue such problems where I had left off.

vijay pandit

### vijay pandit (view profile)

DEAR SIR,
I am using this techniques for my optimizaion technique.would u please help me i am facing lots of problem..

AfanSveta

Joe Hays

### Joe Hays (view profile)

Very helpful tool. Loved having the parallel implementation too!

Erdal Bizkevelci

Joe Ajay

### Joe Ajay (view profile)

Hi Markus, does it solve discrete optimization problems? if not is there any differential evolution solver which solves discrete problems

deng

### deng (view profile)

Thank you very much!

mklcst mklcst

### mklcst mklcst (view profile)

Hi, I have to call a variable in the objectfunction, where I have to call it in order to pass it to the function?

Matthias Thul

john

### john (view profile)

Where can I find Multi objective version of the Differential evolution algorithms?

thanks

Evi Daems

William Chang

Very Good,Thanks

Mahmud Iwan

### Mahmud Iwan (view profile)

This is an excellent code..
However, i am wondering how to use this code for constrained optimization problem (inequality constraint).?? It was given in the demo for such problem, but the code needs the initial values not to violate the constraint. in the real practice, we do not need to know whether the initial value violate it or not.Just give any initial values..right?...pls, enlighten! thanks,,

ardian mohib

### ardian mohib (view profile)

thank u for sharing markus, but can u tell me why when I run the codes in my computer, the codes is always error. and one more question for u, would u like to help me to explain Differential evolution algorithm. i 'm a student that interest to Differential evolution algorithm and would like to use this algorithm for solve optimization inventory supply chain for my research.
thank b4

RMS Danaraj

### RMS Danaraj (view profile)

Very nice algorithm .I have implemented this to solve three non linear optimization problems which I will upload shrtly in the file exchange.Thank you very much.

James Pullen

James Pullen

### James Pullen (view profile)

I'm normally way too lazy to bother rating code and submissions, but this deserves to be an exception to the rule. Very minor quibble: I'd have liked one of the demo files to have incorporated the use of external vectors/matrices. Otherwise a very concise and usable (and, more importantly, utterly useful) piece of code. Thank you Markus.

helo,
based on the above comments im overwhelmed in using the code posted buy markus.ive been working on genetic algorithms n optimization toolbox for my problem on alkylation.Differential Evolution can also be applied to this prob but im not sure of how to handle the code. So could u plz give insights on how to work on it. i ve been using matlab-GA only n am a new user so i request to plz help me in dis regard.Im highly interested in workin on dis.

Olivier Desmaison

### Olivier Desmaison (view profile)

Un package vraiment complet.
Un programme qui marche de suite sans avoir à retoucher quoi que ce soit. Je l'utilise pour du recalage de modèles éléments finis et ca fonctionne du tonnerre.
Bravo

Olivier Desmaison

Markus Buehren

### Markus Buehren (view profile)

Thanks for pointing that out, Scott. I have corrected the link.

Scott

### Scott (view profile)

Markus,

I think the point Andrew Koh was making is that for some reason the ")" is part of the URL in your abstract, but not in Andrew's URL, so your URL does not get to Storn's website, but Andrew's does.

Scott

STTAR WDAA

THANK YOU VERY MUCH

Hom Gharti

Thanks for the very very useful code. I wanted to apply this code for 4-D, 3 space dimension and 1 time dimension. I got the error 'error in bestmem ~= lastbestmem', when I checked, obviously these two vectors have different lengths, then I changed parGridVector = cell2mat(paramDefCell(:,3));
to
parGridVector = cell2mat(paramDefCell(:,4));
and code seems to be working fine. Am I doing right?

many thanks,

James Yong

One of the best optimizers I've used. The speedy support from Markus was invaluable and greatly appreciated. An amazing package. Straightforward to implement, flexible and provides results!

David Brown

This package is unbelievably powerful. In addition Markus is always available to answer tons of questions about how to use it properly. He's even responded to fix small quirks I found in lightning fast time. This thing CHOPS hard problems to bits.

Markus Buehren

As it is written in the description, exactly that code is used as the core algorithm! However, this packages offers much much more than the core algorithm.

ANDREW KOH

I think the better DE code appears at

http://www.icsi.berkeley.edu/~storn/code.html

Marcelo P.

Hi Markues, This is a great submission, thanks for sharing it.

I think it is just missing documentation regarding the method, not the code. For instance, which algo are you using? did u got it from a book or a paper? If so, can you provide the reference?