numpy 使用pymoo实现多目标优化问题

e5nqia27  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在尝试使用pymoo编写多目标优化问题的目标函数和约束。问题是我无法将数学函数转换为Python。
对于给定的解

,目标函数之一是:



我已经搜索了pymoo的例子(复杂的例子),但我没有得到太多的帮助。
先谢了。

g6ll5ycj

g6ll5ycj1#

如果我没有弄错的话,您正在尝试实现这个paper
我现在给你一个关于如何实现你在问题中提到的目标的粗略纲要。您可以尝试以类似的方式实现其他两个目标。要实现第三个目标的最大化,只需取目标的负数并传递到minimize函数。

PS我使用python 3.8.10pymoo 0.6.0来编写此示例。这是为了说明如何在pymoo中实现它。为了获得精确/正确的解决方案,您可能需要考虑数据并实现本文中提到的其他目标。

from pymoo.core.problem import Problem
from pymoo.operators.crossover.sbx import SBX
from pymoo.operators.mutation.pm import PM
from pymoo.operators.repair.rounding import RoundingRepair
from pymoo.operators.sampling.rnd import IntegerRandomSampling
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.optimize import minimize
import numpy as np

C = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]])
PCI = np.array([[2,1,3], [4,6,5], [9,7,8], [12,10,11]])
W = np.array([2,5,7])
L = np.array([3,5,4])
T = 20

class PavementProblem(Problem):
    def __init__(self, C, PCI, W, L, T, PCImin,  **kwargs):
        n_var = C.shape[0]*C.shape[1]
        # n_var, n_obj, n_ieq_constr, xl, xu, vtype,
        self.C = C
        self.PCI = PCI
        self.PCImin = PCImin
        self.W = W
        self.L = L
        n_ieq = PCI.shape[0]*PCI.shape[1]
        super().__init__(n_var=n_var, n_obj=3, n_ieq_constr=n_ieq, xl=1, xu=T, vtype=int, **kwargs)

    def _evaluate(self, x, out, *args, **kwargs):
        x2d = x.reshape(self.C.shape[0], self.C.shape[1]) # some transformation to convert linear x to x2d
        obj_list = []
        Cx = np.multiply(self.C, x2d)
        # Objective mentioned in the question by OP
        obj1 = np.einsum('ij,j->', Cx, self.W*self.L)
        # Create other objectives and append them to obj_list
        obj_list.append(obj1)
        out["F"] = np.column_stack(obj_list)
        pci = self.PCImin - self.PCI # implement greater-than-equal-to in pymoo
        # flattened the 2d array so that each element will act as a constraint.
        out["G"] = np.column_stack(pci.flatten())
        
problem  = PavementProblem(C, PCI, W, L, T, 8)
algorithm = NSGA2(pop_size=200, sampling=IntegerRandomSampling(),
            crossover=SBX(prob=1.0, eta=3.0, vtype=float, repair=RoundingRepair()),
            mutation=PM(prob=1.0, eta=3.0, vtype=float, repair=RoundingRepair()), eliminate_duplicates=True)
res = minimize(problem, algorithm, ('n_gen', 200), seed=1, verbose=False, return_least_infeasible=True)

字符串

相关问题