EvolvingObjects
eoScalarFitnessAssembled.h
00001  /* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- */
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoScalarFitnessAssembled.h
00005 // Marc Wintermantel & Oliver Koenig
00006 // IMES-ST@ETHZ.CH
00007 // March 2003
00008 
00009 /*
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2 of the License, or (at your option) any later version.
00014 
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 
00024     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00025              Marc.Schoenauer@inria.fr
00026              mak@dhi.dk
00027 */
00028 //-----------------------------------------------------------------------------
00029 
00030 #ifndef eoScalarFitnessAssembled_h
00031 #define eoScalarFitnessAssembled_h
00032 
00033 #include <functional>
00034 #include <iostream>
00035 #include <stdexcept>
00036 #include <vector>
00037 #include <string>
00038 
00043 
00044 
00047 class eoScalarFitnessAssembledTraits{
00048 
00049 public:
00050 
00051   typedef std::vector<std::string>::size_type size_type;
00052 
00053   static void setDescription( size_type _idx, std::string _descr ) {
00054     if ( _idx < TermDescriptions.size() )
00055       TermDescriptions[_idx] = _descr;
00056     else{
00057       TermDescriptions.resize(_idx, "Unnamed variable" );
00058       TermDescriptions[_idx] = _descr;
00059     }
00060   }
00061 
00062   static std::string getDescription( size_type _idx) {
00063     if ( _idx < TermDescriptions.size() )
00064       return TermDescriptions[_idx ];
00065     else
00066       return "Unnamed Variable";
00067   }
00068 
00069   static void resize( size_type _n, const std::string& _descr) {
00070     TermDescriptions.resize(_n, _descr);
00071   }
00072 
00073   static size_type size() { return TermDescriptions.size(); }
00074 
00075   static std::vector<std::string> getDescriptionVector() { return TermDescriptions; }
00076 
00077 private:
00078   static std::vector<std::string> TermDescriptions;
00079 };
00083 
00084 
00093 template <class ScalarType, class Compare, class FitnessTraits >
00094 class eoScalarFitnessAssembled : public std::vector<ScalarType> {
00095 
00096 public:
00097 
00098     using std::vector< ScalarType >::empty;
00099     using std::vector< ScalarType >::front;
00100     using std::vector< ScalarType >::size;
00101 
00102 
00103   typedef typename std::vector<ScalarType> baseVector;
00104   typedef typename baseVector::size_type size_type;
00105 
00106   // Basic constructors and assignments
00107   eoScalarFitnessAssembled()
00108     : baseVector( FitnessTraits::size() ),
00109       feasible(true), failed(false), msg("")
00110   {}
00111 
00112   eoScalarFitnessAssembled( size_type _n,
00113                             const ScalarType& _val,
00114                             const std::string& _descr="Unnamed variable" )
00115     : baseVector(_n, _val),
00116       feasible(true), failed(false), msg("")
00117   {
00118     if ( _n > FitnessTraits::size() )
00119     FitnessTraits::resize(_n, _descr);
00120   }
00121 
00122   eoScalarFitnessAssembled( const eoScalarFitnessAssembled& other)
00123     : baseVector( other ),
00124       feasible(other.feasible),
00125       failed(other.failed),
00126       msg(other.msg)
00127   {}
00128 
00129   eoScalarFitnessAssembled& operator=( const eoScalarFitnessAssembled& other) {
00130     baseVector::operator=( other );
00131     feasible = other.feasible;
00132     failed = other.failed;
00133     msg = other.msg;
00134     return *this;
00135   }
00136 
00137   // Constructors and assignments to work with scalar type
00138   eoScalarFitnessAssembled( const ScalarType& v )
00139     : baseVector( 1, v ),
00140       feasible(true), failed(false), msg("")
00141   {}
00142 
00143   eoScalarFitnessAssembled& operator=( const ScalarType& v ) {
00144 
00145       if( empty() )
00146           push_back( v );
00147       else
00148           front() = v;
00149       return *this;
00150   }
00151 
00153   void push_back(const ScalarType& _val ){
00154     baseVector::push_back( _val );
00155     if ( size() > FitnessTraits::size() )
00156       FitnessTraits::setDescription( size()-1, "Unnamed variable");
00157   }
00158 
00160   void push_back(const ScalarType& _val, const std::string& _descr ){
00161     baseVector::push_back( _val );
00162     FitnessTraits::setDescription( size()-1, _descr );
00163   }
00164 
00166   void resize( size_type _n, const ScalarType& _val = ScalarType(), const std::string& _descr = "Unnamed variable" ){
00167     baseVector::resize(_n, _val);
00168     FitnessTraits::resize(_n, _descr);
00169   }
00170 
00172   void setDescription( size_type _idx, std::string _descr ) {
00173     FitnessTraits::setDescription( _idx, _descr );
00174   }
00175 
00177   std::string getDescription( size_type _idx ){ return FitnessTraits::getDescription( _idx ); }
00178 
00180   std::vector<std::string> getDescriptionVector() { return FitnessTraits::getDescriptionVector(); }
00181 
00183 
00187   bool feasible;
00188 
00190 
00194   bool failed;
00195 
00197 
00201   std::string msg;
00202 
00203 
00204   // Scalar type access
00205   operator ScalarType(void) const {
00206     if ( empty() )
00207       return 0.0;
00208     else
00209       return front();
00210   }
00211 
00213   void printAll(std::ostream& os) const {
00214     for (size_type i=0; i < size(); ++i )
00215       os << FitnessTraits::getDescription(i) << " = " << this->operator[](i) << " ";
00216   }
00217 
00219   bool operator<(const eoScalarFitnessAssembled& other) const{
00220     if ( empty() || other.empty() )
00221       return false;
00222     else
00223       return Compare()( front() , other.front() );
00224   }
00225 
00227   bool operator<(ScalarType x) const{
00228         eoScalarFitnessAssembled ScalarFitness(x);
00229         return this->operator<(ScalarFitness);
00230   }
00231 
00232   // implementation of the other operators
00233   bool operator>( const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const  { return y < *this; }
00234 
00235   // implementation of the other operators
00236   bool operator<=( const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const { return !(*this > y); }
00237 
00238   // implementation of the other operators
00239   bool operator>=(const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const { return !(*this < y); }
00240 
00241 };
00253 typedef eoScalarFitnessAssembled<double, std::less<double>, eoScalarFitnessAssembledTraits >    eoAssembledMaximizingFitness;
00254 typedef eoScalarFitnessAssembled<double, std::greater<double>, eoScalarFitnessAssembledTraits > eoAssembledMinimizingFitness;
00255 
00256 template <class F, class Cmp, class FitnessTraits>
00257 std::ostream& operator<<(std::ostream& os, const eoScalarFitnessAssembled<F, Cmp, FitnessTraits>& f)
00258 {
00259   for (unsigned i=0; i < f.size(); ++i)
00260     os << f[i] << " ";
00261 
00262   os << f.feasible << " ";
00263   os << f.failed << " ";
00264 
00265   return os;
00266 }
00267 
00268 template <class F, class Cmp, class FitnessTraits>
00269 std::istream& operator>>(std::istream& is, eoScalarFitnessAssembled<F, Cmp, FitnessTraits>& f)
00270 {
00271   for (unsigned i=0; i < f.size(); ++i){
00272     F value;
00273     is >> value;
00274     f[i] = value;
00275   }
00276 
00277   is >> f.feasible;
00278   is >> f.failed;
00279 
00280   return is;
00281 }
00282 
00284 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends