def test_rosen_python(): popsize = 31 dim = 5 testfun = Rosen(dim) sdevs = [1.0] * dim max_eval = 100000 limit = 0.00001 for _ in range(5): wrapper = Wrapper(testfun.fun, dim) ret = cmaes.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 (max_eval / popsize + 2 > ret.nit) # too much iterations 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
def test_rosen_ask_tell(): popsize = 31 dim = 5 testfun = Rosen(dim) sdevs = [1.0] * dim max_eval = 100000 limit = 0.00001 for _ in range(5): wrapper = Wrapper(testfun.fun, dim) es = cmaes.Cmaes(testfun.bounds, popsize=popsize, input_sigma=sdevs) iters = max_eval // popsize for j in range(iters): xs = es.ask() ys = [wrapper.eval(x) for x in xs] stop = es.tell(ys) if stop != 0: break ret = OptimizeResult(x=es.best_x, fun=es.best_value, nfev=wrapper.get_count(), nit=es.iterations, status=es.stop) 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
def test_rosen_ask_tell_de(): popsize = 8 dim = 2 testfun = Rosen(dim) max_eval = 10000 limit = 0.00001 for _ in range(5): wrapper = Wrapper(testfun.fun, dim) es = de.DE(dim, testfun.bounds, popsize=popsize) iters = max_eval // popsize for j in range(iters): xs = es.ask() ys = [wrapper.eval(x) for x in xs] stop = es.tell(ys, xs) if stop != 0: break ret = OptimizeResult(x=es.best_x, fun=es.best_value, nfev=wrapper.get_count(), nit=es.iterations, status=es.stop) if limit > ret.fun: break assert (limit > ret.fun) # optimization target not reached assert (max_eval + 2 * 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
def test_rosen_decpp_parallel(): popsize = 8 dim = 2 testfun = Rosen(dim) max_eval = 10000 limit = 0.01 for _ in range(5): wrapper = Wrapper(testfun.fun, dim) ret = decpp.minimize(wrapper.eval, dim, testfun.bounds, 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(ret.nfev == wrapper.get_count()) # wrong number of function calls returned assert (almost_equal(ret.x, wrapper.get_best_x(), eps=1E-2)) # wrong best X returned assert (almost_equal(ret.fun, wrapper.get_best_y(), eps=1E-2)) # wrong best y returned
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
def test_rosen_parallel(): # parallel execution slows down the test since we are using a test function # which is very fast to evaluate # popsize defines the maximal number of used threads #windows cannot pickle function objects if sys.platform.startswith('windows'): return 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 = cmaes.minimize(wrapper.eval, testfun.bounds, input_sigma = sdevs, max_evaluations = max_eval, popsize=popsize, is_parallel=True) 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(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
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
def test_rosen_de_delayed(): popsize = 8 dim = 2 testfun = Rosen(dim) max_eval = 10000 limit = 0.01 for _ in range(5): wrapper = Wrapper(testfun.fun, dim) ret = de.minimize(wrapper.eval, dim, testfun.bounds, 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 (ret.nfev == wrapper.get_count() ) # wrong number of function calls returned
def test_rosen_delayed(): 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 = cmaes.minimize(wrapper.eval, testfun.bounds, input_sigma=sdevs, max_evaluations=max_eval, popsize=popsize, workers=popsize, delayed_update=True) 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