00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <sstream>
00029 #include <iomanip>
00030
00031 #include <eo>
00032
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
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();
00064 unsigned int s_size = parser.createParam((unsigned int)2, "dimension-size", "Dimension size", 'd', section).value();
00065 AtomType mean_value = parser.createParam((AtomType)0, "mean", "Mean value", 'm', section).value();
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
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
00097
00098 eoPop< EOT >& pop = state.takeOwnership( eoPop< EOT >( p_size, *init ) );
00099
00100
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
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
00133 (*distrib_continue)( distrib );
00134
00135
00136
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
00143 edoSampler< Distrib >* sampler = new edoSamplerNormalMulti< EOT >( *bounder );
00144 state.storeFunctor(sampler);
00145
00146
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
00155 eoContinue< EOT >* pop_cont = new eoGenContinue< EOT >( 2 );
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
00173 edoEstimator< Distrib >* estimator = new edoEstimatorNormalMulti< EOT >();
00174 state.storeFunctor(estimator);
00175
00176 distrib = (*estimator)( pop );
00177
00178
00179 (*distrib_continue)( distrib );
00180
00181
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 }