pymoo: Multi-Objective Optimization in Python

0.6.1.6 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart defines a custom constrained multi-objective optimization problem, initializes the NSGA-II algorithm, sets a termination criterion based on the number of generations, runs the optimization, and visualizes the resulting Pareto front. Note that custom problems should now inherit from `pymoo.core.problem.Problem` or `ElementwiseProblem`.

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()

view raw JSON →