From 35212ccc906586f68b574947756bbf57aee74dab Mon Sep 17 00:00:00 2001 From: Lionel Parreaux Date: Mon, 6 May 2013 15:27:19 +0200 Subject: [PATCH 01/36] adapted eoLogger for redirecting to ostreams --- eo/src/utils/eoLogger.cpp | 48 +++++++++++++++++++++++++++------------ eo/src/utils/eoLogger.h | 31 ++++++++++++++----------- eo/test/t-eoLogger.cpp | 24 ++++++++++++++++++-- 3 files changed, 73 insertions(+), 30 deletions(-) diff --git a/eo/src/utils/eoLogger.cpp b/eo/src/utils/eoLogger.cpp index 1330c5323..3a9b20098 100644 --- a/eo/src/utils/eoLogger.cpp +++ b/eo/src/utils/eoLogger.cpp @@ -40,6 +40,11 @@ Caner Candan #include "eoLogger.h" +/* TODO? +- changer oprateurs +- virer la structure "file" +*/ + void eoLogger::_init() { _standard_io_streams[&std::cout] = 1; @@ -55,6 +60,8 @@ void eoLogger::_init() addLevel("logging", eo::logging); addLevel("debug", eo::debug); addLevel("xdebug", eo::xdebug); + + //outStream = &std::cout; } eoLogger::eoLogger() : @@ -66,13 +73,12 @@ eoLogger::eoLogger() : _selectedLevel(eo::progress), _contextLevel(eo::quiet), - _fd(2), - _obuf(_fd, _contextLevel, _selectedLevel) + _obuf(_contextLevel, _selectedLevel) { std::ostream::init(&_obuf); _init(); } - +/* eoLogger::eoLogger(eo::file file) : std::ostream(NULL), @@ -88,11 +94,11 @@ eoLogger::eoLogger(eo::file file) : std::ostream::init(&_obuf); _init(); *this << file; -} +}*/ eoLogger::~eoLogger() { - if (_fd > 2) { ::close(_fd); } + //if (_fd > 2) { ::close(_fd); } } void eoLogger::_createParameters( eoParser& parser ) @@ -112,11 +118,14 @@ void eoLogger::_createParameters( eoParser& parser ) //------------------------------------------------------------------ // we're gonna redirect the log to the given filename if -o is used. //------------------------------------------------------------------ - +/* if ( ! _output.value().empty() ) { eo::log << eo::file( _output.value() ); } +*/ +// TODO with a stream + //------------------------------------------------------------------ @@ -162,12 +171,12 @@ eoLogger& operator<<(eoLogger& l, const eo::Levels lvl) l._contextLevel = lvl; return l; } - +/* eoLogger& operator<<(eoLogger& l, eo::file f) { l._fd = ::open(f._f.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644); return l; -} +}*/ eoLogger& operator<<(eoLogger& l, eo::setlevel v) { @@ -177,26 +186,35 @@ eoLogger& operator<<(eoLogger& l, eo::setlevel v) eoLogger& operator<<(eoLogger& l, std::ostream& os) { +/* if (l._standard_io_streams.find(&os) != l._standard_io_streams.end()) { l._fd = l._standard_io_streams[&os]; } +*/ + l._obuf.outStream = &os; return l; } -eoLogger::outbuf::outbuf(const int& fd, - const eo::Levels& contexlvl, +void eoLogger::redirectTo(std::ostream& os) +{ + _obuf.outStream = &os; +} + + +eoLogger::outbuf::outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl) - : _fd(fd), _contextLevel(contexlvl), _selectedLevel(selectedlvl) + : outStream(&std::cout), /*ownedFileStream(NULL),*/ _contextLevel(contexlvl), _selectedLevel(selectedlvl) {} int eoLogger::outbuf::overflow(int_type c) { if (_selectedLevel >= _contextLevel) { - if (_fd >= 0 && c != EOF) + if (outStream && c != EOF) { - ::write(_fd, &c, 1); + //::write(_fd, &c, 1); + (*outStream) << (char) c; } } return c; @@ -204,9 +222,9 @@ int eoLogger::outbuf::overflow(int_type c) namespace eo { - file::file(const std::string f) + /*file::file(const std::string f) : _f(f) - {} + {}*/ setlevel::setlevel(const std::string v) : _v(v), _lvl((Levels)-1) diff --git a/eo/src/utils/eoLogger.h b/eo/src/utils/eoLogger.h index 9ac1c2115..99bafa001 100644 --- a/eo/src/utils/eoLogger.h +++ b/eo/src/utils/eoLogger.h @@ -91,6 +91,7 @@ Caner Candan #include #include #include +#include #include "eoObject.h" #include "eoParser.h" @@ -113,12 +114,12 @@ namespace eo /** * file * this structure combined with the friend operator<< below is an easy way to select a file as output. - */ + * struct file { explicit file(const std::string f); const std::string _f; - }; + };*/ /** * setlevel @@ -146,7 +147,7 @@ public: eoLogger(); //! overidded ctor in order to instanciate a logger with a file define in parameter - eoLogger(eo::file file); + //eoLogger(eo::file file); //! dtor ~eoLogger(); @@ -189,11 +190,12 @@ private: class outbuf : public std::streambuf { public: - outbuf(const int& fd, const eo::Levels& contexlvl, const eo::Levels& selectedlvl); + outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl); + std::ostream * outStream; protected: virtual int overflow(int_type c); private: - const int& _fd; + //std::ofstream * ownedFileStream; const eo::Levels& _contextLevel; const eo::Levels& _selectedLevel; }; @@ -215,7 +217,7 @@ public: * operator<< used there to set a filename through the class file. */ //! in order to use stream style to define a file to dump instead the standart output - friend eoLogger& operator<<(eoLogger&, eo::file); + //friend eoLogger& operator<<(eoLogger&, eo::file); /** * operator<< used there to set a verbose level through the class setlevel. @@ -224,12 +226,21 @@ public: friend eoLogger& operator<<(eoLogger&, eo::setlevel); /** + * DEPRECATED: Use instead the redirectTo method * operator<< used there to be able to use std::cout to say that we wish to redirect back the buffer to a standard output. */ //! in order to use stream style to go back to a standart output defined by STL //! and to get retro-compatibility +#warning deprecated friend eoLogger& operator<<(eoLogger&, std::ostream&); + /** + * Redirects the logger to a given output stream. Closing the stream and returning its memory is at the charge of the caller, + * but should not be done while the log is still redirected to it. + */ + void redirectTo(std::ostream&); + + private: friend void make_verbose(eoParser&); @@ -244,13 +255,7 @@ private: eo::Levels _contextLevel; /** - * _fd in storing the file descriptor at this place we can disable easily the buffer in - * changing the value at -1. It is used by operator <<. - */ - int _fd; - - /** - * _obuf std::ostream mandates to use a buffer. _obuf is a outbuf inheriting of std::streambuf. + * _obuf std::ostream mandates to use a buffer. _obuf is a outbuf inheriting from std::streambuf. */ outbuf _obuf; diff --git a/eo/test/t-eoLogger.cpp b/eo/test/t-eoLogger.cpp index 198a88720..65f9df603 100644 --- a/eo/test/t-eoLogger.cpp +++ b/eo/test/t-eoLogger.cpp @@ -3,6 +3,10 @@ //----------------------------------------------------------------------------- #include +//#include +#include +#include +#include //----------------------------------------------------------------------------- @@ -25,8 +29,24 @@ int main(int ac, char** av) eo::log << "We are writing on the default output stream" << std::endl; - eo::log << eo::file("test.txt") << "In FILE" << std::endl; - eo::log << std::cout << "on COUT" << std::endl; + //eo::log << eo::file("test.txt") << "In FILE" << std::endl; + + std::ofstream ofs("test.txt"); + //eo::log << ofs << "In FILE" << std::endl; + eo::log.redirectTo(ofs); + eo::log << "In FILE" << std::endl; + + std::ostringstream oss; + //eo::log << oss << "In STRINGSTREAM"; + eo::log.redirectTo(oss); + eo::log << oss << "In STRINGSTREAM"; + + //ofs << oss; + std::cout << "Content of ostringstream: " << oss.str() << std::endl; + + //eo::log << std::cout << "on COUT" << std::endl; + eo::log.redirectTo(std::cout); + eo::log << "on COUT" << std::endl; eo::log << eo::setlevel("errors"); eo::log << eo::setlevel(eo::errors); From 002e4a196469f301e0dbb7fa0215ca85f9da88a5 Mon Sep 17 00:00:00 2001 From: Lionel Parreaux Date: Mon, 6 May 2013 17:33:36 +0200 Subject: [PATCH 02/36] added possibility to open files and close them automatically --- eo/src/utils/eoLogger.cpp | 87 ++++++++++++++++----------------------- eo/src/utils/eoLogger.h | 18 ++++---- eo/test/t-eoLogger.cpp | 11 +++-- 3 files changed, 52 insertions(+), 64 deletions(-) diff --git a/eo/src/utils/eoLogger.cpp b/eo/src/utils/eoLogger.cpp index 3a9b20098..79813137c 100644 --- a/eo/src/utils/eoLogger.cpp +++ b/eo/src/utils/eoLogger.cpp @@ -47,9 +47,6 @@ Caner Candan void eoLogger::_init() { - _standard_io_streams[&std::cout] = 1; - _standard_io_streams[&std::clog] = 2; - _standard_io_streams[&std::cerr] = 2; // /!\ If you want to add a level dont forget to add it at the header file in the enumerator Levels @@ -61,7 +58,6 @@ void eoLogger::_init() addLevel("debug", eo::debug); addLevel("xdebug", eo::xdebug); - //outStream = &std::cout; } eoLogger::eoLogger() : @@ -78,27 +74,13 @@ eoLogger::eoLogger() : std::ostream::init(&_obuf); _init(); } -/* -eoLogger::eoLogger(eo::file file) : - std::ostream(NULL), - - _verbose("quiet", "verbose", "Set the verbose level", 'v'), - _printVerboseLevels(false, "print-verbose-levels", "Print verbose levels", 'l'), - _output("", "output", "Redirect a standard output to a file", 'o'), - - _selectedLevel(eo::progress), - _contextLevel(eo::quiet), - _fd(2), - _obuf(_fd, _contextLevel, _selectedLevel) -{ - std::ostream::init(&_obuf); - _init(); - *this << file; -}*/ eoLogger::~eoLogger() { - //if (_fd > 2) { ::close(_fd); } + //redirect(NULL); + if (_obuf._ownedFileStream != NULL) { + delete _obuf._ownedFileStream; + } } void eoLogger::_createParameters( eoParser& parser ) @@ -116,22 +98,21 @@ void eoLogger::_createParameters( eoParser& parser ) //------------------------------------------------------------------ - // we're gonna redirect the log to the given filename if -o is used. + // we redirect the log to the given filename if -o is used. //------------------------------------------------------------------ -/* + if ( ! _output.value().empty() ) { - eo::log << eo::file( _output.value() ); + redirect(_output.value()); } -*/ -// TODO with a stream + //------------------------------------------------------------------ //------------------------------------------------------------------ - // we're gonna print the list of levels if -l parameter is used. + // we print the list of levels if -l parameter is used. //------------------------------------------------------------------ if ( _printVerboseLevels.value() ) @@ -171,12 +152,6 @@ eoLogger& operator<<(eoLogger& l, const eo::Levels lvl) l._contextLevel = lvl; return l; } -/* -eoLogger& operator<<(eoLogger& l, eo::file f) -{ - l._fd = ::open(f._f.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644); - return l; -}*/ eoLogger& operator<<(eoLogger& l, eo::setlevel v) { @@ -186,35 +161,49 @@ eoLogger& operator<<(eoLogger& l, eo::setlevel v) eoLogger& operator<<(eoLogger& l, std::ostream& os) { -/* - if (l._standard_io_streams.find(&os) != l._standard_io_streams.end()) - { - l._fd = l._standard_io_streams[&os]; - } -*/ - l._obuf.outStream = &os; + l._obuf._outStream = &os; return l; } -void eoLogger::redirectTo(std::ostream& os) +void eoLogger::redirect(std::ostream& os) { - _obuf.outStream = &os; + if (_obuf._ownedFileStream != NULL) { + delete _obuf._ownedFileStream; + _obuf._ownedFileStream = NULL; + } + _obuf._outStream = &os; +} + +void eoLogger::redirect(const char * filename) +{ + std::ofstream * os; + if (filename == NULL) { + os = NULL; + } else { + os = new std::ofstream(filename); + } + redirect(*os); + _obuf._ownedFileStream = os; +} + +void eoLogger::redirect(const std::string& filename) +{ + redirect(filename.c_str()); } eoLogger::outbuf::outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl) - : outStream(&std::cout), /*ownedFileStream(NULL),*/ _contextLevel(contexlvl), _selectedLevel(selectedlvl) + : _outStream(&std::cout), _ownedFileStream(NULL), _contextLevel(contexlvl), _selectedLevel(selectedlvl) {} int eoLogger::outbuf::overflow(int_type c) { if (_selectedLevel >= _contextLevel) { - if (outStream && c != EOF) + if (_outStream && c != EOF) { - //::write(_fd, &c, 1); - (*outStream) << (char) c; + (*_outStream) << (char) c; } } return c; @@ -222,10 +211,6 @@ int eoLogger::outbuf::overflow(int_type c) namespace eo { - /*file::file(const std::string f) - : _f(f) - {}*/ - setlevel::setlevel(const std::string v) : _v(v), _lvl((Levels)-1) {} diff --git a/eo/src/utils/eoLogger.h b/eo/src/utils/eoLogger.h index 99bafa001..e655848c7 100644 --- a/eo/src/utils/eoLogger.h +++ b/eo/src/utils/eoLogger.h @@ -191,11 +191,11 @@ private: { public: outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl); - std::ostream * outStream; + std::ostream * _outStream; + std::ofstream * _ownedFileStream; protected: virtual int overflow(int_type c); private: - //std::ofstream * ownedFileStream; const eo::Levels& _contextLevel; const eo::Levels& _selectedLevel; }; @@ -213,12 +213,6 @@ public: //! in order to use stream style to define the context verbose level where the following logs will be saved friend eoLogger& operator<<(eoLogger&, const eo::Levels); - /** - * operator<< used there to set a filename through the class file. - */ - //! in order to use stream style to define a file to dump instead the standart output - //friend eoLogger& operator<<(eoLogger&, eo::file); - /** * operator<< used there to set a verbose level through the class setlevel. */ @@ -238,8 +232,14 @@ public: * Redirects the logger to a given output stream. Closing the stream and returning its memory is at the charge of the caller, * but should not be done while the log is still redirected to it. */ - void redirectTo(std::ostream&); + void redirect(std::ostream&); + /** + * Redirects the logger to a file using a filename. + * Closing the file will be done automatically when the logger is redirected again or destroyed. + */ + void redirect(const char * filename); + void redirect(const std::string& filename); private: friend void make_verbose(eoParser&); diff --git a/eo/test/t-eoLogger.cpp b/eo/test/t-eoLogger.cpp index 65f9df603..a1ea4b60c 100644 --- a/eo/test/t-eoLogger.cpp +++ b/eo/test/t-eoLogger.cpp @@ -31,21 +31,24 @@ int main(int ac, char** av) //eo::log << eo::file("test.txt") << "In FILE" << std::endl; - std::ofstream ofs("test.txt"); + std::ofstream ofs("logtest.txt"); //eo::log << ofs << "In FILE" << std::endl; - eo::log.redirectTo(ofs); + eo::log.redirect(ofs); eo::log << "In FILE" << std::endl; + + eo::log.redirect("logtest2.txt"); + eo::log << "In FILE 2" << std::endl; std::ostringstream oss; //eo::log << oss << "In STRINGSTREAM"; - eo::log.redirectTo(oss); + eo::log.redirect(oss); eo::log << oss << "In STRINGSTREAM"; //ofs << oss; std::cout << "Content of ostringstream: " << oss.str() << std::endl; //eo::log << std::cout << "on COUT" << std::endl; - eo::log.redirectTo(std::cout); + eo::log.redirect(std::cout); eo::log << "on COUT" << std::endl; eo::log << eo::setlevel("errors"); From 1c8ff46848dd9856eab6c43419a885ec3404c188 Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 5 Jun 2013 15:46:33 +0200 Subject: [PATCH 03/36] cooling schedule changes --- mo/src/coolingSchedule/moCoolingSchedule.h | 3 +- .../moDynSpanCoolingSchedule.h | 39 ++++++++++--------- .../coolingSchedule/moSimpleCoolingSchedule.h | 6 ++- mo/src/explorer/moSAexplorer.h | 2 +- mo/test/t-moDynSpanCoolingSchedule.cpp | 18 ++++----- mo/test/t-moSimpleCoolingSchedule.cpp | 18 ++++----- 6 files changed, 45 insertions(+), 41 deletions(-) diff --git a/mo/src/coolingSchedule/moCoolingSchedule.h b/mo/src/coolingSchedule/moCoolingSchedule.h index 9b65cf1b5..b9863b02f 100644 --- a/mo/src/coolingSchedule/moCoolingSchedule.h +++ b/mo/src/coolingSchedule/moCoolingSchedule.h @@ -55,8 +55,9 @@ public: * update the temperature * @param _temp current temperature to update * @param _acceptedMove true when the move is accepted, false otherwise + * @param _currentSolution the current solution */ - virtual void update(double& _temp, bool _acceptedMove) = 0; + virtual void update(double& _temp, bool _acceptedMove, EOT & _currentSolution) = 0; }; diff --git a/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h b/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h index 34b386312..4e6216ad5 100644 --- a/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h +++ b/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h @@ -80,26 +80,27 @@ public: * update the temperature by a factor * @param _temp current temperature to update * @param _acceptedMove true when the move is accepted, false otherwise + * @param _currentSolution the current solution */ - virtual void update(double& _temp, bool _acceptedMove) { - spanTries++; - - if (_acceptedMove) - spanMove++; - - if (spanTries >= spanTriesMax || spanMove >= spanMoveMax) { - _temp *= alpha; - - if (spanMove == 0) // no move during this span ? - nbSpan++; - else - nbSpan = 0; - - spanTries = 0; - spanMove = 0; - } - } - + virtual void update(double& _temp, bool _acceptedMove, EOT & _currentSolution) { + spanTries++; + + if (_acceptedMove) + spanMove++; + + if (spanTries >= spanTriesMax || spanMove >= spanMoveMax) { + _temp *= alpha; + + if (spanMove == 0) // no move during this span ? + nbSpan++; + else + nbSpan = 0; + + spanTries = 0; + spanMove = 0; + } + } + /** * compare the number of span with no move * @param _temp current temperature diff --git a/mo/src/coolingSchedule/moSimpleCoolingSchedule.h b/mo/src/coolingSchedule/moSimpleCoolingSchedule.h index 5ff4f4920..aa8f1138f 100644 --- a/mo/src/coolingSchedule/moSimpleCoolingSchedule.h +++ b/mo/src/coolingSchedule/moSimpleCoolingSchedule.h @@ -52,7 +52,8 @@ public: * @param _span number of iteration with equal temperature * @param _finalT final temperature, threshold of the stopping criteria */ - moSimpleCoolingSchedule(double _initT, double _alpha, unsigned _span, double _finalT) : initT(_initT), alpha(_alpha), span(_span), finalT(_finalT) {} + moSimpleCoolingSchedule(double _initT, double _alpha, unsigned _span, double _finalT) + : initT(_initT), alpha(_alpha), span(_span), finalT(_finalT) {} /** * Getter on the initial temperature @@ -70,8 +71,9 @@ public: * update the temperature by a factor * @param _temp current temperature to update * @param _acceptedMove true when the move is accepted, false otherwise + * @param _currentSolution the current solution */ - virtual void update(double& _temp, bool _acceptedMove) { + virtual void update(double& _temp, bool _acceptedMove, EOT & _currentSolution) { if (step >= span) { _temp *= alpha; step = 0; diff --git a/mo/src/explorer/moSAexplorer.h b/mo/src/explorer/moSAexplorer.h index 3d9f477ee..37fa57955 100644 --- a/mo/src/explorer/moSAexplorer.h +++ b/mo/src/explorer/moSAexplorer.h @@ -96,7 +96,7 @@ public: * @param _solution unused solution */ virtual void updateParam(EOT & _solution) { - coolingSchedule.update(temperature, this->moveApplied()); + coolingSchedule.update(temperature, this->moveApplied(), _solution); }; /** diff --git a/mo/test/t-moDynSpanCoolingSchedule.cpp b/mo/test/t-moDynSpanCoolingSchedule.cpp index 7bef4ad59..da67008ea 100755 --- a/mo/test/t-moDynSpanCoolingSchedule.cpp +++ b/mo/test/t-moDynSpanCoolingSchedule.cpp @@ -48,38 +48,38 @@ int main() { assert(temperature == 100); //temperature must not changed 2* - test.update(temperature, 0); + test.update(temperature, 0, sol); assert(temperature == 100); assert(test(temperature)); - test.update(temperature, 0); + test.update(temperature, 0, sol); assert(temperature == 100); assert(test(temperature)); //then temperature must be /10 - test.update(temperature, 0); + test.update(temperature, 0, sol); assert(temperature == 10); assert(test(temperature)); - test.update(temperature, 0); + test.update(temperature, 0, sol); assert(temperature == 10); assert(test(temperature)); - test.update(temperature, 0); + test.update(temperature, 0, sol); assert(temperature == 10); assert(test(temperature)); - test.update(temperature, 0); + test.update(temperature, 0, sol); assert(temperature == 1); std::cout << "\n"; assert(test(temperature)); - test.update(temperature, 0); + test.update(temperature, 0, sol); std::cout << "\n"; assert(temperature == 1); assert(test(temperature)); - test.update(temperature, 0); + test.update(temperature, 0, sol); std::cout << "\n"; assert(temperature == 1); assert(test(temperature)); - test.update(temperature, 0); + test.update(temperature, 0, sol); assert(temperature == 0.1); assert(!test(temperature)); diff --git a/mo/test/t-moSimpleCoolingSchedule.cpp b/mo/test/t-moSimpleCoolingSchedule.cpp index b2f8cca17..dc78c6870 100644 --- a/mo/test/t-moSimpleCoolingSchedule.cpp +++ b/mo/test/t-moSimpleCoolingSchedule.cpp @@ -48,38 +48,38 @@ int main() { assert(temperature==100); //temperature must not changed 2* - test.update(temperature,0); + test.update(temperature,0,sol); assert(temperature==100); assert(test(temperature)); - test.update(temperature,0); + test.update(temperature,0,sol); assert(temperature==100); assert(test(temperature)); //then temperature must be /10 - test.update(temperature,0); + test.update(temperature,0,sol); assert(temperature==10); assert(test(temperature)); - test.update(temperature,0); + test.update(temperature,0,sol); assert(temperature==10); assert(test(temperature)); - test.update(temperature,0); + test.update(temperature,0,sol); assert(temperature==10); assert(test(temperature)); - test.update(temperature,0); + test.update(temperature,0,sol); assert(temperature == 1); std::cout<< "\n"; assert(test(temperature)); - test.update(temperature,0); + test.update(temperature,0,sol); std::cout<< "\n"; assert(temperature==1); assert(test(temperature)); - test.update(temperature,0); + test.update(temperature,0,sol); std::cout<< "\n"; assert(temperature==1); assert(test(temperature)); - test.update(temperature,0); + test.update(temperature,0,sol); assert(temperature==0.1); assert(!test(temperature)); From b66c38ce2cb39933db411c7cdf543e358008a5d3 Mon Sep 17 00:00:00 2001 From: LPTK Date: Mon, 10 Jun 2013 18:34:57 +0200 Subject: [PATCH 04/36] minor fixes --- eo/src/utils/eoLogger.cpp | 4 +-- eo/src/utils/eoLogger.h | 57 +++------------------------------------ eo/test/t-eoLogger.cpp | 6 ----- 3 files changed, 6 insertions(+), 61 deletions(-) diff --git a/eo/src/utils/eoLogger.cpp b/eo/src/utils/eoLogger.cpp index 79813137c..cd80ba67e 100644 --- a/eo/src/utils/eoLogger.cpp +++ b/eo/src/utils/eoLogger.cpp @@ -41,8 +41,8 @@ Caner Candan #include "eoLogger.h" /* TODO? -- changer oprateurs -- virer la structure "file" +add a possibility to redirect to several streams +add_redirect/remove_redirect */ void eoLogger::_init() diff --git a/eo/src/utils/eoLogger.h b/eo/src/utils/eoLogger.h index e655848c7..6c2599eb6 100644 --- a/eo/src/utils/eoLogger.h +++ b/eo/src/utils/eoLogger.h @@ -30,56 +30,7 @@ Caner Candan Global logger for EO. - Here's an example explaning how to use eoLogger: -\code - #include - - int main(int ac, char** av) - { - // We are declaring the usual eoParser class - eoParser parser(ac, av); - - // This call is important to allow -v parameter to change user level. - make_verbose(parser); - - // At this time we are switching to warning message and messages - // which are going to follow it are going to be warnings message too. - // These messages can be displayed only if the user level (sets with - // eo::setlevel function) is set to eo::warnings. - eo::log << eo::warnings; - - // With the following eo::file function we are defining that - // all future logs are going to this new file resource which is - // test.txt - eo::log << eo::file("test.txt") << "In FILE" << std::endl; - - // Now we are changing again the resources destination to cout which - // is the standard output. - eo::log << std::cout << "In COUT" << std::endl; - - // Here are 2 differents examples of how to set the errors user level - // in using either a string or an identifier. - eo::log << eo::setlevel("errors"); - eo::log << eo::setlevel(eo::errors); - - // Now we are writting a message, that will be displayed only if we are above the "quiet" level - eo::log << eo::quiet << "1) Must be in quiet mode to see that" << std::endl; - - // And so on... - eo::log << eo::setlevel(eo::warnings) << eo::warnings << "2) Must be in warnings mode to see that" << std::endl; - - eo::log << eo::setlevel(eo::logging); - - eo::log << eo::errors; - eo::log << "3) Must be in errors mode to see that"; - eo::log << std::endl; - - eo::log << eo::debug << 4 << ')' - << " Must be in debug mode to see that\n"; - - return 0; - } -\endcode + For an example explaning of how to use eoLogger, see paradiseo/eo/test/t-eoLogger.cpp @{ */ @@ -220,7 +171,7 @@ public: friend eoLogger& operator<<(eoLogger&, eo::setlevel); /** - * DEPRECATED: Use instead the redirectTo method + * DEPRECATED: Use instead the redirect method * operator<< used there to be able to use std::cout to say that we wish to redirect back the buffer to a standard output. */ //! in order to use stream style to go back to a standart output defined by STL @@ -230,7 +181,7 @@ public: /** * Redirects the logger to a given output stream. Closing the stream and returning its memory is at the charge of the caller, - * but should not be done while the log is still redirected to it. + * but should not be done while the logger is still redirected to it. */ void redirect(std::ostream&); @@ -249,7 +200,7 @@ private: eoValueParam _output; /** - * _selectedLevel is the member storing verbose level setted by the user thanks to operator() + * _selectedLevel is the member storing verbose level set by the user thanks to operator() */ eo::Levels _selectedLevel; eo::Levels _contextLevel; diff --git a/eo/test/t-eoLogger.cpp b/eo/test/t-eoLogger.cpp index a1ea4b60c..67a06c8e3 100644 --- a/eo/test/t-eoLogger.cpp +++ b/eo/test/t-eoLogger.cpp @@ -29,10 +29,7 @@ int main(int ac, char** av) eo::log << "We are writing on the default output stream" << std::endl; - //eo::log << eo::file("test.txt") << "In FILE" << std::endl; - std::ofstream ofs("logtest.txt"); - //eo::log << ofs << "In FILE" << std::endl; eo::log.redirect(ofs); eo::log << "In FILE" << std::endl; @@ -40,14 +37,11 @@ int main(int ac, char** av) eo::log << "In FILE 2" << std::endl; std::ostringstream oss; - //eo::log << oss << "In STRINGSTREAM"; eo::log.redirect(oss); eo::log << oss << "In STRINGSTREAM"; - //ofs << oss; std::cout << "Content of ostringstream: " << oss.str() << std::endl; - //eo::log << std::cout << "on COUT" << std::endl; eo::log.redirect(std::cout); eo::log << "on COUT" << std::endl; From de9af0ba21e221ca096520754b124b6acc6b6477 Mon Sep 17 00:00:00 2001 From: LPTK Date: Tue, 11 Jun 2013 14:08:06 +0200 Subject: [PATCH 05/36] new logger interface --- eo/src/utils/eoLogger.h | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/eo/src/utils/eoLogger.h b/eo/src/utils/eoLogger.h index 6c2599eb6..97cc73467 100644 --- a/eo/src/utils/eoLogger.h +++ b/eo/src/utils/eoLogger.h @@ -30,7 +30,7 @@ Caner Candan Global logger for EO. - For an example explaning of how to use eoLogger, see paradiseo/eo/test/t-eoLogger.cpp + For an example explaning how to use eoLogger, please refer to paradiseo/eo/test/t-eoLogger.cpp @{ */ @@ -171,7 +171,7 @@ public: friend eoLogger& operator<<(eoLogger&, eo::setlevel); /** - * DEPRECATED: Use instead the redirect method + * DEPRECATED: Use instead the redirectTo method * operator<< used there to be able to use std::cout to say that we wish to redirect back the buffer to a standard output. */ //! in order to use stream style to go back to a standart output defined by STL @@ -180,11 +180,27 @@ public: friend eoLogger& operator<<(eoLogger&, std::ostream&); /** - * Redirects the logger to a given output stream. Closing the stream and returning its memory is at the charge of the caller, - * but should not be done while the logger is still redirected to it. + * Redirects the logger to a given output stream. + * Closing the stream and returning its memory is at the charge of the caller, + * but should not be done while the log is still redirected to it. + * This resets all previous redirections set up with redirect and add_redirect. */ void redirect(std::ostream&); + /** + * Adds a redirection from the logger to a given output stream. + * Closing the stream and returning its memory is at the charge of the caller, + * but should not be done while the log is still redirected to it. + * This does not reset previous redirections, which remain active. + */ + void addRedirect(std::ostream&); + + /** + * Removes a redirection from the logger to the given output stream. + * This only resets the redirection to the stream passed in argument. + */ + void removeRedirect(std::ostream&); + /** * Redirects the logger to a file using a filename. * Closing the file will be done automatically when the logger is redirected again or destroyed. @@ -200,7 +216,7 @@ private: eoValueParam _output; /** - * _selectedLevel is the member storing verbose level set by the user thanks to operator() + * _selectedLevel is the member storing verbose level setted by the user thanks to operator() */ eo::Levels _selectedLevel; eo::Levels _contextLevel; From b30a15b746047fe6789ca32ff406a4598656a971 Mon Sep 17 00:00:00 2001 From: LPTK Date: Tue, 11 Jun 2013 16:04:42 +0200 Subject: [PATCH 06/36] logger tests with assertions --- eo/test/t-eoLogger.cpp | 46 +++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/eo/test/t-eoLogger.cpp b/eo/test/t-eoLogger.cpp index 67a06c8e3..355870671 100644 --- a/eo/test/t-eoLogger.cpp +++ b/eo/test/t-eoLogger.cpp @@ -7,6 +7,9 @@ #include #include #include +#include + +// TODO test multiple redirection //----------------------------------------------------------------------------- @@ -15,10 +18,10 @@ int main(int ac, char** av) eoParser parser(ac, av); if (parser.userNeedsHelp()) - { - parser.printHelp(std::cout); - exit(1); - } + { + parser.printHelp(std::cout); + exit(1); + } make_help(parser); make_verbose(parser); @@ -29,18 +32,37 @@ int main(int ac, char** av) eo::log << "We are writing on the default output stream" << std::endl; - std::ofstream ofs("logtest.txt"); - eo::log.redirect(ofs); - eo::log << "In FILE" << std::endl; - - eo::log.redirect("logtest2.txt"); - eo::log << "In FILE 2" << std::endl; + { + std::ofstream ofs("logtest.txt"); + eo::log.redirect(ofs); + eo::log << "In FILE" << std::endl; + eo::log.redirect("logtest2.txt"); + eo::log << "In FILE 2" << std::endl; + } + + std::ifstream ifs("logtest.txt"); + //ifs >> str; + std::string line; + assert(std::getline(ifs, line)); + assert(line == "In FILE"); + //std::cout << (line == "In FILE") << std::endl; + assert(!std::getline(ifs, line)); std::ostringstream oss; eo::log.redirect(oss); - eo::log << oss << "In STRINGSTREAM"; + //eo::log << oss << "In STRINGSTREAM"; + eo::log << "In STRINGSTREAM"; std::cout << "Content of ostringstream: " << oss.str() << std::endl; + assert(oss.str() == "In STRINGSTREAM"); + + ifs.close(); + ifs.open("logtest2.txt"); + assert(std::getline(ifs, line)); + assert(line == "In FILE 2"); + assert(!std::getline(ifs, line)); + //assert(false); + eo::log.redirect(std::cout); eo::log << "on COUT" << std::endl; @@ -59,7 +81,7 @@ int main(int ac, char** av) eo::log << std::endl; eo::log << eo::debug << 4 << ')' - << "4) in debug mode\n"; + << "4) in debug mode\n"; return 0; } From 11c32bb5f1388081b715c13a31c666f0802d99ce Mon Sep 17 00:00:00 2001 From: LPTK Date: Tue, 11 Jun 2013 17:01:52 +0200 Subject: [PATCH 07/36] performance tests map/vector --- eo/src/utils/eoLogger.cpp | 143 ++++++++++++++++++++++++++++++-------- eo/src/utils/eoLogger.h | 30 ++++++-- eo/test/t-eoLogger.cpp | 53 +++++++++++--- 3 files changed, 182 insertions(+), 44 deletions(-) diff --git a/eo/src/utils/eoLogger.cpp b/eo/src/utils/eoLogger.cpp index cd80ba67e..8a2c37831 100644 --- a/eo/src/utils/eoLogger.cpp +++ b/eo/src/utils/eoLogger.cpp @@ -37,6 +37,7 @@ Caner Candan #include // used to define EOF #include +#include // std::find #include "eoLogger.h" @@ -45,6 +46,13 @@ add a possibility to redirect to several streams add_redirect/remove_redirect */ +#ifdef USE_SET +typedef std::set::iterator StreamIter; +#else +typedef std::vector::iterator StreamIter; +#endif + + void eoLogger::_init() { @@ -61,7 +69,7 @@ void eoLogger::_init() } eoLogger::eoLogger() : - std::ostream(NULL), + std::ostream(NULL), _verbose("quiet", "verbose", "Set the verbose level", 'v'), _printVerboseLevels(false, "print-verbose-levels", "Print verbose levels", 'l'), @@ -71,16 +79,16 @@ eoLogger::eoLogger() : _contextLevel(eo::quiet), _obuf(_contextLevel, _selectedLevel) { - std::ostream::init(&_obuf); + std::ostream::init(&_obuf); _init(); } eoLogger::~eoLogger() { - //redirect(NULL); - if (_obuf._ownedFileStream != NULL) { - delete _obuf._ownedFileStream; - } + //redirect(NULL); + if (_obuf._ownedFileStream != NULL) { + delete _obuf._ownedFileStream; + } } void eoLogger::_createParameters( eoParser& parser ) @@ -161,29 +169,97 @@ eoLogger& operator<<(eoLogger& l, eo::setlevel v) eoLogger& operator<<(eoLogger& l, std::ostream& os) { - l._obuf._outStream = &os; +#warning deprecated + l.addRedirect(os); return l; } void eoLogger::redirect(std::ostream& os) { - if (_obuf._ownedFileStream != NULL) { - delete _obuf._ownedFileStream; - _obuf._ownedFileStream = NULL; - } - _obuf._outStream = &os; + doRedirect(&os); +} + +void eoLogger::doRedirect(std::ostream* os) +{ + if (_obuf._ownedFileStream != NULL) { + delete _obuf._ownedFileStream; + _obuf._ownedFileStream = NULL; + } + _obuf._outStreams.clear(); + if (os != NULL) + //_obuf._outStreams.push_back(os); + #ifdef USE_SET + _obuf._outStreams.insert(os); + #else + _obuf._outStreams.push_back(os); + #endif +} + +void eoLogger::addRedirect(std::ostream& os) +{ + /* + if (tryRemoveRedirect(&os)) + //throw eoWrongParamTypeException("Cannot redirect the logger to a stream it is already redirected to"); + //eo::log << eo::warnings << "Cannot redirect the logger to a stream it is already redirected to."; + { eo::log << eo::warnings << "Cannot redirect the logger to a stream it is already redirected to."; std::cout << "OK!!!" << std::endl; }*/ + bool already_there = tryRemoveRedirect(&os); + //_obuf._outStreams.push_back(&os); +#ifdef USE_SET + _obuf._outStreams.insert(&os); +#else + _obuf._outStreams.push_back(&os); +#endif + if (already_there) + eo::log << eo::warnings << "Cannot redirect the logger to a stream it is already redirected to." << std::endl; +} + +void eoLogger::removeRedirect(std::ostream& os) +{ + if (!tryRemoveRedirect(&os)) + //throw eoWrongParamTypeException("Cannot remove from the logger a stream it was not redirected to"); + //throw eoWrongParamTypeException(std::string("Cannot remove from the logger a stream it was not redirected to")); + eo::log << eo::warnings << "Cannot remove from the logger a stream it was not redirected to."; +} + +bool eoLogger::tryRemoveRedirect(std::ostream* os) +{ + /* + std::vector::iterator it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os); + //std::cout << _obuf._outStreams.size() << " " << (void*)&*_obuf._outStreams.begin() << " " << (void*)&*_obuf._outStreams.end() << std::endl; + //std::cout << "it: " << (void*)&*it << " " << (void*)os << std::endl; + if (it == _obuf._outStreams.end()) + return false; + _obuf._outStreams.erase(it); + return true;*/ + /* +#ifdef USE_SET + std::set::iterator it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os); + if (it == _obuf._outStreams.end()) + return false; + _obuf._outStreams.erase(it); +#else + std::vector::iterator it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os); + if (it == _obuf._outStreams.end()) + return false; + _obuf._outStreams.erase(it); +#endif*/ + StreamIter it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os); + if (it == _obuf._outStreams.end()) + return false; + _obuf._outStreams.erase(it); + return true; } void eoLogger::redirect(const char * filename) { - std::ofstream * os; - if (filename == NULL) { - os = NULL; - } else { - os = new std::ofstream(filename); - } - redirect(*os); - _obuf._ownedFileStream = os; + std::ofstream * os; + if (filename == NULL) { + os = NULL; + } else { + os = new std::ofstream(filename); + } + doRedirect(os); + _obuf._ownedFileStream = os; } void eoLogger::redirect(const std::string& filename) @@ -194,18 +270,29 @@ void eoLogger::redirect(const std::string& filename) eoLogger::outbuf::outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl) - : _outStream(&std::cout), _ownedFileStream(NULL), _contextLevel(contexlvl), _selectedLevel(selectedlvl) -{} + : +#ifndef USE_SET + _outStreams(1, &std::cout), +#endif + _ownedFileStream(NULL), _contextLevel(contexlvl), _selectedLevel(selectedlvl) +{ +#ifdef USE_SET + _outStreams.insert(&std::cout); +#endif +} int eoLogger::outbuf::overflow(int_type c) { if (_selectedLevel >= _contextLevel) - { - if (_outStream && c != EOF) - { - (*_outStream) << (char) c; - } - } + { + for (StreamIter it = _outStreams.begin(); it != _outStreams.end(); it++) + { + if (c != EOF) + { + (**it) << (char) c; + } + } + } return c; } diff --git a/eo/src/utils/eoLogger.h b/eo/src/utils/eoLogger.h index 97cc73467..81ce0da8e 100644 --- a/eo/src/utils/eoLogger.h +++ b/eo/src/utils/eoLogger.h @@ -1,6 +1,7 @@ // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- /* + (c) Thales group, 2010 This library is free software; you can redistribute it and/or @@ -47,6 +48,14 @@ Caner Candan #include "eoObject.h" #include "eoParser.h" +#define USE_SET +#undef USE_SET + +#ifdef USE_SET +#include +#endif + + namespace eo { /** @@ -133,6 +142,12 @@ private: //! used by the set of ctors to initiate some useful variables void _init(); + //! helper function regrouping redirection operations; takes a pointer because can be given NULL + void doRedirect(std::ostream*); + + //! helper function searching for a stream and removing it, returning true if successful, false otherwise + bool tryRemoveRedirect(std::ostream*); + private: /** * outbuf @@ -142,7 +157,14 @@ private: { public: outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl); - std::ostream * _outStream; + //std::ostream * _outStream; + + #ifdef USE_SET + std::set _outStreams; + #else + std::vector _outStreams; + #endif + std::ofstream * _ownedFileStream; protected: virtual int overflow(int_type c); @@ -171,12 +193,8 @@ public: friend eoLogger& operator<<(eoLogger&, eo::setlevel); /** - * DEPRECATED: Use instead the redirectTo method - * operator<< used there to be able to use std::cout to say that we wish to redirect back the buffer to a standard output. + * DEPRECATED: Use instead the redirect or addRedirect method; has the same effect as addRedirect */ - //! in order to use stream style to go back to a standart output defined by STL - //! and to get retro-compatibility -#warning deprecated friend eoLogger& operator<<(eoLogger&, std::ostream&); /** diff --git a/eo/test/t-eoLogger.cpp b/eo/test/t-eoLogger.cpp index 355870671..f030c2de3 100644 --- a/eo/test/t-eoLogger.cpp +++ b/eo/test/t-eoLogger.cpp @@ -13,6 +13,19 @@ //----------------------------------------------------------------------------- +static void test(); + +static void test2() +{ + #define NB 100 + std::ostream* os = (std::ostream*) 1; + eo::log.redirect(*os); + for (int i = 0; i < NB; i++) + eo::log.addRedirect(*(++os)); + for (int i = 0; i < NB; i++) + eo::log.removeRedirect(*(os--)); +} + int main(int ac, char** av) { eoParser parser(ac, av); @@ -26,6 +39,15 @@ int main(int ac, char** av) make_help(parser); make_verbose(parser); + for (int i = 0; i < 10000; i++) + //test(); + test2(); + + return 0; +} + +static void test() +{ eo::log << eo::setlevel(eo::debug); eo::log << eo::warnings; @@ -33,40 +55,52 @@ int main(int ac, char** av) eo::log << "We are writing on the default output stream" << std::endl; { - std::ofstream ofs("logtest.txt"); + /*std::ofstream ofs("logtest.txt"); eo::log.redirect(ofs); eo::log << "In FILE" << std::endl; eo::log.redirect("logtest2.txt"); + eo::log << "In FILE 2" << std::endl;*/ + eo::log.redirect("logtest.txt"); + eo::log << "In FILE" << std::endl; + std::ofstream ofs("logtest2.txt"); + eo::log.addRedirect(ofs); eo::log << "In FILE 2" << std::endl; + eo::log.removeRedirect(ofs); } - std::ifstream ifs("logtest.txt"); + std::ifstream ifs("logtest2.txt"); //ifs >> str; std::string line; assert(std::getline(ifs, line)); - assert(line == "In FILE"); + //std::cout << line << std::endl; + assert(line == "In FILE 2"); //std::cout << (line == "In FILE") << std::endl; assert(!std::getline(ifs, line)); std::ostringstream oss; - eo::log.redirect(oss); + eo::log.addRedirect(oss); //eo::log << oss << "In STRINGSTREAM"; eo::log << "In STRINGSTREAM"; std::cout << "Content of ostringstream: " << oss.str() << std::endl; assert(oss.str() == "In STRINGSTREAM"); + eo::log.redirect(std::cout); + eo::log << "on COUT" << std::endl; + + ifs.close(); - ifs.open("logtest2.txt"); + ifs.open("logtest.txt"); + assert(std::getline(ifs, line)); + assert(line == "In FILE"); assert(std::getline(ifs, line)); assert(line == "In FILE 2"); + assert(std::getline(ifs, line)); + assert(line == "In STRINGSTREAM"); assert(!std::getline(ifs, line)); //assert(false); - eo::log.redirect(std::cout); - eo::log << "on COUT" << std::endl; - eo::log << eo::setlevel("errors"); eo::log << eo::setlevel(eo::errors); @@ -82,8 +116,7 @@ int main(int ac, char** av) eo::log << eo::debug << 4 << ')' << "4) in debug mode\n"; - - return 0; + } //----------------------------------------------------------------------------- From 9fc61f5a3b3c7f8515d2c20de3b8980f073e9007 Mon Sep 17 00:00:00 2001 From: LPTK Date: Tue, 11 Jun 2013 17:11:20 +0200 Subject: [PATCH 08/36] performance tests adjusted --- eo/src/utils/eoLogger.h | 2 +- eo/test/t-eoLogger.cpp | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/eo/src/utils/eoLogger.h b/eo/src/utils/eoLogger.h index 81ce0da8e..e5fd34b34 100644 --- a/eo/src/utils/eoLogger.h +++ b/eo/src/utils/eoLogger.h @@ -49,7 +49,7 @@ Caner Candan #include "eoParser.h" #define USE_SET -#undef USE_SET +//#undef USE_SET #ifdef USE_SET #include diff --git a/eo/test/t-eoLogger.cpp b/eo/test/t-eoLogger.cpp index f030c2de3..431920ebe 100644 --- a/eo/test/t-eoLogger.cpp +++ b/eo/test/t-eoLogger.cpp @@ -22,8 +22,12 @@ static void test2() eo::log.redirect(*os); for (int i = 0; i < NB; i++) eo::log.addRedirect(*(++os)); + /* for (int i = 0; i < NB; i++) - eo::log.removeRedirect(*(os--)); + eo::log.removeRedirect(*(os--));*/ + os = (std::ostream*) 1; + for (int i = 0; i < NB; i++) + eo::log.removeRedirect(*(++os)); } int main(int ac, char** av) From a0464934cbe6810d0e73273b1fdda0344c07ca89 Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 12 Jun 2013 11:58:38 +0200 Subject: [PATCH 09/36] cleaning & doc --- eo/src/utils/eoLogger.cpp | 34 ---------------------------------- eo/src/utils/eoLogger.h | 18 ++++++------------ eo/test/t-eoLogger.cpp | 39 +++++---------------------------------- 3 files changed, 11 insertions(+), 80 deletions(-) diff --git a/eo/src/utils/eoLogger.cpp b/eo/src/utils/eoLogger.cpp index 8a2c37831..10371b0a0 100644 --- a/eo/src/utils/eoLogger.cpp +++ b/eo/src/utils/eoLogger.cpp @@ -41,10 +41,6 @@ Caner Candan #include "eoLogger.h" -/* TODO? -add a possibility to redirect to several streams -add_redirect/remove_redirect -*/ #ifdef USE_SET typedef std::set::iterator StreamIter; @@ -85,7 +81,6 @@ eoLogger::eoLogger() : eoLogger::~eoLogger() { - //redirect(NULL); if (_obuf._ownedFileStream != NULL) { delete _obuf._ownedFileStream; } @@ -187,7 +182,6 @@ void eoLogger::doRedirect(std::ostream* os) } _obuf._outStreams.clear(); if (os != NULL) - //_obuf._outStreams.push_back(os); #ifdef USE_SET _obuf._outStreams.insert(os); #else @@ -197,13 +191,7 @@ void eoLogger::doRedirect(std::ostream* os) void eoLogger::addRedirect(std::ostream& os) { - /* - if (tryRemoveRedirect(&os)) - //throw eoWrongParamTypeException("Cannot redirect the logger to a stream it is already redirected to"); - //eo::log << eo::warnings << "Cannot redirect the logger to a stream it is already redirected to."; - { eo::log << eo::warnings << "Cannot redirect the logger to a stream it is already redirected to."; std::cout << "OK!!!" << std::endl; }*/ bool already_there = tryRemoveRedirect(&os); - //_obuf._outStreams.push_back(&os); #ifdef USE_SET _obuf._outStreams.insert(&os); #else @@ -216,33 +204,11 @@ void eoLogger::addRedirect(std::ostream& os) void eoLogger::removeRedirect(std::ostream& os) { if (!tryRemoveRedirect(&os)) - //throw eoWrongParamTypeException("Cannot remove from the logger a stream it was not redirected to"); - //throw eoWrongParamTypeException(std::string("Cannot remove from the logger a stream it was not redirected to")); eo::log << eo::warnings << "Cannot remove from the logger a stream it was not redirected to."; } bool eoLogger::tryRemoveRedirect(std::ostream* os) { - /* - std::vector::iterator it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os); - //std::cout << _obuf._outStreams.size() << " " << (void*)&*_obuf._outStreams.begin() << " " << (void*)&*_obuf._outStreams.end() << std::endl; - //std::cout << "it: " << (void*)&*it << " " << (void*)os << std::endl; - if (it == _obuf._outStreams.end()) - return false; - _obuf._outStreams.erase(it); - return true;*/ - /* -#ifdef USE_SET - std::set::iterator it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os); - if (it == _obuf._outStreams.end()) - return false; - _obuf._outStreams.erase(it); -#else - std::vector::iterator it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os); - if (it == _obuf._outStreams.end()) - return false; - _obuf._outStreams.erase(it); -#endif*/ StreamIter it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os); if (it == _obuf._outStreams.end()) return false; diff --git a/eo/src/utils/eoLogger.h b/eo/src/utils/eoLogger.h index e5fd34b34..98ba09660 100644 --- a/eo/src/utils/eoLogger.h +++ b/eo/src/utils/eoLogger.h @@ -48,8 +48,12 @@ Caner Candan #include "eoObject.h" #include "eoParser.h" -#define USE_SET -//#undef USE_SET +#define USE_SET // defines if a set is to be used instead of a vector for storing streams the logger is redirected to +#undef USE_SET +/* expriments have shown that here a vector is by far faster than a set even if O(n), + * because it needs less dynamic allocations, uses less memory and less instructions + */ + #ifdef USE_SET #include @@ -71,16 +75,6 @@ namespace eo debug, xdebug}; - /** - * file - * this structure combined with the friend operator<< below is an easy way to select a file as output. - * - struct file - { - explicit file(const std::string f); - const std::string _f; - };*/ - /** * setlevel * this structure combined with the friend operator<< below is an easy way to set a verbose level. diff --git a/eo/test/t-eoLogger.cpp b/eo/test/t-eoLogger.cpp index 431920ebe..0406cb6b1 100644 --- a/eo/test/t-eoLogger.cpp +++ b/eo/test/t-eoLogger.cpp @@ -9,27 +9,10 @@ #include #include -// TODO test multiple redirection - //----------------------------------------------------------------------------- static void test(); -static void test2() -{ - #define NB 100 - std::ostream* os = (std::ostream*) 1; - eo::log.redirect(*os); - for (int i = 0; i < NB; i++) - eo::log.addRedirect(*(++os)); - /* - for (int i = 0; i < NB; i++) - eo::log.removeRedirect(*(os--));*/ - os = (std::ostream*) 1; - for (int i = 0; i < NB; i++) - eo::log.removeRedirect(*(++os)); -} - int main(int ac, char** av) { eoParser parser(ac, av); @@ -43,9 +26,7 @@ int main(int ac, char** av) make_help(parser); make_verbose(parser); - for (int i = 0; i < 10000; i++) - //test(); - test2(); + test(); return 0; } @@ -59,37 +40,28 @@ static void test() eo::log << "We are writing on the default output stream" << std::endl; { - /*std::ofstream ofs("logtest.txt"); - eo::log.redirect(ofs); - eo::log << "In FILE" << std::endl; - eo::log.redirect("logtest2.txt"); - eo::log << "In FILE 2" << std::endl;*/ eo::log.redirect("logtest.txt"); eo::log << "In FILE" << std::endl; - std::ofstream ofs("logtest2.txt"); + std::ofstream ofs("logtest2.txt"); // closed and destroyed at the en of the scope eo::log.addRedirect(ofs); eo::log << "In FILE 2" << std::endl; - eo::log.removeRedirect(ofs); + eo::log.removeRedirect(ofs); // must be removed because the associated stream is closed } - std::ifstream ifs("logtest2.txt"); - //ifs >> str; + std::ifstream ifs("logtest2.txt"); // stream to logtest2.txt is closed, we can start reading std::string line; assert(std::getline(ifs, line)); - //std::cout << line << std::endl; assert(line == "In FILE 2"); - //std::cout << (line == "In FILE") << std::endl; assert(!std::getline(ifs, line)); std::ostringstream oss; eo::log.addRedirect(oss); - //eo::log << oss << "In STRINGSTREAM"; eo::log << "In STRINGSTREAM"; std::cout << "Content of ostringstream: " << oss.str() << std::endl; assert(oss.str() == "In STRINGSTREAM"); - eo::log.redirect(std::cout); + eo::log.redirect(std::cout); // removes all previously redirected streams; closes the file logtest.txt eo::log << "on COUT" << std::endl; @@ -102,7 +74,6 @@ static void test() assert(std::getline(ifs, line)); assert(line == "In STRINGSTREAM"); assert(!std::getline(ifs, line)); - //assert(false); eo::log << eo::setlevel("errors"); From 23a7482f1c3fc7a6700e253da5ea35116e563e3c Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 12 Jun 2013 15:19:32 +0200 Subject: [PATCH 10/36] files for trikisa --- mo/src/continuator/moFitnessMomentsStat.h | 119 ++++++ mo/src/continuator/moStdDevEstimator.h | 143 +++++++ .../coolingSchedule/moTrikiCoolingSchedule.h | 363 ++++++++++++++++++ mo/src/neighborhood/moRealNeighbor.h | 87 +++++ mo/src/neighborhood/moRealNeighborhood.h | 78 ++++ mo/src/trikisa | 22 ++ mo/test/t-moTriki.cpp | 0 mo/test/t-moTrikiReal.cpp | 0 8 files changed, 812 insertions(+) create mode 100644 mo/src/continuator/moFitnessMomentsStat.h create mode 100644 mo/src/continuator/moStdDevEstimator.h create mode 100644 mo/src/coolingSchedule/moTrikiCoolingSchedule.h create mode 100644 mo/src/neighborhood/moRealNeighbor.h create mode 100644 mo/src/neighborhood/moRealNeighborhood.h create mode 100644 mo/src/trikisa create mode 100644 mo/test/t-moTriki.cpp create mode 100644 mo/test/t-moTrikiReal.cpp diff --git a/mo/src/continuator/moFitnessMomentsStat.h b/mo/src/continuator/moFitnessMomentsStat.h new file mode 100644 index 000000000..794d113b8 --- /dev/null +++ b/mo/src/continuator/moFitnessMomentsStat.h @@ -0,0 +1,119 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef moFitnessMomentsStat_h +#define moFitnessMomentsStat_h + +#include +#include + +/** + * Statistic that saves the standard deviation of the fitness of the solutions during the search + */ +template +//class moFitnessMomentsStat : public moStat > +class moFitnessMomentsStat : public moStat > +{ +public : + typedef typename EOT::Fitness Fitness; + //typedef std::pair Pair; + typedef std::pair Pair; + using moStat::value; + + /** + * Default Constructor + * @param _reInitSol when true the best so far is reinitialized + */ + moFitnessMomentsStat(bool _reInitSol = true) + : moStat(Pair(Fitness(), 0.0), "moments (average and stdev)"), + reInitSol(_reInitSol), firstTime(true), + nbSolutionsEncountered(0), currentAvg(0), currentVar(0) + { } + + /** + * Initialization of the best solution on the first one + * @param _sol the first solution + */ + virtual void init(EOT & _sol) { + if (reInitSol || firstTime) + { + value() = Pair(0.0,0.0); + nbSolutionsEncountered = currentAvg = currentVar = 0; + firstTime = false; + } + /*else if (firstTime) + { + value() = 0.0; + firstTime = false; + }*/ + operator()(_sol); + } + + /** + * Update the best solution + * @param _sol the current solution + */ + virtual void operator()(EOT & _sol) { + ++nbSolutionsEncountered; + double x = _sol.fitness(); + double oldAvg = currentAvg; + currentAvg = oldAvg + (x - oldAvg)/nbSolutionsEncountered; + if (nbSolutionsEncountered > 1) // <- not really necessary + { + //value() = (value()/nbSolutionsEncountered + _sol.fitness())/(nbSolutionsEncountered+1); + double oldVar = currentVar; + currentVar = oldVar + (x - oldAvg) * (x - currentAvg); + value() = Pair(currentAvg, currentVar/nbSolutionsEncountered); + } + } + + /** + * @return name of the class + */ + virtual std::string className(void) const { + return "moFitnessVarianceStat"; + } + +protected: + bool reInitSol; + bool firstTime; + double + nbSolutionsEncountered + , currentAvg + , currentVar // actually the var * n + ; + +}; + +#endif // moFitnessMomentsStat_h diff --git a/mo/src/continuator/moStdDevEstimator.h b/mo/src/continuator/moStdDevEstimator.h new file mode 100644 index 000000000..4d8902055 --- /dev/null +++ b/mo/src/continuator/moStdDevEstimator.h @@ -0,0 +1,143 @@ +#ifndef __moStdDevEstimator_h__ +#define __moStdDevEstimator_h__ + +#include +#include + +#include // TODO rm +#include // TODO rm + // TODO make tests + + +template< class T > +class eoOptional { +public: + static const eoOptional null; // = eoOptional(); + + eoOptional (T& init) + : _val(&init) + { } + + bool hasValue() const + { + return _val != NULL; + } + + T& get () const + { + if (!hasValue()) + throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value"); + return *_val; + } + +protected: + eoOptional () + : _val(NULL) + { } + +private: + T* _val; +}; + +template< class T > +const eoOptional eoOptional::null = eoOptional(); + + +template< class EOT, class Neighbor > +class moStdDevEstimator : public eoUF +{ +public: + + /** + * General constructor for the estimator + * @param continuator a user-defined continuator + * @param neighborhood the neighborhood + * @param fullEval the full evaluation function + * @param eval neighbor's evaluation function + * @param walker a local search algorithm + */ + moStdDevEstimator ( + moContinuator& continuator, + moNeighborhood < Neighbor > & neighborhood, + eoEvalFunc& fullEval, + + /* The following should be read: + moEval >& eval = _default_eval + * (which is not possible to achieve as is in C++) */ + const eoOptional< moEval >& eval = eoOptional< moEval >::null, + + const eoOptional< moLocalSearch >& walker = eoOptional< moLocalSearch >::null + ) + : _default_eval ( fullEval ), + _eval(eval.hasValue()? eval.get(): _default_eval), + _default_continuator( 0 ), + _continuator( _continuator ), + _checkpoint( _continuator ), + _default_walker( neighborhood, fullEval, _eval, _checkpoint ), + _walker( walker.hasValue()? walker.get(): _default_walker ) + { + _checkpoint.add( _varStat ); + } + + /** + * Simpler constructor for the estimator + * @param max_iters the number of steps the default moIterContinuator should perform + * @param neighborhood the neighborhood + * @param fullEval the full evaluation function + * @param eval neighbor's evaluation function + * @param walker a local search algorithm + */ + moStdDevEstimator ( + unsigned int max_iters, + moNeighborhood < Neighbor > & neighborhood, + eoEvalFunc& fullEval, + const eoOptional< moEval >& eval = eoOptional< moEval >::null, + const eoOptional< moLocalSearch >& walker = eoOptional< moLocalSearch >::null + ) + : _default_eval ( fullEval ), + _eval(eval.hasValue()? eval.get(): _default_eval), + _default_continuator( max_iters, false ), + _continuator( _default_continuator ), + _checkpoint( _continuator ), + _default_walker( neighborhood, fullEval, _eval, _checkpoint ), + _walker( walker.hasValue()? walker.get(): _default_walker ) + { + _checkpoint.add( _varStat ); + } + + /** + * Evaluates the estimator with the walker algorithm and returns the standard deviation + * @param solution the solution from where to start the walk + */ + double operator()( EOT & solution ) + { + _walker(solution); + return sqrt(_varStat.value()); + } + + /** + * @return the class name + */ + virtual std::string className(void) const { + return "moStdDevEstimator"; + } + +private: + + moFullEvalByCopy _default_eval; + moEval& _eval; + + moIterContinuator _default_continuator; + moContinuator & _continuator; + + moCheckpoint _checkpoint; + + moRandomWalk _default_walker; + moLocalSearch _walker; + + moFitnessVarianceStat _varStat; +}; + +#endif // __moStdDevEstimator_h__ + + diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h new file mode 100644 index 000000000..259f1747c --- /dev/null +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -0,0 +1,363 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008 + (C) OPAC Team, LIFL, 2002-2008 + + Sébastien Cahon, Jean-Charles Boisson (Jean-Charles.Boisson@lifl.fr) + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr + */ + +#ifndef _moTrikiCoolingSchedule_h +#define _moTrikiCoolingSchedule_h + +#include + +#include +#include +#include +#include +#include + + +/* +#include + +#include +#include +#include +#include + */ + +#include +using namespace std; + + +//! +/*! + */ +template< class EOT, class Neighbor > //, class Neighborhood > +class moTrikiCoolingSchedule: public moCoolingSchedule< EOT > +{ +public: + //typedef typename Neighbor::EOT EOT ; + typedef moNeighborhood Neighborhood ; + + //! Constructor + /*! + */ + + moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval& _eval, double _initTemp) + : initTemp(_initTemp), + mu2(10), // mu2 typically belongs to [1; 20] + K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature + K2(5), // ??? + lambda1(2), // the increase in temperature, typically in [1.5; 4] + lambda2(.7), // lambda2 in [0.5; 0.99] + mu1(10), // target decrease in cost factor, in [2; 20] + xi(1.05), // xi typically belongs to [1; 1.1] + max_accepted(50), // depends on pb/neighborhood + max_generated(100), // depends on pb/neighborhood + theta(10), // theta is typically set to 10 + statIsInitialized(false), + outf("out.data") + { } + + moTrikiCoolingSchedule ( + Neighborhood& _neighborhood, moEval& _eval, double _initTemp, + double _max_accepted, + double _max_generated + ) + : initTemp(_initTemp), + mu2(10), // mu2 typically belongs to [1; 20] + K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature + K2(5), // ??? + lambda1(2), // the increase in temperature, typically in [1.5; 4] + lambda2(.7), // lambda2 in [0.5; 0.99] + mu1(10), // target decrease in cost factor, in [2; 20] + xi(1.05), // xi typically belongs to [1; 1.1] + max_accepted(_max_accepted), // depends on pb/neighborhood + max_generated(_max_generated), // depends on pb/neighborhood + theta(10), // theta is typically set to 10 + statIsInitialized(false), + outf("out.data") + { } + + /** + * Initial temperature + * @param _solution initial solution + */ + double init(EOT & _solution) { + + accepted = generated = costs_sum = 0; + + negative_temp = equilibrium_not_reached = frozen = 0; + + reinitializing = false; + terminated = false; + statIsInitialized = false; + + /// + cout << "INIT T=" << initTemp << endl; + /// + + //outf.open("out"); + //outf << "ok"; + //outf.close(); + + + return initTemp; + } + + /** + * update the temperature by a factor + * @param _temp current temperature to update + * @param _acceptedMove true when the move is accepted, false otherwise + */ + void update(double& _temp, bool _acceptedMove, EOT & _solution) { + + //cout << _temp << " g " << generated << endl; + + generated++; + + if (_acceptedMove) + { + accepted++; + //costs_sum += _solution.fitness(); + //varStat(_solution); + if (statIsInitialized) + momStat(_solution); + else momStat.init(_solution), statIsInitialized = true; + + //cout << _solution.fitness() << " avgCost=" << momStat.value().first << endl; + } + + + if (accepted > max_accepted || generated > max_generated) { + + if (accepted == 0) // ADDED! Otherwise the computed std dev is null; we're probably at equilibrium + { + /// + cout << "Stopping: no accepted solution" << endl; + /// + + terminated = true; + return; + } + + /// + cout << (accepted > max_accepted? "MAXACC ": "MAXGEN "); + /// + + //double avgCost = costs_sum/(double)accepted; + //double stdDev = sqrt(varStat.value()); // WARNING: IT'S NO MORE THE AVG COST, NOW IT'S THE STD DEV! + //double variance = varStat.value(); + double avgCost = momStat.value().first; + double variance = momStat.value().second; + double stdDev = sqrt(variance); + double sigma = stdDev; + double delta = sigma/mu2; + + + //outf << avgCost << endl; + //outf << _temp << endl; + outf << prevAvgCost-delta << endl; + + + accepted = generated = costs_sum = 0; + //varStat.init(_solution);//TODON use next chain's first sol + //momStat.init(_solution);//TODONE use next chain's first sol + statIsInitialized = false; + + /// + cout << "T=" << _temp << " avgCost=" << avgCost << " stdDev=" << stdDev << " currCost=" << _solution.fitness() << endl; + /// + + double alpha; + double oldprevAvgCost = prevAvgCost; + + /// + cout << "negTemp: " << negative_temp << " / " << K2 << endl; + /// + + if (negative_temp < K2) + { + if (!reinitializing) + { + /// + if (avgCost/(prevAvgCost-delta) > xi) cout << "/!\\ eq not reached!" << endl; + /// + + if (avgCost/(prevAvgCost-delta) > xi) + equilibrium_not_reached++; + else equilibrium_not_reached = 0; + } + if (equilibrium_not_reached > K1) + { + /// + cout << "/!\\ Reinitializing (eq not reached)" << endl; + /// + + reinitializing = true; + alpha = lambda1; + delta = sigma/mu1; + equilibrium_not_reached = 0; // ADDED! Otherwise the algo gets trapped here! + } + else if (_temp*delta/(sigma*sigma) >= 1) + { + /// + cout << "/!\\ neg temp!" << endl; + /// + + negative_temp++; + reinitializing = true; + if (negative_temp < K2) + { + alpha = lambda1; + delta = sigma/mu1; + } else + alpha = lambda2; + } + + // First interpretation of the pseudocode indentation: (seems obviously false because it makes the above code unreachable) + /* + } + else + { + cout << "ccc" << endl; + reinitializing = false; + prevAvgCost = avgCost; + alpha = 1-_temp*delta/(sigma*sigma); + } + */ + + // Second interpretation of the pseudocode indentation: + else + { + /// + cout << "[normal decrease]" << endl; + /// + + reinitializing = false; + prevAvgCost = avgCost; + //alpha = 1-_temp*delta/(sigma*sigma); + alpha = 1-_temp*delta/variance; + + //alpha = (sigma==0? 1: 1-_temp*delta/(sigma*sigma)); // ADDED! but removed + + if (sigma == 0) // ADDED! When std dev is null, the solution is probably at eq, and the algo can't go on anyways + terminated = true, cout << "Stopping: null std dev" << endl; + } + } + // FIXME: else what? alpha=? + + + + /// + cout << "*=" << alpha << endl; + /// + + _temp *= alpha; + + + // Never seems to be used + if (avgCost == oldprevAvgCost) // use a neighborhood to approximate double equality? + frozen++; + else frozen = 0; + + + + //exit(0); + //cin.get(); + + + } + + } + + //! Function which proceeds to the cooling + /*! + */ + bool operator() (double temperature) + { + /// + if (terminated) cout << "TERMINATED" << endl; + /// + + return frozen < theta + && !terminated ; // ADDED! because 'frozen' doesn't terminate anything + } + +private: + //moNeighborhoodStat nhStat; + //moStdFitnessNeighborStat stdDevStat; + const double + // parameters of the algorithm + //currentTemp, + initTemp, + //ratio, + //threshold, + mu2, // mu2 typically belongs to [1; 20] + K1, // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature + K2, + lambda1, // the increase in temperature, typically in [1.5; 4] + lambda2, // lambda2 in [0.5; 0.99] + mu1, // target decrease in cost factor, in [2; 20] + xi // xi typically belongs to [1; 1.1] + // private variables + ; + double + stdDev, + prevAvgCost, + expectedDecreaseInCost, // delta + costs_sum + ; + const int + max_accepted, + max_generated, + theta // theta is typically set to 10 + ; + int + accepted, + generated, + equilibrium_not_reached, + negative_temp, + frozen + ; + bool reinitializing, terminated; + + //moFitnessVarianceStat varStat; + moFitnessMomentsStat momStat; + bool statIsInitialized; + + ofstream outf; + +}; + +#endif + diff --git a/mo/src/neighborhood/moRealNeighbor.h b/mo/src/neighborhood/moRealNeighbor.h new file mode 100644 index 000000000..4af8924f2 --- /dev/null +++ b/mo/src/neighborhood/moRealNeighbor.h @@ -0,0 +1,87 @@ +#ifndef __moRealNeighbor_h__ +#define __moRealNeighbor_h__ + +#include +#include +#include + +//! A neighbor as produced by a moRealNeighborhood +/*! + * In a real neighborhood, the move is just a translation vector, of the same type than a solution. + */ + +template +class moRealNeighbor : public moNeighbor +{ +protected: + //! The move to be applied + EOT _translation; + + edoBounder * _bounder; + + +public: + + moRealNeighbor() : _bounder( NULL ) { } + + //! Returns the solution attached to this neighbor + EOT translation() { return _translation; } + + //! Set the translation + void translation( EOT translation ) { _translation = translation; } + + + void bounder( edoBounder * bounder ) { _bounder = bounder; } + + /** + * Assignment operator + * @param _neighbor the neighbor to assign + * @return a neighbor equal to the other + */ + virtual moNeighbor& operator=(const moNeighbor& _neighbor) { + fitness( _neighbor.fitness() ); + return (*this); + } + + /*! + * Move a solution to the solution of this neighbor + * @param _solution the related solution + */ + virtual void move(EOT & _solution) + { + assert( _solution.size() == _translation.size() ); + + for( unsigned int i=0, size= _solution.size(); i& _neighbor) { + return _neighbor.translation() == _translation; + } + + /** + * Return the class Name + * @return the class name as a std::string + */ + virtual std::string className() const { + return "moRealNeighbor"; + } +}; + + +#endif // __moRealNeighbor_h__ + diff --git a/mo/src/neighborhood/moRealNeighborhood.h b/mo/src/neighborhood/moRealNeighborhood.h new file mode 100644 index 000000000..7e0a4a738 --- /dev/null +++ b/mo/src/neighborhood/moRealNeighborhood.h @@ -0,0 +1,78 @@ + +#ifndef __moRealNeighborhood_h__ +#define __moRealNeighborhood_h__ + +#include +#include // FIXME: Why don't we use eoFunctorBase on the mother classes +#include "moRealNeighbor.h" + +template +class moRealNeighborhood : public moRndNeighborhood< Neighbor >, public eoFunctorBase +{ +public: + typedef typename Distrib::EOType EOT; + +protected: + Distrib & _distrib; + edoSampler & _sampler; + edoBounder & _bounder; + +public: + + moRealNeighborhood( Distrib & distrib, edoSampler & sampler, edoBounder & bounder ) : _distrib(distrib), _sampler(sampler), _bounder(bounder) {} + + /** + * It alway remains at least a solution in an infinite neighborhood + * @param _solution the related solution + * @return true + */ + virtual bool hasNeighbor(EOT &) + { + return true; + } + + /** + * Draw the next neighbor + * @param _solution the solution to explore + * @param _current the next neighbor + */ + virtual void next(EOT &, Neighbor & _current) + { + _current.bounder( &_bounder ); + + // Draw a translation in the distrib, using the sampler + _current.translation( _sampler( _distrib ) ); + } + + + /** + * Initialization of the neighborhood + * @param _solution the solution to explore + * @param _current the first neighbor + */ + virtual void init(EOT & _solution, Neighbor & _current) + { + // there is no difference between an init and a random draw + next( _solution, _current ); + } + /** + * There is always a solution in an infinite neighborhood + * @param _solution the solution to explore + * @return true + */ + virtual bool cont(EOT &) + { + return true; + } + + /** + * Return the class Name + * @return the class name as a std::string + */ + virtual std::string className() const { + return "moRealNeighborhood"; + } + +}; + +#endif // __moRealNeighborhood_h__ diff --git a/mo/src/trikisa b/mo/src/trikisa new file mode 100644 index 000000000..63f59ff9a --- /dev/null +++ b/mo/src/trikisa @@ -0,0 +1,22 @@ +// (c) Thales group, 2010 +/* + Authors: + Johann Dreo + Caner Candan +*/ + +#ifndef _trikisa_ +#define _trikisa_ + +#include "moRealNeighbor.h" +#include "moRealNeighborhood.h" +#include "moStdDevEstimator.h" +#include "moTrikiCoolingSchedule.h" +#include "moFitnessVarianceStat.h" // TODO rm +#include "moFitnessMomentsStat.h" + +#endif // !_trikisa_ + +// Local Variables: +// mode: C++ +// End: diff --git a/mo/test/t-moTriki.cpp b/mo/test/t-moTriki.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/mo/test/t-moTrikiReal.cpp b/mo/test/t-moTrikiReal.cpp new file mode 100644 index 000000000..e69de29bb From 122b7442c1012167eddc78f378fe15f22aefc572 Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 12 Jun 2013 15:26:27 +0200 Subject: [PATCH 11/36] created eoOptional --- eo/src/eoOptional.h | 79 ++++++++++++++++++++++++++ mo/src/continuator/moStdDevEstimator.h | 34 ----------- 2 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 eo/src/eoOptional.h diff --git a/eo/src/eoOptional.h b/eo/src/eoOptional.h new file mode 100644 index 000000000..d3cdbff80 --- /dev/null +++ b/eo/src/eoOptional.h @@ -0,0 +1,79 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +/* +(c) Thales group, 2013 + + 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; + version 2 of the License. + + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + +/** @defgroup Logging Logging + * @ingroup Utilities + + + +@{ +*/ + +#ifndef _EOOPTIONAL_H +#define _EOOPTIONAL_H + +#include + + +template< class T > +class eoOptional { +public: + static const eoOptional null; // = eoOptional(); + + eoOptional (T& init) + : _val(&init) + { } + + bool hasValue() const + { + return _val != NULL; + } + + T& get () const + { + if (!hasValue()) + throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value"); + return *_val; + } + +protected: + eoOptional () + : _val(NULL) + { } + +private: + T* _val; +}; + +template< class T > +const eoOptional eoOptional::null = eoOptional(); + + +#endif // _EOOPTIONAL_H + + +/** @} */ + diff --git a/mo/src/continuator/moStdDevEstimator.h b/mo/src/continuator/moStdDevEstimator.h index 4d8902055..ff3b645d2 100644 --- a/mo/src/continuator/moStdDevEstimator.h +++ b/mo/src/continuator/moStdDevEstimator.h @@ -9,40 +9,6 @@ // TODO make tests -template< class T > -class eoOptional { -public: - static const eoOptional null; // = eoOptional(); - - eoOptional (T& init) - : _val(&init) - { } - - bool hasValue() const - { - return _val != NULL; - } - - T& get () const - { - if (!hasValue()) - throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value"); - return *_val; - } - -protected: - eoOptional () - : _val(NULL) - { } - -private: - T* _val; -}; - -template< class T > -const eoOptional eoOptional::null = eoOptional(); - - template< class EOT, class Neighbor > class moStdDevEstimator : public eoUF { From 3f817db6d47b6ef4f836100bc334810f1baf03dd Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 12 Jun 2013 15:38:40 +0200 Subject: [PATCH 12/36] doc --- eo/src/eoOptional.h | 48 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/eo/src/eoOptional.h b/eo/src/eoOptional.h index d3cdbff80..905da51d4 100644 --- a/eo/src/eoOptional.h +++ b/eo/src/eoOptional.h @@ -24,11 +24,48 @@ Lionel Parreaux */ -/** @defgroup Logging Logging - * @ingroup Utilities - +/** + +A utility class for wrapping non-const references and use them as default arguments in functions. + +For example, this is not valid C++98 code: + +\code +struct MyClass { + MyClass(T& my_T = default_T) + : actual_T(my_T) + { } +private: + T default_T; + T& actual_T; +} +\endcode + +This is the same code using eoOptional, which is valid: + +\code +struct MyClass { + MyClass(eoOptional my_T = NULL) + : actual_T(my_T.getOr(default_T)) + { } +private: + T default_T; + T& actual_T; +} +\endcode + +And from the point of view of the user, it is transparent: + +\code +// Three ways of using MyClass: +MyClass mc1; +MyClass mc2(NULL); +T t; +MyClass mc3(t); +\endcode +@ingroup Utilities @{ */ @@ -46,6 +83,11 @@ public: eoOptional (T& init) : _val(&init) { } + + // used mainly for converting NULL to this + eoOptional (T* init) + : _val(init) + { } bool hasValue() const { From 95a6e7a231dbcd0e38bfc758f55a6728e8fc9158 Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 12 Jun 2013 15:44:59 +0200 Subject: [PATCH 13/36] added a test and a method --- eo/src/eoOptional.h | 83 ++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/eo/src/eoOptional.h b/eo/src/eoOptional.h index 905da51d4..5c7bc58c7 100644 --- a/eo/src/eoOptional.h +++ b/eo/src/eoOptional.h @@ -32,26 +32,26 @@ For example, this is not valid C++98 code: \code struct MyClass { - MyClass(T& my_T = default_T) - : actual_T(my_T) - { } + MyClass(T& my_T = default_T) + : actual_T(my_T) + { } private: - T default_T; - T& actual_T; -} + T default_T; + T& actual_T; +}; \endcode This is the same code using eoOptional, which is valid: \code struct MyClass { - MyClass(eoOptional my_T = NULL) - : actual_T(my_T.getOr(default_T)) - { } + MyClass(eoOptional my_T = NULL) + : actual_T(my_T.getOr(default_T)) + { } private: - T default_T; - T& actual_T; -} + T default_T; + T& actual_T; +}; \endcode And from the point of view of the user, it is transparent: @@ -78,36 +78,41 @@ MyClass mc3(t); template< class T > class eoOptional { public: - static const eoOptional null; // = eoOptional(); - - eoOptional (T& init) - : _val(&init) - { } + static const eoOptional null; // = eoOptional(); + + eoOptional (T& init) + : _val(&init) + { } - // used mainly for converting NULL to this - eoOptional (T* init) - : _val(init) - { } - - bool hasValue() const - { - return _val != NULL; - } - - T& get () const - { - if (!hasValue()) - throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value"); - return *_val; - } - + // used mainly for converting NULL to this class + eoOptional (T* init) + : _val(init) + { } + + bool hasValue() const + { + return _val != NULL; + } + + T& get () const + { + if (!hasValue()) + throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value"); + return *_val; + } + + T& getOr (T& default) const + { + return hasValue()? *_val: default; + } + protected: - eoOptional () - : _val(NULL) - { } - + eoOptional () + : _val(NULL) + { } + private: - T* _val; + T* _val; }; template< class T > From eaa3960373c0d4356621699c71c28bbd47cdf1bb Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 12 Jun 2013 15:45:10 +0200 Subject: [PATCH 14/36] added a test --- eo/test/t-eoOptional.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 eo/test/t-eoOptional.cpp diff --git a/eo/test/t-eoOptional.cpp b/eo/test/t-eoOptional.cpp new file mode 100644 index 000000000..e22781a24 --- /dev/null +++ b/eo/test/t-eoOptional.cpp @@ -0,0 +1,27 @@ +//----------------------------------------------------------------------------- +// t-eoOptional.cpp +//----------------------------------------------------------------------------- + +#include "eoOptional.h" + +//----------------------------------------------------------------------------- + +typedef T int; + +struct MyClass { + MyClass(eoOptional my_T = NULL) + : actual_T(my_T.getOr(default_T)) + { } +private: + T default_T; + T& actual_T; +}; + +int main(int ac, char** av) +{ + // Three ways of using MyClass: + MyClass mc1; + MyClass mc2(NULL); + T t; + MyClass mc3(t); +} From d317cc2ea74f7de4e5bccdf1ddb2a9f4f09af2f2 Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 12 Jun 2013 15:59:07 +0200 Subject: [PATCH 15/36] fixed test & impl --- eo/src/eoOptional.h | 8 ++++++-- eo/test/CMakeLists.txt | 1 + eo/test/t-eoOptional.cpp | 10 ++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/eo/src/eoOptional.h b/eo/src/eoOptional.h index 5c7bc58c7..9143be3cd 100644 --- a/eo/src/eoOptional.h +++ b/eo/src/eoOptional.h @@ -27,6 +27,7 @@ Lionel Parreaux /** A utility class for wrapping non-const references and use them as default arguments in functions. +This especially is useful for avoiding constructor multiplication. For example, this is not valid C++98 code: @@ -72,7 +73,9 @@ MyClass mc3(t); #ifndef _EOOPTIONAL_H #define _EOOPTIONAL_H +#include #include +//#include template< class T > @@ -98,12 +101,13 @@ public: { if (!hasValue()) throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value"); + //throw eoEx; return *_val; } - T& getOr (T& default) const + T& getOr (T& defaultValue) const { - return hasValue()? *_val: default; + return hasValue()? *_val: defaultValue; } protected: diff --git a/eo/test/CMakeLists.txt b/eo/test/CMakeLists.txt index 15fd22464..e283ab958 100644 --- a/eo/test/CMakeLists.txt +++ b/eo/test/CMakeLists.txt @@ -69,6 +69,7 @@ set (TEST_LIST #t-openmp # does not work anymore since functions used in this test were removed from EO #t-eoDualFitness t-eoParser + t-eoOptional ) diff --git a/eo/test/t-eoOptional.cpp b/eo/test/t-eoOptional.cpp index e22781a24..bee58957c 100644 --- a/eo/test/t-eoOptional.cpp +++ b/eo/test/t-eoOptional.cpp @@ -6,12 +6,14 @@ //----------------------------------------------------------------------------- -typedef T int; +typedef int T; struct MyClass { MyClass(eoOptional my_T = NULL) - : actual_T(my_T.getOr(default_T)) - { } + : default_T(42), actual_T(my_T.getOr(default_T)) + { + std::cout << "Value " << actual_T << " was used for construction" << std::endl; + } private: T default_T; T& actual_T; @@ -22,6 +24,6 @@ int main(int ac, char** av) // Three ways of using MyClass: MyClass mc1; MyClass mc2(NULL); - T t; + T t(666); MyClass mc3(t); } From 930c21f46590403bdcf27c9e6089445be54fa4f4 Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 12 Jun 2013 16:05:41 +0200 Subject: [PATCH 16/36] added test files --- mo/src/continuator/moStdDevEstimator.h | 2 +- mo/test/CMakeLists.txt | 4 ++++ mo/test/t-moStdDevEstimator.cpp | 0 mo/test/t-moStdFitnessNeighborStat.cpp | 12 ++++++++++++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 mo/test/t-moStdDevEstimator.cpp create mode 100644 mo/test/t-moStdFitnessNeighborStat.cpp diff --git a/mo/src/continuator/moStdDevEstimator.h b/mo/src/continuator/moStdDevEstimator.h index ff3b645d2..e517240db 100644 --- a/mo/src/continuator/moStdDevEstimator.h +++ b/mo/src/continuator/moStdDevEstimator.h @@ -28,7 +28,7 @@ public: eoEvalFunc& fullEval, /* The following should be read: - moEval >& eval = _default_eval + moEval& eval = _default_eval * (which is not possible to achieve as is in C++) */ const eoOptional< moEval >& eval = eoOptional< moEval >::null, diff --git a/mo/test/CMakeLists.txt b/mo/test/CMakeLists.txt index 652b66be3..2cb8d97c6 100644 --- a/mo/test/CMakeLists.txt +++ b/mo/test/CMakeLists.txt @@ -94,7 +94,11 @@ set (TEST_LIST t-moStatistics t-moIndexedVectorTabuList # t-moRndIndexedVectorTabuList + t-moStdFitnessNeighborStat + t-moStdDevEstimator.cpp t-moDynSpanCoolingSchedule + t-moTriki.cpp + t-moTrikiReal.cpp ) ###################################################################################### diff --git a/mo/test/t-moStdDevEstimator.cpp b/mo/test/t-moStdDevEstimator.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/mo/test/t-moStdFitnessNeighborStat.cpp b/mo/test/t-moStdFitnessNeighborStat.cpp new file mode 100644 index 000000000..bd5ce0de0 --- /dev/null +++ b/mo/test/t-moStdFitnessNeighborStat.cpp @@ -0,0 +1,12 @@ +//----------------------------------------------------------------------------- +// t-moFitnessNeighborStat.cpp +//----------------------------------------------------------------------------- + +#include ".h" + +//----------------------------------------------------------------------------- + +int main(int ac, char** av) +{ + +} From aacba6f8138b4eb0b3f94f41ccfd8259395ff25a Mon Sep 17 00:00:00 2001 From: LPTK Date: Thu, 13 Jun 2013 11:26:15 +0200 Subject: [PATCH 17/36] stddev test --- mo/src/continuator/moStdFitnessNeighborStat.h | 2 +- mo/src/neighborhood/moRealNeighborhood.h | 2 +- mo/test/CMakeLists.txt | 7 ++++--- mo/test/t-moStdFitnessNeighborStat.cpp | 16 ++++++++++++++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/mo/src/continuator/moStdFitnessNeighborStat.h b/mo/src/continuator/moStdFitnessNeighborStat.h index b9a547954..4c058c8ee 100644 --- a/mo/src/continuator/moStdFitnessNeighborStat.h +++ b/mo/src/continuator/moStdFitnessNeighborStat.h @@ -42,7 +42,7 @@ * From moNeighborhoodStat, to compute the average and the standard deviation of fitness in the neighborhood */ template< class Neighbor > -class moStdFitnessNeighborStat : public moStat +class moStdFitnessNeighborStat : public moStat { public : typedef typename Neighbor::EOT EOT ; diff --git a/mo/src/neighborhood/moRealNeighborhood.h b/mo/src/neighborhood/moRealNeighborhood.h index 7e0a4a738..7cac43735 100644 --- a/mo/src/neighborhood/moRealNeighborhood.h +++ b/mo/src/neighborhood/moRealNeighborhood.h @@ -4,7 +4,7 @@ #include #include // FIXME: Why don't we use eoFunctorBase on the mother classes -#include "moRealNeighbor.h" +#include "neighbor/moRealNeighbor.h" template class moRealNeighborhood : public moRndNeighborhood< Neighbor >, public eoFunctorBase diff --git a/mo/test/CMakeLists.txt b/mo/test/CMakeLists.txt index 2cb8d97c6..6e8846be9 100644 --- a/mo/test/CMakeLists.txt +++ b/mo/test/CMakeLists.txt @@ -4,6 +4,7 @@ include_directories(${EO_SRC_DIR}/src) include_directories(${MO_SRC_DIR}/src) +include_directories(${EDO_SRC_DIR}/src) include_directories(${PROBLEMS_SRC_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) @@ -95,10 +96,10 @@ set (TEST_LIST t-moIndexedVectorTabuList # t-moRndIndexedVectorTabuList t-moStdFitnessNeighborStat - t-moStdDevEstimator.cpp + t-moStdDevEstimator t-moDynSpanCoolingSchedule - t-moTriki.cpp - t-moTrikiReal.cpp + t-moTriki + t-moTrikiReal ) ###################################################################################### diff --git a/mo/test/t-moStdFitnessNeighborStat.cpp b/mo/test/t-moStdFitnessNeighborStat.cpp index bd5ce0de0..d562dffa9 100644 --- a/mo/test/t-moStdFitnessNeighborStat.cpp +++ b/mo/test/t-moStdFitnessNeighborStat.cpp @@ -2,11 +2,23 @@ // t-moFitnessNeighborStat.cpp //----------------------------------------------------------------------------- -#include ".h" +#include +//#include "eoReal.h" +#include "continuator/moStdFitnessNeighborStat.h" +#include "neighborhood/moRealNeighbor.h" +#include "neighborhood/moRealNeighborhood.h" //----------------------------------------------------------------------------- +typedef eoReal< eoMinimizingFitness > EOT; +typedef moRealNeighbor< EOT > Neighbor; + int main(int ac, char** av) { - + moNeighborhoodStat nhStat + moStdFitnessNeighborStat stat(nhStat); + EOT solution(2, 5); + stat(solution); + //assert(stat.value() == 1); + std::cout << "ok " << stat.value() << endl; } From 716abe4610f67f1f7a7350fec8dc2a7b2aa1e2a2 Mon Sep 17 00:00:00 2001 From: LPTK Date: Thu, 13 Jun 2013 14:16:07 +0200 Subject: [PATCH 18/36] wrong file replaced with the right version --- mo/src/continuator/moFitnessVarianceStat.h | 115 ++++++++++++++++++ mo/test/CMakeLists.txt | 2 +- ...orStat.cpp => t-moFitnessVarianceStat.cpp} | 6 +- 3 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 mo/src/continuator/moFitnessVarianceStat.h rename mo/test/{t-moStdFitnessNeighborStat.cpp => t-moFitnessVarianceStat.cpp} (81%) diff --git a/mo/src/continuator/moFitnessVarianceStat.h b/mo/src/continuator/moFitnessVarianceStat.h new file mode 100644 index 000000000..3769c5983 --- /dev/null +++ b/mo/src/continuator/moFitnessVarianceStat.h @@ -0,0 +1,115 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef moFitnessVarianceStat_h +#define moFitnessVarianceStat_h + +#include + +/** + * Statistic that incrementally computes the variance of the fitness of the solutions during the search + */ +template +class moFitnessVarianceStat : public moStat +{ +public : + typedef typename EOT::Fitness Fitness; + using moStat< EOT , typename EOT::Fitness >::value; + + /** + * Default Constructor + * @param _reInitSol when true the best so far is reinitialized + */ + moFitnessVarianceStat(bool _reInitSol = true) + : moStat(Fitness(), "var"), + reInitSol(_reInitSol), firstTime(true), + nbSolutionsEncountered(0), currentAvg(0), currentVar(0) + { } + + /** + * Initialization of the best solution on the first one + * @param _sol the first solution + */ + virtual void init(EOT & _sol) { + if (reInitSol || firstTime) + { + value() = 0.0; + nbSolutionsEncountered = currentAvg = currentVar = 0; + firstTime = false; + } + /*else if (firstTime) + { + value() = 0.0; + firstTime = false; + }*/ + operator()(_sol); + } + + /** + * Update the best solution + * @param _sol the current solution + */ + virtual void operator()(EOT & _sol) { + ++nbSolutionsEncountered; + double x = _sol.fitness(); + double oldAvg = currentAvg; + currentAvg = oldAvg + (x - oldAvg)/nbSolutionsEncountered; + if (nbSolutionsEncountered > 1) // <- not really necessary + { + //value() = (value()/nbSolutionsEncountered + _sol.fitness())/(nbSolutionsEncountered+1); + double oldVar = currentVar; + currentVar = oldVar + (x - oldAvg) * (x - currentAvg); + value() = currentVar/nbSolutionsEncountered; + } + } + + /** + * @return name of the class + */ + virtual std::string className(void) const { + return "moFitnessVarianceStat"; + } + +protected: + bool reInitSol; + bool firstTime; + double + nbSolutionsEncountered + , currentAvg + , currentVar // actually the var * n + ; + +}; + +#endif // moFitnessVarianceStat_h diff --git a/mo/test/CMakeLists.txt b/mo/test/CMakeLists.txt index 6e8846be9..319ffb0c9 100644 --- a/mo/test/CMakeLists.txt +++ b/mo/test/CMakeLists.txt @@ -95,7 +95,7 @@ set (TEST_LIST t-moStatistics t-moIndexedVectorTabuList # t-moRndIndexedVectorTabuList - t-moStdFitnessNeighborStat + t-moFitnessVarianceStat t-moStdDevEstimator t-moDynSpanCoolingSchedule t-moTriki diff --git a/mo/test/t-moStdFitnessNeighborStat.cpp b/mo/test/t-moFitnessVarianceStat.cpp similarity index 81% rename from mo/test/t-moStdFitnessNeighborStat.cpp rename to mo/test/t-moFitnessVarianceStat.cpp index d562dffa9..f1082864d 100644 --- a/mo/test/t-moStdFitnessNeighborStat.cpp +++ b/mo/test/t-moFitnessVarianceStat.cpp @@ -4,7 +4,7 @@ #include //#include "eoReal.h" -#include "continuator/moStdFitnessNeighborStat.h" +#include "continuator/moFitnessVarianceStat.h" #include "neighborhood/moRealNeighbor.h" #include "neighborhood/moRealNeighborhood.h" @@ -15,8 +15,8 @@ typedef moRealNeighbor< EOT > Neighbor; int main(int ac, char** av) { - moNeighborhoodStat nhStat - moStdFitnessNeighborStat stat(nhStat); + //moNeighborhoodStat nhStat + moFitnessVarianceStat stat(); EOT solution(2, 5); stat(solution); //assert(stat.value() == 1); From b1067a05259354492bd9b58e824f4f974da2f32d Mon Sep 17 00:00:00 2001 From: LPTK Date: Thu, 13 Jun 2013 14:55:01 +0200 Subject: [PATCH 19/36] minor fixes --- mo/src/neighborhood/moRealNeighborhood.h | 2 +- mo/test/t-moFitnessVarianceStat.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mo/src/neighborhood/moRealNeighborhood.h b/mo/src/neighborhood/moRealNeighborhood.h index 7cac43735..5efa3ec6f 100644 --- a/mo/src/neighborhood/moRealNeighborhood.h +++ b/mo/src/neighborhood/moRealNeighborhood.h @@ -4,7 +4,7 @@ #include #include // FIXME: Why don't we use eoFunctorBase on the mother classes -#include "neighbor/moRealNeighbor.h" +#include "neighborhood/moRealNeighbor.h" template class moRealNeighborhood : public moRndNeighborhood< Neighbor >, public eoFunctorBase diff --git a/mo/test/t-moFitnessVarianceStat.cpp b/mo/test/t-moFitnessVarianceStat.cpp index f1082864d..3975338cc 100644 --- a/mo/test/t-moFitnessVarianceStat.cpp +++ b/mo/test/t-moFitnessVarianceStat.cpp @@ -3,7 +3,7 @@ //----------------------------------------------------------------------------- #include -//#include "eoReal.h" +#include "es/eoReal.h" #include "continuator/moFitnessVarianceStat.h" #include "neighborhood/moRealNeighbor.h" #include "neighborhood/moRealNeighborhood.h" @@ -16,9 +16,9 @@ typedef moRealNeighbor< EOT > Neighbor; int main(int ac, char** av) { //moNeighborhoodStat nhStat - moFitnessVarianceStat stat(); + moFitnessVarianceStat stat; EOT solution(2, 5); stat(solution); //assert(stat.value() == 1); - std::cout << "ok " << stat.value() << endl; + std::cout << "ok " << stat.value() << std::endl; } From 93b4e2568c8921f66cdc2a054b091656b3a3e7b4 Mon Sep 17 00:00:00 2001 From: LPTK Date: Thu, 13 Jun 2013 15:05:17 +0200 Subject: [PATCH 20/36] fixed test --- mo/test/t-moFitnessVarianceStat.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mo/test/t-moFitnessVarianceStat.cpp b/mo/test/t-moFitnessVarianceStat.cpp index 3975338cc..6bb5faf8e 100644 --- a/mo/test/t-moFitnessVarianceStat.cpp +++ b/mo/test/t-moFitnessVarianceStat.cpp @@ -13,11 +13,25 @@ typedef eoReal< eoMinimizingFitness > EOT; typedef moRealNeighbor< EOT > Neighbor; +double objective_function(const EOT & sol) +{ + double sum = 0; + + for ( size_t i = 0; i < sol.size(); ++i ) + { + sum += sol[i] * sol[i]; + } + + return sum; +} + int main(int ac, char** av) { //moNeighborhoodStat nhStat moFitnessVarianceStat stat; + eoEvalFuncPtr< EOT, double > eval( objective_function ); EOT solution(2, 5); + eval(solution); stat(solution); //assert(stat.value() == 1); std::cout << "ok " << stat.value() << std::endl; From 737cae0469dda1a48cb6cc1d6a9d4f6fd1c6dd30 Mon Sep 17 00:00:00 2001 From: LPTK Date: Thu, 13 Jun 2013 15:19:18 +0200 Subject: [PATCH 21/36] finished test for var stat --- mo/test/t-moFitnessVarianceStat.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mo/test/t-moFitnessVarianceStat.cpp b/mo/test/t-moFitnessVarianceStat.cpp index 6bb5faf8e..b90070254 100644 --- a/mo/test/t-moFitnessVarianceStat.cpp +++ b/mo/test/t-moFitnessVarianceStat.cpp @@ -19,6 +19,7 @@ double objective_function(const EOT & sol) for ( size_t i = 0; i < sol.size(); ++i ) { + //std::cout << sol[i] << std::endl; sum += sol[i] * sol[i]; } @@ -33,6 +34,11 @@ int main(int ac, char** av) EOT solution(2, 5); eval(solution); stat(solution); + solution[0] = solution[1] = 0; + solution.invalidate(); + eval(solution); + stat(solution); //assert(stat.value() == 1); - std::cout << "ok " << stat.value() << std::endl; + std::cout << "var: " << stat.value() << std::endl; + assert(stat.value() == 625); } From a1ce594a38695ccd04d452250e7c84f387e758e7 Mon Sep 17 00:00:00 2001 From: LPTK Date: Thu, 13 Jun 2013 15:37:47 +0200 Subject: [PATCH 22/36] cleaning --- mo/src/continuator/moFitnessVarianceStat.h | 15 +++------------ mo/test/t-moFitnessVarianceStat.cpp | 3 --- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/mo/src/continuator/moFitnessVarianceStat.h b/mo/src/continuator/moFitnessVarianceStat.h index 3769c5983..1d4c7f4b6 100644 --- a/mo/src/continuator/moFitnessVarianceStat.h +++ b/mo/src/continuator/moFitnessVarianceStat.h @@ -68,11 +68,6 @@ public : nbSolutionsEncountered = currentAvg = currentVar = 0; firstTime = false; } - /*else if (firstTime) - { - value() = 0.0; - firstTime = false; - }*/ operator()(_sol); } @@ -85,13 +80,9 @@ public : double x = _sol.fitness(); double oldAvg = currentAvg; currentAvg = oldAvg + (x - oldAvg)/nbSolutionsEncountered; - if (nbSolutionsEncountered > 1) // <- not really necessary - { - //value() = (value()/nbSolutionsEncountered + _sol.fitness())/(nbSolutionsEncountered+1); - double oldVar = currentVar; - currentVar = oldVar + (x - oldAvg) * (x - currentAvg); - value() = currentVar/nbSolutionsEncountered; - } + double oldVar = currentVar; + currentVar = oldVar + (x - oldAvg) * (x - currentAvg); + value() = currentVar/nbSolutionsEncountered; } /** diff --git a/mo/test/t-moFitnessVarianceStat.cpp b/mo/test/t-moFitnessVarianceStat.cpp index b90070254..8f343a6ac 100644 --- a/mo/test/t-moFitnessVarianceStat.cpp +++ b/mo/test/t-moFitnessVarianceStat.cpp @@ -19,7 +19,6 @@ double objective_function(const EOT & sol) for ( size_t i = 0; i < sol.size(); ++i ) { - //std::cout << sol[i] << std::endl; sum += sol[i] * sol[i]; } @@ -28,7 +27,6 @@ double objective_function(const EOT & sol) int main(int ac, char** av) { - //moNeighborhoodStat nhStat moFitnessVarianceStat stat; eoEvalFuncPtr< EOT, double > eval( objective_function ); EOT solution(2, 5); @@ -38,7 +36,6 @@ int main(int ac, char** av) solution.invalidate(); eval(solution); stat(solution); - //assert(stat.value() == 1); std::cout << "var: " << stat.value() << std::endl; assert(stat.value() == 625); } From ef34af74938eb0a54ce8fd9454003dce233da5ca Mon Sep 17 00:00:00 2001 From: LPTK Date: Thu, 13 Jun 2013 16:06:14 +0200 Subject: [PATCH 23/36] test for stddevest --- eo/src/eo | 1 + .../moStdDevEstimator.h | 2 +- mo/test/t-moStdDevEstimator.cpp | 76 +++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) rename mo/src/{continuator => sampling}/moStdDevEstimator.h (98%) diff --git a/eo/src/eo b/eo/src/eo index 8196e2b14..5057371d3 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -35,6 +35,7 @@ // general purpose #include #include +#include #include #include #include diff --git a/mo/src/continuator/moStdDevEstimator.h b/mo/src/sampling/moStdDevEstimator.h similarity index 98% rename from mo/src/continuator/moStdDevEstimator.h rename to mo/src/sampling/moStdDevEstimator.h index e517240db..338984f3b 100644 --- a/mo/src/continuator/moStdDevEstimator.h +++ b/mo/src/sampling/moStdDevEstimator.h @@ -4,7 +4,7 @@ #include #include -#include // TODO rm +#include // TODO rm #include // TODO rm // TODO make tests diff --git a/mo/test/t-moStdDevEstimator.cpp b/mo/test/t-moStdDevEstimator.cpp index e69de29bb..43f51cb8c 100644 --- a/mo/test/t-moStdDevEstimator.cpp +++ b/mo/test/t-moStdDevEstimator.cpp @@ -0,0 +1,76 @@ +//----------------------------------------------------------------------------- +// t-moStdDevEstimator.cpp +//----------------------------------------------------------------------------- + +#include +#include "es/eoReal.h" +#include "continuator/moFitnessVarianceStat.h" +#include "neighborhood/moRealNeighbor.h" +#include "neighborhood/moRealNeighborhood.h" + +//Representation and initializer +#include +#include +#include + +// fitness function +#include +#include +#include + +//Neighbors and Neighborhoods +#include +#include + +//Algorithm and its components +#include +#include + +//comparator +#include + + +#include + + +//----------------------------------------------------------------------------- +// Define types of the representation solution, different neighbors and neighborhoods +//----------------------------------------------------------------------------- +typedef eoInt Queen; //Permutation (Queen's problem representation) + +typedef moShiftNeighbor shiftNeighbor; //shift Neighbor +typedef moRndWithReplNeighborhood rndShiftNeighborhood; //rnd shift Neighborhood (Indexed) + +//----------------------------------------------------------------------------- + +typedef eoReal< eoMinimizingFitness > EOT; +typedef moRealNeighbor< EOT > Neighbor; + +int main(int ac, char** av) +{ + + + unsigned vecSize = 8; + + + queenEval fullEval; + + + eoInitPermutation init(vecSize); + + //moFullEvalByCopy shiftEval(fullEval); /// by default + + rndShiftNeighborhood rndShiftNH((vecSize-1) * (vecSize-1)); + + Queen solution; + + init(solution); + + fullEval(solution); + + moStdDevEstimator initTemp (500, rndShiftNH, fullEval); + + + std::cout << "temp: " << initTemp(solution) << std::endl; + //assert(stat.value() == 625); +} From 78a793284672e8af32fb925ad3e71fafe2ffc72e Mon Sep 17 00:00:00 2001 From: LPTK Date: Thu, 13 Jun 2013 16:29:34 +0200 Subject: [PATCH 24/36] test cleaning --- mo/test/t-moStdDevEstimator.cpp | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/mo/test/t-moStdDevEstimator.cpp b/mo/test/t-moStdDevEstimator.cpp index 43f51cb8c..a3a6d4e77 100644 --- a/mo/test/t-moStdDevEstimator.cpp +++ b/mo/test/t-moStdDevEstimator.cpp @@ -4,32 +4,21 @@ #include #include "es/eoReal.h" -#include "continuator/moFitnessVarianceStat.h" #include "neighborhood/moRealNeighbor.h" -#include "neighborhood/moRealNeighborhood.h" //Representation and initializer #include -#include +//#include #include // fitness function #include -#include -#include //Neighbors and Neighborhoods #include #include -//Algorithm and its components -#include -#include - -//comparator -#include - - +//Sampling #include @@ -48,18 +37,12 @@ typedef moRealNeighbor< EOT > Neighbor; int main(int ac, char** av) { - - unsigned vecSize = 8; - queenEval fullEval; - eoInitPermutation init(vecSize); - //moFullEvalByCopy shiftEval(fullEval); /// by default - rndShiftNeighborhood rndShiftNH((vecSize-1) * (vecSize-1)); Queen solution; @@ -70,7 +53,9 @@ int main(int ac, char** av) moStdDevEstimator initTemp (500, rndShiftNH, fullEval); + double temp = initTemp(solution); - std::cout << "temp: " << initTemp(solution) << std::endl; - //assert(stat.value() == 625); + std::cout << "temp: " << temp << std::endl; + + assert(temp >= 0); } From 34dcdb181c91a0b966f0f5f1192d3bd2616005d1 Mon Sep 17 00:00:00 2001 From: LPTK Date: Fri, 14 Jun 2013 17:03:55 +0200 Subject: [PATCH 25/36] minor changes --- mo/src/continuator/moFitnessVarianceStat.h | 43 +++++++---------- .../coolingSchedule/moTrikiCoolingSchedule.h | 46 ++++++++----------- mo/src/explorer/moMetropolisHastingExplorer.h | 14 +++++- mo/src/sampling/moStdDevEstimator.h | 25 ++++++++++ mo/test/t-moFitnessVarianceStat.cpp | 25 ++++++++++ mo/test/t-moStdDevEstimator.cpp | 25 ++++++++++ 6 files changed, 122 insertions(+), 56 deletions(-) diff --git a/mo/src/continuator/moFitnessVarianceStat.h b/mo/src/continuator/moFitnessVarianceStat.h index 1d4c7f4b6..3415ab3ea 100644 --- a/mo/src/continuator/moFitnessVarianceStat.h +++ b/mo/src/continuator/moFitnessVarianceStat.h @@ -1,35 +1,26 @@ /* - - Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau +(c) Thales group, 2010 - This software is governed by the CeCILL license under French law and - abiding by the rules of distribution of free software. You can use, - modify and/ or redistribute the software under the terms of the CeCILL - license as circulated by CEA, CNRS and INRIA at the following URL - "http://www.cecill.info". + 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; + version 2 of the License. - As a counterpart to the access to the source code and rights to copy, - modify and redistribute granted by the license, users are provided only - with a limited warranty and the software's author, the holder of the - economic rights, and the successive licensors have only limited liability. + 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. - In this respect, the user's attention is drawn to the risks associated - with loading, using, modifying and/or developing or reproducing the - software by the user in light of its specific status of free software, - that may mean that it is complicated to manipulate, and that also - therefore means that it is reserved for developers and experienced - professionals having in-depth computer knowledge. Users are therefore - encouraged to load and test the software's suitability as regards their - requirements in conditions enabling the security of their systems and/or - data to be ensured and, more generally, to use and operate it in the - same conditions as regards security. - The fact that you are presently reading this means that you have had - knowledge of the CeCILL license and that you accept its terms. + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux - ParadisEO WebSite : http://paradiseo.gforge.inria.fr - Contact: paradiseo-help@lists.gforge.inria.fr */ #ifndef moFitnessVarianceStat_h diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index 259f1747c..d98baa74c 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -1,37 +1,27 @@ /* - - Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008 - (C) OPAC Team, LIFL, 2002-2008 - Sébastien Cahon, Jean-Charles Boisson (Jean-Charles.Boisson@lifl.fr) +(c) Thales group, 2010 - This software is governed by the CeCILL license under French law and - abiding by the rules of distribution of free software. You can use, - modify and/ or redistribute the software under the terms of the CeCILL - license as circulated by CEA, CNRS and INRIA at the following URL - "http://www.cecill.info". + 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; + version 2 of the License. - As a counterpart to the access to the source code and rights to copy, - modify and redistribute granted by the license, users are provided only - with a limited warranty and the software's author, the holder of the - economic rights, and the successive licensors have only limited liability. + 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. - In this respect, the user's attention is drawn to the risks associated - with loading, using, modifying and/or developing or reproducing the - software by the user in light of its specific status of free software, - that may mean that it is complicated to manipulate, and that also - therefore means that it is reserved for developers and experienced - professionals having in-depth computer knowledge. Users are therefore - encouraged to load and test the software's suitability as regards their - requirements in conditions enabling the security of their systems and/or - data to be ensured and, more generally, to use and operate it in the - same conditions as regards security. - The fact that you are presently reading this means that you have had - knowledge of the CeCILL license and that you accept its terms. + 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 - ParadisEO WebSite : http://paradiseo.gforge.inria.fr - Contact: paradiseo-help@lists.gforge.inria.fr - */ +Contact: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ #ifndef _moTrikiCoolingSchedule_h #define _moTrikiCoolingSchedule_h diff --git a/mo/src/explorer/moMetropolisHastingExplorer.h b/mo/src/explorer/moMetropolisHastingExplorer.h index 3123c4914..ee6169d99 100644 --- a/mo/src/explorer/moMetropolisHastingExplorer.h +++ b/mo/src/explorer/moMetropolisHastingExplorer.h @@ -68,7 +68,17 @@ public: * @param _solNeighborComparator a solution vs neighbor comparator * @param _nbStep maximum number of step to do */ - moMetropolisHastingExplorer(Neighborhood& _neighborhood, moEval& _eval, moNeighborComparator& _neighborComparator, moSolNeighborComparator& _solNeighborComparator, unsigned int _nbStep) : moNeighborhoodExplorer(_neighborhood, _eval), neighborComparator(_neighborComparator), solNeighborComparator(_solNeighborComparator), nbStep(_nbStep) { + moMetropolisHastingExplorer( + Neighborhood& _neighborhood, + moEval& _eval, + moNeighborComparator& _neighborComparator, + moSolNeighborComparator& _solNeighborComparator, + unsigned int _nbStep + ): moNeighborhoodExplorer(_neighborhood, _eval), + neighborComparator(_neighborComparator), + solNeighborComparator(_solNeighborComparator), + nbStep(_nbStep) + { isAccept = false; if (!neighborhood.isRandom()) { std::cout << "moMetropolisHastingExplorer::Warning -> the neighborhood used is not random" << std::endl; @@ -140,7 +150,7 @@ public: virtual bool accept(EOT & _solution) { double alpha=0.0; if (neighborhood.hasNeighbor(_solution)) { - if (solNeighborComparator(_solution, selectedNeighbor)) + if (solNeighborComparator(_solution, selectedNeighbor)) isAccept = true; else { if (_solution.fitness() != 0) { diff --git a/mo/src/sampling/moStdDevEstimator.h b/mo/src/sampling/moStdDevEstimator.h index 338984f3b..d39b88f9d 100644 --- a/mo/src/sampling/moStdDevEstimator.h +++ b/mo/src/sampling/moStdDevEstimator.h @@ -1,3 +1,28 @@ +/* + +(c) Thales group, 2010 + + 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; + version 2 of the License. + + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + #ifndef __moStdDevEstimator_h__ #define __moStdDevEstimator_h__ diff --git a/mo/test/t-moFitnessVarianceStat.cpp b/mo/test/t-moFitnessVarianceStat.cpp index 8f343a6ac..a1510a831 100644 --- a/mo/test/t-moFitnessVarianceStat.cpp +++ b/mo/test/t-moFitnessVarianceStat.cpp @@ -1,3 +1,28 @@ +/* + +(c) Thales group, 2010 + + 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; + version 2 of the License. + + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + //----------------------------------------------------------------------------- // t-moFitnessNeighborStat.cpp //----------------------------------------------------------------------------- diff --git a/mo/test/t-moStdDevEstimator.cpp b/mo/test/t-moStdDevEstimator.cpp index a3a6d4e77..6473ffceb 100644 --- a/mo/test/t-moStdDevEstimator.cpp +++ b/mo/test/t-moStdDevEstimator.cpp @@ -1,3 +1,28 @@ +/* + +(c) Thales group, 2010 + + 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; + version 2 of the License. + + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + //----------------------------------------------------------------------------- // t-moStdDevEstimator.cpp //----------------------------------------------------------------------------- From 10e024b6a19fff8e4954acfac392c4dc058f64a8 Mon Sep 17 00:00:00 2001 From: LPTK Date: Fri, 14 Jun 2013 17:21:08 +0200 Subject: [PATCH 26/36] removed useless inheritance --- mo/src/neighborhood/moRealNeighbor.h | 25 +++++++++++++++++ mo/src/neighborhood/moRealNeighborhood.h | 35 ++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/mo/src/neighborhood/moRealNeighbor.h b/mo/src/neighborhood/moRealNeighbor.h index 4af8924f2..e4a4a18d6 100644 --- a/mo/src/neighborhood/moRealNeighbor.h +++ b/mo/src/neighborhood/moRealNeighbor.h @@ -1,3 +1,28 @@ +/* + +(c) Thales group, 2010 + + 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; + version 2 of the License. + + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + #ifndef __moRealNeighbor_h__ #define __moRealNeighbor_h__ diff --git a/mo/src/neighborhood/moRealNeighborhood.h b/mo/src/neighborhood/moRealNeighborhood.h index 5efa3ec6f..e839d16fa 100644 --- a/mo/src/neighborhood/moRealNeighborhood.h +++ b/mo/src/neighborhood/moRealNeighborhood.h @@ -1,3 +1,27 @@ +/* + +(c) Thales group, 2010 + + 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; + version 2 of the License. + + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ #ifndef __moRealNeighborhood_h__ #define __moRealNeighborhood_h__ @@ -7,7 +31,7 @@ #include "neighborhood/moRealNeighbor.h" template -class moRealNeighborhood : public moRndNeighborhood< Neighbor >, public eoFunctorBase +class moRealNeighborhood : public moRndNeighborhood< Neighbor > { public: typedef typename Distrib::EOType EOT; @@ -19,7 +43,14 @@ protected: public: - moRealNeighborhood( Distrib & distrib, edoSampler & sampler, edoBounder & bounder ) : _distrib(distrib), _sampler(sampler), _bounder(bounder) {} + moRealNeighborhood( + Distrib& distrib, + edoSampler& sampler, + edoBounder& bounder + ): _distrib(distrib), + _sampler(sampler), + _bounder(bounder) + { } /** * It alway remains at least a solution in an infinite neighborhood From 270add8a97a155a98c2c5299946e5a455b766db8 Mon Sep 17 00:00:00 2001 From: LPTK Date: Mon, 17 Jun 2013 14:28:50 +0200 Subject: [PATCH 27/36] triki tests --- .../coolingSchedule/moTrikiCoolingSchedule.h | 2 +- mo/src/sampling/moStdDevEstimator.h | 2 +- mo/test/t-moTrikiReal.cpp | 188 ++++++++++++++++++ 3 files changed, 190 insertions(+), 2 deletions(-) diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index d98baa74c..e120f9440 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -32,7 +32,7 @@ Lionel Parreaux #include #include #include -#include +#include /* diff --git a/mo/src/sampling/moStdDevEstimator.h b/mo/src/sampling/moStdDevEstimator.h index d39b88f9d..63afb6d24 100644 --- a/mo/src/sampling/moStdDevEstimator.h +++ b/mo/src/sampling/moStdDevEstimator.h @@ -49,7 +49,7 @@ public: */ moStdDevEstimator ( moContinuator& continuator, - moNeighborhood < Neighbor > & neighborhood, + moNeighborhood& neighborhood, eoEvalFunc& fullEval, /* The following should be read: diff --git a/mo/test/t-moTrikiReal.cpp b/mo/test/t-moTrikiReal.cpp index e69de29bb..9b8038ba7 100644 --- a/mo/test/t-moTrikiReal.cpp +++ b/mo/test/t-moTrikiReal.cpp @@ -0,0 +1,188 @@ +/* + +(c) Thales group, 2010 + + 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; + version 2 of the License. + + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + +#include +#include +#include +#include + +//#include + +#include + +#include "neighborhood/moRealNeighbor.h" +#include "neighborhood/moRealNeighborhood.h" + +#include "sampling/moStdDevEstimator.h" + +#include "coolingSchedule/moTrikiCoolingSchedule.h" + + +//#include "moRealInitTemperature.h" + +//#include "do/make_real_init_temperature.h" + +typedef eoReal< eoMinimizingFitness > EOT; +typedef moRealNeighbor< EOT > Neighbor; + +double objective_function(const EOT & sol) +{ + double sum = 0; + + for ( size_t i = 0; i < sol.size(); ++i ) + { + sum += sol[i] * sol[i]; + } + + return sum; +} + +int main( int ac, char** av ) +{ + eoParser parser( ac, av ); + + eoState state; + + eoEvalFuncPtr< EOT, double > eval( objective_function ); + moFullEvalByCopy< Neighbor > fullEval( eval ); + + //moNeighborhood< Neighbor >* neighborhood; + + int dimSize = 20; + + //moRealInitTemperature< EOT >& real_init = do_make_real_init_temperature( parser, state, eval ); + //moInitTemperature< EOT, Neighbor >& real_init = do_make_real_init_temperature( parser, state, eval, neval ); + //moInitTemperature< EOT, Neighbor >& real_init = do_make_init_temperature( parser, state, eval, neval, neighborhood ); + //moStdDevEstimator< EOT, Neighbor >& real_init = do_make_init_temperature( parser, state, eval, neval, neighborhood, dimSize ); + + + //------------------------------------------------------- + // Parameters + //------------------------------------------------------- + + std::string section( "Temperature initialization paramaters" ); + + unsigned int dimension_size = parser.getORcreateParam( (unsigned int)dimSize, "dimension-size", "Dimension size", 'd', section ).value(); + double jump_bound = parser.getORcreateParam( (double)1, "jump-bound", "Bound of jump", '\0', section ).value(); + unsigned int maxiter = parser.getORcreateParam( (unsigned int)10, "max-iter", "Maximum number of iterations", '\0', section ).value(); + //unsigned int ratio = parser.getORcreateParam( (unsigned int)1, "ratio", "Bounder ratio", '\0', section ).value(); // not used + + //------------------------------------------------------- + + + //------------------------------------------------------- + // Instanciate needed classes + //------------------------------------------------------- + + edoUniform< EOT > distrib( EOT(dimension_size, -1 * jump_bound), EOT(dimension_size, 1 * jump_bound) ); + + edoBounder< EOT > bounder_search( EOT(dimension_size, -10), EOT(dimension_size, 10) ); + + edoSamplerUniform< EOT > sampler( bounder_search ); + + //moRealNeighborhood< edoUniform< EOT >, Neighbor >* neighborhood = new moRealNeighborhood< edoUniform< EOT >, Neighbor >( *distrib, *sampler, *bounder_search ); + moRealNeighborhood< edoUniform< EOT >, Neighbor > neighborhood( distrib, sampler, bounder_search ); + //state.storeFunctor(neighborhood);state.storeFunctor(neighborhood); + + //moStdDevEstimator< EOT, Neighbor >* init = new moStdDevEstimator< EOT, Neighbor >( *neighborhood, fullEval, eval, maxiter ); + //moStdDevEstimator< EOT, Neighbor > init( maxiter, neighborhood, fullEval, eval ); + moStdDevEstimator< EOT, Neighbor > init( maxiter, neighborhood, fullEval ); + + //------------------------------------------------------- + + + + + //------------------------------------------------------- + // Help + Verbose routines + //------------------------------------------------------- + + if (parser.userNeedsHelp()) + { + parser.printHelp(std::cout); + exit(1); + } + + make_help(parser); + + //------------------------------------------------------- + + + //EOT solution(2, 5); + EOT solution(dimSize, 5); + /* + real_init( solution ); + + std::cout << "do_make_real_init_temperature( parser, eval ): " + << real_init.value() + << std::endl; + */ + + std::cout << "do_make_real_init_temperature( parser, eval ): " + << init( solution ) + << std::endl; + + + + + moTrueContinuator continuator; + moCheckpoint checkpoint(continuator); + moFitnessStat fitStat; + checkpoint.add(fitStat); + eoFileMonitor monitor("triki.out", ""); + //eoGnuplot1DMonitor monitor2("trikignu.out", true); + moCounterMonitorSaver countMon(1, monitor); + checkpoint.add(countMon); + //moCounterMonitorSaver gnuMon (1, monitor2); + //checkpoint.add(gnuMon); + monitor.add(fitStat); + //monitor2.add(fitStat); + + + + moTrikiCoolingSchedule coolingSchedule ( + neighborhood, neval, init( solution ), + //50, + 200, //150, + //100 + 350 // 250 + ); + moSA localSearch(*neighborhood, eval, fullEval, coolingSchedule, checkpoint); + + std::cout << "#########################################" << std::endl; + std::cout << "initial solution1: " << solution << std::endl ; + + localSearch(solution); + + std::cout << "final solution1: " << solution << std::endl ; + std::cout << "#########################################" << std::endl; + + //delete neighborhood; + + return 0; +} + + + From 7f9b8fe6070d864ac29ab7ff05cae05b82b0aa58 Mon Sep 17 00:00:00 2001 From: LPTK Date: Mon, 17 Jun 2013 17:15:07 +0200 Subject: [PATCH 28/36] changelog update --- eo/NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/eo/NEWS b/eo/NEWS index 6657f36b8..4161ff613 100644 --- a/eo/NEWS +++ b/eo/NEWS @@ -1,4 +1,5 @@ * current release: + - added an EOT& parameter to the moCoolingSchedule::update interface because some cooling schedules need it * release 1.3.1 (2012-07-27) - the eo::mpi modules is no longer dependent from boost::mpi From 1e6240f9bf33a08de19eb96f71aa2898d9dc1ab9 Mon Sep 17 00:00:00 2001 From: LPTK Date: Fri, 21 Jun 2013 18:03:57 +0200 Subject: [PATCH 29/36] intermediate commit --- eo/test/t-eoOptional.cpp | 1 + mo/src/algo/moSA.h | 164 +++++++++++++----- mo/src/continuator/moFitnessMomentsStat.h | 43 ++--- .../coolingSchedule/moTrikiCoolingSchedule.h | 19 +- mo/src/explorer/moSAexplorer.h | 11 +- mo/src/mo.h | 3 + mo/src/trikisa | 22 --- mo/test/t-moTriki.cpp | 155 +++++++++++++++++ mo/test/t-moTrikiReal.cpp | 116 +++++-------- 9 files changed, 363 insertions(+), 171 deletions(-) delete mode 100644 mo/src/trikisa diff --git a/eo/test/t-eoOptional.cpp b/eo/test/t-eoOptional.cpp index bee58957c..5b4a9fbc2 100644 --- a/eo/test/t-eoOptional.cpp +++ b/eo/test/t-eoOptional.cpp @@ -27,3 +27,4 @@ int main(int ac, char** av) T t(666); MyClass mc3(t); } + diff --git a/mo/src/algo/moSA.h b/mo/src/algo/moSA.h index c126f31fc..31c98cc7d 100644 --- a/mo/src/algo/moSA.h +++ b/mo/src/algo/moSA.h @@ -37,6 +37,8 @@ Contact: paradiseo-help@lists.gforge.inria.fr #include #include #include +#include +#include /** * Simulated Annealing @@ -60,38 +62,60 @@ public: * @param _span number of iteration with equal temperature for cooling schedule (default = 100) * @param _finalT final temperature, threshold of the stopping criteria for cooling schedule (default = 0.01) */ - moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01): - moLocalSearch(explorer, trueCont, _fullEval), - defaultCool(_initT, _alpha, _span, _finalT), - explorer(_neighborhood, _eval, defaultSolNeighborComp, defaultCool) - {} + /*moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01) + : defaultCool(_initT, _alpha, _span, _finalT), + explorer(_neighborhood, _eval, defaultSolNeighborComp, defaultCool), + moLocalSearch(explorer, trueCont, _fullEval) + { }*/ + moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01) + : moLocalSearch(*(explorer = new moSAexplorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool)), *(trueCont = new moTrueContinuator()), _fullEval), + defaultCool(new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)), + default_eval(NULL), // removed in C++11 with unique_ptr + defaultSolNeighborComp(new moSolNeighborComparator()) + //explorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool) + { } - /** - * Simple constructor for a simulated annealing - * @param _neighborhood the neighborhood - * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function - * @param _cool a cooling schedule - */ - moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool): - moLocalSearch(explorer, trueCont, _fullEval), - defaultCool(0, 0, 0, 0), - explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool) - {} - - /** - * General constructor for a simulated annealing - * @param _neighborhood the neighborhood - * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function - * @param _cool a cooling schedule - * @param _cont an external continuator - */ - moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool, moContinuator& _cont): - moLocalSearch(explorer, _cont, _fullEval), - defaultCool(0, 0, 0, 0), - explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool) - {} +// /** +// * Simple constructor for a simulated annealing +// * @param _neighborhood the neighborhood +// * @param _fullEval the full evaluation function +// * @param _eval neighbor's evaluation function +// * @param _cool a cooling schedule +// */ +// moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool): +// moLocalSearch(explorer, trueCont, _fullEval), +// defaultCool(0, 0, 0, 0), +// explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool) +// {} +// +// /** +// * General constructor for a simulated annealing +// * @param _neighborhood the neighborhood +// * @param _fullEval the full evaluation function +// * @param _eval neighbor's evaluation function +// * @param _cool a cooling schedule +// * @param _cont an external continuator +// */ +// moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool, moContinuator& _cont): +// moLocalSearch(explorer, _cont, _fullEval), +// defaultCool(0, 0, 0, 0), +// explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool) +// {} +// +// /** +// * General constructor for a simulated annealing +// * @param _neighborhood the neighborhood +// * @param _fullEval the full evaluation function +// * @param _eval neighbor's evaluation function +// * @param _cool a cooling schedule +// * @param _comp a solution vs neighbor comparator +// * @param _cont an external continuator +// */ +// moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool, moSolNeighborComparator& _comp, moContinuator& _cont): +// moLocalSearch(explorer, _cont, _fullEval), +// defaultCool(0, 0, 0, 0), +// explorer(_neighborhood, _eval, _comp, _cool) +// {} /** * General constructor for a simulated annealing @@ -102,19 +126,75 @@ public: * @param _comp a solution vs neighbor comparator * @param _cont an external continuator */ - moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool, moSolNeighborComparator& _comp, moContinuator& _cont): - moLocalSearch(explorer, _cont, _fullEval), - defaultCool(0, 0, 0, 0), - explorer(_neighborhood, _eval, _comp, _cool) - {} - - + moSA ( + Neighborhood& _neighborhood, + eoEvalFunc& _fullEval, + moCoolingSchedule& _cool, + eoOptional< moEval > _eval = NULL, + eoOptional< moContinuator > _cont = NULL, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + /*: moLocalSearch(explorer, _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval), + defaultCool(NULL), // removed in C++11 with unique_ptr + default_eval(NULL), // removed in C++11 with unique_ptr + defaultSolNeighborComp(NULL), // removed in C++11 with unique_ptr + trueCont(NULL), // removed in C++11 with unique_ptr + explorer ( + _neighborhood, + _eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), + // C++11: _eval.hasValue()? _eval.get(): default_eval = new moFullEvalByCopy(), + _comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), + _cool ) + { }*/ + : moLocalSearch ( + *(explorer = new moSAexplorer ( + _neighborhood, + _eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), + // C++11: _eval.hasValue()? _eval.get(): default_eval = new moFullEvalByCopy(), + _comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), + _cool )), + _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval ), + defaultCool(NULL), // removed in C++11 with unique_ptr + default_eval(NULL), // removed in C++11 with unique_ptr + trueCont(NULL), // removed in C++11 with unique_ptr + defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr + { } + + moSA ( + moSAexplorer& _explorer, + eoOptional< moContinuator > _cont = NULL, + ) + : moLocalSearch ( + *(explorer = &_explorer), + _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval ), + defaultCool(NULL), // removed in C++11 with unique_ptr + default_eval(NULL), // removed in C++11 with unique_ptr + trueCont(NULL), // removed in C++11 with unique_ptr + defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr + { } + + virtual ~moSA () + { + // Note: using unique_ptr would allow us to remove this explicit destructor, but they were only introduced in C++11 + if (trueCont != NULL) + delete trueCont; + if (defaultSolNeighborComp != NULL) + delete defaultSolNeighborComp; + if (default_eval != NULL) + delete default_eval; + if (defaultCool != NULL) + delete defaultCool; + delete explorer; + } private: - moTrueContinuator trueCont; - moSimpleCoolingSchedule defaultCool; - moSolNeighborComparator defaultSolNeighborComp; - moSAexplorer explorer; + moSAexplorer* explorer; // Not NULL + moSimpleCoolingSchedule* defaultCool; // C++11: const std::unique_ptr> + moFullEvalByCopy* default_eval; + moTrueContinuator* trueCont; + moSolNeighborComparator* defaultSolNeighborComp; }; #endif + + diff --git a/mo/src/continuator/moFitnessMomentsStat.h b/mo/src/continuator/moFitnessMomentsStat.h index 794d113b8..0aaa5ad48 100644 --- a/mo/src/continuator/moFitnessMomentsStat.h +++ b/mo/src/continuator/moFitnessMomentsStat.h @@ -1,35 +1,26 @@ /* - - Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau +(c) Thales group, 2010 - This software is governed by the CeCILL license under French law and - abiding by the rules of distribution of free software. You can use, - modify and/ or redistribute the software under the terms of the CeCILL - license as circulated by CEA, CNRS and INRIA at the following URL - "http://www.cecill.info". + 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; + version 2 of the License. - As a counterpart to the access to the source code and rights to copy, - modify and redistribute granted by the license, users are provided only - with a limited warranty and the software's author, the holder of the - economic rights, and the successive licensors have only limited liability. + 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. - In this respect, the user's attention is drawn to the risks associated - with loading, using, modifying and/or developing or reproducing the - software by the user in light of its specific status of free software, - that may mean that it is complicated to manipulate, and that also - therefore means that it is reserved for developers and experienced - professionals having in-depth computer knowledge. Users are therefore - encouraged to load and test the software's suitability as regards their - requirements in conditions enabling the security of their systems and/or - data to be ensured and, more generally, to use and operate it in the - same conditions as regards security. - The fact that you are presently reading this means that you have had - knowledge of the CeCILL license and that you accept its terms. + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux - ParadisEO WebSite : http://paradiseo.gforge.inria.fr - Contact: paradiseo-help@lists.gforge.inria.fr */ #ifndef moFitnessMomentsStat_h diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index e120f9440..202b2d2f2 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -141,10 +141,10 @@ public: //costs_sum += _solution.fitness(); //varStat(_solution); if (statIsInitialized) - momStat(_solution); - else momStat.init(_solution), statIsInitialized = true; + momentStat(_solution); + else momentStat.init(_solution), statIsInitialized = true; - //cout << _solution.fitness() << " avgCost=" << momStat.value().first << endl; + //cout << _solution.fitness() << " avgCost=" << momentStat.value().first << endl; } @@ -167,21 +167,20 @@ public: //double avgCost = costs_sum/(double)accepted; //double stdDev = sqrt(varStat.value()); // WARNING: IT'S NO MORE THE AVG COST, NOW IT'S THE STD DEV! //double variance = varStat.value(); - double avgCost = momStat.value().first; - double variance = momStat.value().second; + double avgCost = momentStat.value().first; + double variance = momentStat.value().second; double stdDev = sqrt(variance); double sigma = stdDev; double delta = sigma/mu2; //outf << avgCost << endl; - //outf << _temp << endl; - outf << prevAvgCost-delta << endl; + outf << _temp << endl; + //outf << prevAvgCost-delta << endl; accepted = generated = costs_sum = 0; - //varStat.init(_solution);//TODON use next chain's first sol - //momStat.init(_solution);//TODONE use next chain's first sol + //momentStat.init(_solution);//TODONE use next chain's first sol statIsInitialized = false; /// @@ -342,7 +341,7 @@ private: bool reinitializing, terminated; //moFitnessVarianceStat varStat; - moFitnessMomentsStat momStat; + moFitnessMomentsStat momentStat; bool statIsInitialized; ofstream outf; diff --git a/mo/src/explorer/moSAexplorer.h b/mo/src/explorer/moSAexplorer.h index 37fa57955..a3e729330 100644 --- a/mo/src/explorer/moSAexplorer.h +++ b/mo/src/explorer/moSAexplorer.h @@ -68,7 +68,16 @@ public: * @param _solNeighborComparator a solution vs neighbor comparator * @param _coolingSchedule the cooling schedule */ - moSAexplorer(Neighborhood& _neighborhood, moEval& _eval, moSolNeighborComparator& _solNeighborComparator, moCoolingSchedule& _coolingSchedule) : moNeighborhoodExplorer(_neighborhood, _eval), solNeighborComparator(_solNeighborComparator), coolingSchedule(_coolingSchedule) { + moSAexplorer ( + Neighborhood& _neighborhood, + moEval& _eval, + moSolNeighborComparator& _solNeighborComparator, + moCoolingSchedule& _coolingSchedule + ) + : moNeighborhoodExplorer(_neighborhood, _eval), + solNeighborComparator(_solNeighborComparator), + coolingSchedule(_coolingSchedule) + { isAccept = false; if (!neighborhood.isRandom()) { diff --git a/mo/src/mo.h b/mo/src/mo.h index 2a56a86a5..3e65f8081 100755 --- a/mo/src/mo.h +++ b/mo/src/mo.h @@ -105,6 +105,7 @@ #include #include #include +#include #include #include @@ -112,6 +113,7 @@ #include #include #include +#include #include #include @@ -207,5 +209,6 @@ #include #include #include +#include #endif diff --git a/mo/src/trikisa b/mo/src/trikisa deleted file mode 100644 index 63f59ff9a..000000000 --- a/mo/src/trikisa +++ /dev/null @@ -1,22 +0,0 @@ -// (c) Thales group, 2010 -/* - Authors: - Johann Dreo - Caner Candan -*/ - -#ifndef _trikisa_ -#define _trikisa_ - -#include "moRealNeighbor.h" -#include "moRealNeighborhood.h" -#include "moStdDevEstimator.h" -#include "moTrikiCoolingSchedule.h" -#include "moFitnessVarianceStat.h" // TODO rm -#include "moFitnessMomentsStat.h" - -#endif // !_trikisa_ - -// Local Variables: -// mode: C++ -// End: diff --git a/mo/test/t-moTriki.cpp b/mo/test/t-moTriki.cpp index e69de29bb..d8f5e7119 100644 --- a/mo/test/t-moTriki.cpp +++ b/mo/test/t-moTriki.cpp @@ -0,0 +1,155 @@ +//============================================================================ +// Name : Trikitest.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +//#define HAVE_GNUPLOT + +#include +using namespace std; + +#include +#include + +//Representation and initializer +#include +#include +#include + +/* +// fitness function +#include +#include +#include + +//Neighbors and Neighborhoods +#include +#include + +//Algorithm and its components +#include +#include + +//comparator +#include + +//continuators +#include +#include +#include +#include +#include +*/ + +//----------------------------------------------------------------------------- +// Define types of the representation solution, different neighbors and neighborhoods +//----------------------------------------------------------------------------- +typedef eoInt Queen; //Permutation (Queen's problem representation) + +typedef moShiftNeighbor shiftNeighbor; //shift Neighbor +typedef moRndWithReplNeighborhood rndShiftNeighborhood; //rnd shift Neighborhood (Indexed) + + + + +int main() { + //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! + //return 0; + + unsigned vecSize = 8; + + + queenEval fullEval; + + + /* ========================================================= + * + * Initilization of the solution + * + * ========================================================= */ + + eoInitPermutation init(vecSize); + + /* ========================================================= + * + * evaluation of a neighbor solution + * + * ========================================================= */ + + moFullEvalByCopy shiftEval(fullEval); /// by default + + /* ========================================================= + * + * the neighborhood of a solution + * + * ========================================================= */ + + rndShiftNeighborhood rndShiftNH((vecSize-1) * (vecSize-1)); + + /* ========================================================= + * + * the cooling schedule of the process + * + * ========================================================= */ + + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + + /* ========================================================= + * + * execute the local search from random solution + * + * ========================================================= */ + + Queen solution1; + + init(solution1); + + fullEval(solution1); + + moStdDevEstimator initTemp (500, rndShiftNH, fullEval); + + moTrueContinuator continuator; + moCheckpoint checkpoint(continuator); + moFitnessStat fitStat; + checkpoint.add(fitStat); + eoFileMonitor monitor("triki.out", ""); + eoGnuplot1DMonitor monitor2("trikignu.out", true); + moCounterMonitorSaver countMon(100, monitor); + checkpoint.add(countMon); + moCounterMonitorSaver gnuMon (1, monitor2); + checkpoint.add(gnuMon); + monitor.add(fitStat); + monitor2.add(fitStat); + //#ifdef HAVE_GNUPLOT + + + moTrikiCoolingSchedule coolingSchedule(rndShiftNH, shiftEval, initTemp(solution1)); + moSA localSearch1(rndShiftNH, fullEval, coolingSchedule, shiftEval, checkpoint); + + + std::cout << "#########################################" << std::endl; + std::cout << "initial solution1: " << solution1 << std::endl ; + + localSearch1(solution1); + + std::cout << "final solution1: " << solution1 << std::endl ; + std::cout << "#########################################" << std::endl; + + + +} + + + + + + + + diff --git a/mo/test/t-moTrikiReal.cpp b/mo/test/t-moTrikiReal.cpp index 9b8038ba7..0ee694652 100644 --- a/mo/test/t-moTrikiReal.cpp +++ b/mo/test/t-moTrikiReal.cpp @@ -28,8 +28,6 @@ Lionel Parreaux #include #include -//#include - #include #include "neighborhood/moRealNeighbor.h" @@ -40,10 +38,6 @@ Lionel Parreaux #include "coolingSchedule/moTrikiCoolingSchedule.h" -//#include "moRealInitTemperature.h" - -//#include "do/make_real_init_temperature.h" - typedef eoReal< eoMinimizingFitness > EOT; typedef moRealNeighbor< EOT > Neighbor; @@ -52,9 +46,9 @@ double objective_function(const EOT & sol) double sum = 0; for ( size_t i = 0; i < sol.size(); ++i ) - { - sum += sol[i] * sol[i]; - } + { + sum += sol[i] * sol[i]; + } return sum; } @@ -66,18 +60,10 @@ int main( int ac, char** av ) eoState state; eoEvalFuncPtr< EOT, double > eval( objective_function ); - moFullEvalByCopy< Neighbor > fullEval( eval ); - - //moNeighborhood< Neighbor >* neighborhood; + moFullEvalByCopy< Neighbor > neval( eval ); int dimSize = 20; - //moRealInitTemperature< EOT >& real_init = do_make_real_init_temperature( parser, state, eval ); - //moInitTemperature< EOT, Neighbor >& real_init = do_make_real_init_temperature( parser, state, eval, neval ); - //moInitTemperature< EOT, Neighbor >& real_init = do_make_init_temperature( parser, state, eval, neval, neighborhood ); - //moStdDevEstimator< EOT, Neighbor >& real_init = do_make_init_temperature( parser, state, eval, neval, neighborhood, dimSize ); - - //------------------------------------------------------- // Parameters //------------------------------------------------------- @@ -87,8 +73,7 @@ int main( int ac, char** av ) unsigned int dimension_size = parser.getORcreateParam( (unsigned int)dimSize, "dimension-size", "Dimension size", 'd', section ).value(); double jump_bound = parser.getORcreateParam( (double)1, "jump-bound", "Bound of jump", '\0', section ).value(); unsigned int maxiter = parser.getORcreateParam( (unsigned int)10, "max-iter", "Maximum number of iterations", '\0', section ).value(); - //unsigned int ratio = parser.getORcreateParam( (unsigned int)1, "ratio", "Bounder ratio", '\0', section ).value(); // not used - + //------------------------------------------------------- @@ -102,13 +87,9 @@ int main( int ac, char** av ) edoSamplerUniform< EOT > sampler( bounder_search ); - //moRealNeighborhood< edoUniform< EOT >, Neighbor >* neighborhood = new moRealNeighborhood< edoUniform< EOT >, Neighbor >( *distrib, *sampler, *bounder_search ); moRealNeighborhood< edoUniform< EOT >, Neighbor > neighborhood( distrib, sampler, bounder_search ); - //state.storeFunctor(neighborhood);state.storeFunctor(neighborhood); - - //moStdDevEstimator< EOT, Neighbor >* init = new moStdDevEstimator< EOT, Neighbor >( *neighborhood, fullEval, eval, maxiter ); - //moStdDevEstimator< EOT, Neighbor > init( maxiter, neighborhood, fullEval, eval ); - moStdDevEstimator< EOT, Neighbor > init( maxiter, neighborhood, fullEval ); + + moStdDevEstimator< EOT, Neighbor > init( maxiter, neighborhood, eval ); //------------------------------------------------------- @@ -129,57 +110,52 @@ int main( int ac, char** av ) //------------------------------------------------------- - - //EOT solution(2, 5); + EOT solution(dimSize, 5); - /* - real_init( solution ); - - std::cout << "do_make_real_init_temperature( parser, eval ): " - << real_init.value() - << std::endl; - */ - - std::cout << "do_make_real_init_temperature( parser, eval ): " - << init( solution ) - << std::endl; + + std::cout << "init temp: " + << init( solution ) + << std::endl; - moTrueContinuator continuator; - moCheckpoint checkpoint(continuator); - moFitnessStat fitStat; - checkpoint.add(fitStat); - eoFileMonitor monitor("triki.out", ""); - //eoGnuplot1DMonitor monitor2("trikignu.out", true); - moCounterMonitorSaver countMon(1, monitor); - checkpoint.add(countMon); - //moCounterMonitorSaver gnuMon (1, monitor2); - //checkpoint.add(gnuMon); - monitor.add(fitStat); - //monitor2.add(fitStat); - + moTrueContinuator continuator; + moCheckpoint checkpoint(continuator); + moFitnessStat fitStat; + checkpoint.add(fitStat); + eoFileMonitor monitor("triki.out", ""); + //eoGnuplot1DMonitor monitor2("trikignu.out", true); + moCounterMonitorSaver countMon(1, monitor); + checkpoint.add(countMon); + //moCounterMonitorSaver gnuMon (1, monitor2); + //checkpoint.add(gnuMon); + monitor.add(fitStat); + //monitor2.add(fitStat); - moTrikiCoolingSchedule coolingSchedule ( - neighborhood, neval, init( solution ), - //50, - 200, //150, - //100 - 350 // 250 - ); - moSA localSearch(*neighborhood, eval, fullEval, coolingSchedule, checkpoint); - - std::cout << "#########################################" << std::endl; - std::cout << "initial solution1: " << solution << std::endl ; - - localSearch(solution); - - std::cout << "final solution1: " << solution << std::endl ; - std::cout << "#########################################" << std::endl; - - //delete neighborhood; + + moTrikiCoolingSchedule coolingSchedule ( + neighborhood, neval, init( solution ), + //50, + 200, //150, + //100 + 350 // 250 + ); + //moSA localSearch(neighborhood, eval, neval, coolingSchedule, checkpoint); + //moSA localSearch(neighborhood, eval, neval); + //moSA localSearch(neighborhood, eval, coolingSchedule, neval, checkpoint); + //moSA localSearch(neighborhood, eval, coolingSchedule); + moSA localSearch(neighborhood, eval, coolingSchedule, neval, checkpoint); + + std::cout << "#########################################" << std::endl; + std::cout << "initial solution1: " << solution << std::endl ; + + localSearch(solution); + + std::cout << "final solution1: " << solution << std::endl ; + std::cout << "#########################################" << std::endl; + return 0; } From bb27a3525a9427e54f816aad501068c3bcd5b3c2 Mon Sep 17 00:00:00 2001 From: LPTK Date: Mon, 24 Jun 2013 11:36:07 +0200 Subject: [PATCH 30/36] intermediate commit 2 --- mo/src/algo/moSA.h | 49 ++++++++++++++++------------------ mo/src/explorer/moSAexplorer.h | 29 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/mo/src/algo/moSA.h b/mo/src/algo/moSA.h index 31c98cc7d..49d8e4c0d 100644 --- a/mo/src/algo/moSA.h +++ b/mo/src/algo/moSA.h @@ -38,7 +38,6 @@ Contact: paradiseo-help@lists.gforge.inria.fr #include #include #include -#include /** * Simulated Annealing @@ -68,10 +67,10 @@ public: moLocalSearch(explorer, trueCont, _fullEval) { }*/ moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01) - : moLocalSearch(*(explorer = new moSAexplorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool)), *(trueCont = new moTrueContinuator()), _fullEval), - defaultCool(new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)), - default_eval(NULL), // removed in C++11 with unique_ptr - defaultSolNeighborComp(new moSolNeighborComparator()) + : moLocalSearch(explorer = *(defaultExplorer = new moSAexplorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool)), *(trueCont = new moTrueContinuator()), _fullEval), + defaultCool(new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)) + //default_eval(NULL), // removed in C++11 with unique_ptr + //defaultSolNeighborComp(new moSolNeighborComparator()) //explorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool) { } @@ -147,30 +146,32 @@ public: _cool ) { }*/ : moLocalSearch ( - *(explorer = new moSAexplorer ( + explorer = *(defaultExplorer = new moSAexplorer ( _neighborhood, - _eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), + _eval, //_eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), // C++11: _eval.hasValue()? _eval.get(): default_eval = new moFullEvalByCopy(), - _comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), + _comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), _cool )), - _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval ), + _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), + _fullEval ), defaultCool(NULL), // removed in C++11 with unique_ptr - default_eval(NULL), // removed in C++11 with unique_ptr - trueCont(NULL), // removed in C++11 with unique_ptr - defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr + //default_eval(NULL), // removed in C++11 with unique_ptr + trueCont(NULL) // removed in C++11 with unique_ptr + //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr { } moSA ( moSAexplorer& _explorer, - eoOptional< moContinuator > _cont = NULL, + eoOptional< moContinuator > _cont = NULL ) : moLocalSearch ( *(explorer = &_explorer), _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval ), + defaultExplorer(NULL), // removed in C++11 with unique_ptr defaultCool(NULL), // removed in C++11 with unique_ptr - default_eval(NULL), // removed in C++11 with unique_ptr - trueCont(NULL), // removed in C++11 with unique_ptr - defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr + //default_eval(NULL), // removed in C++11 with unique_ptr + trueCont(NULL) // removed in C++11 with unique_ptr + //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr { } virtual ~moSA () @@ -178,21 +179,17 @@ public: // Note: using unique_ptr would allow us to remove this explicit destructor, but they were only introduced in C++11 if (trueCont != NULL) delete trueCont; - if (defaultSolNeighborComp != NULL) - delete defaultSolNeighborComp; - if (default_eval != NULL) - delete default_eval; - if (defaultCool != NULL) - delete defaultCool; - delete explorer; + if (explorer != NULL) + delete explorer; } private: - moSAexplorer* explorer; // Not NULL + moSAexplorer* defaultExplorer; + moSAexplorer& explorer; // Not NULL (ref) moSimpleCoolingSchedule* defaultCool; // C++11: const std::unique_ptr> - moFullEvalByCopy* default_eval; + //moFullEvalByCopy* default_eval; moTrueContinuator* trueCont; - moSolNeighborComparator* defaultSolNeighborComp; + //moSolNeighborComparator* defaultSolNeighborComp; }; #endif diff --git a/mo/src/explorer/moSAexplorer.h b/mo/src/explorer/moSAexplorer.h index a3e729330..09eda6dab 100644 --- a/mo/src/explorer/moSAexplorer.h +++ b/mo/src/explorer/moSAexplorer.h @@ -41,6 +41,8 @@ #include #include #include +#include +#include #include @@ -68,6 +70,7 @@ public: * @param _solNeighborComparator a solution vs neighbor comparator * @param _coolingSchedule the cooling schedule */ + /* moSAexplorer ( Neighborhood& _neighborhood, moEval& _eval, @@ -80,6 +83,24 @@ public: { isAccept = false; + if (!neighborhood.isRandom()) { + std::cout << "moSAexplorer::Warning -> the neighborhood used is not random" << std::endl; + } + }*/ + moSAexplorer ( + Neighborhood& _neighborhood, + moCoolingSchedule& _cool, + eoOptional< moEval > _eval = NULL, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moNeighborhoodExplorer(_neighborhood, _eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval))), + default_eval(NULL), // removed in C++11 with unique_ptr + defaultSolNeighborComp(NULL), // removed in C++11 with unique_ptr + solNeighborComparator(_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator())), + coolingSchedule(_coolingSchedule) + { + isAccept = false; + if (!neighborhood.isRandom()) { std::cout << "moSAexplorer::Warning -> the neighborhood used is not random" << std::endl; } @@ -89,6 +110,10 @@ public: * Destructor */ ~moSAexplorer() { + if (defaultSolNeighborComp != NULL) + delete defaultSolNeighborComp; + if (default_eval != NULL) + delete default_eval; } /** @@ -175,6 +200,10 @@ public: } private: + + moFullEvalByCopy* default_eval; + moSolNeighborComparator* defaultSolNeighborComp; + // comparator betwenn solution and neighbor moSolNeighborComparator& solNeighborComparator; From eeb9faec4a240dbe940c5c84cf7d78b377f6f9ae Mon Sep 17 00:00:00 2001 From: LPTK Date: Tue, 25 Jun 2013 14:14:37 +0200 Subject: [PATCH 31/36] intermediate commit 3 --- mo/src/algo/moLocalSearch.h | 148 +++-- mo/src/algo/moSA.h | 58 +- .../coolingSchedule/moTrikiCoolingSchedule.h | 571 +++++++++--------- mo/src/explorer/moSAexplorer.h | 220 ------- 4 files changed, 405 insertions(+), 592 deletions(-) delete mode 100644 mo/src/explorer/moSAexplorer.h diff --git a/mo/src/algo/moLocalSearch.h b/mo/src/algo/moLocalSearch.h index bf77a8f3f..0c125d62a 100644 --- a/mo/src/algo/moLocalSearch.h +++ b/mo/src/algo/moLocalSearch.h @@ -47,99 +47,97 @@ template class moLocalSearch: public eoMonOp { public: - typedef moNeighborhood Neighborhood; - typedef moNeighborhoodExplorer NeighborhoodExplorer; - typedef typename Neighbor::EOT EOT; + typedef moNeighborhood Neighborhood; + typedef moNeighborhoodExplorer NeighborhoodExplorer; + typedef typename Neighbor::EOT EOT; - /** - * Constructor of a moLocalSearch - * @param _searchExpl a neighborhood explorer - * @param _cont an external continuator (can be a checkpoint!) - * @param _fullEval a full evaluation function - */ - moLocalSearch(NeighborhoodExplorer& _searchExpl, - moContinuator & _cont, eoEvalFunc& _fullEval) : - searchExplorer(_searchExpl), cont(&_cont), fullEval(_fullEval) { - } - ; + /** + * Constructor of a moLocalSearch + * @param _searchExpl a neighborhood explorer + * @param _cont an external continuator (can be a checkpoint!) + * @param _fullEval a full evaluation function + */ + moLocalSearch(NeighborhoodExplorer& _searchExpl, + moContinuator & _cont, eoEvalFunc& _fullEval) + : searchExplorer(_searchExpl), cont(&_cont), fullEval(_fullEval) + { } - /** - * Run the local search on a solution - * @param _solution the related solution - */ - virtual bool operator()(EOT & _solution) { + /** + * Run the local search on a solution + * @param _solution the related solution + */ + virtual bool operator()(EOT & _solution) { - if (_solution.invalid()) - fullEval(_solution); + if (_solution.invalid()) + fullEval(_solution); - // initialization of the parameter of the search (for example fill empty the tabu list) - searchExplorer.initParam(_solution); + // initialization of the parameter of the search (for example fill empty the tabu list) + searchExplorer.initParam(_solution); - // initialization of the external continuator (for example the time, or the number of generations) - cont->init(_solution); + // initialization of the external continuator (for example the time, or the number of generations) + cont->init(_solution); - bool b; - do { - // explore the neighborhood of the solution - searchExplorer(_solution); - // if a solution in the neighborhood can be accepted - if (searchExplorer.accept(_solution)) { - searchExplorer.move(_solution); - searchExplorer.moveApplied(true); - } else - searchExplorer.moveApplied(false); + bool b; + do { + // explore the neighborhood of the solution + searchExplorer(_solution); + // if a solution in the neighborhood can be accepted + if (searchExplorer.accept(_solution)) { + searchExplorer.move(_solution); + searchExplorer.moveApplied(true); + } else + searchExplorer.moveApplied(false); - // update the parameter of the search (for ex. Temperature of the SA) - searchExplorer.updateParam(_solution); + // update the parameter of the search (for ex. Temperature of the SA) + searchExplorer.updateParam(_solution); - b = (*cont)(_solution); - } while (b && searchExplorer.isContinue(_solution)); + b = (*cont)(_solution); + } while (b && searchExplorer.isContinue(_solution)); - searchExplorer.terminate(_solution); + searchExplorer.terminate(_solution); - cont->lastCall(_solution); + cont->lastCall(_solution); - return true; - } - ; + return true; + } - /** - * Set an external continuator - * @param _cont the external continuator - */ - void setContinuator(moContinuator & _cont) { - cont = &_cont; - } + /** + * Set an external continuator + * @param _cont the external continuator + */ + void setContinuator(moContinuator & _cont) { + cont = &_cont; + } - /** - * external continuator object - * - * @overload - * @return the external continuator - */ - moContinuator* getContinuator() const { - return cont; - } + /** + * external continuator object + * + * @overload + * @return the external continuator + */ + moContinuator* getContinuator() const { + return cont; + } - /** - * to get the neighborhood explorer - * - * @overload - * @return the neighborhood explorer - */ - moNeighborhoodExplorer & getNeighborhoodExplorer() const { - return searchExplorer; - } + /** + * to get the neighborhood explorer + * + * @overload + * @return the neighborhood explorer + */ + moNeighborhoodExplorer & getNeighborhoodExplorer() const { + return searchExplorer; + } protected: - // make the exploration of the neighborhood according to a local search heuristic - moNeighborhoodExplorer& searchExplorer; + // make the exploration of the neighborhood according to a local search heuristic + moNeighborhoodExplorer& searchExplorer; - // external continuator - moContinuator * cont; + // external continuator + moContinuator * cont; - //full evaluation function - eoEvalFunc& fullEval; + //full evaluation function + eoEvalFunc& fullEval; }; #endif diff --git a/mo/src/algo/moSA.h b/mo/src/algo/moSA.h index 49d8e4c0d..89c61ad3a 100644 --- a/mo/src/algo/moSA.h +++ b/mo/src/algo/moSA.h @@ -67,9 +67,13 @@ public: moLocalSearch(explorer, trueCont, _fullEval) { }*/ moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01) - : moLocalSearch(explorer = *(defaultExplorer = new moSAexplorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool)), *(trueCont = new moTrueContinuator()), _fullEval), - defaultCool(new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)) - //default_eval(NULL), // removed in C++11 with unique_ptr + : moLocalSearch ( + explorer_ptr = defaultExplorer = new moSAexplorer(_neighborhood, _eval, NULL, *(defaultCool = new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT))), + *(trueCont = new moTrueContinuator()), + _fullEval ), + explorer(*explorer_ptr), + //defaultCool(), + default_eval(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(new moSolNeighborComparator()) //explorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool) { } @@ -146,46 +150,76 @@ public: _cool ) { }*/ : moLocalSearch ( - explorer = *(defaultExplorer = new moSAexplorer ( + explorer_ptr = defaultExplorer = new moSAexplorer ( _neighborhood, - _eval, //_eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), + //_eval, //_eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), + _eval.hasValue()? default_eval = NULL, _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), // C++11: _eval.hasValue()? _eval.get(): default_eval = new moFullEvalByCopy(), _comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), - _cool )), - _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), + _cool ), + _cont.hasValue()? trueCont = NULL, _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval ), + explorer(*explorer_ptr), + defaultCool(NULL) // removed in C++11 with unique_ptr + //default_eval(NULL), // removed in C++11 with unique_ptr + //trueCont(NULL) // removed in C++11 with unique_ptr + //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr + { } + + + moSA ( + eoEvalFunc& _fullEval, + moSAexplorer& _explorer, + eoOptional< moContinuator > _cont = NULL + ) + : moLocalSearch ( + *(explorer_ptr = &_explorer), + _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval ), + defaultExplorer(NULL), // removed in C++11 with unique_ptr + explorer(*explorer_ptr), defaultCool(NULL), // removed in C++11 with unique_ptr //default_eval(NULL), // removed in C++11 with unique_ptr trueCont(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr { } + /* moSA ( + eoEvalFunc& _fullEval, moSAexplorer& _explorer, - eoOptional< moContinuator > _cont = NULL + moContinuator _cont ) : moLocalSearch ( *(explorer = &_explorer), - _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval ), + _cont, _fullEval ), defaultExplorer(NULL), // removed in C++11 with unique_ptr defaultCool(NULL), // removed in C++11 with unique_ptr //default_eval(NULL), // removed in C++11 with unique_ptr trueCont(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr { } + */ + + + + virtual ~moSA () { // Note: using unique_ptr would allow us to remove this explicit destructor, but they were only introduced in C++11 if (trueCont != NULL) delete trueCont; - if (explorer != NULL) - delete explorer; + if (defaultExplorer != NULL) + delete defaultExplorer; + if (default_eval != NULL) + delete default_eval; } private: + moFullEvalByCopy* default_eval; moSAexplorer* defaultExplorer; - moSAexplorer& explorer; // Not NULL (ref) + moSAexplorer* explorer_ptr; // Not NULL + moSAexplorer& explorer; moSimpleCoolingSchedule* defaultCool; // C++11: const std::unique_ptr> //moFullEvalByCopy* default_eval; moTrueContinuator* trueCont; diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index 202b2d2f2..5c1e7e0c8 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -55,297 +55,298 @@ template< class EOT, class Neighbor > //, class Neighborhood > class moTrikiCoolingSchedule: public moCoolingSchedule< EOT > { public: - //typedef typename Neighbor::EOT EOT ; - typedef moNeighborhood Neighborhood ; + //typedef typename Neighbor::EOT EOT ; + typedef moNeighborhood Neighborhood ; - //! Constructor - /*! - */ + //! Constructor + /*! + */ - moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval& _eval, double _initTemp) - : initTemp(_initTemp), - mu2(10), // mu2 typically belongs to [1; 20] - K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature - K2(5), // ??? - lambda1(2), // the increase in temperature, typically in [1.5; 4] - lambda2(.7), // lambda2 in [0.5; 0.99] - mu1(10), // target decrease in cost factor, in [2; 20] - xi(1.05), // xi typically belongs to [1; 1.1] - max_accepted(50), // depends on pb/neighborhood - max_generated(100), // depends on pb/neighborhood - theta(10), // theta is typically set to 10 - statIsInitialized(false), - outf("out.data") - { } - - moTrikiCoolingSchedule ( - Neighborhood& _neighborhood, moEval& _eval, double _initTemp, - double _max_accepted, - double _max_generated - ) - : initTemp(_initTemp), - mu2(10), // mu2 typically belongs to [1; 20] - K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature - K2(5), // ??? - lambda1(2), // the increase in temperature, typically in [1.5; 4] - lambda2(.7), // lambda2 in [0.5; 0.99] - mu1(10), // target decrease in cost factor, in [2; 20] - xi(1.05), // xi typically belongs to [1; 1.1] - max_accepted(_max_accepted), // depends on pb/neighborhood - max_generated(_max_generated), // depends on pb/neighborhood - theta(10), // theta is typically set to 10 - statIsInitialized(false), - outf("out.data") - { } - - /** - * Initial temperature - * @param _solution initial solution - */ - double init(EOT & _solution) { - - accepted = generated = costs_sum = 0; - - negative_temp = equilibrium_not_reached = frozen = 0; - - reinitializing = false; - terminated = false; - statIsInitialized = false; - - /// - cout << "INIT T=" << initTemp << endl; - /// - - //outf.open("out"); - //outf << "ok"; - //outf.close(); - - - return initTemp; - } + moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval& _eval, double _initTemp) + : initTemp(_initTemp), + mu2(10), // mu2 typically belongs to [1; 20] + K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature + K2(5), // ??? + lambda1(2), // the increase in temperature, typically in [1.5; 4] + lambda2(.7), // lambda2 in [0.5; 0.99] + mu1(10), // target decrease in cost factor, in [2; 20] + xi(1.05), // xi typically belongs to [1; 1.1] + max_accepted(50), // depends on pb/neighborhood + max_generated(100), // depends on pb/neighborhood + theta(10), // theta is typically set to 10 + statIsInitialized(false), + outf("out.data") + { } + + moTrikiCoolingSchedule ( + Neighborhood& _neighborhood, moEval& _eval, double _initTemp, + double _max_accepted, + double _max_generated + ) + : initTemp(_initTemp), + mu2(10), // mu2 typically belongs to [1; 20] + K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature + K2(5), // ??? + lambda1(2), // the increase in temperature, typically in [1.5; 4] + lambda2(.7), // lambda2 in [0.5; 0.99] + mu1(10), // target decrease in cost factor, in [2; 20] + xi(1.05), // xi typically belongs to [1; 1.1] + max_accepted(_max_accepted), // depends on pb/neighborhood + max_generated(_max_generated), // depends on pb/neighborhood + theta(10), // theta is typically set to 10 + statIsInitialized(false), + outf("out.data") + { } + + /** + * Initial temperature + * @param _solution initial solution + */ + double init(EOT & _solution) { + + accepted = generated = costs_sum = 0; + + negative_temp = equilibrium_not_reached = frozen = 0; + + reinitializing = false; + terminated = false; + statIsInitialized = false; + + /// + cout << "INIT T=" << initTemp << endl; + /// + + //outf.open("out"); + //outf << "ok"; + //outf.close(); + + + return initTemp; + } - /** - * update the temperature by a factor - * @param _temp current temperature to update - * @param _acceptedMove true when the move is accepted, false otherwise - */ - void update(double& _temp, bool _acceptedMove, EOT & _solution) { - - //cout << _temp << " g " << generated << endl; - - generated++; - - if (_acceptedMove) - { - accepted++; - //costs_sum += _solution.fitness(); - //varStat(_solution); - if (statIsInitialized) - momentStat(_solution); - else momentStat.init(_solution), statIsInitialized = true; - - //cout << _solution.fitness() << " avgCost=" << momentStat.value().first << endl; - } - - - if (accepted > max_accepted || generated > max_generated) { - - if (accepted == 0) // ADDED! Otherwise the computed std dev is null; we're probably at equilibrium - { - /// - cout << "Stopping: no accepted solution" << endl; - /// - - terminated = true; - return; - } - - /// - cout << (accepted > max_accepted? "MAXACC ": "MAXGEN "); - /// - - //double avgCost = costs_sum/(double)accepted; - //double stdDev = sqrt(varStat.value()); // WARNING: IT'S NO MORE THE AVG COST, NOW IT'S THE STD DEV! - //double variance = varStat.value(); - double avgCost = momentStat.value().first; - double variance = momentStat.value().second; - double stdDev = sqrt(variance); - double sigma = stdDev; - double delta = sigma/mu2; - - - //outf << avgCost << endl; - outf << _temp << endl; - //outf << prevAvgCost-delta << endl; - - - accepted = generated = costs_sum = 0; - //momentStat.init(_solution);//TODONE use next chain's first sol - statIsInitialized = false; - - /// - cout << "T=" << _temp << " avgCost=" << avgCost << " stdDev=" << stdDev << " currCost=" << _solution.fitness() << endl; - /// - - double alpha; - double oldprevAvgCost = prevAvgCost; - - /// - cout << "negTemp: " << negative_temp << " / " << K2 << endl; - /// - - if (negative_temp < K2) - { - if (!reinitializing) - { - /// - if (avgCost/(prevAvgCost-delta) > xi) cout << "/!\\ eq not reached!" << endl; - /// - - if (avgCost/(prevAvgCost-delta) > xi) - equilibrium_not_reached++; - else equilibrium_not_reached = 0; - } - if (equilibrium_not_reached > K1) - { - /// - cout << "/!\\ Reinitializing (eq not reached)" << endl; - /// - - reinitializing = true; - alpha = lambda1; - delta = sigma/mu1; - equilibrium_not_reached = 0; // ADDED! Otherwise the algo gets trapped here! - } - else if (_temp*delta/(sigma*sigma) >= 1) - { - /// - cout << "/!\\ neg temp!" << endl; - /// - - negative_temp++; - reinitializing = true; - if (negative_temp < K2) - { - alpha = lambda1; - delta = sigma/mu1; - } else - alpha = lambda2; - } - - // First interpretation of the pseudocode indentation: (seems obviously false because it makes the above code unreachable) - /* - } - else - { - cout << "ccc" << endl; - reinitializing = false; - prevAvgCost = avgCost; - alpha = 1-_temp*delta/(sigma*sigma); - } - */ - - // Second interpretation of the pseudocode indentation: - else - { - /// - cout << "[normal decrease]" << endl; - /// - - reinitializing = false; - prevAvgCost = avgCost; - //alpha = 1-_temp*delta/(sigma*sigma); - alpha = 1-_temp*delta/variance; - - //alpha = (sigma==0? 1: 1-_temp*delta/(sigma*sigma)); // ADDED! but removed - - if (sigma == 0) // ADDED! When std dev is null, the solution is probably at eq, and the algo can't go on anyways - terminated = true, cout << "Stopping: null std dev" << endl; - } - } - // FIXME: else what? alpha=? - - + /** + * update the temperature by a factor + * @param _temp current temperature to update + * @param _acceptedMove true when the move is accepted, false otherwise + */ + void update(double& _temp, bool _acceptedMove, EOT & _solution) { + + //cout << _temp << " g " << generated << endl; + + generated++; + + if (_acceptedMove) + { + accepted++; + //costs_sum += _solution.fitness(); + //varStat(_solution); + if (statIsInitialized) + momentStat(_solution); + else momentStat.init(_solution), statIsInitialized = true; + + //cout << _solution.fitness() << " avgCost=" << momentStat.value().first << endl; + } + + + if (accepted > max_accepted || generated > max_generated) { + + if (accepted == 0) // ADDED! Otherwise the computed std dev is null; we're probably at equilibrium + { + /// + cout << "Stopping: no accepted solution" << endl; + /// + + terminated = true; + return; + } + + /// + cout << (accepted > max_accepted? "MAXACC ": "MAXGEN "); + /// + + //double avgCost = costs_sum/(double)accepted; + //double stdDev = sqrt(varStat.value()); // WARNING: IT'S NO MORE THE AVG COST, NOW IT'S THE STD DEV! + //double variance = varStat.value(); + double avgCost = momentStat.value().first; + double variance = momentStat.value().second; + double stdDev = sqrt(variance); + double sigma = stdDev; + double delta = sigma/mu2; + + + //outf << avgCost << endl; + outf << _temp << endl; + //outf << prevAvgCost-delta << endl; + + + accepted = generated = costs_sum = 0; + //momentStat.init(_solution);//TODONE use next chain's first sol + statIsInitialized = false; + + /// + cout << "T=" << _temp << " avgCost=" << avgCost << " stdDev=" << stdDev << " currCost=" << _solution.fitness() << endl; + /// + + double alpha; + double oldprevAvgCost = prevAvgCost; + + /// + cout << "negTemp: " << negative_temp << " / " << K2 << endl; + /// + + if (negative_temp < K2) + { + if (!reinitializing) + { + /// + if (avgCost/(prevAvgCost-delta) > xi) cout << "/!\\ eq not reached!" << endl; + /// + + if (avgCost/(prevAvgCost-delta) > xi) + equilibrium_not_reached++; + else equilibrium_not_reached = 0; + } + if (equilibrium_not_reached > K1) + { + /// + cout << "/!\\ Reinitializing (eq not reached)" << endl; + /// + + reinitializing = true; + alpha = lambda1; + delta = sigma/mu1; + equilibrium_not_reached = 0; // ADDED! Otherwise the algo gets trapped here! + } + else if (_temp*delta/(sigma*sigma) >= 1) + { + /// + cout << "/!\\ neg temp!" << endl; + /// + + negative_temp++; + reinitializing = true; + if (negative_temp < K2) + { + alpha = lambda1; + delta = sigma/mu1; + } else + alpha = lambda2; + } + + // First interpretation of the pseudocode indentation: (seems obviously false because it makes the above code unreachable) + /* + } + else + { + cout << "ccc" << endl; + reinitializing = false; + prevAvgCost = avgCost; + alpha = 1-_temp*delta/(sigma*sigma); + } + */ + + // Second interpretation of the pseudocode indentation: + else + { + /// + cout << "[normal decrease]" << endl; + /// + + reinitializing = false; + prevAvgCost = avgCost; + //alpha = 1-_temp*delta/(sigma*sigma); + alpha = 1-_temp*delta/variance; + + //alpha = (sigma==0? 1: 1-_temp*delta/(sigma*sigma)); // ADDED! but removed + + if (sigma == 0) // ADDED! When std dev is null, the solution is probably at eq, and the algo can't go on anyways + terminated = true, cout << "Stopping: null std dev" << endl; + } + } + // FIXME: else what? alpha=? + + - /// - cout << "*=" << alpha << endl; - /// - - _temp *= alpha; - - - // Never seems to be used - if (avgCost == oldprevAvgCost) // use a neighborhood to approximate double equality? - frozen++; - else frozen = 0; - - - - //exit(0); - //cin.get(); - - - } - - } + /// + cout << "*=" << alpha << endl; + /// + + _temp *= alpha; + + + // Never seems to be used + if (avgCost == oldprevAvgCost) // use a neighborhood to approximate double equality? + frozen++; + else frozen = 0; + + + + //exit(0); + //cin.get(); + + + } + + } - //! Function which proceeds to the cooling - /*! - */ - bool operator() (double temperature) - { - /// - if (terminated) cout << "TERMINATED" << endl; - /// - - return frozen < theta - && !terminated ; // ADDED! because 'frozen' doesn't terminate anything - } + //! Function which proceeds to the cooling + /*! + */ + bool operator() (double temperature) + { + /// + if (terminated) cout << "TERMINATED" << endl; + /// + + return frozen < theta + && !terminated ; // ADDED! because 'frozen' doesn't terminate anything + } -private: - //moNeighborhoodStat nhStat; - //moStdFitnessNeighborStat stdDevStat; - const double - // parameters of the algorithm - //currentTemp, - initTemp, - //ratio, - //threshold, - mu2, // mu2 typically belongs to [1; 20] - K1, // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature - K2, - lambda1, // the increase in temperature, typically in [1.5; 4] - lambda2, // lambda2 in [0.5; 0.99] - mu1, // target decrease in cost factor, in [2; 20] - xi // xi typically belongs to [1; 1.1] - // private variables - ; - double - stdDev, - prevAvgCost, - expectedDecreaseInCost, // delta - costs_sum - ; - const int - max_accepted, - max_generated, - theta // theta is typically set to 10 - ; - int - accepted, - generated, - equilibrium_not_reached, - negative_temp, - frozen - ; - bool reinitializing, terminated; - - //moFitnessVarianceStat varStat; - moFitnessMomentsStat momentStat; - bool statIsInitialized; - - ofstream outf; - +//private: +public://FIXME add friend + //moNeighborhoodStat nhStat; + //moStdFitnessNeighborStat stdDevStat; + const double + // parameters of the algorithm + //currentTemp, + initTemp, + //ratio, + //threshold, + mu2, // mu2 typically belongs to [1; 20] + K1, // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature + K2, + lambda1, // the increase in temperature, typically in [1.5; 4] + lambda2, // lambda2 in [0.5; 0.99] + mu1, // target decrease in cost factor, in [2; 20] + xi // xi typically belongs to [1; 1.1] + // private variables + ; + double + stdDev, + prevAvgCost, + expectedDecreaseInCost, // delta + costs_sum + ; + const int + max_accepted, + max_generated, + theta // theta is typically set to 10 + ; + int + accepted, + generated, + equilibrium_not_reached, + negative_temp, + frozen + ; + bool reinitializing, terminated; + + //moFitnessVarianceStat varStat; + moFitnessMomentsStat momentStat; + bool statIsInitialized; + + ofstream outf; + }; #endif diff --git a/mo/src/explorer/moSAexplorer.h b/mo/src/explorer/moSAexplorer.h deleted file mode 100644 index 09eda6dab..000000000 --- a/mo/src/explorer/moSAexplorer.h +++ /dev/null @@ -1,220 +0,0 @@ -/* - - Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - - Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau - - This software is governed by the CeCILL license under French law and - abiding by the rules of distribution of free software. You can use, - modify and/ or redistribute the software under the terms of the CeCILL - license as circulated by CEA, CNRS and INRIA at the following URL - "http://www.cecill.info". - - As a counterpart to the access to the source code and rights to copy, - modify and redistribute granted by the license, users are provided only - with a limited warranty and the software's author, the holder of the - economic rights, and the successive licensors have only limited liability. - - In this respect, the user's attention is drawn to the risks associated - with loading, using, modifying and/or developing or reproducing the - software by the user in light of its specific status of free software, - that may mean that it is complicated to manipulate, and that also - therefore means that it is reserved for developers and experienced - professionals having in-depth computer knowledge. Users are therefore - encouraged to load and test the software's suitability as regards their - requirements in conditions enabling the security of their systems and/or - data to be ensured and, more generally, to use and operate it in the - same conditions as regards security. - The fact that you are presently reading this means that you have had - knowledge of the CeCILL license and that you accept its terms. - - ParadisEO WebSite : http://paradiseo.gforge.inria.fr - Contact: paradiseo-help@lists.gforge.inria.fr -*/ - -#ifndef _moSAexplorer_h -#define _moSAexplorer_h - -#include - -#include -#include -#include -#include -#include -#include - -#include - -/** - * Explorer for the Simulated Annealing - * Only the symetric case is considered when Q(x,y) = Q(y,x) - * Fitness must be > 0 - * - */ -template< class Neighbor > -class moSAexplorer : public moNeighborhoodExplorer -{ -public: - typedef typename Neighbor::EOT EOT ; - typedef moNeighborhood Neighborhood ; - - using moNeighborhoodExplorer::neighborhood; - using moNeighborhoodExplorer::eval; - using moNeighborhoodExplorer::selectedNeighbor; - - /** - * Constructor - * @param _neighborhood the neighborhood - * @param _eval the evaluation function - * @param _solNeighborComparator a solution vs neighbor comparator - * @param _coolingSchedule the cooling schedule - */ - /* - moSAexplorer ( - Neighborhood& _neighborhood, - moEval& _eval, - moSolNeighborComparator& _solNeighborComparator, - moCoolingSchedule& _coolingSchedule - ) - : moNeighborhoodExplorer(_neighborhood, _eval), - solNeighborComparator(_solNeighborComparator), - coolingSchedule(_coolingSchedule) - { - isAccept = false; - - if (!neighborhood.isRandom()) { - std::cout << "moSAexplorer::Warning -> the neighborhood used is not random" << std::endl; - } - }*/ - moSAexplorer ( - Neighborhood& _neighborhood, - moCoolingSchedule& _cool, - eoOptional< moEval > _eval = NULL, - eoOptional< moSolNeighborComparator > _comp = NULL - ) - : moNeighborhoodExplorer(_neighborhood, _eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval))), - default_eval(NULL), // removed in C++11 with unique_ptr - defaultSolNeighborComp(NULL), // removed in C++11 with unique_ptr - solNeighborComparator(_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator())), - coolingSchedule(_coolingSchedule) - { - isAccept = false; - - if (!neighborhood.isRandom()) { - std::cout << "moSAexplorer::Warning -> the neighborhood used is not random" << std::endl; - } - } - - /** - * Destructor - */ - ~moSAexplorer() { - if (defaultSolNeighborComp != NULL) - delete defaultSolNeighborComp; - if (default_eval != NULL) - delete default_eval; - } - - /** - * initialization of the initial temperature - * @param _solution the solution - */ - virtual void initParam(EOT & _solution) { - temperature = coolingSchedule.init(_solution); - isAccept = false; - }; - - /** - * decrease the temperature if necessary - * @param _solution unused solution - */ - virtual void updateParam(EOT & _solution) { - coolingSchedule.update(temperature, this->moveApplied(), _solution); - }; - - /** - * terminate: NOTHING TO DO - * @param _solution unused solution - */ - virtual void terminate(EOT & _solution) {}; - - /** - * Explore one random solution in the neighborhood - * @param _solution the solution - */ - virtual void operator()(EOT & _solution) { - //Test if _solution has a Neighbor - if (neighborhood.hasNeighbor(_solution)) { - //init on the first neighbor: supposed to be random solution in the neighborhood - neighborhood.init(_solution, selectedNeighbor); - - //eval the _solution moved with the neighbor and stock the result in the neighbor - eval(_solution, selectedNeighbor); - } - else { - //if _solution hasn't neighbor, - isAccept=false; - } - }; - - /** - * continue if the temperature is not too low - * @param _solution the solution - * @return true if the criteria from the cooling schedule is true - */ - virtual bool isContinue(EOT & _solution) { - return coolingSchedule(temperature); - }; - - /** - * acceptance criterion according to the boltzmann criterion - * @param _solution the solution - * @return true if better neighbor or rnd < exp(delta f / T) - */ - virtual bool accept(EOT & _solution) { - if (neighborhood.hasNeighbor(_solution)) { - if (solNeighborComparator(_solution, selectedNeighbor)) // accept if the current neighbor is better than the solution - isAccept = true; - else { - double alpha=0.0; - double fit1, fit2; - fit1=(double)selectedNeighbor.fitness(); - fit2=(double)_solution.fitness(); - if (fit1 < fit2) // this is a maximization - alpha = exp((fit1 - fit2) / temperature ); - else // this is a minimization - alpha = exp((fit2 - fit1) / temperature ); - isAccept = (rng.uniform() < alpha) ; - } - } - return isAccept; - }; - - /** - * Getter - * @return the temperature - */ - double getTemperature() { - return temperature; - } - -private: - - moFullEvalByCopy* default_eval; - moSolNeighborComparator* defaultSolNeighborComp; - - // comparator betwenn solution and neighbor - moSolNeighborComparator& solNeighborComparator; - - moCoolingSchedule& coolingSchedule; - - // temperatur of the process - double temperature; - - // true if the move is accepted - bool isAccept ; -}; - - -#endif From 8c6610ec67558df7ee3c4bdb23a1c679e5652781 Mon Sep 17 00:00:00 2001 From: LPTK Date: Mon, 1 Jul 2013 16:36:10 +0200 Subject: [PATCH 32/36] intermediate commit 4 --- eo/src/utils/eoUpdater.h | 23 +++ mo/src/algo/moMetropolisHasting.h | 110 ---------- mo/src/algo/moSA.h | 79 ++++---- mo/src/continuator/moUpdater.h | 5 +- .../coolingSchedule/moTrikiCoolingSchedule.h | 69 ++++++- mo/src/explorer/moMetropolisHastingExplorer.h | 190 ------------------ mo/src/explorer/moNeighborhoodExplorer.h | 10 +- mo/src/mo.h | 7 +- .../sampling/moMHBestFitnessCloudSampling.h | 4 +- mo/src/sampling/moMHRndFitnessCloudSampling.h | 4 +- mo/test/CMakeLists.txt | 6 +- mo/test/t-moMetropolisHasting.cpp | 65 ------ mo/test/t-moMetropolisHastingExplorer.cpp | 97 --------- mo/test/t-moSAexplorer.cpp | 87 -------- 14 files changed, 149 insertions(+), 607 deletions(-) delete mode 100644 mo/src/algo/moMetropolisHasting.h delete mode 100644 mo/src/explorer/moMetropolisHastingExplorer.h delete mode 100644 mo/test/t-moMetropolisHasting.cpp delete mode 100644 mo/test/t-moMetropolisHastingExplorer.cpp delete mode 100644 mo/test/t-moSAexplorer.cpp diff --git a/eo/src/utils/eoUpdater.h b/eo/src/utils/eoUpdater.h index 072b9a9bb..e86225950 100644 --- a/eo/src/utils/eoUpdater.h +++ b/eo/src/utils/eoUpdater.h @@ -50,6 +50,29 @@ public: eoUpdater& addTo(eoCheckPoint& cp) { cp.add(*this); return *this; } }; +/** + an eoUpdater that simply calls a function with no arguments + + @ingroup Utilities +*/ +class eoFunctionCaller : public eoUpdater +{public : + /** Default Ctor - requires a pointer to the function to call */ + eoFunctionCaller(void (*_fct)()) + : fct(_fct) + { } + + /** Simply increments */ + virtual void operator()() + { + (*fct)(); + } + + virtual std::string className(void) const { return "eoFunctionCaller"; } +private: + void (*fct)(); +}; + /** an eoUpdater that simply increments a counter diff --git a/mo/src/algo/moMetropolisHasting.h b/mo/src/algo/moMetropolisHasting.h deleted file mode 100644 index 5b4292459..000000000 --- a/mo/src/algo/moMetropolisHasting.h +++ /dev/null @@ -1,110 +0,0 @@ -/* - -Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - -Sebastien Verel, Arnaud Liefooghe, Jeremie Humeau - -This software is governed by the CeCILL license under French law and -abiding by the rules of distribution of free software. You can ue, -modify and/ or redistribute the software under the terms of the CeCILL -license as circulated by CEA, CNRS and INRIA at the following URL -"http://www.cecill.info". - -In this respect, the user's attention is drawn to the risks associated -with loading, using, modifying and/or developing or reproducing the -software by the user in light of its specific status of free software, -that may mean that it is complicated to manipulate, and that also -therefore means that it is reserved for developers and experienced -professionals having in-depth computer knowledge. Users are therefore -encouraged to load and test the software's suitability as regards their -requirements in conditions enabling the security of their systems and/or -data to be ensured and, more generally, to use and operate it in the -same conditions as regards security. -The fact that you are presently reading this means that you have had -knowledge of the CeCILL license and that you accept its terms. - -ParadisEO WebSite : http://paradiseo.gforge.inria.fr -Contact: paradiseo-help@lists.gforge.inria.fr -*/ - -#ifndef _moMetropolisHasting_h -#define _moMetropolisHasting_h - -#include -#include -#include -#include -#include - -/** - * Metropolis-Hasting local search - * Only the symetric case is considered when Q(x,y) = Q(y,x) - * Fitness must be > 0 - * - * At each iteration, - * one of the random solution in the neighborhood is selected - * if the selected neighbor have higher or equal fitness than the current solution - * then the solution is replaced by the selected neighbor - * if a random number from [0,1] is lower than fitness(neighbor) / fitness(solution) - * then the solution is replaced by the selected neighbor - * the algorithm stops when the number of iterations is too large - */ -template -class moMetropolisHasting: public moLocalSearch -{ -public: - typedef typename Neighbor::EOT EOT; - typedef moNeighborhood Neighborhood ; - - /** - * Basic constructor of the Metropolis-Hasting - * @param _neighborhood the neighborhood - * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function - * @param _nbStep maximum step to do - */ - moMetropolisHasting(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep): - moLocalSearch(explorer, trueCont, _fullEval), - explorer(_neighborhood, _eval, defaultNeighborComp, defaultSolNeighborComp, _nbStep) - {} - - /** - * Simple constructor of the Metropolis-Hasting - * @param _neighborhood the neighborhood - * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function - * @param _nbStep maximum step to do - * @param _cont an external continuator - */ - moMetropolisHasting(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep, moContinuator& _cont): - moLocalSearch(explorer, _cont, _fullEval), - explorer(_neighborhood, _eval, defaultNeighborComp, defaultSolNeighborComp, _nbStep) - {} - - /** - * General constructor of the Metropolis-Hasting - * @param _neighborhood the neighborhood - * @param _fullEval the full evaluation function - * @param _eval neighbor's evaluation function - * @param _nbStep maximum step to do - * @param _cont an external continuator - * @param _compN a neighbor vs neighbor comparator - * @param _compSN a solution vs neighbor comparator - */ - moMetropolisHasting(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep, moContinuator& _cont, moNeighborComparator& _compN, moSolNeighborComparator& _compSN): - moLocalSearch(explorer, _cont, _fullEval), - explorer(_neighborhood, _eval, _compN, _compSN, _nbStep) - {} - -private: - // always true continuator - moTrueContinuator trueCont; - // compare the fitness values of neighbors - moNeighborComparator defaultNeighborComp; - // compare the fitness values of the solution and the neighbor - moSolNeighborComparator defaultSolNeighborComp; - // MetropolisHasting explorer - moMetropolisHastingExplorer explorer; -}; - -#endif diff --git a/mo/src/algo/moSA.h b/mo/src/algo/moSA.h index 89c61ad3a..0914f0db0 100644 --- a/mo/src/algo/moSA.h +++ b/mo/src/algo/moSA.h @@ -2,7 +2,7 @@ Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 -Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau +Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau, Lionel Parreaux This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can ue, @@ -31,7 +31,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr #define _moSA_h #include -#include +#include #include #include #include @@ -64,16 +64,16 @@ public: /*moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01) : defaultCool(_initT, _alpha, _span, _finalT), explorer(_neighborhood, _eval, defaultSolNeighborComp, defaultCool), - moLocalSearch(explorer, trueCont, _fullEval) + moLocalSearch(explorer, defaultContinuator, _fullEval) { }*/ moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01) : moLocalSearch ( - explorer_ptr = defaultExplorer = new moSAexplorer(_neighborhood, _eval, NULL, *(defaultCool = new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT))), - *(trueCont = new moTrueContinuator()), + explorer_ptr = defaultExplorer = new moSAExplorer(_neighborhood, _eval, /*NULL,*/ *(defaultCool = new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)), NULL), + *(defaultContinuator = new moTrueContinuator()), _fullEval ), explorer(*explorer_ptr), //defaultCool(), - default_eval(NULL) // removed in C++11 with unique_ptr + defaultEval(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(new moSolNeighborComparator()) //explorer(_neighborhood, _eval, *defaultSolNeighborComp, *defaultCool) { } @@ -86,7 +86,7 @@ public: // * @param _cool a cooling schedule // */ // moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, moCoolingSchedule& _cool): -// moLocalSearch(explorer, trueCont, _fullEval), +// moLocalSearch(explorer, defaultContinuator, _fullEval), // defaultCool(0, 0, 0, 0), // explorer(_neighborhood, _eval, defaultSolNeighborComp, _cool) // {} @@ -137,56 +137,59 @@ public: eoOptional< moContinuator > _cont = NULL, eoOptional< moSolNeighborComparator > _comp = NULL ) - /*: moLocalSearch(explorer, _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval), + /*: moLocalSearch(explorer, _cont.hasValue()? _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval), defaultCool(NULL), // removed in C++11 with unique_ptr - default_eval(NULL), // removed in C++11 with unique_ptr + defaultEval(NULL), // removed in C++11 with unique_ptr defaultSolNeighborComp(NULL), // removed in C++11 with unique_ptr - trueCont(NULL), // removed in C++11 with unique_ptr + defaultContinuator(NULL), // removed in C++11 with unique_ptr explorer ( _neighborhood, - _eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), - // C++11: _eval.hasValue()? _eval.get(): default_eval = new moFullEvalByCopy(), + _eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy(_fullEval)), + // C++11: _eval.hasValue()? _eval.get(): defaultEval = new moFullEvalByCopy(), _comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), _cool ) { }*/ : moLocalSearch ( - explorer_ptr = defaultExplorer = new moSAexplorer ( + explorer_ptr = defaultExplorer = new moSAExplorer ( _neighborhood, - //_eval, //_eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), - _eval.hasValue()? default_eval = NULL, _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval)), - // C++11: _eval.hasValue()? _eval.get(): default_eval = new moFullEvalByCopy(), - _comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), - _cool ), - _cont.hasValue()? trueCont = NULL, _cont.get(): *(trueCont = new moTrueContinuator()), + //_eval, //_eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy(_fullEval)), + _eval.hasValue()? defaultEval = NULL, _eval.get(): *(defaultEval = new moFullEvalByCopy(_fullEval)), + // C++11: _eval.hasValue()? _eval.get(): defaultEval = new moFullEvalByCopy(), + //_comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), + //_cool ), + _cool, + _comp ), + // ), + _cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval ), explorer(*explorer_ptr), defaultCool(NULL) // removed in C++11 with unique_ptr - //default_eval(NULL), // removed in C++11 with unique_ptr - //trueCont(NULL) // removed in C++11 with unique_ptr + //defaultEval(NULL), // removed in C++11 with unique_ptr + //defaultContinuator(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr { } moSA ( eoEvalFunc& _fullEval, - moSAexplorer& _explorer, + moSAExplorer& _explorer, eoOptional< moContinuator > _cont = NULL ) : moLocalSearch ( *(explorer_ptr = &_explorer), - _cont.hasValue()? _cont.get(): *(trueCont = new moTrueContinuator()), _fullEval ), + _cont.hasValue()? _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval ), defaultExplorer(NULL), // removed in C++11 with unique_ptr explorer(*explorer_ptr), defaultCool(NULL), // removed in C++11 with unique_ptr - //default_eval(NULL), // removed in C++11 with unique_ptr - trueCont(NULL) // removed in C++11 with unique_ptr + //defaultEval(NULL), // removed in C++11 with unique_ptr + defaultContinuator(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr { } /* moSA ( eoEvalFunc& _fullEval, - moSAexplorer& _explorer, + moSAExplorer& _explorer, moContinuator _cont ) : moLocalSearch ( @@ -194,8 +197,8 @@ public: _cont, _fullEval ), defaultExplorer(NULL), // removed in C++11 with unique_ptr defaultCool(NULL), // removed in C++11 with unique_ptr - //default_eval(NULL), // removed in C++11 with unique_ptr - trueCont(NULL) // removed in C++11 with unique_ptr + //defaultEval(NULL), // removed in C++11 with unique_ptr + defaultContinuator(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr { } */ @@ -207,22 +210,22 @@ public: virtual ~moSA () { // Note: using unique_ptr would allow us to remove this explicit destructor, but they were only introduced in C++11 - if (trueCont != NULL) - delete trueCont; + if (defaultContinuator != NULL) + delete defaultContinuator; if (defaultExplorer != NULL) delete defaultExplorer; - if (default_eval != NULL) - delete default_eval; + if (defaultEval != NULL) + delete defaultEval; } private: - moFullEvalByCopy* default_eval; - moSAexplorer* defaultExplorer; - moSAexplorer* explorer_ptr; // Not NULL - moSAexplorer& explorer; + moFullEvalByCopy* defaultEval; + moSAExplorer* defaultExplorer; + moSAExplorer* explorer_ptr; // Not NULL + moSAExplorer& explorer; moSimpleCoolingSchedule* defaultCool; // C++11: const std::unique_ptr> - //moFullEvalByCopy* default_eval; - moTrueContinuator* trueCont; + //moFullEvalByCopy* defaultEval; + moTrueContinuator* defaultContinuator; //moSolNeighborComparator* defaultSolNeighborComp; }; diff --git a/mo/src/continuator/moUpdater.h b/mo/src/continuator/moUpdater.h index 0be846d38..37a26a04f 100644 --- a/mo/src/continuator/moUpdater.h +++ b/mo/src/continuator/moUpdater.h @@ -39,9 +39,8 @@ #include /** - * Base class for to update what ever you want - * similar to eoUpdater - * But there is an init method ! + * Base class for updating whatever you want. + * Similar to eoUpdater, but there is an "init" method ! */ class moUpdater : public eoF { diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index 5c1e7e0c8..900e3b189 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -132,6 +132,7 @@ public: void update(double& _temp, bool _acceptedMove, EOT & _solution) { //cout << _temp << " g " << generated << endl; + prevTemp = _temp; generated++; @@ -147,9 +148,12 @@ public: //cout << _solution.fitness() << " avgCost=" << momentStat.value().first << endl; } + markovChainEnded = false; if (accepted > max_accepted || generated > max_generated) { + markovChainEnded = true; + if (accepted == 0) // ADDED! Otherwise the computed std dev is null; we're probably at equilibrium { /// @@ -301,9 +305,24 @@ public: return frozen < theta && !terminated ; // ADDED! because 'frozen' doesn't terminate anything } + + + bool markovChainJustEnded() + { + return markovChainEnded; + } -//private: -public://FIXME add friend + MarkovChainStats& getMarkovChainStats() + { + return markovChainStats; + } + + + + + +private: +//public://FIXME add friend //moNeighborhoodStat nhStat; //moStdFitnessNeighborStat stdDevStat; const double @@ -325,7 +344,8 @@ public://FIXME add friend stdDev, prevAvgCost, expectedDecreaseInCost, // delta - costs_sum + costs_sum, + prevTemp ; const int max_accepted, @@ -339,7 +359,7 @@ public://FIXME add friend negative_temp, frozen ; - bool reinitializing, terminated; + bool reinitializing, terminated, markovChainEnded; //moFitnessVarianceStat varStat; moFitnessMomentsStat momentStat; @@ -347,7 +367,48 @@ public://FIXME add friend ofstream outf; + +protected: + + class Monitor { + public: + + Monitor(moTrikiCoolingSchedule& _cooling) + : cooling(_cooling) + { } + + /*void setTemperatureOstream(ostream& os) + { + + }*/ + void getLatestTemperature() + { + return cooling.prevTemp; + } + + void printCurrentStatus(ostream& os) + { + if (accepted >= max_accepted || generated >= max_generated) + { + os << "Markov chain finished. Temp was " << getLatestTemperature(); // chain number + + } + } + + private: + moTrikiCoolingSchedule& cooling; + }; + }; #endif + + + + + + + + + diff --git a/mo/src/explorer/moMetropolisHastingExplorer.h b/mo/src/explorer/moMetropolisHastingExplorer.h deleted file mode 100644 index ee6169d99..000000000 --- a/mo/src/explorer/moMetropolisHastingExplorer.h +++ /dev/null @@ -1,190 +0,0 @@ -/* - - Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - - Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau - - This software is governed by the CeCILL license under French law and - abiding by the rules of distribution of free software. You can use, - modify and/ or redistribute the software under the terms of the CeCILL - license as circulated by CEA, CNRS and INRIA at the following URL - "http://www.cecill.info". - - As a counterpart to the access to the source code and rights to copy, - modify and redistribute granted by the license, users are provided only - with a limited warranty and the software's author, the holder of the - economic rights, and the successive licensors have only limited liability. - - In this respect, the user's attention is drawn to the risks associated - with loading, using, modifying and/or developing or reproducing the - software by the user in light of its specific status of free software, - that may mean that it is complicated to manipulate, and that also - therefore means that it is reserved for developers and experienced - professionals having in-depth computer knowledge. Users are therefore - encouraged to load and test the software's suitability as regards their - requirements in conditions enabling the security of their systems and/or - data to be ensured and, more generally, to use and operate it in the - same conditions as regards security. - The fact that you are presently reading this means that you have had - knowledge of the CeCILL license and that you accept its terms. - - ParadisEO WebSite : http://paradiseo.gforge.inria.fr - Contact: paradiseo-help@lists.gforge.inria.fr -*/ - -#ifndef _moMetropolisHastingExplorer_h -#define _moMetropolisHastingExplorer_h - -#include - -#include -#include -#include -#include - -#include - -/** - * Explorer for the Metropolis-Hasting Sampling. - * Only the symetric case is considered when Q(x,y) = Q(y,x) - * Fitness must be > 0 - */ -template< class Neighbor > -class moMetropolisHastingExplorer : public moNeighborhoodExplorer -{ -public: - typedef typename Neighbor::EOT EOT ; - typedef moNeighborhood Neighborhood ; - - using moNeighborhoodExplorer::neighborhood; - using moNeighborhoodExplorer::eval; - using moNeighborhoodExplorer::selectedNeighbor; - - /** - * Constructor - * @param _neighborhood the neighborhood - * @param _eval the evaluation function - * @param _neighborComparator a neighbor comparator - * @param _solNeighborComparator a solution vs neighbor comparator - * @param _nbStep maximum number of step to do - */ - moMetropolisHastingExplorer( - Neighborhood& _neighborhood, - moEval& _eval, - moNeighborComparator& _neighborComparator, - moSolNeighborComparator& _solNeighborComparator, - unsigned int _nbStep - ): moNeighborhoodExplorer(_neighborhood, _eval), - neighborComparator(_neighborComparator), - solNeighborComparator(_solNeighborComparator), - nbStep(_nbStep) - { - isAccept = false; - if (!neighborhood.isRandom()) { - std::cout << "moMetropolisHastingExplorer::Warning -> the neighborhood used is not random" << std::endl; - } - } - - /** - * Destructor - */ - ~moMetropolisHastingExplorer() { - } - - /** - * initialization of the number of step to be done - * @param _solution unused solution - */ - virtual void initParam(EOT & _solution) { - step = 0; - isAccept = true; - }; - - /** - * increase the number of step - * @param _solution unused solution - */ - virtual void updateParam(EOT & _solution) { - step++; - }; - - /** - * terminate: NOTHING TO DO - * @param _solution unused solution - */ - virtual void terminate(EOT & _solution) {}; - - /** - * Explore the neighborhood of a solution - * @param _solution - */ - virtual void operator()(EOT & _solution) { - //Test if _solution has a Neighbor - if (neighborhood.hasNeighbor(_solution)) { - //init the first neighbor - neighborhood.init(_solution, selectedNeighbor); - - //eval the _solution moved with the neighbor and stock the result in the neighbor - eval(_solution, selectedNeighbor); - } - else { - //if _solution hasn't neighbor, - isAccept=false; - } - }; - - /** - * continue if there is a neighbor and it is remainds some steps to do - * @param _solution the solution - * @return true there is some steps to do - */ - virtual bool isContinue(EOT & _solution) { - return (step < nbStep) ; - }; - - /** - * accept test if an ameliorated neighbor was found - * @param _solution the solution - * @return true if the best neighbor ameliorate the fitness - */ - virtual bool accept(EOT & _solution) { - double alpha=0.0; - if (neighborhood.hasNeighbor(_solution)) { - if (solNeighborComparator(_solution, selectedNeighbor)) - isAccept = true; - else { - if (_solution.fitness() != 0) { - if ( (double)selectedNeighbor.fitness() < (double)_solution.fitness()) // maximizing - alpha = (double) selectedNeighbor.fitness() / (double) _solution.fitness(); - else //minimizing - alpha = (double) _solution.fitness() / (double) selectedNeighbor.fitness(); - isAccept = (rng.uniform() < alpha) ; - } - else { - if ( (double) selectedNeighbor.fitness() < (double) _solution.fitness()) // maximizing - isAccept = true; - else - isAccept = false; - } - } - } - return isAccept; - }; - -private: - // comparator betwenn solution and neighbor or between neighbors - moNeighborComparator& neighborComparator; - moSolNeighborComparator& solNeighborComparator; - - // current number of step - unsigned int step; - - // maximum number of steps to do - unsigned int nbStep; - - // true if the move is accepted - bool isAccept ; -}; - - -#endif diff --git a/mo/src/explorer/moNeighborhoodExplorer.h b/mo/src/explorer/moNeighborhoodExplorer.h index 8b4113b7a..76ee12af7 100644 --- a/mo/src/explorer/moNeighborhoodExplorer.h +++ b/mo/src/explorer/moNeighborhoodExplorer.h @@ -65,15 +65,19 @@ public: typedef typename Neighbor::EOT EOT; typedef typename EOT::Fitness Fitness ; - moNeighborhoodExplorer():neighborhood(dummyNeighborhood), eval(dummyEval), isMoved(false) {} + moNeighborhoodExplorer() + : neighborhood(dummyNeighborhood), eval(dummyEval), isMoved(false) + { } /** * Constructor with a Neighborhood and evaluation function * @param _neighborhood the neighborhood * @param _eval the evaluation function */ - moNeighborhoodExplorer(Neighborhood& _neighborhood, moEval& _eval):neighborhood(_neighborhood), eval(_eval), isMoved(false) {} - + moNeighborhoodExplorer(Neighborhood& _neighborhood, moEval& _eval) + : neighborhood(_neighborhood), eval(_eval), isMoved(false) + { } + /** * Init Search parameters * @param _solution the solution to explore diff --git a/mo/src/mo.h b/mo/src/mo.h index 3e65f8081..ef83384be 100755 --- a/mo/src/mo.h +++ b/mo/src/mo.h @@ -43,7 +43,7 @@ #include #include #include -#include +#include #include #include #include @@ -118,14 +118,15 @@ #include #include #include -#include +#include #include #include #include #include #include #include -#include +#include +//#include #include #include #include diff --git a/mo/src/sampling/moMHBestFitnessCloudSampling.h b/mo/src/sampling/moMHBestFitnessCloudSampling.h index ebba8a3b9..e9c71f35b 100644 --- a/mo/src/sampling/moMHBestFitnessCloudSampling.h +++ b/mo/src/sampling/moMHBestFitnessCloudSampling.h @@ -39,7 +39,7 @@ #include #include #include -#include +#include #include #include @@ -87,7 +87,7 @@ public: delete localSearch; // Metropolis-Hasting sampling - localSearch = new moMetropolisHasting(_neighborhood, _fullEval, _eval, _nbStep); + localSearch = new moMetropolisHastings(_neighborhood, _fullEval, _eval, _nbStep); // delete the checkpoint with the wrong continuator delete checkpoint; diff --git a/mo/src/sampling/moMHRndFitnessCloudSampling.h b/mo/src/sampling/moMHRndFitnessCloudSampling.h index 292d42344..2a3cca639 100644 --- a/mo/src/sampling/moMHRndFitnessCloudSampling.h +++ b/mo/src/sampling/moMHRndFitnessCloudSampling.h @@ -39,7 +39,7 @@ #include #include #include -#include +#include #include #include @@ -87,7 +87,7 @@ public: delete localSearch; // Metropolis-Hasting sampling - localSearch = new moMetropolisHasting(_neighborhood, _fullEval, _eval, _nbStep); + localSearch = new moMetropolisHastings(_neighborhood, _fullEval, _eval, _nbStep); // delete the checkpoint with the wrong continuator delete checkpoint; diff --git a/mo/test/CMakeLists.txt b/mo/test/CMakeLists.txt index 319ffb0c9..898e6e22d 100644 --- a/mo/test/CMakeLists.txt +++ b/mo/test/CMakeLists.txt @@ -38,7 +38,7 @@ set (TEST_LIST t-moNeutralHCexplorer t-moFirstImprHCexplorer t-moRandomWalkExplorer - t-moMetropolisHastingExplorer + t-moMetropolisHastingsExplorer t-moRandomNeutralWalkExplorer t-moTSexplorer t-moSolComparator @@ -52,7 +52,7 @@ set (TEST_LIST t-moMonOpPerturb t-moRestartPerturb t-moNeighborhoodPerturb - t-moSAexplorer + t-moSAExplorer t-moSA t-moLocalSearch t-moILSexplorer @@ -65,7 +65,7 @@ set (TEST_LIST t-moILS t-moDummyLS t-moRandomSearch - t-moMetropolisHasting + t-moMetropolisHastings t-moNeutralHC t-moRandomWalk t-moRandomNeutralWalk diff --git a/mo/test/t-moMetropolisHasting.cpp b/mo/test/t-moMetropolisHasting.cpp deleted file mode 100644 index 638c64bd8..000000000 --- a/mo/test/t-moMetropolisHasting.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - -Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - -Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau - -This software is governed by the CeCILL license under French law and -abiding by the rules of distribution of free software. You can ue, -modify and/ or redistribute the software under the terms of the CeCILL -license as circulated by CEA, CNRS and INRIA at the following URL -"http://www.cecill.info". - -In this respect, the user's attention is drawn to the risks associated -with loading, using, modifying and/or developing or reproducing the -software by the user in light of its specific status of free software, -that may mean that it is complicated to manipulate, and that also -therefore means that it is reserved for developers and experienced -professionals having in-depth computer knowledge. Users are therefore -encouraged to load and test the software's suitability as regards their -requirements in conditions enabling the security of their systems and/or -data to be ensured and, more generally, to use and operate it in the -same conditions as regards security. -The fact that you are presently reading this means that you have had -knowledge of the CeCILL license and that you accept its terms. - -ParadisEO WebSite : http://paradiseo.gforge.inria.fr -Contact: paradiseo-help@lists.gforge.inria.fr -*/ - -#include -#include -#include - -#include -#include "moTestClass.h" -#include -#include -#include -#include - -int main() { - - std::cout << "[t-moMetropolisHasting] => START" << std::endl; - - bitNeighborhood nh(4); - oneMaxEval fullEval; - evalOneMax eval(4); - moTrueContinuator cont; - moSolNeighborComparator sncomp; - moNeighborComparator ncomp; - - //test du 1er constructeur - moMetropolisHasting test1(nh, fullEval, eval, 3); - - //test du 2eme constructeur - moMetropolisHasting test2(nh, fullEval, eval, 3, cont); - - //test du 3eme constructeur - moMetropolisHasting test3(nh, fullEval, eval, 3, cont, ncomp, sncomp); - - std::cout << "[t-moMetropolisHasting] => OK" << std::endl; - - return EXIT_SUCCESS; -} - diff --git a/mo/test/t-moMetropolisHastingExplorer.cpp b/mo/test/t-moMetropolisHastingExplorer.cpp deleted file mode 100644 index 6214465a1..000000000 --- a/mo/test/t-moMetropolisHastingExplorer.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* - -Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - -Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau - -This software is governed by the CeCILL license under French law and -abiding by the rules of distribution of free software. You can ue, -modify and/ or redistribute the software under the terms of the CeCILL -license as circulated by CEA, CNRS and INRIA at the following URL -"http://www.cecill.info". - -In this respect, the user's attention is drawn to the risks associated -with loading, using, modifying and/or developing or reproducing the -software by the user in light of its specific status of free software, -that may mean that it is complicated to manipulate, and that also -therefore means that it is reserved for developers and experienced -professionals having in-depth computer knowledge. Users are therefore -encouraged to load and test the software's suitability as regards their -requirements in conditions enabling the security of their systems and/or -data to be ensured and, more generally, to use and operate it in the -same conditions as regards security. -The fact that you are presently reading this means that you have had -knowledge of the CeCILL license and that you accept its terms. - -ParadisEO WebSite : http://paradiseo.gforge.inria.fr -Contact: paradiseo-help@lists.gforge.inria.fr -*/ - -#include -#include "moTestClass.h" - -#include -#include -#include - -int main() { - - std::cout << "[t-moMetropolisHastingExplorer] => START" << std::endl; - - //Instanciation - eoBit sol(4, true); - sol.fitness(4); - bitNeighborhood nh(4); - evalOneMax eval(4); - moNeighborComparator ncomp; - moSolNeighborComparator sncomp; - - moMetropolisHastingExplorer test(nh, eval, ncomp, sncomp, 3); - - //test de l'acceptation d'un voisin améliorant - test.initParam(sol); - test(sol); - assert(test.accept(sol)); - test.move(sol); - assert(sol.fitness()==3); - test.updateParam(sol); - assert(test.isContinue(sol)); - - unsigned int oui=0, non=0; - - //test de l'acceptation d'un voisin non améliorant - for (unsigned int i=0; i<1000; i++) { - test(sol); - if (test.accept(sol)) - oui++; - else - non++; - } - std::cout << "Attention test en fonction d'une proba \"p\" uniforme dans [0,1] , oui si p < 3/4, non sinon -> resultat sur 1000 essai" << std::endl; - std::cout << "oui: " << oui << std::endl; - std::cout << "non: " << non << std::endl; - - assert(oui > 700 && oui < 800); //verification grossiere - - //test du critere d'arret - test.updateParam(sol); - assert(test.isContinue(sol)); - test.updateParam(sol); - assert(!test.isContinue(sol)); - - //test de l'acceptation d'un voisin - sol[0]=false; - sol[1]=false; - sol[2]=false; - sol[3]=false; - sol.fitness(0); - - test.initParam(sol); - test(sol); - assert(!test.accept(sol)); - - std::cout << "[t-moMetropolisHastingExplorer] => OK" << std::endl; - - return EXIT_SUCCESS; -} - diff --git a/mo/test/t-moSAexplorer.cpp b/mo/test/t-moSAexplorer.cpp deleted file mode 100644 index 4c619ff88..000000000 --- a/mo/test/t-moSAexplorer.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - -Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 - -Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau - -This software is governed by the CeCILL license under French law and -abiding by the rules of distribution of free software. You can ue, -modify and/ or redistribute the software under the terms of the CeCILL -license as circulated by CEA, CNRS and INRIA at the following URL -"http://www.cecill.info". - -In this respect, the user's attention is drawn to the risks associated -with loading, using, modifying and/or developing or reproducing the -software by the user in light of its specific status of free software, -that may mean that it is complicated to manipulate, and that also -therefore means that it is reserved for developers and experienced -professionals having in-depth computer knowledge. Users are therefore -encouraged to load and test the software's suitability as regards their -requirements in conditions enabling the security of their systems and/or -data to be ensured and, more generally, to use and operate it in the -same conditions as regards security. -The fact that you are presently reading this means that you have had -knowledge of the CeCILL license and that you accept its terms. - -ParadisEO WebSite : http://paradiseo.gforge.inria.fr -Contact: paradiseo-help@lists.gforge.inria.fr -*/ - -#include -#include -#include - -#include "moTestClass.h" -#include -#include - -int main() { - - std::cout << "[t-moSAexplorer] => START" << std::endl; - - eoBit sol(4, true); - sol.fitness(4); - bitNeighborhood nh(4); - bitNeighborhood emptyNH(0); - evalOneMax eval(4); - moSolNeighborComparator sncomp; - moSimpleCoolingSchedule cool(10,0.1,2,0.1); - - moSAexplorer test1(emptyNH, eval, sncomp, cool); - moSAexplorer test2(nh, eval, sncomp, cool); - - //test d'un voisinage vide - test1.initParam(sol); - test1(sol); - assert(!test1.accept(sol)); - assert(test1.getTemperature()==10.0); - - //test d'un voisinage "normal" - test2.initParam(sol); - test2(sol); - assert(test2.accept(sol)); - test2.updateParam(sol); - assert(test2.isContinue(sol)); - test2.move(sol); - assert(sol.fitness()==3); - unsigned int ok=0; - unsigned int ko=0; - for (unsigned int i=0; i<1000; i++) { - test2(sol); - if (test2.isContinue(sol)) - test2.updateParam(sol); - if (test2.accept(sol)) - ok++; - else - ko++; - test2.move(sol); - } - assert((ok>0) && (ko>0)); - - - - std::cout << "[t-moSAexplorer] => OK" << std::endl; - - return EXIT_SUCCESS; -} - From 816ea1553aa2a43d3508ef305ae4ba19f53794a8 Mon Sep 17 00:00:00 2001 From: LPTK Date: Tue, 9 Jul 2013 16:25:30 +0200 Subject: [PATCH 33/36] intermediate commit 5 --- eo/src/utils/eoFileMonitor.cpp | 18 +- eo/src/utils/eoMonitor.h | 1 + eo/src/utils/eoParam.h | 4 +- mo/src/algo/moLocalSearch.h | 24 +- mo/src/algo/moSA.h | 15 +- mo/src/continuator/moCheckpoint.h | 15 +- .../moDynSpanCoolingSchedule.h | 38 +- .../coolingSchedule/moTrikiCoolingSchedule.h | 393 +++++++++++++----- mo/src/mo.h | 2 +- mo/test/t-moSA.cpp | 4 +- problems/eval/queenEval.h | 20 +- 11 files changed, 377 insertions(+), 157 deletions(-) diff --git a/eo/src/utils/eoFileMonitor.cpp b/eo/src/utils/eoFileMonitor.cpp index 3cc96deb1..4927fe2ab 100644 --- a/eo/src/utils/eoFileMonitor.cpp +++ b/eo/src/utils/eoFileMonitor.cpp @@ -73,16 +73,18 @@ eoMonitor& eoFileMonitor::operator()(void) eoMonitor& eoFileMonitor::operator()(std::ostream& os) { - - iterator it = vec.begin(); - - os << (*it)->getValue(); - - for(++it; it != vec.end(); ++it) + if (vec.size() > 0) { - os << delim.c_str() << (*it)->getValue(); + iterator it = vec.begin(); + + os << (*it)->getValue(); + + for(++it; it != vec.end(); ++it) + { + os << delim.c_str() << (*it)->getValue(); + } } - + os << std::endl; return *this; diff --git a/eo/src/utils/eoMonitor.h b/eo/src/utils/eoMonitor.h index 1c92e9c00..d234921c4 100644 --- a/eo/src/utils/eoMonitor.h +++ b/eo/src/utils/eoMonitor.h @@ -78,3 +78,4 @@ protected : }; #endif + diff --git a/eo/src/utils/eoParam.h b/eo/src/utils/eoParam.h index 948092c93..1c0937528 100644 --- a/eo/src/utils/eoParam.h +++ b/eo/src/utils/eoParam.h @@ -224,7 +224,9 @@ public : void setValue(const std::string& _value) { std::istringstream is(_value); - is >> repValue; + bool read = (is >> repValue); + if (!read) + throw std::runtime_error("Could not read value passed in eoValueParam::setValue"); } protected: diff --git a/mo/src/algo/moLocalSearch.h b/mo/src/algo/moLocalSearch.h index 0c125d62a..ca2f04c6e 100644 --- a/mo/src/algo/moLocalSearch.h +++ b/mo/src/algo/moLocalSearch.h @@ -57,9 +57,8 @@ public: * @param _cont an external continuator (can be a checkpoint!) * @param _fullEval a full evaluation function */ - moLocalSearch(NeighborhoodExplorer& _searchExpl, - moContinuator & _cont, eoEvalFunc& _fullEval) - : searchExplorer(_searchExpl), cont(&_cont), fullEval(_fullEval) + moLocalSearch(NeighborhoodExplorer& _searchExpl, moContinuator & _cont, eoEvalFunc& _fullEval) + : searchExplorer(_searchExpl), cont(&_cont), fullEval(_fullEval), currentSolutionFitness(0) { } /** @@ -70,15 +69,19 @@ public: if (_solution.invalid()) fullEval(_solution); - + // initialization of the parameter of the search (for example fill empty the tabu list) searchExplorer.initParam(_solution); - + // initialization of the external continuator (for example the time, or the number of generations) cont->init(_solution); - + bool b; do { + currentSolutionFitness = _solution.fitness(); + //std::cout << currentSolutionFitness << std::endl; + //std::cin.get(); + // explore the neighborhood of the solution searchExplorer(_solution); // if a solution in the neighborhood can be accepted @@ -128,6 +131,11 @@ public: moNeighborhoodExplorer & getNeighborhoodExplorer() const { return searchExplorer; } + + // TODO doc + double getCurrentSolutionFitness() const { + return currentSolutionFitness; + } protected: // make the exploration of the neighborhood according to a local search heuristic @@ -138,6 +146,10 @@ protected: //full evaluation function eoEvalFunc& fullEval; + +private: + double currentSolutionFitness; + }; #endif diff --git a/mo/src/algo/moSA.h b/mo/src/algo/moSA.h index 0914f0db0..4cf1b3fba 100644 --- a/mo/src/algo/moSA.h +++ b/mo/src/algo/moSA.h @@ -68,7 +68,7 @@ public: { }*/ moSA(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, double _initT=10, double _alpha=0.9, unsigned _span=100, double _finalT=0.01) : moLocalSearch ( - explorer_ptr = defaultExplorer = new moSAExplorer(_neighborhood, _eval, /*NULL,*/ *(defaultCool = new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)), NULL), + *(explorer_ptr = defaultExplorer = new moSAExplorer(_neighborhood, _eval, /*NULL,*/ *(defaultCool = new moSimpleCoolingSchedule(_initT, _alpha, _span, _finalT)), NULL)), *(defaultContinuator = new moTrueContinuator()), _fullEval ), explorer(*explorer_ptr), @@ -150,7 +150,7 @@ public: _cool ) { }*/ : moLocalSearch ( - explorer_ptr = defaultExplorer = new moSAExplorer ( + *(explorer_ptr = defaultExplorer = new moSAExplorer ( _neighborhood, //_eval, //_eval.hasValue()? _eval.get(): *(defaultEval = new moFullEvalByCopy(_fullEval)), _eval.hasValue()? defaultEval = NULL, _eval.get(): *(defaultEval = new moFullEvalByCopy(_fullEval)), @@ -158,7 +158,7 @@ public: //_comp, //_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator()), //_cool ), _cool, - _comp ), + _comp )), // ), _cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval ), @@ -177,12 +177,13 @@ public: ) : moLocalSearch ( *(explorer_ptr = &_explorer), - _cont.hasValue()? _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval ), + _cont.hasValue()? defaultContinuator = NULL, _cont.get(): *(defaultContinuator = new moTrueContinuator()), _fullEval ), + defaultEval(NULL), // removed in C++11 with unique_ptr defaultExplorer(NULL), // removed in C++11 with unique_ptr explorer(*explorer_ptr), - defaultCool(NULL), // removed in C++11 with unique_ptr - //defaultEval(NULL), // removed in C++11 with unique_ptr - defaultContinuator(NULL) // removed in C++11 with unique_ptr + defaultCool(NULL) // removed in C++11 with unique_ptr + //defaultEval(NULL) // removed in C++11 with unique_ptr + //defaultContinuator(NULL) // removed in C++11 with unique_ptr //defaultSolNeighborComp(NULL) // removed in C++11 with unique_ptr { } diff --git a/mo/src/continuator/moCheckpoint.h b/mo/src/continuator/moCheckpoint.h index 37b5cd610..6cc3d0fb5 100644 --- a/mo/src/continuator/moCheckpoint.h +++ b/mo/src/continuator/moCheckpoint.h @@ -107,14 +107,21 @@ public : virtual void init(EOT& _sol) { for (unsigned i = 0; i < stats.size(); ++i) stats[i]->init(_sol); - counter=1; - + counter = 1; + + //for (unsigned i = 0; i < updaters.size(); ++i) + // updaters[i]->init(); + for (unsigned i = 0; i < moupdaters.size(); ++i) moupdaters[i]->init(); - + /* + * Removed because there was no reason for it to be done here. + * It caused premature monitoring of eoParams with undefined values + * for (unsigned int i = 0; i < monitors.size(); ++i) (*monitors[i])(); - + */ + for (unsigned i = 0; i < continuators.size(); ++i) continuators[i]->init(_sol); } diff --git a/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h b/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h index 4e6216ad5..5ec3a886c 100644 --- a/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h +++ b/mo/src/coolingSchedule/moDynSpanCoolingSchedule.h @@ -82,25 +82,25 @@ public: * @param _acceptedMove true when the move is accepted, false otherwise * @param _currentSolution the current solution */ - virtual void update(double& _temp, bool _acceptedMove, EOT & _currentSolution) { - spanTries++; - - if (_acceptedMove) - spanMove++; - - if (spanTries >= spanTriesMax || spanMove >= spanMoveMax) { - _temp *= alpha; - - if (spanMove == 0) // no move during this span ? - nbSpan++; - else - nbSpan = 0; - - spanTries = 0; - spanMove = 0; - } - } - + virtual void update(double& _temp, bool _acceptedMove, EOT & _currentSolution) { + spanTries++; + + if (_acceptedMove) + spanMove++; + + if (spanTries >= spanTriesMax || spanMove >= spanMoveMax) { + _temp *= alpha; + + if (spanMove == 0) // no move during this span ? + nbSpan++; + else + nbSpan = 0; + + spanTries = 0; + spanMove = 0; + } + } + /** * compare the number of span with no move * @param _temp current temperature diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index 900e3b189..4bb09d649 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -47,22 +47,28 @@ Lionel Parreaux #include using namespace std; +/* +static const char * stoppingReasons[] = {"no accepted solution", "null std dev" , "frozen >= theta"}; +static const char * chainEndingReasons[] = {"MAX GENerated solutions", "MAX ACCepted solutions"}; +*/ //! /*! */ -template< class EOT, class Neighbor > //, class Neighborhood > +//template< class Neighbor > //, class Neighborhood > +//class moTrikiCoolingSchedule: public moCoolingSchedule< typename Neighbor::EOT > +template< class EOT > class moTrikiCoolingSchedule: public moCoolingSchedule< EOT > { public: //typedef typename Neighbor::EOT EOT ; - typedef moNeighborhood Neighborhood ; + //typedef moNeighborhood Neighborhood ; //! Constructor /*! */ - - moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval& _eval, double _initTemp) + /* + moTrikiCoolingSchedule (Neighborhood& _neighborhood, moEval& _eval, double _initTemp) // FIXME rem useless params!! : initTemp(_initTemp), mu2(10), // mu2 typically belongs to [1; 20] K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature @@ -74,29 +80,59 @@ public: max_accepted(50), // depends on pb/neighborhood max_generated(100), // depends on pb/neighborhood theta(10), // theta is typically set to 10 - statIsInitialized(false), - outf("out.data") + statIsInitialized(false)//, + //outf("out.data") { } +*/ + +// moTrikiCoolingSchedule ( +// Neighborhood& _neighborhood, moEval& _eval, double _initTemp, +// double _max_accepted, +// double _max_generated +// ) +// : initTemp(_initTemp), +// mu2(10), // mu2 typically belongs to [1; 20] +// K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature +// K2(5), // ??? +// lambda1(2), // the increase in temperature, typically in [1.5; 4] +// lambda2(.7), // lambda2 in [0.5; 0.99] +// mu1(10), // target decrease in cost factor, in [2; 20] +// xi(1.05), // xi typically belongs to [1; 1.1] +// max_accepted(_max_accepted), // depends on pb/neighborhood +// max_generated(_max_generated), // depends on pb/neighborhood +// theta(10), // theta is typically set to 10 +// statIsInitialized(false)//, +// //outf("out.data") +// { } moTrikiCoolingSchedule ( - Neighborhood& _neighborhood, moEval& _eval, double _initTemp, - double _max_accepted, - double _max_generated - ) + double _initTemp, + int _max_accepted = 50, + int _max_generated = 100, + double _mu2 = 2, + double _mu1 = 10, + double _lambda1 = 2, + double _lambda2 = .7, + double _xi = 1.05, + int _theta = 10, + int _K1 = 10, + int _K2 = 5 + ) // TODO reorder inits : initTemp(_initTemp), - mu2(10), // mu2 typically belongs to [1; 20] - K1(2), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature - K2(5), // ??? - lambda1(2), // the increase in temperature, typically in [1.5; 4] - lambda2(.7), // lambda2 in [0.5; 0.99] - mu1(10), // target decrease in cost factor, in [2; 20] - xi(1.05), // xi typically belongs to [1; 1.1] + mu2(_mu2), // mu2 typically belongs to [1; 20] + K1(_K1), // K1 in [1; 4], the number of chains without reaching equilibrium before we raise the temperature + K2(_K2), // ??? + lambda1(_lambda1), // the increase in temperature (reheating factor), typically in [1.5; 4] + lambda2(_lambda2), // lambda2 in [0.5; 0.99] + mu1(_mu1), // target decrease in cost factor, in [2; 20] + xi(_xi), // xi typically belongs to [1; 1.1] max_accepted(_max_accepted), // depends on pb/neighborhood max_generated(_max_generated), // depends on pb/neighborhood - theta(10), // theta is typically set to 10 - statIsInitialized(false), - outf("out.data") - { } + theta(_theta), // theta is typically set to 10 + statIsInitialized(false) + { + chainStat.temperature = initTemp; + } /** * Initial temperature @@ -104,16 +140,25 @@ public: */ double init(EOT & _solution) { - accepted = generated = costs_sum = 0; + accepted = generated = 0;// = costs_sum = 0; negative_temp = equilibrium_not_reached = frozen = 0; + //cout << "acc " << max_accepted << " " << max_generated << endl; + + /* reinitializing = false; terminated = false; statIsInitialized = false; + */ + reinitializing = false; + //cout << "INIT T=" << initTemp << endl; + //cout << "INIT T=" << chainStat.temperature << endl; + //chainStat.temperature = initTemp; + /// - cout << "INIT T=" << initTemp << endl; + //cout << "INIT T=" << initTemp << endl; /// //outf.open("out"); @@ -132,9 +177,18 @@ public: void update(double& _temp, bool _acceptedMove, EOT & _solution) { //cout << _temp << " g " << generated << endl; - prevTemp = _temp; + chainStat.temperature = _temp; + chainStat.stoppingReason = NULL; + chainStat.chainEndingReason = NULL; + chainStat.equilibriumNotReached = false; + chainStat.negativeTemp = false; + //chainStat.markovChainEnded = false; + chainStat.generatedSolutions = generated; + chainStat.acceptedSolutions = accepted; generated++; + cout << "gen " << generated << endl; + if (_acceptedMove) { @@ -145,78 +199,83 @@ public: momentStat(_solution); else momentStat.init(_solution), statIsInitialized = true; - //cout << _solution.fitness() << " avgCost=" << momentStat.value().first << endl; + //cout << _solution.fitness() << " avgFitness=" << momentStat.value().first << endl; } - markovChainEnded = false; - if (accepted > max_accepted || generated > max_generated) { + + chainStat.chainEndingReason = accepted > max_accepted ? chainEndingReasons[0]: chainEndingReasons[1]; - markovChainEnded = true; + //chainStat.markovChainEnded = true; if (accepted == 0) // ADDED! Otherwise the computed std dev is null; we're probably at equilibrium { /// - cout << "Stopping: no accepted solution" << endl; + //cout << "Stopping: no accepted solution" << endl; /// - terminated = true; - return; + chainStat.terminated = true, chainStat.stoppingReason = stoppingReasons[0]; + return; // FIXME nutgud } /// - cout << (accepted > max_accepted? "MAXACC ": "MAXGEN "); + //cout << (accepted > max_accepted? "MAXACC ": "MAXGEN "); /// - //double avgCost = costs_sum/(double)accepted; + //double avgFitness = costs_sum/(double)accepted; //double stdDev = sqrt(varStat.value()); // WARNING: IT'S NO MORE THE AVG COST, NOW IT'S THE STD DEV! //double variance = varStat.value(); - double avgCost = momentStat.value().first; + double avgFitness = momentStat.value().first; double variance = momentStat.value().second; - double stdDev = sqrt(variance); - double sigma = stdDev; + //double stdDev = sqrt(variance); + chainStat.stdDev = sqrt(variance); + double sigma = chainStat.stdDev; double delta = sigma/mu2; - //outf << avgCost << endl; - outf << _temp << endl; - //outf << prevAvgCost-delta << endl; + //outf << avgFitness << endl; + //outf << _temp << endl; + //outf << prevAvgFitness-delta << endl; - accepted = generated = costs_sum = 0; + accepted = generated = 0; // = costs_sum = 0; //momentStat.init(_solution);//TODONE use next chain's first sol statIsInitialized = false; /// - cout << "T=" << _temp << " avgCost=" << avgCost << " stdDev=" << stdDev << " currCost=" << _solution.fitness() << endl; + //cout << "T=" << _temp << " avgFitness=" << avgFitness << " stdDev=" << chainStat.stdDev << " currCost=" << _solution.fitness() << endl; /// - double alpha; - double oldprevAvgCost = prevAvgCost; + double alpha = 0; + double prevAvgFitness = chainStat.avgFitness; /// - cout << "negTemp: " << negative_temp << " / " << K2 << endl; + //cout << "negTemp: " << negative_temp << " / " << K2 << endl; /// if (negative_temp < K2) { + //if (!chainStat.reinitializing) if (!reinitializing) { /// - if (avgCost/(prevAvgCost-delta) > xi) cout << "/!\\ eq not reached!" << endl; + //if (avgFitness/(chainStat.prevAvgFitness-delta) > xi) cout << "/!\\ eq not reached!" << endl; /// - if (avgCost/(prevAvgCost-delta) > xi) + if (avgFitness/(prevAvgFitness-delta) > xi) equilibrium_not_reached++; else equilibrium_not_reached = 0; } if (equilibrium_not_reached > K1) { /// - cout << "/!\\ Reinitializing (eq not reached)" << endl; + //cout << "/!\\ Reinitializing (eq not reached)" << endl; /// - + + //chainStat.reinitializing = true; reinitializing = true; + chainStat.equilibriumNotReached = true; + alpha = lambda1; delta = sigma/mu1; equilibrium_not_reached = 0; // ADDED! Otherwise the algo gets trapped here! @@ -224,11 +283,13 @@ public: else if (_temp*delta/(sigma*sigma) >= 1) { /// - cout << "/!\\ neg temp!" << endl; + //cout << "/!\\ neg temp!" << endl; /// negative_temp++; reinitializing = true; + chainStat.negativeTemp = true; + if (negative_temp < K2) { alpha = lambda1; @@ -244,7 +305,7 @@ public: { cout << "ccc" << endl; reinitializing = false; - prevAvgCost = avgCost; + prevAvgFitness = avgFitness; alpha = 1-_temp*delta/(sigma*sigma); } */ @@ -253,37 +314,42 @@ public: else { /// - cout << "[normal decrease]" << endl; + //cout << "[normal decrease]" << endl; /// reinitializing = false; - prevAvgCost = avgCost; + //chainStat.avgFitness = avgFitness; //alpha = 1-_temp*delta/(sigma*sigma); alpha = 1-_temp*delta/variance; //alpha = (sigma==0? 1: 1-_temp*delta/(sigma*sigma)); // ADDED! but removed if (sigma == 0) // ADDED! When std dev is null, the solution is probably at eq, and the algo can't go on anyways - terminated = true, cout << "Stopping: null std dev" << endl; + chainStat.terminated = true, chainStat.stoppingReason = stoppingReasons[1]; //, cout << "Stopping: null std dev" << endl; } } // FIXME: else what? alpha=? - - /// - cout << "*=" << alpha << endl; + //cout << "*=" << alpha << endl; /// _temp *= alpha; + + chainStat.currentFitness = _solution.fitness(); // FIXME here? + chainStat.alpha = alpha; + chainStat.delta = delta; + chainStat.avgFitness = avgFitness; // Never seems to be used - if (avgCost == oldprevAvgCost) // use a neighborhood to approximate double equality? + if (avgFitness == prevAvgFitness) // use a neighborhood to approximate double equality? frozen++; else frozen = 0; + if (frozen >= theta) + chainStat.terminated = true, chainStat.stoppingReason = stoppingReasons[2];; //exit(0); //cin.get(); @@ -299,26 +365,134 @@ public: bool operator() (double temperature) { /// - if (terminated) cout << "TERMINATED" << endl; + //if (chainStat.terminated) cout << "TERMINATED" << endl; /// return frozen < theta - && !terminated ; // ADDED! because 'frozen' doesn't terminate anything + && !chainStat.terminated ; // ADDED! because 'frozen' doesn't terminate anything + + + return !chainStat.terminated ; // ADDED! because 'frozen' doesn't terminate anything + } - - bool markovChainJustEnded() + /* + bool markovChainJustEnded() const { return markovChainEnded; - } + }*/ + + + // Code for generating the getters: +/* +#define __triki_getterType double +#define __triki_makeGetter(name) __triki_getterType name() { return chainStat.name; } + + __triki_makeGetter(stdDev) + __triki_makeGetter(avgFitness) + __triki_makeGetter(prevTemp) + __triki_makeGetter(currentFitness) + __triki_makeGetter(alpha) - MarkovChainStats& getMarkovChainStats() +#undef __triki_getterType +#define __triki_getterType int + + __triki_makeGetter(generatedSolutions) + __triki_makeGetter(acceptedSolutions) + +#undef __triki_getterType +#define __triki_getterType const char * + + __triki_makeGetter(stoppingReason) + __triki_makeGetter(chainEndingReason) + +#undef __triki_getterType +#undef __triki_makeGetter + */ + +#define __triki_makeGetter(name, type) type name() { return chainStat.name; } + + __triki_makeGetter(stdDev, double) + __triki_makeGetter(avgFitness, double) + __triki_makeGetter(temperature, double) + __triki_makeGetter(currentFitness, double) + __triki_makeGetter(alpha, double) + __triki_makeGetter(delta, double) + + __triki_makeGetter(generatedSolutions, int) + __triki_makeGetter(acceptedSolutions, int) + + __triki_makeGetter(equilibriumNotReached, bool) + __triki_makeGetter(negativeTemp, bool) + __triki_makeGetter(terminated, bool) + + __triki_makeGetter(stoppingReason, const char *) + __triki_makeGetter(chainEndingReason, const char *) + +#undef __triki_makeGetter + + + const bool markovChainEnded() const { - return markovChainStats; + return chainStat.chainEndingReason != NULL; } + struct MarkovChainStats; + const MarkovChainStats& markovChainStats() const + { + return chainStat; + } - + struct MarkovChainStats { + + MarkovChainStats() + : stdDev(0), avgFitness(0), temperature(-1), currentFitness(-1), alpha(0), delta(0), + //equilibriumNotReached(false), negativeTemp(false), terminated(false), markovChainEnded(false) + equilibriumNotReached(false), negativeTemp(false), terminated(false) + { } + + double + stdDev, + avgFitness, + //prevAvgFitness, + //expectedDecreaseInCost, // delta + //costs_sum, + temperature, + currentFitness, + alpha, + delta + ; + int generatedSolutions, acceptedSolutions; + //bool reinitializing, terminated, markovChainEnded; + bool equilibriumNotReached, negativeTemp, terminated; //, markovChainEnded; + const char * stoppingReason; + const char * chainEndingReason; // if NULL, the chain has not ended + //EOT& currentSolution; + //moTrikiCoolingSchedule& coolingSchedule; + void print(std::ostream& os = std::cout, bool onlyWhenChainEnds = true) const + { + //if (markovChainEnded || !onlyWhenChainEnds) + if (chainEndingReason != NULL || !onlyWhenChainEnds) + { + //os << "T=" << prevTemp << " avgFitness=" << prevAvgFitness << " stdDev=" << stdDev << " currentFitness=" << currentSolution.fitness() << endl; + os << "T=" << temperature << " avgFitness=" << avgFitness << " stdDev=" << stdDev << " currentFitness=" << currentFitness << " expected decrease in cost=" << delta << endl; + //os << "T=" << prevTemp << " \t\tavgFitness=" << prevAvgFitness << " \t\tstdDev=" << stdDev << " \t\tcurrentFitness=" << currentFitness << endl; + //os << "T*=" << alpha << " reinitializing=" << reinitializing << " markovChainEnded=" << markovChainEnded << endl;// << " terminated=" << terminated; + //os << "T*=" << alpha << " markovChainEnded=" << markovChainEnded;// << " terminated=" << terminated; + // TODONE print delta ? (expected decrease in cost) + if (chainEndingReason != NULL) + os << "T*=" << alpha << " chain ended, because " << chainEndingReason; + if (equilibriumNotReached) + os << " /!\\ equilibrium not reached"; + if (negativeTemp) + os << " /!\\ negative temperature"; + os << endl; + if (terminated) + os << "Terminated, because " << stoppingReason << endl; + // os << endl; + } + } + }; private: @@ -340,13 +514,17 @@ private: xi // xi typically belongs to [1; 1.1] // private variables ; + /* double stdDev, - prevAvgCost, + prevAvgFitness, expectedDecreaseInCost, // delta costs_sum, prevTemp - ; + ;*/ + MarkovChainStats chainStat; + //double costs_sum; + const int max_accepted, max_generated, @@ -359,48 +537,65 @@ private: negative_temp, frozen ; - bool reinitializing, terminated, markovChainEnded; + //bool reinitializing, terminated, markovChainEnded; //moFitnessVarianceStat varStat; moFitnessMomentsStat momentStat; - bool statIsInitialized; - - ofstream outf; + bool statIsInitialized, reinitializing; + //ofstream outf; + /* + static const char * stoppingReasons[] = {"no accepted solution", "null std dev" , "frozen >= theta"}; + static const char * chainEndingReasons[] = {"MAX GENerated solutions", "MAX ACCepted solutions"}; + */ + static const char * stoppingReasons[]; + static const char * chainEndingReasons[]; protected: - class Monitor { - public: - - Monitor(moTrikiCoolingSchedule& _cooling) - : cooling(_cooling) - { } - - /*void setTemperatureOstream(ostream& os) - { - - }*/ - void getLatestTemperature() - { - return cooling.prevTemp; - } - - void printCurrentStatus(ostream& os) - { - if (accepted >= max_accepted || generated >= max_generated) - { - os << "Markov chain finished. Temp was " << getLatestTemperature(); // chain number - - } - } - - private: - moTrikiCoolingSchedule& cooling; - }; +// class Monitor { +// public: +// +// Monitor(moTrikiCoolingSchedule& _cooling) +// : cooling(_cooling) +// { } +// +// /*void setTemperatureOstream(ostream& os) +// { +// +// }*/ +// void getLatestTemperature() +// { +// return cooling.prevTemp; +// } +// +// void printCurrentStatus(ostream& os) +// { +// if (accepted >= max_accepted || generated >= max_generated) +// { +// os << "Markov chain finished. Temp was " << getLatestTemperature(); // chain number +// +// } +// } +// +// private: +// moTrikiCoolingSchedule& cooling; +// }; }; + + +template< class Neighbor > +const char * moTrikiCoolingSchedule::stoppingReasons[] = {"no accepted solution", "null std dev" , "frozen >= theta"}; + +template< class Neighbor > +//const char * moTrikiCoolingSchedule::chainEndingReasons[] = {"MAX GENerated solutions", "MAX ACCepted solutions"}; +const char * moTrikiCoolingSchedule::chainEndingReasons[] = {"MAX ACCepted solutions", "MAX GENerated solutions"}; + + + + #endif diff --git a/mo/src/mo.h b/mo/src/mo.h index ef83384be..922a2c01e 100755 --- a/mo/src/mo.h +++ b/mo/src/mo.h @@ -106,6 +106,7 @@ #include #include #include +#include #include #include @@ -113,7 +114,6 @@ #include #include #include -#include #include #include diff --git a/mo/test/t-moSA.cpp b/mo/test/t-moSA.cpp index b1cdfeb67..3c67865cb 100644 --- a/mo/test/t-moSA.cpp +++ b/mo/test/t-moSA.cpp @@ -51,12 +51,12 @@ int main() { //test second constructor moSimpleCoolingSchedule cool(10, 0.9, 100, 0.01); - moSA test2(nh, fullEval, eval, cool); + moSA test2(nh, fullEval, cool, eval); //test third constructor moTrueContinuator cont; moSolNeighborComparator comp; - moSA test3(nh, fullEval, eval, cool, comp, cont); + moSA test3(nh, fullEval, cool, eval, cont, comp); std::cout << "[t-moSA] => OK" << std::endl; diff --git a/problems/eval/queenEval.h b/problems/eval/queenEval.h index da8dd9ee3..daaed9220 100644 --- a/problems/eval/queenEval.h +++ b/problems/eval/queenEval.h @@ -40,17 +40,17 @@ class queenEval : public eoEvalFunc { public: - /** - * Count number of threat - * @param _queen a solution - */ + /** + * Count number of threat + * @param _queen a solution + */ void operator()(EOT& _queen){ - unsigned int fit=0; - for(unsigned int i=0; i<_queen.size()-1; i++) - for(unsigned int j=i+1; j< _queen.size(); j++) - if(((unsigned int)_queen[i]+j-i == (unsigned int)_queen[j]) || ((unsigned int)_queen[i]+i-j == (unsigned int)_queen[j])) - fit++; - _queen.fitness(fit); + unsigned int fit=0; + for(unsigned int i=0; i<_queen.size()-1; i++) + for(unsigned int j=i+1; j< _queen.size(); j++) + if(((unsigned int)_queen[i]+j-i == (unsigned int)_queen[j]) || ((unsigned int)_queen[i]+i-j == (unsigned int)_queen[j])) + fit++; + _queen.fitness(fit); } }; From 483f815610d67b2090236f609d3fd2f19e716686 Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 10 Jul 2013 17:49:08 +0200 Subject: [PATCH 34/36] intermediate commit 6 --- eo/NEWS | 3 ++- mo/src/continuator/moCheckpoint.h | 10 ++++++---- mo/src/coolingSchedule/moTrikiCoolingSchedule.h | 6 +++--- mo/src/neighborhood/moRealNeighborhood.h | 1 - 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/eo/NEWS b/eo/NEWS index 4161ff613..a90d996d3 100644 --- a/eo/NEWS +++ b/eo/NEWS @@ -1,5 +1,6 @@ * current release: - - added an EOT& parameter to the moCoolingSchedule::update interface because some cooling schedules need it + - added an EOT& parameter to the moCoolingSchedule::update interface method because some cooling schedules need it + - eoMonitor's are no more executed in moCheckpoint::init to prevent access to uninitialized data * release 1.3.1 (2012-07-27) - the eo::mpi modules is no longer dependent from boost::mpi diff --git a/mo/src/continuator/moCheckpoint.h b/mo/src/continuator/moCheckpoint.h index 6cc3d0fb5..950b96fa0 100644 --- a/mo/src/continuator/moCheckpoint.h +++ b/mo/src/continuator/moCheckpoint.h @@ -109,14 +109,16 @@ public : stats[i]->init(_sol); counter = 1; + for (unsigned i = 0; i < moupdaters.size(); ++i) + moupdaters[i]->init(); + //for (unsigned i = 0; i < updaters.size(); ++i) // updaters[i]->init(); - for (unsigned i = 0; i < moupdaters.size(); ++i) - moupdaters[i]->init(); /* - * Removed because there was no reason for it to be done here. - * It caused premature monitoring of eoParams with undefined values + * Removed because there was no reason for it to be done here + * It caused premature monitoring of eoParams with uninitialized values + * (eoUpdater's don't have a init function) * for (unsigned int i = 0; i < monitors.size(); ++i) (*monitors[i])(); diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index 4bb09d649..eb078120a 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -187,7 +187,7 @@ public: chainStat.acceptedSolutions = accepted; generated++; - cout << "gen " << generated << endl; + //cout << "gen " << generated << endl; if (_acceptedMove) @@ -263,7 +263,7 @@ public: /// if (avgFitness/(prevAvgFitness-delta) > xi) - equilibrium_not_reached++; + equilibrium_not_reached++, chainStat.equilibriumNotReached = true; else equilibrium_not_reached = 0; } if (equilibrium_not_reached > K1) @@ -274,7 +274,7 @@ public: //chainStat.reinitializing = true; reinitializing = true; - chainStat.equilibriumNotReached = true; + //chainStat.equilibriumNotReached = true; alpha = lambda1; delta = sigma/mu1; diff --git a/mo/src/neighborhood/moRealNeighborhood.h b/mo/src/neighborhood/moRealNeighborhood.h index e839d16fa..cd06e1c68 100644 --- a/mo/src/neighborhood/moRealNeighborhood.h +++ b/mo/src/neighborhood/moRealNeighborhood.h @@ -27,7 +27,6 @@ Lionel Parreaux #define __moRealNeighborhood_h__ #include -#include // FIXME: Why don't we use eoFunctorBase on the mother classes #include "neighborhood/moRealNeighbor.h" template From 9d0b83022d469be106db07afef5cc49cb22ff4ce Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 10 Jul 2013 17:50:01 +0200 Subject: [PATCH 35/36] intermediate commit 6' --- eo/src/utils/eoGetterUpdater.h | 90 +++++++ mo/src/algo/moMetropolisHastings.h | 112 ++++++++ mo/src/continuator/moFunctionContinuator.h | 64 +++++ .../coolingSchedule/moHuangCoolingSchedule.h | 114 ++++++++ .../explorer/moMetropolisHastingsExplorer.h | 203 +++++++++++++++ mo/src/explorer/moSAExplorer.h | 243 ++++++++++++++++++ .../moSimpleMetropolisHastingsExplorer.h | 230 +++++++++++++++++ mo/test/t-moMetropolisHastings.cpp | 65 +++++ mo/test/t-moMetropolisHastingsExplorer.cpp | 98 +++++++ mo/test/t-moSAExplorer.cpp | 87 +++++++ 10 files changed, 1306 insertions(+) create mode 100644 eo/src/utils/eoGetterUpdater.h create mode 100644 mo/src/algo/moMetropolisHastings.h create mode 100644 mo/src/continuator/moFunctionContinuator.h create mode 100644 mo/src/coolingSchedule/moHuangCoolingSchedule.h create mode 100644 mo/src/explorer/moMetropolisHastingsExplorer.h create mode 100644 mo/src/explorer/moSAExplorer.h create mode 100644 mo/src/explorer/moSimpleMetropolisHastingsExplorer.h create mode 100644 mo/test/t-moMetropolisHastings.cpp create mode 100644 mo/test/t-moMetropolisHastingsExplorer.cpp create mode 100644 mo/test/t-moSAExplorer.cpp diff --git a/eo/src/utils/eoGetterUpdater.h b/eo/src/utils/eoGetterUpdater.h new file mode 100644 index 000000000..99d2d8c54 --- /dev/null +++ b/eo/src/utils/eoGetterUpdater.h @@ -0,0 +1,90 @@ +/* + +(c) Thales group, 2010 + + 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; + version 2 of the License. + + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + +#ifndef _eoGetterUpdater_h +#define _eoGetterUpdater_h + +#include + +template class eoCheckPoint; + +/** + eoGetterUpdater is an eoUpdater + TODO + + @ingroup Utilities +*/ +template +class eoGetterUpdater : public eoUpdater, public eoValueParam +{ +public: + using eoValueParam::value; + + virtual std::string className(void) const { return "eoGetterUpdater"; } + + typedef V (T::*MethodType)(); + + // Overload to accept const getter methods; safely casts them to non-const + eoGetterUpdater(T& _instance, V (T::*_method)() const) + //eoGetterUpdater(T& _instance, V (T::*_method)()=&T::value) + //: instance(_instance), method(static_cast(_method)) + //: instance(_instance), method(const_cast(_method)) + : instance(_instance), method((MethodType)_method) + { } + + //eoGetterUpdater(T& _instance, V (T::*_method)()) + eoGetterUpdater(T& _instance, MethodType _method) + //eoGetterUpdater(T& _instance, V (T::*_method)()=&T::value) + : instance(_instance), method(_method) + { } + + virtual void operator()() + { + value() = (instance.*method)(); + } + +private: + T& instance; + V (T::*method)(); +}; + + + +#endif + + + + + + + + + + + + + + + diff --git a/mo/src/algo/moMetropolisHastings.h b/mo/src/algo/moMetropolisHastings.h new file mode 100644 index 000000000..069451e9b --- /dev/null +++ b/mo/src/algo/moMetropolisHastings.h @@ -0,0 +1,112 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + +Sebastien Verel, Arnaud Liefooghe, Jeremie Humeau, Lionel Parreaux + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef _moMetropolisHastings_h +#define _moMetropolisHastings_h + +#include +#include +#include +#include +#include +#include + +/** + * Metropolis-Hasting local search + * Only the symetric case is considered when Q(x,y) = Q(y,x) + * Fitness must be > 0 + * + * At each iteration, + * one of the random solution in the neighborhood is selected + * if the selected neighbor have higher or equal fitness than the current solution + * then the solution is replaced by the selected neighbor + * if a random number from [0,1] is lower than fitness(neighbor) / fitness(solution) + * then the solution is replaced by the selected neighbor + * the algorithm stops when the number of iterations is too large + */ +template +class moMetropolisHastings: public moLocalSearch +{ +public: + typedef typename Neighbor::EOT EOT; + typedef moNeighborhood Neighborhood ; + + /** + * Basic constructor of the Metropolis-Hasting + * @param _neighborhood the neighborhood + * @param _fullEval the full evaluation function + * @param _eval neighbor's evaluation function + * @param _nbStep maximum step to do + */ + moMetropolisHastings(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep): + moLocalSearch(explorer, trueCont, _fullEval), + explorer(_neighborhood, _eval, defaultNeighborComp, defaultSolNeighborComp, _nbStep) + {} + + /** + * Simple constructor of the Metropolis-Hasting + * @param _neighborhood the neighborhood + * @param _fullEval the full evaluation function + * @param _eval neighbor's evaluation function + * @param _nbStep maximum step to do + * @param _cont an external continuator + */ + moMetropolisHastings(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep, moContinuator& _cont): + moLocalSearch(explorer, _cont, _fullEval), + explorer(_neighborhood, _eval, defaultNeighborComp, defaultSolNeighborComp, _nbStep) + {} + + /** + * General constructor of the Metropolis-Hasting + * @param _neighborhood the neighborhood + * @param _fullEval the full evaluation function + * @param _eval neighbor's evaluation function + * @param _nbStep maximum step to do + * @param _cont an external continuator + * @param _compN a neighbor vs neighbor comparator + * @param _compSN a solution vs neighbor comparator + */ + moMetropolisHastings(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned int _nbStep, moContinuator& _cont, moNeighborComparator& _compN, moSolNeighborComparator& _compSN): + moLocalSearch(explorer, _cont, _fullEval), + explorer(_neighborhood, _eval, _compN, _compSN, _nbStep) + {} + +private: + // always true continuator + moTrueContinuator trueCont; + // compare the fitness values of neighbors + moNeighborComparator defaultNeighborComp; + // compare the fitness values of the solution and the neighbor + moSolNeighborComparator defaultSolNeighborComp; + // MetropolisHasting explorer + //moMetropolisHastingsExplorer explorer; + moSimpleMetropolisHastingsExplorer explorer; +}; + +#endif diff --git a/mo/src/continuator/moFunctionContinuator.h b/mo/src/continuator/moFunctionContinuator.h new file mode 100644 index 000000000..f2cf7747e --- /dev/null +++ b/mo/src/continuator/moFunctionContinuator.h @@ -0,0 +1,64 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef _moFunctionContinuator_h +#define _moFunctionContinuator_h + +#include +#include + +/** + * To make specific continuator from a solution + */ +template< class Neighbor > +class moFunctionContinuator : public moContinuator +{ +public: + + typedef typename Neighbor::EOT EOT ; + + /** + * Init Continuator parameters + * @param _solution the related solution + */ + virtual void init(EOT& _solution) {}; + + /** + * Last Call to terminate the checkpoint + * @param _solution the related solution + */ + virtual void lastCall(EOT& _solution) {}; +}; + +#endif diff --git a/mo/src/coolingSchedule/moHuangCoolingSchedule.h b/mo/src/coolingSchedule/moHuangCoolingSchedule.h new file mode 100644 index 000000000..6ac29d778 --- /dev/null +++ b/mo/src/coolingSchedule/moHuangCoolingSchedule.h @@ -0,0 +1,114 @@ +/* + +(c) Thales group, 2010 + + 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; + version 2 of the License. + + 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: http://eodev.sourceforge.net + +Authors: +Lionel Parreaux + +*/ + +#ifndef _moHuangCoolingSchedule_h +#define _moHuangCoolingSchedule_h + +#include + +#include +#include +#include +#include +#include + + +#include +using namespace std; + +//! +/*! + */ +template< class EOT > +class moHuangCoolingSchedule: public moCoolingSchedule< EOT > +{ +public: + //typedef typename Neighbor::EOT EOT ; + //typedef moNeighborhood Neighborhood ; + + //! Constructor + /*! + */ + + moHuangCoolingSchedule (double _initTemp, double _stdDevEstimation, double _lambda = .7, double _finalTemp = .01) + : initTemp(_initTemp), + stdDevEstimation(_stdDevEstimation), + lambda(_lambda), + finalTemp(_finalTemp) + { } + + /** + * Initial temperature + * @param _solution initial solution + */ + double init(EOT & _solution) { + + return initTemp; + } + + /** + * update the temperature by a factor + * @param _temp current temperature to update + * @param _acceptedMove true when the move is accepted, false otherwise + */ + void update(double& _temp, bool _acceptedMove, EOT & _solution) { + + _temp *= exp ( -lambda*_temp / stdDevEstimation ) ; + + } + + //! Function which proceeds to the cooling + /*! + */ + bool operator() (double temperature) + { + return temperature > finalTemp; + + } + + +private: + + const double + // parameters of the algorithm + initTemp, + stdDevEstimation, + lambda, + finalTemp + ; + +}; + +#endif + + + + + + + + + + diff --git a/mo/src/explorer/moMetropolisHastingsExplorer.h b/mo/src/explorer/moMetropolisHastingsExplorer.h new file mode 100644 index 000000000..caf44f49f --- /dev/null +++ b/mo/src/explorer/moMetropolisHastingsExplorer.h @@ -0,0 +1,203 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau, Lionel Parreaux + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef _moMetropolisHastingsExplorer_h +#define _moMetropolisHastingsExplorer_h + +#include + +#include +#include +#include +#include +#include + +#include +#include + +/** + * Explorer for the Metropolis-Hasting Sampling. + * Only the symetric case is considered when Q(x,y) = Q(y,x) + * Fitness must be > 0 + */ +template< class Neighbor, class Derived /*TODO reqs on Derived*/> +class moMetropolisHastingsExplorer : public moNeighborhoodExplorer +{ +public: + typedef typename Neighbor::EOT EOT ; + typedef moNeighborhood Neighborhood ; + + using moNeighborhoodExplorer::neighborhood; + using moNeighborhoodExplorer::eval; + using moNeighborhoodExplorer::selectedNeighbor; + + /** + * Constructor + * @param _neighborhood the neighborhood + * @param _eval the evaluation function + * @param _neighborComparator a neighbor comparator + * @param _solNeighborComparator a solution vs neighbor comparator + * @param _nbStep maximum number of step to do + */ + moMetropolisHastingsExplorer( + Neighborhood& _neighborhood, + moEval& _eval, + //moNeighborComparator& _neighborComparator, + //moSolNeighborComparator& _solNeighborComparator + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moNeighborhoodExplorer(_neighborhood, _eval), + //neighborComparator(_neighborComparator), + //solNeighborComparator(_solNeighborComparator), + defaultSolNeighborComp(NULL), + solNeighborComparator(_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator())) + { + //isMoveAccepted = false; + if (!neighborhood.isRandom()) { + //std::cout << "moMetropolisHastingsExplorer::Warning -> the neighborhood used is not random" << std::endl; + eo::log << eo::warnings << "moMetropolisHastingsExplorer::Warning -> the neighborhood used is not random" << std::endl; + } + } + + /** + * Destructor + */ + ~moMetropolisHastingsExplorer() { + if (defaultSolNeighborComp != NULL) + delete defaultSolNeighborComp; + } + +// /** +// * initialization of the number of step to be done +// * @param _solution unused solution +// */ +// virtual void initParam(EOT & _solution) { +// step = 0; +// isMoveAccepted = true; +// }; +// +// /** +// * terminate: NOTHING TO DO +// * @param _solution unused solution +// */ +// virtual void terminate(EOT & _solution) {}; + + virtual void initParam(EOT & _solution) {}; + virtual void terminate(EOT & _solution) {}; + + + + /** + * Explore the neighborhood of a solution + * @param _solution + */ + virtual void operator()(EOT & _solution) { + //Test if _solution has a Neighbor + if (neighborhood.hasNeighbor(_solution)) { + //init the first neighbor + neighborhood.init(_solution, selectedNeighbor); + + //eval the _solution moved with the neighbor and stores the result in the neighbor + eval(_solution, selectedNeighbor); + } + /* + else { + //if _solution hasn't neighbor, + isMoveAccepted = false; + }*/ + }; + + /** + * accept test if an ameliorated neighbor was found + * @param _solution the solution + * @return true if the best neighbor ameliorate the fitness + */ + virtual bool accept(EOT & _solution) { + /*double alpha=0.0; + if (neighborhood.hasNeighbor(_solution)) { + if (solNeighborComparator(_solution, selectedNeighbor)) + isMoveAccepted = true; + else { + if (_solution.fitness() != 0) { + if ( (double)selectedNeighbor.fitness() < (double)_solution.fitness()) // maximizing + alpha = (double) selectedNeighbor.fitness() / (double) _solution.fitness(); + else //minimizing + alpha = (double) _solution.fitness() / (double) selectedNeighbor.fitness(); + isMoveAccepted = (rng.uniform() < alpha) ; + } + else { + if ( (double) selectedNeighbor.fitness() < (double) _solution.fitness()) // maximizing + isMoveAccepted = true; + else + isMoveAccepted = false; + } + } + }*/ + /*bool accepted = false; + if (neighborhood.hasNeighbor(_solution)) { + if (solNeighborComparator(_solution, selectedNeighbor)) // accept if the current neighbor is better than the solution + accepted = true; + //else accepted = static_cast(this)->doAccept(_solution); + else accepted = rng.uniform() <= static_cast(this)->getAlpha(_solution); + } + return isMoveAccepted = accepted;*/ + bool isMoveAccepted = false; + if (neighborhood.hasNeighbor(_solution)) { + if (solNeighborComparator(_solution, selectedNeighbor)) // accept if the current neighbor is better than the solution + isMoveAccepted = true; + //else accepted = static_cast(this)->doAccept(_solution); + else isMoveAccepted = rng.uniform() < static_cast(this)->alpha(_solution); + } + return isMoveAccepted; + }; + +private: + moSolNeighborComparator* defaultSolNeighborComp; + + // comparator betwenn solution and neighbor or between neighbors + //moNeighborComparator& neighborComparator; + moSolNeighborComparator& solNeighborComparator; + /* + // current number of step + unsigned int step; + + // maximum number of steps to do + unsigned int nbStep; + */ + // true if the move is accepted + //bool isMoveAccepted ; +}; + + +#endif diff --git a/mo/src/explorer/moSAExplorer.h b/mo/src/explorer/moSAExplorer.h new file mode 100644 index 000000000..12afeac1f --- /dev/null +++ b/mo/src/explorer/moSAExplorer.h @@ -0,0 +1,243 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau, Lionel Parreaux + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef _moSAExplorer_h +#define _moSAExplorer_h + +#include + +//#include +#include +#include +#include +#include +#include +#include + +#include + +/** + * Explorer for the Simulated Annealing + * Only the symetric case is considered when Q(x,y) = Q(y,x) + * Fitness must be > 0 + * + */ +template< class Neighbor > +class moSAExplorer : public moMetropolisHastingsExplorer< Neighbor, moSAExplorer > +{ +public: + typedef typename Neighbor::EOT EOT ; + typedef moNeighborhood Neighborhood ; + + using moNeighborhoodExplorer::neighborhood; + using moNeighborhoodExplorer::eval; + using moNeighborhoodExplorer::selectedNeighbor; + + + /*moSAExplorer(Neighborhood& _neighborhood, moEval& _eval) + : moNeighborhoodExplorer(_neighborhood, _eval) + { }*/ + + moSAExplorer ( + Neighborhood& _neighborhood, + //eoOptional< moEval > _eval = NULL, + moEval& _eval, + moCoolingSchedule& _cool, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moMetropolisHastingsExplorer< Neighbor, moSAExplorer >(_neighborhood, _eval, _comp), + /*moNeighborhoodExplorer(_neighborhood, _eval.hasValue()? _eval.get(): *(default_eval = new moFullEvalByCopy(_fullEval))), + default_eval(NULL), // removed in C++11 with unique_ptr*/ + //defaultSolNeighborComp(NULL), // removed in C++11 with unique_ptr + //solNeighborComparator(_comp.hasValue()? _comp.get(): *(defaultSolNeighborComp = new moSolNeighborComparator())), + //coolingSchedule(_coolingSchedule) + coolingSchedule(_cool) + { + /*isMoveAccepted = false; + if (!neighborhood.isRandom()) { + std::cout << "moSAexplorer::Warning -> the neighborhood used is not random" << std::endl; + }*/ + } + + /** + * Destructor + */ + ~moSAExplorer() { + } + + /** + * initialization of the initial temperature + * @param _solution the solution + */ + virtual void initParam(EOT & _solution) { + temperature = coolingSchedule.init(_solution); + //isMoveAccepted = false; + }; + + /** + * decrease the temperature if necessary + * @param _solution unused solution + */ + virtual void updateParam(EOT & _solution) { + coolingSchedule.update(temperature, this->moveApplied(), _solution); + }; + + /** + * terminate: NOTHING TO DO + * @param _solution unused solution + */ + virtual void terminate(EOT & _solution) {}; + + /** + * Explore one random solution in the neighborhood + * @param _solution the solution + */ + virtual void operator()(EOT & _solution) { + //Test if _solution has a Neighbor + if (neighborhood.hasNeighbor(_solution)) { + //init on the first neighbor: supposed to be random solution in the neighborhood + neighborhood.init(_solution, selectedNeighbor); + + //eval the _solution moved with the neighbor and stock the result in the neighbor + eval(_solution, selectedNeighbor); + } + else { + //if _solution hasn't neighbor, + //isMoveAccepted = false; + } + }; + + /** + * continue if the temperature is not too low + * @param _solution the solution + * @return true if the criteria from the cooling schedule is true + */ + virtual bool isContinue(EOT & _solution) { + return coolingSchedule(temperature); + }; + + /** + * acceptance criterion according to the boltzmann criterion + * @param _solution the solution + * @return true if better neighbor or rnd < exp(delta f / T) + */ +// virtual bool accept(EOT & _solution) { +// if (neighborhood.hasNeighbor(_solution)) { +// if (solNeighborComparator(_solution, selectedNeighbor)) // accept if the current neighbor is better than the solution +// isMoveAccepted = true; +// else { +// /* +// double alpha=0.0; +// double fit1, fit2; +// fit1=(double)selectedNeighbor.fitness(); +// fit2=(double)_solution.fitness(); +// if (fit1 < fit2) // this is a maximization +// alpha = exp((fit1 - fit2) / temperature ); +// else // this is a minimization +// alpha = exp((fit2 - fit1) / temperature ); +// isMoveAccepted = (rng.uniform() < alpha) ;*/ +// /* +// double fit1 = (double)selectedNeighbor.fitness(), +// fit2 = (double)_solution.fitness(), +// alpha = fit1 < fit2 ? exp((fit1 - fit2) / temperature) : exp((fit2 - fit1) / temperature); +// //if (fit1 < fit2) // this is a maximization +// //else // this is a minimization +// isMoveAccepted = (rng.uniform() < alpha);*/ +// /*double fit1 = (double) selectedNeighbor.fitness(), +// fit2 = (double) _solution.fitness(), +// alpha = fit1 < fit2 ? exp((fit1 - fit2) / temperature) : 1; +// isMoveAccepted = (rng.uniform() <= alpha);*/ +// /* +// double fit1 = (double) selectedNeighbor.fitness(), +// fit2 = (double) _solution.fitness(), +// alpha = exp( - fabs(fit1 - fit2) / temperature ); +// // (fit1 - fit2) positive or negative depending on whether we're maximizing or minimizing +// isMoveAccepted = (rng.uniform() <= alpha);*/ +// //isMoveAccepted = Accepter::accept(_solution, selectedNeighbor); +// isMoveAccepted = static_cast(this)->accept(_solution); +// } +// } +// return isMoveAccepted; +// }; + + /** + * Getter + * @return the temperature + */ + double getTemperature() const { + return temperature; + } + + + /* + virtual bool doAccept(EOT & _solution) { + double fit1 = (double) selectedNeighbor.fitness(), + fit2 = (double) _solution.fitness(), + alpha = exp( - fabs(fit1 - fit2) / temperature ); + // (fit1 - fit2) positive or negative depending on whether we're maximizing or minimizing + return rng.uniform() <= alpha; + }*/ + double alpha(EOT & _solution) { + //std::cout << "ok " << exp( - fabs((double) selectedNeighbor.fitness() - (double) _solution.fitness()) / temperature ) << " "; + return exp( - fabs((double) selectedNeighbor.fitness() - (double) _solution.fitness()) / temperature ); + } + + +private: + + //moSolNeighborComparator* defaultSolNeighborComp; + +public://FIXME add friend + // comparator betwenn solution and neighbor + //moSolNeighborComparator& solNeighborComparator; + + moCoolingSchedule& coolingSchedule; + + // temperatur of the process + double temperature; + + // true if the move is accepted + //bool isMoveAccepted ; + + /** + * Getter + * @return the temperature + */ + //virtual double getTemperature() = 0; + +}; + + +#endif + diff --git a/mo/src/explorer/moSimpleMetropolisHastingsExplorer.h b/mo/src/explorer/moSimpleMetropolisHastingsExplorer.h new file mode 100644 index 000000000..64fdc7bd9 --- /dev/null +++ b/mo/src/explorer/moSimpleMetropolisHastingsExplorer.h @@ -0,0 +1,230 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau, Lionel Parreaux + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef _moSimpleMetropolisHastingsExplorer_h +#define _moSimpleMetropolisHastingsExplorer_h +/* +#include + +#include +#include +#include +#include + +#include */ +#include + + +/** + * Explorer for the Metropolis-Hasting Sampling. + * Only the symetric case is considered when Q(x,y) = Q(y,x) + * Fitness must be > 0 + */ +template< class Neighbor > +class moSimpleMetropolisHastingsExplorer : public moMetropolisHastingsExplorer< Neighbor, moSimpleMetropolisHastingsExplorer > +{ +public: + typedef typename Neighbor::EOT EOT ; + typedef moNeighborhood Neighborhood ; + /* + using moNeighborhoodExplorer::neighborhood; + using moNeighborhoodExplorer::eval; + using moNeighborhoodExplorer::selectedNeighbor; + */ + + using moNeighborhoodExplorer::selectedNeighbor; + + + /** +// * Constructor TODO +// * @param _neighborhood the neighborhood +// * @param _eval the evaluation function +// * @param _neighborComparator a neighbor comparator +// * @param _solNeighborComparator a solution vs neighbor comparator +// * @param _maxSteps maximum number of currentStepNb to do +// */ + moSimpleMetropolisHastingsExplorer ( + Neighborhood& _neighborhood, + moEval& _eval, + unsigned int _maxSteps, + eoOptional< moSolNeighborComparator > _comp = NULL + ) + : moMetropolisHastingsExplorer< Neighbor, moSimpleMetropolisHastingsExplorer >(_neighborhood, _eval, _comp), + maxSteps(_maxSteps) + { } + + + + + + /** +// * Constructor +// * @param _neighborhood the neighborhood +// * @param _eval the evaluation function +// * @param _neighborComparator a neighbor comparator +// * @param _solNeighborComparator a solution vs neighbor comparator +// * @param _maxSteps maximum number of currentStepNb to do +// */ +// moSimpleMetropolisHastingsExplorer( +// Neighborhood& _neighborhood, +// moEval& _eval, +// moNeighborComparator& _neighborComparator, +// moSolNeighborComparator& _solNeighborComparator, +// unsigned int _maxSteps +// ): moNeighborhoodExplorer(_neighborhood, _eval), +// neighborComparator(_neighborComparator), +// solNeighborComparator(_solNeighborComparator), +// maxSteps(_maxSteps) +// { +// isAccept = false; +// if (!neighborhood.isRandom()) { +// std::cout << "moSimpleMetropolisHastingsExplorer::Warning -> the neighborhood used is not random" << std::endl; +// } +// } +// +// /** +// * Destructor +// */ +// ~moSimpleMetropolisHastingsExplorer() { +// } +// + /** + * initialization of the number of currentStepNb to be done + * @param _solution unused solution + */ + virtual void initParam(EOT & _solution) { + currentStepNb = 0; + //isAccept = true; + }; +// + /** + * increase the number of currentStepNb + * @param _solution unused solution + */ + virtual void updateParam(EOT & _solution) { + currentStepNb++; + }; +// +// /** +// * terminate: NOTHING TO DO +// * @param _solution unused solution +// */ +// virtual void terminate(EOT & _solution) {}; +// +// /** +// * Explore the neighborhood of a solution +// * @param _solution +// */ +// virtual void operator()(EOT & _solution) { +// //Test if _solution has a Neighbor +// if (neighborhood.hasNeighbor(_solution)) { +// //init the first neighbor +// neighborhood.init(_solution, selectedNeighbor); +// +// //eval the _solution moved with the neighbor and stock the result in the neighbor +// eval(_solution, selectedNeighbor); +// } +// else { +// //if _solution hasn't neighbor, +// isAccept=false; +// } +// }; +// + /** + * continue if there is a neighbor and it is remainds some steps to do + * @param _solution the solution + * @return true there is some steps to do + */ + virtual bool isContinue(EOT & _solution) { + return currentStepNb < maxSteps; + }; +// +// /** +// * accept test if an ameliorated neighbor was found +// * @param _solution the solution +// * @return true if the best neighbor ameliorate the fitness +// */ +// virtual bool accept(EOT & _solution) { +// double alpha=0.0; +// if (neighborhood.hasNeighbor(_solution)) { +// if (solNeighborComparator(_solution, selectedNeighbor)) +// isAccept = true; +// else { +// if (_solution.fitness() != 0) { +// if ( (double)selectedNeighbor.fitness() < (double)_solution.fitness()) // maximizing +// alpha = (double) selectedNeighbor.fitness() / (double) _solution.fitness(); +// else //minimizing +// alpha = (double) _solution.fitness() / (double) selectedNeighbor.fitness(); +// isAccept = (rng.uniform() < alpha) ; +// } +// else { +// if ( (double) selectedNeighbor.fitness() < (double) _solution.fitness()) // maximizing +// isAccept = true; +// else +// isAccept = false; +// } +// } +// } +// return isAccept; +// }; + + double alpha(EOT & _solution) { + if (selectedNeighbor.fitness() == 0) + return selectedNeighbor.fitness() < (double) _solution.fitness() ? 1: 0; + return (double) _solution.fitness() / (double) selectedNeighbor.fitness(); + } +// +//private: +// // comparator betwenn solution and neighbor or between neighbors +// moNeighborComparator& neighborComparator; +// moSolNeighborComparator& solNeighborComparator; +// +// // current number of currentStepNb +// unsigned int currentStepNb; +// +// // maximum number of steps to do +// unsigned int maxSteps; +// +// // true if the move is accepted +// bool isAccept ; +private: + // current number of steps + unsigned int currentStepNb; + + // maximum number of steps to do + unsigned int maxSteps; +}; + + +#endif diff --git a/mo/test/t-moMetropolisHastings.cpp b/mo/test/t-moMetropolisHastings.cpp new file mode 100644 index 000000000..6bab6c332 --- /dev/null +++ b/mo/test/t-moMetropolisHastings.cpp @@ -0,0 +1,65 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + +Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#include +#include +#include + +#include +#include "moTestClass.h" +#include +#include +#include +#include + +int main() { + + std::cout << "[t-moMetropolisHastings] => START" << std::endl; + + bitNeighborhood nh(4); + oneMaxEval fullEval; + evalOneMax eval(4); + moTrueContinuator cont; + moSolNeighborComparator sncomp; + moNeighborComparator ncomp; + + //test du 1er constructeur + moMetropolisHastings test1(nh, fullEval, eval, 3); + + //test du 2eme constructeur + moMetropolisHastings test2(nh, fullEval, eval, 3, cont); + + //test du 3eme constructeur + moMetropolisHastings test3(nh, fullEval, eval, 3, cont, ncomp, sncomp); + + std::cout << "[t-moMetropolisHastings] => OK" << std::endl; + + return EXIT_SUCCESS; +} + diff --git a/mo/test/t-moMetropolisHastingsExplorer.cpp b/mo/test/t-moMetropolisHastingsExplorer.cpp new file mode 100644 index 000000000..b7504486d --- /dev/null +++ b/mo/test/t-moMetropolisHastingsExplorer.cpp @@ -0,0 +1,98 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + +Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#include +#include "moTestClass.h" + +#include +#include +#include + +int main() { + + std::cout << "[t-moMetropolisHastingsExplorer] => START" << std::endl; + + //Instanciation + eoBit sol(4, true); + sol.fitness(4); + bitNeighborhood nh(4); + evalOneMax eval(4); + moNeighborComparator ncomp; + moSolNeighborComparator sncomp; + + //moSimpleMetropolisHastingsExplorer test(nh, eval, ncomp, sncomp, 3); + moSimpleMetropolisHastingsExplorer test(nh, eval, 3, sncomp); + + //test de l'acceptation d'un voisin améliorant + test.initParam(sol); + test(sol); + assert(test.accept(sol)); + test.move(sol); + assert(sol.fitness()==3); + test.updateParam(sol); + assert(test.isContinue(sol)); + + unsigned int oui=0, non=0; + + //test de l'acceptation d'un voisin non améliorant + for (unsigned int i=0; i<1000; i++) { + test(sol); + if (test.accept(sol)) + oui++; + else + non++; + } + std::cout << "Attention test en fonction d'une proba \"p\" uniforme dans [0,1] , oui si p < 3/4, non sinon -> resultat sur 1000 essai" << std::endl; + std::cout << "oui: " << oui << std::endl; + std::cout << "non: " << non << std::endl; + + assert(oui > 700 && oui < 800); //verification grossiere + + //test du critere d'arret + test.updateParam(sol); + assert(test.isContinue(sol)); + test.updateParam(sol); + assert(!test.isContinue(sol)); + + //test de l'acceptation d'un voisin + sol[0]=false; + sol[1]=false; + sol[2]=false; + sol[3]=false; + sol.fitness(0); + + test.initParam(sol); + test(sol); + assert(!test.accept(sol)); + + std::cout << "[t-moMetropolisHastingsExplorer] => OK" << std::endl; + + return EXIT_SUCCESS; +} + diff --git a/mo/test/t-moSAExplorer.cpp b/mo/test/t-moSAExplorer.cpp new file mode 100644 index 000000000..873dbf080 --- /dev/null +++ b/mo/test/t-moSAExplorer.cpp @@ -0,0 +1,87 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + +Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#include +#include +#include + +#include "moTestClass.h" +#include +#include + +int main() { + + std::cout << "[t-moSAExplorer] => START" << std::endl; + + eoBit sol(4, true); + sol.fitness(4); + bitNeighborhood nh(4); + bitNeighborhood emptyNH(0); + evalOneMax eval(4); + moSolNeighborComparator sncomp; + moSimpleCoolingSchedule cool(10,0.1,2,0.1); + + moSAExplorer test1(emptyNH, eval, cool, sncomp); + moSAExplorer test2(nh, eval, cool, sncomp); + + //test d'un voisinage vide + test1.initParam(sol); + test1(sol); + assert(!test1.accept(sol)); + assert(test1.getTemperature()==10.0); + + //test d'un voisinage "normal" + test2.initParam(sol); + test2(sol); + assert(test2.accept(sol)); + test2.updateParam(sol); + assert(test2.isContinue(sol)); + test2.move(sol); + assert(sol.fitness()==3); + unsigned int ok=0; + unsigned int ko=0; + for (unsigned int i=0; i<1000; i++) { + test2(sol); + if (test2.isContinue(sol)) + test2.updateParam(sol); + if (test2.accept(sol)) + ok++; + else + ko++; + test2.move(sol); + } + assert((ok>0) && (ko>0)); + + + + std::cout << "[t-moSAExplorer] => OK" << std::endl; + + return EXIT_SUCCESS; +} + From ba19b29a2256348017886d0a0a1bb456b667b08c Mon Sep 17 00:00:00 2001 From: LPTK Date: Mon, 15 Jul 2013 11:34:55 +0200 Subject: [PATCH 36/36] intermediate commit 7 --- mo/src/continuator/moCounterMonitorSaver.h | 6 +- .../coolingSchedule/moHuangCoolingSchedule.h | 77 ++++++++++++++----- .../coolingSchedule/moTrikiCoolingSchedule.h | 20 +++-- 3 files changed, 76 insertions(+), 27 deletions(-) diff --git a/mo/src/continuator/moCounterMonitorSaver.h b/mo/src/continuator/moCounterMonitorSaver.h index f57e05d2d..6ac5a7062 100644 --- a/mo/src/continuator/moCounterMonitorSaver.h +++ b/mo/src/continuator/moCounterMonitorSaver.h @@ -52,7 +52,11 @@ public : moCounterMonitorSaver(unsigned _interval, eoMonitor& _mon) : interval(_interval), counter(0) { monitors.push_back(&_mon); } - + + moCounterMonitorSaver(unsigned _interval) + : interval(_interval), counter(0) + { } + /** * call monitors if interval is reach by a counter */ diff --git a/mo/src/coolingSchedule/moHuangCoolingSchedule.h b/mo/src/coolingSchedule/moHuangCoolingSchedule.h index 6ac29d778..c702b852f 100644 --- a/mo/src/coolingSchedule/moHuangCoolingSchedule.h +++ b/mo/src/coolingSchedule/moHuangCoolingSchedule.h @@ -35,8 +35,11 @@ Lionel Parreaux #include -#include -using namespace std; +//#include +//using namespace std; + +#include + //! /*! @@ -51,23 +54,30 @@ public: //! Constructor /*! */ - - moHuangCoolingSchedule (double _initTemp, double _stdDevEstimation, double _lambda = .7, double _finalTemp = .01) - : initTemp(_initTemp), - stdDevEstimation(_stdDevEstimation), - lambda(_lambda), - finalTemp(_finalTemp) - { } +// moHuangCoolingSchedule (double _initTemp, int _spanSize, double _lambda = .7, double _finalTemp = .01) +// moHuangCoolingSchedule (double _initTemp, int _spanSize, double _lambda = .7, double _finalStdDev = .01) + moHuangCoolingSchedule (double _initTemp, int _spanSize, double _lambda = .7, double _finalTempDecrease = .995) + : initTemp(_initTemp) + , spanSize(_spanSize) + , lambda(_lambda) +// , finalTemp(_finalTemp) + , finalStdDev(_finalTempDecrease) +// , statIsInitialized(false) +// , step(0) +// , currentStdDevEstimation(std::numeric_limits::max()) + { } + /** * Initial temperature * @param _solution initial solution */ double init(EOT & _solution) { - + statIsInitialized = terminated = false; + step = 0; return initTemp; } - + /** * update the temperature by a factor * @param _temp current temperature to update @@ -75,8 +85,30 @@ public: */ void update(double& _temp, bool _acceptedMove, EOT & _solution) { - _temp *= exp ( -lambda*_temp / stdDevEstimation ) ; + if (_acceptedMove) + { + if (statIsInitialized) + momentStat(_solution); + else momentStat.init(_solution), statIsInitialized = true; + + } + if (step >= spanSize) + { + step = 0; + /* + //double avgFitness = momentStat.value().first; + double variance = momentStat.value().second; + //double stdDevEstimation = sqrt(variance); + double currentStdDevEstimation = sqrt(variance); + _temp *= exp( -lambda*_temp / currentStdDevEstimation );*/ + double alpha = exp( -lambda*_temp / sqrt(momentStat.value().second) ); + _temp *= alpha; + //std::cout << alpha << std::endl; + terminated = alpha > finalStdDev; + } + + step++; } //! Function which proceeds to the cooling @@ -84,8 +116,9 @@ public: */ bool operator() (double temperature) { - return temperature > finalTemp; - + //return temperature > finalTemp; + //return currentStdDevEstimation > finalStdDev; + return !terminated; } @@ -93,11 +126,19 @@ private: const double // parameters of the algorithm - initTemp, - stdDevEstimation, - lambda, - finalTemp + initTemp +// , stdDevEstimation + , lambda +// , finalTemp + , finalStdDev ; + const int spanSize; + int step; + + //double currentStdDevEstimation; + + moFitnessMomentsStat momentStat; + bool statIsInitialized, terminated; }; diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index eb078120a..0b4466671 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -144,6 +144,8 @@ public: negative_temp = equilibrium_not_reached = frozen = 0; + chainStat.delta = initTemp/mu2; + //cout << "acc " << max_accepted << " " << max_generated << endl; /* @@ -230,7 +232,9 @@ public: //double stdDev = sqrt(variance); chainStat.stdDev = sqrt(variance); double sigma = chainStat.stdDev; - double delta = sigma/mu2; + /////////////// + //double delta = sigma/mu2; + /////////////// //outf << avgFitness << endl; @@ -262,7 +266,7 @@ public: //if (avgFitness/(chainStat.prevAvgFitness-delta) > xi) cout << "/!\\ eq not reached!" << endl; /// - if (avgFitness/(prevAvgFitness-delta) > xi) + if (avgFitness/(prevAvgFitness-chainStat.delta) > xi) equilibrium_not_reached++, chainStat.equilibriumNotReached = true; else equilibrium_not_reached = 0; } @@ -277,10 +281,10 @@ public: //chainStat.equilibriumNotReached = true; alpha = lambda1; - delta = sigma/mu1; + chainStat.delta = sigma/mu1; equilibrium_not_reached = 0; // ADDED! Otherwise the algo gets trapped here! } - else if (_temp*delta/(sigma*sigma) >= 1) + else if (_temp*chainStat.delta/(sigma*sigma) >= 1) { /// //cout << "/!\\ neg temp!" << endl; @@ -293,7 +297,7 @@ public: if (negative_temp < K2) { alpha = lambda1; - delta = sigma/mu1; + chainStat.delta = sigma/mu1; } else alpha = lambda2; } @@ -320,7 +324,7 @@ public: reinitializing = false; //chainStat.avgFitness = avgFitness; //alpha = 1-_temp*delta/(sigma*sigma); - alpha = 1-_temp*delta/variance; + alpha = 1-_temp*chainStat.delta/variance; //alpha = (sigma==0? 1: 1-_temp*delta/(sigma*sigma)); // ADDED! but removed @@ -338,12 +342,12 @@ public: chainStat.currentFitness = _solution.fitness(); // FIXME here? chainStat.alpha = alpha; - chainStat.delta = delta; + //chainStat.delta = delta; chainStat.avgFitness = avgFitness; // Never seems to be used - if (avgFitness == prevAvgFitness) // use a neighborhood to approximate double equality? + if (avgFitness == prevAvgFitness) // use a neighborhood to approximate double floating equality? frozen++; else frozen = 0;