Exemplo n.º 1
0
    def __init__(self, func, xpar, xmin, xmax, npop, sfactor, xprob, step,
                 seed):
        Opt.__init__(self, func, xmin, xmax)
        self.ncores_nm = ncoresNelderMead()
        self.key2 = Key2()
        self.npop = min(npop, 4096)
        self.seed = seed
        self.strategies = \
            (Strategy0(self.func, self.npar, npop, sfactor, xprob),
             Strategy1(self.func, self.npar, npop, sfactor, xprob),
             Strategy2(self.func, self.npar, npop, sfactor, xprob),
             Strategy3(self.func, self.npar, npop, sfactor, xprob),
             Strategy4(self.func, self.npar, npop, sfactor, xprob),
             Strategy5(self.func, self.npar, npop, sfactor, xprob),
             Strategy6(self.func, self.npar, npop, sfactor, xprob),
             Strategy7(self.func, self.npar, npop, sfactor, xprob),
             Strategy8(self.func, self.npar, npop, sfactor, xprob),
             Strategy9(self.func, self.npar, npop, sfactor, xprob))

        xpar = numpy.asarray(xpar)
        if step is None:
            step = xpar * 1.2 + 1.2
        factor = 10
        self.polytope = \
            SimplexRandom(func, npop, xpar, xmin, xmax, step, seed, factor)
        self.local_opt = self.ncores_nm.algo
        return
Exemplo n.º 2
0
 def __call__(self, x, maxnfev, tol, step, finalsimplex, verbose):
     npar = len(x)
     factor = 2
     simplex = SimplexRandom(self.func, npar + 1, x, self.xmin,
                             self.xmax, None, None, factor)
     result = \
         self.optimize(x, simplex, maxnfev, tol, finalsimplex, verbose)
     # print('MyNelderMead7::__call__ result =', result)
     return result
Exemplo n.º 3
0
class MyDifEvo(Opt):

    def __init__(self, func, xpar, xmin, xmax, npop, sfactor, xprob, step,
                 seed):
        Opt.__init__(self, func, xmin, xmax)
        self.ncores_nm = ncoresNelderMead()
        self.key2 = Key2()
        self.npop = min(npop, 4096)
        self.seed = seed
        self.strategies = \
            (Strategy0(self.func, self.npar, npop, sfactor, xprob),
             Strategy1(self.func, self.npar, npop, sfactor, xprob),
             Strategy2(self.func, self.npar, npop, sfactor, xprob),
             Strategy3(self.func, self.npar, npop, sfactor, xprob),
             Strategy4(self.func, self.npar, npop, sfactor, xprob),
             Strategy5(self.func, self.npar, npop, sfactor, xprob),
             Strategy6(self.func, self.npar, npop, sfactor, xprob),
             Strategy7(self.func, self.npar, npop, sfactor, xprob),
             Strategy8(self.func, self.npar, npop, sfactor, xprob),
             Strategy9(self.func, self.npar, npop, sfactor, xprob))

        xpar = numpy.asarray(xpar)
        if step is None:
            step = xpar * 1.2 + 1.2
        factor = 10
        self.polytope = \
            SimplexRandom(func, npop, xpar, xmin, xmax, step, seed, factor)
        self.local_opt = self.ncores_nm.algo
        return

    def __call__(self, maxnfev, ftol):

        random.seed(self.seed)
        mypop = self.polytope
        npop_1 = self.npop - 1
        while self.nfev[0] < maxnfev:
            for pop_index in range(self.npop):
                key = self.calc_key([pop_index])
                # trial = self.all_strategies(pop_index)
                trial = self.all_strategies(key[0])
                if trial[-1] < mypop[npop_1][-1]:
                    mypop[npop_1] = trial[1:]
                    self.polytope.sort()

            if self.check_convergence(mypop, ftol, 0):
                break

        best_vertex = mypop[0]
        best_par = best_vertex[:-1]
        best_val = best_vertex[-1]
        return self.nfev[0], best_val, best_par

    def all_strategies(self, key):
        rand, index = self.key2.parse(key)
        random.seed(rand)
        mypop = self.polytope
        best_trial = self.strategies[0](mypop, index)
        for ii in range(1, len(self.strategies)):
            trial = self.strategies[ii](mypop, index)
            if trial[-1] < best_trial[-1]:
                best_trial = trial
        if best_trial[-1] < mypop[0][-1]:
            best_trial = self.apply_local_opt(best_trial, index)
        return best_trial

    def apply_local_opt(self, arg, index):
        local_opt = self.local_opt[index % len(self.local_opt)]
        result = local_opt(self.func, arg[1:-1], self.xmin, self.xmax)
        tmp = numpy.append(result[0], result[2])
        result = numpy.append(tmp, result[1])
        return result

    def calc_key(self, indices, start=0, end=65536):
        result = numpy.empty(len(indices), dtype=numpy.int64)
        for ii, index in enumerate(indices):
            rand = random.randint(start, end)
            result[ii] = self.key2.calc(rand, index)
        return result

    def check_convergence(self, mypop, ftol, npar):
        fval_std = numpy.std([col[-1] for col in mypop])
        if fval_std < ftol:
            return True
        return False