Error using ofun
Too many input arguments.
Error in AFO1 (line 181)
y(i)=option.fobj(x(i,:),option,data);
Error in main_AFO (line 31)
[bestY,bestX,recording]=AFO1(x,y,option,data);
my fuction is
function f = ofun(x) % objective function (minimization)
of =2.6773*x(1)+3.1007*x(2)+3.2178*x(3)+ 3.8021*x(4)+3.6727*x(5)+3.0442*x(6)+1.6362*x(7)+3.0479*x(8)+3.3917*x(9)+3.7468*x(10)+3.8989*x(11)+3.1073*x(12)+3.3207*x(13)+ 1.6382*x(14);
% if there is no constraints then comments all c0 lines below
c0=[];
CTI =0.3;
A(1) =2.6773*x(1); B(1) =4.2191*x(6); %first row
A(2) =3.1007*x(2); B(2) =5.0777*x(1); %second row
A(3) =3.1007*x(2); B(3) =2.1891*x(7); %third row
A(4) =3.2178*x(3); B(4) =3.9908*x(2); %fourth row
A(5) =3.8021*x(4); B(5) =4.1278*x(3); %fifth row
A(6) =3.6727*x(5); B(6) =5.0785*x(4); %sixth row
A(7) =3.0442*x(6); B(7) =5.8436*x(5); %seventh row
A(8) =1.6362*x(7); B(8) =5.8436*x(5); %eighth row
A(9) =3.0442*x(6); B(9) =2.1962*x(14); %nighth row
A(10)=1.6362*x(7); B(10)=7.3310*x(13); %tenth row
A(11)=3.0479*x(8); B(11)=2.1891*x(7); %eleventh row
A(12)=3.0479*x(8); B(12)=5.4250*x(9); %tweleventh row
A(13)=3.3917*x(9); B(13)=4.9527*x(10); %thirteenth row
A(14)=3.7468*x(10); B(14)=5.2862*x(11); %fourteenth row
A(15)=3.8989*x(11); B(15)=3.8989*x(12); %fiveteenth row
A(16)=3.1073*x(12); B(16)=7.3310*x(13); %sixteenth row
A(17)=3.1073*x(12); B(17)=2.1962*x(14); %seventennth row
A(18)=3.3207*x(13); B(18)=4.4351*x(8); %eighteenth row
A(19)=1.6382*x(14); B(19)=5.0777*x(1); %nighnteenth row
A(20)=1.6382*x(14); B(20)=5.4250*x(9); % twenty row
c0(1)= B(1) -A(1)-CTI;
c0(2)= B(2) -A(2)-CTI;
c0(3)= B(3) -A(3)-CTI;
c0(4)= B(4) -A(4)-CTI;
c0(5)= B(5) -A(5)-CTI;
c0(6)= B(6) -A(6)-CTI;
c0(7)= B(7) -A(7)-CTI;
c0(8)= B(8) -A(8)-CTI;
c0(9)= B(9) -A(9)-CTI;
c0(10)= B(10)-A(10)-CTI;
c0(11)= B(11)-A(11)-CTI;
c0(12)= B(12)-A(12)-CTI;
c0(13)= B(13)-A(13)-CTI;
c0(14)= B(14)-A(14)-CTI;
c0(15)= B(15)-A(15)-CTI;
c0(16)= B(16)-A(16)-CTI;
c0(17)= B(17)-A(17)-CTI;
c0(18)= B(18)-A(18)-CTI;
c0(19)= B(19)-A(19)-CTI;
c0(20)= B(20)-A(20)-CTI;
% for d = 1: length(c0)
% if c0(d)<0
% c(d)=1;
% else
% c(d)=0;
% end
% end
% penalty = 10000; % penalty on each constraint violation
% E = penalty*sum(c);
for i=1:length(c0)
if c0(i)<0
c0(i)=c0(i)*10000;
end
D(i) = abs(c0(i));
end
Z = sum(D);
f=of+Z; % fitness function
%---------------------------------------------------------------------------------------------end
the code being from

 Réponse acceptée

You constructed your own main_AFO file.m file, probably based upon the main1.m in the File Exchange contribution.
The main1.m source code has
[lb,ub,dim,fobj] = Get_Functions_details(option.F);
%stuff
option.fobj0=fobj;
option.fobj=@fitFCN_BX;
I suspect that you instead did
options.fobj = @fobj;
but that you instead should have done
option.fobj0 = @fobj;
option.fobj=@fitFCN_BX;
Unfortunately once again someone has asked us to debug code without providing us with a copy of the code, so we can only guess about the details.

3 commentaires

tahseen alshmary
tahseen alshmary le 11 Oct 2021
Modifié(e) : Walter Roberson le 11 Oct 2021
Sorry
The code is
addpath('benchmark functions')
% Problem preparation
option.dim = 14;
option.ub = 1.1* ones(1,option.dim);
option.lb = 0.05* ones(1,option.dim);
option.fobj = @ofun;
option.maxIteration=1000; %number of iteration
option.numAgent=10; %number of dingoes that will attack
% µÛÆó¶ìËã·¨
option.v_lb=-(option.ub-option.lb)/4;
option.v_ub=(option.ub-option.lb)/4;
option.w2=0.5; %weight of Moving strategy III
option.w4=1;%weight of Moving strategy III
option.w5=1;%weight of Moving strategy III
option.pe=0.01; % rate to judge Premature convergence
option.gap0=ceil(sqrt(option.maxIteration*2))+1;
option.gapMin=5; % min gap
option.dec=2; % dec of gap
option.L=10; % Catastrophe
data=[];
x=ones(option.numAgent,option.dim);
y=ones(option.numAgent,1);
%-------------------------------------------------------------------------
%%
[bestY,bestX,recording]=AFO1(x,y,option,data);
You need to replace
option.fobj = @ofun;
with
option.fobj0 = @ofun;
option.fobj = @fitFCN_BX;

Connectez-vous pour commenter.

Plus de réponses (1)

I cannot reproduce your issue. When I try the following, I do not get an error:
x = randn(20,1);
r = ofun(x)
r = 3.5276e+05
function f = ofun(x) % objective function (minimization)
of =2.6773*x(1)+3.1007*x(2)+3.2178*x(3)+ 3.8021*x(4)+3.6727*x(5)+3.0442*x(6)+1.6362*x(7)+3.0479*x(8)+3.3917*x(9)+3.7468*x(10)+3.8989*x(11)+3.1073*x(12)+3.3207*x(13)+ 1.6382*x(14);
% if there is no constraints then comments all c0 lines below
c0=[];
CTI =0.3;
A(1) =2.6773*x(1); B(1) =4.2191*x(6); %first row
A(2) =3.1007*x(2); B(2) =5.0777*x(1); %second row
A(3) =3.1007*x(2); B(3) =2.1891*x(7); %third row
A(4) =3.2178*x(3); B(4) =3.9908*x(2); %fourth row
A(5) =3.8021*x(4); B(5) =4.1278*x(3); %fifth row
A(6) =3.6727*x(5); B(6) =5.0785*x(4); %sixth row
A(7) =3.0442*x(6); B(7) =5.8436*x(5); %seventh row
A(8) =1.6362*x(7); B(8) =5.8436*x(5); %eighth row
A(9) =3.0442*x(6); B(9) =2.1962*x(14); %nighth row
A(10)=1.6362*x(7); B(10)=7.3310*x(13); %tenth row
A(11)=3.0479*x(8); B(11)=2.1891*x(7); %eleventh row
A(12)=3.0479*x(8); B(12)=5.4250*x(9); %tweleventh row
A(13)=3.3917*x(9); B(13)=4.9527*x(10); %thirteenth row
A(14)=3.7468*x(10); B(14)=5.2862*x(11); %fourteenth row
A(15)=3.8989*x(11); B(15)=3.8989*x(12); %fiveteenth row
A(16)=3.1073*x(12); B(16)=7.3310*x(13); %sixteenth row
A(17)=3.1073*x(12); B(17)=2.1962*x(14); %seventennth row
A(18)=3.3207*x(13); B(18)=4.4351*x(8); %eighteenth row
A(19)=1.6382*x(14); B(19)=5.0777*x(1); %nighnteenth row
A(20)=1.6382*x(14); B(20)=5.4250*x(9); % twenty row
c0(1)= B(1) -A(1)-CTI;
c0(2)= B(2) -A(2)-CTI;
c0(3)= B(3) -A(3)-CTI;
c0(4)= B(4) -A(4)-CTI;
c0(5)= B(5) -A(5)-CTI;
c0(6)= B(6) -A(6)-CTI;
c0(7)= B(7) -A(7)-CTI;
c0(8)= B(8) -A(8)-CTI;
c0(9)= B(9) -A(9)-CTI;
c0(10)= B(10)-A(10)-CTI;
c0(11)= B(11)-A(11)-CTI;
c0(12)= B(12)-A(12)-CTI;
c0(13)= B(13)-A(13)-CTI;
c0(14)= B(14)-A(14)-CTI;
c0(15)= B(15)-A(15)-CTI;
c0(16)= B(16)-A(16)-CTI;
c0(17)= B(17)-A(17)-CTI;
c0(18)= B(18)-A(18)-CTI;
c0(19)= B(19)-A(19)-CTI;
c0(20)= B(20)-A(20)-CTI;
% for d = 1: length(c0)
% if c0(d)<0
% c(d)=1;
% else
% c(d)=0;
% end
% end
% penalty = 10000; % penalty on each constraint violation
% E = penalty*sum(c);
for i=1:length(c0)
if c0(i)<0
c0(i)=c0(i)*10000;
end
D(i) = abs(c0(i));
end
Z = sum(D);
f=of+Z; % fitness function
%---------------------------------------------------------------------------------------------end
end
Alan Weiss
MATLAB mathematical toolbox documentation

6 commentaires

where can I put this
x = randn(20,1);
r = ofun(x)
The problem has to do with the (Emperor Penguin) optimization interface being used to call the function; it expects to call a function with three parameters, not a function with one parameter.
tahseen alshmary
tahseen alshmary le 11 Oct 2021
Modifié(e) : tahseen alshmary le 11 Oct 2021
So , How can I solve it .
it is also with Error
Unrecognized field name "XB".
Error in fitFCN_BX (line 3)
y=option.fobj0(x+option.XB);
Error in AFO1 (line 181)
y(i)=option.fobj(x(i,:),option,data);
Error in main_AFO (line 32)
[bestY,bestX,recording]=AFO1(x,y,option,data);
option.fobj0 = @ofun;
option.fobj = @fitFCN_BX;
option.XB = 0;

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear Programming and Mixed-Integer Linear Programming dans Centre d'aide et File Exchange

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by