[Avida-SVN] r1908 - in development: documentation source/actions source/analyze source/main

matt at myxo.css.msu.edu matt at myxo.css.msu.edu
Wed Aug 8 13:29:49 PDT 2007


Author: matt
Date: 2007-08-08 16:29:48 -0400 (Wed, 08 Aug 2007)
New Revision: 1908

Modified:
   development/documentation/actions.html
   development/source/actions/PrintActions.cc
   development/source/analyze/cAnalyze.cc
   development/source/analyze/cAnalyze.h
   development/source/analyze/cAnalyzeGenotype.cc
   development/source/main/cPhenPlastGenotype.cc
   development/source/main/cPhenPlastGenotype.h
Log:
Added additional functionality to handle plastic phenotype analysis.

Modified: development/documentation/actions.html
===================================================================
--- development/documentation/actions.html	2007-08-08 14:54:58 UTC (rev 1907)
+++ development/documentation/actions.html	2007-08-08 20:29:48 UTC (rev 1908)
@@ -130,6 +130,7 @@
     </td>
     <td>
       <a href="#PrintPhenotypeStatus">PrintPhenotypeStatus</a>
+      <br /><a href="#PrintPhenotypicPlasticity">PrintPhenotypicPlasticity</a>
       <br /><a href="#PrintPopulationDistanceData">PrintPopulationDistanceData</a>
       <br /><a href="#PrintRelativeFitnessHistogram">PrintRelativeFitnessHistogram</a>
       <br /><a href="#PrintResourceData">PrintResourceData</a>
@@ -563,6 +564,18 @@
 </li>
 
 <li>
+  <strong><a name="PrintPhenotypicPlasticity">PrintPhenotypicPlasticity</a></strong>
+  [<span class="cmdarg">string filename="phenplast.dat | phenplast-Update.dat"</span>] [<span class="cmdarg">int num_trials</span>]
+  <p>
+  This function will provided detailed information about the phenotypic varients
+  of the current population/batch.  If this command is executed in run mode, the
+  <kbd>filename</kbd> will be appeneded with <kbd>-Update.dat</kbd> where <kbd>Update</kbd>
+  is the current update.  In analyze mode, the default file is merely </kbd>phenplast.dat</kbd>.
+  The output file contains the following: id, parent_id, phenotypic_varient_number, frequency, fitness, merit, 
+  gestation_time, and task counts for each phenotypic variant of each genotype.
+  </p>
+</li>
+<li>
   <strong><a name="PrintGeneticDistanceData">PrintGeneticDistanceData</a></strong>
   [<span class="cmdarg">string ref_creature_file='START_CREATURE'</span>]
   [<span class="cmdarg">string filename='genetic_distance.dat'</span>]

Modified: development/source/actions/PrintActions.cc
===================================================================
--- development/source/actions/PrintActions.cc	2007-08-08 14:54:58 UTC (rev 1907)
+++ development/source/actions/PrintActions.cc	2007-08-08 20:29:48 UTC (rev 1908)
@@ -40,6 +40,8 @@
 #include "cInjectGenotype.h"
 #include "cInstSet.h"
 #include "cOrganism.h"
+#include "cPhenPlastGenotype.h"
+#include "cPlasticPhenotype.h"
 #include "cPopulation.h"
 #include "cPopulationCell.h"
 #include "cSpecies.h"
@@ -1483,7 +1485,100 @@
 };
 
 
