test support for Eigen implementations
This commit is contained in:
parent
661ef08e44
commit
f0564c233e
2 changed files with 146 additions and 203 deletions
|
|
@ -40,22 +40,29 @@ typedef eoReal< eoMinimizingFitness > EOT;
|
||||||
typedef edoNormalMulti< EOT > Distrib;
|
typedef edoNormalMulti< EOT > Distrib;
|
||||||
typedef EOT::AtomType AtomType;
|
typedef EOT::AtomType AtomType;
|
||||||
|
|
||||||
|
#ifdef WITH_BOOST
|
||||||
|
#include <boost/numeric/ublas/vector.hpp>
|
||||||
|
#include <boost/numeric/ublas/symmetric.hpp>
|
||||||
|
typedef ublas::vector< AtomType > Vector;
|
||||||
|
typedef ublas::symmetric_matrix< AtomType, ublas::lower > Matrix;
|
||||||
|
#else
|
||||||
|
#ifdef WITH_EIGEN
|
||||||
|
#include <Eigen/Dense>
|
||||||
|
typedef Eigen::Matrix< AtomType, Eigen::Dynamic, 1> Vector;
|
||||||
|
typedef Eigen::Matrix< AtomType, Eigen::Dynamic, Eigen::Dynamic> Matrix;
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
int main(int ac, char** av)
|
int main(int ac, char** av)
|
||||||
{
|
{
|
||||||
//-----------------------------------------------------
|
|
||||||
// (0) parser + eo routines
|
// (0) parser + eo routines
|
||||||
//-----------------------------------------------------
|
|
||||||
|
|
||||||
eoParser parser(ac, av);
|
eoParser parser(ac, av);
|
||||||
|
|
||||||
std::string section("Algorithm parameters");
|
std::string section("Algorithm parameters");
|
||||||
|
|
||||||
unsigned int p_size = parser.createParam((unsigned int)100, "popSize", "Population Size", 'P', section).value(); // P
|
unsigned int p_size = parser.createParam((unsigned int)100, "popSize", "Population Size", 'P', section).value(); // P
|
||||||
|
|
||||||
unsigned int s_size = parser.createParam((unsigned int)2, "dimension-size", "Dimension size", 'd', section).value(); // d
|
unsigned int s_size = parser.createParam((unsigned int)2, "dimension-size", "Dimension size", 'd', section).value(); // d
|
||||||
|
|
||||||
AtomType mean_value = parser.createParam((AtomType)0, "mean", "Mean value", 'm', section).value(); // m
|
AtomType mean_value = parser.createParam((AtomType)0, "mean", "Mean value", 'm', section).value(); // m
|
||||||
|
|
||||||
AtomType covar1_value = parser.createParam((AtomType)1.0, "covar1", "Covar value 1", '1', section).value();
|
AtomType covar1_value = parser.createParam((AtomType)1.0, "covar1", "Covar value 1", '1', section).value();
|
||||||
AtomType covar2_value = parser.createParam((AtomType)0.5, "covar2", "Covar value 2", '2', section).value();
|
AtomType covar2_value = parser.createParam((AtomType)0.5, "covar2", "Covar value 2", '2', section).value();
|
||||||
AtomType covar3_value = parser.createParam((AtomType)1.0, "covar3", "Covar value 3", '3', section).value();
|
AtomType covar3_value = parser.createParam((AtomType)1.0, "covar3", "Covar value 3", '3', section).value();
|
||||||
|
|
@ -66,8 +73,7 @@ int main(int ac, char** av)
|
||||||
<< covar3_value << "_gen";
|
<< covar3_value << "_gen";
|
||||||
std::string gen_filename = ss.str();
|
std::string gen_filename = ss.str();
|
||||||
|
|
||||||
if (parser.userNeedsHelp())
|
if( parser.userNeedsHelp() ) {
|
||||||
{
|
|
||||||
parser.printHelp(std::cout);
|
parser.printHelp(std::cout);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
@ -75,20 +81,12 @@ int main(int ac, char** av)
|
||||||
make_verbose(parser);
|
make_verbose(parser);
|
||||||
make_help(parser);
|
make_help(parser);
|
||||||
|
|
||||||
|
|
||||||
assert(p_size > 0);
|
assert(p_size > 0);
|
||||||
assert(s_size > 0);
|
assert(s_size > 0);
|
||||||
|
|
||||||
|
|
||||||
eoState state;
|
eoState state;
|
||||||
|
|
||||||
//-----------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------
|
|
||||||
// (1) Population init and sampler
|
// (1) Population init and sampler
|
||||||
//-----------------------------------------------------
|
|
||||||
|
|
||||||
eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
|
eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
|
||||||
state.storeFunctor(gen);
|
state.storeFunctor(gen);
|
||||||
|
|
||||||
|
|
@ -99,18 +97,14 @@ int main(int ac, char** av)
|
||||||
// fill population thanks to eoInit instance
|
// fill population thanks to eoInit instance
|
||||||
eoPop< EOT >& pop = state.takeOwnership( eoPop< EOT >( p_size, *init ) );
|
eoPop< EOT >& pop = state.takeOwnership( eoPop< EOT >( p_size, *init ) );
|
||||||
|
|
||||||
//-----------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// (2) distribution initial parameters
|
// (2) distribution initial parameters
|
||||||
//-----------------------------------------------------------------------------
|
Vector mean( s_size );
|
||||||
|
|
||||||
ublas::vector< AtomType > mean( s_size );
|
for (unsigned int i = 0; i < s_size; ++i) {
|
||||||
|
mean( i ) = mean_value;
|
||||||
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < s_size; ++i) { mean( i ) = mean_value; }
|
Matrix varcovar( s_size, s_size );
|
||||||
|
|
||||||
ublas::symmetric_matrix< AtomType, ublas::lower > varcovar( s_size, s_size );
|
|
||||||
|
|
||||||
varcovar( 0, 0 ) = covar1_value;
|
varcovar( 0, 0 ) = covar1_value;
|
||||||
varcovar( 0, 1 ) = covar2_value;
|
varcovar( 0, 1 ) = covar2_value;
|
||||||
|
|
@ -118,13 +112,7 @@ int main(int ac, char** av)
|
||||||
|
|
||||||
Distrib distrib( mean, varcovar );
|
Distrib distrib( mean, varcovar );
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// (3a) distribution output preparation
|
// (3a) distribution output preparation
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
edoDummyContinue< Distrib >* distrib_dummy_continue = new edoDummyContinue< Distrib >();
|
edoDummyContinue< Distrib >* distrib_dummy_continue = new edoDummyContinue< Distrib >();
|
||||||
state.storeFunctor(distrib_dummy_continue);
|
state.storeFunctor(distrib_dummy_continue);
|
||||||
|
|
||||||
|
|
@ -141,60 +129,29 @@ int main(int ac, char** av)
|
||||||
distrib_file_snapshot->add(*distrib_stat);
|
distrib_file_snapshot->add(*distrib_stat);
|
||||||
distrib_continue->add(*distrib_file_snapshot);
|
distrib_continue->add(*distrib_file_snapshot);
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// (3b) distribution output
|
// (3b) distribution output
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
(*distrib_continue)( distrib );
|
(*distrib_continue)( distrib );
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Prepare bounder class to set bounds of sampling.
|
// Prepare bounder class to set bounds of sampling.
|
||||||
// This is used by edoSampler.
|
// This is used by edoSampler.
|
||||||
//-----------------------------------------------------------------------------
|
edoBounder< EOT >* bounder = new edoBounderRng< EOT >(
|
||||||
|
EOT(pop[0].size(), -5), EOT(pop[0].size(), 5), *gen
|
||||||
edoBounder< EOT >* bounder = new edoBounderRng< EOT >(EOT(pop[0].size(), -5),
|
);
|
||||||
EOT(pop[0].size(), 5),
|
|
||||||
*gen);
|
|
||||||
state.storeFunctor(bounder);
|
state.storeFunctor(bounder);
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Prepare sampler class with a specific distribution
|
// Prepare sampler class with a specific distribution
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
edoSampler< Distrib >* sampler = new edoSamplerNormalMulti< EOT >( *bounder );
|
edoSampler< Distrib >* sampler = new edoSamplerNormalMulti< EOT >( *bounder );
|
||||||
state.storeFunctor(sampler);
|
state.storeFunctor(sampler);
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// (4) sampling phase
|
// (4) sampling phase
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
pop.clear();
|
pop.clear();
|
||||||
|
|
||||||
for (unsigned int i = 0; i < p_size; ++i)
|
for( unsigned int i = 0; i < p_size; ++i ) {
|
||||||
{
|
|
||||||
EOT candidate_solution = (*sampler)( distrib );
|
EOT candidate_solution = (*sampler)( distrib );
|
||||||
pop.push_back( candidate_solution );
|
pop.push_back( candidate_solution );
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// (5) population output
|
// (5) population output
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
eoContinue< EOT >* pop_cont = new eoGenContinue< EOT >( 2 ); // never reached fitness
|
eoContinue< EOT >* pop_cont = new eoGenContinue< EOT >( 2 ); // never reached fitness
|
||||||
state.storeFunctor(pop_cont);
|
state.storeFunctor(pop_cont);
|
||||||
|
|
||||||
|
|
@ -212,41 +169,21 @@ int main(int ac, char** av)
|
||||||
|
|
||||||
(*pop_continue)( pop );
|
(*pop_continue)( pop );
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// (6) estimation phase
|
// (6) estimation phase
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
edoEstimator< Distrib >* estimator = new edoEstimatorNormalMulti< EOT >();
|
edoEstimator< Distrib >* estimator = new edoEstimatorNormalMulti< EOT >();
|
||||||
state.storeFunctor(estimator);
|
state.storeFunctor(estimator);
|
||||||
|
|
||||||
distrib = (*estimator)( pop );
|
distrib = (*estimator)( pop );
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// (7) distribution output
|
// (7) distribution output
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
(*distrib_continue)( distrib );
|
(*distrib_continue)( distrib );
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// (8) euclidianne distance estimation
|
// (8) euclidianne distance estimation
|
||||||
//-----------------------------------------------------------------------------
|
Vector new_mean = distrib.mean();
|
||||||
|
Matrix new_varcovar = distrib.varcovar();
|
||||||
ublas::vector< AtomType > new_mean = distrib.mean();
|
|
||||||
ublas::symmetric_matrix< AtomType, ublas::lower > new_varcovar = distrib.varcovar();
|
|
||||||
|
|
||||||
AtomType distance = 0;
|
AtomType distance = 0;
|
||||||
|
for( unsigned int d = 0; d < s_size; ++d ) {
|
||||||
for ( unsigned int d = 0; d < s_size; ++d )
|
|
||||||
{
|
|
||||||
distance += pow( mean[ d ] - new_mean[ d ], 2 );
|
distance += pow( mean[ d ] - new_mean[ d ], 2 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -258,7 +195,5 @@ int main(int ac, char** av)
|
||||||
<< "distance: " << distance << std::endl
|
<< "distance: " << distance << std::endl
|
||||||
;
|
;
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,6 @@ Authors:
|
||||||
|
|
||||||
#include <edo>
|
#include <edo>
|
||||||
|
|
||||||
#include <boost/numeric/ublas/vector.hpp>
|
|
||||||
#include <boost/numeric/ublas/symmetric.hpp>
|
|
||||||
|
|
||||||
#include "Rosenbrock.h"
|
#include "Rosenbrock.h"
|
||||||
#include "Sphere.h"
|
#include "Sphere.h"
|
||||||
|
|
@ -47,12 +45,22 @@ typedef eoReal< eoMinimizingFitness > EOT;
|
||||||
typedef edoNormalMulti< EOT > Distrib;
|
typedef edoNormalMulti< EOT > Distrib;
|
||||||
typedef EOT::AtomType AtomType;
|
typedef EOT::AtomType AtomType;
|
||||||
|
|
||||||
|
#ifdef WITH_BOOST
|
||||||
|
#include <boost/numeric/ublas/vector.hpp>
|
||||||
|
#include <boost/numeric/ublas/symmetric.hpp>
|
||||||
|
typedef ublas::vector< AtomType > Vector;
|
||||||
|
typedef ublas::symmetric_matrix< AtomType, ublas::lower > Matrix;
|
||||||
|
#else
|
||||||
|
#ifdef WITH_EIGEN
|
||||||
|
#include <Eigen/Dense>
|
||||||
|
typedef Eigen::Matrix< AtomType, Eigen::Dynamic, 1> Vector;
|
||||||
|
typedef Eigen::Matrix< AtomType, Eigen::Dynamic, Eigen::Dynamic> Matrix;
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
int main(int ac, char** av)
|
int main(int ac, char** av)
|
||||||
{
|
{
|
||||||
//-----------------------------------------------------
|
|
||||||
// (0) parser + eo routines
|
// (0) parser + eo routines
|
||||||
//-----------------------------------------------------
|
|
||||||
|
|
||||||
eoParser parser(ac, av);
|
eoParser parser(ac, av);
|
||||||
|
|
||||||
std::string section("Algorithm parameters");
|
std::string section("Algorithm parameters");
|
||||||
|
|
@ -80,7 +88,7 @@ int main(int ac, char** av)
|
||||||
make_verbose(parser);
|
make_verbose(parser);
|
||||||
make_help(parser);
|
make_help(parser);
|
||||||
|
|
||||||
//-----------------------------------------------------
|
|
||||||
|
|
||||||
assert(r_max >= 1);
|
assert(r_max >= 1);
|
||||||
assert(s_size >= 2);
|
assert(s_size >= 2);
|
||||||
|
|
@ -113,9 +121,9 @@ int main(int ac, char** av)
|
||||||
eoState state;
|
eoState state;
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------
|
|
||||||
// (1) Population init and sampler
|
// (1) Population init and sampler
|
||||||
//-----------------------------------------------------
|
|
||||||
|
|
||||||
eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
|
eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
|
||||||
state.storeFunctor(gen);
|
state.storeFunctor(gen);
|
||||||
|
|
@ -127,15 +135,15 @@ int main(int ac, char** av)
|
||||||
// fill population thanks to eoInit instance
|
// fill population thanks to eoInit instance
|
||||||
eoPop< EOT >& pop = state.takeOwnership( eoPop< EOT >( p_size, *init ) );
|
eoPop< EOT >& pop = state.takeOwnership( eoPop< EOT >( p_size, *init ) );
|
||||||
|
|
||||||
//-----------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// (2) distribution initial parameters
|
// (2) distribution initial parameters
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
ublas::vector< AtomType > mean( s_size, mean_value );
|
|
||||||
ublas::symmetric_matrix< AtomType, ublas::lower > varcovar( s_size, s_size );
|
Vector mean( s_size, mean_value );
|
||||||
|
Matrix varcovar( s_size, s_size );
|
||||||
|
|
||||||
varcovar( 0, 0 ) = covar1_value;
|
varcovar( 0, 0 ) = covar1_value;
|
||||||
varcovar( 0, 1 ) = covar2_value;
|
varcovar( 0, 1 ) = covar2_value;
|
||||||
|
|
@ -143,35 +151,35 @@ int main(int ac, char** av)
|
||||||
|
|
||||||
Distrib distrib( mean, varcovar );
|
Distrib distrib( mean, varcovar );
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Prepare bounder class to set bounds of sampling.
|
// Prepare bounder class to set bounds of sampling.
|
||||||
// This is used by edoSampler.
|
// This is used by edoSampler.
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
edoBounder< EOT >* bounder = new edoBounderRng< EOT >(EOT(pop[0].size(), -5),
|
edoBounder< EOT >* bounder = new edoBounderRng< EOT >(EOT(pop[0].size(), -5),
|
||||||
EOT(pop[0].size(), 5),
|
EOT(pop[0].size(), 5),
|
||||||
*gen);
|
*gen);
|
||||||
state.storeFunctor(bounder);
|
state.storeFunctor(bounder);
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Prepare sampler class with a specific distribution
|
// Prepare sampler class with a specific distribution
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
edoSampler< Distrib >* sampler = new edoSamplerNormalMulti< EOT >( *bounder );
|
edoSampler< Distrib >* sampler = new edoSamplerNormalMulti< EOT >( *bounder );
|
||||||
state.storeFunctor(sampler);
|
state.storeFunctor(sampler);
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// (4) sampling phase
|
// (4) sampling phase
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
pop.clear();
|
pop.clear();
|
||||||
|
|
||||||
|
|
@ -181,27 +189,27 @@ int main(int ac, char** av)
|
||||||
pop.push_back( candidate_solution );
|
pop.push_back( candidate_solution );
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// (6) estimation phase
|
// (6) estimation phase
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
edoEstimator< Distrib >* estimator = new edoEstimatorNormalMulti< EOT >();
|
edoEstimator< Distrib >* estimator = new edoEstimatorNormalMulti< EOT >();
|
||||||
state.storeFunctor(estimator);
|
state.storeFunctor(estimator);
|
||||||
|
|
||||||
distrib = (*estimator)( pop );
|
distrib = (*estimator)( pop );
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// (8) euclidianne distance estimation
|
// (8) euclidianne distance estimation
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
ublas::vector< AtomType > new_mean = distrib.mean();
|
|
||||||
ublas::symmetric_matrix< AtomType, ublas::lower > new_varcovar = distrib.varcovar();
|
Vector new_mean = distrib.mean();
|
||||||
|
Matrix new_varcovar = distrib.varcovar();
|
||||||
|
|
||||||
AtomType distance = 0;
|
AtomType distance = 0;
|
||||||
|
|
||||||
|
|
@ -218,7 +226,7 @@ int main(int ac, char** av)
|
||||||
<< distance << std::endl
|
<< distance << std::endl
|
||||||
;
|
;
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue