1

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)
xdze2
  • 3,986
  • 2
  • 12
  • 29
DDR
  • 459
  • 5
  • 15
  • 1
    Your link is broken. For obtaining the right address of a StackOverflow question or answer, pleas click on the **share** under it. – MarianD Aug 23 '18 at 14:21
  • 1
    "Constraints for COBYLA, SLSQP are defined as a list of dictionaries [...] with fields: [...] args : sequence, optional". What part of this is unclear? –  Aug 23 '18 at 14:29

0 Answers0