[Avida-SVN] r2504 - in development/source: cpu main

barrick at myxo.css.msu.edu barrick at myxo.css.msu.edu
Sun Mar 30 07:53:49 PDT 2008


Author: barrick
Date: 2008-03-30 10:53:48 -0400 (Sun, 30 Mar 2008)
New Revision: 2504

Modified:
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/main/cDeme.cc
   development/source/main/cDeme.h
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
Log:
First commit of new Deme Germline mode that uses genotypes to maintain phylogenetic tree tracking. Enable with DEMES_USE_GERMLINE 2, DEMES_USE_GERMLINE 1 continues to use previous method. Still need deme "founders" to keep track of their generation and other phenotype information.



Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2008-03-28 04:08:18 UTC (rev 2503)
+++ development/source/cpu/cHardwareCPU.cc	2008-03-30 14:53:48 UTC (rev 2504)
@@ -1631,7 +1631,7 @@
   return true;
 }
 
-void cHardwareCPU::InheritState(cHardwareBase& in_hardware)
+void cHardwareCPU::Divide_InheritState(cHardwareBase& in_hardware)
 { 
   cHardwareCPU& in_h = (cHardwareCPU&)in_hardware; 
   const cLocalThread& thread = in_h.GetThread(in_h.GetCurThread());
@@ -1642,6 +1642,7 @@
 }
 
 
+
 //////////////////////////
 // And the instructions...
 //////////////////////////

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2008-03-28 04:08:18 UTC (rev 2503)
+++ development/source/cpu/cHardwareCPU.h	2008-03-30 14:53:48 UTC (rev 2504)
@@ -221,6 +221,7 @@
   bool Divide_Main2RS(cAvidaContext& ctx, const int divide_point, const int extra_lines=0, double mut_multiplier=1); //AWC 07/28/06
 
   void Divide_DoTransposons(cAvidaContext& ctx);
+  void Divide_InheritState(cHardwareBase& in_hardware);
   
   void InjectCode(const cGenome& injection, const int line_num);
   
@@ -305,9 +306,6 @@
   
   int GetActiveStack() const { return m_threads[m_cur_thread].cur_stack; }
   bool GetMalActive() const   { return m_mal_active; }
-  
-  // --------  Inherit State  --------
-  void InheritState(cHardwareBase& in_hardware);
 
   
 private:

Modified: development/source/main/cDeme.cc
===================================================================
--- development/source/main/cDeme.cc	2008-03-28 04:08:18 UTC (rev 2503)
+++ development/source/main/cDeme.cc	2008-03-30 14:53:48 UTC (rev 2504)
@@ -291,3 +291,23 @@
   // empty our list
   m_founder_genotype_ids.ResizeClear(0);
 }
+
+void cDeme::ReplaceGermline(cGenotype& _in_genotype) {
+  
+  // same genotype, no changes
+  if (m_germline_genotype_id == _in_genotype.GetID()) return;
+    
+  // first, save and put a hold on new germline genotype
+  int prev_germline_genotype_id = m_germline_genotype_id;
+  m_germline_genotype_id = _in_genotype.GetID();
+  _in_genotype.IncDeferAdjust();  
+      
+  // next, if we previously were saving a germline genotype, free it
+  cGenotype * genotype = m_world->GetClassificationManager().FindGenotype(prev_germline_genotype_id);
+  if (genotype) {
+    genotype->DecDeferAdjust();
+    m_world->GetClassificationManager().AdjustGenotype(*genotype);
+  }
+
+}
+

Modified: development/source/main/cDeme.h
===================================================================
--- development/source/main/cDeme.h	2008-03-28 04:08:18 UTC (rev 2503)
+++ development/source/main/cDeme.h	2008-03-30 14:53:48 UTC (rev 2504)
@@ -70,6 +70,7 @@
   
   tArray<cDemeCellEvent> cell_events;
   
+  int         m_germline_genotype_id; // Genotype id of germline (if in use)
   tArray<int> m_founder_genotype_ids; // List of genotype ids used to found deme.
                                       // Keep a lease on these genotypes for the deme's lifetime.
                                       
@@ -79,7 +80,7 @@
 public:
   cDeme() : _id(0), width(0), birth_count(0), org_count(0), _age(0), generation(0), total_org_energy(0.0),
             time_used(0), gestation_time(0), cur_normalized_time_used(0.0), last_normalized_time_used(0.0), 
