[Avida-SVN] r3551 - in branches/biounit/source: actions classification main

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Wed Dec 9 12:00:41 PST 2009


Author: brysonda
Date: 2009-12-09 15:00:41 -0500 (Wed, 09 Dec 2009)
New Revision: 3551

Modified:
   branches/biounit/source/actions/SaveLoadActions.cc
   branches/biounit/source/classification/cBGGenotype.cc
   branches/biounit/source/classification/cBGGenotype.h
   branches/biounit/source/classification/cBGGenotypeManager.cc
   branches/biounit/source/classification/cBGGenotypeManager.h
   branches/biounit/source/classification/cBioGroup.h
   branches/biounit/source/classification/cBioGroupManager.h
   branches/biounit/source/classification/cBioUnit.cc
   branches/biounit/source/classification/cBioUnit.h
   branches/biounit/source/classification/cClassificationManager.cc
   branches/biounit/source/classification/cClassificationManager.h
   branches/biounit/source/main/cPopulation.cc
   branches/biounit/source/main/cPopulation.h
Log:
Add basic support for saving bio group based genotypes.

Modified: branches/biounit/source/actions/SaveLoadActions.cc
===================================================================
--- branches/biounit/source/actions/SaveLoadActions.cc	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/actions/SaveLoadActions.cc	2009-12-09 20:00:41 UTC (rev 3551)
@@ -260,6 +260,29 @@
 };
 
 
