I have a python program running under Jython (to use a third party Java API), inside which I would like to calculate a constrained minimization of a multivariate function.
Scipy has a module for this that works perfectly (scipy.optimize
) but unfortunately you cannot use scipy within Jython. Does anyone know of a good library/any other way to do this in Jython? If I could just run this under Jython, I'd be all set:
def func(x, sign=1.0):
""" Objective function -- minimize this """
return sign*(2*x[0]*x[1] + 2*x[0] - x[0]**2 - 2*x[1]**2)
def func_deriv(x, sign=1.0):
""" Derivative of objective function """
dfdx0 = sign*(-2*x[0] + 2*x[1] + 2)
dfdx1 = sign*(2*x[0] - 4*x[1])
return np.array([ dfdx0, dfdx1 ])
cons = ({'type': 'eq',
'fun' : lambda x: np.array([x[0]**3 - x[1]]),
'jac' : lambda x: np.array([3.0*(x[0]**2.0), -1.0])}, #partial derivative of fun
{'type': 'ineq',
'fun' : lambda x: np.array([x[1] - 1]),
'jac' : lambda x: np.array([0.0, 1.0])}) #partial derivative of fun
res = minimize(func, [-1.0,1.0], args=(-1.0,), jac=func_deriv,
method='SLSQP', constraints=cons, options={'disp': True})
Thanks! -Michael