Operator '*' is not supported for operands of type 'struct'.

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 Commenti

what is class(x) when you stop in the function? How is the function being invoked?
Hi,
The Matlab struct type is defined as "struct_name = struct('element_name', Array)" where Array is the set of x(i) values. So to extract the x(i) value try to write "struct_name(i).x".
Hi
excuse me, can you writ the objective function to me by formula x(i)
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?

Accedi per commentare.

 Risposta accettata

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 Commenti

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

Accedi per commentare.

Più risposte (2)

what do you mean
the full objective function below
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

10 Commenti

Please post your entire code.
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
What about the x variable? It's seems not defined.
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)*1000;
end
D(i) = abs(c0(i));
end
Z = sum(D);
f=of+Z; % fitness function
Okay, but please show one of the calls to ofun . We need to see what you are passing in to ofun()
noP=10; %number of population
maxIter=1000; %number of iteration
func=@ofun;
visFlag = 1; % set this to 0 if you do not want visualization
I do not see any call to ofun there. I see you constructing a function handle and assigning it to func, but I do not see you calling ofun or func .
The code you posted hints to me that you might have created your own genetic algorithm code ?
clear all
close all
clc
tic
warning off
addpath('PSO')
% 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 ) ;
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 .

Accedi per commentare.

Categorie

Prodotti

Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by