コード例 #1
0
ファイル: bracket.py プロジェクト: SuwenJunliu/seisflows
    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
コード例 #2
0
def _good_enough(step_lens, func_vals, thresh=np.log10(1.2)):
    """ Checks if step length is reasonably close to quadratic estimate
    """
    x, f = step_lens, func_vals
    if not _check_bracket(x,f):
        return 0
    x0 = polyfit2(x,f)
    if any(np.abs(np.log10(x[1:]/x0)) < thresh):
        return 1
    else:
        return 0
コード例 #3
0
    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
コード例 #4
0
ファイル: bracket.py プロジェクト: SuwenJunliu/seisflows
def _good_enough(step_lens, func_vals, thresh=np.log10(1.2)):
    """ Checks if step length is reasonably close to quadratic estimate
    """
    x, f = step_lens, func_vals
    if not _check_bracket(x, f):
        return 0
    print("x", x)
    print("f", f)
    x0 = polyfit2(x, f)
    print("x0 " + str(x0))
    if any(np.abs(np.log10(x[1:] / x0)) < thresh):
        return 1
    else:
        print("not good ", np.min(np.abs(np.log10(x[1:] / x0))), thresh)
        return 0
コード例 #5
0
    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)
コード例 #6
0
ファイル: base.py プロジェクト: AlainPlattner/seisflows
    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)
コード例 #7
0
ファイル: bracket.py プロジェクト: seisflows/seisflows
def _good_enough(step_lens, func_vals, thresh=np.log10(1.2)):
    """
    Checks if step length is reasonably close to quadratic estimate
    
    polyfit2 requires 
    
    :type step_lens: np.array
    :param step_lens: an array of the step lengths taken during iteration
    :type func_vals: np.array
    :param func_vals: array of misfit values from eval func function
    :type thresh: numpy.float64
    :param thresh: threshold value for comparison against quadratic estimate
    :rtype: int
    :return: status of function as a bool
    """
    x, f = step_lens, func_vals
    if not _check_bracket(x,f):
        return 0
    x0 = polyfit2(x,f)
    if any(np.abs(np.log10(x[1:]/x0)) < thresh):
        return 1
    else:
        return 0
コード例 #8
0
ファイル: bracket.py プロジェクト: seisflows/seisflows
    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