%**************************************************************************************************
% Author: Wei Du
% Last Edited: Jan 25, 2016
% Email: duwei0203@gmail.com
% Reference: Differential Evolution With Event-Triggered Impulsive Control
% IEEE Transactions on Cybernetics, doi: 10.1109/TCYB.2015.2512942
%**************************************************************************************************
% The MATLAB source codes of JADE was provided by Dr. J. Zhang.
% References: J. Zhang and A. C. Sanderson, "JADE: adaptive differential evolution with optional external archive," IEEE Trans. Evolut. Comput., vol. 13, no. 5, pp. 945-958, 2009.
clc;
clear all;
tic;
format long;
format compact;
warning off
'ETI-JADE'
n=30;
popsize=100;
%migrate = 0.9;
lu = [-100 * ones(1, n); 100 * ones(1, n)];
fhd=str2func('cec14_func');
problemSet = 1:30;
for problemIndex = [17:30,1:16]
problem = problemSet(problemIndex);
filename = strcat(strcat('f',num2str(problem)),'_5f2_09.txt');
fp = fopen(filename,'a+');
func_num = problem;
thediv = 5.0e-02;
% Record the best results
outcome = [];
beishu = 10;
MaxGen = n * 10000 * beishu / popsize;
curdiv = 0.0;
time = 1;
% The total number of runs
totalTime = 30;
while time <= totalTime
curbestfit = realmax();%%%%%%%%%%%%%%%%%%our parameters
prebestfit = realmax();
curdiv = 1.0;
prediv = 1.0;
recdiv = 1.0;
flag = 0;
count = 0;
%times = 10;
sign = 0;
everbestfitall = realmax();
everbestfitinrun = realmax();%%%%%%%%%%%%%%%%%%our parameters
rand('state', sum(100 * clock));
% Initialize the main population
popold = repmat(lu(1, :), popsize, 1) + rand(popsize, n) .* (repmat(lu(2, :) - lu(1, :), popsize, 1));
valParents = feval(fhd, popold', func_num) - problemIndex * 100;
valParents = valParents';
gbest=min(valParents);
c = 1/10;
p = 0.05;
CRm = 0.5;
goodCR = [];
Fm = 0.5;
Afactor = 1;
srpop=0;
pbest_STG=zeros(popsize,1);
LN=1;
UN=popsize;
num=LN;
archive.NP = Afactor * popsize; % the maximum size of the archive
archive.pop = zeros(0, n); % the solutions stored in te archive
archive.funvalues = zeros(0, 1); % the function value of the archived solutions
%% the values and indices of the best solutions
[valBest, indBest] = sort(valParents, 'ascend');
FES = popsize;%%%%%%%%0;
xxx = 0;
curdiv = 0.0;
for x =1 : n
midpoint(x) = median(popold(:,x));
end
distobest = 1 : popsize;
for x = 1: popsize
distobest (x)= 0;
for y = 1 : n
distobest(x) = distobest(x) + abs((popold(x,y) - midpoint(y))/(lu(2, y) - lu(1, y)));
end
distobest (x) = distobest (x) / n;
curdiv = curdiv + distobest (x);
end
curdiv = curdiv / popsize;
everbestfitall = min(valParents);
fprintf(fp,'%d %e %e %e %e\r\n', FES, curdiv, mean(valParents), min(valParents),everbestfitall);% %g %g %d %d %,everbestfitall,everbestfitinrun,mycount,ourOpt);
%ourOpt = 0;
%curbestfit = valParents(1);
%prebestfit = realmax();
%curbestchrom = popold(1, :);
%preind = 0;
while FES < n * 10000 * beishu
pop = popold; % the old population becomes the current population
valParents_temp=valParents;
if FES > 1 && ~isempty(goodCR) && sum(goodF) > 0 % If goodF and goodCR are empty, pause the update
CRm = (1 - c) * CRm + c * mean(goodCR);
Fm = (1 - c) * Fm + c * sum(goodF .^ 2) / sum(goodF); % Lehmer mean
end
% Generate CR according to a normal distribution with mean CRm, and std 0.1
% Generate F according to a cauchy distribution with location parameter Fm, and scale parameter 0.1
[F, CR] = randFCR(popsize, CRm, 0.1, Fm, 0.1);
r0 = [1 : popsize];
popAll = [pop; archive.pop];
[r1, r2] = gnR1R2(popsize, size(popAll, 1), r0);
[r3, r4] = gnR1R2(popsize, popsize, r0);%%%%%%%%
% Find the p-best solutions
pNP = max(round(p * popsize), 2); % choose at least two best solutions
randindex = ceil(rand(1, popsize) * pNP); % select from [1, 2, 3, ..., pNP]
randindex = max(1, randindex); % to avoid the problem that rand = 0 and thus ceil(rand) = 0
pbest = pop(indBest(randindex), :); % randomly choose one of the top 100p% solutions
%%%%%%%%%%%%%%%%%%%
prebestfit = curbestfit;
%prebestchrom = curbestchrom;
[curbestfit,ind] = min(valParents);
%curbestchrom = popold(ind, :);
if prebestfit < everbestfitinrun
everbestfitinrun = prebestfit;
%everbestchrominrun = prebestchrom;
end
if prebestfit < everbestfitall
everbestfitall = prebestfit;
end
if sign == 0
if (curbestfit >= everbestfitinrun) || (curbestfit < everbestfitinrun && (everbestfitinrun - curbestfit) / everbestfitinrun < 1e-5)
mycount = mycount +1;
%disp(mycount);
else
mycount = 0;
end
upcount =500;
if abs(everbestfitinrun - everbestfitall) < 1e-10
upcount = upcount * 2;
end
if mycount >= upcount
sign = 1;%表示这一代开始回退
mycount = 0;
if curdiv > thediv
migrate = 1.0;
else
migrate = 0.9;
end
everbestfitinrun = realmax();
archive.pop = zeros(0, n); % the solutions stored in te archive
archive.funvalues = zeros(0, 1); % the function value of the archived solutions
end
else
curdiv = 0.0;
for x =1 : n
midpoint(x) = median(popold(:,x));
end
distobest = 1 : popsize;
for x = 1: popsize
distobest (x)= 0;
for y = 1 : n
distobest(x) = distobest(x) + abs((popold(x,y) - midpoint(y))/(lu(2, y) - lu(1, y)));
end
distobest (x) = distobest (x) / n;
curdiv = curdiv + distobest (x);
end
curdiv = curdiv / popsize;
%disp(curdiv);
end
%%%%%%%%%%%%%%%%%%%
% == == == == == == == == == == == == == == == Mutation == == == == == == == == == == == == ==
if sign == 0
vi = pop + F(:, ones(1, n)) .* (pbest - pop + pop(r1, :) - popAll(r2, :));
else
vi = pop + F(:, ones(1, n)) .* (pop(r3, :) - pop(r4, :));
end
vi = boundConstraint(vi, pop, lu);
% == == == == = Crossover == == == == =
mask = rand(popsize, n) > CR(:, ones(1, n)); % mask is used to indicate which elements of ui comes from the parent
rows = (1 : popsize)'; cols = floor(rand(popsize, 1) * n)+1; % choose one position where the element of ui doesn't come from the parent
jrand = sub2ind([popsize n], rows, cols); mask(jrand) = false;
ui = vi; ui(mask) = pop(mask);
if sign ==0 || (sign == 1 && curdiv > thediv)
valOffspring = feval(fhd, ui',