Esempio n. 1
0
def test_rosen_cpp_parallel():
    popsize = 8
    dim = 2
    testfun = Rosen(dim)
    sdevs = [1.0] * dim
    max_eval = 10000

    limit = 0.00001
    for _ in range(5):
        wrapper = Wrapper(testfun.fun, dim)
        ret = cmaescpp.minimize(wrapper.eval,
                                testfun.bounds,
                                input_sigma=sdevs,
                                max_evaluations=max_eval,
                                popsize=popsize,
                                workers=popsize)
        if limit > ret.fun:
            break

    assert (limit > ret.fun)  # optimization target not reached
    assert (max_eval + popsize >= ret.nfev)  # too much function calls
    assert (max_eval // popsize + 2 > ret.nit)  # too much iterations
    assert (almost_equal(ret.x, wrapper.get_best_x()))  # wrong best X returned
    assert (almost_equal(ret.fun, wrapper.get_best_y(),
                         eps=1E-1))  # wrong best y returned
Esempio n. 2
0
 def cma_cpp(self, fun, guess, bounds, sdevs, rg):
     """CMA_ES C++ implementation."""
     ret = cmaescpp.minimize(fun, bounds, guess,
             input_sigma=sdevs, max_evaluations=self.store.eval_num(), 
             popsize=self.popsize, stop_fittness = self.stop_fittness,
             rg=rg, runid=self.store.get_count_runs())     
     return ret.x, ret.fun, ret.nfev
Esempio n. 3
0
def test_rosen_cpp():
    if not sys.platform.startswith('linux'):
        return
    popsize = 31
    dim = 5
    testfun = Rosen(dim)
    sdevs = [1.0] * dim
    max_eval = 100000

    limit = 0.00001
    for i in range(5):
        wrapper = Wrapper(testfun.func, dim)
        ret = cmaescpp.minimize(wrapper.eval,
                                testfun.bounds,
                                input_sigma=sdevs,
                                max_evaluations=max_eval,
                                popsize=popsize)
        if limit > ret.fun:
            break

    assert (limit > ret.fun)  # optimization target not reached
    assert (max_eval + popsize > ret.nfev)  # too much function calls
    assert (ret.nfev == wrapper.get_count()
            )  # wrong number of function calls returned
    assert (almost_equal(ret.x, wrapper.get_best_x()))  # wrong best X returned
    assert (ret.fun == wrapper.get_best_y())  # wrong best y returned
Esempio n. 4
0
def test_cma_cpp(problem, num):
    best = math.inf
    t0 = time.perf_counter()
    for i in range(num):
        ret = cmaescpp.minimize(problem.fun, bounds=problem.bounds)
        best = min(ret.fun, best)
        print("{0}: time = {1:.1f} best = {2:.1f} f(xmin) = {3:.1f}".format(
            i + 1, dtime(t0), best, ret.fun))
Esempio n. 5
0
def test_cma_cpp(problem, num):
    best = math.inf
    t0 = time.perf_counter();
    for i in range(num):
        ret = cmaescpp.minimize(problem.fun, max_evaluations = 100000, bounds = problem.bounds)
        if best > ret.fun or i % 100 == 99:
            print("{0}: time = {1:.1f} best = {2:.1f} f(xmin) = {3:.1f}"
              .format(i+1, dtime(t0), best, ret.fun))
        best = min(ret.fun, best)
Esempio n. 6
0
 def minimize(self,
              fun,
              bounds,
              guess=None,
              sdevs=0.3,
              rg=Generator(MT19937()),
              store=None,
              workers=None):
     ret = cmaescpp.minimize(
         fun,
         bounds,
         self.guess if not self.guess is None else guess,
         input_sigma=self.sdevs if not self.sdevs is None else sdevs,
         max_evaluations=self.max_eval_num(store),
         popsize=self.popsize,
         stop_fitness=self.stop_fitness,
         rg=rg,
         runid=self.get_count_runs(store),
         update_gap=self.update_gap,
         workers=self.workers if workers is None else workers)
     return ret.x, ret.fun, ret.nfev