-            deme_resource_count(0) { ; }
+            deme_resource_count(0), m_germline_genotype_id(0) { ; }
   ~cDeme() { ; }
 
   void Setup(int id, const tArray<int>& in_cells, int in_width = -1, cWorld* world = NULL);
@@ -164,6 +165,10 @@
   void ClearFounders();
   void AddFounder(cGenotype& _in_genotype);
   tArray<int>& GetFounders() { return m_founder_genotype_ids; }
+  
+  // --- Germline management --- //
+  void ReplaceGermline(cGenotype& _in_genotype);
+  int GetGermlineGenotypeID() { return m_germline_genotype_id; }
 };
 
 #endif

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2008-03-28 04:08:18 UTC (rev 2503)
+++ development/source/main/cPopulation.cc	2008-03-30 14:53:48 UTC (rev 2504)
@@ -398,8 +398,7 @@
     ActivateOrganism(ctx, child_array[i], GetCell(target_cells[i]));
     
     //@JEB - we may want to pass along some state information from parent to child
-    if (m_world->GetConfig().DIVIDE_METHOD.Get() == DIVIDE_METHOD_KEEP_STATE_EPIGENETIC)
-    {
+    if (m_world->GetConfig().DIVIDE_METHOD.Get() == DIVIDE_METHOD_KEEP_STATE_EPIGENETIC) {
       child_array[i]->GetHardware().InheritState(parent_organism.GetHardware());
     }
     
@@ -1220,7 +1219,8 @@
   
   // Are we using germlines?  If so, we need to mutate the germline to get the
   // genome that we're going to seed the target with.
-  if(m_world->GetConfig().DEMES_USE_GERMLINE.Get()) {
+  if(m_world->GetConfig().DEMES_USE_GERMLINE.Get() == 1) {
+    // @JEB Original germlines
     cCPUMemory next_germ(source_deme.GetGermline().GetLatest());
     const cInstSet& instset = m_world->GetHardwareManager().GetInstSet();
     cAvidaContext ctx(m_world->GetRandom());
@@ -1249,7 +1249,7 @@
     // mutated germ to ONLY the target's germline.  The source remains unchanged.
     target_deme.ReplaceGermline(source_deme.GetGermline());
     target_deme.GetGermline().Add(next_germ);
-    
+        
     // Germline stats tracking.
     m_world->GetStats().GermlineReplication(source_deme.GetGermline(), target_deme.GetGermline());
     
@@ -1257,6 +1257,46 @@
     SeedDeme(source_deme, source_deme.GetGermline().GetLatest());
     SeedDeme(target_deme, target_deme.GetGermline().GetLatest());
     
+  } else if(m_world->GetConfig().DEMES_USE_GERMLINE.Get() == 2) {
+    // @JEB -- New germlines using cGenotype
+    
+    // get germline genotype
+    int germline_genotype_id = source_deme.GetGermlineGenotypeID();
+    cGenotype * germline_genotype = m_world->GetClassificationManager().FindGenotype(germline_genotype_id);
+    assert(germline_genotype);
+    
+    // create a new genome by mutation
+    cCPUMemory new_genome(germline_genotype->GetGenome());
+    const cInstSet& instset = m_world->GetHardwareManager().GetInstSet();
+    cAvidaContext ctx(m_world->GetRandom());
+    
+    if(m_world->GetConfig().GERMLINE_COPY_MUT.Get() > 0.0) {
+      for(int i=0; i<new_genome.GetSize(); ++i) {
+        if(m_world->GetRandom().P(m_world->GetConfig().GERMLINE_COPY_MUT.Get())) {
+          new_genome[i] = instset.GetRandomInst(ctx);
+        }
+      }
+    }
+    
+    if((m_world->GetConfig().GERMLINE_INS_MUT.Get() > 0.0)
+       && m_world->GetRandom().P(m_world->GetConfig().GERMLINE_INS_MUT.Get())) {
+      const unsigned int mut_line = ctx.GetRandom().GetUInt(new_genome.GetSize() + 1);
+      new_genome.Insert(mut_line, instset.GetRandomInst(ctx));
+    }
+    
+    if((m_world->GetConfig().GERMLINE_DEL_MUT.Get() > 0.0)
+       && m_world->GetRandom().P(m_world->GetConfig().GERMLINE_DEL_MUT.Get())) {
+      const unsigned int mut_line = ctx.GetRandom().GetUInt(new_genome.GetSize());
+      new_genome.Remove(mut_line);
+    }
+    
+    //Create a new genotype which is daughter to the old one.
+    cGenotype * new_germline_genotype = m_world->GetClassificationManager().GetGenotype(new_genome, germline_genotype, NULL);
+    source_deme.ReplaceGermline(*new_germline_genotype);
+    target_deme.ReplaceGermline(*new_germline_genotype);
+    SeedDeme(source_deme, *new_germline_genotype);
+    SeedDeme(target_deme, *new_germline_genotype);
+
   } else {
     // Not using germlines; things are much simpler.  Seed the target from the source.
     SeedDeme(source_deme, target_deme);
@@ -1323,6 +1363,21 @@
   }
 }
 
+void cPopulation::SeedDeme(cDeme& _deme, cGenotype& _genotype) {
+  // Kill all the organisms in the deme.
+  _deme.KillAll();
+  _deme.ClearFounders();
+  
+  // Create the specified number of organisms in the deme.
+  for(int i=0; i< m_world->GetConfig().DEMES_REPLICATE_SIZE.Get(); ++i) {
+    int cellid = DemeSelectInjectionCell(_deme, i);
+    InjectGenotype(cellid, &_genotype);
+    DemePostInjection(_deme, cell_array[cellid]);
+    _deme.AddFounder(_genotype);
+  }
+  
+}
+
 /*! Helper method to seed a target deme from the organisms in the source deme.
 All organisms in the target deme are terminated, and a subset of the organisms in
 the source will be cloned to the target.
@@ -1483,8 +1538,8 @@
       }
       
       // Clear the demes.
+      target_deme.ClearFounders();
       target_deme.KillAll();
-      target_deme.ClearFounders();
       
       // Now populate the target (and optionally the source) using InjectGenotype.
       // In the future InjectClone could be used, but this would require the 
@@ -1503,8 +1558,8 @@
       
       // source deme is replaced in the same way as the target
       if (m_world->GetConfig().DEMES_DIVIDE_METHOD.Get() == 0) {
+        source_deme.ClearFounders();
         source_deme.KillAll();
-        source_deme.ClearFounders();
         
         for(int i=0; i<founders.GetSize(); i++) {
           int cellid = DemeSelectInjectionCell(source_deme, i);
@@ -3418,12 +3473,20 @@
   LineageSetupOrganism(GetCell(cell_id).GetOrganism(), 0, lineage_label);
   
   // If we're using germlines, then we have to be a little careful here.
-	if(m_world->GetConfig().DEMES_USE_GERMLINE.Get()) {
+  // This should probably not be within Inject() since we mainly want it to
+  // apply to the START_CREATURE? -- @JEB
+	if(m_world->GetConfig().DEMES_USE_GERMLINE.Get() == 1) {
 		cDeme& deme = deme_array[GetCell(cell_id).GetDemeID()];
 		if(deme.GetGermline().Size()==0) {  
 			deme.GetGermline().Add(GetCell(cell_id).GetOrganism()->GetGenome());
 		}
 	}  
+  else if(m_world->GetConfig().DEMES_USE_GERMLINE.Get() == 2) {
+    //find the genotype we just created from the genome, and save it
+		cDeme& deme = deme_array[GetCell(cell_id).GetDemeID()];
+    cGenotype * genotype = m_world->GetClassificationManager().FindGenotype(genome, lineage_label);
+    deme.ReplaceGermline(*genotype);
+  }
 }
 
 void cPopulation::InjectParasite(const cCodeLabel& label, const cGenome& injected_code, int cell_id)
@@ -3622,7 +3685,7 @@
 {
   assert(cell_id >= 0 && cell_id < cell_array.GetSize());
   if (cell_id < 0 || cell_id >= cell_array.GetSize()) {
-    m_world->GetDriver().RaiseFatalException(1, "InjectGenotype into nonexistant cell");
+    m_world->GetDriver().RaiseFatalException(1, "InjectGenotype into nonexistent cell");
   }
   
   cAvidaContext& ctx = m_world->GetDefaultContext();

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2008-03-28 04:08:18 UTC (rev 2503)
+++ development/source/main/cPopulation.h	2008-03-30 14:53:48 UTC (rev 2504)
@@ -195,6 +195,9 @@
   
   //! Helper method that seeds a deme from the given genome.
   void SeedDeme(cDeme& deme, cGenome& genome);
+
+  //! Helper method that seeds a deme from the given genotype.
+  void SeedDeme(cDeme& _deme, cGenotype& _genotype);
   
   //! Helper method that seeds a target deme from the organisms in the source deme.
   void SeedDeme(cDeme& source_deme, cDeme& target_deme);




More information about the Avida-cvs mailing list