standard and probabilistic bee algorithm explanation
이전 댓글 표시
%% 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
James Tursa
2020년 6월 12일
Do you have a question?
Bisrat Yeshidagna
2020년 6월 12일
편집: Bisrat Yeshidagna
2020년 6월 12일
James Tursa
2020년 6월 12일
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
2020년 6월 12일
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
2020년 6월 13일
Bisrat Yeshidagna
2020년 6월 13일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!