Operator '*' is not supported for operands of type 'struct'.
82 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
tahseen alshmary
am 4 Okt. 2021
Beantwortet: tahseen alshmary
am 4 Okt. 2021
Dear Sir
Nice Greeting
I get this Error
Operator '*' is not supported for operands of type 'struct'.
with this objective function
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);
How can I solve this Error
Thanks in advance
4 Kommentare
Andrea Strappato
am 4 Okt. 2021
x(i) is the i-values/element from your x struct data type. So , for example, to get the i-th element from x try this: "struct_name(i).x".
Can you show your struct x data type?
Akzeptierte Antwort
Walter Roberson
am 4 Okt. 2021
The code itself did not have the problem you reported, but you need a replacement PSO.m file.
% Problem preparation
problem.nVar = 14;
problem.ub = 1.1 * ones(1, 14);
problem.lb = 0.05 * ones(1, 14);
problem.fobj = @ofun;
%--------------------------------------------------------------------------
noP=30; %number of population
maxIter=1000; %number of iteration
func=@ofun;
visFlag = 1; % set this to 0 if you do not want visualization
RunNo = 30;
BestSolutions_PSO = zeros(1 , RunNo);
%%
[GBEST,cgcurve ] = PSO( noP , maxIter, problem , visFlag ) ;
The modified PSO.m is attached.
Also, I optimized your ofun slightly;
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 = zeros(1,0, 'like', x);
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;
if isa(x, 'sym')
D = arrayfun(@(C) piecewise(C < 0, -C*1000, C), c0);
else
D = c0;
mask = D < 0;
D(mask) = -1000*D(mask);
end
Z = sum(D);
f=of+Z; % fitness function
end
2 Kommentare
Walter Roberson
am 4 Okt. 2021
You posted your entire code, and that code did not have a call
func(GBEST);
If you want to know the value of the function at the best location, just examine
GBEST.O
and the best location found is
GBEST.X
Weitere Antworten (2)
tahseen alshmary
am 4 Okt. 2021
Bearbeitet: Walter Roberson
am 4 Okt. 2021
10 Kommentare
Walter Roberson
am 4 Okt. 2021
Bearbeitet: Walter Roberson
am 4 Okt. 2021
Mathworks does not supply a function named PSO . The Mathworks particle swarm function is named particleswarm()
So we would need to know where you found the PSO.m function .
Siehe auch
Kategorien
Mehr zu Direct Search finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!