hi please help me to fix this error: Attempt to reference field of non-structure array,

1 回表示 (過去 30 日間)
James Kossi
James Kossi 2021 年 9 月 7 日
コメント済み: James Kossi 2021 年 9 月 9 日
I am trying to run this code:
function [bestX,bestFitness,bestFitnessEvolution,RESULTS,nEval]=PSO_v2(options)
bestFitnessEvolution=[];
nEval=0;
ProblemSize=options.ProblemSize;
PopulationSize=options.PopulationSize;
minPosition=options.minPosition;
maxPosition=options.maxPosition;
MaxIter=options.MaxIter;
ObjFunction=options.ObjFunction;
ObjFunctionDisplay=options.ObjFunctionDisplay;
c1=options.c1;
c2=options.c2;
w=options.w;
d=abs(maxPosition-minPosition);
Pposition=zeros(PopulationSize,ProblemSize);
Pvelocity=rand(PopulationSize,ProblemSize);
for i=1:ProblemSize
Pposition(:,i)=minPosition(i)+rand(PopulationSize,1)*(maxPosition(i)-minPosition(i));
Pvelocity(:,i)=-d(i)+2*rand(PopulationSize,1)*d(i);
end
P_cost=feval(ObjFunction,Pposition,options);
nEval=nEval+length(P_cost);
Pp_best_cost=P_cost;
Pp_best=Pposition;
but i get this error:
Attempt to reference field of non-structure array.
Error in PSO_v2 (line 17)
ProblemSize=options.ProblemSize;
please how can i fix this error
  1 件のコメント
Sajid Afaque
Sajid Afaque 2021 年 9 月 7 日
please try to put your code using code section for better readability
%put your code like this

サインインしてコメントする。

回答 (1 件)

KSSV
KSSV 2021 年 9 月 7 日
The input which you are using i.e. options should be a structure with fields
ProblemSize
PopulationSize
minPosition
maxPosition
Check your input. Is it a structure? Use class for this. If it is a structure check does it have those fields.

カテゴリ

Help Center および File ExchangeParticle Swarm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by