In[]:=
​​FunctionDejong[x_]:=Module{d=Length@x},
d
∑
i=1
(Abs[x[[i]]])^(i+1);​​FunctionRastrigin[x_]:=Module{D=Length@x},10*D+
D
∑
i=1
(x[[i]]^2-10Cos[2πx[[i]]]);​​​​FunctionAckley[x_]:=Module{d=Length@x},-20*
-0.2
1
d
d
∑
i=1
x[[i]]^2

-
1
d
d
∑
i=1
Cos[2πx[[i]]]

+20+
1

;​​​​FunctionGRIEWANK[x_]:=Module{l=Length@x},
l
∑
i=1
2
x
[[i]]
4000
-
l
∏
i=1
Cosx[[i]]
i
+1;​​​​FunctionDejon[x_]:=Module{l=Length@x},
l-1
∑
i=1
(100(x[[i+1]]-
2
x
[[i]])^2+(x[[i]]-1)^2);
In[]:=
​​​​​​DE[Lim_,Dim_,NP_,Gen_,F_,CR_,Vota_,CostFunction_]:=​​Module[{FirstGen,Population,i,Act,pop,Sol1,Sol2,Sol3,Solect1,Solect2,Solect3,repair,k,mutantPos,TryVec,pos,rand,m,stop,l},​​globalBestFitness={};​​BestFind={};​​NewGen={};​​​​​​​​​​For[j=1,j≤NP,j++,​​FirstGen={};​​For[i=1,i<=Dim,i++,​​If[Length[FirstGen]0,​​AppendTo[FirstGen,i],​​FirstGen=Insert[FirstGen,i,RandomInteger[{1,Length[FirstGen]+1}]]​​]​​];​​AppendTo[NewGen,FirstGen]​​];​​​​​​​​For[g=1,g≤Gen,g++,​​Population=NewGen;​​NewGen={};​​​​For[i=1,i≤Length[Population],i++,​​​​Act=Population[[i]];​​pop=Drop[Population,{i}];​​​​Sol1=RandomInteger[{1,Length[pop]}];Solect1=pop[[Sol1]];pop=Drop[pop,{Sol1}];​​​​Sol2=RandomInteger[{1,Length[pop]}];Solect2=pop[[Sol2]];pop=Drop[pop,{Sol2}];​​​​Sol3=RandomInteger[{1,Length[pop]}];Solect3=pop[[Sol3]];​​(*DEfirstvariant*)​​mutantPos=Round[Solect3+F*(Solect1-Solect2)];​​​​For[j=1,j≤Dim,j++,​​If[mutantPos[[j]]≤0,​​mutantPos[[j]]=1];​​​​​​If[mutantPos[[j]]>Lim[[1]],mutantPos[[j]]=Lim[[2]];​​If[mutantPos[[j]]<Lim[[1]],mutantPos[[j]]=Lim[[2]];​​];​​];​​​​];​​​​TryVec={};​​repair=1-Length[Union[mutantPos]]/Dim;​​​​​​If[repair≤Vota,​​For[k=1,k≤Dim,k++,​​If[RandomInteger[{0,1}]<CR,​​AppendTo[TryVec,mutantPos[[k]]],​​AppendTo[TryVec,Act[[k]]]​​]​​];​​​​For[l=1,l≤Dim,l++,​​pos=Flatten[Position[TryVec,l]];​​If[Length[pos]>1,​​For[m=1,m<Length[pos],m++,​​stop==0;​​While[stop0,​​If[MemberQ[TryVec,rand=RandomInteger[{1,Dim}]]False,TryVec[[pos[[m]]]]=rand;stop=1];​​];​​]​​]​​],​​​​TryVec=Act;​​];​​(*Print["FunctionRastrigin; Act",Act];*)​​If[CostFunction[Act]<CostFunction[TryVec],​​​​​​AppendTo[NewGen,Act],​​AppendTo[NewGen,TryVec]​​];​​​​];​​​​Best=Min[Table[Round[CostFunction[NewGen[[i]]]],{i,NP}]];​​globalBestFitness=Append[globalBestFitness,Best];​​​​];​​​​Best=Table[Prepend[NewGen[[i]],Round[CostFunction[NewGen[[i]]]]],{i,NP}];​​BestFind=Append[BestFind,Best];​​(*agentfromthepopulationthathasthebestfitness*)​​Return[{globalBestFitness,BestFind}];​​​​​​];​​​​
In[]:=
​​DE[{-1,1},5,10,5,0.5,0.8,0.1,FunctionDejong]​​DE[{-5.12,5.12},10,10,0.3,0.5,0.9,0.1,FunctionRastrigin]​​DE[{-32.768,32.768},10,10,10,0.5,0.9,0.1,FunctionAckley]​​DE[{-600,600},10,5,10,0.5,0.9,0.1,FunctionGRIEWANK]​​DE[{-2.12,2.12},5,10,0.3,0.5,0.9,0.1,FunctionDejon]​​​​
Out[]=
{{423,423,423,423,423},{{{3473,1,3,4,5,2},{4481,1,5,2,3,4},{15803,1,4,3,2,5},{423,3,5,4,2,1},{3275,2,4,3,5,1},{16675,3,1,2,4,5},{4973,1,2,5,3,4},{4753,2,3,5,1,4},{4211,5,2,3,1,4},{2387,1,2,5,4,3}}}}