Passing values to PSO options

3 views (last 30 days)
Buddhi Wimarshana
Buddhi Wimarshana on 8 Aug 2022
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);

Answers (1)

Alan Weiss
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)
Best Mean Stall Iteration f-count f(x) f(x) Iterations 0 20 -0.02168 0.008792 0 1 40 -0.4151 -0.02415 0 2 60 -0.4151 -2.352e-05 1 3 80 -0.4151 -0.002245 2 4 100 -0.4151 -0.000726 3 5 120 -0.4151 0.015 4 6 140 -0.4151 0.004306 5 7 160 -0.4151 0.0146 6 8 180 -0.4151 -0.04039 7 9 200 -0.4151 -0.02363 8 10 220 -0.4151 -0.1728 9 11 240 -0.4177 -0.3032 0 12 260 -0.4177 -0.3321 1 13 280 -0.4206 -0.3799 0 14 300 -0.4288 -0.4074 0 15 320 -0.4288 -0.4167 1 16 340 -0.4288 -0.4254 2 17 360 -0.4289 -0.4278 0 18 380 -0.4289 -0.4283 1 19 400 -0.4289 -0.4286 0 20 420 -0.4289 -0.4288 1 21 440 -0.4289 -0.4289 2 22 460 -0.4289 -0.4289 3 23 480 -0.4289 -0.4289 0 24 500 -0.4289 -0.4289 0 25 520 -0.4289 -0.4289 1 26 540 -0.4289 -0.4289 0 27 560 -0.4289 -0.4289 0 28 580 -0.4289 -0.4289 0 29 600 -0.4289 -0.4289 1 30 620 -0.4289 -0.4289 2 Best Mean Stall Iteration f-count f(x) f(x) Iterations 31 640 -0.4289 -0.4289 0 32 660 -0.4289 -0.4289 0 33 680 -0.4289 -0.4289 0 34 700 -0.4289 -0.4289 0 35 720 -0.4289 -0.4289 1 36 740 -0.4289 -0.4289 0 37 760 -0.4289 -0.4289 0 38 780 -0.4289 -0.4289 0 Optimization ended: relative change in the objective value over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.
sol = 1×2
-0.7071 0.0000
fv = -0.4289
ef = 1
output = struct with fields:
rngstate: [1×1 struct] iterations: 38 funccount: 780 message: 'Optimization ended: relative change in the objective value ↵over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.' hybridflag: []
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
Buddhi Wimarshana
Buddhi Wimarshana on 8 Aug 2022
Oh silly me!!! have done a silly mistake, it works great...thanks Alan so much...really shouldn't have asked here, but didn't notice the mistake for hours, but I do now

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by