Exemple #1
0
def _par_at_boundary(low, val, high, tol):
    for par_min, par_val, par_max in zip(low, val, high):
        if sao_fcmp(par_val, par_min, tol) == 0:
            return True
        if sao_fcmp(par_val, par_max, tol) == 0:
            return True
    return False
Exemple #2
0
 def par_at_boundary( low, val, high, tol ):
     for par_min, par_val, par_max in izip( low, val, high ):
         if sao_fcmp( par_val, par_min, tol ) == 0:
             return True
         if sao_fcmp( par_val, par_max, tol ) == 0:
             return True
     return False
Exemple #3
0
def test_calc_sao_fcmp(tolerance, expected):
    """
    this test was created using sherpa 4.8.1 (2507414) as an oracle for the python 3 migration

    py3-todo: what is the difference between sao_fcmp and gsl_fcmp?
    """
    assert_array_equal(expected, _utils.sao_fcmp([0.12345, 0.54321], [0.12346, 0.54320], tolerance))
Exemple #4
0
    def assertEqualWithinTol(self, first, second, tol=1e-7, msg=None):
        """Check that the values are equal within an absolute tolerance.

        Parameters
        ----------
        first : number or array_like
           The expected value, or values.
        second : number or array_like
           The value, or values, to check. If first is an array, then
           second must be an array of the same size. If first is
           a scalar then second can be a scalar or an array.
        tol : number
           The absolute tolerance used for comparison.
        msg : string
           The message to display if the check fails.

        """

        self.assertFalse(numpy.any(sao_fcmp(first, second, tol)), msg)
Exemple #5
0
    def assertEqualWithinTol(self, first, second, tol=1e-7, msg=None):
        """Check that the values are equal within an absolute tolerance.

        Parameters
        ----------
        first : number or array_like
           The expected value, or values.
        second : number or array_like
           The value, or values, to check. If first is an array, then
           second must be an array of the same size. If first is
           a scalar then second can be a scalar or an array.
        tol : number
           The absolute tolerance used for comparison.
        msg : string
           The message to display if the check fails.

        """

        self.assertFalse(numpy.any(sao_fcmp(first, second, tol)), msg)
Exemple #6
0
    def myopt( myfcn, xxx, ftol, maxfev, seed, pop, xprob,
               weight, factor=4.0, debug=False ):

        x = xxx[ 0 ]
        xmin = xxx[ 1 ]
        xmax = xxx[ 2 ]
        maxfev_per_iter = 512 * x.size

        def random_start( xmin, xmax ):
            xx = []
            for ii in range( len(xmin ) ):
                xx.append( random.uniform( xmin[ ii ], xmax[ ii ] ) )
            return numpy.asarray( xx )

        ############################# NelderMead #############################
        mymaxfev = min( maxfev_per_iter, maxfev )
        if all( x == 0.0 ):
            mystep = map( lambda fubar: 1.2 + fubar, x )
        else: 
            mystep = map( lambda fubar: 1.2 * fubar, x )
        result = neldermead( myfcn, x, xmin, xmax, maxfev=mymaxfev, ftol=ftol,
                             finalsimplex=9, step=mystep )
        x = numpy.asarray( result[ 1 ], numpy.float_ )
        nfval = result[2]
        nfev = result[4].get( 'nfev' )
        if verbose or False != debug:
            print 'f_nm%s=%.14e in %d nfev' % ( x, nfval, nfev )
        ############################# NelderMead #############################

        ############################## nmDifEvo ##############################
        xmin, xmax = _narrow_limits( 4 * factor, [x,xmin,xmax], debug=False )
        mymaxfev = min( maxfev_per_iter, maxfev - nfev )
        result = difevo_nm( myfcn, x, xmin, xmax, ftol=ftol, maxfev=mymaxfev,
                            seed=seed, population_size=pop, xprob=xprob,
                            weighting_factor=weight )
        nfev += result[4].get( 'nfev' )
        x = numpy.asarray( result[1], numpy.float_ )
        nfval = result[2]
        if verbose or False != debug:
            print 'f_de_nm%s=%.14e in %d nfev' % ( x, result[2],
                                                   result[4].get('nfev'))
        ############################## nmDifEvo ##############################
            
        ofval = FUNC_MAX        
        while nfev < maxfev:

            xmin, xmax = _narrow_limits( factor, [x,xmin,xmax], debug=False )

            ############################ nmDifEvo #############################
            y = random_start( xmin, xmax )
            mymaxfev = min( maxfev_per_iter, maxfev - nfev )
            result = difevo_nm( myfcn, y, xmin, xmax, ftol=ftol,
                                maxfev=mymaxfev, seed=seed,
                                population_size=pop, xprob=xprob,
                                weighting_factor=weight )
            nfev += result[4].get( 'nfev' )
            if result[2] < nfval:
                nfval = result[2]
                x = numpy.asarray( result[1], numpy.float_ )
            if verbose or False != debug:
                print 'f_de_nm%s=%.14e in %d nfev' % \
                      ( x, result[2], result[4].get('nfev'))
            ############################ nmDifEvo #############################


            if False != debug:
                print 'ofval=%.14e\tnfval=%.14e\n' % (ofval, nfval)

            if sao_fcmp( ofval, nfval, ftol ) <= 0:
                return x, nfval, nfev
            ofval = nfval
            factor *= 2
            
        return x, nfval, nfev
