[Avida-SVN] r1144 - in branches/coopcomm/source: actions main

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Thu Dec 14 06:21:26 PST 2006


Author: dknoester
Date: 2006-12-14 09:21:26 -0500 (Thu, 14 Dec 2006)
New Revision: 1144

Modified:
   branches/coopcomm/source/actions/PrintActions.cc
   branches/coopcomm/source/main/cPopulation.cc
   branches/coopcomm/source/main/cStats.cc
   branches/coopcomm/source/main/cStats.h
Log:
Added PrintDemeGenomes, that prints the genome IDs of all organisms in the deme with maximum fitness.

Modified: branches/coopcomm/source/actions/PrintActions.cc
===================================================================
--- branches/coopcomm/source/actions/PrintActions.cc	2006-12-13 23:54:42 UTC (rev 1143)
+++ branches/coopcomm/source/actions/PrintActions.cc	2006-12-14 14:21:26 UTC (rev 1144)
@@ -70,6 +70,7 @@
 STATS_OUT_FILE(PrintCellData, cell_log.dat);
 STATS_OUT_FILE(PrintMessageData, message_log.dat);
 STATS_OUT_FILE(PrintDemeFitness, deme_fitness.dat);
+STATS_OUT_FILE(PrintDemeGenomes, deme_genomes.dat);
 STATS_OUT_FILE(PrintIDData, id_log.dat);
 STATS_OUT_FILE(PrintMessageDataPerUpdate, message.dat);
 STATS_OUT_FILE(PrintMessageSnapshot, msg_snapshot.dat);
@@ -1532,6 +1533,7 @@
   action_lib->Register<cActionPrintCellData>("PrintCellData");
   action_lib->Register<cActionPrintMessageData>("PrintMessageData"); 
   action_lib->Register<cActionPrintDemeFitness>("PrintDemeFitness");
+  action_lib->Register<cActionPrintDemeGenomes>("PrintDemeGenomes");
   action_lib->Register<cActionPrintIDData>("PrintIDData");  
   action_lib->Register<cActionPrintMessageDataPerUpdate>("PrintMessageDataPerUpdate");
   action_lib->Register<cActionPrintMessageSnapshot>("PrintMessageSnapshot");

Modified: branches/coopcomm/source/main/cPopulation.cc
===================================================================
--- branches/coopcomm/source/main/cPopulation.cc	2006-12-13 23:54:42 UTC (rev 1143)
+++ branches/coopcomm/source/main/cPopulation.cc	2006-12-14 14:21:26 UTC (rev 1144)
@@ -871,6 +871,9 @@
 }
 
 
