pymoo: Multi-objective Optimization in Python
https://pymoo.org/installation.html#installation
https://www.pymoo.org/algorithms/nsga2.html
- 安装pymoo
- 定义问题
N个变量;M个目标函数;J个不等式,K个等式约束。eg:
Next, the derived problem formulation is implemented in Python. Each optimization problem in pymoo has to inherit from the Problem class. First, by calling the super() function the problem properties such as the number of variables (n_var), objectives (n_obj) and constraints (n_constr) are initialized. Furthermore, lower (xl) and upper variables boundaries (xu) are supplied as a NumPy array. Additionally, the evaluation function _evaluate needs to be overwritten from the superclass. The method takes a two-dimensional NumPy array x with n rows and m columns as an input. Each row represents an individual and each column an optimization variable. After doing the necessary calculations, the objective values are added to the dictionary out with the key F and the constraints with key G.
接下来,在python中实现导出的问题方程。在pymoo中,每个优化问题都要继承自Problem类。首先,通过调用super()函数,初始化问题的属性,如说变量数n_var,目标数n_obj和约束数n_constr。更进一步,以Numpy数组的格式提供两个边界值xl,xu(最小&最大)。此外,度量函数_evaluate需要被重写。该方法需要二维n行m列的Numpy数组x作为输入。每一行表示一个个体,每一列是一个最优变量。做完所需计算后,目标值需要被加入字典out,关键字为F;约束值同样地加入out字典,关键字为G。
- 算法初始化
- 开始优化
example:
from pymoo.algorithms.nsga2 import NSGA2 from pymoo.factory import get_problem from pymoo.optimize import minimize from pymoo.visualization.scatter import Scatter problem = get_problem("zdt1") algorithm = NSGA2(pop_size=100) res = minimize(problem, algorithm, ('n_gen', 200), seed=1, verbose=False) plot = Scatter() plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7) plot.add(res.F, color="red") plot.show()