esa / pagmo2

A C++ platform to perform parallel computations of optimisation tasks (global and local) via the asynchronous generalized island model.
https://esa.github.io/pagmo2/
GNU General Public License v3.0
808 stars 160 forks source link

[BUG] cmaes & xnes do not honor bounds #494

Open jschueller opened 2 years ago

jschueller commented 2 years ago

it seems cmaes (and xnes) does not care about the bounds of the problem as the final population ends up way outside eventhough the initial population is inside the bounds:

#include <pagmo/algorithms/cmaes.hpp>
#include <pagmo/population.hpp>
#include <pagmo/problems/rosenbrock.hpp>

using namespace pagmo;

struct problem1 {
    vector_double fitness(const vector_double &x) const
    {
        double x1=x[0];
        double x2=x[1];
        return {x2};
//         return {1.+100.*(x2-x1*x1)*(x2-x1*x1)+(1.-x1)*(1.-x1)};
    }
    std::pair<vector_double, vector_double> get_bounds() const
    {
        return {{-1.5, -1.5}, {1.5, 1.5}};
    }
    vector_double::size_type get_nobj() const
    {
        return 1;
    }
};

int main()
{
  try {
    problem prob{problem1{}};

    population pop1{prob, 0, 0};
    int size = 100;
    std::uniform_real_distribution<double> unif(-1.5, 1.5);
    std::default_random_engine re;
    for (int i=0; i<size; ++i)
    {
      vector_double x(2);
      x[0] = unif(re);
      x[1] = unif(re);
      pop1.push_back(x);
    }

    cmaes user_algo1{10u};
    pop1 = user_algo1.evolve(pop1);
    for (int i=0; i<size; ++i)
    {
      vector_double x = pop1.get_x()[i];
      std::cout << "x1="<<x[0]<<" x2="<<x[1] <<std::endl;
    }

    }
    catch(const std::exception & exc) {
        std::cout << "ex="<<exc.what() <<std::endl;
        return 2;
    }
    return 0;
}

...
x1=-1495.25 x2=-8833.04
x1=-731.242 x2=-4187.9
x1=-1317.33 x2=-8026.68
x1=-5534.71 x2=-34318.1
x1=1402.88 x2=8971.83
x1=970.03 x2=5018.52
bluescarni commented 2 years ago

I am going to ping @darioizzo about this.

But as far as I remember, in pagmo it is unspecified at this time if the bounds are always honoured strictly (i.e., it is algorithm-dependent). Some algorithms benefit from being able to explore the search space slightly outside the bounds, but I do not know if that is the case for cmaes/xnes.

jschueller commented 2 years ago

it seems force_bounds make take the bounds into account but the behavior is inconsistent with the other algorithms if it defaults to false