Why I get this message?

1 visualización (últimos 30 días)
Sadiq Akbar
Sadiq Akbar el 7 de En. de 2021
Comentada: Mathieu NOE el 11 de En. de 2021
When I run this code:
clear all
clc
close all
d=4; %2; % dimension of the problem
options.ECPSize=2; %50; % Population size
options.MaxIter=2;%100; % Maximum number of iterations
options.V=[1 2 1]; % Startegy, NPI, Archive size
options.ObjFunction=@myfitness; %@Ackley; % Name of the objective function
options.lb=[0 0 0 0]; %-32*ones(1,d); % Lower boundaries
options.ub=[10 10 pi pi]; %32*ones(1,d); % Upper boundaries
options.ProblemSize=length(options.ub); % Number of the decision variables
options.Display_Flag=1; % Flag for displaying results over iterations
options.run_parallel_index=0; % 1 to run the different runs in parallel
options.run=2; %10; % number of runs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Till here %%%%%%%%%%%%%%
%--------------------------------------------------------------------------
bestFitnessEvolution=[];
%--------------------------------------------------------------------------
ECPSize = options.ECPSize;
MaxIter = options.MaxIter; % MaxIter: maximum number of iterations
Strategy=options.V(1);
NPI=options.V(2);
archSize=ECPSize/options.V(3);
ObjFunction=options.ObjFunction;
lb=options.lb; % Lower boundaries
ub=options.ub; % Upper boundaries
ProblemSize=options.ProblemSize; % Number of the decision variables
%--------------------------------------------------------------------------
% Initialization
ECP=repmat(lb,ECPSize,1)+rand(ECPSize,ProblemSize).*repmat((ub-lb),ECPSize,1);
F_ECP = feval(ObjFunction,ECP); nEval=ECPSize;
[F_ECP, ind] = sort(F_ECP);
ECP = ECP(ind,:);
if Strategy==1
pop_fac=2*nchoosek(NPI,2);
elseif Strategy==2
pop_fac=NPI;
elseif Strategy==3
pop_fac=2*nchoosek(NPI,2)+NPI;
end
% Main Loop
for iter=1:MaxIter
pop_arch = ECP(1:archSize,:);
F_arch=F_ECP(1:archSize);
newECP=[];
newECP1=[];
newECP2=[];
for i=1:1:ceil(ECPSize/pop_fac)
% generate beta = Gaussian number, with mean=0.7 and standard
% deviation=0.1
Force = normrnd(0.7,0.2);
SP=sort(randperm(ECPSize,NPI));
SP=SP(1:NPI);
if Strategy==1
for ii=1:NPI
for jj=1:NPI
S1=ECP(SP(ii),:)+ Force * (ECP(1,:) - ECP(SP(ii),:));
if jj<ii
S1= S1 + Force * (ECP(SP(jj),:) - ECP(SP(ii),:));
newECP(end+1,:)=S1;
elseif jj>ii
S1= S1 - Force * (ECP(SP(jj),:) - ECP(SP(ii),:));
newECP(end+1,:)=S1;
end
end
end
elseif Strategy==2
for ii=1:NPI
S1=ECP(SP(ii),:)+ 0*Force * (ECP(1,:) - ECP(SP(ii),:));
for jj=1:NPI
if jj<ii
S1= S1 + Force * (ECP(SP(jj),:) - ECP(SP(ii),:));
elseif jj>ii
S1= S1 - Force * (ECP(SP(jj),:) - ECP(SP(ii),:));
end
end
newECP(end+1,:)=S1;
end
elseif Strategy==3
for ii=1:NPI
S2=ECP(SP(ii),:)+ 1*Force * (ECP(1,:) - ECP(SP(ii),:));
for jj=1:NPI
S1=ECP(SP(ii),:)+ Force * (ECP(1,:) - ECP(SP(ii),:));
if jj<ii
S1= S1 + Force * (ECP(SP(jj),:) - ECP(SP(ii),:));
S2= S2 + Force * (ECP(SP(jj),:) - ECP(SP(ii),:));
newECP1(end+1,:)=S1;
elseif jj>ii
S1= S1 - Force * (ECP(SP(jj),:) - ECP(SP(ii),:));
S2= S2 - Force * (ECP(SP(jj),:) - ECP(SP(ii),:));
newECP1(end+1,:)=S1;
end
end
newECP2(end+1,:)=S2;
end
newECP=[newECP1; newECP2];
end
end
newECP = bound(newECP,lb, ub);
for i1=1:size(newECP,1)
for j=1:ProblemSize
r=rand;
if (r<0.2)
pos= randi(archSize(1));
newECP(i1,j)= pop_arch(pos,j);
end
end
end
ECP_All=[pop_arch; newECP];
F_All=[F_arch feval(ObjFunction,newECP)];
nEval=nEval+size(newECP,1);
[F_All, index] = sort(F_All);
ECP_All = ECP_All( index, : );
ECP = ECP_All (1:ECPSize,:);
F_ECP = F_All(1:ECPSize);
bestX=ECP(1,:);
bestFitnessEvolution(iter)=F_ECP(1);
if options.Display_Flag==1
fprintf('Iteration N�� is %g Best Fitness is %g\n',iter,F_ECP(1))
end
end
bestFitness=F_ECP(1);
where myfitness is:
function out=myfitness(b)
u = [1 2 3 4];
out1 = u-b; %needs R2016b or later
out = sum(out1.^2,2);
end
I get this error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in ECPO_v1 (line 124)
F_All=[F_arch feval(ObjFunction,newECP)];
  3 comentarios
Sadiq Akbar
Sadiq Akbar el 10 de En. de 2021
Thank you very much dear Mathieu NOE. Yes I have noticed that in the 1st iteration, size of F_arch=2x1 and newECP=2x4, but in 2nd iteration, size of F_arch=1x2 but that of newECP remains the same 2x4.Why this happens? I don't know and due to this this error occurs.But what to do with this to remove this bug?
Mathieu NOE
Mathieu NOE el 11 de En. de 2021
hello
you have to insert some break points where F_arch appears either as input or output
I see that the main loop does involve F_arch in multiple ways and there must be somewhere the explanation why F_arch changes sizes when you iterates :
Going bottom up in your code :
F_arch is concatenated with feval(ObjFunction,newECP) to generate F_All
a few lines after, F_ECP is generated from the above , so depends from F_arch
F_All=[F_arch feval(ObjFunction,newECP)];
nEval=nEval+size(newECP,1);
[F_All, index] = sort(F_All);
ECP_All = ECP_All( index, : );
ECP = ECP_All (1:ECPSize,:);
F_ECP = F_All(1:ECPSize);
and at the next iteration, F_arch will be (re)defined from F_ECP :
% Main Loop
for iter=1:MaxIter
pop_arch = ECP(1:archSize,:);
F_arch=F_ECP(1:archSize);
so test your code carefully line by line (insert breakpoints and check dimensions )
good luck !

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 8 de En. de 2021
Use the debugger to check the sizes of the arrays:
dbstop if error
Type this in the command line and run the code again. When it stops at the error, check:
size(F_arch)
size(feval(ObjFunction,newECP))
  5 comentarios
Sadiq Akbar
Sadiq Akbar el 10 de En. de 2021
Thank you Jan but again it doesn't solve the problem
Sadiq Akbar
Sadiq Akbar el 10 de En. de 2021
Thank you very much dear Image Analyst. Yes, I know that they have different sizes as I have told earlier. But the problem is why it is so? Further, what should we do so that this problem is overcome?

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by