I Have a MATLAB CODE for Artificial Bee Colony , anyone can help me to plot the convergence graph?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
clear all
close all
clc
warning off
objfun='tracklsqPID';
D=3;% dimension, number of parameters
R=40;% upper bound
R1=0;% Lower bound
NP=10; %number of population N*D
limit=30; %special function for scoutbee
maxCycle=15;%iteration
FoodNumber=NP/2;
ub=ones(1,D)*R; %/*Upper bounds of the parameters. */
lb=ones(1,D)*(R1);%/*lower bound of the parameters.*/
runtime=1;%/*Algorithm can be run many times in order to see its robustness*/
ObjVal(:,:)=0*ones(1,FoodNumber);
GlobalMins=zeros(1,runtime);
for r=1:runtime
% /*All food sources are initialized */
%/*Variables are initialized in the range [lb,ub]. If each parameter has different range, use arrays lb[j], ub[j] instead of lb and ub */
tic;
Range = repmat((ub-lb),[FoodNumber 1]);
Lower = repmat(lb, [FoodNumber 1]);
Foods = rand(FoodNumber,D) .* Range + Lower;
% i=1
for i=1:FoodNumber
ObjVal(1,i)=feval(objfun,Foods(i,:));
end
Fitness=calculateFitness(ObjVal);
FirstFitness = Fitness;
%reset trial counters
trial=zeros(1,FoodNumber);
%/*The best food source is memorized*/
BestInd=find(ObjVal==min(ObjVal));
BestInd=BestInd(end);
GlobalMin=ObjVal(BestInd);
GlobalParams=Foods(BestInd,:);
iter=1;
while ((iter <= maxCycle))
%%%%%%%%% EMPLOYED BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%
for i=1:(FoodNumber)
%/*The parameter to be changed is determined randomly*/
Param2Change=fix(rand*D)+1;
%/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
neighbour=fix(rand*(FoodNumber))+1;
%/*Randomly selected solution must be different from the solution i*/
while(neighbour==i)
neighbour=fix(rand*(FoodNumber))+1;
end;
sol=Foods(i,:);
% /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
sol(Param2Change)=Foods(i,Param2Change)+(Foods(i,Param2Change)-Foods(neighbour,Param2Change))*(rand-0.5)*2;
% /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
ind=find(sol<lb);
sol(ind)=lb(ind);
ind=find(sol>ub);
sol(ind)=ub(ind);
%evaluate new solution
ObjValSol=feval(objfun,sol);
FitnessSol=calculateFitness(ObjValSol);
% /*a greedy selection is applied between the current solution i and its mutant*/
if (FitnessSol>Fitness(i)) %/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
Foods(i,:)=sol;
Fitness(i)=FitnessSol;
ObjVal(i)=ObjValSol;
trial(i)=0;
else
trial(i)=trial(i)+1; %/*if the solution i can not be improved, increase its trial counter*/
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%% CalculateProbabilities %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%/* A food source is chosen with the probability which is proportioal to its quality*/
%/*Different schemes can be used to calculate the probability values*/
%/*For example prob(i)=fitness(i)/sum(fitness)*/
%/*or in a way used in the metot below prob(i)=a*fitness(i)/max(fitness)+b*/
%/*probability values are calculated by using fitness values and normalized by dividing maximum fitness value*/
prob=(0.9*Fitness./max(Fitness))+0.1;
%%%%%%%%%%%%%%%%%%%%%%%% ONLOOKER BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
i=1;
t=0;
while(t<FoodNumber)
if(rand<prob(i))
t=t+1;
%/*The parameter to be changed is determined randomly*/
Param2Change=fix(rand*D)+1;
%/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
neighbour=fix(rand*(FoodNumber))+1;
%/*Randomly selected solution must be different from the solution i*/
while(neighbour==i)
neighbour=fix(rand*(FoodNumber))+1;
end;
sol=Foods(i,:);
% /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
sol(Param2Change)=Foods(i,Param2Change)+(Foods(i,Param2Change)-Foods(neighbour,Param2Change))*(rand-0.5)*2;
% /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
ind=find(sol<lb);
sol(ind)=lb(ind);
ind=find(sol>ub);
sol(ind)=ub(ind);
%evaluate new solution
ObjValSol=feval(objfun,sol);
FitnessSol=calculateFitness(ObjValSol);
% /*a greedy selection is applied between the current solution i and its mutant*/
if (FitnessSol>Fitness(i)) %/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
Foods(i,:)=sol;
Fitness(i)=FitnessSol;
ObjVal(i)=ObjValSol;
trial(i)=0;
else
trial(i)=trial(i)+1; %/*if the solution i can not be improved, increase its trial counter*/
end;
end;
i=i+1;
if (i==(FoodNumber)+1)
i=1;
end;
end;
GlobalParams;
%/*The best food source is memorized*/
ind=find(ObjVal==min(ObjVal));
ind=ind(end);
if (ObjVal(ind)<GlobalMin)
GlobalMin=ObjVal(ind);
GlobalParams=Foods(ind,:);
end;
best(:,iter)=GlobalParams;
%/*The best food source is memorized*/
%%%%%%%%%%%% SCOUT BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%/*determine the food sources whose trial counter exceeds the "limit" value.
%In Basic ABC, only one scout is allowed to occur in each cycle*/
ind=find(trial==max(trial));
ind=ind(end);
if (trial(ind)>limit)
trial(ind)=0;
sol=(ub-lb).*rand(1,D)+lb;
ObjValSol=feval(objfun,sol);
FitnessSol=calculateFitness(ObjValSol);
Foods(ind,:)=sol;
Fitness(ind)=FitnessSol;
ObjVal(ind)=ObjValSol;
end;
fprintf('GloabalParams = \n\n kp=%f ki=%f kd=%f \n\n ',GlobalParams);
fprintf(' Ýter=%d ObjVal=%g D=%g\n\n ',iter,GlobalMin,D);
GlobalParams;
k(:,iter)=GlobalMin;
kl(:,iter)=GlobalParams;
iter=iter+1;
end % End of ABC
GlobalMins(r)=GlobalMin;
toc
kH=toc;
end; %end of runs
save all
1 Kommentar
Sam Chak
am 11 Aug. 2023
Hi @AZRUL AZIM
Since you have established the lower and upper bounds to range between 0 and 40 in order to ensure the discovery of a solution within this specific range, could you evaluate the ABC algorithm using the provided simple convex function tracklsqPID(x)? If it operates as expected, it should yield the solution at .
objfun = @tracklsqPID;
[xsol, fval, exitflag, output] = ga(objfun, 1, [], [], [], [], 0, 40)
function J = tracklsqPID(x)
J = (x - 20).^2;
end
Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!