フィルターのクリア

why do i have so many errors when trying to use particle swarm optimisation in matlab?

3 ビュー (過去 30 日間)
his is my code for the objective function
function y = T(x,c)
% this is the objective function
if x(1)^2-x(2)+1 <= 0
y = x(1)^2 + x(2)^2;
elseif x(1)^2-x(2)+1 > 0
if c == 1
y = x(1)^2 + x(2)^2 + (1/2)*(x(1)^2-x(2)+1)^2;
elseif c == 10
y = x(1)^2 + x(2)^2 + (10/2)*(x(1)^2-x(2)+1)^2;
elseif c == 100
y = x(1)^2 + x(2)^2 + (100/2)*(x(1)^2-x(2)+1)^2;
end
end
And here is my script file in trying to obtain optimal values near global optimum by following the example in this link https://uk.mathworks.com/help/gads/optimize-using-particle-swarm-optimization.html
fun = @T;
n = 2;
rng default
[x,fval,exitflag] = particleswarm(fun,n)
lb = [-50;-50];
ub = -lb;
[x,fval,exitflag] = particleswarm(fun,n,lb,ub)
options = optimoptions('particleswarm','SwarmSize',100);
[x,fval,exitflag] = particleswarm(fun,n,lb,ub,options)
options.HybridFCn= @fmincon;
[x,fval,exitflag] = particleswarm(fun,n,lb,ub,options)
after executing the file i get these errors
Error using particleswarm>makeState (line 700)
Failure in initial objective function evaluation. PARTICLESWARM cannot
continue.
Error in particleswarm>pswcore (line 170)
state = makeState(nvars,lbMatrix,ubMatrix,objFcn,options);
Error in particleswarm (line 152)
[x,fval,exitFlag,output] =
pswcore(objFcn,nvars,lbRow,ubRow,output,options);
Error in question_7b (line 5)
[x,fval,exitflag] = particleswarm(fun,n)
Caused by:
Not enough input arguments.
  1 件のコメント
Adam
Adam 2016 年 12 月 16 日
Just for info, that is only 1 error being reported, it is just giving you the callstack that leads to the error.

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

採用された回答

José-Luis
José-Luis 2016 年 12 月 16 日
T, and consequently fun expect two arguments. From the documentation of particle swarm:
"Objective function, specified as a function handle. The objective function should accept a row vector of length nvars and return a scalar value."
The function you pass should only have one argument...
  3 件のコメント
Maurice Moh
Maurice Moh 2016 年 12 月 16 日
Hi thanks so much for the tip i have implemented the changes and now i have received another error and
No public property HybridFCn exists for class
optim.options.Particleswarm.
Error in question_7b (line 11)
options.HybridFCn= @fmincon;
Appreciate the help in advance.
Alan Weiss
Alan Weiss 2016 年 12 月 16 日
Looks like a typo to me. The correct capitalization is HybridFcn, not HybridFCn.
Alan Weiss
MATLAB mathematical toolbox documentation

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

その他の回答 (1 件)

nazli melek
nazli melek 2017 年 12 月 28 日
Thanks José-Luis for your help. I faced the same problem and it is solved now

カテゴリ

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