def setup(self): """ Sets up nonlinear optimization machinery """ unix.mkdir(PATH.OPTIMIZE) # prepare output writers self.writer = Writer(path=PATH.OUTPUT) self.stepwriter = StepWriter(path=PATH.SUBMIT) # prepare algorithm machinery if PAR.SCHEME in ['NLCG']: self.NLCG = NLCG(path=PATH.OPTIMIZE, maxiter=PAR.NLCGMAX, thresh=PAR.NLCGTHRESH, precond=self.precond()) elif PAR.SCHEME in ['LBFGS']: self.LBFGS = LBFGS(path=PATH.OPTIMIZE, memory=PAR.LBFGSMEM, maxiter=PAR.LBFGSMAX, thresh=PAR.LBFGSTHRESH, precond=self.precond()) # write initial model if exists(PATH.MODEL_INIT): import solver src = PATH.MODEL_INIT dst = join(PATH.OPTIMIZE, 'm_new') savenpy(dst, solver.merge(solver.load(src)))
def apply_precond(self, r): if not self.precond: return r elif self.precond in ['LBFGS_3']: if self.iter == 1: self.LBFGS = LBFGS(self.path, memory=3) y = r elif self.ilcg == 0: S, Y = self.LBFGS.update() y = -self.LBFGS.apply(self.load('LCG/r'), S, Y) else: y = -self.LBFGS.apply(self.load('LCG/r')) return y elif self.precond in ['LBFGS_6']: if self.iter == 1: self.LBFGS = LBFGS(self.path, memory=6) y = r elif self.ilcg == 0: S, Y = self.LBFGS.update() y = -self.LBFGS.apply(self.load('LCG/r'), S, Y) else: y = -self.LBFGS.apply(self.load('LCG/r')) return y elif self.precond in ['LBFGS_9']: if self.iter == 1: self.LBFGS = LBFGS(self.path, memory=9) y = r elif self.ilcg == 0: S, Y = self.LBFGS.update() y = -self.LBFGS.apply(self.load('LCG/r'), S, Y) else: y = -self.LBFGS.apply(self.load('LCG/r')) return y else: raise ValueError