[SciPy-User] Constrained Optimization

Hanno Klemm klemm at phys.ethz.ch
Sat May 30 04:02:13 EDT 2015



> On 30.05.2015, at 02:05, Arash M <arashaga at gmail.com> wrote:
> 
> 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)
> __________________________________

Hi,
I haven't fully checked your equations but in your call to minimize, you don't pass in your constraints. Therefore it is not a surprise they  aren't honoured. Just try to add constraints=cons to the call to minimize and see if that solves your problems. 

Hanno

> _____________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150530/19ea3582/attachment.html>


More information about the SciPy-User mailing list