1 /* 2 * cPopulationInterface.h 3 * Avida 4 * 5 * Called "pop_interface.hh" prior to 12/5/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 cPopulationInterface_h 24 #define cPopulationInterface_h 25 26 #include "avida/core/WorldDriver.h" 27 28 #include "cOrgInterface.h" 29 #include "cWorld.h" 30 #include "cGenomeUtil.h" 31 #include "cPopulationCell.h" 32 33 namespace Avida { 34 class Sequence; 35 }; 36 37 class cAvidaContext; 38 class cDeme; 39 class cPopulation; 40 class cOrgMessage; 41 class cOrganism; 42 43 using namespace Avida; 44 45 46 class cPopulationInterface : public cOrgInterface 47 { 48 private: 49 cWorld* m_world; 50 int m_cell_id; 51 int m_deme_id; 52 53 int m_prevseen_cell_id; // Previously-seen cell's ID 54 int m_prev_task_cell; // Cell ID of previous task 55 int m_num_task_cells; // Number of task cells seen 56 57 cPopulationInterface(); // @not_implemented 58 cPopulationInterface(const cPopulationInterface&); // @not_implemented 59 cPopulationInterface& operator=(const cPopulationInterface&); // @not_implemented 60 61 public: 62 cPopulationInterface(cWorld* world); 63 virtual ~cPopulationInterface(); 64 65 const tSmartArray <cOrganism*> GetLiveOrgList() const; 66 //! Retrieve this organism. 67 cOrganism* GetOrganism(); 68 //! Retrieve the ID of this cell. GetCellID()69 int GetCellID() { return m_cell_id; } 70 //! Retrieve the cell in which this organism lives. 71 cPopulationCell* GetCell(); 72 cPopulationCell* GetCell(int cell_id); 73 //! Retrieve the cell currently faced by this organism. 74 cPopulationCell* GetCellFaced(); GetDemeID()75 int GetDemeID() { return m_deme_id; } 76 //! Retrieve the deme in which this organism lives. 77 cDeme* GetDeme(); SetCellID(int in_id)78 void SetCellID(int in_id) { m_cell_id = in_id; } SetDemeID(int in_id)79 void SetDemeID(int in_id) { m_deme_id = in_id; } 80 int GetCellXPosition(); 81 int GetCellYPosition(); 82 83 int GetCellData(); 84 int GetCellDataOrgID(); 85 int GetCellDataUpdate(); 86 int GetCellDataTerritory(); 87 int GetCellDataForagerType(); 88 void SetCellData(const int newData); 89 int GetFacedCellData(); 90 int GetFacedCellDataOrgID(); 91 int GetFacedCellDataUpdate(); 92 int GetFacedCellDataTerritory(); 93 GetPrevSeenCellID()94 int GetPrevSeenCellID() { return m_prevseen_cell_id; } GetPrevTaskCellID()95 int GetPrevTaskCellID() { return m_prev_task_cell; } GetNumTaskCellsReached()96 int GetNumTaskCellsReached() { return m_num_task_cells; } AddReachedTaskCell()97 void AddReachedTaskCell() { m_num_task_cells++; } SetPrevSeenCellID(int in_id)98 void SetPrevSeenCellID(int in_id) { m_prevseen_cell_id = in_id; } SetPrevTaskCellID(int in_id)99 void SetPrevTaskCellID(int in_id) { m_prev_task_cell = in_id; } 100 101 bool GetLGTFragment(cAvidaContext& ctx, int region, const Genome& dest_genome, Sequence& seq); 102 103 bool Divide(cAvidaContext& ctx, cOrganism* parent, const Genome& offspring_genome); 104 cOrganism* GetNeighbor(); 105 bool IsNeighborCellOccupied(); 106 int GetNumNeighbors(); 107 void GetNeighborhoodCellIDs(tArray<int>& list); 108 int GetFacing(); // Returns the facing of this organism. 109 int GetFacedCellID(); 110 int GetFacedDir(); // Returns the human interpretable facing of this org. 111 int GetNeighborCellContents(); 112 void Rotate(int direction = 1); Breakpoint()113 void Breakpoint() { m_world->GetDriver().SignalBreakpoint(); } 114 int GetInputAt(int& input_pointer); 115 void ResetInputs(cAvidaContext& ctx); 116 const tArray<int>& GetInputs() const; 117 const tArray<double>& GetResources(cAvidaContext& ctx); 118 const tArray<double>& GetFacedCellResources(cAvidaContext& ctx); 119 const tArray<double>& GetCellResources(int cell_id, cAvidaContext& ctx); 120 const tArray<double>& GetFrozenResources(cAvidaContext& ctx, int cell_id); 121 const tArray<double>& GetDemeResources(int deme_id, cAvidaContext& ctx); 122 const tArray< tArray<int> >& GetCellIdLists(); 123 int GetCurrPeakX(cAvidaContext& ctx, int res_id); 124 int GetCurrPeakY(cAvidaContext& ctx, int res_id); 125 int GetFrozenPeakX(cAvidaContext& ctx, int res_id); 126 int GetFrozenPeakY(cAvidaContext& ctx, int res_id); 127 cResourceCount* GetResourceCount(); 128 void TriggerDoUpdates(cAvidaContext& ctx); 129 void UpdateResources(cAvidaContext& ctx, const tArray<double>& res_change); 130 void UpdateDemeResources(cAvidaContext& ctx, const tArray<double>& res_change); 131 void Die(cAvidaContext& ctx); 132 void KillCellID(int target, cAvidaContext& ctx); 133 void Kaboom(int distance, cAvidaContext& ctx); 134 void SpawnDeme(cAvidaContext& ctx); 135 cOrgSinkMessage* NetReceive(); 136 bool NetRemoteValidate(cAvidaContext& ctx, cOrgSinkMessage* msg); 137 int ReceiveValue(); 138 void SellValue(const int data, const int label, const int sell_price, const int org_id); 139 int BuyValue(const int label, const int buy_price); 140 bool InjectParasite(cOrganism* host, cBioUnit* parent, const cString& label, const Sequence& injected_code); 141 bool UpdateMerit(double new_merit); 142 bool TestOnDivide(); 143 //! Send a message to the faced organism. 144 bool SendMessage(cOrgMessage& msg); 145 //! Send a message to the organism in the given cell. 146 bool SendMessage(cOrgMessage& msg, cPopulationCell& rcell); 147 //! Send a message to the cell with the given cell id. 148 bool SendMessage(cOrgMessage& msg, int cellid); 149 //! Broadcast a message. 150 bool BroadcastMessage(cOrgMessage& msg, int depth); 151 bool BcastAlarm(int jump_label, int bcast_range); 152 void DivideOrgTestamentAmongDeme(double value); 153 //! Send a flash to all neighboring organisms. 154 void SendFlash(); 155 156 int GetStateGridID(cAvidaContext& ctx); 157 158 bool Move(cAvidaContext& ctx, int src_id, int dest_id); 159 160 // Reputation 161 void RotateToGreatestReputation(); 162 void RotateToGreatestReputationWithDifferentTag(int tag); 163 void RotateToGreatestReputationWithDifferentLineage(int line); 164 165 // -------- Network creation support -------- 166 public: 167 //! Link this organism's cell to the cell it is currently facing. 168 void CreateLinkByFacing(double weight=1.0); 169 //! Link this organism's cell to the cell with coordinates (x,y). 170 void CreateLinkByXY(int x, int y, double weight=1.0); 171 //! Link this organism's cell to the cell with index idx. 172 void CreateLinkByIndex(int idx, double weight=1.0); 173 //! Broadcast a message to all organisms that are connected by this network. 174 bool NetworkBroadcast(cOrgMessage& msg); 175 //! Unicast a message to the current selected organism. 176 bool NetworkUnicast(cOrgMessage& msg); 177 //! Rotate to select a new network link. 178 bool NetworkRotate(int x); 179 //! Select a new network link. 180 bool NetworkSelect(int x); 181 182 int GetNextDemeInput(cAvidaContext& ctx); 183 void DoDemeInput(int value); 184 void DoDemeOutput(cAvidaContext& ctx, int value); 185 186 // -------- HGT support -------- 187 public: 188 //! Container type for fragments used during HGT. 189 typedef cPopulationCell::fragment_list_type fragment_list_type; 190 //! Match record, used to indicate the region within a genome that should be mutated. 191 typedef cGenomeUtil::substring_match substring_match; 192 //! Called when this organism is the donor during conjugation. 193 void DoHGTDonation(cAvidaContext& ctx); 194 //! Called when this organism "requests" an HGT conjugation. 195 void DoHGTConjugation(cAvidaContext& ctx); 196 //! Perform an HGT mutation on this offspring. 197 void DoHGTMutation(cAvidaContext& ctx, Genome& offspring); 198 199 protected: 200 //! Place the fragment at the location of best match. 201 void HGTMatchPlacement(cAvidaContext& ctx, const Sequence& offspring, 202 fragment_list_type::iterator& selected, 203 substring_match& location); 204 //! Place the fragment at the location of best match, with redundant instructions trimmed. 205 void HGTTrimmedPlacement(cAvidaContext& ctx, const Sequence& offspring, 206 fragment_list_type::iterator& selected, 207 substring_match& location); 208 //! Place the fragment at a random location. 209 void HGTRandomPlacement(cAvidaContext& ctx, const Sequence& offspring, 210 fragment_list_type::iterator& selected, 211 substring_match& location); 212 //! Support for stateful HGT mutations. 213 struct HGTSupport { 214 fragment_list_type _pending; //!< HGT fragments that are awaiting an offspring. 215 }; 216 HGTSupport* m_hgt_support; //!< Lazily-initialized pointer to HGT data. 217 //! Initialize HGT support. InitHGTSupport()218 inline void InitHGTSupport() { if(!m_hgt_support) { m_hgt_support = new HGTSupport(); } } 219 //! Called when this organism is the receiver of an HGT donation. 220 void ReceiveHGTDonation(const Sequence& fragment); 221 222 public: 223 void AddLiveOrg(); 224 void RemoveLiveOrg(); 225 226 bool HasOpinion(cOrganism* in_organism); 227 void SetOpinion(int opinion, cOrganism* in_organism); 228 void ClearOpinion(cOrganism* in_organism); 229 230 void JoinGroup(int group_id); 231 void MakeGroup(); 232 void LeaveGroup(int group_id); 233 int NumberOfOrganismsInGroup(int group_id); 234 int NumberGroupFemales(int group_id); 235 int NumberGroupMales(int group_id); 236 int NumberGroupJuvs(int group_id); 237 void ChangeGroupMatingTypes(cOrganism* org, int group_id, int old_type, int new_type); 238 239 // ----- Tolerance/Group support ------ 240 int IncTolerance(const int tolerance_type, cAvidaContext& ctx); 241 int DecTolerance(const int tolerance_type, cAvidaContext& ctx); 242 int CalcGroupToleranceImmigrants(int prop_group_id, int mating_type = -1); 243 int CalcGroupToleranceOffspring(cOrganism* parent_organism); 244 double CalcGroupOddsImmigrants(int group_id, int mating_type); 245 double CalcGroupOddsOffspring(cOrganism* parent); 246 double CalcGroupOddsOffspring(int group_id); 247 bool AttemptImmigrateGroup(int group_id, cOrganism* org); 248 void PushToleranceInstExe(int tol_inst, cAvidaContext& ctx); 249 int& GetGroupIntolerances(int group_id, int tol_num, int mating_type); 250 251 void DecNumPreyOrganisms(); 252 void DecNumPredOrganisms(); 253 void IncNumPreyOrganisms(); 254 void IncNumPredOrganisms(); 255 void RecordMinPreyFailedAttack(); 256 void AttackFacedOrg(cAvidaContext& ctx, int loser); 257 258 // --------Donate Specific support ---- 259 void PushDonateSpecInstExe(cAvidaContext& ctx, cOrganism* recipient_org, int kin); 260 261 // -------- Avatar support -------- @JJB 262 private: 263 struct sIO_avatar 264 { 265 int av_cell_id; 266 int av_facing; 267 int av_faced_cell; 268 bool av_input; 269 bool av_output; sIO_avatarsIO_avatar270 sIO_avatar() : av_cell_id(-1), av_facing(0), av_faced_cell(-1), av_input(false), av_output(false) { ; } sIO_avatarsIO_avatar271 sIO_avatar(int av_cell_id, int av_facing, int av_faced_cell, bool input, bool output) : av_cell_id(av_cell_id), av_facing(av_facing), av_faced_cell(av_faced_cell), av_input(input), av_output(output) { ; } 272 }; 273 274 tSmartArray<sIO_avatar> m_avatars; getNumAV()275 inline int getNumAV() { return m_avatars.GetSize(); } 276 public: 277 bool HasOutputAV(int av_num = 0); 278 bool FacedHasOutputAV(int av_num = 0); 279 bool FacedHasAV(int av_num = 0); 280 bool FacedHasPredAV(int av_num = 0); 281 bool FacedHasPreyAV(int av_num = 0); 282 void AddIOAV(int av_cell_id, int av_facing, bool input, bool output); 283 void AddPredPreyAV(int av_cell_id); 284 void SwitchPredPrey(int av_num = 0); 285 void RemoveAllAV(); 286 int GetAVFacing(int av_num = 0); 287 int GetAVCellID(int av_num = 0); 288 int GetAVFacedCellID(int av_num = 0); 289 int GetAVNumNeighbors(int av_num = 0); 290 int GetAVFacedData(int av_num = 0); 291 int GetAVFacedDataOrgID(int av_num = 0); 292 int GetAVFacedDataUpdate(int av_num = 0); 293 int GetAVFacedDataTerritory(int av_num = 0); 294 int GetAVData(int av_num = 0); 295 int GetAVDataOrgID(int av_num = 0); 296 int GetAVDataUpdate(int av_num = 0); 297 int GetAVDataTerritory(int av_num = 0); 298 int FindAV(bool input, bool output, int av_num = 0); 299 void SetAVFacing(int av_facing, int av_num = 0); 300 bool SetAVCellID(int av_cell_id, int av_num = 0); 301 void SetAVFacedCellID(int av_num = 0); 302 void SetAVCellData(const int newData, const int org_id, int av_num = 0); 303 bool MoveAV(cAvidaContext& ctx, int av_num = 0); 304 bool RotateAV(int increment, int av_num = 0); 305 cOrganism* GetRandFacedAV(int av_num = 0); 306 cOrganism* GetRandFacedPredAV(int av_num = 0); 307 cOrganism* GetRandFacedPreyAV(int av_num = 0); 308 tArray<cOrganism*> GetFacedAVs(int av_num = 0); 309 tArray<cOrganism*> GetCellAVs(int cell_id, int av_num = 0); 310 tArray<cOrganism*> GetFacedPreyAVs(int av_num = 0); 311 const tArray<double>& GetAVResources(cAvidaContext& ctx, int av_num = 0); 312 const tArray<double>& GetAVFacedResources(cAvidaContext& ctx, int av_num = 0); 313 void UpdateAVResources(cAvidaContext& ctx, const tArray<double>& res_change, int av_num = 0); 314 315 void BeginSleep(); 316 void EndSleep(); 317 }; 318 319 #endif 320