import numpy as np from pylab import norm def GaussNewton(fctn, 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 Input: ------ fctn - objective Function (lambda function) x0 - starting guess Output: ------- xOpt - numerical optimizer """ # 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 = fctn(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 = fctn(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