pymoo: Multi-Objective Optimization in Python
Pymoo is an open-source Python library for multi-objective optimization. It provides state-of-the-art single- and multi-objective optimization algorithms, along with features for visualization and decision-making. Currently at version 0.6.1.6, the library maintains an active development cadence with regular updates and bug fixes.
Warnings
- breaking The module organization was entirely changed in version 0.5.0. Many classes, especially algorithms, moved to nested submodules (e.g., `pymoo.algorithms.moo.nsga2.NSGA2`).
- breaking Factory methods (`get_algorithm`, `get_problem`, `get_mutation`, etc.) were deprecated or deactivated in version 0.6.0 to improve clarity and reduce maintenance overhead.
- breaking The termination criterion interface changed in version 0.6.0. Custom termination objects now need to return a floating-point number (0 for continue, 1 for terminate) instead of a boolean.
- breaking Versions prior to 0.6.1.3 are incompatible with NumPy 2.0.0 due to changes in how `autograd` interacts with NumPy's internal structure.
- gotcha Pymoo offers three ways to define an optimization problem: `Problem` (vectorized evaluation), `ElementwiseProblem` (single-solution evaluation), and `FunctionalProblem` (using functions). Choosing the wrong type can impact performance and parallelization.
Install
-
pip install -U pymoo -
pip install -U pymoo[visualization] -
pip install -U pymoo[parallelization]
Imports
- NSGA2
from pymoo.algorithms.moo.nsga2 import NSGA2
- get_problem
from pymoo.problems import get_problem
- minimize
from pymoo.optimize import minimize
- Scatter
from pymoo.visualization.scatter import Scatter
Quickstart
import numpy as np
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.problem import Problem
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter
# Define your custom problem as an object
class MyProblem(Problem):
def __init__(self):
super().__init__(n_var=2, n_obj=2, n_constr=2, xl=np.array([-2.0, -2.0]), xu=np.array([2.0, 2.0]))
def _evaluate(self, X, out, *args, **kwargs):
f1 = X[:, 0]**2 + X[:, 1]**2
f2 = (X[:, 0]-1)**2 + X[:, 1]**2
g1 = 2 * (X[:, 0]-0.1) * (X[:, 0]-0.9)
g2 = 20 * (X[:, 0]-0.4) * (X[:, 0]-0.6)
out["F"] = np.column_stack([f1, f2])
out["G"] = np.column_stack([g1, g2])
# Instantiate the problem
problem = MyProblem()
# Choose an algorithm
algorithm = NSGA2(pop_size=100)
# Define the termination criterion
termination = ('n_gen', 200)
# Optimize
res = minimize(problem, algorithm, termination, seed=1, verbose=False)
# Plot the results
plot = Scatter()
plot.add(res.F, color="red")
plot.show()