Operator '<' is not supported for operands of type 'struct'.
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
When I test this code which is shown below. I got an error of " Operator '<' is not supported for operands of type 'struct'. "
Error in untitled2 (line 157)
if NewSol.Cost < pop(i).Cost
. How can i solve this error?
clc;
clear all;
close all;
%% Problem Definition
% [lb,ub,D,fobj] = Get_Functions_details(Function_name); % The shifted functions' information
D = 2; % Number of Decision Variables
lb1=[2 0.001];
ub1=[33 0.5 ];
[ sLOSS,Busvoltage]=power_flow1()
n = 200; % Population Size
m = 2; % Number of search agenets in a group
%% Generate initial population of cheetahs (Algorithm 1, L#2)
empty_individual.Position = [];
empty_individual.Cost = [];
BestSol.Cost = sLOSS;
pop = repmat(empty_individual,n,1);
for i=1:n
pop(i).Position = lb1+rand.*(ub1-lb1);
pop(i).Position(1) = round(pop(i).Position(1));
end
for i=1:n
pop(i).Cost = power_flow_DG(pop(i).Position(1),pop(i).Position(2))
if pop(i).Cost < BestSol.Cost
BestSol.Cost = pop(i).Cost; % Initial leader position
end
end
%% Initialization (Algorithm 1, L#3)
pop1 = pop; % Population's initial home position
BestCost = []; % Leader fittnes value in a current hunting period
X_best = BestSol; % Prey solution sofar
Globest = BestCost; % Prey fittnes value sofar
%% Initial parameters
t = 0; % Hunting time counter (Algorithm 1, L#4)
it = 1; % Iteration counter(Algorithm 1, L#5)
MaxIt = D*10000; % Maximum number of iterations (Algorithm 1, L#6)
T = ceil(D/10)*60; % Hunting time (Algorithm 1, L#7)
FEs = 0; % Counter for function evaluations (FEs)
%% CO Main Loop
while FEs <= MaxIt % Algorithm 1, L#8
m = 1+randi (ceil(n/2));
i0 = randi(n,1,m); % select a random member of cheetahs (Algorithm 1, L#9)
for k = 1 : m % Algorithm 1, L#10
i = i0(k);
% neighbor agent selection (Algorithm 1, L#11)
if k == length(i0)
a = i0(k-1);
else
a = i0(k+1);
end
X = pop(i).Position; % The current position of i-th cheetah
X1 = pop(a).Position; % The neighbor position
Xb = BestSol%.Position; % The leader position
Xbest = X_best%.Position;% The pery position
kk=0;
% Uncomment the follwing statements, it may improve the performance of CO
if i<=2 && t>2 && t>ceil(0.2*T+1) && abs(BestCost(t-2)-BestCost(t-ceil(0.2*T+1)))<=0.0001*Globest(t-1)
X = X_best.Cost;
kk = 0;
elseif i == 3
X = BestSol.Cost;
kk = -0.1*rand*t/T;
else
kk = 0.25;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if mod(it,100)==0 || it==1
xd = randperm(numel(X));
end
Z = X;
%% Algorithm 1, L#12
for j = 1:1 % select arbitrary set of arrangements
%% Algorithm 1, L#13
r_Hat = randn; % Randomization paameter, Equation (1)
r1 = rand;
if k == 1 % The leader's step length (it is assumed that k==1 is associated to the leade number)
alpha = 0.001*t/T.*(ub1(j)-lb1(j)); % Step length, Equation (1) %This can be updated by desired equation
else % The members' step length
%alpha = 0.001 * t / T * abs(Xb.j - X.j) + 0.001 * round(double(rand > 0.9));
alpha = 0.001*t/T*abs(Xb(j).Cost-X(j))+0.001.*round(double(rand>0.9));%member step length, Equation (1)%This can be updated by desired equation
end
r = randn;
r_Check = abs(r).^exp(r/2).*sin(2*pi*r); % Turning factor, Equation (3)%This can be updated by desired equation
beta = X1(j)-X(j); % Interaction factor, Equation (3)
h0 = exp(2-2*t/T);
H = abs(2*r1*h0-h0);
%% Algorithm 1, L#14
r2 = rand;
r3 = kk+rand;
%% Strategy selection mechanism
if r2 <= r3 % Algorithm 1, L#15
r4 = 3*rand; % Algorithm 1, L#16
if H > r4 % Algorithm 1, L#17
Z(j) = X(j)+r_Hat.^-1.*alpha; % Search, Equation(1) (Algorithm 1, L#18)
else
Z(j) = Xbest(j).Cost+r_Check.*beta; % Attack, Equation(3) (Algorithm 1, L#20)
end
else
Z(j) = X(j); % Sit&wait, Equation(2) (Algorithm 1, L#23)
end
end
%% Update the solutions of member i (Algorithm 1, L#26)
% Check the limits
xx1=find(Z<lb1);
Z(xx1)=lb1(xx1)+rand(1,numel(xx1)).*(ub1(xx1)-lb1(xx1));
xx1=find(Z>ub1);
Z(xx1)=lb1(xx1)+rand(1,numel(xx1)).*(ub1(xx1)-lb1(xx1));
% Evaluate the new position
NewSol.Position = Z;
NewSol.Position(1)= round(NewSol.Position(1));
NewSol.Cost = power_flow_DG(NewSol.Position(1),NewSol.Position(2));
%NewSol.taille=pop.taille;
if NewSol.Cost < pop(i).Cost
pop(i).Cost = NewSol.Cost;
if pop(i).Cost < BestSol.Cost
BestSol = pop(i);
end
end
FEs = FEs+1;
end
t = t+1; % (Algorithm 1, L#28)
%% Leave the prey and go back home (Algorithm 1, L#29)
if t>T && t-round(T)-1>=1 && t>2
if abs(BestCost(t-1)-BestCost(t-round(T)-1))<=abs(0.01*BestCost(t-1))
% Change the leader position (Algorithm 1, L#30)
best = X_best.Cost;
j0=randi(D,1,ceil(D/10*rand));
% best(j0) = lb1(j0)+rand(1,length(j0)).*(ub1(j0)-lb1(j0));
BestSol.Cost = power_flow_DG(NewSol.Position(1),NewSol.Position(2));
BestSol.Position = best; % Leader's new position
FEs = FEs+1;
i0 = randi(n,1,round(1*n));
% Go back home, (Algorithm 1, L#30)
pop(i0(n-m+1:n)) = pop1(i0(1:m)); % Some members back their initial positions
pop(i).Cost = X_best; % Substitude the member i by the prey (Algorithm 1, L#31)
t = 1; % Reset the hunting time (Algorithm 1, L#32)
end
end
it = it +1; % Algorithm 1, L#34
%% Update the prey (global best) position (Algorithm 1, L#35)
if BestSol.Cost<X_best.Cost
X_best=BestSol;
end
BestCost(t)=BestSol.Cost;
Globest(1,t)=X_best.Cost;
%% Display
if mod(it,500)==0
disp([' FEs>> ' num2str(FEs) ' BestCost = ' num2str(Globest(t))]);
end
end
fit(run) = Globest(end) ;
mean_f = mean (fit);
std_f = std (fit);
% clc
toc
1 commentaire
Stephen23
le 2 Avr 2024
Modifié(e) : Stephen23
le 2 Avr 2024
We cannot run your code because you have not provided us with power_flow1.m
But you can tell us the classes of the structure content that you are comparing.
Note that bdata33bus.m and ldata33busmod4.m are not runnable Mfies, and thus the .m extension is very misleading. You should simply use a .txt extension and e.g. READMATRIX to import their data.
Réponses (1)
Constantino Carlos Reyes-Aldasoro
le 2 Avr 2024
Hard to know the exact issue without access to all the code and variables but most probably it is because you cannot compare a struct, all of it, just specific fields, look at the following code:
a=3;
b=[1 2 3];
c.a =1; c.b=2;
a>1
b>1
% the following will create an error so let's use a try-catch
try
c>1
catch
disp('Error will be Operator ''>'' is not supported for operands of type ''struct''. ')
end
However, if you only try one field of the struct
c.a>1
That works, so look at the variable that you are trying to compare withy < and see if it is a struct. Then see which fields you need to compare and that should fix the problem.
0 commentaires
Voir également
Catégories
En savoir plus sur Variables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!