** Constrained optimization
:PROPERTIES:
:categories: optimization
:date: 2013/02/27 14:43:37
:updated: 2013/02/27 14:43:37
:END:
[[index:optimization!constrained]]
index:fmin_slsqp
[[http://matlab.cheme.cmu.edu/2011/12/24/constrained-optimization/][Matlab post]]
adapted from http://en.wikipedia.org/wiki/Lagrange_multipliers.
Suppose we seek to minimize the function $f(x,y)=x+y$ subject to the constraint that $x^2 + y^2 = 1$. The function we seek to maximize is an unbounded plane, while the constraint is a unit circle. We could setup a Lagrange multiplier approach to solving this problem, but we will use a constrained optimization approach instead.
#+BEGIN_SRC python
from scipy.optimize import fmin_slsqp
def objective(X):
x, y = X
return x + y
def eqc(X):
'equality constraint'
x, y = X
return x**2 + y**2 - 1.0
X0 = [-1, -1]
X = fmin_slsqp(objective, X0, eqcons=[eqc])
print X
#+END_SRC
#+RESULTS:
: Optimization terminated successfully. (Exit mode 0)
: Current function value: -1.41421356237
: Iterations: 5
: Function evaluations: 20
: Gradient evaluations: 5
: [-0.70710678 -0.70710678]