# 依赖: pymoo import numpy as np from pymoo.algorithms.moo.nsga2 import NSGA2 from pymoo.core.problem import ElementwiseProblem from pymoo.optimize import minimize class BiObjective(ElementwiseProblem): def __init__(self): super().__init__(n_var=2, n_obj=2, xl=np.array([0.0, 0.0]), xu=np.array([1.0, 1.0])) def _evaluate(self, x, out, *args, **kwargs): out['F'] = [x[0] ** 2 + x[1] ** 2, (x[0] - 1.0) ** 2 + (x[1] - 1.0) ** 2] res = minimize(BiObjective(), NSGA2(pop_size=60), ('n_gen', 120), seed=1, verbose=False) print('Pareto 前沿前 5 个点:\n', np.round(res.F[:5], 4)) # 要报告前沿而不是单个解