Exemple #7
0
    def myopt(myfcn,
              xxx,
              ftol,
              maxfev,
              seed,
              pop,
              xprob,
              weight,
              factor=4.0,
              debug=False):

        x = xxx[0]
        xmin = xxx[1]
        xmax = xxx[2]
        maxfev_per_iter = 512 * x.size

        def random_start(xmin, xmax):
            xx = []
            for ii in range(len(xmin)):
                xx.append(random.uniform(xmin[ii], xmax[ii]))
            return numpy.asarray(xx)

        ############################# NelderMead #############################
        mymaxfev = min(maxfev_per_iter, maxfev)
        if all(x == 0.0):
            mystep = list(map(lambda fubar: 1.2 + fubar, x))
        else:
            mystep = list(map(lambda fubar: 1.2 * fubar, x))
        if 1 == numcores:
            result = neldermead(myfcn,
                                x,
                                xmin,
                                xmax,
                                maxfev=mymaxfev,
                                ftol=ftol,
                                finalsimplex=9,
                                step=mystep)
            x = numpy.asarray(result[1], numpy.float_)
            nfval = result[2]
            nfev = result[4].get('nfev')
        else:
            ncores_nm = ncoresNelderMead()
            nfev, nfval, x = \
                ncores_nm(stat_cb0, x, xmin, xmax, ftol, mymaxfev, numcores)
        if verbose or debug != False:
            print('f_nm%s=%.14e in %d nfev' % (x, nfval, nfev))
        ############################# NelderMead #############################

        ############################## nmDifEvo #############################
        xmin, xmax = _narrow_limits(4 * factor, [x, xmin, xmax], debug=False)
        mymaxfev = min(maxfev_per_iter, maxfev - nfev)
        if 1 == numcores:
            result = difevo_nm(myfcn, x, xmin, xmax, ftol, mymaxfev, verbose,
                               seed, pop, xprob, weight)
            nfev += result[4].get('nfev')
            x = numpy.asarray(result[1], numpy.float_)
            nfval = result[2]
        else:
            ncores_de = ncoresDifEvo()
            mystep = None
            tmp_nfev, tmp_fmin, tmp_par = \
                ncores_de(stat_cb0, x, xmin, xmax, ftol, mymaxfev, mystep,
                          numcores, pop, seed, weight, xprob, verbose)
            nfev += tmp_nfev
            if tmp_fmin < nfval:
                nfval = tmp_fmin
                x = tmp_par
        if verbose or debug != False:
            print('f_de_nm%s=%.14e in %d nfev' % (x, nfval, nfev))
        ############################## nmDifEvo #############################

        ofval = FUNC_MAX
        while nfev < maxfev:

            xmin, xmax = _narrow_limits(factor, [x, xmin, xmax], debug=False)

            ############################ nmDifEvo #############################
            y = random_start(xmin, xmax)
            mymaxfev = min(maxfev_per_iter, maxfev - nfev)
            if 1 == numcores:
                result = difevo_nm(myfcn, y, xmin, xmax, ftol, mymaxfev,
                                   verbose, seed, pop, xprob, weight)
                nfev += result[4].get('nfev')
                if result[2] < nfval:
                    nfval = result[2]
                    x = numpy.asarray(result[1], numpy.float_)
                if verbose or debug != False:
                    print('f_de_nm%s=%.14e in %d nfev' % \
                          (x, result[2], result[4].get('nfev')))
            ############################ nmDifEvo #############################

            if debug != False:
                print('ofval=%.14e\tnfval=%.14e\n' % (ofval, nfval))

            if sao_fcmp(ofval, nfval, ftol) <= 0:
                return x, nfval, nfev
            ofval = nfval
            factor *= 2

        return x, nfval, nfev
