def calculate_step(self): """ Determines step length and search status """ x, f, gtg, gtp, step_count, update_count = self.search_history() if update_count==0: # quasi-Newton direction is not yet scaled properly, so instead # of a bactracking line perform a bracketing line search alpha, status = super(Backtrack, self).calculate_step() elif step_count==0: # our choice of a unit step length here assumes a well-scaled # search direction alpha = min(1., self.step_len_max) status = 0 elif _check_decrease(x,f): alpha = x[f.argmin()] status = 1 elif step_count <= self.step_count_max: # we need a smaller step length slope = gtp[-1]/gtg[-1] alpha = backtrack2(f[0], slope, x[1], f[1], b1=0.1, b2=0.5) status = 0 else: # failed because step_count_max exceeded alpha = None status = -1 return alpha, status
def calculate_step(self): """ Determines step length and search status """ x, f, gtg, gtp, step_count, update_count = self.search_history() if step_count == 0 and update_count == 0: # based on idea from Dennis and Schnabel alpha = gtg[-1]**-1 status = 0 elif step_count == 0: # based on the first equation in sec 3.5 of Nocedal and Wright 2ed idx = np.argmin(self.func_vals[:-1]) alpha = self.step_lens[idx] * gtp[-2] / gtp[-1] status = 0 elif _check_bracket(x, f) and _good_enough(x, f): alpha = x[f.argmin()] status = 1 elif _check_bracket(x, f): alpha = polyfit2(x, f) status = 0 elif step_count <= self.step_count_max and all(f <= f[0]): # we need a larger step length alpha = 1.618034 * x[-1] status = 0 elif step_count <= self.step_count_max: print("we need a smaller step length") # we need a smaller step length slope = gtp[-1] / gtg[-1] alpha = backtrack2(f[0], slope, x[1], f[1], b1=0.1, b2=0.5) #print("we need a smaller step length") status = 0 else: # failed because step_count_max exceeded alpha = None status = -1 # apply optional step length safeguard if alpha > self.step_len_max and \ step_count == 0: alpha = 0.618034 * self.step_len_max status = 0 elif alpha > self.step_len_max: # stop because safeguard prevents us from going further alpha = self.step_len_max status = 1 return alpha, status
def calculate_step(self): """ Determines step length and search status """ x, f, gtg, gtp, step_count, update_count = self.search_history() if step_count==0 and update_count==0: # based on idea from Dennis and Schnabel alpha = gtg[-1]**-1 status = 0 elif step_count==0: # based on the first equation in sec 3.5 of Nocedal and Wright 2ed idx = np.argmin(self.func_vals[:-1]) alpha = self.step_lens[idx] * gtp[-2]/gtp[-1] status = 0 elif _check_bracket(x,f) and _good_enough(x,f): alpha = x[f.argmin()] status = 1 elif _check_bracket(x,f): alpha = polyfit2(x,f) status = 0 elif step_count <= self.step_count_max and all(f <= f[0]): # we need a larger step length alpha = 1.618034*x[-1] status = 0 elif step_count <= self.step_count_max: # we need a smaller step length slope = gtp[-1]/gtg[-1] alpha = backtrack2(f[0], slope, x[1], f[1], b1=0.1, b2=0.5) status = 0 else: # failed because step_count_max exceeded alpha = None status = -1 # apply optional step length safeguard if alpha > self.step_len_max and \ step_count==0: alpha = 0.618034*self.step_len_max status = 0 elif alpha > self.step_len_max: # stop because safeguard prevents us from going further alpha = self.step_len_max status = 1 return alpha, status
def compute_step(self): """ Computes next trial step length """ unix.cd(PATH.OPTIMIZE) m = self.load('m_new') g = self.load('g_new') p = self.load('p_new') s = loadtxt('s_new') norm_m = max(abs(m)) norm_p = max(abs(p)) p_ratio = float(norm_m / norm_p) x = self.step_lens() f = self.func_vals() # compute trial step length if PAR.LINESEARCH == 'Fixed': alpha = p_ratio * (self.step_count + 1) * PAR.STEPINIT #elif PAR.LINESEARCH == 'Bracket' or \ # self.iter == 1 or self.restarted: elif PAR.LINESEARCH == 'Bracket': if any(f[1:] < f[0]) and (f[-2] < f[-1]): alpha = polyfit2(x, f) elif any(f[1:] <= f[0]): alpha = loadtxt('alpha') * PAR.STEPFACTOR**-1 else: alpha = loadtxt('alpha') * PAR.STEPFACTOR elif PAR.LINESEARCH == 'Backtrack': # calculate slope along 1D profile slope = s / self.dot(g, g)**0.5 if PAR.ADHOCFACTOR: slope *= PAR.ADHOCFACTOR alpha = backtrack2(f[0], slope, x[1], f[1], b1=0.1, b2=0.5) # write trial model corresponding to chosen step length savetxt('alpha', alpha) self.save('m_try', m + alpha * p)
def compute_step(self): """ Computes next trial step length """ unix.cd(PATH.OPTIMIZE) m = self.load('m_new') g = self.load('g_new') p = self.load('p_new') s = loadtxt('s_new') norm_m = max(abs(m)) norm_p = max(abs(p)) p_ratio = float(norm_m/norm_p) x = self.step_lens() f = self.func_vals() # compute trial step length if PAR.LINESEARCH == 'Fixed': alpha = p_ratio*(self.step_count + 1)*PAR.STEPINIT elif PAR.LINESEARCH == 'Bracket' or \ self.iter==1 or self.restarted: if any(f[1:] < f[0]) and (f[-2] < f[-1]): alpha = polyfit2(x, f) elif any(f[1:] <= f[0]): alpha = loadtxt('alpha')*PAR.STEPFACTOR**-1 else: alpha = loadtxt('alpha')*PAR.STEPFACTOR elif PAR.LINESEARCH == 'Backtrack': # calculate slope along 1D profile slope = s/self.dot(g,g)**0.5 if PAR.ADHOCFACTOR: slope *= PAR.ADHOCFACTOR alpha = backtrack2(f[0], slope, x[1], f[1], b1=0.1, b2=0.5) # write trial model corresponding to chosen step length savetxt('alpha', alpha) self.save('m_try', m + alpha*p)
def calculate_step(self): """ Determines step length and search status """ x, f, gtg, gtp, step_count, update_count = self.search_history() # quasi-Newton direction is not yet scaled properly, so instead # of a bactracking line perform a bracketing line search if update_count == 0: alpha, status = super(Backtrack, self).calculate_step() # Assumed well scaled search direction, attempt backtracking line search # with unit step length else: print '\tBacktracking line search' print '\t\tStep lengths = {}'.format(x) print '\t\tMisfits = {}'.format(f) # try unit step length if step_count == 0: print "\t\tAttempting unit step length" alpha = min(1., self.step_len_max) status = 0 # pass if misfit is reduced elif _check_decrease(x, f): print "\t\tMisfit decrease, pass" alpha = x[f.argmin()] status = 1 # if misfit continually increases, decrease step length elif step_count <= self.step_count_max: print "\t\tMisfit increase, decreasing step length" slope = gtp[-1] / gtg[-1] alpha = backtrack2(f[0], slope, x[1], f[1], b1=0.1, b2=0.5) status = 0 # failed because step_count_max exceeded else: print "\t\tBacktracking failed, step_count_max exceeded" alpha = None status = -1 return alpha, status
def calculate_step(self): """ Determines step length and search status """ x, f, gtg, gtp, step_count, update_count = self.search_history() print '\tBracketing line search' print '\t\tStep lengths = {}'.format(x) print '\t\tMisfits = {}'.format(f) # For the first inversion and initial step, set alpha if step_count==0 and update_count==0: # based on idea from Dennis and Schnabel print "\t\tFirst inversion, initial trial step, continuing..." alpha = gtg[-1]**-1 status = 0 # For every i'th inversion's initial step, set alpha elif step_count==0: # based on the first equation in sec 3.5 of Nocedal and Wright 2ed print "\t\tInitial trial step, setting scaled step length" idx = np.argmin(self.func_vals[:-1]) alpha = self.step_lens[idx] * gtp[-2]/gtp[-1] status = 0 # If misfit is reduced and then increased, we've bracketed. Pass elif _check_bracket(x,f) and _good_enough(x,f): print "\t\tBracket okay, step length reasonable, pass" alpha = x[f.argmin()] status = 1 # if misfit is reduced but not close, set to quadratic fit elif _check_bracket(x,f): print "\t\tBracket okay, step length unreasonable, manual step..." alpha = polyfit2(x,f) status = 0 # if misfit continues to step down, increase step length elif step_count <= self.step_count_max and all(f <= f[0]): print "\t\tMisfit not bracketed, increasing step length..." alpha = 1.618034*x[-1] status = 0 # if misfit increases, reduce step length elif step_count <= self.step_count_max: print "\t\tMisfit increasing, reducing step length..." slope = gtp[-1]/gtg[-1] alpha = backtrack2(f[0], slope, x[1], f[1], b1=0.1, b2=0.5) status = 0 # step_count_max exceeded, fail else: print "\t\tBracketing failed, step_count_max exceeded" alpha = None status = -1 # apply optional step length safeguard if alpha > self.step_len_max and step_count==0: print "\tInitial step length safegaurd, setting manual step length" alpha = 0.618034*self.step_len_max status = 0 # stop because safeguard prevents us from going further elif alpha > self.step_len_max: print "step_len_max exceeded, manual set alpha" alpha = self.step_len_max status = 1 return alpha, status