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