[SciPy-User] Constrained Optimization

Arash M arashaga at gmail.com
Fri May 29 20:05:31 EDT 2015


I am new to scipy, I am trying to s maximize x^(0.5)y^(0.5)

st. x+y=10 using scipy. I can't figure out which method to use. I would
really appreciate it if someone could guide me on this.
  and this is the code I have tried :

from scipy.optimize import *
import numpy as np

def func(x, sign=1.0):
    """ Objective function """
    return sign*(x[0]**(0.5)*2*x[1]**(0.5))

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

cons = ({'type': 'eq', 'fun': lambda x: x[0] + x[1] - 10,
         'jac' : lambda x: np.array([1.0, 1.0])})

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

print(res.x)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150529/8120efbc/attachment.html>


More information about the SciPy-User mailing list