From ba5fe01ca013d91b63742033f934ba13b269bd79 Mon Sep 17 00:00:00 2001 From: nojhan Date: Thu, 15 Mar 2012 14:37:31 +0100 Subject: [PATCH 01/10] implicit conversion operator to the base scalar type for eoDualFitness (but is it a good idea?) --- eo/src/eoDualFitness.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/eo/src/eoDualFitness.h b/eo/src/eoDualFitness.h index 3b7fda0d..674fe075 100644 --- a/eo/src/eoDualFitness.h +++ b/eo/src/eoDualFitness.h @@ -103,6 +103,17 @@ public: _is_feasible(dual.second) {} + // FIXME is it a good idea to include implicit conversion here? + /** Conversion operator: it permits to use a fitness instance as its scalar + * type, if needed. For example, this is possible: + * eoDualFitness > fit; + * double val = 1.0; + * fit = val; + * val = fit; + */ + operator BaseType(void) const { return _value; } + + inline bool is_feasible() const { return _is_feasible; From 7e7a47c9bad5448ffab4409fb90dd43f0b1875a3 Mon Sep 17 00:00:00 2001 From: nojhan Date: Thu, 15 Mar 2012 18:14:04 +0100 Subject: [PATCH 02/10] added a getParam method to the eoParser, that raise an exception if the parameter has not been declared --- eo/src/utils/eoParser.cpp | 9 +++++++++ eo/src/utils/eoParser.h | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index fc1068b3..6ddcdb63 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -129,6 +129,15 @@ eoParam * eoParser::getParamWithLongName(const std::string& _name) const return 0; } +eoParam * getParam(const std::string& _name) const +{ + eoParam * p = getParamWithLongName( _name ); + if( p == NULL ) { + throw eoMissingParamException(_name ); + } else { + return p; + } +} void eoParser::processParam(eoParam& param, std::string section) { diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index e65f7566..1ba46317 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -176,6 +176,12 @@ public: eoParam * getParamWithLongName(const std::string& _name) const; + /** + * Get a handle on a param from its long name + * If not found, raise an eoMissingParamException + */ + eoParam * getParam(const std::string& _name) const; + /** Get or create parameter From 0808c52bf8f41bfeb127fb82a902dee88ec046af Mon Sep 17 00:00:00 2001 From: nojhan Date: Thu, 15 Mar 2012 18:15:23 +0100 Subject: [PATCH 03/10] removed a (funny but wontfix notabug) old comment in eoParser --- eo/src/utils/eoParser.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index 1ba46317..b0f4c888 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -188,9 +188,6 @@ public: It seems finally that the easiest use of the above method is through the following, whose interface is similar to that of the widely-used createParam. - - For some (probably very stupid) reason, I failed to put it in the - .cpp. Any hint??? */ template eoValueParam& getORcreateParam(ValueType _defaultValue, From a1aec6a5bedfed0ee168944a325ee13dcafcab6d Mon Sep 17 00:00:00 2001 From: nojhan Date: Thu, 15 Mar 2012 18:16:07 +0100 Subject: [PATCH 04/10] added the declaration of the exception used by eoParser::getParam --- eo/src/eoExceptions.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/eo/src/eoExceptions.h b/eo/src/eoExceptions.h index 3b888436..0066d985 100644 --- a/eo/src/eoExceptions.h +++ b/eo/src/eoExceptions.h @@ -55,7 +55,6 @@ private: }; - /*! An error that signals that a maximum number of evaluations has been reached. @@ -80,4 +79,26 @@ private: }; +/*! + * An error that signals a missing parameter + * + * Thrown by eoParser::getParam + * + * @ingroup Parameters + */ +class eoMissingParamException : public std::exception +{ +public: + eoMissingParamException(std::string name) : _name(name){} + + virtual const char* what() const throw() + { + std::ostringstream ss; + ss << "The command parameter " << _name << " has not been declared"; + return ss.str().c_str(); + } + +private: + std::string _name; +}; #endif // __eoExceptions_h__ From b89a216d2d272b4dea5729f23a3438f8d406483e Mon Sep 17 00:00:00 2001 From: nojhan Date: Thu, 15 Mar 2012 18:16:57 +0100 Subject: [PATCH 05/10] added a pragma warning along with the eo::log one in eoProportionalCombinedOp --- eo/src/eoProportionalCombinedOp.h | 1 + 1 file changed, 1 insertion(+) diff --git a/eo/src/eoProportionalCombinedOp.h b/eo/src/eoProportionalCombinedOp.h index 123c634b..4fea77e5 100644 --- a/eo/src/eoProportionalCombinedOp.h +++ b/eo/src/eoProportionalCombinedOp.h @@ -188,6 +188,7 @@ public: virtual void add(eoQuadOp & _op, const double _rate, bool _verbose) { +#pragma message "The use of the verbose parameter in eoPropCombinedQuadOp::add is deprecated and will be removed in the next release." eo::log << eo::warnings << "WARNING: the use of the verbose parameter in eoPropCombinedQuadOp::add is deprecated and will be removed in the next release." << std::endl; add(_op,_rate); } From 8be94d95cfd6ca551f8f4da967cebeb31851da93 Mon Sep 17 00:00:00 2001 From: nojhan Date: Fri, 16 Mar 2012 10:28:25 +0100 Subject: [PATCH 06/10] throwing destructor to avoid looser throw specifier error within gcc --- eo/src/eoExceptions.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/eo/src/eoExceptions.h b/eo/src/eoExceptions.h index 0066d985..d38b6cb1 100644 --- a/eo/src/eoExceptions.h +++ b/eo/src/eoExceptions.h @@ -98,6 +98,8 @@ public: return ss.str().c_str(); } + ~eoMissingParamException() throw() {} + private: std::string _name; }; From 47447c47650d55102c11464b5ce532dcabdce0d9 Mon Sep 17 00:00:00 2001 From: nojhan Date: Mon, 26 Mar 2012 18:15:45 +0200 Subject: [PATCH 07/10] inherit eoCombinedContinue from std::vector, instead of having it as a member, so that we can easily manipulate continuators themselves --- eo/src/eoCombinedContinue.h | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/eo/src/eoCombinedContinue.h b/eo/src/eoCombinedContinue.h index 4dac4d69..5226e28d 100644 --- a/eo/src/eoCombinedContinue.h +++ b/eo/src/eoCombinedContinue.h @@ -19,6 +19,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: todos@geneura.ugr.es, http://geneura.ugr.es +Authors : + todos@geneura.ugr.es + Marc Schoenauer + Ramón Casero Cañas + Johann Dréo */ //----------------------------------------------------------------------------- @@ -40,7 +45,7 @@ @ingroup Combination */ template< class EOT> -class eoCombinedContinue: public eoContinue { +class eoCombinedContinue: public eoContinue, public std::vector* > { public: /// Define Fitness @@ -48,45 +53,45 @@ public: /// Ctor, make sure that at least on continuator is present eoCombinedContinue( eoContinue& _cont) - : eoContinue () + : eoContinue(), std::vector* >(1, &_cont) { - continuators.push_back(&_cont); } /// Ctor - for historical reasons ... should disspear some day eoCombinedContinue( eoContinue& _cont1, eoContinue& _cont2) - : eoContinue () + : eoContinue(), std::vector* >() { - continuators.push_back(&_cont1); - continuators.push_back(&_cont2); +#pragma message "The double continuators constructor of eocombinedContinue is deprecated and will be removed in the next release." + + this->push_back(&_cont1); + this->push_back(&_cont2); } void add(eoContinue & _cont) { - continuators.push_back(&_cont); + this->push_back(&_cont); } - ///////////// RAMON'S CODE /////////////// void removeLast(void) { - continuators.pop_back(); +#pragma message "The removeLast method of eocombinedContinue is deprecated and will be removed in the next release, use pop_back instead." + this->pop_back(); } - ///////////// RAMON'S CODE (end) /////////////// /** Returns false when one of the embedded continuators say so (logical and) */ virtual bool operator() ( const eoPop& _pop ) { - for (unsigned i = 0; i < continuators.size(); ++i) - if ( !(*continuators[i])(_pop) ) return false; + for (unsigned i = 0; i < this->size(); ++i) + if ( ! (*this->at(i))(_pop) ) return false; return true; } virtual std::string className(void) const { return "eoCombinedContinue"; } -private: - std::vector*> continuators; +//private: +// std::vector*> continuators; }; #endif From 1bc2c5cc461127995e444463179fcf211db3f085 Mon Sep 17 00:00:00 2001 From: nojhan Date: Mon, 26 Mar 2012 18:18:45 +0200 Subject: [PATCH 08/10] added the 'value' method to eoParser, so as to permits a direct access to the value stored within the eovalueParam corresponding to a given param name --- eo/src/utils/eoParser.cpp | 13 +++++++++---- eo/src/utils/eoParser.h | 31 +++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index 6ddcdb63..7489cf65 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -18,9 +18,12 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Contact: todos@geneura.ugr.es, http://geneura.ugr.es - Marc.Schoenauer@inria.fr - mkeijzer@dhi.dk +Contact: http://eodev.sourceforge.net +Authors: + todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mkeijzer@dhi.dk + Johann Dréo */ //----------------------------------------------------------------------------- @@ -37,6 +40,8 @@ #include #include #include +#include + using namespace std; @@ -129,7 +134,7 @@ eoParam * eoParser::getParamWithLongName(const std::string& _name) const return 0; } -eoParam * getParam(const std::string& _name) const +eoParam * eoParser::getParam(const std::string& _name) const { eoParam * p = getParamWithLongName( _name ); if( p == NULL ) { diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index b0f4c888..b33f956e 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -1,4 +1,4 @@ -/* (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000 +/* (c) Marc Schoenauer, Maarten Keijzer, GeNeura Team, Thales group 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 @@ -14,9 +14,11 @@ 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 - todos@geneura.ugr.es, http://geneura.ugr.es - Marc.Schoenauer@polytechnique.fr - mkeijzer@dhi.dk +Authors: + todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mkeijzer@dhi.dk + Johann Dréo */ @@ -183,6 +185,27 @@ public: eoParam * getParam(const std::string& _name) const; + /** + * Get the value of a param from its long name + * If not found, raise an eoMissingParamException + * + * Remember to specify the expected return type with a templated call: + * unsigned int popSize = eoparser.value("popSize"); + */ + template + ValueType value(const std::string& _name) const + { + eoParam* param = getParam(_name); + + eoValueParam* vparam( + dynamic_cast< eoValueParam* >(param) + ); + + return vparam->value(); + } + + + /** Get or create parameter It seems finally that the easiest use of the above method is From 8c121bdafb95bd3102f297e97211c587e2cdea16 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 17:48:55 +0200 Subject: [PATCH 09/10] added an new exception to handle cases where a wrong template is given to eoParser::valueOf --- eo/src/eoExceptions.h | 26 ++++++++++++++++++++++++++ eo/src/utils/eoParser.cpp | 1 - eo/src/utils/eoParser.h | 18 +++++++++++++----- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/eo/src/eoExceptions.h b/eo/src/eoExceptions.h index d38b6cb1..4d56ddb9 100644 --- a/eo/src/eoExceptions.h +++ b/eo/src/eoExceptions.h @@ -103,4 +103,30 @@ public: private: std::string _name; }; + +/*! + * An error that signals a bad parameter type + * + * Thrown by eoParser::valueOf + * + * @ingroup Parameters + */ +class eoWrongParamTypeException : public std::exception +{ +public: + eoWrongParamTypeException(std::string name) : _name(name){} + + virtual const char* what() const throw() + { + std::ostringstream ss; + ss << "You asked for the parameter " << _name << " but it has not been declared under this type"; + return ss.str().c_str(); + } + + ~eoWrongParamTypeException() throw() {} + +private: + std::string _name; +}; + #endif // __eoExceptions_h__ diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index 7489cf65..cc306bf7 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -40,7 +40,6 @@ Authors: #include #include #include -#include using namespace std; diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index b33f956e..be680ab0 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -32,6 +32,7 @@ Authors: #include "eoParam.h" #include "eoObject.h" #include "eoPersistent.h" +#include "eoExceptions.h" /** Parameter saving and loading @@ -191,17 +192,24 @@ public: * * Remember to specify the expected return type with a templated call: * unsigned int popSize = eoparser.value("popSize"); + * + * If the template type is not the good one, an eoWrongParamTypeException is raised. */ template - ValueType value(const std::string& _name) const + ValueType valueOf(const std::string& _name) const { eoParam* param = getParam(_name); - eoValueParam* vparam( - dynamic_cast< eoValueParam* >(param) - ); + // Note: eoParam is the polymorphic base class of eoValueParam, thus we can do a dynamix cast + eoValueParam* vparam = dynamic_cast< eoValueParam* >(param); - return vparam->value(); + if( vparam == NULL ) { + // if the dynamic cast has failed, chances are that ValueType + // is not the same than the one used at declaration. + throw eoWrongParamTypeException( _name ); + } else { + return vparam->value(); + } } From 28fedd3c517bdc1a87a44372acf46b3e77b1949d Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 17:49:41 +0200 Subject: [PATCH 10/10] added a deprecated pragma message to eoOStreamMonitor --- eo/src/utils/eoOStreamMonitor.h | 1 + 1 file changed, 1 insertion(+) diff --git a/eo/src/utils/eoOStreamMonitor.h b/eo/src/utils/eoOStreamMonitor.h index b09a11b5..cb4c476a 100644 --- a/eo/src/utils/eoOStreamMonitor.h +++ b/eo/src/utils/eoOStreamMonitor.h @@ -49,6 +49,7 @@ public : { (void)_verbose; eo::log << eo::warnings << "WARNING: the use of the verbose parameter in eoOStreamMonitor constructor is deprecated and will be removed in the next release" << std::endl; +#pragma message "WARNING: the use of the verbose parameter in eoOStreamMonitor constructor is deprecated and will be removed in the next release" } eoOStreamMonitor( std::ostream & _out, std::string _delim = "\t", unsigned int _width=20, char _fill=' ' ) :