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_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)
Exemple #4
0
    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])
            F[i, :] = objective.feedback(X[i])
            cmaes.set_evaluation_feedback(F[i])

        current_path = np.array(path)
        plt.plot(current_path[:, 0], current_path[:, 1], "o-", c="y", alpha=0.2)

        weights = np.zeros(n_samples_per_update)
        weights[np.argsort(F.ravel())[::-1][:len(cmaes.weights)]] = cmaes.weights
        plt.scatter(X[:, 0], X[:, 1], c=weights, cmap=plt.cm.gray)
        plt.scatter(objective.x_opt[0], objective.x_opt[1], s=100, color="r")

        plot_ellipse(cov=last_cov, mean=last_mean, color="orange")
        plot_ellipse(cov=cmaes.var * cmaes.cov, mean=cmaes.mean, color="green")
    plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
plt.show()