Skip to main content
 首页 » 编程设计

python之传递给函数的未知类型的约束

2024年11月24日10sky-heaven

您好,我正在做一个最小化问题,我想尝试使用 'trust-constr' 方法,使用 scipy 函数 optimize.minimize,将其与 ' SLSQP 的结果。不幸的是,我不知道为什么当我将我的方法设置为 'trust-constr' 时,它告诉我:

ValueError:传递了未知类型的约束。

代码如下:

con1 = {'type':'eq', 'fun': eq2} 
con2 =  {'type':'ineq', 'fun' : ieq_bck2} 
con3 =  {'type':'ineq', 'fun' : ineq_rb2} 
con4 =  {'type':'ineq', 'fun' : ls2} 
 
con = ([con1, con2, con3, con4]) 
 
scipy.optimize.minimize(fitness2, opt, method = 'trust-constr', jac =  
'cs', hess = h1, constraints = con) 

知道如何解决吗?

请您参考如下方法:

根据 the Scipy documentation ,您的变量 con 必须是字典。还有一个例子,con必须是这样的:

cons = ({'type': 'ineq', 'fun': lambda x:  x[0] - 2 * x[1] + 2}, 
        {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6}, 
        {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2}) 

所以在你的情况下,这样做:

con = (con1, con2, con3, con4)