skills/pymoo/references/parallelization.md
Reference for parallel evaluation of expensive ElementwiseProblem instances.
Use parallelization when _evaluate is the bottleneck (simulations, ML inference, external solvers). Pymoo evaluates one solution per _evaluate call for ElementwiseProblem; pass a runner to evaluate multiple solutions concurrently.
Requirements:
ElementwiseProblem (not vectorized Problem)elementwise_evaluation=True (default for ElementwiseProblem)elementwise_runner to the problem constructorUses Python's multiprocessing.Pool.starmap interface via StarmapParallelization.
import multiprocessing
from multiprocessing.pool import ThreadPool
from pymoo.algorithms.soo.nonconvex.ga import GA
from pymoo.core.problem import ElementwiseProblem
from pymoo.optimize import minimize
from pymoo.parallelization.starmap import StarmapParallelization
class MyProblem(ElementwiseProblem):
def __init__(self, elementwise_runner=None, **kwargs):
super().__init__(
n_var=10, n_obj=1, xl=-5, xu=5,
elementwise_runner=elementwise_runner,
**kwargs,
)
def _evaluate(self, x, out, *args, **kwargs):
out["F"] = (x ** 2).sum()
# Thread pool (shared memory; good for I/O-bound evaluation)
n_threads = 4
pool = ThreadPool(n_threads)
runner = StarmapParallelization(pool.starmap)
problem = MyProblem(elementwise_runner=runner)
result = minimize(problem, GA(), ("n_gen", 50), seed=1)
pool.close()
# Process pool (separate memory; good for CPU-bound evaluation)
n_processes = 4
pool = multiprocessing.Pool(n_processes)
runner = StarmapParallelization(pool.starmap)
problem = MyProblem(elementwise_runner=runner)
result = minimize(problem, GA(), ("n_gen", 50), seed=1)
pool.close()
Alternative using the joblib library:
from joblib import Parallel, delayed
from pymoo.parallelization.joblib import JoblibParallelization
runner = JoblibParallelization(lambda func, X: Parallel(n_jobs=4)(delayed(func)(x) for x in X))
problem = MyProblem(elementwise_runner=runner)
Install joblib if needed: uv pip install joblib
minimize() completesProblem subclass evaluating batches), implement batching inside _evaluate insteadDocumentation: https://pymoo.org/parallelization/starmap.html