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] gaco::pheromone_computation crashes #490

Closed jschueller closed 2 years ago

jschueller commented 2 years ago

the following sample makes gaco crash:

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

using namespace pagmo;

int main()
{
        problem prob{rosenbrock{2u}};
        population pop1{prob, 1u, 23u};
        gaco user_algo1{3u, 1u, 1.0, 1e9, 0.01, 0, 7u, 1000u, 1000u, 0.0, false, 23u};
        pop1 = user_algo1.evolve(pop1);
}

under gdb:

   Gen:        Fevals:          Best:        Kernel:        Oracle:            dx:            dp:
      1              0          76143              1          1e+09              0              0
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1042: std::vector::reference std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >::operator[](std::vector::size_type) [_Tp = std::vector<double, std::allocator<double> >, _Alloc = std::allocator<std::vector<double, std::allocator<double> > >]: Assertion '__builtin_expect(__n < this->size(), true)' failed.

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff671f859 in __GI_abort () at abort.c:79
#2  0x00007ffff7004592 in pagmo::gaco::pheromone_computation (this=<optimized out>, gen=<optimized out>, prob_cumulative=<error reading variable: Cannot access memory at address 0x8>, omega_vec=..., 
    sigma_vec=std::vector of length 2, capacity 2 = {...}, popul=..., sol_archive=...) at /home/schueller/projects/pagmo2/src/algorithms/gaco.cpp:784
#3  0x00007ffff6ffff98 in pagmo::gaco::evolve (this=<optimized out>, pop=...) at /home/schueller/projects/pagmo2/src/algorithms/gaco.cpp:273
#4  0x0000000000402454 in main () at /home/schueller/projects/pagmo2/tests/gaco.cpp:68

to reproduce

ubuntu 20.04, gcc 9.3
CMAKE_CXX_FLAGS:STRING=-Wall -Wextra -D_GLIBCXX_ASSERTIONS -Og
bluescarni commented 2 years ago

@darioizzo would you like to take a look at this?

bluescarni commented 2 years ago

@Sceki as well, since IIRC he is the original author.

Sceki commented 2 years ago

I believe that if the memory parameter is set to false, then your n_gen_mark parameter should always be smaller than the gen size and different than zero. This should throw an exception on C++ (it does on python, see below). I will investigate.

import pygmo as pg
prob = pg.problem(pg.rosenbrock(2))
pop = pg.population(prob,size = 1,seed = 23)
uda = pg.gaco(gen = 3, ker = 1, q = 1.0, oracle = 1e9, acc = 0.01, threshold = 0,  n_gen_mark = 7, impstop = 1000, evalstop = 1000, focus = 0.0, memory = False, seed = 23)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/f3/zsqnptzj5wb532m1qfccgbz80000gn/T/ipykernel_6499/554914302.py in <module>
      2 
      3 pop = pg.population(prob,size = 1,seed = 23)
----> 4 uda = pg.gaco(gen = 3, ker = 1, q = 1.0, oracle = 1e9, acc = 0.01, threshold = 0,  n_gen_mark = 7, impstop = 1000, evalstop = 1000, focus = 0.0, memory = False, seed = 23)
      5 

ValueError: 
function: gaco
where: /Users/runner/miniforge3/conda-bld/pagmo_1627978948198/work/src/algorithms/gaco.cpp, 78
what: If memory is inactive, the threshold parameter must be either in [1,m_gen] while a value of 0 was detected
bluescarni commented 2 years ago

I believe that if the memory parameter is set to false, then your n_gen_mark parameter should always be smaller than the gen size. This should throw an exception on C++ (it does on python, see below). I will investigate.

import pygmo as pg
prob = pg.problem(pg.rosenbrock(2))
pop = pg.population(prob,size = 1,seed = 23)
uda = pg.gaco(gen = 3, ker = 1, q = 1.0, oracle = 1e9, acc = 0.01, threshold = 0,  n_gen_mark = 7, impstop = 1000, evalstop = 1000, focus = 0.0, memory = False, seed = 23)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/f3/zsqnptzj5wb532m1qfccgbz80000gn/T/ipykernel_6499/554914302.py in <module>
      2 
      3 pop = pg.population(prob,size = 1,seed = 23)
----> 4 uda = pg.gaco(gen = 3, ker = 1, q = 1.0, oracle = 1e9, acc = 0.01, threshold = 0,  n_gen_mark = 7, impstop = 1000, evalstop = 1000, focus = 0.0, memory = False, seed = 23)
      5 

ValueError: 
function: gaco
where: /Users/runner/miniforge3/conda-bld/pagmo_1627978948198/work/src/algorithms/gaco.cpp, 78
what: If memory is inactive, the threshold parameter must be either in [1,m_gen] while a value of 0 was detected

Thanks!

Sceki commented 2 years ago

So, I have tried on C++ as well and I get the correct exception:

function: gaco
where: /Users/runner/miniforge3/conda-bld/pagmo_1627978948198/work/src/algorithms/gaco.cpp, 78
what: If memory is inactive, the threshold parameter must be either in [1,m_gen] while a value of 0 was detected

@jschueller I hence cannot reproduce your error

jschueller commented 2 years ago

indeed its not a crash, sorry

jschueller commented 2 years ago

Turns out the crash happened with other values to default:

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

using namespace pagmo;

int main()
{
    try {
        problem prob{rosenbrock{2u}};
        population pop1{prob, 8u, 23u};
        gaco user_algo1{10u, 1u};
        pop1 = user_algo1.evolve(pop1);
    }
    catch(const std::exception & exc) {
            std::cout << "ex="<<exc.what() <<std::endl;
                return 2;
        }
        return 0;
}
Sceki commented 2 years ago

@jschueller You did not paste the log of your error: but I guess it is related to the ker value: it should be between 2 (included) and the population size (in your case 8). I believe this is indeed a bug as it is not checked and thrown immediately. I will make a PR, thanks for spotting this!