Why the given codes give errors for vector bounds but runs well for scalar bounds?

3 vues (au cours des 30 derniers jours)
I have downloaded an algorithm BBO from the Mathworks site. I run it with my fitness function "fitVectorized1.m". I oberved the following;
1- When I run it for scalar bounds, it works and gives reults
But
2- When I run it for vector bounds, it gives me errors.
What to do?
  4 commentaires
KSSV
KSSV le 31 Juil 2023
u=[1 5 30 70];dim=length(u);
[BestX,fmin]=bbo3(@(b)fitVectorized1(b,u),dim,0,90,100,50)
Unrecognized function or variable 'RouletteWheelSelection'.
Error in bbo3 (line 75)
j=RouletteWheelSelection(EP);
Sadiq Akbar
Sadiq Akbar le 31 Juil 2023
Sorry for forgetting to upload the rrquired file. Here it is attached.

Connectez-vous pour commenter.

Réponse acceptée

KSSV
KSSV le 31 Juil 2023
Use this function instead of your bbo3.
function [BestX,fmin]=bbo3(obj,dim,lb,ub,iter,pop1)
%% Problem Definition
CostFunction=obj; % Cost Function
nVar=dim; % Number of Decision Variables
VarSize=[1 nVar]; % Decision Variables Matrix Size
VarMin=lb; % Decision Variables Lower Bound
VarMax=ub; % Decision Variables Upper Bound
%% BBO Parameters
MaxIt=iter; % Maximum Number of Iterations
nPop=pop1;%50; % Number of Habitats (Population Size)
KeepRate=0.01;%0.2; % Keep Rate
nKeep=round(KeepRate*nPop); % Number of Kept Habitats
nNew=nPop-nKeep; % Number of New Habitats
% Migration Rates
mu=linspace(1,0,nPop); % Emmigration Rates
lambda=1-mu; % Immigration Rates
alpha=0.9;
pMutation=0.1;
sigma=0.02*(VarMax-VarMin);
%% Initialization
% Empty Habitat
habitat.Position=zeros([],dim);
habitat.Cost=[];
% Create Habitats Array
pop=repmat(habitat,nPop,1);
% Initialize Habitats
for i=1:nPop
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
pop(i).Cost=CostFunction(pop(i).Position);
end
% Sort Population
[~, SortOrder]=sort([pop.Cost]);
pop=pop(SortOrder);
% Best Solution Ever Found
BestSol=pop(1);
% Array to Hold Best Costs
BestCost=zeros(MaxIt,1);
%% BBO Main Loop
for it=1:MaxIt
newpop=pop;
for i=1:nPop
for k=1:nVar
% Migration
if rand<=lambda(i)
% Emmigration Probabilities
EP=mu;
EP(i)=0;
EP=EP/sum(EP);
% Select Source Habitat
j=RouletteWheelSelection(EP);
% Migration
newpop(i).Position(k)=pop(i).Position(k) ...
+alpha*(pop(j).Position(k)-pop(i).Position(k));
end
% Mutation
if rand<=pMutation
newpop(i).Position(k)=newpop(i).Position(k)+sigma(k)*randn; %<------ Changed here
end
end
% Apply Lower and Upper Bound Limits
newpop(i).Position = max(newpop(i).Position, VarMin);
newpop(i).Position = min(newpop(i).Position, VarMax);
% Evaluation
newpop(i).Cost=CostFunction(newpop(i).Position);
end
% Sort New Population
[~, SortOrder]=sort([newpop.Cost]);
newpop=newpop(SortOrder);
% Select Next Iteration Population
pop=[pop(1:nKeep)
newpop(1:nNew)];
% Sort Population
[~, SortOrder]=sort([pop.Cost]);
pop=pop(SortOrder);
% Update Best Solution Ever Found
BestSol=pop(1);
% Store Best Cost Ever Found
BestCost(it)=BestSol.Cost;
% Show Iteration Information
%disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
end
%% Results
% % figure;
% % %plot(BestCost,'LineWidth',2);
% % semilogy(BestCost,'LineWidth',2);
% % xlabel('Iteration');
% % ylabel('Best Cost');
% % grid on;
BestX=BestSol.Position % By Me
fmin=BestSol.Cost % By Me

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by