示例#1
0
def difevo(fcn,
           x0,
           xmin,
           xmax,
           maxfev=None,
           ftol=EPSILON,
           xprob=0.9,
           weighting_factor=0.8,
           population_size=None,
           multicore=True,
           verbose=0):

    x0, xmin, xmax = _check_args(x0, xmin, xmax)

    npar = len(x0)
    if maxfev is None:
        maxfev = 4096 * npar * 16
    if population_size is None:
        population_size = 16 * npar

    starttime = time.time()
    nm_result = sherpa_neldermead(fcn,
                                  x0,
                                  xmin,
                                  xmax,
                                  ftol=ftol,
                                  maxfev=maxfev,
                                  initsimplex=0,
                                  finalsimplex=9,
                                  step=None,
                                  verbose=0)

    print nm_result
    print '%f secs' % (time.time() - starttime)

    if nm_result[0]:

        dif_evo = DifEvo(fcn)

        maxfev_per_iter = min(maxfev - nm_result[4].get('nfev'), 512 * npar)
        factor = 4.0
        myx = nm_result[1]
        myxmin, myxmax = _narrow_limit(4 * factor, [myx, xmin, xmax],
                                       debug=True)
        starttime = time.time()
        de_result = dif_evo(myx, myxmin, myxmax, maxfev_per_iter, ftol,
                            population_size, xprob, weighting_factor)
        print de_result
        print '%f secs' % (time.time() - starttime)

        de_result = list(de_result)
        de_result[-1] += nm_result[4].get('nfev')
        return get_result(de_result, maxfev)

    else:

        return nm_result
示例#2
0
def difevo(
    fcn,
    x0,
    xmin,
    xmax,
    maxfev=None,
    ftol=EPSILON,
    xprob=0.9,
    weighting_factor=0.8,
    population_size=None,
    multicore=True,
    verbose=0,
):

    x0, xmin, xmax = _check_args(x0, xmin, xmax)

    npar = len(x0)
    if maxfev is None:
        maxfev = 4096 * npar * 16
    if population_size is None:
        population_size = 16 * npar

    starttime = time.time()
    nm_result = sherpa_neldermead(
        fcn, x0, xmin, xmax, ftol=ftol, maxfev=maxfev, initsimplex=0, finalsimplex=9, step=None, verbose=0
    )

    print nm_result
    print "%f secs" % (time.time() - starttime)

    if nm_result[0]:

        dif_evo = DifEvo(fcn)

        maxfev_per_iter = min(maxfev - nm_result[4].get("nfev"), 512 * npar)
        factor = 4.0
        myx = nm_result[1]
        myxmin, myxmax = _narrow_limit(4 * factor, [myx, xmin, xmax], debug=True)
        starttime = time.time()
        de_result = dif_evo(myx, myxmin, myxmax, maxfev_per_iter, ftol, population_size, xprob, weighting_factor)
        print de_result
        print "%f secs" % (time.time() - starttime)

        de_result = list(de_result)
        de_result[-1] += nm_result[4].get("nfev")
        return get_result(de_result, maxfev)

    else:

        return nm_result
