t-mean-distance.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 <sys/stat.h>
00029 #include <sys/types.h>
00030 
00031 #include <sstream>
00032 #include <iomanip>
00033 #include <fstream>
00034 
00035 #include <eo>
00036 //#include <mo>
00037 
00038 #include <edo>
00039 
00040 
00041 #include "Rosenbrock.h"
00042 #include "Sphere.h"
00043 
00044 typedef eoReal< eoMinimizingFitness > EOT;
00045 typedef edoNormalMulti< EOT > Distrib;
00046 typedef typename EOT::AtomType AtomType;
00047 
00048 #ifdef WITH_BOOST
00049 #include <boost/numeric/ublas/vector.hpp>
00050 #include <boost/numeric/ublas/symmetric.hpp>
00051     typedef ublas::vector< AtomType > Vector;
00052     typedef ublas::symmetric_matrix< AtomType, ublas::lower > Matrix;
00053 #else
00054 #ifdef WITH_EIGEN
00055 #include <Eigen/Dense>
00056     typedef typename edoNormalMulti<EOT>::Vector Vector;
00057     typedef typename edoNormalMulti<EOT>::Matrix Matrix;
00058 #endif
00059 #endif
00060 
00061 int main(int ac, char** av)
00062 {
00063     // (0) parser + eo routines
00064     eoParser parser(ac, av);
00065 
00066     std::string        section("Algorithm parameters");
00067 
00068     unsigned int r_max = parser.createParam((unsigned int)100, "run-number", "Number of run", 'r', section).value(); // r
00069     unsigned int p_min = parser.createParam((unsigned int)10, "population-min", "Population min", 'p', section).value(); // p
00070     unsigned int p_max = parser.createParam((unsigned int)1000, "population-max", "Population max", 'P', section).value(); // P
00071     unsigned int p_step = parser.createParam((unsigned int)50, "population-step", "Population step", 't', section).value(); // t
00072     unsigned int s_size = parser.createParam((unsigned int)2, "dimension-size", "Dimension size", 'd', section).value(); // d
00073 
00074     AtomType mean_value = parser.createParam((AtomType)0, "mean", "Mean value", 'm', section).value(); // m
00075     AtomType covar1_value = parser.createParam((AtomType)1.0, "covar1", "Covar value 1", '1', section).value(); // 1
00076     AtomType covar2_value = parser.createParam((AtomType)0.5, "covar2", "Covar value 2", '2', section).value(); // 2
00077     AtomType covar3_value = parser.createParam((AtomType)1.0, "covar3", "Covar value 3", '3', section).value(); // 3
00078 
00079     std::string results_directory = parser.createParam((std::string)"means_distances_results", "results-directory", "Results directory", 'R', section).value(); // R
00080     std::string files_description = parser.createParam((std::string)"files_description.txt", "files-description", "Files description", 'F', section).value(); // F
00081 
00082     if (parser.userNeedsHelp())
00083         {
00084             parser.printHelp(std::cout);
00085             exit(1);
00086         }
00087 
00088     make_verbose(parser);
00089     make_help(parser);
00090 
00091 
00092 
00093     assert(r_max >= 1);
00094     assert(s_size >= 2);
00095 
00096     eo::log << eo::quiet;
00097 
00098     ::mkdir( results_directory.c_str(), 0755 );
00099 
00100     for ( unsigned int p_size = p_min; p_size <= p_max; p_size += p_step )
00101         {
00102             assert(p_size >= p_min);
00103 
00104             std::ostringstream desc_file;
00105             desc_file << results_directory << "/" << files_description;
00106 
00107             std::ostringstream cur_file;
00108             cur_file << results_directory << "/pop_" << p_size << ".txt";
00109 
00110             eo::log << eo::file( desc_file.str() ) << cur_file.str().c_str() << std::endl;
00111 
00112             eo::log << eo::file( cur_file.str() );
00113 
00114             eo::log << eo::logging << "run_number p_size s_size mean(0) mean(1) new-mean(0) new-mean(1) distance" << std::endl;
00115 
00116             eo::log << eo::quiet;
00117 
00118             for ( unsigned int r = 1; r <= r_max; ++r)
00119                 {
00120 
00121                     eoState state;
00122 
00123 
00124 
00125                     // (1) Population init and sampler
00126 
00127 
00128                     eoRndGenerator< double >* gen = new eoUniformGenerator< double >(-5, 5);
00129                     state.storeFunctor(gen);
00130 
00131                     eoInitFixedLength< EOT >* init = new eoInitFixedLength< EOT >( s_size, *gen );
00132                     state.storeFunctor(init);
00133 
00134                     // create an empty pop and let the state handle the memory
00135                     // fill population thanks to eoInit instance
00136                     eoPop< EOT >& pop = state.takeOwnership( eoPop< EOT >( p_size, *init ) );
00137 
00138 
00139 
00140 
00141 
00142                     // (2) distribution initial parameters
00143 
00144 
00145 #ifdef WITH_BOOST
00146                     Vector mean( s_size, mean_value );
00147 #else
00148 #ifdef WITH_EIGEN
00149                     Vector mean( s_size );
00150                     mean = Vector::Constant( s_size, mean_value);
00151 #endif
00152 #endif
00153                     Matrix varcovar( s_size, s_size );
00154 
00155                     varcovar( 0, 0 ) = covar1_value;
00156                     varcovar( 0, 1 ) = covar2_value;
00157                     varcovar( 1, 1 ) = covar3_value;
00158 
00159                     Distrib distrib( mean, varcovar );
00160 
00161 
00162 
00163 
00164 
00165                     // Prepare bounder class to set bounds of sampling.
00166                     // This is used by edoSampler.
00167 
00168 
00169                     edoBounder< EOT >* bounder = new edoBounderRng< EOT >(EOT(pop[0].size(), -5),
00170                                                                         EOT(pop[0].size(), 5),
00171                                                                         *gen);
00172                     state.storeFunctor(bounder);
00173 
00174 
00175 
00176 
00177 
00178                     // Prepare sampler class with a specific distribution
00179 
00180 
00181                     edoSampler< Distrib >* sampler = new edoSamplerNormalMulti< EOT >( *bounder );
00182                     state.storeFunctor(sampler);
00183 
00184 
00185 
00186 
00187 
00188                     // (4) sampling phase
00189 
00190 
00191                     pop.clear();
00192 
00193                     for (unsigned int i = 0; i < p_size; ++i)
00194                         {
00195                             EOT candidate_solution = (*sampler)( distrib );
00196                             pop.push_back( candidate_solution );
00197                         }
00198 
00199 
00200 
00201 
00202 
00203                     // (6) estimation phase
00204 
00205 
00206                     edoEstimator< Distrib >* estimator = new edoEstimatorNormalMulti< EOT >();
00207                     state.storeFunctor(estimator);
00208 
00209                     distrib = (*estimator)( pop );
00210 
00211 
00212 
00213 
00214 
00215                     // (8) euclidianne distance estimation
00216 
00217 
00218                     Vector new_mean = distrib.mean();
00219                     Matrix new_varcovar = distrib.varcovar();
00220 
00221                     AtomType distance = 0;
00222 
00223                     for ( unsigned int d = 0; d < s_size; ++d )
00224                         {
00225                             distance += pow( mean[ d ] - new_mean[ d ], 2 );
00226                         }
00227 
00228                     distance = sqrt( distance );
00229 
00230                     eo::log << r << " " << p_size << " " << s_size << " "
00231                             << mean(0) << " " << mean(1) << " "
00232                             << new_mean(0) << " " << new_mean(1) << " "
00233                             << distance << std::endl
00234                         ;
00235 
00236 
00237 
00238                 }
00239 
00240         }
00241 
00242     return 0;
00243 }
 All Classes Functions Variables Typedefs