如何在群体优化的每次迭代中显示建立的最佳值x

问题描述 投票:0回答:1

如何使群体优化在每次迭代中显示最佳解决方案。

例如此代码:

fun = @(x)x(1)*exp(-norm(x)^2);
lb = [-10,-15];
ub = [15,20];
rng default  % For reproducibility
nvars = 2;
x = particleswarm(fun,nvars,lb,ub)

最后显示了解决方案,但我需要在每次迭代中查看解决方案。我怎么找到这个。

matlab function optimization particles swarm
1个回答
0
投票

根据documentation,您需要使用optimoptions并将'Display'设置为'iter'

options = optimoptions('particleswarm', 'Display', 'iter');

完整代码示例:

fun = @(x)x(1)*exp(-norm(x)^2);
lb = [-10,-15];
ub = [15,20];
options = optimoptions('particleswarm', 'Display', 'iter');
rng default  % For reproducibility
nvars = 2;
x = particleswarm(fun,nvars,lb,ub,options);

仅作记录:我以前从未听说过粒子群优化,但这似乎很有趣。


执行输出:

                                 Best            Mean     Stall
Iteration     f-count            f(x)            f(x)    Iterations
    0              20      -7.493e-57       5.072e-08        0
    1              40      -0.0002027      -1.014e-05        0
    2              60      -0.0002027      -5.024e-11        1
    3              80      -0.0002027      -6.219e-18        2
    4             100      -0.0002027       3.866e-05        3
    5             120        -0.02687        0.001035        0
    6             140        -0.02687      -0.0001022        1
    7             160        -0.02687      -1.011e-06        2
    8             180        -0.02687        2.37e-05        3
    9             200        -0.02687          0.0138        4
   10             220        -0.02687        0.003792        5
   11             240         -0.4043        -0.03831        0
   12             260         -0.4043        -0.01652        1
   13             280         -0.4043         -0.0745        2
   14             300         -0.4043         -0.1615        3
   15             320         -0.4172         -0.2733        0
   16             340         -0.4231         -0.3282        0
   17             360         -0.4231         -0.3558        1
   18             380         -0.4288         -0.3999        0
   19             400         -0.4288         -0.4219        1
   20             420         -0.4288         -0.4266        0
   21             440         -0.4289         -0.4283        0
   22             460         -0.4289         -0.4286        0
   23             480         -0.4289         -0.4288        0
   24             500         -0.4289         -0.4288        1
   25             520         -0.4289         -0.4289        0
   26             540         -0.4289         -0.4289        0
   27             560         -0.4289         -0.4289        1
   28             580         -0.4289         -0.4289        0
   29             600         -0.4289         -0.4289        1
   30             620         -0.4289         -0.4289        0
   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        0
   36             740         -0.4289         -0.4289        1
   37             760         -0.4289         -0.4289        2
   38             780         -0.4289         -0.4289        3
   39             800         -0.4289         -0.4289        4
   40             820         -0.4289         -0.4289        5
   41             840         -0.4289         -0.4289        6
   42             860         -0.4289         -0.4289        7
© www.soinside.com 2019 - 2024. All rights reserved.