00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef MOEOVECTOR_H_
00014 #define MOEOVECTOR_H_
00015
00016 #include <vector>
00017 #include <iterator>
00018 #include <MOEO.h>
00019
00024 template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity, class GeneType >
00025 class moeoVector : public MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >, public std::vector < GeneType >
00026 {
00027 public:
00028
00029 using MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity > :: invalidate;
00030 using std::vector < GeneType > :: operator[];
00031 using std::vector < GeneType > :: begin;
00032 using std::vector < GeneType > :: end;
00033 using std::vector < GeneType > :: resize;
00034 using std::vector < GeneType > :: size;
00035
00037 typedef GeneType AtomType;
00039 typedef std::vector < GeneType > ContainerType;
00040
00041
00047 moeoVector(unsigned _size = 0, GeneType _value = GeneType()) :
00048 MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >(), std::vector<GeneType>(_size, _value)
00049 {}
00050
00051
00056 void value(const std::vector < GeneType > & _v)
00057 {
00058 if (_v.size() != size())
00059 {
00060 if (size())
00061 {
00062 std::cout << "Warning: Changing size in moeoVector assignation"<<std::endl;
00063 resize(_v.size());
00064 }
00065 }
00066 std::copy(_v.begin(), _v.end(), begin());
00067 invalidate();
00068 }
00069
00070
00075 bool operator<(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo) const
00076 {
00077 return MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::operator<(_moeo);
00078 }
00079
00080
00085 virtual void printOn(std::ostream & _os) const
00086 {
00087 MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::printOn(_os);
00088 _os << ' ';
00089 _os << size() << ' ';
00090 std::copy(begin(), end(), std::ostream_iterator<AtomType>(_os, " "));
00091 }
00092
00093
00098 virtual void readFrom(std::istream & _is)
00099 {
00100 MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::readFrom(_is);
00101 unsigned sz;
00102 _is >> sz;
00103 resize(sz);
00104 unsigned i;
00105 for (i = 0; i < sz; ++i)
00106 {
00107 AtomType atom;
00108 _is >> atom;
00109 operator[](i) = atom;
00110 }
00111 }
00112
00113 };
00114
00115
00121 template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity, class GeneType >
00122 bool operator<(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo1, const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo2)
00123 {
00124 return _moeo1.operator<(_moeo2);
00125 }
00126
00127
00133 template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity, class GeneType >
00134 bool operator>(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo1, const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo2)
00135 {
00136 return _moeo1.operator>(_moeo2);
00137 }
00138
00139
00143 template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity >
00144 class moeoRealVector : public moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, double >
00145 {
00146 public:
00147
00153 moeoRealVector(unsigned _size = 0, double _value = 0.0) : moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, double >(_size, _value)
00154 {}
00155
00156 };
00157
00158
00162 template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity >
00163 class moeoBitVector : public moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool >
00164 {
00165 public:
00166
00167 using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: begin;
00168 using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: end;
00169 using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: resize;
00170 using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: size;
00171
00172
00178 moeoBitVector(unsigned _size = 0, bool _value = false) : moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool >(_size, _value)
00179 {}
00180
00181
00186 virtual void printOn(std::ostream & _os) const
00187 {
00188 MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::printOn(_os);
00189 _os << ' ';
00190 _os << size() << ' ';
00191 std::copy(begin(), end(), std::ostream_iterator<bool>(_os));
00192 }
00193
00194
00199 virtual void readFrom(std::istream & _is)
00200 {
00201 MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::readFrom(_is);
00202 unsigned s;
00203 _is >> s;
00204 std::string bits;
00205 _is >> bits;
00206 if (_is)
00207 {
00208 resize(bits.size());
00209 std::transform(bits.begin(), bits.end(), begin(), std::bind2nd(std::equal_to<char>(), '1'));
00210 }
00211 }
00212
00213 };
00214
00215
00216 #endif