In [1]:
Copied!
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.optimize import minimize
import paretobench as pb
from paretobench.ext.pymoo import PymooProblemWrapper
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.optimize import minimize
import paretobench as pb
from paretobench.ext.pymoo import PymooProblemWrapper
Using ParetoBench Problems in Pymoo¶
PymooProblemWrapper subclasses pymoo's Problem class and evaluates the ParetoBench problem using vectorization. Data such as number of objectives, variables and their bounds, etc are passed through from the ParetoBench problem.
In [2]:
Copied!
# Wrap one of ParetoBench's problems for use in pymoo
prob = PymooProblemWrapper.from_line_fmt("ZDT3")
prob
# Wrap one of ParetoBench's problems for use in pymoo
prob = PymooProblemWrapper.from_line_fmt("ZDT3")
prob
Out[2]:
PymooProblemWrapper(ZDT3 (n=30))
The wrapper may be passed directly to pymoo's algorithms. We will run a short optimization with pymoo's NSGA-II implementation.
In [3]:
Copied!
# Run a short optimization
res = minimize(prob, NSGA2(pop_size=32), ("n_gen", 64), seed=1, verbose=False)
print(f"Number of nondominated solutions: {len(res.X)}")
# Run a short optimization
res = minimize(prob, NSGA2(pop_size=32), ("n_gen", 64), seed=1, verbose=False)
print(f"Number of nondominated solutions: {len(res.X)}")
Number of nondominated solutions: 32
In [4]:
Copied!
# Load the result into a ParetoBench Population and plot it against the true Pareto front
pop = pb.Population(x=res.X, f=res.F)
pop.plot_obj_scatter(problem="ZDT3")
# Load the result into a ParetoBench Population and plot it against the true Pareto front
pop = pb.Population(x=res.X, f=res.F)
pop.plot_obj_scatter(problem="ZDT3")
Out[4]:
(<Figure size 640x480 with 1 Axes>, <Axes: xlabel='$f_1$', ylabel='$f_2$'>)