moeoVector.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // moeoVector.h
00005 // (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
00006 /*
00007     This library...
00008 
00009     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
00010  */
00011 //-----------------------------------------------------------------------------
00012 
00013 #ifndef MOEOVECTOR_H_
00014 #define MOEOVECTOR_H_
00015 
00016 #include <iterator>
00017 #include <vector>
00018 #include <core/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 int _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())           // safety check
00059         {
00060             if (size())            // NOT an initial empty std::vector
00061             {
00062                 std::cout << "Warning: Changing size in moeoVector assignation"<<std::endl;
00063                 resize(_v.size());
00064             }
00065             else
00066             {
00067                 throw std::runtime_error("Size not initialized in moeoVector");
00068             }
00069         }
00070         std::copy(_v.begin(), _v.end(), begin());
00071         invalidate();
00072     }
00073 
00074 
00079     bool operator<(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo) const
00080     {
00081         return MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::operator<(_moeo);
00082     }
00083 
00084 
00089     virtual void printOn(std::ostream & _os) const
00090     {
00091         MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::printOn(_os);
00092         _os << ' ';
00093         _os << size() << ' ';
00094         std::copy(begin(), end(), std::ostream_iterator<AtomType>(_os, " "));
00095     }
00096 
00097 
00102     virtual void readFrom(std::istream & _is)
00103     {
00104         MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::readFrom(_is);
00105         unsigned int sz;
00106         _is >> sz;
00107         resize(sz);
00108         unsigned int i;
00109         for (i = 0; i < sz; ++i)
00110         {
00111             AtomType atom;
00112             _is >> atom;
00113             operator[](i) = atom;
00114         }
00115     }
00116 
00117 };
00118 
00119 
00125 template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity, class GeneType >
00126 bool operator<(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo1, const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo2)
00127 {
00128     return _moeo1.operator<(_moeo2);
00129 }
00130 
00131 
00137 template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity, class GeneType >
00138 bool operator>(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo1, const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo2)
00139 {
00140     return _moeo1.operator>(_moeo2);
00141 }
00142 
00143 #endif /*MOEOVECTOR_H_*/

Generated on Tue Jun 26 15:13:04 2007 for ParadisEO-MOEO by  doxygen 1.4.7