Using Python 3.6, I am trying to minimize a function with scipy.optimize.minimize. My minimization problem is two limitations, and I can find a solution. So far I have the following:
import numpy as np
from scipy.optimize import minimize
array = np.array([[3.0, 0.25, 0.75],
[0.1, 0.65, 2.50],
[0.80, 2.5, 1.20],
[0.0, 0.25, 0.15],
[1.2, 2.40, 3.60]])
matrix = np.array([[1.0, 1.5, -2.],
[0.5, 3.0, 2.5],
[1.0, 0.25, 0.75]])
def fct1(x):
return -sum(x.dot(array.T))
def fct2(x):
return x.dot(matrix).dot(x)
x0 = np.ones(3) / 3
cons = ({'type': 'eq', 'fun': lambda x: x.sum() - 1.0},
{'type': 'eq', 'fun': lambda x: fct2(x) - tgt})
tgt = 0.15
w = minimize(fct1, x0, method='SLSQP', constraints=cons)['x']
res1 = fct1(w)
res2 = fct2(w)
Now I am trying to get my optimizer to work faster, since this is only a simplified problem. In the end, my arrays and matrices are much larger. In the previous question, someone came up with the idea of optimizing the jacobian of my function for optimization, so I added the following:
def fct1_deriv(x):
return -sum(np.ones_like(x).dot(array.T))
w = minimize(fct1, x0, method='SLSQP', jac=fct1_deriv, constraints=cons)['x']
The problem is that when you try to start, the following error message appears:
0-th dimension must be fixed to 4 but got 2
Traceback (most recent call last):
File "C:\Anaconda2\envs\py36\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-111-d1b854178c13>", line 1, in <module>
w = minimize(fct1, x0, method='SLSQP', jac=fct1_deriv, constraints=cons)['x']
File "C:\Anaconda2\envs\py36\lib\site-packages\scipy\optimize\_minimize.py", line 458, in minimize
constraints, callback=callback, **options)
File "C:\Anaconda2\envs\py36\lib\site-packages\scipy\optimize\slsqp.py", line 410, in _minimize_slsqp
slsqp(m, meq, x, xl, xu, fx, c, g, a, acc, majiter, mode, w, jw)
_slsqp.error: failed in converting 8th argument `g' of _slsqp.slsqp to C/Fortran array
, ? :
python?