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.
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
Optimization terminated successfully. (Exit mode 0) Current function value: -1.41421356237 Iterations: 5 Function evaluations: 20 Gradient evaluations: 5 [-0.70710678 -0.70710678]
Copyright (C) 2013 by John Kitchin. See the License for information about copying.