moeoObjectiveVector.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // moeoObjectiveVector.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 MOEOOBJECTIVEVECTOR_H_
00014 #define MOEOOBJECTIVEVECTOR_H_
00015 
00016 #include <iostream>
00017 #include <math.h>
00018 #include <vector>
00019 #include <moeoObjectiveVectorComparator.h>
00020 
00027 template < class ObjectiveVectorTraits >
00028 class moeoObjectiveVector
00029 {
00030 public:
00031 
00033     typedef ObjectiveVectorTraits Traits;
00034 
00035 
00041     static void setup(unsigned _nObjectives, std::vector < bool > & _bObjectives)
00042     {
00043         ObjectiveVectorTraits::setup(_nObjectives, _bObjectives);
00044     }
00045 
00046 
00050     static unsigned nObjectives()
00051     {
00052         return ObjectiveVectorTraits::nObjectives();
00053     }
00054 
00055 
00060     static bool minimizing(unsigned _i) {
00061         return ObjectiveVectorTraits::minimizing(_i);
00062     }
00063 
00064 
00069     static bool maximizing(unsigned _i) {
00070         return ObjectiveVectorTraits::maximizing(_i);
00071     }
00072 
00073 };
00074 
00075 
00080 template < class ObjectiveVectorTraits >
00081 class moeoObjectiveVectorDouble : public moeoObjectiveVector < ObjectiveVectorTraits >, public std::vector < double >
00082 {
00083 public:
00084 
00085     using std::vector< double >::size;
00086     using std::vector< double >::operator[];
00087 
00091     moeoObjectiveVectorDouble() : std::vector < double > (ObjectiveVectorTraits::nObjectives(), 0.0) {}
00092 
00093 
00098     moeoObjectiveVectorDouble(std::vector <double> & _v) : std::vector < double > (_v) {}
00099 
00100 
00106     bool dominates(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const
00107     {
00108         moeoParetoObjectiveVectorComparator < moeoObjectiveVectorDouble<ObjectiveVectorTraits> > comparator;
00109         return comparator(*this, _other);
00110     }
00111 
00112 
00117     bool operator==(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const
00118     {
00119         for (unsigned i=0; i < size(); i++)
00120         {
00121             if ( fabs(operator[](i) - _other[i]) > ObjectiveVectorTraits::tolerance() )
00122             {
00123                 return false;
00124             }
00125         }
00126         return true;
00127     }
00128 
00129 
00134     bool operator!=(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const
00135     {
00136         return ! operator==(_other);
00137     }
00138 
00139 
00145     bool operator<(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const
00146     {
00147         for (unsigned i=0; i < size(); i++)
00148         {
00149             if ( fabs(operator[](i) - _other[i]) > ObjectiveVectorTraits::tolerance() )
00150             {
00151                 if (operator[](i) < _other[i])
00152                 {
00153                     return true;
00154                 }
00155                 else
00156                 {
00157                     return false;
00158                 }
00159             }
00160         }
00161         return false;
00162     }
00163 
00164 
00170     bool operator>(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const
00171     {
00172         return _other < *this;
00173     }
00174 
00175 
00181     bool operator<=(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const
00182     {
00183         return operator==(_other) || operator<(_other);
00184     }
00185 
00186 
00192     bool operator>=(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const
00193     {
00194         return operator==(_other) || operator>(_other);
00195     }
00196 
00197 };
00198 
00199 
00205 template < class ObjectiveVectorTraits >
00206 std::ostream & operator<<(std::ostream & _os, const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _objectiveVector)
00207 {
00208     for (unsigned i=0; i<_objectiveVector.size(); i++)
00209     {
00210         _os << _objectiveVector[i] << '\t';
00211     }
00212     return _os;
00213 }
00214 
00220 template < class ObjectiveVectorTraits >
00221 std::istream & operator>>(std::istream & _is, moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _objectiveVector)
00222 {
00223     _objectiveVector = moeoObjectiveVectorDouble < ObjectiveVectorTraits > ();
00224     for (unsigned i=0; i<_objectiveVector.size(); i++)
00225     {
00226         _is >> _objectiveVector[i];
00227     }
00228     return _is;
00229 }
00230 
00231 #endif /*MOEOOBJECTIVEVECTOR_H_*/

Generated on Tue Apr 17 16:53:21 2007 for ParadisEO-MOEO by  doxygen 1.5.1