Contenuto principale

Parallel Computing with ga, gamultiobj, particleswarm, and paretosearch

R2026b

Enabling Parallel Computing

ga, gamultiobj, particleswarm, and paretosearch can automatically distribute the evaluation of objective and constraint functions to multiple processors. These solvers evaluate all individuals in a population (or poll points) in parallel once per iteration.

To enable parallel computing, set options using optimoptions:

% ga
options = optimoptions("ga",UseParallel="auto",UseVectorized=false);

% gamultiobj
options = optimoptions("gamultiobj",UseParallel="auto",UseVectorized=false);

% particleswarm
options = optimoptions("particleswarm",UseParallel="auto",UseVectorized=false);

% paretosearch
options = optimoptions("paretosearch",UseParallel="auto");

The key requirements are:

  • UseParallel is "auto" or "on".

  • UseVectorized is false (default) for ga, gamultiobj, and particleswarm. (paretosearch does not have a UseVectorized option.)

When UseVectorized is true, the solver evaluates the entire population with one function call rather than distributing evaluations, so it does not use parfor.

Parallel Hybrid Functions

ga, gamultiobj, and particleswarm can call hybrid functions that run after they finish. To have the hybrid function take advantage of parallel computing, set its options separately.

If your hybrid function is patternsearch:

hybridopts = optimoptions("patternsearch",UseParallel="auto",...
    UseCompletePoll=true);
options = optimoptions("ga",UseParallel="auto",...
    HybridFcn={@patternsearch,hybridopts});

If your hybrid function is fmincon:

hybridopts = optimoptions(@fmincon,UseParallel="auto",...
    Algorithm="interior-point");
options = optimoptions("ga",UseParallel="auto",...
    HybridFcn={@fmincon,hybridopts});

For simulannealbnd, which does not run in parallel directly, you can still use a parallel hybrid function:

hybridopts = optimoptions("patternsearch",UseParallel="auto",...
    UseCompletePoll=true);
options = optimoptions(@simulannealbnd,...
    HybridFcn={@patternsearch,hybridopts});

gamultiobj Hybrid Function

gamultiobj allows only one hybrid function, fgoalattain. Each individual in the final Pareto frontier becomes the starting point for an optimization using fgoalattain. These optimizations run in parallel:

fgoalopts = optimoptions(@fgoalattain,UseParallel="auto");
gaoptions = optimoptions("gamultiobj",HybridFcn={@fgoalattain,fgoalopts});

gamultiobj calls fgoalattain using a parfor loop, so fgoalattain does not estimate gradients in parallel when used as a hybrid function.

Limitations

  • No nested parfor loops. parfor does not work in parallel when called from within another parfor loop. If your objective or constraint functions use parfor, the solver's parallel evaluation takes precedence and your inner parfor runs serially. paretosearch internally uses parfeval (Parallel Computing Toolbox), not parfor, but the same limitation applies.

  • Nonreproducible random numbers. For ga and gamultiobj, parallel population generation gives nonreproducible results because random number sequences on parallel workers are not controllable.

  • Occasional serial evaluation. Even when running in parallel, these solvers occasionally call the objective and constraint functions serially on the host machine. Ensure that your functions have no assumptions about whether they are evaluated in serial or parallel.

For information on factors that affect the speed and results of parallel computations, see Improving Performance with Parallel Computing.

See Also

Topics