问题
The following code:
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 )
throws the error
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
I want to minimize f1
and f2
such that the s_t > 0
and sum s_t <= 1
, for t = 1, 2, 3.
回答1:
minimize( eq, (0.3,0.3,0.3), bounds=bnds, constraints=cons )
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/generated/scipy.optimize.minimize.html
回答2:
The second parameter should be of type ndarray, try using
minimize( eq, np.ndarray([0.3,0.3,0.3]), bounds=bnds, constraints=cons )
来源:https://stackoverflow.com/questions/36108921/minimize-system-of-equations-with-constraints-scipy-optimize-minimize