+
 /*
+ This function will go through all genotypes in the population/batch and
+ allow you to retrieve information about the different plastic phenotypes.
+ Arguments:
+    filename    name of output file in analyze mode; root of filename in
+                run mode (-update.dat appeneded in run mode).
+                [default: phenpalst-update.dat in run-mode, phenplast.dat in analyze]
+    trials      number of test_cpu recalculations for each genotype [default: 1000]
+*/
+class cActionPrintPhenotypicPlasticity : public cAction
+{
+  private:
+    cString m_filename;
+    int     m_num_trials;
+  
+  private:
+    void PrintHeader(ofstream& fot)
+    {
+      fot << "# Phenotypic Plasticity" << endl
+          << "# Format: " << endl
+          << "# genotype id" << endl
+          << "# parent genotype id" << endl
+          << "# phenotypic varient number" << endl
+          << "# varient frequency" << endl
+          << "# fitness" << endl
+          << "# merit" << endl
+          << "# gestation time" << endl;
+      for (int k = 0; k < m_world->GetEnvironment().GetNumTasks(); k++)
+        fot << "# task." << k << endl;
+      fot << endl;
+    }
+    
+    void PrintPPG(ofstream& fot, const cPhenPlastGenotype* ppgen, int id, int pid)
+    {
+      
+      for (int k = 0; k < ppgen->GetNumPhenotypes(); k++){
+        const cPlasticPhenotype* pp = ppgen->GetPlasticPhenotype(k);
+        fot << id << " "
+            << pid << " "
+            << k << " "
+            << pp->GetFrequency() << " "
+            << pp->GetFitness() << " "
+            << pp->GetMerit() << " "
+            << pp->GetGestationTime() << " ";
+        tArray<int> tasks = pp->GetLastTaskCount();
+        for (int t = 0; t < tasks.GetSize(); t++)
+          fot << tasks[t] << " ";
+        fot << endl;
+      }
+    }
+    
+  public:
+  cActionPrintPhenotypicPlasticity(cWorld* world, const cString& args)
+      : cAction(world,  args)
+    {
+        cString largs(args);
+        m_filename = (largs.GetSize()) ? largs.PopWord() : "phenplast";
+        m_num_trials = (largs.GetSize()) ? largs.PopWord().AsInt() : 1000;
+    }
+    
+    static const cString GetDescription() { return "Arguments: [string filename='phenplast'] [int num_trials=1000]"; };
+    
+    void Process(cAvidaContext& ctx)
+    {
+      if (ctx.GetAnalyzeMode()){ // Analyze mode
+        cString this_path = m_filename;
+        ofstream& fot = m_world->GetDataFileOFStream(this_path);
+        PrintHeader(fot);
+        tListIterator<cAnalyzeGenotype> batch_it(m_world->GetAnalyze().GetCurrentBatch().List());
+				cAnalyzeGenotype* genotype = NULL;
+				while((genotype = batch_it.Next())){
+					const cPhenPlastGenotype* ppgen = new cPhenPlastGenotype(genotype->GetGenome(), m_num_trials, m_world, ctx);
+          PrintPPG(fot, ppgen, genotype->GetID(), genotype->GetParentID());
+          delete ppgen;
+				}
+        m_world->GetDataFileManager().Remove(this_path);
+      } else{  // Run mode
+        cString this_path = m_filename + "-" + cStringUtil::Convert(m_world->GetStats().GetUpdate()) + ".dat";
+        ofstream& fot = m_world->GetDataFileOFStream(this_path);
+        PrintHeader(fot);
+        cGenotype* genotype = m_world->GetClassificationManager().GetBestGenotype();
+        for (int k = 0; k < m_world->GetClassificationManager().GetGenotypeCount(); k++){
+          const cPhenPlastGenotype* ppgen = new cPhenPlastGenotype(genotype->GetGenome(), m_num_trials, m_world, ctx);
+          PrintPPG(fot, ppgen, genotype->GetID(), genotype->GetParentID());
+          delete ppgen;
+          genotype = genotype->GetNext();
+        }
+        m_world->GetDataFileManager().Remove(this_path);
+      }
+    }
+};
+
+/*
  This function goes through all genotypes currently present in the soup,
  and writes into an output file the average Hamming distance between the
  creatures in the population and a given reference genome.
@@ -2385,7 +2480,8 @@
   action_lib->Register<cActionPrintDebug>("PrintDebug");
 
   action_lib->Register<cActionPrintGenotypes>("PrintGenotypes");
-
+  action_lib->Register<cActionPrintPhenotypicPlasticity>("PrintPhenotypicPlasticity");
+  
   action_lib->Register<cActionTestDominant>("TestDominant");
   action_lib->Register<cActionPrintTaskSnapshot>("PrintTaskSnapshot");
   action_lib->Register<cActionPrintViableTasksData>("PrintViableTasksData");

Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc	2007-08-08 14:54:58 UTC (rev 1907)
+++ development/source/analyze/cAnalyze.cc	2007-08-08 20:29:48 UTC (rev 1908)
@@ -7034,22 +7034,6 @@
 }
 
 
-void cAnalyze::CommandAnalyzePlasticity(cString cur_string)
-{
-  tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
-  cAnalyzeGenotype* genotype;
-  while ( (genotype = batch_it.Next()) != NULL ){
-    cerr << "Using Genome: " << genotype->GetGenome().AsString() << endl;
-    cPhenPlastGenotype PPG(genotype->GetGenome(), 1000, m_world, m_ctx);
-    cerr << "There are " << PPG.GetNumPhenotypes() << " phenotype(s) for this genome.\n";
-    for (int k = 0; k < PPG.GetNumPhenotypes(); k++)
-      cerr << "\t Phenotype " << k << " has fitness " << PPG.GetPlasticPhenotype(k).GetFitness() 
-           << " with frequency " << PPG.GetPlasticPhenotype(k).GetFrequency() <<  endl;
-    cerr << endl;
-  }
-  cerr << "here" << endl;
-  
-}
 
 
 //////////////// Control...
@@ -8369,7 +8353,6 @@
   AddLibraryDef("ANALYZE_POP_COMPLEXITY", &cAnalyze::AnalyzePopComplexity);
   AddLibraryDef("MAP_DEPTH", &cAnalyze::CommandMapDepth);
   // (Untested) AddLibraryDef("PAIRWISE_ENTROPY", &cAnalyze::CommandPairwiseEntropy); 
-  AddLibraryDef("ANALYZE_PLASTICITY", &cAnalyze::CommandAnalyzePlasticity); 
   
   // Population comparison commands...
   AddLibraryDef("HAMMING", &cAnalyze::CommandHamming);

Modified: development/source/analyze/cAnalyze.h
===================================================================
--- development/source/analyze/cAnalyze.h	2007-08-08 14:54:58 UTC (rev 1907)
+++ development/source/analyze/cAnalyze.h	2007-08-08 20:29:48 UTC (rev 1908)
@@ -221,6 +221,7 @@
   void SampleGenotypes(cString cur_string);
   void KeepTopGenotypes(cString cur_string);
   void TruncateLineage(cString cur_string);
+  
 
   // Direct Output Commands...
   void CommandPrint(cString cur_string);
@@ -250,8 +251,7 @@
   void CommandMapMutations(cString cur_string);
   void CommandMapDepth(cString cur_string);
   void CommandPairwiseEntropy(cString cur_string);
-  void CommandAnalyzePlasticity(cString cur_string);
-
+ 
   // Population Comparison Commands...
   void CommandHamming(cString cur_string);
   void CommandLevenstein(cString cur_string);

Modified: development/source/analyze/cAnalyzeGenotype.cc
===================================================================
--- development/source/analyze/cAnalyzeGenotype.cc	2007-08-08 14:54:58 UTC (rev 1907)
+++ development/source/analyze/cAnalyzeGenotype.cc	2007-08-08 20:29:48 UTC (rev 1908)
@@ -420,22 +420,22 @@
   m_world->GetHardwareManager().GetInstSet() = env_inst_set_backup;
   
   // The most likely phenotype will be assigned to the phenotype stats
-  const cPlasticPhenotype likely_phenotype = recalc_data.GetMostLikelyPhenotype();
+  const cPlasticPhenotype* likely_phenotype = recalc_data.GetMostLikelyPhenotype();
   
-  viable         = likely_phenotype.IsViable();
-  m_env_inputs   = likely_phenotype.GetEnvInputs()[0];
-  executed_flags = likely_phenotype.GetExecutedFlags();
-  length         = likely_phenotype.GetGenomeLength();
-  copy_length    = likely_phenotype.GetCopiedSize();
-  exe_length     = likely_phenotype.GetExecutedSize();
-  merit          = likely_phenotype.GetMerit().GetDouble();
-  gest_time      = likely_phenotype.GetGestationTime();
-  fitness        = likely_phenotype.GetFitness();
-  errors         = likely_phenotype.GetLastNumErrors();
-  div_type       = likely_phenotype.GetDivType();
-  mate_id        = likely_phenotype.MateSelectID();
-  task_counts    = likely_phenotype.GetLastTaskCount();
-  task_qualities = likely_phenotype.GetLastTaskQuality();
+  viable         = likely_phenotype->IsViable();
+  m_env_inputs   = likely_phenotype->GetEnvInputs()[0];
+  executed_flags = likely_phenotype->GetExecutedFlags();
+  length         = likely_phenotype->GetGenomeLength();
+  copy_length    = likely_phenotype->GetCopiedSize();
+  exe_length     = likely_phenotype->GetExecutedSize();
+  merit          = likely_phenotype->GetMerit().GetDouble();
+  gest_time      = likely_phenotype->GetGestationTime();
+  fitness        = likely_phenotype->GetFitness();
+  errors         = likely_phenotype->GetLastNumErrors();
+  div_type       = likely_phenotype->GetDivType();
+  mate_id        = likely_phenotype->MateSelectID();
+  task_counts    = likely_phenotype->GetLastTaskCount();
+  task_qualities = likely_phenotype->GetLastTaskQuality();
   
   // Setup a new parent stats if we have a parent to work with.
   if (parent_genotype != NULL) {

Modified: development/source/main/cPhenPlastGenotype.cc
===================================================================
--- development/source/main/cPhenPlastGenotype.cc	2007-08-08 14:54:58 UTC (rev 1907)
+++ development/source/main/cPhenPlastGenotype.cc	2007-08-08 20:29:48 UTC (rev 1908)
@@ -103,15 +103,15 @@
 }
 
 
-const cPlasticPhenotype& cPhenPlastGenotype::GetPlasticPhenotype(int num) const
+const cPlasticPhenotype* cPhenPlastGenotype::GetPlasticPhenotype(int num) const
 {
   assert(num >= 0 && num < (int) m_unique.size() && m_unique.size() > 0);
   UniquePhenotypes::const_iterator it = m_unique.begin();
   for (int k = 0; k < num; k++, it++);
-  return *static_cast<cPlasticPhenotype*>(*it);
+  return static_cast<cPlasticPhenotype*>(*it);
 }
 
-const cPlasticPhenotype& cPhenPlastGenotype::GetMostLikelyPhenotype() const
+const cPlasticPhenotype* cPhenPlastGenotype::GetMostLikelyPhenotype() const
 {
   assert(m_unique.size() > 0);
   UniquePhenotypes::const_iterator it = m_unique.begin();
@@ -120,10 +120,10 @@
     if ( static_cast<cPlasticPhenotype*>(*it)->GetFrequency() > 
          static_cast<cPlasticPhenotype*>(*ret_it)->GetFrequency() )
       ret_it = it;
-  return *static_cast<cPlasticPhenotype*>(*ret_it);
+  return static_cast<cPlasticPhenotype*>(*ret_it);
 }
 
-const cPlasticPhenotype& cPhenPlastGenotype::GetHighestFitnessPhenotype() const
+const cPlasticPhenotype* cPhenPlastGenotype::GetHighestFitnessPhenotype() const
 {
   assert(m_unique.size() > 0);
   UniquePhenotypes::const_iterator it = m_unique.begin();
@@ -132,5 +132,5 @@
     if ( static_cast<cPlasticPhenotype*>(*it)->GetFitness() > 
          static_cast<cPlasticPhenotype*>(*ret_it)->GetFitness() )
       ret_it = it;
-  return *static_cast<cPlasticPhenotype*>(*ret_it);
+  return static_cast<cPlasticPhenotype*>(*ret_it);
 }

Modified: development/source/main/cPhenPlastGenotype.h
===================================================================
--- development/source/main/cPhenPlastGenotype.h	2007-08-08 14:54:58 UTC (rev 1907)
+++ development/source/main/cPhenPlastGenotype.h	2007-08-08 20:29:48 UTC (rev 1908)
@@ -89,9 +89,9 @@
     double GetMaximumFrequency() const  { return m_max_freq; }
     double GetMaximumFitnessFrequency() const {return m_max_fit_freq;}
     double GetMinimumFitnessFrequency() const {return m_min_fit_freq;}
-    const cPlasticPhenotype& GetPlasticPhenotype(int num) const;
-    const cPlasticPhenotype& GetMostLikelyPhenotype() const;
-    const cPlasticPhenotype& GetHighestFitnessPhenotype() const;
+    const cPlasticPhenotype* GetPlasticPhenotype(int num) const;
+    const cPlasticPhenotype* GetMostLikelyPhenotype() const;
+    const cPlasticPhenotype* GetHighestFitnessPhenotype() const;
     
     
 };




More information about the Avida-cvs mailing list