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 #include <stdexcept>
00016 #include <EO.h>
00017 //-----------------------------------------------------------------------------
00018 
00025 template < class F > class PO:public EO < F >
00026 {
00027 
00028 public:
00029 
00030     typedef typename PO < F >::Fitness Fitness;
00031 
00035     PO ():repFitness (Fitness ()), invalidFitness (true),
00036             bestFitness (Fitness()){}
00037 
00038 
00040     Fitness fitness () const
00041     {
00042         if (invalid ())
00043             throw std::runtime_error ("invalid fitness in PO.h");
00044         return repFitness;
00045     }
00046 
00047 
00051     void fitness (const Fitness & _fitness)
00052     {
00053         repFitness = _fitness;
00054         invalidFitness = false;
00055     }
00056 
00060     Fitness best () const
00061     {
00062         if (invalid ())
00063             throw std::runtime_error ("invalid best fitness in PO.h");
00064         return bestFitness;
00065     }
00066 
00067 
00071     void best (const Fitness & _bestFitness)
00072     {
00073         bestFitness = _bestFitness;
00074         invalidBestFitness = false;
00075     }
00076 
00077 
00081     bool invalid () const
00082     {
00083         return invalidFitness;
00084     }
00085 
00089     void invalidate ()
00090     {
00091         invalidFitness = true;
00092     }
00093 
00097     bool invalidBest () const
00098     {
00099         return invalidBestFitness;
00100     }
00101 
00105     void invalidateBest ()
00106     {
00107         invalidBestFitness = true;
00108     }
00109 
00113     virtual std::string className () const
00114     {
00115         return "PO";
00116     }
00117 
00121     bool operator< (const PO & _po2) const { return fitness () < _po2.fitness ();}
00122     bool operator> (const PO & _po2) const { return !(fitness () <= _po2.fitness ());}
00123 
00128     virtual void printOn(std::ostream& _os) const {
00129 
00130 
00131         // the latest version of the code. Very similar to the old code
00132         if (invalid()) {
00133             _os << "INVALID ";
00134         }
00135         else
00136         {
00137             _os << repFitness  << ' ' ;
00138         }
00139 
00140         if (invalidBest()) {
00141             _os << "INVALID BEST";
00142         }
00143         else
00144         {
00145             _os << "best: " << bestFitness  ;
00146         }
00147 
00148     }
00149 
00158     virtual void readFrom(std::istream& _is) {
00159 
00160         // the new version of the reafFrom function.
00161         // It can distinguish between valid and invalid fitness values.
00162         std::string fitness_str;
00163         int pos = _is.tellg();
00164         _is >> fitness_str;
00165 
00166         if (fitness_str == "INVALID")
00167         {
00168             invalidFitness = true;
00169         }
00170         else
00171         {
00172             invalidFitness = false;
00173             _is.seekg(pos); // rewind
00174             _is >> repFitness;
00175         }
00176     }
00177 
00178 private:
00179     Fitness repFitness;         // value of fitness for this particle
00180     bool invalidFitness;                // true if the value of the fitness is invalid
00181 
00182     Fitness bestFitness;                // value of the best fitness found for the particle
00183     bool invalidBestFitness;    // true if the value of the best fitness is invalid
00184 
00185 };
00186 
00187 //-----------------------------------------------------------------------------
00188 
00189 #endif /*PO_H */

Generated on Fri Jun 22 10:17:02 2007 for EO-PSO by  doxygen 1.4.7