PO.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // PO.h
00005 // (c) OPAC 2007
00006 /*
00007     Contact: paradiseo-help@lists.gforge.inria.fr
00008  */
00009 //-----------------------------------------------------------------------------
00010 
00011 #ifndef PO_H
00012 #define PO_H
00013 
00014 //-----------------------------------------------------------------------------
00015 
00016 #include <stdexcept>            // std::runtime_error
00017 #include <EO.h>
00018 //-----------------------------------------------------------------------------
00019 
00026 template < class F > class PO:public EO < F >
00027 {
00028 
00029 public:
00030 
00031     typedef typename PO < F >::Fitness Fitness;
00032 
00036     PO ():repFitness (Fitness ()), invalidFitness (true),
00037             bestFitness (Fitness()){}
00038 
00039 
00041     Fitness fitness () const
00042     {
00043         if (invalid ())
00044             throw std::runtime_error ("invalid fitness in PO.h");
00045         return repFitness;
00046     }
00047 
00048 
00052     void fitness (const Fitness & _fitness)
00053     {
00054         repFitness = _fitness;
00055         invalidFitness = false;
00056     }
00057 
00061     Fitness best () const
00062     {
00063         if (invalid ())
00064             throw std::runtime_error ("invalid best fitness in PO.h");
00065         return bestFitness;
00066     }
00067 
00068 
00072     void best (const Fitness & _bestFitness)
00073     {
00074         bestFitness = _bestFitness;
00075         invalidBestFitness = false;
00076     }
00077 
00078 
00082     bool invalid () const
00083     {
00084         return invalidFitness;
00085     }
00086 
00090     void invalidate ()
00091     {
00092         invalidFitness = true;
00093     }
00094 
00098     bool invalidBest () const
00099     {
00100         return invalidBestFitness;
00101     }
00102 
00106     void invalidateBest ()
00107     {
00108         invalidBestFitness = true;
00109     }
00110 
00114     virtual std::string className () const
00115     {
00116         return "PO";
00117     }
00118 
00122     bool operator< (const PO & _po2) const { return fitness () < _po2.fitness ();}
00123     bool operator> (const PO & _po2) const { return !(fitness () <= _po2.fitness ());}
00124 
00129     virtual void printOn(std::ostream& _os) const {
00130 
00131 
00132         // the latest version of the code. Very similar to the old code
00133         if (invalid()) {
00134             _os << "INVALID ";
00135         }
00136         else
00137         {
00138             _os << repFitness  << ' ' ;
00139         }
00140 
00141         if (invalidBest()) {
00142             _os << "INVALID BEST";
00143         }
00144         else
00145         {
00146             _os << "best: " << bestFitness  ;
00147         }
00148 
00149     }
00150 
00159     virtual void readFrom(std::istream& _is) {
00160 
00161         // the new version of the reafFrom function.
00162         // It can distinguish between valid and invalid fitness values.
00163         std::string fitness_str;
00164         int pos = _is.tellg();
00165         _is >> fitness_str;
00166 
00167         if (fitness_str == "INVALID")
00168         {
00169             invalidFitness = true;
00170         }
00171         else
00172         {
00173             invalidFitness = false;
00174             _is.seekg(pos); // rewind
00175             _is >> repFitness;
00176         }
00177     }
00178 
00179 private:
00180     Fitness repFitness;         // value of fitness for this particle
00181     bool invalidFitness;                // true if the value of the fitness is invalid
00182 
00183     Fitness bestFitness;                // value of the best fitness found for the particle
00184     bool invalidBestFitness;    // true if the value of the best fitness is invalid
00185 
00186 };
00187 
00188 //-----------------------------------------------------------------------------
00189 
00190 #endif /*PO_H */

Generated on Thu Apr 19 11:02:29 2007 for EO by  doxygen 1.4.7