Passing values to PSO options
4 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Alan Weiss
on 8 Aug 2022
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 Comments
Alan Weiss
on 8 Aug 2022
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
See Also
Categories
Find more on Particle Swarm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!