From a038586edbe551ac13db8a9754b04e65b992bdb5 Mon Sep 17 00:00:00 2001 From: Caner Candan Date: Mon, 5 Jul 2010 19:04:35 +0200 Subject: [PATCH] + do files --- src/CMakeLists.txt | 35 ++++ src/do | 36 ++++ src/doAlgo.h | 16 ++ src/doBounder.h | 27 +++ src/doBounderBound.h | 35 ++++ src/doBounderNo.h | 14 ++ src/doBounderRng.h | 35 ++++ src/doCMASA.h | 395 +++++++++++++++++++++++++++++++++++++ src/doDistrib.h | 14 ++ src/doDistribParams.h | 32 +++ src/doEstimator.h | 16 ++ src/doEstimatorNormal.h | 46 +++++ src/doEstimatorUniform.h | 44 +++++ src/doFileSnapshot.h | 227 +++++++++++++++++++++ src/doModifier.h | 13 ++ src/doModifierDispersion.h | 16 ++ src/doModifierMass.h | 17 ++ src/doNormal.h | 16 ++ src/doNormalCenter.h | 19 ++ src/doNormalParams.h | 24 +++ src/doSampler.h | 58 ++++++ src/doSamplerNormal.h | 60 ++++++ src/doSamplerUniform.h | 55 ++++++ src/doStats.h | 121 ++++++++++++ src/doUniform.h | 16 ++ src/doUniformCenter.h | 28 +++ src/doVectorBounds.h | 24 +++ 27 files changed, 1439 insertions(+) create mode 100644 src/CMakeLists.txt create mode 100644 src/do create mode 100644 src/doAlgo.h create mode 100644 src/doBounder.h create mode 100644 src/doBounderBound.h create mode 100644 src/doBounderNo.h create mode 100644 src/doBounderRng.h create mode 100644 src/doCMASA.h create mode 100644 src/doDistrib.h create mode 100644 src/doDistribParams.h create mode 100644 src/doEstimator.h create mode 100644 src/doEstimatorNormal.h create mode 100644 src/doEstimatorUniform.h create mode 100644 src/doFileSnapshot.h create mode 100644 src/doModifier.h create mode 100644 src/doModifierDispersion.h create mode 100644 src/doModifierMass.h create mode 100644 src/doNormal.h create mode 100644 src/doNormalCenter.h create mode 100644 src/doNormalParams.h create mode 100644 src/doSampler.h create mode 100644 src/doSamplerNormal.h create mode 100644 src/doSamplerUniform.h create mode 100644 src/doStats.h create mode 100644 src/doUniform.h create mode 100644 src/doUniformCenter.h create mode 100644 src/doVectorBounds.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000..a913882b --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,35 @@ +FIND_PACKAGE(Boost 1.33.0 REQUIRED) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) + +SET(RESOURCES + cma_sa.param + plot.py + ) + +FOREACH(file ${RESOURCES}) + EXECUTE_PROCESS( + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/${file} + ${CMAKE_CURRENT_BINARY_DIR}/${file} + ) +ENDFOREACH(file) + +ADD_EXECUTABLE(cma_sa main.cpp) + +TARGET_LINK_LIBRARIES(cma_sa + ${Boost_LIBRARIES} + BOPO + eoutils + pthread + moeo + eo + peo + rmc_mpi + eometah + nklandscapes + BOPO + #${MPICH2_LIBRARIES} + ${LIBXML2_LIBRARIES} + ${MPI_LIBRARIES} + ) diff --git a/src/do b/src/do new file mode 100644 index 00000000..30289578 --- /dev/null +++ b/src/do @@ -0,0 +1,36 @@ +#ifndef _do_ +#define _do_ + +#include "doAlgo.h" +#include "doCMASA.h" + +#include "doDistrib.h" +#include "doUniform.h" +#include "doNormal.h" + +#include "doEstimator.h" +#include "doEstimatorUniform.h" +#include "doEstimatorNormal.h" + +#include "doModifier.h" +#include "doModifierDispersion.h" +#include "doModifierMass.h" +#include "doUniformCenter.h" +#include "doNormalCenter.h" + +#include "doSampler.h" +#include "doSamplerUniform.h" +#include "doSamplerNormal.h" + +#include "doDistribParams.h" +#include "doVectorBounds.h" +#include "doNormalParams.h" + +#include "doBounder.h" +#include "doBounderNo.h" +#include "doBounderBound.h" +#include "doBounderRng.h" + +#include "doStats.h" + +#endif // !_do_ diff --git a/src/doAlgo.h b/src/doAlgo.h new file mode 100644 index 00000000..54ea1698 --- /dev/null +++ b/src/doAlgo.h @@ -0,0 +1,16 @@ +#ifndef _doAlgo_h +#define _doAlgo_h + +#include + +template < typename D > +class doAlgo : public eoAlgo< typename D::EOType > +{ + //! Alias for the type + typedef typename D::EOType EOT; + +public: + virtual ~doAlgo(){} +}; + +#endif // !_doAlgo_h diff --git a/src/doBounder.h b/src/doBounder.h new file mode 100644 index 00000000..28996474 --- /dev/null +++ b/src/doBounder.h @@ -0,0 +1,27 @@ +#ifndef _doBounder_h +#define _doBounder_h + +#include + +template < typename EOT > +class doBounder : public eoUF< EOT&, void > +{ +public: + doBounder( EOT min = -5, EOT max = 5 ) + : _min(min), _max(max) + { + assert(_min.size() > 0); + assert(_min.size() == _max.size()); + } + + // virtual void operator()( EOT& ) = 0 (provided by eoUF< A1, R >) + + EOT& min(){return _min;} + EOT& max(){return _max;} + +private: + EOT _min; + EOT _max; +}; + +#endif // !_doBounder_h diff --git a/src/doBounderBound.h b/src/doBounderBound.h new file mode 100644 index 00000000..a284d2cb --- /dev/null +++ b/src/doBounderBound.h @@ -0,0 +1,35 @@ +#ifndef _doBounderBound_h +#define _doBounderBound_h + +#include "doBounder.h" + +template < typename EOT > +class doBounderBound : public doBounder< EOT > +{ +public: + doBounderBound( EOT min, EOT max ) + : doBounder< EOT >( min, max ) + {} + + void operator()( EOT& x ) + { + unsigned int size = x.size(); + assert(size > 0); + + for (unsigned int d = 0; d < size; ++d) // browse all dimensions + { + if (x[d] < this->min()[d]) + { + x[d] = this->min()[d]; + continue; + } + + if (x[d] > this->max()[d]) + { + x[d] = this->max()[d]; + } + } + } +}; + +#endif // !_doBounderBound_h diff --git a/src/doBounderNo.h b/src/doBounderNo.h new file mode 100644 index 00000000..36e34451 --- /dev/null +++ b/src/doBounderNo.h @@ -0,0 +1,14 @@ +#ifndef _doBounderNo_h +#define _doBounderNo_h + +#include "doBounder.h" + +template < typename EOT > +class doBounderNo : public doBounder< EOT > +{ +public: + void operator()( EOT& x ) + {} +}; + +#endif // !_doBounderNo_h diff --git a/src/doBounderRng.h b/src/doBounderRng.h new file mode 100644 index 00000000..2f2450bc --- /dev/null +++ b/src/doBounderRng.h @@ -0,0 +1,35 @@ +#ifndef _doBounderRng_h +#define _doBounderRng_h + +#include "doBounder.h" + +template < typename EOT > +class doBounderRng : public doBounder< EOT > +{ +public: + doBounderRng( EOT min, EOT max, eoRndGenerator< double > & rng ) + : doBounder< EOT >( min, max ), _rng(rng) + {} + + void operator()( EOT& x ) + { + unsigned int size = x.size(); + assert(size > 0); + + for (unsigned int d = 0; d < size; ++d) // browse all dimensions + { + + // FIXME: attention: les bornes RNG ont les memes bornes quelque soit les dimensions idealement on voudrait avoir des bornes differentes pour chaque dimensions. + + if (x[d] < this->min()[d] || x[d] > this->max()[d]) + { + x[d] = _rng(); + } + } + } + +private: + eoRndGenerator< double> & _rng; +}; + +#endif // !_doBounderRng_h diff --git a/src/doCMASA.h b/src/doCMASA.h new file mode 100644 index 00000000..cdf446c8 --- /dev/null +++ b/src/doCMASA.h @@ -0,0 +1,395 @@ +#ifndef _doCMASA_h +#define _doCMASA_h + +//----------------------------------------------------------------------------- +// Temporary solution to store populations state at each iteration for plotting. +//----------------------------------------------------------------------------- + +// system header inclusion +#include +// end system header inclusion + +// mkdir headers inclusion +#include +#include +// end mkdir headers inclusion + +#include +#include + +#include + +#include +#include + +//----------------------------------------------------------------------------- + +#include +#include + +#include + +#include "doAlgo.h" +#include "doEstimator.h" +#include "doModifierMass.h" +#include "doSampler.h" + +using namespace boost::numeric::ublas; + +template < typename D > +class doCMASA : public doAlgo< D > +{ +public: + //! Alias for the type + typedef typename D::EOType EOT; + + //! Alias for the fitness + typedef typename EOT::Fitness Fitness; + +public: + + //! doCMASA constructor + /*! + All the boxes used by a CMASA need to be given. + + \param selector The EOT selector + \param estomator The EOT selector + \param selectone SelectOne + \param modifier The D modifier + \param sampler The D sampler + \param evaluation The evaluation function. + \param continue The stopping criterion. + \param cooling_schedule The cooling schedule, describes how the temperature is modified. + \param initial_temperature The initial temperature. + \param replacor The EOT replacor + */ + doCMASA (eoSelect< EOT > & selector, + doEstimator< D > & estimator, + eoSelectOne< EOT > & selectone, + doModifierMass< D > & modifier, + doSampler< D > & sampler, + eoContinue< EOT > & monitoring_continue, + eoEvalFunc < EOT > & evaluation, + moSolContinue < EOT > & continuator, + moCoolingSchedule & cooling_schedule, + double initial_temperature, + eoReplacement< EOT > & replacor + ) + : _selector(selector), + _estimator(estimator), + _selectone(selectone), + _modifier(modifier), + _sampler(sampler), + _monitoring_continue(monitoring_continue), + _evaluation(evaluation), + _continuator(continuator), + _cooling_schedule(cooling_schedule), + _initial_temperature(initial_temperature), + _replacor(replacor) + {} + + //! function that launches the CMASA algorithm. + /*! + As a moTS or a moHC, the CMASA can be used for HYBRIDATION in an evolutionary algorithm. + + \param pop A population to improve. + \return TRUE. + */ + //bool operator ()(eoPop< EOT > & pop) + void operator ()(eoPop< EOT > & pop) + { + assert(pop.size() > 0); + + double temperature = _initial_temperature; + + eoPop< EOT > current_pop = pop; + + eoPop< EOT > selected_pop; + + //------------------------------------------------------------- + // Temporary solution used by plot to enumerate iterations in + // several files. + //------------------------------------------------------------- + + int number_of_iterations = 0; + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Temporary solution to store populations state at each + // iteration for plotting. + //------------------------------------------------------------- + + std::string pop_results_destination("ResPop"); + + { + std::stringstream ss; + ss << "rm -rf " << pop_results_destination; + ::system(ss.str().c_str()); + } + + ::mkdir(pop_results_destination.c_str(), 0755); // create a first time the + // directory where populations state are going to be stored. + std::ofstream ofs_params("ResParams.txt"); + std::ofstream ofs_params_var("ResVars.txt"); + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Temporary solution to store bounds valeur for each distribution. + //------------------------------------------------------------- + + std::string bounds_results_destination("ResBounds"); + + { + std::stringstream ss; + ss << "rm -rf " << bounds_results_destination; + ::system(ss.str().c_str()); + } + + ::mkdir(bounds_results_destination.c_str(), 0755); // create a first time the + + //------------------------------------------------------------- + + + { + D distrib = _estimator(pop); + + double size = distrib.size(); + + assert(size > 0); + + double vacc = 1; + + for (int i = 0; i < size; ++i) + { + vacc *= sqrt(distrib.variance()[i]); + } + + assert(vacc <= std::numeric_limits::max()); + + ofs_params_var << vacc << std::endl; + } + + do + { + if (pop != current_pop) + { + _replacor(pop, current_pop); + } + + current_pop.clear(); + selected_pop.clear(); + + + //------------------------------------------------------------- + // (3) Selection of the best points in the population + //------------------------------------------------------------- + + _selector(pop, selected_pop); + + //------------------------------------------------------------- + + + _continuator.init(); + + + //------------------------------------------------------------- + // (4) Estimation of the distribution parameters + //------------------------------------------------------------- + + D distrib = _estimator(selected_pop); + + //------------------------------------------------------------- + + + // TODO: utiliser selected_pop ou pop ??? + + assert(selected_pop.size() > 0); + + + //------------------------------------------------------------- + // Init of a variable contening a point with the bestest fitnesses + //------------------------------------------------------------- + + EOT current_solution = _selectone(selected_pop); + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Fit the current solution with the distribution parameters (bounds) + //------------------------------------------------------------- + + // FIXME: si besoin de modifier la dispersion de la distribution + // _modifier_dispersion(distribution, selected_pop); + _modifier(distrib, current_solution); + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Building of the sampler in current_pop + //------------------------------------------------------------- + + do + { + EOT candidate_solution = _sampler(distrib); + + EOT& e1 = candidate_solution; + _evaluation( e1 ); + EOT& e2 = current_solution; + _evaluation( e2 ); + + // TODO: verifier le critere d'acceptation + if ( e1.fitness() < e2.fitness() || + rng.uniform() < exp( ::fabs(e1.fitness() - e2.fitness()) / temperature ) ) + { + current_pop.push_back(candidate_solution); + current_solution = candidate_solution; + } + } + while ( _continuator( current_solution) ); + + + //------------------------------------------------------------- + // Temporary solution to store populations state + // at each iteration for plotting. + //------------------------------------------------------------- + + { + std::stringstream ss; + ss << pop_results_destination << "/" << number_of_iterations; + std::ofstream ofs(ss.str().c_str()); + ofs << current_pop; + } + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Temporary solution used by plot to enumerate iterations in + // several files. + //------------------------------------------------------------- + + { + double size = distrib.size(); + + assert(size > 0); + + std::stringstream ss; + ss << bounds_results_destination << "/" << number_of_iterations; + std::ofstream ofs(ss.str().c_str()); + + ofs << size << " "; + std::copy(distrib.param(0).begin(), distrib.param(0).end(), std::ostream_iterator< double >(ofs, " ")); + std::copy(distrib.param(1).begin(), distrib.param(1).end(), std::ostream_iterator< double >(ofs, " ")); + ofs << std::endl; + } + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Temporary saving to get a proof for distribution bounds + // dicreasement + //------------------------------------------------------------- + + { + // double size = distrib.size(); + + // assert(size > 0); + + // vector< double > vmin(size); + // vector< double > vmax(size); + + // std::copy(distrib.param(0).begin(), distrib.param(0).end(), vmin.begin()); + // std::copy(distrib.param(1).begin(), distrib.param(1).end(), vmax.begin()); + + // vector< double > vrange = vmax - vmin; + + // double vacc = 1; + + // for (int i = 0, size = vrange.size(); i < size; ++i) + // { + // vacc *= vrange(i); + // } + + // assert(vacc <= std::numeric_limits::max()); + + // ofs_params << vacc << std::endl; + } + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Temporary saving to get a proof for distribution bounds + // dicreasement + //------------------------------------------------------------- + + { + double size = distrib.size(); + + assert(size > 0); + + double vacc = 1; + + for (int i = 0; i < size; ++i) + { + vacc *= sqrt(distrib.variance()[i]); + } + + assert(vacc <= std::numeric_limits::max()); + + ofs_params_var << vacc << std::endl; + } + + //------------------------------------------------------------- + + ++number_of_iterations; + + } + while ( _cooling_schedule( temperature ) && + _monitoring_continue( selected_pop ) ); + } + +private: + + //! A EOT selector + eoSelect < EOT > & _selector; + + //! A EOT estimator. It is going to estimate distribution parameters. + doEstimator< D > & _estimator; + + //! SelectOne + eoSelectOne< EOT > & _selectone; + + //! A D modifier + doModifierMass< D > & _modifier; + + //! A D sampler + doSampler< D > & _sampler; + + //! A EOT monitoring continuator + eoContinue < EOT > & _monitoring_continue; + + //! A full evaluation function. + eoEvalFunc < EOT > & _evaluation; + + //! Stopping criterion before temperature update + moSolContinue < EOT > & _continuator; + + //! The cooling schedule + moCoolingSchedule & _cooling_schedule; + + //! Initial temperature + double _initial_temperature; + + //! A EOT replacor + eoReplacement < EOT > & _replacor; +}; + +#endif // !_doCMASA_h diff --git a/src/doDistrib.h b/src/doDistrib.h new file mode 100644 index 00000000..71c2f6f1 --- /dev/null +++ b/src/doDistrib.h @@ -0,0 +1,14 @@ +#ifndef _doDistrib_h +#define _doDistrib_h + +template < typename EOT > +class doDistrib +{ +public: + //! Alias for the type + typedef EOT EOType; + + virtual ~doDistrib(){} +}; + +#endif // !_doDistrib_h diff --git a/src/doDistribParams.h b/src/doDistribParams.h new file mode 100644 index 00000000..b895eec9 --- /dev/null +++ b/src/doDistribParams.h @@ -0,0 +1,32 @@ +#ifndef _doDistribParams_h +#define _doDistribParams_h + +#include + +template < typename EOT > +class doDistribParams +{ +public: + doDistribParams(unsigned n = 2) + : _params(n) + {} + + EOT& param(unsigned int i = 0){return _params[i];} + + unsigned int param_size(){return _params.size();} + + unsigned int size() + { + for (unsigned int i = 0, size = param_size(); i < size - 1; ++i) + { + assert(param(i).size() == param(i + 1).size()); + } + + return param(0).size(); + } + +private: + std::vector< EOT > _params; +}; + +#endif // !_doDistribParams_h diff --git a/src/doEstimator.h b/src/doEstimator.h new file mode 100644 index 00000000..682dd3f7 --- /dev/null +++ b/src/doEstimator.h @@ -0,0 +1,16 @@ +#ifndef _doEstimator_h +#define _doEstimator_h + +#include +#include + +template < typename D > +class doEstimator : public eoUF< eoPop< typename D::EOType >&, D > +{ +public: + typedef typename D::EOType EOType; + + // virtual D operator() ( eoPop< EOT >& )=0 (provided by eoUF< A1, R >) +}; + +#endif // !_doEstimator_h diff --git a/src/doEstimatorNormal.h b/src/doEstimatorNormal.h new file mode 100644 index 00000000..133b9803 --- /dev/null +++ b/src/doEstimatorNormal.h @@ -0,0 +1,46 @@ +#ifndef _doEstimatorNormal_h +#define _doEstimatorNormal_h + +#include "doEstimator.h" +#include "doUniform.h" +#include "doStats.h" + +// TODO: calcule de la moyenne + covariance dans une classe derivee + +template < typename EOT > +class doEstimatorNormal : public doEstimator< doNormal< EOT > > +{ +public: + doNormal< EOT > operator()(eoPop& pop) + { + unsigned int popsize = pop.size(); + assert(popsize > 0); + + unsigned int dimsize = pop[0].size(); + assert(dimsize > 0); + + std::vector< Var > var(dimsize); + + for (unsigned int i = 0; i < popsize; ++i) + { + for (unsigned int d = 0; d < dimsize; ++d) + { + var[d].update(pop[i][d]); + } + } + + EOT mean(dimsize); + EOT variance(dimsize); + + for (unsigned int d = 0; d < dimsize; ++d) + { + mean[d] = var[d].get_mean(); + variance[d] = var[d].get_var(); + //variance[d] = var[d].get_std(); // perhaps I should use this !?! + } + + return doNormal< EOT >(mean, variance); + } +}; + +#endif // !_doEstimatorNormal_h diff --git a/src/doEstimatorUniform.h b/src/doEstimatorUniform.h new file mode 100644 index 00000000..8f9b3551 --- /dev/null +++ b/src/doEstimatorUniform.h @@ -0,0 +1,44 @@ +#ifndef _doEstimatorUniform_h +#define _doEstimatorUniform_h + +#include "doEstimator.h" +#include "doUniform.h" + +// TODO: calcule de la moyenne + covariance dans une classe derivee + +template < typename EOT > +class doEstimatorUniform : public doEstimator< doUniform< EOT > > +{ +public: + doUniform< EOT > operator()(eoPop& pop) + { + unsigned int size = pop.size(); + + assert(size > 0); + + EOT min = pop[0]; + EOT max = pop[0]; + + for (unsigned int i = 1; i < size; ++i) + { + unsigned int size = pop[i].size(); + + assert(size > 0); + + // possibilité d'utiliser std::min_element et std::max_element mais exige 2 pass au lieu d'1. + + for (unsigned int d = 0; d < size; ++d) + { + if (pop[i][d] < min[d]) + min[d] = pop[i][d]; + + if (pop[i][d] > max[d]) + max[d] = pop[i][d]; + } + } + + return doUniform< EOT >(min, max); + } +}; + +#endif // !_doEstimatorUniform_h diff --git a/src/doFileSnapshot.h b/src/doFileSnapshot.h new file mode 100644 index 00000000..39a5bb16 --- /dev/null +++ b/src/doFileSnapshot.h @@ -0,0 +1,227 @@ +//----------------------------------------------------------------------------- +// eoFileSnapshot.h +// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2001 +/* + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mkeijzer@dhi.dk + Caner Candan caner@candan.fr + Johann Dréo nojhan@gmail.com + */ +//----------------------------------------------------------------------------- + +#ifndef _doFileSnapshot_h +#define _doFileSnapshot_h + +#include // for mkdir +#include // for mkdir +#include // for unlink + +#include +#include +#include + +#include +#include +#include + +#include + +/** +Prints snapshots of fitnesses to a (new) file every N generations + +Assumes that the parameters that are passed to the monitor +(method add in eoMonitor.h) are eoValueParam > of same size. + +A dir is created and one file per snapshot is created there - +so you can later generate a movie! + +TODO: The counter is handled internally, but this should be changed +so that you can pass e.g. an evalcounter (minor) + +I failed to templatize everything so that it can handle eoParam > +for any type T, simply calling their getValue method ... +*/ + +template < typename EOTParam > +class doFileSnapshot : public eoMonitor +{ +public : + doFileSnapshot(std::string _dirname, unsigned _frequency = 1, + std::string _filename = "gen", + std::string _delim = " ", unsigned _counter = 0, + bool _rmFiles = true): + dirname(_dirname), frequency(_frequency), + filename(_filename), delim(_delim), counter(_counter), boolChanged(true) + { + // FIXME START + // TO REPLACE test command by somethink more gernerical + + std::string s = "test -d " + dirname; + + int res = system(s.c_str()); + // test for (unlikely) errors + if ( (res==-1) || (res==127) ) + throw std::runtime_error("Problem executing test of dir in eoFileSnapshot"); + + // FIXME END + + // now make sure there is a dir without any genXXX file in it + if (res) // no dir present + { + //s = std::string("mkdir ")+dirname; + ::mkdir(dirname.c_str(), 0755); + } + else if (!res && _rmFiles) + { + //s = std::string("/bin/rm ")+dirname+ "/" + filename + "*"; + std::string s = dirname+ "/" + filename + "*"; + ::unlink(s.c_str()); + } + // else + // s = " "; + + //int nothing = system(s.c_str()); + // all done + } + + /** accessor: has something changed (for gnuplot subclass) + */ + virtual bool hasChanged() {return boolChanged;} + + /** accessor to the counter: needed by the gnuplot subclass + */ + unsigned getCounter() {return counter;} + + /** accessor to the current filename: needed by the gnuplot subclass + */ + std::string getFileName() {return currentFileName;} + + /** sets the current filename depending on the counter + */ + void setCurrentFileName() + { + std::ostringstream oscount; + oscount << counter; + currentFileName = dirname + "/" + filename + oscount.str(); + } + + /** The operator(void): opens the std::ostream and calls the write method + */ + eoMonitor& operator()(void) + { + if (counter % frequency) + { + boolChanged = false; // subclass with gnuplot will do nothing + counter++; + return (*this); + } + counter++; + boolChanged = true; + setCurrentFileName(); + std::ofstream os(currentFileName.c_str()); + + if (!os) + { + std::string str = "eoFileSnapshot: Could not open " + currentFileName; + throw std::runtime_error(str); + } + + return operator()(os); + } + + /** The operator(): write on an std::ostream + */ + eoMonitor& operator()(std::ostream& _os) + { + //eo::log << eo::debug << "TOTO" << std::endl; + + //_os << "TOTO" << "\n"; + + //_os << v[0] << "\n"; + + // const eoValueParam > * ptParam = + // static_cast >* >(vec[0]); + + // what's the size of vec ?!? + + eo::log << eo::debug; + + eo::log << "vec size: " << vec.size() << std::endl; + + const eoValueParam< EOTParam > * ptParam = + static_cast< const eoValueParam< EOTParam >* >(vec[0]); + + //const std::vector v = ptParam->value(); + EOTParam v(ptParam->value()); + if (vec.size() == 1) // only one std::vector: -> add number in front + { + eo::log << "I am here..." << std::endl; + + //eo::log << *vec[0] << std::endl; + + for (unsigned k=0; k > vv(vec.size()); + std::vector< EOTParam > vv(vec.size()); + vv[0]=v; + for (unsigned i=1; i >* >(vec[1]); + ptParam = static_cast< const eoValueParam< EOTParam >* >(vec[1]); + vv[i] = ptParam->value(); + if (vv[i].size() != v.size()) + throw std::runtime_error("Dimension error in eoSnapshotMonitor"); + } + for (unsigned k=0; k +class doModifier +{ +public: + virtual ~doModifier(){} + + typedef typename D::EOType EOType; +}; + +#endif // !_doModifier_h diff --git a/src/doModifierDispersion.h b/src/doModifierDispersion.h new file mode 100644 index 00000000..2632757b --- /dev/null +++ b/src/doModifierDispersion.h @@ -0,0 +1,16 @@ +#ifndef _doModifierDispersion_h +#define _doModifierDispersion_h + +#include +#include + +#include "doModifier.h" + +template < typename D > +class doModifierDispersion : public doModifier< D >, public eoBF< D&, eoPop< typename D::EOType >&, void > +{ +public: + // virtual void operator() ( D&, eoPop< D::EOType >& )=0 (provided by eoBF< A1, A2, R >) +}; + +#endif // !_doModifierDispersion_h diff --git a/src/doModifierMass.h b/src/doModifierMass.h new file mode 100644 index 00000000..411c3f29 --- /dev/null +++ b/src/doModifierMass.h @@ -0,0 +1,17 @@ +#ifndef _doModifierMass_h +#define _doModifierMass_h + +#include + +#include "doModifier.h" + +template < typename D > +class doModifierMass : public doModifier< D >, public eoBF< D&, typename D::EOType&, void > +{ +public: + //typedef typename D::EOType::AtomType AtomType; // does not work !!! + + // virtual void operator() ( D&, D::EOType& )=0 (provided by eoBF< A1, A2, R >) +}; + +#endif // !_doModifierMass_h diff --git a/src/doNormal.h b/src/doNormal.h new file mode 100644 index 00000000..6de887fc --- /dev/null +++ b/src/doNormal.h @@ -0,0 +1,16 @@ +#ifndef _doNormal_h +#define _doNormal_h + +#include "doDistrib.h" +#include "doNormalParams.h" + +template < typename EOT > +class doNormal : public doDistrib< EOT >, public doNormalParams< EOT > +{ +public: + doNormal(EOT mean, EOT variance) + : doNormalParams< EOT >(mean, variance) + {} +}; + +#endif // !_doNormal_h diff --git a/src/doNormalCenter.h b/src/doNormalCenter.h new file mode 100644 index 00000000..06113060 --- /dev/null +++ b/src/doNormalCenter.h @@ -0,0 +1,19 @@ +#ifndef _doNormalCenter_h +#define _doNormalCenter_h + +#include "doModifierMass.h" +#include "doNormal.h" + +template < typename EOT > +class doNormalCenter : public doModifierMass< doNormal< EOT > > +{ +public: + typedef typename EOT::AtomType AtomType; + + void operator() ( doNormal< EOT >& distrib, EOT& mass ) + { + distrib.mean() = mass; // vive les references!!! + } +}; + +#endif // !_doNormalCenter_h diff --git a/src/doNormalParams.h b/src/doNormalParams.h new file mode 100644 index 00000000..fbfc88ed --- /dev/null +++ b/src/doNormalParams.h @@ -0,0 +1,24 @@ +#ifndef _doNormalParams_h +#define _doNormalParams_h + +#include "doDistribParams.h" + +template < typename EOT > +class doNormalParams : public doDistribParams< EOT > +{ +public: + doNormalParams(EOT _mean, EOT _variance) + : doDistribParams< EOT >(2) + { + assert(_mean.size() > 0); + assert(_mean.size() == _variance.size()); + + mean() = _mean; + variance() = _variance; + } + + EOT& mean(){return this->param(0);} + EOT& variance(){return this->param(1);} +}; + +#endif // !_doNormalParams_h diff --git a/src/doSampler.h b/src/doSampler.h new file mode 100644 index 00000000..1bb66b44 --- /dev/null +++ b/src/doSampler.h @@ -0,0 +1,58 @@ +#ifndef _doSampler_h +#define _doSampler_h + +#include + +#include "doBounder.h" + +template < typename D > +class doSampler : public eoUF< D&, typename D::EOType > +{ +public: + typedef typename D::EOType EOType; + + doSampler(doBounder< EOType > & bounder) + : _bounder(bounder) + {} + + // virtual EOType operator()( D& ) = 0 (provided by eoUF< A1, R >) + + virtual EOType sample( D& ) = 0; + + EOType operator()( D& distrib ) + { + unsigned int size = distrib.size(); + assert(size > 0); + + + //------------------------------------------------------------- + // Point we want to sample to get higher a set of points + // (coordinates in n dimension) + // x = {x1, x2, ..., xn} + // the sample method is implemented in the derivated class + //------------------------------------------------------------- + + EOType solution(sample(distrib)); + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Now we are bounding the distribution thanks to min and max + // parameters. + //------------------------------------------------------------- + + _bounder(solution); + + //------------------------------------------------------------- + + + return solution; + } + +private: + //! Bounder functor + doBounder< EOType > & _bounder; +}; + +#endif // !_doSampler_h diff --git a/src/doSamplerNormal.h b/src/doSamplerNormal.h new file mode 100644 index 00000000..7ba4c2d5 --- /dev/null +++ b/src/doSamplerNormal.h @@ -0,0 +1,60 @@ +#ifndef _doSamplerNormal_h +#define _doSamplerNormal_h + +#include + +#include "doSampler.h" +#include "doNormal.h" +#include "doBounder.h" + +/** + * doSamplerNormal + * This class uses the Normal distribution parameters (bounds) to return + * a random position used for population sampling. + */ +template < typename EOT > +class doSamplerNormal : public doSampler< doNormal< EOT > > +{ +public: + typedef typename EOT::AtomType AtomType; + + doSamplerNormal( doBounder< EOT > & bounder ) + : doSampler< doNormal< EOT > >( bounder ) + {} + + EOT sample( doNormal< EOT >& distrib ) + { + unsigned int size = distrib.size(); + + assert(size > 0); + + //------------------------------------------------------------- + // Point we want to sample to get higher a set of points + // (coordinates in n dimension) + // x = {x1, x2, ..., xn} + //------------------------------------------------------------- + + EOT solution; + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Sampling all dimensions + //------------------------------------------------------------- + + for (unsigned int i = 0; i < size; ++i) + { + solution.push_back( + rng.normal(distrib.mean()[i], + distrib.variance()[i]) + ); + } + + //------------------------------------------------------------- + + return solution; + } +}; + +#endif // !_doSamplerNormal_h diff --git a/src/doSamplerUniform.h b/src/doSamplerUniform.h new file mode 100644 index 00000000..f49b6924 --- /dev/null +++ b/src/doSamplerUniform.h @@ -0,0 +1,55 @@ +#ifndef _doSamplerUniform_h +#define _doSamplerUniform_h + +#include + +#include "doSampler.h" +#include "doUniform.h" + +/** + * doSamplerUniform + * This class uses the Uniform distribution parameters (bounds) to return + * a random position used for population sampling. + */ +template < typename EOT > +class doSamplerUniform : public doSampler< doUniform< EOT > > +{ +public: + EOT sample( doUniform< EOT >& distrib ) + { + unsigned int size = distrib.size(); + assert(size > 0); + + //------------------------------------------------------------- + // Point we want to sample to get higher a population + // x = {x1, x2, ..., xn} + //------------------------------------------------------------- + + EOT solution; + + //------------------------------------------------------------- + + + //------------------------------------------------------------- + // Sampling all dimensions + //------------------------------------------------------------- + + for (unsigned int i = 0; i < size; ++i) + { + double min = distrib.min()[i]; + double max = distrib.max()[i]; + double random = rng.uniform(min, max); + + assert(min <= random && random <= max); + + solution.push_back(random); + } + + //------------------------------------------------------------- + + + return solution; + } +}; + +#endif // !_doSamplerUniform_h diff --git a/src/doStats.h b/src/doStats.h new file mode 100644 index 00000000..b5ac32b2 --- /dev/null +++ b/src/doStats.h @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2005 Maarten Keijzer + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include + +class Mean { + + double n; + double mean; + + public: + Mean() : n(0), mean(0) {} + + void update(double v) { + n++; + double d = v - mean; + mean += 1/n * d; + } + + double get_mean() const { return mean; } +}; + +class Var { + double n; + double mean; + double sumvar; + + public: + Var() : n(0), mean(0), sumvar(0) {} + + void update(double v) { + n++; + double d = v - mean; + mean += 1/n * d; + sumvar += (n-1)/n * d * d; + } + + double get_mean() const { return mean; } + double get_var() const { return sumvar / (n-1); } + double get_std() const { return sqrt(get_var()); } +}; + +/** Single covariance between two variates */ +class Cov { + double n; + double meana; + double meanb; + double sumcov; + + public: + Cov() : n(0), meana(0), meanb(0), sumcov(0) {} + + void update(double a, double b) { + ++n; + double da = a - meana; + double db = b - meanb; + + meana += 1/n * da; + meanb += 1/n * db; + + sumcov += (n-1)/n * da * db; + } + + double get_meana() const { return meana; } + double get_meanb() const { return meanb; } + double get_cov() const { return sumcov / (n-1); } +}; + +class CovMatrix { + double n; + std::vector mean; + std::vector< std::vector > sumcov; + + public: + CovMatrix(unsigned dim) : n(0), mean(dim), sumcov(dim , std::vector(dim)) {} + + void update(const std::vector& v) { + assert(v.size() == mean.size()); + + n++; + + for (unsigned i = 0; i < v.size(); ++i) { + double d = v[i] - mean[i]; + mean[i] += 1/n * d; + + sumcov[i][i] += (n-1)/n * d * d; + + for (unsigned j = i; j < v.size(); ++j) { + double e = v[j] - mean[j]; // mean[j] is not updated yet + + double upd = (n-1)/n * d * e; + + sumcov[i][j] += upd; + sumcov[j][i] += upd; + + } + } + + } + + double get_mean(int i) const { return mean[i]; } + double get_var(int i ) const { return sumcov[i][i] / (n-1); } + double get_std(int i) const { return sqrt(get_var(i)); } + double get_cov(int i, int j) const { return sumcov[i][j] / (n-1); } + +}; diff --git a/src/doUniform.h b/src/doUniform.h new file mode 100644 index 00000000..6bfd9016 --- /dev/null +++ b/src/doUniform.h @@ -0,0 +1,16 @@ +#ifndef _doUniform_h +#define _doUniform_h + +#include "doDistrib.h" +#include "doVectorBounds.h" + +template < typename EOT > +class doUniform : public doDistrib< EOT >, public doVectorBounds< EOT > +{ +public: + doUniform(EOT min, EOT max) + : doVectorBounds< EOT >(min, max) + {} +}; + +#endif // !_doUniform_h diff --git a/src/doUniformCenter.h b/src/doUniformCenter.h new file mode 100644 index 00000000..74864d8b --- /dev/null +++ b/src/doUniformCenter.h @@ -0,0 +1,28 @@ +#ifndef _doUniformCenter_h +#define _doUniformCenter_h + +#include "doModifierMass.h" +#include "doUniform.h" + +template < typename EOT > +class doUniformCenter : public doModifierMass< doUniform< EOT > > +{ +public: + typedef typename EOT::AtomType AtomType; + + void operator() ( doUniform< EOT >& distrib, EOT& mass ) + { + for (unsigned int i = 0, n = mass.size(); i < n; ++i) + { + AtomType& min = distrib.min()[i]; + AtomType& max = distrib.max()[i]; + + AtomType range = (max - min) / 2; + + min = mass[i] - range; + max = mass[i] + range; + } + } +}; + +#endif // !_doUniformCenter_h diff --git a/src/doVectorBounds.h b/src/doVectorBounds.h new file mode 100644 index 00000000..65655c3c --- /dev/null +++ b/src/doVectorBounds.h @@ -0,0 +1,24 @@ +#ifndef _doVectorBounds_h +#define _doVectorBounds_h + +#include "doDistribParams.h" + +template < typename EOT > +class doVectorBounds : public doDistribParams< EOT > +{ +public: + doVectorBounds(EOT _min, EOT _max) + : doDistribParams< EOT >(2) + { + assert(_min.size() > 0); + assert(_min.size() == _max.size()); + + min() = _min; + max() = _max; + } + + EOT& min(){return this->param(0);} + EOT& max(){return this->param(1);} +}; + +#endif // !_doVectorBounds_h