Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# 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
    00040
    00043 class eoScalarFitnessAssembledTraits{
    00044
    00045 public:
    00046
    00047   typedef std::vector<std::string>::size_type size_type;
    00048
    00049   static void setDescription( size_type _idx, std::string _descr ) {
    00050     if ( _idx < TermDescriptions.size() )
    00051       TermDescriptions[_idx] = _descr;
    00052     else{
    00053       TermDescriptions.resize(_idx, "Unnamed variable" );
    00054       TermDescriptions[_idx] = _descr;
    00055     }
    00056   }
    00057
    00058   static std::string getDescription( size_type _idx) {
    00059     if ( _idx < TermDescriptions.size() )
    00060       return TermDescriptions[_idx ];
    00061     else
    00062       return "Unnamed Variable";
    00063   }
    00064
    00065   static void resize( size_type _n, const std::string& _descr) {
    00066     TermDescriptions.resize(_n, _descr);
    00067   }
    00068
    00069   static size_type size() { return TermDescriptions.size(); }
    00070
    00071   static std::vector<std::string> getDescriptionVector() { return TermDescriptions; }
    00072
    00073 private:
    00074   static std::vector<std::string> TermDescriptions;
    00075 };
    00076
    00078
    00087 template <class ScalarType, class Compare, class FitnessTraits >
    00088 class eoScalarFitnessAssembled : public std::vector<ScalarType> {
    00089
    00090 public:
    00091
    00092     using std::vector< ScalarType >::empty;
    00093     using std::vector< ScalarType >::front;
    00094     using std::vector< ScalarType >::size;
    00095
    00096
    00097   typedef typename std::vector<ScalarType> baseVector;
    00098   typedef typename baseVector::size_type size_type;
    00099
    00100   // Basic constructors and assignments
    00101   eoScalarFitnessAssembled()
    00102     : baseVector( FitnessTraits::size() ),
    00103       feasible(true), failed(false), msg("")
    00104   {}
    00105
    00106   eoScalarFitnessAssembled( size_type _n,
    00107                             const ScalarType& _val,
    00108                             const std::string& _descr="Unnamed variable" )
    00109     : baseVector(_n, _val),
    00110       feasible(true), failed(false), msg("")
    00111   {
    00112     if ( _n > FitnessTraits::size() )
    00113     FitnessTraits::resize(_n, _descr);
    00114   }
    00115
    00116   eoScalarFitnessAssembled( const eoScalarFitnessAssembled& other)
    00117     : baseVector( other ),
    00118       feasible(other.feasible),
    00119       failed(other.failed),
    00120       msg(other.msg)
    00121   {}
    00122
    00123   eoScalarFitnessAssembled& operator=( const eoScalarFitnessAssembled& other) {
    00124     baseVector::operator=( other );
    00125     feasible = other.feasible;
    00126     failed = other.failed;
    00127     msg = other.msg;
    00128     return *this;
    00129   }
    00130
    00131   // Constructors and assignments to work with scalar type
    00132   eoScalarFitnessAssembled( const ScalarType& v )
    00133     : baseVector( 1, v ),
    00134       feasible(true), failed(false), msg("")
    00135   {}
    00136
    00137   eoScalarFitnessAssembled& operator=( const ScalarType& v ) {
    00138
    00139       if( empty() )
    00140           push_back( v );
    00141       else
    00142           front() = v;
    00143       return *this;
    00144   }
    00145
    00147   void push_back(const ScalarType& _val ){
    00148     baseVector::push_back( _val );
    00149     if ( size() > FitnessTraits::size() )
    00150       FitnessTraits::setDescription( size()-1, "Unnamed variable");
    00151   }
    00152
    00154   void push_back(const ScalarType& _val, const std::string& _descr ){
    00155     baseVector::push_back( _val );
    00156     FitnessTraits::setDescription( size()-1, _descr );
    00157   }
    00158
    00160   void resize( size_type _n, const ScalarType& _val = ScalarType(), const std::string& _descr = "Unnamed variable" ){
    00161     baseVector::resize(_n, _val);
    00162     FitnessTraits::resize(_n, _descr);
    00163   }
    00164
    00166   void setDescription( size_type _idx, std::string _descr ) {
    00167     FitnessTraits::setDescription( _idx, _descr );
    00168   }
    00169
    00171   std::string getDescription( size_type _idx ){ return FitnessTraits::getDescription( _idx ); }
    00172
    00174   std::vector<std::string> getDescriptionVector() { return FitnessTraits::getDescriptionVector(); }
    00175
    00177
    00181   bool feasible;
    00182
    00184
    00188   bool failed;
    00189
    00191
    00195   std::string msg;
    00196
    00197
    00198   // Scalar type access
    00199   operator ScalarType(void) const {
    00200     if ( empty() )
    00201       return 0.0;
    00202     else
    00203       return front();
    00204   }
    00205
    00207   void printAll(std::ostream& os) const {
    00208     for (size_type i=0; i < size(); ++i )
    00209       os << FitnessTraits::getDescription(i) << " = " << operator[](i) << " ";
    00210   }
    00211
    00212   // Comparison, using less by default
    00213   bool operator<(const eoScalarFitnessAssembled& other) const{
    00214     if ( empty() || other.empty() )
    00215       return false;
    00216     else
    00217       return Compare()( front() , other.front() );
    00218   }
    00219
    00220   // implementation of the other operators
    00221   bool operator>( const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const  { return y < *this; }
    00222
    00223   // implementation of the other operators
    00224   bool operator<=( const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const { return !(*this > y); }
    00225
    00226   // implementation of the other operators
    00227   bool operator>=(const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const { return !(*this < y); }
    00228
    00229 };
    00230
    00237 typedef eoScalarFitnessAssembled<double, std::less<double>, eoScalarFitnessAssembledTraits >    eoAssembledMaximizingFitness;
    00238 typedef eoScalarFitnessAssembled<double, std::greater<double>, eoScalarFitnessAssembledTraits > eoAssembledMinimizingFitness;
    00239
    00240 template <class F, class Cmp, class FitnessTraits>
    00241 std::ostream& operator<<(std::ostream& os, const eoScalarFitnessAssembled<F, Cmp, FitnessTraits>& f)
    00242 {
    00243   for (unsigned i=0; i < f.size(); ++i)
    00244     os << f[i] << " ";
    00245
    00246   os << f.feasible << " ";
    00247   os << f.failed << " ";
    00248
    00249   return os;
    00250 }
    00251
    00252 template <class F, class Cmp, class FitnessTraits>
    00253 std::istream& operator>>(std::istream& is, eoScalarFitnessAssembled<F, Cmp, FitnessTraits>& f)
    00254 {
    00255   for (unsigned i=0; i < f.size(); ++i){
    00256     F value;
    00257     is >> value;
    00258     f[i] = value;
    00259   }
    00260
    00261   is >> f.feasible;
    00262   is >> f.failed;
    00263
    00264   return is;
    00265 }
    00266
    00267 #endif
    00268
    00269
    00270