Passing values to PSO options
조회 수: 4 (최근 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!