diff --git a/cmake/Config.cmake b/cmake/Config.cmake index c0576397f..8b08ff1c0 100644 --- a/cmake/Config.cmake +++ b/cmake/Config.cmake @@ -31,11 +31,11 @@ endif() ### 0) Define general CXX flags for DEBUG and RELEASE ###################################################################################### -if(DEBUG) - set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "" FORCE) -else(DEBUG) - set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE) -endif(DEBUG) +#if(DEBUG) +# set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "" FORCE) +#else(DEBUG) +# set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE) +#endif(DEBUG) add_definitions(-DDEPRECATED_MESSAGES) set(CMAKE_CXX_FLAGS_DEBUG "-Wunknown-pragmas -O0 -g -Wall -Wextra -ansi -pedantic" CACHE STRING "" FORCE) diff --git a/edo/src/edo b/edo/src/edo index 816ad4211..1668a2ee2 100644 --- a/edo/src/edo +++ b/edo/src/edo @@ -88,6 +88,8 @@ Authors: #include "utils/edoFileSnapshot.h" #include "utils/edoPopStat.h" +#include "edoTransform.h" + #endif // !_edo_ // Local Variables: diff --git a/eo/CMakeLists.txt b/eo/CMakeLists.txt index 9b6962f2c..9966de557 100644 --- a/eo/CMakeLists.txt +++ b/eo/CMakeLists.txt @@ -21,6 +21,8 @@ endif(ENABLE_OPENMP) if(ENABLE_GNUPLOT) include(FindGnuplot) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_GNUPLOT -DGNUPLOT_PROGRAM=\\\"${GNUPLOT_EXECUTABLE}\\\"") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_GNUPLOT -DGNUPLOT_PROGRAM=\\\"${GNUPLOT_EXECUTABLE}\\\"") endif(ENABLE_GNUPLOT) # set a special flag if the environment is windows (should do the same in a config.g file) diff --git a/eo/src/serial/CMakeLists.txt b/eo/src/serial/CMakeLists.txt index 43d1ce451..efc9f42e2 100644 --- a/eo/src/serial/CMakeLists.txt +++ b/eo/src/serial/CMakeLists.txt @@ -13,10 +13,10 @@ set(EOSERIAL_LIB_OUTPUT_PATH ${EO_BIN_DIR}/lib) set(LIBRARY_OUTPUT_PATH ${EOSERIAL_LIB_OUTPUT_PATH}) set(EOSERIAL_SOURCES - Array.cpp - Object.cpp + SerialArray.cpp + SerialObject.cpp Parser.cpp - String.cpp + SerialString.cpp ) add_library(eoserial STATIC ${EOSERIAL_SOURCES}) diff --git a/eo/src/serial/Parser.cpp b/eo/src/serial/Parser.cpp index 57b52a0dd..e78b35392 100644 --- a/eo/src/serial/Parser.cpp +++ b/eo/src/serial/Parser.cpp @@ -23,9 +23,9 @@ Authors: # include "Parser.h" -# include "Array.h" -# include "Object.h" -# include "String.h" +# include "SerialArray.h" +# include "SerialObject.h" +# include "SerialString.h" // in debug mode only // # define DEBUG(x) std::cout << x << std::endl; diff --git a/eo/src/serial/Parser.h b/eo/src/serial/Parser.h index 20f6a1bb2..392f25ffa 100644 --- a/eo/src/serial/Parser.h +++ b/eo/src/serial/Parser.h @@ -23,8 +23,8 @@ Authors: # define __EOSERIAL_PARSER_H__ # include "Entity.h" -# include "String.h" -# include "Object.h" +# include "SerialString.h" +# include "SerialObject.h" /** * @file Parser.h diff --git a/eo/src/serial/Array.cpp b/eo/src/serial/SerialArray.cpp similarity index 98% rename from eo/src/serial/Array.cpp rename to eo/src/serial/SerialArray.cpp index 180aad16b..5282370f5 100644 --- a/eo/src/serial/Array.cpp +++ b/eo/src/serial/SerialArray.cpp @@ -19,7 +19,7 @@ Contact: http://eodev.sourceforge.net Authors: Benjamin Bouvier */ -# include "Array.h" +# include "SerialArray.h" namespace eoserial { diff --git a/eo/src/serial/Array.h b/eo/src/serial/SerialArray.h similarity index 99% rename from eo/src/serial/Array.h rename to eo/src/serial/SerialArray.h index d453add99..b349953aa 100644 --- a/eo/src/serial/Array.h +++ b/eo/src/serial/SerialArray.h @@ -26,7 +26,7 @@ Authors: # include "Entity.h" # include "Serializable.h" -# include "Object.h" +# include "SerialObject.h" namespace eoserial { diff --git a/eo/src/serial/Object.cpp b/eo/src/serial/SerialObject.cpp similarity index 98% rename from eo/src/serial/Object.cpp rename to eo/src/serial/SerialObject.cpp index dd859052e..e926e448f 100644 --- a/eo/src/serial/Object.cpp +++ b/eo/src/serial/SerialObject.cpp @@ -19,7 +19,7 @@ Contact: http://eodev.sourceforge.net Authors: Benjamin Bouvier */ -# include "Object.h" +# include "SerialObject.h" using namespace eoserial; diff --git a/eo/src/serial/Object.h b/eo/src/serial/SerialObject.h similarity index 100% rename from eo/src/serial/Object.h rename to eo/src/serial/SerialObject.h diff --git a/eo/src/serial/String.cpp b/eo/src/serial/SerialString.cpp similarity index 97% rename from eo/src/serial/String.cpp rename to eo/src/serial/SerialString.cpp index c50882786..6d49106cc 100644 --- a/eo/src/serial/String.cpp +++ b/eo/src/serial/SerialString.cpp @@ -19,7 +19,7 @@ Contact: http://eodev.sourceforge.net Authors: Benjamin Bouvier */ -# include "String.h" +# include "SerialString.h" namespace eoserial { diff --git a/eo/src/serial/String.h b/eo/src/serial/SerialString.h similarity index 93% rename from eo/src/serial/String.h rename to eo/src/serial/SerialString.h index 526cab365..2ef1f94f5 100644 --- a/eo/src/serial/String.h +++ b/eo/src/serial/SerialString.h @@ -62,7 +62,7 @@ namespace eoserial * @param value The value in which we're writing. */ template - inline void deserialize( T & value ); + inline void deserialize( T & value ) const; protected: // Copy and reaffectation are forbidden @@ -80,7 +80,7 @@ namespace eoserial * invoking. */ template - inline void String::deserialize( T & value ) + inline void String::deserialize( T & value ) const { std::stringstream ss; ss.precision(std::numeric_limits::digits10 + 1); @@ -93,7 +93,7 @@ namespace eoserial * a stringstream. */ template<> - inline void String::deserialize( std::string & value ) + inline void String::deserialize( std::string & value ) const { value = *this; } diff --git a/eo/src/serial/Traits.h b/eo/src/serial/Traits.h new file mode 100644 index 000000000..7cce0959e --- /dev/null +++ b/eo/src/serial/Traits.h @@ -0,0 +1,58 @@ +/* +(c) Benjamin Bouvier, 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: + Benjamin Bouvier +*/ +# ifndef __EOSERIAL_TRAITS_H__ +# define __EOSERIAL_TRAITS_H__ + +/** + * @file Traits.h + * + * Traits used for serialization purposes. + * + * @author Benjamin Bouvier + */ + +namespace eoserial +{ + + /** + * @brief Trait to know whether Derived is derived from Base or not. + * + * To know whether A is derived from B, just test the boolean IsDerivedFrom::value. + * + * @see http://www.gotw.ca/publications/mxc++-item-4.htm + */ + template + class IsDerivedFrom + { + struct no{}; + struct yes{ no _[2]; }; + + static yes Test( Base* something ); + static no Test( ... ); + + public: + enum { value = sizeof( Test( static_cast(0) ) ) == sizeof(yes) }; + }; + +} + +# endif // __EOSERIAL_TRAITS_H__ diff --git a/eo/src/serial/Utils.h b/eo/src/serial/Utils.h index 33172a747..b6ea360b3 100644 --- a/eo/src/serial/Utils.h +++ b/eo/src/serial/Utils.h @@ -22,31 +22,353 @@ Authors: # ifndef __EOSERIAL_UTILS_H__ # define __EOSERIAL_UTILS_H__ -# include "Array.h" -# include "Object.h" -# include "String.h" +# include "SerialArray.h" +# include "SerialObject.h" +# include "SerialString.h" + +# include "Traits.h" + +# include +# include + +/** + * @file Utils.h + * + * @brief Contains utilities for simple serialization and deserialization. + * + * @todo encapsulate implementations. + * + * Example + * + * @code +# include +# include +# include + +# include "eoSerial.h" + +struct SimpleObject: public eoserial::Persistent +{ + public: + + SimpleObject( int v ) : value(v) + { + // empty + } + + eoserial::Object* pack() const + { + eoserial::Object* obj = new eoserial::Object; + (*obj)["value"] = eoserial::pack( value ); + return obj; + } + + void unpack( const eoserial::Object* json ) + { + eoserial::unpack( *json, "value", value ); + } + + int value; +}; + +int main() +{ + eoserial::Object o; + + std::cout << "packing..." << std::endl; + // directly pack raw types + o["long"] = eoserial::pack(123456L); + o["bool"] = eoserial::pack(true); + o["double"] = eoserial::pack(3.141592653); + o["float"] = eoserial::pack(3.141592653f); + + std::string str = "Hello, world!"; + o["str"] = eoserial::pack( str ); + + // pack objects the same way + SimpleObject obj(42); + o["obj"] = eoserial::pack( obj ); + + // pack vector and list the same way + std::vector vec; + vec.push_back(1); + vec.push_back(3); + vec.push_back(3); + vec.push_back(7); + o["vec"] = eoserial::pack( vec ); + + std::map str2int; + str2int["one"] = 1; + str2int["two"] = 2; + str2int["answer"] = 42; + o["map"] = eoserial::pack( str2int ); + + // print it + o.print( std::cout ); + + std::cout << "unpacking..." << std::endl; + + // unpack as easily raw types + long oneTwoThreeFourFiveSix = 0L; + eoserial::unpack( o, "long", oneTwoThreeFourFiveSix); + std::cout << "the long: " << oneTwoThreeFourFiveSix << std::endl; + + // since vec is encoded as an internal eoserial::Array, it can be + // decoded into a std::vector or a std::list without difference. + std::list lis; + eoserial::unpack( o, "vec", lis ); + + std::cout << "the list: "; + for( auto it = lis.begin(), end = lis.end(); it != end; ++it) + { + std::cout << *it << ';'; + } + std::cout << std::endl; + + std::map< std::string, int > readMap; + eoserial::unpack( o, "map", readMap ); + std::cout << "The answer is " << readMap["answer"] << std::endl; + + obj.value = -1; + // unpack object the same way + eoserial::unpack( o, "obj", obj ); + std::cout << "obj.value = " << obj.value << std::endl; + + return 0; +} + +@endcode + * + * @author Benjamin Bouvier + */ namespace eoserial { - /* *************************** - * DESERIALIZATION FUNCTIONS * - ***************************** + /* ***************** + * DESERIALIZATION * + ******************/ + + /** + * @brief Recursively unpack elements of an object which implements push_back. + */ + template< class T > + inline void unpackBasePushBack( const Entity* obj, T& container ) + { + const Array* arr = static_cast( obj ); + for( auto it = arr->begin(), end = arr->end(); + it != end; + ++it ) + { + typename T::value_type item; + unpackBase( *it, item ); + container.push_back( item ); + } + } + + /** + * @brief Unpack method for std::vector + */ + template< class T > + inline void unpackBase( const Entity* obj, std::vector& v ) + { + unpackBasePushBack( obj, v ); + } + + /** + * @brief Unpack method for std::list + */ + template< class T > + inline void unpackBase( const Entity* obj, std::list& l ) + { + unpackBasePushBack( obj, l ); + } + + /** + * @brief Unpack method for std::map< std::string, T > + */ + template< class T > + inline void unpackBase( const Entity* entity, std::map & m ) + { + const Object* obj = static_cast< const Object* >( entity ); + for( auto it = obj->begin(), end = obj->end(); + it != end; + ++it ) + { + unpackBase( it->second, m[ it->first ] ); + } + } + + /** + * @brief Unpack implementation for non eoserial::Persistent objects. + * + * This implementation is being used for every objects that can be transmitted + * to a std::ostream (i.e. which implements the operator <<) + */ + template + struct UnpackImpl + { + void operator()( const Entity* obj, T& value ) + { + static_cast( obj )->deserialize( value ); + } + }; + + /** + * @brief Unpack implementation for eoserial::Persistent objects. + */ + template + struct UnpackImpl + { + void operator()( const Entity* obj, T& value ) + { + value.unpack( static_cast(obj) ); + } + }; + + /** + * @brief Unpack helper for determining which implementation to use. + * + * The trick comes from Herb Sutter: IsDerivedFrom::value is + * true if and only if T inherits from Persistent. In this case, it's equal + * to 1, thus the partial specialization of UnpackImpl is used. In the other + * case, it's equal to 0 and the generic implementation is used. + */ + template + inline void unpackBase( const Entity* obj, T& value ) + { + UnpackImpl< T, IsDerivedFrom< T, Persistent >::value > impl; + impl( obj, value ); + } + + /** + * @brief Universal unpack method. + * + * @param obj The eoserial::object containing the value to deserialize. + * @param key Name of the field to deserialize + * @param value The object in which we'll store the deserialized value. + */ + template + inline void unpack( const Object& obj, const std::string& key, T& value ) + { + unpackBase( obj.find(key)->second, value ); + } + + /* ******************* + * SERIALIZATION ***** + ********************/ + + /** + * @brief Pack implementation for non eoserial::Printable objects. + * + * This implementation is being used for every objects that can be transmitted + * to a std::istream (i.e. which implements the operator >>) + */ + template + struct PackImpl + { + Entity* operator()( const T& value ) + { + std::stringstream ss; + ss.precision(std::numeric_limits::digits10 + 1); + ss << value; + return new String(ss.str()); + } + }; + + /** + * @brief Pack implementation for eoserial::Printable objects. + */ + template + struct PackImpl + { + Entity* operator()( const T& value ) + { + return value.pack(); + } + }; + + // Pre declaration for being usable in packIterable. + template + inline Entity* pack( const T& value ); + + /** + * @brief Pack method for iterable (begin, end) objects. + */ + template + inline Entity* packIterable( const T& container ) + { + Array* arr = new Array; + for( auto it = container.begin(), end = container.end(); + it != end; + ++it ) + { + arr->push_back( pack(*it) ); + } + return arr; + } + + /** + * @brief Pack method for std::vector + */ + template + inline Entity* pack( const std::vector& v ) + { + return packIterable( v ); + } + + /** + * @brief Pack method for std::list + */ + template + inline Entity* pack( const std::list& l ) + { + return packIterable( l ); + } + + /** + * @brief Pack method for std::map< std::string, T > + */ + template + inline Entity* pack( const std::map& map ) + { + Object* obj = new Object; + for( auto it = map.begin(), end = map.end(); + it != end; + ++it ) + { + (*obj)[ it->first ] = pack( it->second ); + } + return obj; + } + + /** + * @brief Universal pack method. + * + * @see unpackBase to understand the trick with the implementation. + * + * @param value Variable to store into an entity. + * @return An entity to store into an object. + */ + template + inline Entity* pack( const T& value ) + { + PackImpl::value> impl; + return impl( value ); + } + + /** ************** + * OLD FUNCTIONS * + **************** These functions are useful for casting eoserial::objects into simple, primitive variables or into class instance which implement eoserial::Persistent. - The model is always quite the same : + The model is always quite the same : - the first argument is the containing object (which is a eoserial::Entity, an object or an array) - the second argument is the key or index, - the last argument is the value in which we're writing. + */ - - template< class T > - inline void unpack( const Object & obj, const std::string & key, T & value ) - { - static_cast( obj.find( key )->second )->deserialize( value ); - } - inline void unpackObject( const Object & obj, const std::string & key, Persistent & value ) { static_cast( obj.find( key )->second )->deserialize( value ); diff --git a/eo/src/serial/eoSerial.h b/eo/src/serial/eoSerial.h index 55a116f0c..fec5fb4dd 100644 --- a/eo/src/serial/eoSerial.h +++ b/eo/src/serial/eoSerial.h @@ -22,11 +22,10 @@ Authors: # ifndef __EOSERIAL_HEADERS__ # define __EOSERIAL_HEADERS__ -# include "Object.h" # include "Serializable.h" -# include "Array.h" -# include "Object.h" -# include "String.h" +# include "SerialArray.h" +# include "SerialObject.h" +# include "SerialString.h" # include "Parser.h" # include "Utils.h" diff --git a/eo/src/utils/eoRealBounds.h b/eo/src/utils/eoRealBounds.h index 47c061005..a41646d47 100644 --- a/eo/src/utils/eoRealBounds.h +++ b/eo/src/utils/eoRealBounds.h @@ -27,6 +27,7 @@ #ifndef _eoRealBounds_h #define _eoRealBounds_h +#include #include // std::exceptions! #include @@ -226,10 +227,14 @@ public : */ eoRealInterval(double _min=0, double _max=1) : repMinimum(_min), repMaximum(_max), repRange(_max-_min) - { - if (repRange<=0) - throw std::logic_error("Void range in eoRealBounds"); - } + { + assert( repRange >= 0 ); +#ifndef NDEBUG + if( repRange == 0 ) { + eo::log << eo::warnings << "Null range in eoRealBounds (min=" << _min << ", max=" << _max << ")" << std::endl; + } +#endif + } // accessors virtual double minimum() const { return repMinimum; } diff --git a/moeo/src/metric/moeoHyperVolumeDifferenceMetric.h b/moeo/src/metric/moeoHyperVolumeDifferenceMetric.h index 3e222e21b..906923b98 100644 --- a/moeo/src/metric/moeoHyperVolumeDifferenceMetric.h +++ b/moeo/src/metric/moeoHyperVolumeDifferenceMetric.h @@ -136,27 +136,36 @@ class moeoHyperVolumeDifferenceMetric : public moeoVectorVsVectorBinaryMetric < * @param _set1 the vector contains all objective Vector of the first pareto front * @param _set2 the vector contains all objective Vector of the second pareto front */ - void setup(const std::vector < ObjectiveVector > & _set1, const std::vector < ObjectiveVector > & _set2){ - if(_set1.size() < 1 || _set2.size() < 1) - throw("Error in moeoHyperVolumeUnaryMetric::setup -> argument1: vector size must be greater than 0"); - else{ - double min, max; - unsigned int nbObj=ObjectiveVector::Traits::nObjectives(); - bounds.resize(nbObj); - for (unsigned int i=0; i & _set1, const std::vector < ObjectiveVector > & _set2) + { + if(_set1.size() < 1 || _set2.size() < 1) { + throw("Error in moeoHyperVolumeUnaryMetric::setup -> argument1: vector size must be greater than 0"); + } else { +#ifndef NDEBUG + if( _set1.size() == 1 || _set2.size() == 1 ) { + eo::log << eo::warnings << "Warning in moeoHyperVolumeUnaryMetric::setup one of the pareto set contains only one point (set1.size=" + << _set1.size() << ", set2.size=" << _set2.size() << ")" + << std::endl; + } +#endif + + double min, max; + unsigned int nbObj=ObjectiveVector::Traits::nObjectives(); + bounds.resize(nbObj); + for (unsigned int i=0; i(), rho(_rho) { // not-a-maximization problem check for (unsigned int i=0; i