EvolvingObjects
|
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 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 Contact: thomas.legrand@lifl.fr 00022 */ 00023 //----------------------------------------------------------------------------- 00024 00025 #ifndef PO_H 00026 #define PO_H 00027 00028 //----------------------------------------------------------------------------- 00029 #include <stdexcept> 00030 #include <EO.h> 00031 //----------------------------------------------------------------------------- 00032 00041 template < class F > class PO:public EO < F > 00042 { 00043 00044 public: 00045 00046 #if defined(__CUDACC__) 00047 typedef typename EO < F >::Fitness Fitness; 00048 #else 00049 typedef typename PO<F>::Fitness Fitness; 00050 #endif 00051 00055 PO ():repFitness (Fitness ()), invalidFitness (true), 00056 bestFitness (Fitness()){} 00057 00058 00060 Fitness fitness () const 00061 { 00062 if (invalid ()) 00063 throw std::runtime_error ("invalid fitness in PO.h"); 00064 return repFitness; 00065 } 00066 00067 00071 void fitness (const Fitness & _fitness) 00072 { 00073 repFitness = _fitness; 00074 invalidFitness = false; 00075 } 00076 00080 Fitness best () const 00081 { 00082 if (invalid ()) 00083 throw std::runtime_error ("invalid best fitness in PO.h"); 00084 return bestFitness; 00085 } 00086 00087 00091 void best (const Fitness & _bestFitness) 00092 { 00093 bestFitness = _bestFitness; 00094 invalidBestFitness = false; 00095 } 00096 00097 00101 bool invalid () const 00102 { 00103 return invalidFitness; 00104 } 00105 00109 void invalidate () 00110 { 00111 invalidFitness = true; 00112 } 00113 00117 bool invalidBest () const 00118 { 00119 return invalidBestFitness; 00120 } 00121 00125 void invalidateBest () 00126 { 00127 invalidBestFitness = true; 00128 } 00129 00133 virtual std::string className () const 00134 { 00135 return "PO"; 00136 } 00137 00141 bool operator< (const PO & _po2) const { return fitness () < _po2.fitness ();} 00142 bool operator> (const PO & _po2) const { return !(fitness () <= _po2.fitness ());} 00143 00144 00149 virtual void printOn(std::ostream& _os) const { _os << bestFitness << ' ' ;} 00150 00151 00160 virtual void readFrom(std::istream& _is) { 00161 00162 // the new version of the reafFrom function. 00163 // It can distinguish between valid and invalid fitness values. 00164 std::string fitness_str; 00165 int pos = _is.tellg(); 00166 _is >> fitness_str; 00167 00168 if (fitness_str == "INVALID") 00169 { 00170 invalidFitness = true; 00171 } 00172 else 00173 { 00174 invalidFitness = false; 00175 _is.seekg(pos); // rewind 00176 _is >> repFitness; 00177 } 00178 } 00179 00180 private: 00181 Fitness repFitness; // value of fitness for this particle 00182 bool invalidFitness; // true if the value of the fitness is invalid 00183 00184 Fitness bestFitness; // value of the best fitness found for the particle 00185 bool invalidBestFitness; // true if the value of the best fitness is invalid 00186 00187 }; 00188 00189 //----------------------------------------------------------------------------- 00190 00191 #endif /*PO_H */