0

我有以下一组方程:

x = [x1, x2, x3, x4, x5, x6, x7]
c = [1, 2, 3, 4, 5, 6, 7]
g_cons = [1, 2, 3, 4, 5, 6, 7]
d_cons = [1, 2, 3, 4, 5, 6, 7]
g = Σ(x*g_cons)
d = Σ(x*d_cons)
d_p = d/g*100
e = -0.0038 * d_p * d_p + 0.3501 *d_p – 0.811
ef = (g*(e/100)*365)/55.65
tc = Σ(x*c)

我的目标函数是最小化(ef)和最小化(tc)主题。至 Σx <40 且 xi 界限为 [0,15]

我尝试了下面的代码,但它抛出了一个错误

 super().__init__(n_var=7,
                         n_obj=2,
                         n_constr=1,
                         xl=np.array([0, 0, 0, 0, 0, 0, 0]),
                         xu=np.array([15, 15, 15, 15, 15, 15, 15]))

   def _evaluate(self, x, out, *args, **kwargs):
        c = [1, 2, 3, 4, 5, 6, 7]
        g_cons = [1, 2, 3, 4, 5, 6, 7]
        d_cons = [1, 2, 3, 4, 5, 6, 7]
        f1 = (sum(x*g_cons)*(-0.0038 *(pow(sum(x*g_cons)/sum(x*d_cons)*100,2))+ 
        0.3501*(sum(x*g_cons)/sum(x*d_cons)*100)- 0.811)/100*365)/55.65
        f2 = sum(x*c)

        f1, f2 = f1, f2
        g1 = sum(x)-40

        out["F"] = np.column_stack([f1, f2])
        out["G"] = g1

错误信息:

 Exception: Population Set Attribute Error: Number of values and population size do not match!
4

1 回答 1

0

请注意,评估函数 x 的输入 f 的形状为 (n_pop, n_var) 此函数的输出 out['f'] 的形状必须为 (n_pop, n_obj)。您当前sum在错误的轴上使用了函数,请改正,np.sum(array, axis=1)如下所示

class PProblem(Problem):
    def __init__(self):
        super().__init__(n_var=7,
                         n_obj=2,
                         n_constr=1,
                         xl=np.array([0, 0, 0, 0, 0, 0, 0]),
                         xu=np.array([15, 15, 15, 15, 15, 15, 15]))

    def _evaluate(self, x, out, *args, **kwargs):
        c = [1, 2, 3, 4, 5, 6, 7]
        g_cons = [1, 2, 3, 4, 5, 6, 7]
        d_cons = [1, 2, 3, 4, 5, 6, 7]
        f1 = (np.sum(x*g_cons,axis=1)*(-0.0038 *(pow(np.sum(x*g_cons,axis=1)/np.sum(x*d_cons,axis=1)*100,2))+ 
        0.3501*(np.sum(x*g_cons,axis=1)/np.sum(x*d_cons,axis=1)*100)- 0.811)/100*365)/55.65
        f2 = np.sum(x*c,axis=1)

        f1, f2 = f1, f2
        g1 = np.sum(x,axis=1)-40
        out["F"] = np.column_stack([f1, f2])
        out["G"] = np.column_stack([g1])

问候

于 2021-12-08T10:50:19.670 回答