[Avida-SVN] r2437 - development/source/main

barrick at myxo.css.msu.edu barrick at myxo.css.msu.edu
Fri Mar 7 18:31:55 PST 2008


Author: barrick
Date: 2008-03-07 21:31:55 -0500 (Fri, 07 Mar 2008)
New Revision: 2437

Modified:
   development/source/main/cAvidaConfig.h
   development/source/main/cDeme.cc
   development/source/main/cDeme.h
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
Log:
Added configuration options to replicate a deme as soon as it accumulates a certain number of births or uses a certain number of cpu cycles per organism that is currently in the deme.



Modified: development/source/main/cAvidaConfig.h
===================================================================
--- development/source/main/cAvidaConfig.h	2008-03-08 00:03:31 UTC (rev 2436)
+++ development/source/main/cAvidaConfig.h	2008-03-08 02:31:55 UTC (rev 2437)
@@ -308,7 +308,9 @@
   CONFIG_ADD_VAR(GERMLINE_COPY_MUT, double, 0.0075, "Prob. of copy mutations occuring during\ngermline replication (default=0.0075).");
   CONFIG_ADD_VAR(GERMLINE_INS_MUT, double, 0.05, "Prob. of an insertion mutation occuring\nduring germline replication (default=0.05).");
   CONFIG_ADD_VAR(GERMLINE_DEL_MUT, double, 0.05, "Prob. of a deletion mutation occuring\nduring germline replication (default=0.05).");
-  
+  CONFIG_ADD_VAR(DEMES_REPLICATE_CPU_CYCLES, double, 0.0, "Replicate a deme immediately after it has used\nthis number of cpu cycles, normalized\nby number of orgs in deme (0 = OFF).");
+  CONFIG_ADD_VAR(DEMES_REPLICATE_BIRTHS, int, 0, "Replicate a deme immediately after it has \nproduced this many offspring (0 = OFF).");
+
   CONFIG_ADD_GROUP(REPRODUCTION_GROUP, "Birth and Death");
   CONFIG_ADD_VAR(BIRTH_METHOD, int, 0, "Which organism should be replaced on birth?\n0 = Random organism in neighborhood\n1 = Oldest in neighborhood\n2 = Largest Age/Merit in neighborhood\n3 = None (use only empty cells in neighborhood)\n4 = Random from population (Mass Action)\n5 = Oldest in entire population\n6 = Random within deme\n7 = Organism faced by parent\n8 = Next grid cell (id+1)\n9 = Largest energy used in entire population\n10 = Largest energy used in neighborhood");
   CONFIG_ADD_VAR(PREFER_EMPTY, int, 1, "Give empty cells preference in offsping placement?");

Modified: development/source/main/cDeme.cc
===================================================================
--- development/source/main/cDeme.cc	2008-03-08 00:03:31 UTC (rev 2436)
+++ development/source/main/cDeme.cc	2008-03-08 02:31:55 UTC (rev 2437)
@@ -96,6 +96,7 @@
 
 void cDeme::Reset(int previous_generation, bool resetResources)
 {
+  deme_time_used = 0;
   birth_count = 0;
   _age = 0;
   generation = previous_generation + 1;

Modified: development/source/main/cDeme.h
===================================================================
--- development/source/main/cDeme.h	2008-03-08 00:03:31 UTC (rev 2436)
+++ development/source/main/cDeme.h	2008-03-08 02:31:55 UTC (rev 2437)
@@ -45,11 +45,15 @@
   int _id; //!< ID of this deme (position in cPopulation::deme_array).
   tArray<int> cell_ids;
   int width; //!< Width of this deme.
+
+// The following should be moved to cDemePhenotype
   int birth_count; //!< Number of organisms that have been born into this deme since reset.
   int org_count; //!< Number of organisms are currently in this deme.
   int _age; //!< Age of this deme, in updates.
   int generation; //!< Generation of this deme
   double total_org_energy; //! total amount of energy in organisms in this deme
+  double deme_time_used; //!< number of cpu cycles, normalized by current orgs, this deme has used
+// End of phenotypic traits
   
   cGermline _germline; //!< The germline for this deme, if used.
 
@@ -64,7 +68,7 @@
   cMerit _next_merit; //!< Deme merit that will be inherited upon deme replication.
   
 public:
-  cDeme() : _id(0), width(0), birth_count(0), org_count(0), _age(0), generation(0), total_org_energy(0.0), deme_resource_count(0) { ; }
+  cDeme() : _id(0), width(0), birth_count(0), org_count(0), _age(0), generation(0), total_org_energy(0.0), deme_time_used(0.0), deme_resource_count(0) { ; }
   ~cDeme() { ; }
 
   void Setup(int id, const tArray<int>& in_cells, int in_width = -1, cWorld* world = NULL);
@@ -135,6 +139,9 @@
   void SetCellEvent(int x1, int y1, int x2, int y2, int delay, int duration);
   
   double CalculateTotalEnergy();
+  
+  void IncTimeUsed() { deme_time_used += 1.0/(double)org_count; }
+  double GetTimeUsed() { return deme_time_used; }
 };
 
 #endif

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2008-03-08 00:03:31 UTC (rev 2436)
+++ development/source/main/cPopulation.cc	2008-03-08 02:31:55 UTC (rev 2437)
@@ -1102,14 +1102,6 @@
   for(int deme_id=0; deme_id<num_demes; ++deme_id) {
     cDeme& source_deme = deme_array[deme_id];
     
-    // Doesn't make sense to try and replicate a deme that *has no organisms*.
-    if(source_deme.IsEmpty()) continue;
-    
-    // Prevent sterile demes from replicating.
-    if(m_world->GetConfig().DEMES_PREVENT_STERILE.Get() && (source_deme.GetBirthCount() == 0)) {
-      continue;
-    }
-    
     // Test this deme to determine if it should be replicated.  If not,
     // continue on to the next deme.
     switch (rep_trigger) {
@@ -1149,18 +1141,41 @@
       }
     }
     
