t-edoEstimatorNormalMulti.cpp
00001 /*
00002 The Evolving Distribution Objects framework (EDO) is a template-based,
00003 ANSI-C++ evolutionary computation library which helps you to write your
00004 own estimation of distribution algorithms.
00005 
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public
00017 License along with this library; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 Copyright (C) 2010 Thales group
00021 */
00022 /*
00023 Authors:
00024     Johann Dréo <johann.dreo@thalesgroup.com>
00025     Caner Candan <caner.candan@thalesgroup.com>
00026 */
00027 
00028 #include <sstream>
00029 #include <iomanip>
00030 
00031 #include <eo>
00032 //#include <mo>
00033 
00034 #include <edo>
00035 
00036 #include "Rosenbrock.h"
00037 #include "Sphere.h"
00038 
00039 typedef eoReal< eoMinimizingFitness > EOT;
00040 typedef edoNormalMulti< EOT > Distrib;
00041 typedef EOT::AtomType AtomType;
00042 
00043 #ifdef WITH_BOOST
00044 #include <boost/numeric/ublas/vector.hpp>
00045 #include <boost/numeric/ublas/symmetric.hpp>
00046     typedef ublas::vector< AtomType > Vector;
00047     typedef ublas::symmetric_matrix< AtomType, ublas::lower > Matrix;
00048 #else
00049 #ifdef WITH_EIGEN
00050 #include <Eigen/Dense>
00051     typedef typename edoNormalMulti<EOT>::Vector Vector;
00052     typedef typename edoNormalMulti<EOT>::Matrix Matrix;
00053 #endif
00054 #endif
00055 
00056 int main(int ac, char** av)
00057 {
00058     // (0) parser + eo routines
00059     eoParser parser(ac, av);
00060 
00061     std::string        section("Algorithm parameters");
00062 
00063     unsigned int p_size   = parser.createParam((unsigned int)100, "popSize", "Population Size", 'P', section).value(); // P
00064     unsigned int s_size   = parser.createParam((unsigned int)2, "dimension-size", "Dimension size", 'd', section).value(); // d
00065     AtomType mean_value   = parser.createParam((AtomType)0, "mean", "Mean value", 'm', section).value(); // m
00066     AtomType covar1_value = parser.createParam((AtomType)1.0, "covar1", "Covar value 1", '1', section).value();
00067     AtomType covar2_value = parser.createParam((AtomType)0.5, "covar2", "Covar value 2", '2', section).value();
00068     AtomType covar3_value = parser.createParam((AtomType)1.0, "covar3", "Covar value 3", '3', section).value();
00069 
00070     std::ostringstream ss;
00071     ss << p_size << "_" << std::fixed << std::setprecision(1)
00072        << mean_value << "_" << covar1_value << "_" << covar2_value << "_"
00073        << covar3_value << "_gen";
00074     std::string gen_filename = ss.str();
00075 
00076     if( parser.userNeedsHelp() ) {
00077             parser.printHelp(std::cout);
00078             exit(1);
00079     }
00080 
00081     make_verbose(parser);
00082     make_help(parser);
00083 
00084     assert(p_size > 0);
00085     assert(s_size > 0);
00086 
00087     eoState state;
00088 
00089     // (1) Population init and sampler
00090     eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
00091     state.storeFunctor(gen);
00092 
00093     eoInitFixedLength< EOT >* init = new eoInitFixedLength< EOT >( s_size, *gen );
00094     state.storeFunctor(init);
00095 
00096     // create an empty pop and let the state handle the memory
00097     // fill population thanks to eoInit instance
00098     eoPop< EOT >& pop = state.takeOwnership( eoPop< EOT >( p_size, *init ) );
00099 
00100     // (2) distribution initial parameters
00101     Vector mean( s_size );
00102 
00103     for (unsigned int i = 0; i < s_size; ++i) {
00104         mean( i ) = mean_value; 
00105     }
00106 
00107     Matrix varcovar( s_size, s_size );
00108 
00109     varcovar( 0, 0 ) = covar1_value;
00110     varcovar( 0, 1 ) = covar2_value;
00111     varcovar( 1, 1 ) = covar3_value;
00112 
00113     Distrib distrib( mean, varcovar );
00114 
00115     // (3a) distribution output preparation
00116     edoDummyContinue< Distrib >* distrib_dummy_continue = new edoDummyContinue< Distrib >();
00117     state.storeFunctor(distrib_dummy_continue);
00118 
00119     edoCheckPoint< Distrib >* distrib_continue = new edoCheckPoint< Distrib >( *distrib_dummy_continue );
00120     state.storeFunctor(distrib_continue);
00121 
00122     edoDistribStat< Distrib >* distrib_stat = new edoStatNormalMulti< EOT >();
00123     state.storeFunctor(distrib_stat);
00124 
00125     distrib_continue->add( *distrib_stat );
00126 
00127     edoFileSnapshot* distrib_file_snapshot = new edoFileSnapshot( "TestResDistrib", 1, gen_filename );
00128     state.storeFunctor(distrib_file_snapshot);
00129     distrib_file_snapshot->add(*distrib_stat);
00130     distrib_continue->add(*distrib_file_snapshot);
00131 
00132     // (3b) distribution output
00133     (*distrib_continue)( distrib );
00134 
00135     // Prepare bounder class to set bounds of sampling.
00136     // This is used by edoSampler.
00137     edoBounder< EOT >* bounder = new edoBounderRng< EOT >(
00138             EOT(pop[0].size(), -5), EOT(pop[0].size(), 5), *gen
00139         );
00140     state.storeFunctor(bounder);
00141 
00142     // Prepare sampler class with a specific distribution
00143     edoSampler< Distrib >* sampler = new edoSamplerNormalMulti< EOT >( *bounder );
00144     state.storeFunctor(sampler);
00145 
00146     // (4) sampling phase
00147     pop.clear();
00148 
00149     for( unsigned int i = 0; i < p_size; ++i ) {
00150         EOT candidate_solution = (*sampler)( distrib );
00151         pop.push_back( candidate_solution );
00152     }
00153 
00154     // (5) population output
00155     eoContinue< EOT >* pop_cont = new eoGenContinue< EOT >( 2 ); // never reached fitness
00156     state.storeFunctor(pop_cont);
00157 
00158     eoCheckPoint< EOT >* pop_continue = new eoCheckPoint< EOT >( *pop_cont );
00159     state.storeFunctor(pop_continue);
00160 
00161     edoPopStat< EOT >* pop_stat = new edoPopStat<EOT>;
00162     state.storeFunctor(pop_stat);
00163     pop_continue->add(*pop_stat);
00164 
00165     edoFileSnapshot* pop_file_snapshot = new edoFileSnapshot( "TestResPop", 1, gen_filename );
00166     state.storeFunctor(pop_file_snapshot);
00167     pop_file_snapshot->add(*pop_stat);
00168     pop_continue->add(*pop_file_snapshot);
00169 
00170     (*pop_continue)( pop );
00171 
00172     // (6) estimation phase
00173     edoEstimator< Distrib >* estimator = new edoEstimatorNormalMulti< EOT >();
00174     state.storeFunctor(estimator);
00175 
00176     distrib = (*estimator)( pop );
00177 
00178     // (7) distribution output
00179     (*distrib_continue)( distrib );
00180 
00181     // (8) euclidianne distance estimation
00182     Vector new_mean = distrib.mean();
00183     Matrix new_varcovar = distrib.varcovar();
00184 
00185     AtomType distance = 0;
00186     for( unsigned int d = 0; d < s_size; ++d ) {
00187         distance += pow( mean[ d ] - new_mean[ d ], 2 );
00188     }
00189 
00190     distance = sqrt( distance );
00191 
00192     eo::log << eo::logging
00193             << "mean: " << mean << std::endl
00194             << "new mean: " << new_mean << std::endl
00195             << "distance: " << distance << std::endl
00196         ;
00197 
00198     return 0;
00199 }
 All Classes Functions Variables Typedefs