Error using ofun Too many input arguments.

조회 수: 2 (최근 30일)
tahseen alshmary
tahseen alshmary 2021년 10월 11일
댓글: Walter Roberson 2021년 10월 11일
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

채택된 답변

Walter Roberson
Walter Roberson 2021년 10월 11일
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
Walter Roberson
Walter Roberson 2021년 10월 11일
You need to replace
option.fobj = @ofun;
with
option.fobj0 = @ofun;
option.fobj = @fitFCN_BX;

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alan Weiss
Alan Weiss 2021년 10월 11일
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
tahseen alshmary
tahseen alshmary 2021년 10월 11일
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);
Walter Roberson
Walter Roberson 2021년 10월 11일
option.fobj0 = @ofun;
option.fobj = @fitFCN_BX;
option.XB = 0;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by