# [SciPy-user] lagrange multipliers in python

fdu.xiaojf@gmai... fdu.xiaojf@gmai...
Mon Jun 18 08:18:16 CDT 2007

```Hi all,

fdu.xiaojf@gmail.com wrote:

>
> My last email was composed in a hurry, so let me describe my problem in
> detail to make it clear.
>
> I have to minimize a multivariate function F(x1, x2, ..., xn) subject to
> multiple inequality constraints and equality constraints.
>
> The number of variables(x1, x2, ..., xn) is 10 ~ 20, the number of
> inequality constraints is the same with the number of variables( all
> variables should be no less than 0). The number of equality constraints
> is less than 8(mainly 4 or 5), and the equality constraints are linear.
>
> My function is too complicated to get the expression of derivate easily, so
> according to Joachim Dahl(dahl.joachim@gmail.com)'s post, it is probably
> non-convex. But I think it's possible to calculate the first derivate
> numerically.
>
> I have tried scipy.optimize.fmin_l_bfgs_b(), which can handle bound constraints
> but seems cannot handle equality constraints.
>
> Mr. Markus Amann has kindly sent me a script written by him, which can
> handle equality constraints and is easy to use. The method used by
> Markus involves the calculation of Jacobian, which I don't
> understand.(Sorry for my ignorance in this filed. My major is chemistry,
> and I'm trying to learn some knowledge about numerical optimization.)
> However, it seems that the script cannot handle inequality constraints.
> (sorry if I was wrong).
>
> I hope my bad English have described my problem clearly.
>
> Any help will be greatly appreciated.
>
> Best regards,
>
> Xiao Jianfeng

I have found COBYLA(http://www.jeannot.org/~js/code/index.en.html#COBYLA),
and it has a Python interface, which make it very easy to use.

It seems that COBYLA is capable to handle both equality and inequality
constraints together. Here is an example of COBYLA(it actually example.py
shiped with COBYLA):

##--------------------begin of example.py------------------
#!/usr/bin/env python
# Python COBYLA example
# @(#) \$Jeannot: example.py,v 1.2 2004/04/13 16:35:11 js Exp \$

import cobyla

# A function to minimize
# Must return a tuple with the function value and the value of the constraints
# or None to abort the minimization
def function(x):
f = x[0]**2+abs(x[1])**3
# Two constraints to represent the equality constraint x**2+y**2 == 25
con = [0]*2
con[0] = x[0]**2 + x[1]**2 - 25 # x**2+y**2 >= 25
con[1] = - con[0] # x**2+y**2 <= 25
return f, con

# Optimizer call
rc, nf, x = cobyla.minimize(function, [-7, 3], low = [-10, 1], up = [3, 10])

print "After", nf, "function evaluations, COBYLA returned:", cobyla.RCSTRINGS[rc]
print "x =", x
print "f =", function(x)[0]
print "con = ", function(x)[1]
print "exact value = [-4.898979456, 1]"
##--------------------end of example.py------------------

Would somebody who is familiar with COBYLA tell me that is COBYLA suitable
for my problem?

Thanks.

Xiao Jianfeng
```