我发现如何将*args
传递到scipy.minimize.optimize
的主要功能中,请参阅this question。
但是如何将常量和变量传递到约束和边界中?
P
是变量向量,P0
是初始解向量,f(P)
是最小化函数,其余都是常数数据。会这样吗?
def constraint_1(P, args):
M_goal, other_args = args
M = calc_M_from_P(P, args) # calculate M from P
return M-M_goal
# boundary is simply (P0[i]-500, P0[i]+500)
def get_boundaries(P0):
list_bnds = []
for i in range(len(P0)):
list_bnds.append((P0[i]-500.0, P0[i]+500.0))
return tuple(list_bnds)
func = objective(P0, args)
bnds = get_boundaries(P0)
con1 = {'type': 'ineq', 'fun': constraint_1}
sol = minimize(func, P0, args = args, method='SLSQP', bound=bnds, constraints = con1)