standard and probabilistic bee algorithm explanation
Show older comments
%% Problem Definition
CostFunction=@(x) Sphere(x); % Cost Function
nVar=5; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
VarMin=-10; % Decision Variables Lower Bound
VarMax= 10; % Decision Variables Upper Bound
%% Bees Algorithm Parameters
MaxIt=1000; % Maximum Number of Iterations
nScoutBee=30; % Number of Scout Bees
nSelectedSite=round(0.5*nScoutBee); % Number of Selected Sites
nEliteSite=round(0.4*nSelectedSite); % Number of Selected Elite Sites
nSelectedSiteBee=round(0.5*nScoutBee); % Number of Recruited Bees for Selected Sites
nEliteSiteBee=2*nSelectedSiteBee; % Number of Recruited Bees for Elite Sites
r=0.1*(VarMax-VarMin); % Neighborhood Radius
rdamp=0.95; % Neighborhood Radius Damp Rate
%% Initialization
% Empty Bee Structure
empty_bee.Position=[];
empty_bee.Cost=[];
% Initialize Bees Array
bee=repmat(empty_bee,nScoutBee,1);
% Create New Solutions
for i=1:nScoutBee
bee(i).Position=unifrnd(VarMin,VarMax,VarSize);
bee(i).Cost=CostFunction(bee(i).Position);
end
% Sort
[~, SortOrder]=sort([bee.Cost]);
bee=bee(SortOrder);
% Update Best Solution Ever Found
BestSol=bee(1);
% Array to Hold Best Cost Values
BestCost=zeros(MaxIt,1);
%% Bees Algorithm Main Loop
for it=1:MaxIt
% Elite Sites
for i=1:nEliteSite
bestnewbee.Cost=inf;
for j=1:nEliteSiteBee
newbee.Position=PerformBeeDance(bee(i).Position,r);
newbee.Cost=CostFunction(newbee.Position);
if newbee.Cost<bestnewbee.Cost
bestnewbee=newbee;
end
end
if bestnewbee.Cost<bee(i).Cost
bee(i)=bestnewbee;
end
end
% Selected Non-Elite Sites
for i=nEliteSite+1:nSelectedSite
bestnewbee.Cost=inf;
for j=1:nSelectedSiteBee
newbee.Position=PerformBeeDance(bee(i).Position,r);
newbee.Cost=CostFunction(newbee.Position);
if newbee.Cost<bestnewbee.Cost
bestnewbee=newbee;
end
end
if bestnewbee.Cost<bee(i).Cost
bee(i)=bestnewbee;
end
end
% Non-Selected Sites
for i=nSelectedSite+1:nScoutBee
bee(i).Position=unifrnd(VarMin,VarMax,VarSize);
bee(i).Cost=CostFunction(bee(i).Position);
end
% Sort
[~, SortOrder]=sort([bee.Cost]);
bee=bee(SortOrder);
% Update Best Solution Ever Found
BestSol=bee(1);
% Store Best Cost Ever Found
BestCost(it)=BestSol.Cost;
% Display Iteration Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
% Damp Neighborhood Radius
r=r*rdamp;
end
%% Results
figure;
%plot(BestCost,'LineWidth',2);
semilogy(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
6 Comments
James Tursa
on 12 Jun 2020
Do you have a question?
Bisrat Yeshidagna
on 12 Jun 2020
Edited: Bisrat Yeshidagna
on 12 Jun 2020
James Tursa
on 12 Jun 2020
You want someone to go through this code line by line and explain it to you? That's a lot to ask. Maybe you could instead ask about specific parts of the code that you don't understand.
John D'Errico
on 12 Jun 2020
In depth explanations of lengthy codes are way beyond the scope of Answers. That could require some to spend hours or days of time, writing explanations for things you already understand, including teaching you an entire course about optimization here, as well as MATLAB itself.
If you have a SPECIFIC question about a line in the code, then ask it. Otherwise, your question is far too broad.
Bisrat Yeshidagna
on 13 Jun 2020
Bisrat Yeshidagna
on 13 Jun 2020
Answers (0)
Categories
Find more on Physics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!