I found how one can pass *args
into the main function of scipy.minimize.optimize
, see this question.
But how do I pass constants and variables into constraints and boundaries?
P
is a variable vector, P0
is the initial solution vector, f(P)
is the function to minimize, the rest are constant data. Will it work like this?
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)