def test_cmaes_get_best_params_best(): opt = CMAESOptimizer() opt.init(10) params = np.empty(10) opt.get_next_parameters(params) opt.set_evaluation_feedback(np.array([0.0])) best_params = opt.get_best_parameters(method="best") assert_array_almost_equal(params, best_params)
def test_cmaes_respects_bounds(): opt = CMAESOptimizer(bounds=[[-5, 4], [10, 20]], variance=10000.0, random_state=0) opt.init(2) params = np.empty(2) opt.get_next_parameters(params) assert_true(np.all(params >= np.array([-5, 10]))) assert_true(np.all(params <= np.array([4, 20])))
def test_cmaes_stop_fitness_variance(): opt = CMAESOptimizer(n_samples_per_update=5) opt.init(2) params = np.empty(2) it = 0 while not opt.is_behavior_learning_done(): opt.get_next_parameters(params) opt.set_evaluation_feedback([0.0]) it += 1 assert_equal(it, 6)
def test_cmaes_stop_conditioning(): def objective(x): return -1e10 * x[1] ** 2 opt = CMAESOptimizer(random_state=0) opt.init(2) params = np.empty(2) it = 0 while not opt.is_behavior_learning_done(): opt.get_next_parameters(params) opt.set_evaluation_feedback(objective(params)) it += 1 assert_less(it, 600)
def test_acmaes(): x = np.zeros(n_dims) opt = CMAESOptimizer(x, active=True, random_state=0, log_to_stdout=False) opt.init(n_dims) r = eval_loop(x, opt, n_dims) assert_greater(r.max(), -1e-5)
n_generations = 20 n_samples_per_update = 20 n_params = 2 for objective_name in ["Sphere", "SchaffersF7"]: objective = FUNCTIONS[objective_name](0, n_params) initial_params = 4.0 * np.ones(n_params) cmaes = CMAESOptimizer( initial_params=initial_params, variance=0.1, active=True, n_samples_per_update=n_samples_per_update, bounds=np.array([[-5, 5], [-5, 5]]), random_state=0) cmaes.init(n_params) n_rows = 4 plt.figure(figsize=(n_generations * 3 / n_rows, 3 * n_rows)) path = [] for it in range(n_generations): plt.subplot(n_rows, int(n_generations / n_rows, it + 1)) plot_objective() last_mean = cmaes.mean.copy() path.append(last_mean) last_cov = cmaes.var * cmaes.cov X = np.empty((n_samples_per_update, n_params)) F = np.empty((n_samples_per_update, 1)) for i in range(n_samples_per_update): cmaes.get_next_parameters(X[i])
def test_cmaes_no_initial_params(): opt = CMAESOptimizer() opt.init(10) params = np.empty(10) opt.get_next_parameters(params)
def test_cmaes_diagonal_cov(): opt = CMAESOptimizer(covariance=np.zeros(10)) opt.init(10) params = np.empty(10) opt.get_next_parameters(params)