From 48cd71b7078a28b0f37cfcb7974bc952189749c3 Mon Sep 17 00:00:00 2001 From: Lars Ruthotto Date: Wed, 19 Jun 2013 12:14:56 -0700 Subject: [PATCH] GaussNewton example for Rosenbrock function ( to be generalized!!) --- code/GaussNewton.py | 85 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 code/GaussNewton.py diff --git a/code/GaussNewton.py b/code/GaussNewton.py new file mode 100644 index 00000000..997ffbb4 --- /dev/null +++ b/code/GaussNewton.py @@ -0,0 +1,85 @@ +import numpy as np +from pylab import norm + +def GaussNewton(x0, maxIter=20, maxIterLS=10, LSreduction=1e-4, tolJ=1e-3, tolX=1e-3, tolG=1e-3, eps=1e-16, xStop=np.empty): + """ + GaussNewton optimization for Rosenbrock function (has to be generalized) + """ + # initial output + print "%s GaussNewton %s" % ('='*22,'='*22) + print "iter\tJc\t\tnorm(dJ)\tLS" + print "%s" % '-'*57 + + # evaluate stopping criteria + if xStop==np.empty: + xStop=x0 + Jstop = Rosenbrock(xStop) + print "%3d\t%1.2e" % (-1, Jstop[0]) + + # initialize + xc = x0 + STOP = np.zeros((5,1),dtype=bool) + iterLS=0; iter=0 + + Jold = Jstop + xOld=xc + while 1: + # evaluate objective function + Jc,dJ,H = Rosenbrock(xc) + print "%3d\t%1.2e\t%1.2e\t%d" % (iter, Jc[0],norm(dJ),iterLS) + + # check stopping rules + STOP[0] = (iter>0) & (abs(Jc[0]-Jold[0]) <= tolJ*(1+abs(Jstop[0]))) + STOP[1] = (iter>0) & (norm(xc-xOld) <= tolX*(1+norm(x0))) + STOP[2] = norm(dJ) <= tolG*(1+abs(Jstop[0])) + STOP[3] = norm(dJ) <= 1e3*eps + STOP[4] = (iter >= maxIter) + if all(STOP[0:3]) | any(STOP[3:]): + break + + # get search direction + dx = np.linalg.solve(H,-dJ) + + # Armijo linesearch + descent = np.dot(dJ.T,dx) + LS =0; t = 1; iterLS=1 + while (iterLS