1 /*
2  *  cMerit.h
3  *  Avida
4  *
5  *  Called "merit.hh" prior to 12/7/05.
6  *  Copyright 1999-2011 Michigan State University. All rights reserved.
7  *  Copyright 1993-2003 California Institute of Technology.
8  *
9  *
10  *  This file is part of Avida.
11  *
12  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
13  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
14  *
15  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
19  *  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef cMerit_h
24 #define cMerit_h
25 
26 #include <iostream>
27 #include <cmath>
28 #include <climits>
29 #include <cassert>
30 
31 class cWorld;
32 
33 class cMerit
34 {
35 protected:
36   int bits;
37   unsigned int base;
38   int offset;
39   double value;
40 
41   void UpdateValue(double in_value);
42 
43 public:
cMerit()44   cMerit() : bits(0), base(0), offset(0), value(0) { ; }
45 
cMerit(const int in_value)46   explicit cMerit(const int    in_value){ UpdateValue(in_value); }
cMerit(const unsigned int in_value)47   explicit cMerit(const unsigned int   in_value){ UpdateValue(in_value); }
cMerit(const double in_value)48   explicit cMerit(const double in_value){ UpdateValue(in_value); }
cMerit(const cMerit & merit)49   cMerit(const cMerit& merit) { *this = merit; }
50 
51   bool OK() const;
52 
53   void operator=(const cMerit & _merit)
54   {
55     bits   = _merit.bits;
56     base   = _merit.base;
57     offset = _merit.offset;
58     value  = _merit.value;
59   }
60 
61   void operator=(double _merit) { UpdateValue(_merit); }
62 
63   void operator+=(const cMerit & _m) { UpdateValue(value + _m.GetDouble()); }
64   void operator+=(double _merit) { UpdateValue(value + _merit); }
65 
66   cMerit operator*(const cMerit& _m) const { return cMerit(value * _m.GetDouble()); }
67   void operator*=(const cMerit& _m) { UpdateValue(value * _m.GetDouble()); }
68 
69   int  operator>(const cMerit& _m)  const { return value >  _m.GetDouble(); }
70   int  operator<(const cMerit& _m)  const { return value <  _m.GetDouble(); }
71   int  operator>=(const cMerit& _m) const { return value >= _m.GetDouble(); }
72   int  operator<=(const cMerit& _m) const { return value <= _m.GetDouble(); }
73 
74   int  operator==(const cMerit & _m) const { return value == _m.GetDouble(); }
75   int  operator==(const double _m) const { return value == _m; }
76   int  operator==(const unsigned int _m)   const { return (offset==0 && base==_m); }
77 
78   int  operator!=(const cMerit & _m) const { return value != _m.GetDouble(); }
79   int  operator!=(const double _m) const { return value != _m; }
80   int  operator!=(const unsigned int _m)   const { return (offset!=0 || base!=_m); }
81 
Clear()82   void Clear() { value = 0; base = 0; offset = 0; bits = 0; }
83 
84   // @TCC - This function fails for values > UINT_MAX...
GetUInt()85   unsigned int GetUInt()   const {
86     assert(value < UINT_MAX);  // Fails for merit values > UINT_MAX.
87     return (unsigned int) value; }
88 
GetDouble()89   double GetDouble()      const { return value; }
90 
GetBit(int bit_num)91   int GetBit(int bit_num)  const {
92     assert(bit_num >= 0);
93     return ( bit_num >= offset && bit_num < bits ) ?
94       ( base >> (bit_num-offset) ) & 1 : 0; }
95 
GetNumBits()96   int GetNumBits() const { return bits; }
97 
CalcFitness(int gestation_time)98   double CalcFitness(int gestation_time) const { return ( gestation_time != 0 ) ? value / ((double) gestation_time) : 0; }
99 
100   std::ostream& BinaryPrint(std::ostream& os = std::cout) const;
101 };
102 
103 
104 std::ostream& operator<<(std::ostream& os, const cMerit & merit);
105 
106 #endif
107