Top Tutorial 2 Tutorial 4

Tutorial 3: How to create an application using a multi-threaded Evolutionary Algorithm

This tutorial demonstrates the setup of a user defined cost function and its use in a multi-threaded EA application using Differential Evolution.
#include <sl/optimizer/generic_reactor.h>
#include <sl/optimizer/generic_global_optimizer.h>

template< class T >
struct global_optimizer_reactor : public sl::reactor_base< T > {
    typedef typename sl::reactor_base< T > base_type;

    global_optimizer_reactor() {}
    global_optimizer_reactor(unsigned dim) : base_type(dim) {}
    
    template< class T_ITERATOR >
    T operator()(const T_ITERATOR &ibegin,
                 const T_ITERATOR &iend) {
        T sum = 0;

        base_type::pre_process(ibegin, iend);
        for (unsigned i0 = 0; i0 < base_type::_x.size(); i0++) {
            sum += std::pow(base_type::_x [i0], 2);
        }
        return sum;
    }
};

int main() {
    std::mt19937 engine(time(NULL));
    int nthreads = 2, space_dim = 2, swarm_size = 20, max_it = 200, max_f_eval = 2000;
    double error_threshold = 1e-6;

    ui::go_console_ui cui;
    sl::optimizer_core_mt optimizer_core_mt;
    sl::go_operator< double, double > go(engine, space_dim, swarm_size, max_it, max_f_eval, error_threshold, 0, nthreads);

    sl::vector< global_optimizer_reactor< double > > reactors(2, global_optimizer_reactor< double >(space_dim));
    sl::vector< sl::propagator::DE< double > > propagators(2);

    cui.init();
    for (unsigned i0 = 0; i0 < reactors.size(); i0++) {
        reactors[i0].prior_range(-5,5);
    }

    sl::optimize(go, reactors, propagators, optimizer_core_mt, go.prior_sampler(), cui);
}
This code is from the file 'ea-example-mt.cpp' in the mcmll/examples directory. Switch to the mcmll/examples directory and compile all examples by the command 'scons', or alternatively, compile it with:
  g++ ea-example-mt.cpp  -I path-to-mcmll/lib -std=c++0x -D__SL_USE_CXX0X__ -o ea-example-mt
where path-to-mcmll is the installation path of the MCMLL library (for example: /home/me/cpp/mcmll). Run example with:
  ./ea-example-mt

Explanation of the code