% clc
% clear all
function BCGSK
global m runnum Pro Max_FEs d
% population size setting
% m = 50;
% Max_FEs = 500;
%runnum: the number of trial runs
% runnum =1;
KF = 0.5; % Knowledge Factor
KR = 0.0; %Knowledge Ratio
K = 10; %Knowledge Rate
p = 0.1;
initresult = zeros(runnum,1);
Max_gen = round(Max_FEs/m);
finalresults = [];
% Max_FEs = 50000;
results = zeros(runnum,Max_gen);
solution = [];
tic;
% several runs
% t0=cputime;
for run = 1 : runnum
run
%%% Initialize Population %%%
rand('seed', sum(100 * clock));
results = [];
% Population = repmat(MinParValue,m,1) + rand(m,d) .* repmat((MaxParValue - MinParValue),m,1);
% Population = randi(2,m,d) - 1;
Population = round(rand(m,d));
TempPop = zeros (m , d); % Temporary population
[Objective_values, Population] = fitness(Population,Pro);
% initresult(run,1) = min(Objective_values);
results = [results; min(Objective_values)];
bestever = 1e200;
FEs = m;
% Sort Population
% [Population,Objective_values] = PopulationSort(Population,Objective_values);
% pbest = Population;
pbestval = Objective_values; %initialize the pbest and the pbest's fitness value
% [gbestval,gbestid] = min(pbestval);
% best = Population(gbestid,:);
% gbest = pbest(gbestid,:); %initialize the gbest and the gbest's fitness value
%=========================================================================%
%%% BBO Main Loop %%%
% FES = m;
gen = 0;
% tic;
% main loop
% while(gen < Max_gen)
while(FEs < Max_FEs)
% Sort Population
[Population,Objective_values] = PopulationSortforBBSO(Population,Objective_values);
D_Gained_Shared_Junior = ceil(d*(1 - FEs / Max_FEs)^K);
% D_Gained_Shared_Senior = d - D_Gained_Shared_Junior;
% Use migration rates to BCGSKciBCGSK how much information to share between solutions
for i = 1 : m
% Gained_Shared_Junior_R1R2R3
if(i==1)% best
r1_Junior = 2;
r2_Junior = 3;
elseif(i == m)% worst
r1_Junior = m - 2;
r2_Junior = m - 1;
else
r1_Junior = i - 1;
r2_Junior = i + 1;
end
while true
r3_Junior = round(m * rand + 0.5);
if (r3_Junior ~= r1_Junior) && (r3_Junior ~= r2_Junior) && (r3_Junior ~= i) , break, end
end
% Gained_Shared_Senior_R1R2R3
r1_Senior = ceil(p*m * rand);
r2_Senior = ceil((1-p) * m) + ceil(p*m * rand);
while true
r3_Senior = ceil(p * m) + ceil((1-2*p)*m * rand);
if (r3_Senior ~= i), break, end
end
for j = 1:d
% KF = randi(2,1,1) - 1;
KF = round(rand);
if (rand < KR)
if rand < D_Gained_Shared_Junior/d
if i < r3_Junior
Input = [Population(r1_Junior,j)
Population(i,j)
Population(r2_Junior,j)
Population(r3_Junior,j)
KF];
else
Input = [Population(r1_Junior,j)
Population(r3_Junior,j)
Population(r2_Junior,j)
Population(i,j)
KF];
end
else
if i < r3_Senior
Input = [Population(r1_Senior,j)
Population(i,j)
Population(r2_Senior,j)
Population(r3_Senior,j)
KF];
else
Input = [Population(r1_Senior,j)
Population(r3_Senior,j)
Population(r2_Senior,j)
Population(i,j)
KF];
end
end
% Input = Input';
TempPop(i,j) = BCGSK_Operator(Input');
else
TempPop(i,j) = Population(i,j);
end
end
end
% Evaluation
[Objective_values_TempPop, TempPop] = fitness(TempPop,Pro);
FEs = FEs + m;
% Comparision
mask = Objective_values_TempPop < Objective_values;
aa = repmat(mask',1,d);
Population = aa .* TempPop + ~aa .* Population;
Objective_values = mask .* Objective_values_TempPop + ~mask .* Objective_values;
[gbestval,gbestid] = min(Objective_values);
% best = Population(gbestid,:);
MinimumCost = gbestval;
bestsolution = Population(gbestid,:);
% Display Iteration Information
gen = gen + 1;
% results(run,gen) = MinimumCost;
results = [results; MinimumCost];
% fprintf('Best fitness: %e gen %d\n', MinimumCost,gen);
% fprintf('Best fitness: %e and avg: %e gen %d\n', MinimumCost, mean(Objective_values), gen);
end
% xx = round(linspace(1,length(results),100));
xx = length(results);
finalresults = [finalresults results];
solution = [solution; bestsolution];
fprintf('Run No.%d Done!\n', run);
% disp(['CPU time: ',num2str(toc)]);
end
disp(['CPU time: ',num2str(toc/runnum)]);
% t1 = num2str((cputime-t0)/runnum)
Maxresult = max(finalresults(end,1:runnum));
Meanresult = mean(finalresults(end,1:runnum));
Minresult = min(finalresults(end,1:runnum));
stdresult = std(finalresults(end,1:runnum));
Allresults = [Minresult; Maxresult; Meanresult; stdresult];
Allresults_BCGSK = Allresults;
results_BCGSK = [finalresults mean(finalresults,2)];
solution_BCGSK = solution';
str2='Allresults_BCGSK';
str3=strcat(str2, num2str(Pro), '.xls');
xlswrite( str3, Allresults_BCGSK') ;
str2='results_BCGSK';
str3=strcat(str2, num2str(Pro), '.xls');
xlswrite( str3, results_BCGSK) ;
str2='solution_BCGSK';
str3=strcat(str2, num2str(Pro),'.xls');
xlswrite( str3, solution_BCGSK) ;
return