| name | pymoo |
| description | Multi-objective optimization using NSGA-II. Define custom problems, find Pareto front, minimize conflicting objectives. Use when solving optimization problems with 2-3 objectives, Pareto front analysis, or NSGA-II algorithm configuration. |
Pymoo - Multi-Objective Optimization with NSGA-II
Overview
Pymoo is a Python framework for optimization. This skill covers using NSGA-II for bi-objective optimization and defining custom multi-objective problems.
Core Workflow: Multi-Objective Optimization with NSGA-II
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.problem import ElementwiseProblem
from pymoo.optimize import minimize
import numpy as np
class MyProblem(ElementwiseProblem):
def __init__(self):
super().__init__(
n_var=1,
n_obj=2,
n_ieq_constr=0,
n_eq_constr=0,
xl=np.array([-10]),
xu=np.array([10])
)
def _evaluate(self, x, out, **kwargs):
f1 = (x[0] - 2)**2
f2 = (x[0] + 2)**2
out["F"] = [f1, f2]
problem = MyProblem()
algorithm = NSGA2(
pop_size=50,
n_offsprings=50,
eliminate_duplicates=True
)
result = minimize(
problem,
algorithm,
('n_gen', 3),
seed=1,
verbose=False
)
solutions = []
for x, f in zip(result.X, result.F):
solutions.append(f"x1={x[0]:.3f},{f[0]:.6f},{f[1]:.6f}")
solutions.sort(key=lambda s: float(s.split("=")[1].split(",")[0]))
with open("/root/output.txt", "w") as f:
for sol in solutions:
f.write(sol + "\n")
Defining Custom Problems
Extend ElementwiseProblem class:
class CustomProblem(ElementwiseProblem):
def __init__(self):
super().__init__(
n_var=2,
n_obj=2,
n_ieq_constr=0,
n_eq_constr=0,
xl=np.array([x1_min, x2_min]),
xu=np.array([x1_max, x2_max])
)
def _evaluate(self, x, out, **kwargs):
out["F"] = [f1(x), f2(x)]
NSGA-II Algorithm Configuration
from pymoo.algorithms.moo.nsga2 import NSGA2
algorithm = NSGA2(
pop_size=100,
n_offsprings=None,
sampling=None,
selection=None,
crossover=None,
mutation=None,
eliminate_duplicates=True,
n_offsprings=10
)
Key parameters:
pop_size: Number of individuals in population
n_offsprings: Number of offspring created each generation
eliminate_duplicates: Whether to eliminate duplicate individuals (default: True)
Running Optimization
from pymoo.optimize import minimize
result = minimize(
problem,
algorithm,
termination=('n_gen', N),
seed=1,
verbose=False
)
Result object contains:
result.X: Decision variables of all Pareto optimal solutions (Nxvars matrix)
result.F: Objective values of all Pareto optimal solutions (Nxn_obj matrix)
result.algorithm: Algorithm object with history
Output Format
For each Pareto solution, output: x1=f1,f2
Sorted by x1 ascending, x1 rounded to 3 decimal places:
for i in range(len(result.X)):
x1 = result.X[i][0]
f1 = result.F[i][0]
f2 = result.F[i][1]
print(f"x1={x1:.3f},{f1:.6f},{f2:.6f}")
Key Reference
ElementwiseProblem — Base class for custom optimization problems
NSGA2(pop_size=50) — Configure NSGA-II with population size
minimize(problem, algorithm, ('n_gen', N)) — Run optimization for N generations
result.X — Decision variable values
result.F — Objective function values
xl, xu — Lower and upper bounds for variables