5

I am trying to use negative of scipy.optimize.minimize to maximize a function f (a, b, c, d). d is a numpy.array of guess variables.

I am trying to put some bounds on each d. And also a constraint on each d such that (d1 * a1 + d2 * a2 + ... + d3 * a3) < some_Value (a being the other argument to the subject function f).

My problem is how do I define this constraint as an argument to the maximize function.

I could not find any maximize function in the library so we're using the negative of minimize with minimize documentation over here.

Please consider asking for clarifications if the question is not clear enough.

4
  • 1
    Where are you getting maximize from? There's no maximize function in scipy.optimize (usually you would just minimize -f(a, b, c, d)).
    – ali_m
    Commented Jul 8, 2015 at 9:56
  • 1
    See here for an example showing constrained optimization using scipy.optimize.minimize with the SLSQP solver
    – ali_m
    Commented Jul 8, 2015 at 10:01
  • That's exactly what I am doing, I forgot to mention that in the question. Please let me update the question. Commented Jul 8, 2015 at 10:11
  • @ali_m Thanks for your time but the problem I have is with creating a constraint which would take array of x0 (x0 is the internally passed set of variables) values and checks if sum all x0 * a0 values satisfies some conditions. Please tell me if posting some code would help gettign a clearer picture of what I am asking. Commented Jul 9, 2015 at 11:23

1 Answer 1

6

It's not totally clear from your description which of the parameters of f you are optimizing over. For the purposes of this example I'm going to use x to refer to the vector of parameters you are optimizing over, and a to refer to another parameter vector of the same length which is held constant.

Now let's suppose you wanted to enforce the following inequality constraint:

10 <= x[0] * a[0] + x[1] * a[1] + ... + x[n] * a[n]

First you must define a function that accepts x and a and returns a value that is non-negative when the constraint is met. In this case we could use:

lambda x, a: (x * a).sum() - 10

or equivalently:

lambda x, a: x.dot(a) - 10

Constraints are passed to minimize in a dict (or a sequence of dicts if you have multiple constraints to apply):

con = {'type': 'ineq',
       'fun': lambda x, a: a.dot(x) - 10,
       'jac': lambda x, a: a,
       'args': (a,)}

For greater efficiency I've also defined a function that returns the Jacobian (the sequence of partial derivatives of the constraint function w.r.t. each parameter in x), although this is not essential - if unspecified it will be estimated via first-order finite differences.

Your call to minimize would then look something like:

res = minimize(f, x0, args=(a,), method='SLSQP', constraints=con)

You can find another complete example of constrained optimization using SLSQP in the official documentation here.

3
  • Thanks a lot for your help, this is almost exactly what I wanted to ask. A part of my original concern was that how will the minimize function know which values to pass to the constraint function lambda x, a along with x which is default, I assume. Is the parameter a to the lambda expression dependent on the order of items in the tuple passed as args (should a be the first item in the tuple, and hence the first argument of the objective function f? Commented Jul 10, 2015 at 6:31
  • 1
    The first argument to f (and to the jacobian, hessian and/or constraint function(s)) should always be the vector of parameters that f is being optimized over. Any additional parameters that are held constant should be passed via the args tuple in the order that they are needed by f. So if f is called as f(x, a, b, c) then you would call minimize with args=(a, b, c) and your constraint function would also need to accept x, a, b, c in that order.
    – ali_m
    Commented Jul 10, 2015 at 10:01
  • My problem with this is that my constraints are evaluated in f. So, I have to run my model twice per iteration to feed scipy the constraints.
    – kilojoules
    Commented Dec 8, 2017 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.