-    // If we made it this far, we should replicate this deme.  Pick a target
-    // deme to replicate to, making sure that we don't try to replicate over ourself.
-    int target_id = deme_id;
-    while(target_id == deme_id) {
+    // Pick a target deme to replicate to, making sure that 
+    // we don't try to replicate over ourself.
+    int target_id = source_deme.GetID();
+    const int num_demes = GetNumDemes();
+    while(target_id == source_deme.GetID()) {
       target_id = m_world->GetRandom().GetUInt(num_demes);
     }
-    
+  
     ReplaceDeme(source_deme, deme_array[target_id]);
   }
 }
 
+/*! ReplicateDeme is a helper method for replicating a source deme.
+*/
+void cPopulation::ReplicateDeme(cDeme & source_deme)
+{
+    // Doesn't make sense to try and replicate a deme that *has no organisms*.
+    if(source_deme.IsEmpty()) return;
+    
+    // Prevent sterile demes from replicating.
+    if(m_world->GetConfig().DEMES_PREVENT_STERILE.Get() && (source_deme.GetBirthCount() == 0)) {
+      return;
+    }
 
+    // Pick a target deme to replicate to, making sure that 
+    // we don't try to replicate over ourself.
+    int target_id = source_deme.GetID();
+    const int num_demes = GetNumDemes();
+    while(target_id == source_deme.GetID()) {
+      target_id = m_world->GetRandom().GetUInt(num_demes);
+    }
+  
+    ReplaceDeme(source_deme, deme_array[target_id]);
+}
+
 /*! ReplaceDeme is a helper method that handles all the different configuration
 options related to the replacement of a target deme by a source.  It works with
 both CompeteDemes and ReplicateDemes (and can be called directly via an event if
@@ -1297,6 +1312,7 @@
   // Check to see if we're doing probabilistic organism replication from source
   // to target deme.
   if(m_world->GetConfig().DEMES_PROB_ORG_TRANSFER.Get() == 0.0) {
+  
     // Here's the idea: store up a list of the genotypes from the source that we
     // need to copy to the target. Then clear both the source and target demes, 
     // and finally inject organisms from the saved genotypes into both the source 
@@ -1723,6 +1739,16 @@
   InjectClone( cell2_id, *(cell_array[cell1_id].GetOrganism()) );    
 }
 
+void cPopulation::CheckImplicitDemeRepro(cDeme& deme) {
+
+  if (GetNumDemes() <= 1) return;
+  
+  if (m_world->GetConfig().DEMES_REPLICATE_CPU_CYCLES.Get() 
+    && (deme.GetTimeUsed() >= m_world->GetConfig().DEMES_REPLICATE_CPU_CYCLES.Get())) ReplicateDeme(deme);
+  else if (m_world->GetConfig().DEMES_REPLICATE_BIRTHS.Get() 
+    && (deme.GetBirthCount() >= m_world->GetConfig().DEMES_REPLICATE_BIRTHS.Get())) ReplicateDeme(deme);      
+}
+
 // Print out all statistics about individual demes
 void cPopulation::PrintDemeAllStats() {
   PrintDemeFitness();
@@ -2384,7 +2410,16 @@
 
   m_world->GetStats().IncExecuted();
   resource_count.Update(step_size);
-  for(int i = 0; i < GetNumDemes(); i++) GetDeme(i).Update(step_size);
+  
+  // Deme specific
+  if (GetNumDemes() > 1)
+  {
+    for(int i = 0; i < GetNumDemes(); i++) GetDeme(i).Update(step_size);
+  
+    cDeme & deme = GetDeme(GetCell(cell_id).GetDemeID());
+    deme.IncTimeUsed();
+    CheckImplicitDemeRepro(deme);
+  }
 }
 
 
@@ -2424,7 +2459,16 @@
 
   m_world->GetStats().IncExecuted();
   resource_count.Update(step_size);
-  for(int i = 0; i < GetNumDemes(); i++) GetDeme(i).Update(step_size);
+ 
+   // Deme specific
+  if (GetNumDemes() > 1)
+  {
+    for(int i = 0; i < GetNumDemes(); i++) GetDeme(i).Update(step_size);
+  
+    cDeme & deme = GetDeme(GetCell(cell_id).GetDemeID());
+    deme.IncTimeUsed();
+    CheckImplicitDemeRepro(deme);
+  }
 }
 
 // Loop through all the demes getting stats and doing calculations

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2008-03-08 00:03:31 UTC (rev 2436)
+++ development/source/main/cPopulation.h	2008-03-08 02:31:55 UTC (rev 2437)
@@ -187,6 +187,9 @@
   //! Replicate all demes based on the given replication trigger.
   void ReplicateDemes(int rep_trigger);
 
+  //! Helper method to replicate deme
+  void ReplicateDeme(cDeme & source_deme);
+
   //! Helper method that replaces a target deme with the given source deme.
   void ReplaceDeme(cDeme& source_deme, cDeme& target_deme);
   
@@ -207,6 +210,7 @@
   void CopyDeme(int deme1_id, int deme2_id);
   void SpawnDeme(int deme1_id, int deme2_id=-1);
 
+  void CheckImplicitDemeRepro(cDeme& deme);
   
   // Deme-related stats methods
   void PrintDemeAllStats();




More information about the Avida-cvs mailing list