Sequential Quadratic Programming¶
Copyright (C) 2020 Andreas Kloeckner
MIT LicensePermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(which is just using Newton on a Lagrangian)
import numpy as np import numpy.linalg as la
Here's an objective function $f$ and a constraint $g(x)=0$:
def f(vec): x = vec y = vec return (x-2)**4 + 2*(y-1)**2 def g(vec): x = vec y = vec return x + 4*y - 3
Now define the Lagrangian, its gradient, and its Hessian:
def L(vec): lam = vec return f(vec) + lam * g(vec) def grad_L(vec): x = vec y = vec lam = vec return np.array([ 4*(x-2)**3 + lam, 4*(y-1) + 4*lam, x+4*y-3 ]) def hess_L(vec): x = vec y = vec lam = vec return np.array([ [12*(x-2)**2, 0, 1], [0, 4, 4], [1, 4, 0] ])
At this point, we only need to find an unconstrained minimum of the Lagrangian!
Let's fix a starting vector
vec = np.zeros(3)
Implement Newton and run this cell in place a number of times (Ctrl-Enter):
vec = vec - la.solve(hess_L(vec), grad_L(vec)) vec
array([ 1.46398989, 0.38400253, 0.61599747])
Let's first check that we satisfy the constraint:
Next, let's look at a plot:
import matplotlib.pyplot as pt x, y = np.mgrid[0:4:30j, -3:5:30j] pt.contour(x, y, f(np.array([x,y])), 20) pt.contour(x, y, g(np.array([x,y])), levels=) pt.plot(vec, vec, "o")
[<matplotlib.lines.Line2D at 0x7fd6b4398390>]