[Avida-SVN] r3259 - in development/source: actions classification main

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Tue May 26 19:53:38 PDT 2009


Author: brysonda
Date: 2009-05-26 22:53:38 -0400 (Tue, 26 May 2009)
New Revision: 3259

Modified:
   development/source/actions/SaveLoadActions.cc
   development/source/classification/cClassificationManager.cc
   development/source/classification/cClassificationManager.h
   development/source/main/cPhenotype.cc
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
Log:
Implement SaveStructuredPopulation and a stub for LoadStructuredPopulation.  Structured population dumps are full phylogenetic tree dumps that include an additional column delineating the currently occupied cells for each genotype (where applicable).  These dumps should be compatible with sexual populations (i.e. they dump parent id 2) and analyze mode (cells column should be ignored).

Modified: development/source/actions/SaveLoadActions.cc
===================================================================
--- development/source/actions/SaveLoadActions.cc	2009-05-24 23:02:16 UTC (rev 3258)
+++ development/source/actions/SaveLoadActions.cc	2009-05-27 02:53:38 UTC (rev 3259)
@@ -130,6 +130,43 @@
 
 
 /*
+ Sets up a population based on a dump file such as written out by
+ detail_pop. It is also possible to append a history file to the dump
+ file, in order to preserve the history of a previous run.
+ 
+ Parameters:
+   filename (string)
+     The name of the file to open.
+   update (int) *optional*
+     ??
+ */
+class cActionLoadStructuredPopulation : public cAction
+{
+private:
+  cString m_filename;
+  int m_update;
+  
+public:
+  cActionLoadStructuredPopulation(cWorld* world, const cString& args) : cAction(world, args), m_filename(""), m_update(-1)
+  {
+    cString largs(args);
+    if (largs.GetSize()) m_filename = largs.PopWord();
+    if (largs.GetSize()) m_update = largs.PopWord().AsInt();
+  }
+  
+  static const cString GetDescription() { return "Arguments: <cString fname> [int update=-1]"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    // set the update if requested
+    if (m_update >= 0) m_world->GetStats().SetCurrentUpdate(m_update);
+    
+    m_world->GetPopulation().LoadStructuredPopulation(m_filename);
+  }
+};
+
+
+/*
  Writes out a line of data for each genotype in the current population. The
  line contains the genome as string, the number of organisms of that genotype,
  and the genotype ID.
@@ -198,6 +235,29 @@
 };
 
 
+class cActionSaveStructuredPopulation : public cAction
+{
+private:
+  cString m_filename;
+  
+public:
+  cActionSaveStructuredPopulation(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.spop", m_world->GetStats().GetUpdate());
+    m_world->GetPopulation().SaveStructuredPopulation(filename);
+  }
+};
+
+
 /*
  Like detail_pop, but for sexual populations. 
  Info for both parents is writen out.
@@ -339,9 +399,11 @@
   action_lib->Register<cActionLoadClone>("LoadClone");
 
   action_lib->Register<cActionLoadPopulation>("LoadPopulation");
+  action_lib->Register<cActionLoadStructuredPopulation>("LoadStructuredPopulation");
 
   action_lib->Register<cActionDumpPopulation>("DumpPopulation");
   action_lib->Register<cActionSavePopulation>("SavePopulation");
+  action_lib->Register<cActionSaveStructuredPopulation>("SaveStructuredPopulation");
   action_lib->Register<cActionSaveSexPopulation>("SaveSexPopulation");
   action_lib->Register<cActionSaveParasitePopulation>("SaveParasitePopulation");
   action_lib->Register<cActionSaveHistoricPopulation>("SaveHistoricPopulation");

Modified: development/source/classification/cClassificationManager.cc
===================================================================
--- development/source/classification/cClassificationManager.cc	2009-05-24 23:02:16 UTC (rev 3258)
+++ development/source/classification/cClassificationManager.cc	2009-05-27 02:53:38 UTC (rev 3259)
@@ -718,10 +718,10 @@
   return true;
 }
 
-bool cClassificationManager::DumpHistoricSexSummary(ofstream& fp)
+bool cClassificationManager::DumpHistoricSexSummary(ofstream& fp, bool header)
 {
   m_genotype_ctl->ResetHistoric(0);
-  DumpDetailSexHeading(fp);
+  if (header) DumpDetailSexHeading(fp);
   for (int i = 0; i < m_genotype_ctl->GetHistoricCount(); i++) {
     DumpDetailedSexEntry(m_genotype_ctl->Get(0), fp);
     m_genotype_ctl->Next(0);

Modified: development/source/classification/cClassificationManager.h
===================================================================
--- development/source/classification/cClassificationManager.h	2009-05-24 23:02:16 UTC (rev 3258)
+++ development/source/classification/cClassificationManager.h	2009-05-27 02:53:38 UTC (rev 3259)
@@ -158,7 +158,7 @@
   bool DumpDetailedSummary(std::ofstream& fp, bool print_mut_steps = false);
   bool DumpDetailedSexSummary(std::ofstream& fp);
   bool DumpHistoricSummary(std::ofstream& fp, int back_dist, bool print_mut_steps = false);
-  bool DumpHistoricSexSummary(std::ofstream& fp);
+  bool DumpHistoricSexSummary(std::ofstream& fp, bool header = true);
   void DumpDetailedEntry(cGenotype* genotype, std::ofstream& fp, bool print_mut_steps = false);
   void DumpDetailedSexEntry(cGenotype* genotype, std::ofstream& fp);
   

Modified: development/source/main/cPhenotype.cc
===================================================================
--- development/source/main/cPhenotype.cc	2009-05-24 23:02:16 UTC (rev 3258)
+++ development/source/main/cPhenotype.cc	2009-05-27 02:53:38 UTC (rev 3259)
@@ -787,7 +787,7 @@
 
   // A few final changes if the parent was supposed to be be considered
   // a second child on the divide.
-  if(m_world->GetConfig().DIVIDE_METHOD.Get() == DIVIDE_METHOD_SPLIT) {
+  if (m_world->GetConfig().DIVIDE_METHOD.Get() == DIVIDE_METHOD_SPLIT) {
     gestation_start = 0;
     cpu_cycles_used = 0;
     time_used = 0;

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2009-05-24 23:02:16 UTC (rev 3258)
+++ development/source/main/cPopulation.cc	2009-05-27 02:53:38 UTC (rev 3259)
@@ -61,6 +61,10 @@
 #include "cTestCPU.h"
 #include "cCPUTestInfo.h"
 
+#include "tKVPair.h"
+#include "tHashTable.h"
+
+
 #include <fstream>
 #include <vector>
 #include <algorithm>
@@ -4299,8 +4303,7 @@
 bool cPopulation::LoadDumpFile(cString filename, int update)
 {
   // set the update if requested
-  if ( update >= 0 )
-    m_world->GetStats().SetCurrentUpdate(update);
+  if (update >= 0) m_world->GetStats().SetCurrentUpdate(update);
   
   // Clear out the population
   for (int i = 0; i < cell_array.GetSize(); i++) KillOrganism(cell_array[i]);
@@ -4420,6 +4423,81 @@
 }
 
 
+bool cPopulation::SaveStructuredPopulation(const cString& filename)
+{
+  cDataFile& df = m_world->GetDataFile(filename);
+  df.WriteRawComment("#filetype genotype_data");
+  df.WriteRawComment("#format id parent_id parent2_id parent_dist num_cpus total_cpus length merit gest_time fitness update_born update_dead depth sequence cells");
+  df.WriteComment("");
+  df.WriteComment("Structured Population Dump");
+  df.WriteTimeStamp();
+  
+  // Build up hash table of all current genotypes and the cells in which the organisms reside
+  tHashTable<int, tKVPair<cGenotype*, tArray<int> >* > genotype_map;
+  
+  for (int i = 0; i < cell_array.GetSize(); i++) {
+    if (cell_array[i].IsOccupied()) {
+      cGenotype* genotype = cell_array[i].GetOrganism()->GetGenotype();
+      tKVPair<cGenotype*, tArray<int> >* map_entry = NULL;
+      if (genotype_map.Find(genotype->GetID(), map_entry)) {
+        map_entry->Value().Push(i);
+      } else {
+        map_entry = new tKVPair<cGenotype*, tArray<int> >(genotype, tArray<int>(0));
+        map_entry->Value().Push(i);
+        genotype_map.Add(genotype->GetID(), map_entry);
+      }
+    }
+  }
+  
+  // Output all current genotypes
+  
+  tArray<tKVPair<cGenotype*, tArray<int> >* > genotype_entries;
+  genotype_map.GetValues(genotype_entries);
+  for (int i = 0; i < genotype_entries.GetSize(); i++) {
+    cGenotype* genotype = genotype_entries[i]->Key();
+    
+    df.Write(genotype->GetID(), "Genotype ID");
+    df.Write(genotype->GetAncestorID(0), "Parent 1 Genotype ID");
+    df.Write(genotype->GetAncestorID(1), "Parent 2 Genotype ID");
+    df.Write(genotype->GetParentDistance(), "Parent Distance");
+    df.Write(genotype->GetNumOrganisms(), "Number of currently living organisms");
+    df.Write(genotype->GetTotalOrganisms(), "Total number of organisms that ever existed");
+    df.Write(genotype->GetLength(), "Genome Length");
+    df.Write(genotype->GetMerit(), "Merit");
+    df.Write(genotype->GetGestationTime(), "Gestation Time");
+    df.Write(genotype->GetFitness(), "Fitness");
+    df.Write(genotype->GetUpdateBorn(), "Update Born");
+    df.Write(genotype->GetUpdateDeactivated(), "Update Deactivated");
+    df.Write(genotype->GetDepth(), "Phylogenetic Depth");
+    df.Write(genotype->GetGenome().AsString(), "Genome Sequence");
+    
+    tArray<int>& cells = genotype_entries[i]->Value();
+    cString cellstr;
+    cellstr.Set("%d", cells[0]);
+    for (int cell_i = 1; cell_i < cells.GetSize(); cell_i++) {
+      cellstr.Set("%s,%d", (const char*)cellstr, cells[cell_i]);
+    }
+    df.Write(cellstr, "Occupied Cell IDs");
+    df.Endl();
+    
+    delete genotype_entries[i];
+  }
+  
+  // Output historic genotypes
+  m_world->GetClassificationManager().DumpHistoricSexSummary(df.GetOFStream(), false);
+  
+  m_world->GetDataFileManager().Remove(filename);
+  return true;
+}
+
+
+bool cPopulation::LoadStructuredPopulation(const cString& filename)
+{
+  // @TODO - implement structured population dump loading
+  return false;
+}
+
+
 bool cPopulation::DumpMemorySummary(ofstream& fp)
 {
   if (fp.good() == false) return false;

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2009-05-24 23:02:16 UTC (rev 3258)
+++ development/source/main/cPopulation.h	2009-05-27 02:53:38 UTC (rev 3259)
@@ -269,6 +269,8 @@
   bool SaveClone(std::ofstream& fp);
   bool LoadClone(std::ifstream& fp);
   bool LoadDumpFile(cString filename, int update);
+  bool SaveStructuredPopulation(const cString& filename);
+  bool LoadStructuredPopulation(const cString& filename);
   bool DumpMemorySummary(std::ofstream& fp);
 
   bool OK();




More information about the Avida-cvs mailing list