from base import Struct from log import Log conf = Struct( isPlot = True ) log = Log.fromConf( conf, (['data1', 'data2'], ['data3']) ) log( 1, 2, 3 ) log( 1, 2, 3 ) log( 3, 2, 1 ) log( 0, 4, 1, finished = True )
def fmin_sd(conf, x0, fn_of, fn_ofg, args=()): nc_of, tt_of, fn_of = wrapFunction(fn_of, args) nc_ofg, tt_ofg, fn_ofg = wrapFunction(fn_ofg, args) timeStats = {"of": tt_of, "ofg": tt_ofg, "check": []} if conf.log: log = Log.fromConf(conf, (["of"], ["ofgNorm"], ["alpha"])) ofg = None it = 0 xit = x0.copy() while 1: of = fn_of(xit) if it == 0: of0 = ofit0 = ofPrev = of ofPrevPrev = of + 5000.0 if ofg is None: ofg = fn_ofg(xit) if conf.check: tt = time.clock() checkGradient(xit, ofg, fn_of, conf.delta, conf.check) timeStats["check"].append(time.clock() - tt) ofgNorm = vecNorm(ofg, conf.norm) status = convTest(conf, it, of, ofit0, ofgNorm) if status >= 0: break # These values are modified by the line search, even if it fails ofPrev_bak = ofPrev ofPrevPrev_bak = ofPrevPrev if conf.ls: alpha, fc, gc, ofPrev, ofPrevPrev, ofg1 = linesearch.line_search( fn_of, fn_ofg, xit, -ofg, ofg, ofPrev, ofPrevPrev, c2=0.4 ) if alpha is None: # line search failed -- use different one. alpha, fc, gc, ofPrev, ofPrevPrev, ofg1 = sopt.line_search( fn_of, fn_ofg, xit, -ofg, ofg, ofPrev_bak, ofPrevPrev_bak ) if alpha is None or alpha == 0: # This line search also failed to find a better solution. status = 3 break else: alpha = 1.0 ofg1 = None if conf.log: log(of, ofgNorm, alpha) xit = xit - alpha * ofg if ofg1 is None: ofg = None else: ofg = ofg1.copy() for key, val in timeStats.iteritems(): if len(val): print "%10s: %7.2f [s]" % (key, val[-1]) ofit0 = of it = it + 1 print "status: %d" % status print "initial value: %.8e" % of0 print "current value: %.8e" % of print "iterations: %d" % it print "function evaluations: %d in %.2f [s]" % (nc_of[0], nm.sum(timeStats["of"])) print "gradient evaluations: %d in %.2f [s]" % (nc_ofg[0], nm.sum(timeStats["ofg"])) if conf.log: log(of, ofgNorm, alpha, finished=True) return xit, log else: return xit