Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# EO.h

    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
    00034 //-----------------------------------------------------------------------------
    00044 template<class F> class EO: public eoObject, public eoPersistent
    00045 {
    00046 public:
    00047   typedef F Fitness;
    00048
    00054   EO(): repFitness(Fitness()), invalidFitness(true) {}
    00055
    00057   virtual ~EO() {};
    00058
    00060   Fitness fitness() const {
    00061     if (invalid())
    00062       throw std::runtime_error("invalid fitness");
    00063     return repFitness;
    00064   }
    00065
    00066   // Set fitness as invalid.
    00067   void invalidate() { invalidFitness = true; }
    00068
    00072   void fitness(const Fitness& _fitness)
    00073   {
    00074     repFitness = _fitness;
    00075     invalidFitness = false;
    00076   }
    00077
    00081   bool invalid() const { return invalidFitness; }
    00082
    00086   bool operator<(const EO& _eo2) const { return fitness() < _eo2.fitness(); }
    00087   bool operator>(const EO& _eo2) const { return !(fitness() <= _eo2.fitness()); }
    00088
    00090
    00091
    00095   virtual std::string className() const { return "EO"; }
    00096
    00105   virtual void readFrom(std::istream& _is) {
    00106
    00107         // the new version of the reafFrom function.
    00108         // It can distinguish between valid and invalid fitness values.
    00109         std::string fitness_str;
    00110         int pos = _is.tellg();
    00111         _is >> fitness_str;
    00112
    00113         if (fitness_str == "INVALID")
    00114         {
    00115                 invalidFitness = true;
    00116         }
    00117         else
    00118         {
    00119                 invalidFitness = false;
    00120                 _is.seekg(pos); // rewind
    00121                 _is >> repFitness;
    00122         }
    00123   }
    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   }
    00142
    00144
    00145 private:
    00146   Fitness repFitness;   // value of fitness for this chromosome
    00147   bool invalidFitness;  // true if the value of fitness is invalid
    00148 };
    00149
    00150 //-----------------------------------------------------------------------------
    00151
    00152 #endif