00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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
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
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
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
00221 bool operator<(ScalarType x) const{
00222 eoScalarFitnessAssembled ScalarFitness(x);
00223 return this->operator<(ScalarFitness);
00224 }
00225
00226
00227 bool operator>( const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const { return y < *this; }
00228
00229
00230 bool operator<=( const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const { return !(*this > y); }
00231
00232
00233 bool operator>=(const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const { return !(*this < y); }
00234
00235 };
00236
00243 typedef eoScalarFitnessAssembled<double, std::less<double>, eoScalarFitnessAssembledTraits > eoAssembledMaximizingFitness;
00244 typedef eoScalarFitnessAssembled<double, std::greater<double>, eoScalarFitnessAssembledTraits > eoAssembledMinimizingFitness;
00245
00246 template <class F, class Cmp, class FitnessTraits>
00247 std::ostream& operator<<(std::ostream& os, const eoScalarFitnessAssembled<F, Cmp, FitnessTraits>& f)
00248 {
00249 for (unsigned i=0; i < f.size(); ++i)
00250 os << f[i] << " ";
00251
00252 os << f.feasible << " ";
00253 os << f.failed << " ";
00254
00255 return os;
00256 }
00257
00258 template <class F, class Cmp, class FitnessTraits>
00259 std::istream& operator>>(std::istream& is, eoScalarFitnessAssembled<F, Cmp, FitnessTraits>& f)
00260 {
00261 for (unsigned i=0; i < f.size(); ++i){
00262 F value;
00263 is >> value;
00264 f[i] = value;
00265 }
00266
00267 is >> f.feasible;
00268 is >> f.failed;
00269
00270 return is;
00271 }
00272
00273 #endif
00274
00275
00276