Passing values to PSO options

조회 수: 4 (최근 30일)
Buddhi Wimarshana
Buddhi Wimarshana 2022년 8월 8일
댓글: Buddhi Wimarshana 2022년 8월 8일
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);

답변 (1개)

Alan Weiss
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)
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
Alan Weiss
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
swarmSize = 20
opts = optimoptions('particleswarm',"SwarmSize",swarmSize,"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.00953 0.005882 0 1 40 -0.02456 -0.001228 0 2 60 -0.05993 0.001832 0 3 80 -0.05993 -0.0008519 1 4 100 -0.08234 -0.005554 0 5 120 -0.2747 0.02441 0 6 140 -0.2747 0.02433 1 7 160 -0.3332 -0.01632 0 8 180 -0.3332 0.01548 1 9 200 -0.3332 -0.00039 2 10 220 -0.3332 0.00387 3 11 240 -0.3332 0.01008 4 12 260 -0.3332 -0.01568 5 13 280 -0.3332 0.005135 6 14 300 -0.3332 -0.0003027 7 15 320 -0.3332 -0.006723 8 16 340 -0.3797 -0.0914 0 17 360 -0.4253 -0.1851 0 18 380 -0.4263 -0.2863 0 19 400 -0.4272 -0.3619 0 20 420 -0.4285 -0.417 0 21 440 -0.4287 -0.4215 0 22 460 -0.4288 -0.4272 0 23 480 -0.4289 -0.4277 0 24 500 -0.4289 -0.4282 1 25 520 -0.4289 -0.4281 0 26 540 -0.4289 -0.4276 0 27 560 -0.4289 -0.4279 1 28 580 -0.4289 -0.4275 2 29 600 -0.4289 -0.4265 3 30 620 -0.4289 -0.427 4 Best Mean Stall Iteration f-count f(x) f(x) Iterations 31 640 -0.4289 -0.4237 5 32 660 -0.4289 -0.4249 6 33 680 -0.4289 -0.4257 7 34 700 -0.4289 -0.4283 8 35 720 -0.4289 -0.4286 0 36 740 -0.4289 -0.4284 0 37 760 -0.4289 -0.4288 0 38 780 -0.4289 -0.4289 1 39 800 -0.4289 -0.4289 0 40 820 -0.4289 -0.4289 1 41 840 -0.4289 -0.4289 0 42 860 -0.4289 -0.4289 0 43 880 -0.4289 -0.4289 0 44 900 -0.4289 -0.4289 0 45 920 -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: 45 funccount: 920 message: 'Optimization ended: relative change in the objective value ↵over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.' hybridflag: []
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
Buddhi Wimarshana
Buddhi Wimarshana 2022년 8월 8일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Particle Swarm에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by