EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // EO.h 00005 // (c) GeNeura Team 1998 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: todos@geneura.ugr.es, http://geneura.ugr.es 00022 */ 00023 //----------------------------------------------------------------------------- 00024 00025 #ifndef EO_H 00026 #define EO_H 00027 00028 //----------------------------------------------------------------------------- 00029 00030 #include <stdexcept> // std::runtime_error 00031 #include <eoObject.h> // eoObject 00032 #include <eoPersistent.h> // eoPersistent 00033 00061 template<class F = double> class EO: public eoObject, public eoPersistent 00062 { 00063 public: 00064 typedef F Fitness; 00065 00068 EO(): repFitness(Fitness()), invalidFitness(true) { } 00069 00071 virtual ~EO() {}; 00072 00074 const Fitness& fitness() const { 00075 if (invalid()) 00076 throw std::runtime_error("invalid fitness"); 00077 return repFitness; 00078 } 00079 00081 Fitness& fitnessReference() { 00082 if (invalid()) throw std::runtime_error("invalid fitness"); 00083 return repFitness; 00084 } 00085 00086 // Set fitness as invalid. 00087 void invalidate() { invalidFitness = true; repFitness = Fitness(); } 00088 00092 void fitness(const Fitness& _fitness) 00093 { 00094 repFitness = _fitness; 00095 invalidFitness = false; 00096 } 00097 00101 bool invalid() const { return invalidFitness; } 00102 00106 bool operator<(const EO& _eo2) const { return fitness() < _eo2.fitness(); } 00107 bool operator>(const EO& _eo2) const { return !(fitness() <= _eo2.fitness()); } 00108 00110 00111 00115 virtual std::string className() const { return "EO"; } 00116 00125 virtual void readFrom(std::istream& _is) { 00126 00127 // the new version of the reafFrom function. 00128 // It can distinguish between valid and invalid fitness values. 00129 std::string fitness_str; 00130 int pos = _is.tellg(); 00131 _is >> fitness_str; 00132 00133 if (fitness_str == "INVALID") 00134 { 00135 invalidFitness = true; 00136 } 00137 else 00138 { 00139 invalidFitness = false; 00140 _is.seekg(pos); // rewind 00141 _is >> repFitness; 00142 } 00143 } 00144 00149 virtual void printOn(std::ostream& _os) const { 00150 00151 00152 // the latest version of the code. Very similar to the old code 00153 if (invalid()) { 00154 _os << "INVALID "; 00155 } 00156 else 00157 { 00158 _os << repFitness << ' '; 00159 } 00160 00161 } 00162 00164 00165 private: 00166 Fitness repFitness; // value of fitness for this chromosome 00167 bool invalidFitness; // true if the value of fitness is invalid 00168 }; 00169 00170 00171 #endif 00172