0

I am trying to maximize a three variable function, func(x,y,z), under the constraint that two other three variable functions are equal to zero.

I was following the "Constrained minimization of multivariate scalar functions (minimize)" example from http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html. I changed the objective function to my own and adjusted the program to (I thought) run for three variables instead of the example's two. Here's my code:

import numpy as np
from scipy.optimize import minimize

def zeroth(x):      # The form of the zeroth order contribution
    return x/(1+x**2)

def first(x):       # The form of the first order contribution
    return x/(1+x**2)**2 

def second(x):      # The second order contribution
    return x*(3-x**2)/(1+x**2)**3

def derivZeroth(x):
    return (1-x**2)/(1+x**2)**2

def derivFirst(x):
    return (1-3*x**2)/(1+x**2)**3

def derivSecond(x):
    return 3*(x**4 - 6*x**2 + 1)/(1+x**2)**4

def func(x, sign=1.0):
    """ Objective function """
    return  zeroth(x[0]) + zeroth(x[1]) - zeroth(x[2]) 

def func_deriv(x, sign=1.0):
    """ Derivative of objective function """
    dfdx0 = derivZeroth(x[0])
    dfdx1 = derivZeroth(x[1])
    dfdx2 = -derivZeroth(x[2])
    return np.array([ dfdx0, dfdx1, dfdx2 ])

cons = ({'type': 'eq',
         'fun' : lambda x: np.array([ first(x[0]) + first(x[1]) - first(x[2]) ]),
         'jac' : lambda x: np.array([ derivFirst(x[0]) + derivFirst(x[1]) - derivFirst(x[2])  ])},
        {'type': 'eq',
         'fun' : lambda x: np.array([ second(x[0]) + second(x[1]) - second(x[2]) ]),
         'jac' : lambda x: np.array([ derivSecond(x[0]) + derivSecond(x[1]) -  derivSecond(x[2]) ])})


x0 = [1.0,1.0,1.0]

res = minimize(func, x0, args=(-1.0,), jac=func_deriv,
           constraints=cons, method='SLSQP', options={'disp': True})





print(res.x)

I get the error

ValueError: all the input array dimensions except for the concatenation axis must match exactly

The full error looks like this:

Traceback (most recent call last):
  File "mycode.py", line 46, in <module>
    constraints=cons, method='SLSQP', options={'disp': True})
  File ".../python2.7/site-packages/scipy/optimize/_minimize.py", line 388, in minimize
    constraints, **options)
  File ".../python2.7/site-packages/scipy/optimize/slsqp.py", line 393, in _minimize_slsqp
    a = vstack((a_eq, a_ieq))
  File ".../python2.7/site-packages/numpy/core/shape_base.py", line 228, in vstack
    return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
ValueError: all the input array dimensions except for the concatenation axis must match exactly

What's going on?

1 Answer 1

2

I think the jacobians of the constraints should have length 3.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that's it, I was adding them instead of separating by commas. Thanks!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.