Passing values to PSO options
3 次查看(过去 30 天)
显示 更早的评论
Hi there,
How may I pass swarmSize variable value to opts in the PSO? I have included the particular code piece below. When I do as below, it seems 'swarmSize' doesn't get passed to opts. Many thanks in advance.
swarmSize = 10; %doesn't pass to opts??
opts = optimoptions(@particleswarm,...
'Display','iter',...
'PlotFcn','pswplotbestf',...
'SwarmSize', swarmSize,...
'MaxIterations',20,...
'UseVectorized',true,...
'UseParallel',false);
% Cost {function-handle}
cost = @(particlePosition) costfn(iStep,nn,swarmSize,nvars,particlePosition);
% PSO Call
[particlePosition,particleCost,exitflag,output] = particleswarm(cost,nvars-2,particlelb,particleub,opts);
0 个评论
回答(1 个)
Alan Weiss
2022-8-8
It works for me. Here is a little test script:
fun = @(x)x(1)*exp(-norm(x)^2);
lb = [-5,-5];
ub = -lb;
opts = optimoptions('particleswarm',"SwarmSize",20,"Display","iter");
[sol,fv,ef,output] = particleswarm(fun,2,lb,ub,opts)
You see that there were 38 iterations and 780 function evaluations. This corresponds to a swarm size of 20: 20*38 = 760, plus 20 initial evaluations = 780 function evaluations.
Alan Weiss
MATLAB mathematical toolbox documentation
3 个评论
Alan Weiss
2022-8-8
Try the code your way:
rng default
fun = @(x)x(1)*exp(-norm(x)^2);
lb = [-5,-5];
ub = -lb;
swarmSize = 20
opts = optimoptions('particleswarm',"SwarmSize",swarmSize,"Display","iter");
[sol,fv,ef,output] = particleswarm(fun,2,lb,ub,opts)
This time (with rng default for reproducibility) I get 45 iterations times 20 particles = 900 fevals, +20 initial fevals = 920 fevals, as shown. The options get passed as you wanted.
Alan Weiss
MATLAB mathematical toolbox documentation
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle Swarm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!