Main Content

本页采用了机器翻译。点击此处可查看英文原文。

并行 MultiStart

并行 MultiStart 的步骤

如果您有多核处理器或可以访问处理器网络,则可以将 Parallel Computing Toolbox™ 函数与 MultiStart 一起使用。此示例显示如何使用双核处理器并行寻找问题的多个极小值。该问题与 寻找全局或多个局部最小值 中的问题相同。

  1. 编写一个函数文件来计算目标:

    function f = sawtoothxy(x,y)
    [t r] = cart2pol(x,y); % change to polar coordinates
    h = cos(2*t - 1/2)/2 + cos(t) + 2;
    g = (sin(r) - sin(2*r)/2 + sin(3*r)/3 - sin(4*r)/4 + 4) ...
        .*r.^2./(r+1);
    f = g.*h;
    end
  2. 创建问题结构体:

    problem = createOptimProblem('fminunc',...
        'objective',@(x)sawtoothxy(x(1),x(2)),...
        'x0',[100,-50],'options',...
        optimoptions(@fminunc,'Algorithm','quasi-newton'));
  3. 通过运行来验证问题结构体:

    [x,fval] = fminunc(problem)
    x =
        8.4420 -110.2602
    
    fval =
      435.2573
  4. 创建一个 MultiStart 对象,并设置该对象使用并行处理和迭代显示:

    ms = MultiStart('UseParallel',true,'Display','iter');
  5. 设置并行处理:

    parpool
    Starting parpool using the 'local' profile ... connected to 4 workers.
    
    ans = 
    
     Pool with properties: 
    
                Connected: true
               NumWorkers: 4
                  Cluster: local
            AttachedFiles: {}
              IdleTimeout: 30 minute(s) (30 minutes remaining)
              SpmdEnabled: true
  6. 在 50 个起点上运行该问题:

    [x,fval,exitflag,output,manymins] = run(ms,problem,50);
    Running the local solvers in parallel.
    
     Run       Local       Local      Local    Local   First-order
    Index     exitflag      f(x)     # iter   F-count   optimality
       17         2         3953         4        21        0.1626
       16         0         1331        45       201         65.02
       34         0         7271        54       201         520.9
       33         2         8249         4        18         2.968
         ... Many iterations omitted ... 
       47         2         2740         5        21        0.0422
       35         0         8501        48       201         424.8
       50         0         1225        40       201         21.89
    
    MultiStart completed some of the runs from the start points.
    
    17 out of 50 local solver runs converged with a positive 
    local solver exit flag.

    请注意,运行索引看起来是随机的。并行 MultiStart 以不可预测的顺序运行其起点。

    请注意,MultiStart 在输出的第一行中确认了并行处理,其中指出:“并行运行局部求解器。”

  7. 完成后,关闭并行环境:

    delete(gcp)
    Parallel pool using the 'local' profile is shutting down.

有关如何获得该问题的更好解的示例,请参阅 示例:寻找更好的解决方案。您可以将并行处理与该示例中描述的技术一起使用。

通过并行计算加速

MultiStart 运行的结果是随机的。运行的时间也是随机的。尽管如此,下表中仍可见一些明显的趋势。表中的数据来自于在具有两个核心的机器上对每个起点进行的一次运行。

起点并行秒串行秒
503.63.4
1004.95.7
2008.310
5001623
10003146

当仅使用几个起点时,并行计算可能比串行计算慢。随着起点数量的增加,并行计算变得比串行计算更有效率。

有许多因素会影响并行处理的加速(或减速)。有关详细信息,请参阅利用并行计算提高性能

相关主题