+/*! Designed to be used with the adaptable compete demes framework defined in 
+PopulationActions.cc.
+*/
 void cPopulation::CompeteDemes(const std::vector<double>& deme_fitness) {
   double total_fitness = std::accumulate(deme_fitness.begin(), 
                                          deme_fitness.end(), 0.0);
@@ -881,10 +884,15 @@
   
   if(total_fitness > 0.0) {
     for (int i = 0; i < num_demes; i++) {
+      // Pick a random number between 0 and the total fitness of all demes.
       double birth_choice = (double) m_world->GetRandom().GetDouble(total_fitness);
       double test_total = 0;
       for (int test_deme = 0; test_deme < num_demes; test_deme++) {
+        // Start from the 0th deme, and keep a running total of the fitness 
+        // for each deme that we visit.
         test_total += deme_fitness[test_deme];
+        // Once this running total has exceeded the random number we picked above,
+        // select the current deme as one that will be replicated.
         if (birth_choice < test_total) {
           new_demes[i] = test_deme;
           break;

Modified: branches/coopcomm/source/main/cStats.cc
===================================================================
--- branches/coopcomm/source/main/cStats.cc	2006-12-13 23:54:42 UTC (rev 1143)
+++ branches/coopcomm/source/main/cStats.cc	2006-12-14 14:21:26 UTC (rev 1144)
@@ -19,6 +19,7 @@
 #include "cPopulation.h"
 #include "cPopulationCell.h"
 #include "cOrganism.h"
+#include "cGenotype.h"
 
 #include <float.h>
 #include <math.h>
@@ -929,8 +930,15 @@
     std::copy(deme_fitness.begin(), deme_fitness.end(), m_maxDemeFitnesses.begin());
     return;
   }
+
+  m_maxDemeIndex = 0;
+  double max_fitness = -1.0; // < 0 to handle the 0-fitness case.
   for(unsigned int i=0; i<deme_fitness.size(); ++i) {
     m_maxDemeFitnesses[i] = std::max(deme_fitness[i], m_maxDemeFitnesses[i]);
+    if(m_maxDemeFitnesses[i] > max_fitness) {
+      m_maxDemeIndex = i;
+      max_fitness = m_maxDemeFitnesses[i];
+    }
   }
 }
 
@@ -1036,17 +1044,40 @@
   df.WriteColumnDesc("<max fitness of deme>...");
   
   df.WriteAnonymous(m_update);
-  df.WriteAnonymous(*std::max_element(m_maxDemeFitnesses.begin(), m_maxDemeFitnesses.end()));
+  double max = 0.0;
+  if(m_maxDemeFitnesses.size()>0) {
+    df.WriteAnonymous(*std::max_element(m_maxDemeFitnesses.begin(), m_maxDemeFitnesses.end()));
   
-  for(unsigned int i=0; i<m_maxDemeFitnesses.size(); ++i) {
-    df.WriteAnonymous(m_maxDemeFitnesses[i]);
+    for(unsigned int i=0; i<m_maxDemeFitnesses.size(); ++i) {
+      df.WriteAnonymous(m_maxDemeFitnesses[i]);
+    }
   }
-
   df.Endl();
   m_maxDemeFitnesses.clear();
 }
 
 
+/*! Prints out the genomes of all the occupied cells in the deme with the
+max fitness.
+*/
+void cStats::PrintDemeGenomes(const cString& filename) {
+  cDataFile& df = m_world->GetDataFile(filename);
+  df.WriteComment("Genomes that comprise the deme with maximum fitness.");
+  df.WriteColumnDesc("Update.");
+  df.WriteColumnDesc("Genome ID...");
+  df.WriteAnonymous(m_update);
+  
+  cPopulation::t_CellRangeList demes = m_world->GetPopulation().GetDemes();
+  assert(demes.size()>m_maxDemeIndex);
+  for(cPopulation::t_CellIterator i=demes[m_maxDemeIndex].first;
+      i!=demes[m_maxDemeIndex].second; ++i) {
+    if(i->IsOccupied()) {
+      df.WriteAnonymous(i->GetOrganism()->GetGenotype()->GetID());
+    }
+  }
+  df.Endl();
+}
+
 /*! The format of this data file is:
 <update> <cell id>:<id>...
 

Modified: branches/coopcomm/source/main/cStats.h
===================================================================
--- branches/coopcomm/source/main/cStats.h	2006-12-13 23:54:42 UTC (rev 1143)
+++ branches/coopcomm/source/main/cStats.h	2006-12-14 14:21:26 UTC (rev 1144)
@@ -259,6 +259,9 @@
   std::set<int> m_cell_sent_leader; //!< Set of all cells that sent a message carrying the leader's ID.
   t_predicate_list m_predicate_list; //!< The list of predicates used to choose which messages to track.
   std::vector<double> m_maxDemeFitnesses; //!< The max fitness of each deme.
+  //! The index of the deme with maximum fitness within m_maxDemeFitness; correlates to 
+  //! the index of the cell range from cPopulation::GetDemes().
+  unsigned int m_maxDemeIndex;
   
   cStats(); // @not_implemented
   cStats(const cStats&); // @not_implemented
@@ -618,6 +621,8 @@
   void PrintMessageData(const cString& filename);
   //! Log the max fitnesses of demes.
   void PrintDemeFitness(const cString& filename);
+  //! Log the constituent genomes of the deme with the highest fitness.
+  void PrintDemeGenomes(const cString& filename);
   //! Log ID-specific data.
   void PrintIDData(const cString& filename);
   //! Print message data file.




More information about the Avida-cvs mailing list