示例#3
0
    def __call__(
        self,
        x,
        xmin,
        xmax,
        maxnfev,
        tolerance,
        population_size,
        xprob,
        scale_factor,
        seed=81547,
        multicore=False,
        verbose=None,
    ):

        debug = False
        use_local_opt = True

        x, self.xmin, self.xmax = self.check_args(x, xmin, xmax)
        self.fcn = func_bounds(self.fcn, xmin, xmax)
        polytope, fstat = self.init_population(x, xmin, xmax, population_size, seed)
        strategy_function = self.best1exp

        npar = len(x)
        npar_plus_1 = npar + 1

        self.trial_solution = numpy.zeros(npar_plus_1, dtype=numpy.float_)
        fct_vals = numpy.zeros(npar, dtype=numpy.float_)

        model_par = x[:]
        while self.nfev[0] < maxnfev:
            for candidate in xrange(population_size):

                self.trial_solution[:] = polytope[candidate][:]
                strategy_function(candidate, polytope, model_par, xprob, scale_factor)
                self.trial_solution[-1] = self.fcn(self.trial_solution[:-1])
                if self.trial_solution[-1] < polytope[candidate, -1]:
                    polytope[candidate][:] = self.trial_solution[:]

                    if self.trial_solution[npar] < fstat:

                        if use_local_opt:
                            if debug:
                                print "before nm: f%s=%f" % (self.trial_solution[:-1], self.trial_solution[-1])

                            result = sherpa_neldermead(
                                self.fcn,
                                self.trial_solution[:-1],
                                xmin,
                                xmax,
                                ftol=tolerance,
                                maxfev=maxnfev - self.nfev[0],
                                initsimplex=0,
                                finalsimplex=[0, 1],
                                step=None,
                            )
                            model_par[:] = result[1]
                            fstat = result[2]

                            if debug:
                                print "after nm: f%s=%f" % (result[1], result[2])
                                print

                        else:
                            model_par[:] = self.trial_solution[:-1]
                            fstat = self.trial_solution[-1]

                        if self.check_convergence(polytope, tolerance):
                            return 0, model_par, fstat, self.nfev[0]

                if self.nfev[0] >= maxnfev:
                    3, model_par, fstat, self.nfev[0]

        return 3, model_par, fstat, self.nfev[0]
示例#4
0
    def __call__(self,
                 x,
                 xmin,
                 xmax,
                 maxnfev,
                 tolerance,
                 population_size,
                 xprob,
                 scale_factor,
                 seed=81547,
                 multicore=False,
                 verbose=None):

        debug = False
        use_local_opt = True

        x, self.xmin, self.xmax = self.check_args(x, xmin, xmax)
        self.fcn = func_bounds(self.fcn, xmin, xmax)
        polytope, fstat = self.init_population(x, xmin, xmax, population_size,
                                               seed)
        strategy_function = self.best1exp

        npar = len(x)
        npar_plus_1 = npar + 1

        self.trial_solution = numpy.zeros(npar_plus_1, dtype=numpy.float_)
        fct_vals = numpy.zeros(npar, dtype=numpy.float_)

        model_par = x[:]
        while self.nfev[0] < maxnfev:
            for candidate in xrange(population_size):

                self.trial_solution[:] = polytope[candidate][:]
                strategy_function(candidate, polytope, model_par, xprob,
                                  scale_factor)
                self.trial_solution[-1] = self.fcn(self.trial_solution[:-1])
                if self.trial_solution[-1] < polytope[candidate, -1]:
                    polytope[candidate][:] = self.trial_solution[:]

                    if self.trial_solution[npar] < fstat:

                        if use_local_opt:
                            if debug:
                                print 'before nm: f%s=%f' % \
                                      ( self.trial_solution[ :-1 ], \
                                        self.trial_solution[ -1 ] )

                            result = sherpa_neldermead(
                                self.fcn,
                                self.trial_solution[:-1],
                                xmin,
                                xmax,
                                ftol=tolerance,
                                maxfev=maxnfev - self.nfev[0],
                                initsimplex=0,
                                finalsimplex=[0, 1],
                                step=None)
                            model_par[:] = result[1]
                            fstat = result[2]

                            if debug:
                                print 'after nm: f%s=%f' % \
                                      ( result[ 1 ], result[ 2 ])
                                print

                        else:
                            model_par[:] = self.trial_solution[:-1]
                            fstat = self.trial_solution[-1]

                        if self.check_convergence(polytope, tolerance):
                            return 0, model_par, fstat, self.nfev[0]

                if self.nfev[0] >= maxnfev:
                    3, model_par, fstat, self.nfev[0]

        return 3, model_par, fstat, self.nfev[0]