Exemple #8
0
    def myopt( myfcn, xxx, ftol, maxfev, seed, pop, xprob,
               weight, factor=4.0, debug=False ):

        x = xxx[ 0 ]
        xmin = xxx[ 1 ]
        xmax = xxx[ 2 ]
        maxfev_per_iter = 512 * x.size

        def random_start( xmin, xmax ):
            xx = []
            for ii in range( len(xmin ) ):
                xx.append( random.uniform( xmin[ ii ], xmax[ ii ] ) )
            return numpy.asarray( xx )

        ############################# NelderMead #############################
        mymaxfev = min( maxfev_per_iter, maxfev )
        if all( x == 0.0 ):
            mystep = map( lambda fubar: 1.2 + fubar, x )
        else: 
            mystep = map( lambda fubar: 1.2 * fubar, x )
        result = neldermead( myfcn, x, xmin, xmax, maxfev=mymaxfev, ftol=ftol,
                             finalsimplex=9, step=mystep )
        x = numpy.asarray( result[ 1 ], numpy.float_ )
        nfval = result[2]
        nfev = result[4].get( 'nfev' )
        if verbose or False != debug:
            print 'f_nm%s=%.14e in %d nfev' % ( x, nfval, nfev )
        ############################# NelderMead #############################

        ############################## nmDifEvo ##############################
        xmin, xmax = _narrow_limits( 4 * factor, [x,xmin,xmax], debug=False )
        mymaxfev = min( maxfev_per_iter, maxfev - nfev )
        result = difevo_nm( myfcn, x, xmin, xmax, ftol=ftol, maxfev=mymaxfev,
                            seed=seed, population_size=pop, xprob=xprob,
                            weighting_factor=weight )
        nfev += result[4].get( 'nfev' )
        x = numpy.asarray( result[1], numpy.float_ )
        nfval = result[2]
        if verbose or False != debug:
            print 'f_de_nm%s=%.14e in %d nfev' % ( x, result[2],
                                                   result[4].get('nfev'))
        ############################## nmDifEvo ##############################

        ofval = FUNC_MAX        
        while nfev < maxfev:

            xmin, xmax = _narrow_limits( factor, [x,xmin,xmax], debug=False )

            ############################ nmDifEvo #############################
            y = random_start( xmin, xmax )
            mymaxfev = min( maxfev_per_iter, maxfev - nfev )
            result = difevo_nm( myfcn, y, xmin, xmax, ftol=ftol,
                                maxfev=mymaxfev, seed=seed,
                                population_size=pop, xprob=xprob,
                                weighting_factor=weight )
            nfev += result[4].get( 'nfev' )
            if result[2] < nfval:
                nfval = result[2]
                x = numpy.asarray( result[1], numpy.float_ )
            if verbose or False != debug:
                print 'f_de_nm%s=%.14e in %d nfev' % \
                      ( x, result[2], result[4].get('nfev'))
            ############################ nmDifEvo #############################


            if False != debug:
                print 'ofval=%.14e\tnfval=%.14e\n' % (ofval, nfval)

            if sao_fcmp( ofval, nfval, ftol ) <= 0:
                return x, nfval, nfev
            ofval = nfval
            factor *= 2

        return x, nfval, nfev