SciPy忽略了不等式约束

2024-09-27 21:33:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用scipy(SLSQP方法)进行结构优化(scipy版本1.6.1)。 对于每个部分,我有13个设计变量,但只有一个用于成本函数(mass)——其他变量用于满足某些不等式约束。因为一开始我不知道要优化多少部分(PID),所以我使用迭代器生成优化

我将一些不等式约束定义为平滑函数,因为它们描述可行域,但也将一些不等式约束定义为使用泰勒近似的numpy数组:

def generate_ineq_list(PID_list,x,crit_list):
    ineq_list = []

    for i in np.arange(len(PID_list)):
        f01 = -2*x[i*13]**2 + x[i*13+2] + 1
        ...
        f25 = 4*(x[i*13+9] + 1)*(x[i*13+1] + 1) - (x[i*13+1] + 1)**4 - 3*(x[i*13+5])**2

        ineq_list.append([f01,...,f25])

        V1A = x[i*13]
        V3A = x[i*13+2]
        V1D = x[i*13+8]
        V3D = x[i*13+10]
        H   = x[i*13+12]

        # Now iterating over all defined criteria (crit), parts (PID) and variables to 
        # set up a Taylor approximation.
        # The values with _0 denote the last starting point resp. the point where 
        # the gradients were calculated using the finite difference method. These values 
        # are taken from a pandas dataframe which is dispensed here for the 
        # sake of visibility

        SF = -2.0 + results_old[crit]['sf'].values + \
                gradient[PID]['V1A'][crit]['sf'] * (V1A - V1A_0) + \
                gradient[PID]['V3A'][crit]['sf'] * (V3A - V3A_0) + \
                gradient[PID]['V1D'][crit]['sf'] * (V1D - V1D_0) + \
                gradient[PID]['V3D'][crit]['sf'] * (V3D - V3D_0) + \
                gradient[PID]['H'][crit]['sf'] * (H - H_0)

        ineq_list.append(list(SF))

    ineq_list_flattended = list(itertools.chain(*ineq_list))
    return ineq_list_flattened

f01的结果是一个浮点,而SF的结果是一个numpy数组(它变成了一个列表)。所以我所做的是建立一个列表,结果存储在其中(列表列表),然后最终扁平化为一个只有浮点数的列表。然后将此列表作为不等式约束返回给优化器:

ineq_functions = lambda x: np.array(generate_ineq_list(self.PID_list,x,crit_list))
ineq_cons = {'type' : 'ineq',
             'fun' : ineq_functions,
            }

现在,当我使用

res = minimize(cost_function_f_,
               x0,
               method='SLSQP',
               constraints = [ineq_cons,
                              eq_cons],
               options = {'ftol' : 1e-9,
                          'disp' : True,
                          'iprint':2,
                           },
               bounds = bounds,
               )

我可以观察到不等式约束的前25个方程(f01-f25)在其范围内,但SF的值无效,例如它们的值为-0.9

有人知道我做错了什么,不考虑,误解吗

如有任何帮助和/或想法,将不胜感激。提前谢谢

这是我的第一篇帖子,所以我尽量让每件事都尽可能的简单,我希望我做得对:)


Tags: the列表sfpidlistgradientcritineq

热门问题