带约束的最小化方程组(scipy.optimize.minimize) [英] Minimize System of Equations with constraints (scipy.optimize.minimize)
本文介绍了带约束的最小化方程组(scipy.optimize.minimize)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码:
import numpy as np
from scipy.optimize import minimize
def eq( p ):
s1,s2,s3 = p
f1 = 1.1**3 / s1*1.1**1+s2*1.1**2+s3*1.1**3
f2 = 0.9**1 / s1*0.9**1+s2*0.9**2+s3*0.9**3
return (f1, f2)
bnds = ( (0, None), (0, None), (0, None) )
cons = ( { 'type' : 'ineq', 'fun': lambda p: p[0]+[p1]+[p2] - 1} )
minimize( eq, (0.3,0.3,0.3), bounds=bnds, constraints=cons )
引发错误
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
我想最小化f1
和f2
,以使s_t > 0
和sum s_t <= 1
对于t = 1、2、3.
I want to minimize f1
and f2
such that the s_t > 0
and sum s_t <= 1
, for t = 1, 2, 3.
推荐答案
minimize( eq, (0.3,0.3,0.3), bounds=bnds, constraints=cons )
第二个参数应该是一个ndarray而不是一个元组. args元组在初始猜测(x0)之后出现.
The second argument should be an ndarray not a tuple. The args tuple comes after the initial guess (x0).
http://docs. scipy.org/doc/scipy-0.17.0/reference/generation/scipy.optimize.minimize.html
这篇关于带约束的最小化方程组(scipy.optimize.minimize)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文