1 /*
2  *  cBGGenotype.h
3  *  Avida
4  *
5  *  Created by David on 11/5/09.
6  *  Copyright 2009-2011 Michigan State University. All rights reserved.
7  *
8  *
9  *  This file is part of Avida.
10  *
11  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
12  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
13  *
14  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
18  *  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef cBGGenotype_h
23 #define cBGGenotype_h
24 
25 #include "avida/core/Genome.h"
26 
27 #include "cBioGroup.h"
28 #include "cCountTracker.h"
29 #include "cDoubleSum.h"
30 #include "cString.h"
31 
32 
33 class cAvidaContext;
34 class cBGGenotypeManager;
35 class cEntryHandle;
36 class cWorld;
37 
38 using namespace Avida;
39 
40 
41 class cBGGenotype : public cBioGroup
42 {
43   friend class cBGGenotypeManager;
44 private:
45   cBGGenotypeManager* m_mgr;
46   cEntryHandle* m_handle;
47 
48   eBioUnitSource m_src;
49   cString m_src_args;
50   Genome m_genome;
51   cString m_name;
52 
53   struct {
54     bool m_threshold:1;
55     bool m_active:1;
56   };
57 
58   int m_generation_born;
59   int m_update_born;
60   int m_update_deactivated;
61   int m_depth;
62   int m_active_offspring_genotypes;
63   int m_num_organisms;
64   int m_last_num_organisms;
65   int m_total_organisms;
66 
67   tArray<cBGGenotype*> m_parents;
68   cString m_parent_str;
69 
70   cCountTracker m_births;
71   cCountTracker m_deaths;
72   cCountTracker m_breed_in;
73   cCountTracker m_breed_true;
74   cCountTracker m_breed_out;
75 
76   cDoubleSum m_copied_size;
77   cDoubleSum m_exe_size;
78   cDoubleSum m_gestation_time;
79   cDoubleSum m_repro_rate;
80   cDoubleSum m_merit;
81   cDoubleSum m_fitness;
82 
83   int m_last_birth_cell;
84   int m_last_group_id;
85   int m_last_forager_type;
86 
87   cBGGenotype(cBGGenotypeManager* mgr, int in_id, cBioUnit* founder, int update, tArray<cBioGroup*>* parents);
88   cBGGenotype(cBGGenotypeManager* mgr, int in_id, const tDictionary<cString>& props, cWorld* world);
89 
90 public:
91   ~cBGGenotype();
92 
93   // cBioGroup Interface Methods
94   int GetRoleID() const;
95   const cString& GetRole() const;
GetID()96   int GetID() const { return m_id; }
97 
98   cBioGroup* ClassifyNewBioUnit(cBioUnit* bu, tArray<cBioGroup*>* parents = NULL);
99   void HandleBioUnitGestation(cBioUnit* bu);
100   void RemoveBioUnit(cBioUnit* bu);
101 
102   void RemoveActiveReference();
103 
GetDepth()104   int GetDepth() const { return m_depth; }
GetNumUnits()105   int GetNumUnits() const { return m_num_organisms; }
106 
107   const tArray<cString>& GetProperyList() const;
108   bool HasProperty(const cString& prop) const;
109   cFlexVar GetProperty(const cString& prop) const;
110 
111   void Save(cDataFile& df);
112   void DepthSave(cDataFile& df);
113 
114 
115   // Genotype Specific Methods
IsParasite()116   inline bool IsParasite() const { return (m_src == SRC_PARASITE_INJECT || m_src == SRC_PARASITE_FILE_LOAD); }
GetSource()117   inline eBioUnitSource GetSource() const { return m_src; }
GetSourceArgs()118   inline const cString& GetSourceArgs() const { return m_src_args; }
GetGenome()119   inline const Genome& GetGenome() const { return m_genome; }
GetGenomeString()120   inline cString GetGenomeString() const { return m_genome.AsString(); }
121 
GetName()122   inline const cString& GetName() const { return m_name; }
SetName(const cString & name)123   inline void SetName(const cString& name) { m_name = name; }
124 
IsThreshold()125   inline bool IsThreshold() const { return m_threshold; }
IsActive()126   inline bool IsActive() const { return m_active; }
127 
GetUpdateBorn()128   inline int GetUpdateBorn() const { return m_update_born; }
GetUpdateDeactivated()129   inline int GetUpdateDeactivated() const { return m_update_deactivated; }
130 
GetParentString()131   inline const cString& GetParentString() const { return m_parent_str; }
132 
SetThreshold()133   inline void SetThreshold() { m_threshold = true; }
ClearThreshold()134   inline void ClearThreshold() { m_threshold = false; }
135 
Deactivate(int update)136   inline void Deactivate(int update) { m_active = false; m_update_deactivated = update; }
137 
GetParents()138   inline const tArray<cBGGenotype*> GetParents() const { return m_parents; }
139 
GetTotalOrganisms()140   inline int GetTotalOrganisms() const { return m_total_organisms; }
141 
GetLastBirths()142   inline int GetLastBirths() const { return m_births.GetLast(); }
GetLastBreedIn()143   inline int GetLastBreedIn() const { return m_breed_in.GetLast(); }
GetLastBreedTrue()144   inline int GetLastBreedTrue() const { return m_breed_true.GetLast(); }
GetLastBreedOut()145   inline int GetLastBreedOut() const { return m_breed_out.GetLast(); }
146 
GetThisBirths()147   inline int GetThisBirths() const { return m_births.GetCur(); }
GetThisDeaths()148   inline int GetThisDeaths() const { return m_deaths.GetCur(); }
GetThisBreedIn()149   inline int GetThisBreedIn() const { return m_breed_in.GetCur(); }
GetThisBreedTrue()150   inline int GetThisBreedTrue() const { return m_breed_true.GetCur(); }
GetThisBreedOut()151   inline int GetThisBreedOut() const { return m_breed_out.GetCur(); }
152 
GetCopiedSize()153   inline double GetCopiedSize() const { return m_copied_size.Average(); }
GetExecutedSize()154   inline double GetExecutedSize() const { return m_exe_size.Average(); }
GetGestationTime()155   inline double GetGestationTime() const { return m_gestation_time.Average(); }
GetReproRate()156   inline double GetReproRate() const { return m_repro_rate.Average(); }
GetMerit()157   inline double GetMerit() const { return m_merit.Average(); }
GetFitness()158   inline double GetFitness() const { return m_fitness.Average(); }
159 
GetLastBirthCell()160   inline int GetLastBirthCell() const { return m_last_birth_cell; }
GetLastGroupID()161   inline int GetLastGroupID() const { return m_last_group_id; }
GetLastForagerType()162   inline int GetLastForagerType() const { return m_last_forager_type; }
163 
SetLastBirthCell(int birth_cell)164   inline void SetLastBirthCell(int birth_cell) { m_last_birth_cell = birth_cell; }
SetLastGroupID(int group_id)165   inline void SetLastGroupID(int group_id) { m_last_group_id = group_id; }
SetLastForagerType(int forager_type)166   inline void SetLastForagerType(int forager_type) { m_last_forager_type = forager_type; }
167 
168   bool Matches(cBioUnit* bu);
169   void NotifyNewBioUnit(cBioUnit* bu);
170   void UpdateReset();
171 };
172 
173 #endif
174