+class cActionSaveStructuredPopulationBG : public cAction
+{
+private:
+  cString m_filename;
+  
+public:
+  cActionSaveStructuredPopulationBG(cWorld* world, const cString& args) : cAction(world, args), m_filename("")
+  {
+    cString largs(args);
+    if (largs.GetSize()) m_filename = largs.PopWord();
+  }
+  
+  static const cString GetDescription() { return "Arguments: [string fname='']"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    cString filename(m_filename);
+    if (filename == "") filename.Set("detail-%d.bgspop", m_world->GetStats().GetUpdate());
+    m_world->GetPopulation().SaveStructuredPopulationBG(filename);
+  }
+};
+
+
 /*
  Like detail_pop, but for sexual populations. 
  Info for both parents is writen out.
@@ -406,6 +429,7 @@
   action_lib->Register<cActionDumpPopulation>("DumpPopulation");
   action_lib->Register<cActionSavePopulation>("SavePopulation");
   action_lib->Register<cActionSaveStructuredPopulation>("SaveStructuredPopulation");
+  action_lib->Register<cActionSaveStructuredPopulationBG>("SaveStructuredPopulationBG");
   action_lib->Register<cActionSaveSexPopulation>("SaveSexPopulation");
   action_lib->Register<cActionSaveParasitePopulation>("SaveParasitePopulation");
   action_lib->Register<cActionSaveHistoricPopulation>("SaveHistoricPopulation");

Modified: branches/biounit/source/classification/cBGGenotype.cc
===================================================================
--- branches/biounit/source/classification/cBGGenotype.cc	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cBGGenotype.cc	2009-12-09 20:00:41 UTC (rev 3551)
@@ -25,6 +25,8 @@
 #include "cBGGenotype.h"
 
 #include "cBGGenotypeManager.h"
+#include "cDataFile.h"
+#include "cStringUtil.h"
 
 
 cBGGenotype::cBGGenotype(cBGGenotypeManager* mgr, int in_id, cBioUnit* founder, int update, tArray<cBioGroup*>* parents)
@@ -96,6 +98,32 @@
 }
 
 
+void cBGGenotype::Save(cDataFile& df)
+{
+  df.Write(m_id, "ID");
+  cString parent_str("");
+  if (m_parents.GetSize()) {
+    parent_str += cStringUtil::Stringf("%d", m_parents[0]->GetID());
+    for (int i = 1; i < m_parents.GetSize(); i++) {
+      parent_str += cStringUtil::Stringf(",%d", m_parents[i]->GetID());
+    }
+  }
+  df.Write(parent_str, "Parent ID(s)");
+  df.Write(m_num_organisms, "Number of currently living organisms");
+  df.Write(m_total_organisms, "Total number of organisms that ever existed");
+  df.Write(m_genome.GetGenome().GetSize(), "Genome Length");
+  df.Write(m_merit.Average(), "Average Merit");
+  df.Write(m_gestation_time.Average(), "Average Gestation Time");
+  df.Write(m_fitness.Average(), "Average Fitness");
+  df.Write(m_update_born, "Update Born");
+  df.Write(m_update_deactivated, "Update Deactivated");
+  df.Write(m_depth, "Phylogenetic Depth");
+  df.Write(m_genome.GetGenome().AsString(), "Genome Sequence");
+  
+}
+
+
+
 bool cBGGenotype::Matches(cBioUnit* bu)
 {
   // Handle source branching

Modified: branches/biounit/source/classification/cBGGenotype.h
===================================================================
--- branches/biounit/source/classification/cBGGenotype.h	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cBGGenotype.h	2009-12-09 20:00:41 UTC (rev 3551)
@@ -94,11 +94,14 @@
   // cBioGroup Interface Methods
   int GetRoleID() const;
   const cString& GetRole() const;  
+  int GetID() const { return m_id; }
   
   cBioGroup* ClassifyNewBioUnit(cBioUnit* bu, tArray<cBioGroup*>* parents = NULL);
   void RemoveBioUnit(cBioUnit* bu);
   
   int GetDepth() const { return m_depth; }
+  
+  void Save(cDataFile& df);
 
   
   // Genotype Specific Methods
@@ -111,7 +114,6 @@
   inline bool IsThreshold() const { return m_threshold; }
   inline bool IsActive() const { return m_active; }
   
-  inline int GetID() const { return m_id; }
   inline int GetUpdateBorn() const { return m_update_born; }
   inline int GetUpdateDeactivated() const { return m_update_deactivated; }
   

Modified: branches/biounit/source/classification/cBGGenotypeManager.cc
===================================================================
--- branches/biounit/source/classification/cBGGenotypeManager.cc	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cBGGenotypeManager.cc	2009-12-09 20:00:41 UTC (rev 3551)
@@ -25,6 +25,7 @@
 #include "cBGGenotypeManager.h"
 
 #include "cBGGenotype.h"
+#include "cDataFile.h"
 #include "cGenome.h"
 #include "cStats.h"
 #include "cStringUtil.h"
@@ -67,6 +68,20 @@
 }
 
 
+void cBGGenotypeManager::SaveBioGroups(cDataFile& df)
+{
+  // @TODO - Just dump historic for now.  Need structure output format to support top down save
+  //         With a structured save (and save params passed through), a "structured population save" could be attained
+  //         by simply calling the bio group save.  As it stands right now, cPopulation must decorate columns with additional
+  //         data about active genotypes, yet the bio group interface really shouldn't know about active/inactive genotypes.
+  //         Thus it is not proper to split bgm save into a save historic and save active.  Right now we'll just make
+  //         cPopulation do the work.
+  
+  tListIterator<cBGGenotype> list_it(m_historic);
+  while (list_it.Next() != NULL) list_it.Get()->Save(df);
+}
+
+
 cBGGenotype* cBGGenotypeManager::ClassifyNewBioUnit(cBioUnit* bu, tArray<cBioGroup*>* parents)
 {
   int list_num = hashGenome(bu->GetMetaGenome().GetGenome());

Modified: branches/biounit/source/classification/cBGGenotypeManager.h
===================================================================
--- branches/biounit/source/classification/cBGGenotypeManager.h	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cBGGenotypeManager.h	2009-12-09 20:00:41 UTC (rev 3551)
@@ -71,6 +71,8 @@
   
   void UpdateReset();
   
+  void SaveBioGroups(cDataFile& df);
+  
   // Genotype Manager Methods
   cBGGenotype* ClassifyNewBioUnit(cBioUnit* bu, tArray<cBioGroup*>* parents);
   void AdjustGenotype(cBGGenotype* genotype, int old_size, int new_size);

Modified: branches/biounit/source/classification/cBioGroup.h
===================================================================
--- branches/biounit/source/classification/cBioGroup.h	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cBioGroup.h	2009-12-09 20:00:41 UTC (rev 3551)
@@ -32,8 +32,10 @@
 #include "cBioUnit.h"
 #endif
 
+class cDataFile;
 template<typename T> class tArray;
 
+
 class cBioGroup
 {
 protected:
@@ -46,12 +48,16 @@
   
   virtual int GetRoleID() const = 0;
   virtual const cString& GetRole() const = 0;
+  virtual int GetID() const = 0;
   
   virtual cBioGroup* ClassifyNewBioUnit(cBioUnit* bu, tArray<cBioGroup*>* parents = NULL) = 0;
   virtual void RemoveBioUnit(cBioUnit* bu) = 0;
   
   virtual int GetDepth() const = 0;
   
+  virtual void Save(cDataFile& df) = 0;
+  
+  
   int GetReferenceCount() const { return m_a_refs + m_p_refs; }
   int GetActiveReferenceCount() const { return m_a_refs; }
   void AddActiveReference() { m_a_refs++; }

Modified: branches/biounit/source/classification/cBioGroupManager.h
===================================================================
--- branches/biounit/source/classification/cBioGroupManager.h	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cBioGroupManager.h	2009-12-09 20:00:41 UTC (rev 3551)
@@ -31,6 +31,7 @@
 
 class cBioGroup;
 class cBioUnit;
+class cDataFile;
 
 
 class cBioGroupManager
@@ -49,6 +50,8 @@
   
   virtual void UpdateReset() = 0;
     
+  virtual void SaveBioGroups(cDataFile& df) = 0;
+
   inline int GetRoleID() const { return m_role_id; }
   inline const cString& GetRole() const { return m_role; }
   

Modified: branches/biounit/source/classification/cBioUnit.cc
===================================================================
--- branches/biounit/source/classification/cBioUnit.cc	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cBioUnit.cc	2009-12-09 20:00:41 UTC (rev 3551)
@@ -25,6 +25,7 @@
 #include "cBioUnit.h"
 
 #include "cBioGroup.h"
+#include "cString.h"
 #include "tArrayMap.h"
 #include "tArraySet.h"
 
@@ -35,7 +36,14 @@
   for (int i = 0; i < m_bio_groups.GetSize(); i++) m_bio_groups[i]->RemoveBioUnit(this);
 }
 
+cBioGroup* cBioUnit::GetBioGroup(const cString& role) const
+{
+  for (int i = 0; i < m_bio_groups.GetSize(); i++) if (m_bio_groups[i]->GetRole() == role) return m_bio_groups[i];
+  return NULL;
+}
 
+
+
 void cBioUnit::SelfClassify(const tArray<const tArray<cBioGroup*>*>& parents)
 {
   assert(parents.GetSize() > 0);

Modified: branches/biounit/source/classification/cBioUnit.h
===================================================================
--- branches/biounit/source/classification/cBioUnit.h	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cBioUnit.h	2009-12-09 20:00:41 UTC (rev 3551)
@@ -52,6 +52,7 @@
   virtual const cMetaGenome& GetMetaGenome() const = 0;
   
   const tArray<cBioGroup*>& GetBioGroups() const { return m_bio_groups; }
+  cBioGroup* GetBioGroup(const cString& role) const;
   
   void AddClassification(cBioGroup* bg) { m_bio_groups.Push(bg); }
   void SelfClassify(const tArray<const tArray<cBioGroup*>*>& parents);

Modified: branches/biounit/source/classification/cClassificationManager.cc
===================================================================
--- branches/biounit/source/classification/cClassificationManager.cc	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cClassificationManager.cc	2009-12-09 20:00:41 UTC (rev 3551)
@@ -92,7 +92,17 @@
   }
 }
 
+void cClassificationManager::SaveBioGroups(const cString& role, cDataFile& df)
+{
+  for (int i = 0; i < m_bgms.GetSize(); i++) {
+    if (m_bgms[i]->GetRole() == role) {
+      m_bgms[i]->SaveBioGroups(df);
+      return;
+    }
+  }
+}
 
+
 void cClassificationManager::UpdateReset()
 {
   // Notify all bio group managers of the update reset

Modified: branches/biounit/source/classification/cClassificationManager.h
===================================================================
--- branches/biounit/source/classification/cClassificationManager.h	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/classification/cClassificationManager.h	2009-12-09 20:00:41 UTC (rev 3551)
@@ -57,6 +57,7 @@
 class cAvidaContext;
 class cBioGroupManager;
 class cBioUnit;
+class cDataFile;
 class cLineage;
 class cOrganism;
 class cWorld;
@@ -114,8 +115,10 @@
   bool RegisterBioGroupManager(cBioGroupManager* bgm, const cString& role);
   
   void ClassifyNewBioUnit(cBioUnit* bu);
+
+  void SaveBioGroups(const cString& role, cDataFile& df);
+
   
-  
   // Genotype Manipulation
   cGenotype* GetGenotype(const cGenome& in_genome, cGenotype* parent1, cGenotype* parent2);
   cGenotype* GetGenotypeInjected(const cGenome& in_genome, int lineage_label);

Modified: branches/biounit/source/main/cPopulation.cc
===================================================================
--- branches/biounit/source/main/cPopulation.cc	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/main/cPopulation.cc	2009-12-09 20:00:41 UTC (rev 3551)
@@ -26,6 +26,7 @@
 #include "cPopulation.h"
 
 #include "cAvidaContext.h"
+#include "cBioGroup.h"
 #include "cChangeList.h"
 #include "cClassificationManager.h"
 #include "cCodeLabel.h"
@@ -4727,7 +4728,68 @@
   return true;
 }
 
+bool cPopulation::SaveStructuredPopulationBG(const cString& filename)
+{
+  cDataFile& df = m_world->GetDataFile(filename);
+  df.WriteRawComment("#filetype genotype_data");
+  df.WriteRawComment("#format id parents num_cpus total_cpus length merit gest_time fitness update_born update_dead depth sequence cells gest_offset");
+  df.WriteComment("");
+  df.WriteComment("Structured Population Save (BioGroup)");
+  df.WriteTimeStamp();
+  
+  // Build up hash table of all current genotypes and the cells in which the organisms reside
+  tHashTable<int, tKVPair<cBioGroup*, tArray<sOrgInfo> >* > genotype_map;
+  
+  for (int i = 0; i < cell_array.GetSize(); i++) {
+    if (cell_array[i].IsOccupied()) {
+      cOrganism* org = cell_array[i].GetOrganism();
+      cBioGroup* genotype = org->GetBioGroup("genotype");
+      int offset = org->GetPhenotype().GetCPUCyclesUsed();
+      
+      tKVPair<cBioGroup*, tArray<sOrgInfo> >* map_entry = NULL;
+      if (genotype_map.Find(genotype->GetID(), map_entry)) {
+        map_entry->Value().Push(sOrgInfo(i, offset));
+      } else {
+        map_entry = new tKVPair<cBioGroup*, tArray<sOrgInfo> >(genotype, tArray<sOrgInfo>(0));
+        map_entry->Value().Push(sOrgInfo(i, offset));
+        genotype_map.Add(genotype->GetID(), map_entry);
+      }
+    }
+  }
+  
+  // Output all current genotypes
+  
+  tArray<tKVPair<cBioGroup*, tArray<sOrgInfo> >* > genotype_entries;
+  genotype_map.GetValues(genotype_entries);
+  for (int i = 0; i < genotype_entries.GetSize(); i++) {
+    cBioGroup* genotype = genotype_entries[i]->Key();
+    
+    genotype->Save(df);
+    
+    tArray<sOrgInfo>& cells = genotype_entries[i]->Value();
+    cString cellstr;
+    cString offsetstr;
+    cellstr.Set("%d", cells[0].cell_id);
+    offsetstr.Set("%d", cells[0].offset);
+    for (int cell_i = 1; cell_i < cells.GetSize(); cell_i++) {
+      cellstr += cStringUtil::Stringf(",%d", cells[cell_i].cell_id);
+      offsetstr += cStringUtil::Stringf(",%d", cells[cell_i].offset);
+    }
+    df.Write(cellstr, "Occupied Cell IDs");
+    df.Write(offsetstr, "Gestation (CPU) Cycle Offsets");
+    df.Endl();
+    
+    delete genotype_entries[i];
+  }
+  
+  // Output historic genotypes
+  m_world->GetClassificationManager().SaveBioGroups("genotype", df);
+  
+  m_world->GetDataFileManager().Remove(filename);
+  return true;
+}
 
+
 bool cPopulation::LoadStructuredPopulation(const cString& filename)
 {
   // @TODO - build in support for verifying population dimensions

Modified: branches/biounit/source/main/cPopulation.h
===================================================================
--- branches/biounit/source/main/cPopulation.h	2009-12-09 18:49:46 UTC (rev 3550)
+++ branches/biounit/source/main/cPopulation.h	2009-12-09 20:00:41 UTC (rev 3551)
@@ -242,6 +242,7 @@
   bool LoadClone(std::ifstream& fp);
   bool LoadDumpFile(cString filename, int update);
   bool SaveStructuredPopulation(const cString& filename);
+  bool SaveStructuredPopulationBG(const cString& filename);
   bool LoadStructuredPopulation(const cString& filename);
   bool DumpMemorySummary(std::ofstream& fp);
 




More information about the Avida-cvs mailing list