[Avida-SVN] r2267 - in development: documentation source/actions source/cpu source/main

barrick at myxo.css.msu.edu barrick at myxo.css.msu.edu
Mon Jan 21 20:37:24 PST 2008


Author: barrick
Date: 2008-01-21 23:37:23 -0500 (Mon, 21 Jan 2008)
New Revision: 2267

Modified:
   development/documentation/actions.html
   development/source/actions/PopulationActions.cc
   development/source/actions/PrintActions.cc
   development/source/cpu/cHardwareCPU.cc
   development/source/main/cPhenotype.cc
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
   development/source/main/cStats.cc
   development/source/main/cStats.h
Log:
Decided on a stable implementation of NewTrial and CompeteOrganisms (for using Avida in GA mode). Added a PrintCompetitionData action which allows the results of each trial period to be recorded. Even documented these actions. (!)



Modified: development/documentation/actions.html
===================================================================
--- development/documentation/actions.html	2008-01-22 04:34:39 UTC (rev 2266)
+++ development/documentation/actions.html	2008-01-22 04:37:23 UTC (rev 2267)
@@ -54,10 +54,11 @@
 <p>
 <table>
   <tr>
-    <td>
+    <td valign="top">
       <a href="#AnalyzeLandscape">AnalyzeLandscape</a>
       <br /><a href="#AnalyzePopulation">AnalyzePopulation</a>
       <br /><a href="#CompeteDemes">CompeteDemes</a>
+      <br /><a href="#CompeteOrganisms">CompeteOrganisms</a>
       <br /><a href="#ConnectCells">ConnectCells</a>
       <br /><a href="#CopyDeme">CopyDeme</a>
       <br /><a href="#DeletionLandscape">DeletionLandscape</a>
@@ -93,8 +94,9 @@
       <br /><a href="#LoadPopulation">LoadPopulation</a>
       <br /><a href="#ModMutProb">ModMutProb</a>
     </td>
-    <td>
-      <a href="#OutflowScaledResource">OutflowScaledResource</a>
+    <td valign="top">
+      <a href="#NewTrial">NewTrial</a>
+      <br /><a href="#OutflowScaledResource">OutflowScaledResource</a>
       <br /><a href="#PairTestLandscape">PairTestLandscape</a>
       <br /><a href="#PrecalcLandscape">PrecalcLandscape</a>
       <br /><a href="#PredictNuLandscape">PredictNuLandscape</a>
@@ -103,6 +105,7 @@
       <br /><a href="#PrintCCladeCounts">PrintCCladeCounts</a>
       <br /><a href="#PrintCCladeFitnessHistogram">PrintCCladeFitnessHistogram</a>
       <br /><a href="#PrintCCladeRelativeFitnessHistogram">PrintCCladeRelativeFitnessHistogram</a>
+      <br /><a href="#PrintCompetitionData">PrintCompetitionData</a>
       <br /><a href="#PrintCountData">PrintCountData</a>
       <br /><a href="#PrintData">PrintData</a>
       <br /><a href="#PrintDebug">PrintDebug</a>
@@ -130,10 +133,10 @@
       <br /><a href="#PrintPhenotypeStatus">PrintPhenotypeStatus</a>
       <br /><a href="#PrintPhenotypicPlasticity">PrintPhenotypicPlasticity</a>
       <br /><a href="#PrintPopulationDistanceData">PrintPopulationDistanceData</a>
-      <br /><a href="#PrintRelativeFitnessHistogram">PrintRelativeFitnessHistogram</a>
     </td>
-    <td>
-      <a href="#PrintResourceData">PrintResourceData</a>
+    <td valign="top">
+      <a href="#PrintRelativeFitnessHistogram">PrintRelativeFitnessHistogram</a>
+      <br /><a href="#PrintResourceData">PrintResourceData</a>
       <br /><a href="#PrintSpeciesAbundanceData">PrintSpeciesAbundanceData</a>
       <br /><a href="#PrintStatsData">PrintStatsData</a>
       <br /><a href="#PrintTasksSnapshot">PrintTasksSnapshot</a>
@@ -246,7 +249,16 @@
   </p>
 </li>
 
+<li>
+  <strong><a name="PrintCompetitionData">PrintCompetitionData</a></strong>
+  [<span class="cmdarg">string filename='competition.dat'</span>]
+  
+  <p>
+  Print out CompeteOrganism statistics. Make the first call after the first set of trials has been completed to get a complete header.
+  </p>
+</li>
 
+
 <li>
   <strong><a name="PrintErrorData">PrintErrorData</a></strong>
   [<span class="cmdarg">string filename='error.dat'</span>]
@@ -979,6 +991,33 @@
   Disconnects a pair of specified cells.
   </p>
 </li>
+<li>
+  <strong><a name="NewTrial">NewTrial</a></strong>
+  <p>
+  Immediately calculates the fitness of each organism in the population, saves this value for use with the CompeteOrganisms
+  event, and resets the state of all organisms.
+  </p>
+</li>
+<li>
+  <strong><a name="CompeteOrganisms">CompeteOrganisms</a></strong>
+  &lt;<span class="cmdarg">int competition_type=0</span>&gt; &lt;<span class="cmdarg">int parents_survive=0</span>&gt; 
+  </span>&gt; &lt;<span class="cmdarg">int dynamic_scaling=0</span>&gt;
+  <p>Calculates fitness of each organism in the population and creates a new population of descendants
+  where each organism has a chance of replication proportional to its fitness. Can be used to make Avida 
+  operate without population dynamics and asynchronous replication, i.e. like a genetic algorithm. If NewTrial 
+  events have occurred since the last CompeteOrganisms event, then the average fitness over those trials 
+  will be used.
+  </p>
+ 
+  <code>competition_type</code> controls how the fitnesses of multiple trials determine the overall fitness
+  used for the competition: 0=geometric mean of fitnesses, 1=scaled geometric mean of fitnesses (the greatest fitness
+  of each trial is scaled to 1.0 before taking the geometric mean), 2=arithmetic mean, 3=geometric mean plus 
+  rescales effective fitness values by the geometric mean of the difference from the top score and the median.
+ <p>Setting <code>parents_survive</code> to 1, causes the first copy of an organism that makes it into the new 
+  population to be the original (unmutated) parent organism.
+
+  </p>
+</li>
 </ul>
 
 

Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc	2008-01-22 04:34:39 UTC (rev 2266)
+++ development/source/actions/PopulationActions.cc	2008-01-22 04:37:23 UTC (rev 2267)
@@ -1128,23 +1128,19 @@
 private:
   int m_type;
   int m_parents_survive;
-  double m_scaled_time;
-  int m_dynamic_scaling;
 public:
-  cActionCompeteOrganisms(cWorld* world, const cString& args) : cAction(world, args), m_type(0), m_parents_survive(0), m_scaled_time(1.0), m_dynamic_scaling(0)
+  cActionCompeteOrganisms(cWorld* world, const cString& args) : cAction(world, args), m_type(0), m_parents_survive(0)
   {
     cString largs(args);
     if (largs.GetSize()) m_type = largs.PopWord().AsInt();
     if (largs.GetSize()) m_parents_survive = largs.PopWord().AsInt();
-    if (largs.GetSize()) m_scaled_time = largs.PopWord().AsDouble();
-    if (largs.GetSize()) m_dynamic_scaling = largs.PopWord().AsInt();
   }
   
   static const cString GetDescription() { return "Arguments: [int type=0] [int parents_survive=0] [double scaled_time=1.0] [int dynamic_scaling=0]"; }
   
   void Process(cAvidaContext& ctx)
   {
-    m_world->GetPopulation().CompeteOrganisms(m_type, m_parents_survive, m_scaled_time, m_dynamic_scaling);
+    m_world->GetPopulation().CompeteOrganisms(m_type, m_parents_survive);
   }
 };
 

Modified: development/source/actions/PrintActions.cc
===================================================================
--- development/source/actions/PrintActions.cc	2008-01-22 04:34:39 UTC (rev 2266)
+++ development/source/actions/PrintActions.cc	2008-01-22 04:37:23 UTC (rev 2267)
@@ -90,6 +90,7 @@
 STATS_OUT_FILE(PrintSenseData,              sense.dat           );
 STATS_OUT_FILE(PrintSenseExeData,           sense_exe.dat       );
 STATS_OUT_FILE(PrintSleepData,              sleep.dat          );
+STATS_OUT_FILE(PrintCompetitionData,        competition.dat         );
 
 
 #define POP_OUT_FILE(METHOD, DEFAULT)                                                     /*  1 */ \
@@ -2625,6 +2626,7 @@
   action_lib->Register<cActionPrintSenseData>("PrintSenseData");
   action_lib->Register<cActionPrintSenseExeData>("PrintSenseExeData");
   action_lib->Register<cActionPrintSleepData>("PrintSleepData");
+  action_lib->Register<cActionPrintCompetitionData>("PrintCompetitionData");
 
   // Population Out Files
   action_lib->Register<cActionPrintPhenotypeData>("PrintPhenotypeData");

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2008-01-22 04:34:39 UTC (rev 2266)
+++ development/source/cpu/cHardwareCPU.cc	2008-01-22 04:37:23 UTC (rev 2267)
@@ -739,14 +739,13 @@
       
   if (m_world->GetConfig().PROMOTERS_ENABLED.Get())
   {
-    fp << "Promoters: index=" << m_promoter_index << " offset=" << m_promoter_offset;
+    fp << "  Promoters: index=" << m_promoter_index << " offset=" << m_promoter_offset;
     fp << " exe_inst=" << m_threads[m_cur_thread].GetPromoterInstExecuted();
     for (int i=0; i<m_promoters.GetSize(); i++)
     {
-      fp << setfill(' ') << setbase(10) << m_promoters[i].m_pos << ":";
+      fp << setfill(' ') << setbase(10) << " " << m_promoters[i].m_pos << ":";
       fp << "Ox" << setbase(16) << setfill('0') << setw(8) << (m_promoters[i].GetRegulatedBitCode()) << " "; 
     }
-    fp << endl;    
     fp << setfill(' ') << setbase(10) << endl;
   }    
   fp.flush();

Modified: development/source/main/cPhenotype.cc
===================================================================
--- development/source/main/cPhenotype.cc	2008-01-22 04:34:39 UTC (rev 2266)
+++ development/source/main/cPhenotype.cc	2008-01-22 04:37:23 UTC (rev 2267)
@@ -1252,7 +1252,7 @@
 {
   fp << "  MeritBase:"
      << CalcSizeMerit()
-     << " Bonus: " << cur_bonus
+     << " Bonus:" << cur_bonus
      << " Errors:" << cur_num_errors
      << " Donates:" << cur_num_donates;
   fp << endl;

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2008-01-22 04:34:39 UTC (rev 2266)
+++ development/source/main/cPopulation.cc	2008-01-22 04:37:23 UTC (rev 2267)
@@ -3002,7 +3002,7 @@
   tArray<cOrganism*> child_array;
   tArray<cMerit> merit_array;
   birth_chamber.SubmitOffspring(ctx, orig_org.ChildGenome(), orig_org, child_array, merit_array);
-    //@JEB may want to force asex for an injected child, sex will mess up CompeteOrganisms
+    //@JEB for now we force asex for an injected child, sex will probably mess up CompeteOrganisms...
   assert(child_array.GetSize() == 1);
   cOrganism * new_organism = child_array[0];
   orig_org.ChildGenome() = save_child;
@@ -3225,7 +3225,7 @@
                       equals a time of 1 unit
 */
 
-void cPopulation::CompeteOrganisms(int competition_type, int parents_survive, double scaled_time, int dynamic_scaling)
+void cPopulation::CompeteOrganisms(int competition_type, int parents_survive)
 {
   cout << "==Compete Organisms==" << endl;
   double total_fitness = 0;
@@ -3233,15 +3233,19 @@
   tArray<double> org_fitness(num_cells); 
 
   double lowest_fitness = -1.0;
+  double average_fitness = 0;
   double highest_fitness = -1.0;
   double lowest_fitness_copied = -1.0;
+  double average_fitness_copied = 0;
   double highest_fitness_copied = -1.0;
   int different_orgs_copied = 0;
   int num_competed_orgs = 0;
 
   int num_trials = 0;
+  int dynamic_scaling = (competition_type==3);
   
-  // How many trials were there?
+  // How many trials were there? -- same for every organism
+  // we just need to find one...
   for (int i = 0; i < num_cells; i++) 
   {
     if (GetCell(i).IsOccupied())
@@ -3253,30 +3257,37 @@
   }
   tArray<double> min_trial_fitnesses(num_trials);
   tArray<double> max_trial_fitnesses(num_trials);
-  tArray<double> bonus_sums(num_trials);
-  bonus_sums.SetAll(0);
-  
+  tArray<double> avg_trial_fitnesses(num_trials);
+  avg_trial_fitnesses.SetAll(0);
+
   bool init = false;
   // What is the min and max fitness in each trial
   for (int i = 0; i < num_cells; i++) 
   {
     if (GetCell(i).IsOccupied())
     {
+      num_competed_orgs++;
       cPhenotype& p = GetCell(i).GetOrganism()->GetPhenotype();
       tArray<double> trial_fitnesses = p.GetTrialFitnesses();
-      for (int t=0; t < trial_fitnesses.GetSize(); t++) 
+      for (int t=0; t < num_trials; t++) 
       { 
         if ((!init) || (min_trial_fitnesses[t] > trial_fitnesses[t])) min_trial_fitnesses[t] = trial_fitnesses[t];
         if ((!init) || (max_trial_fitnesses[t] < trial_fitnesses[t])) max_trial_fitnesses[t] = trial_fitnesses[t];
+        avg_trial_fitnesses[t] += trial_fitnesses[t];
       }
       init = true;
     }
-  }  
+  } 
   
+  //divide averages for each trial
+  for (int t=0; t < num_trials; t++) 
+  {
+    avg_trial_fitnesses[t] /= num_competed_orgs;
+  }
   
   for (int t=0; t < min_trial_fitnesses.GetSize(); t++) 
   {
-    cout << "Trial #" << t << " Min Fitness = " << min_trial_fitnesses[t] << " Max Fitness = " << max_trial_fitnesses[t] << endl;
+    cout << "Trial #" << t << " Min Fitness = " << min_trial_fitnesses[t] << ", Avg fitness = " << avg_trial_fitnesses[t] << " Max Fitness = " << max_trial_fitnesses[t] << endl;
     //cout << "Bonus sum = " << bonus_sums[t] << endl;
   }
   
@@ -3285,12 +3296,10 @@
   {
     if (GetCell(i).IsOccupied())
     {
-      num_competed_orgs++;
       double fitness = 0.0;
       cPhenotype& p = GetCell(i).GetOrganism()->GetPhenotype();
       //Don't need to reset trial_fitnesses because we will call cPhenotype::OffspringReset on the entire pop
       tArray<double> trial_fitnesses = p.GetTrialFitnesses();
-      tArray<int> trial_times_used = p.GetTrialTimesUsed();
 
       //If there are no trial fitnesses...use the actual fitness.
       if (trial_fitnesses.GetSize() == 0)
@@ -3300,54 +3309,42 @@
       }
       switch (competition_type)
       {
-        //Arithmetic Mean
+        //Geometric Mean        
         case 0:
-        fitness = 0;
-        for (int t=0; t < trial_fitnesses.GetSize(); t++) 
-        { 
-          fitness+=trial_fitnesses[t]; 
-        }
-        fitness /= trial_fitnesses.GetSize();
-        break;
-      
-        //Product        
-        case 1:
+        case 3:
         fitness = 1.0;
         for (int t=0; t < trial_fitnesses.GetSize(); t++) 
         { 
           fitness*=trial_fitnesses[t]; 
         }
+        fitness = exp( (1.0/((double)trial_fitnesses.GetSize())) * log(fitness) );
         break;
-      
-        //Geometric Mean        
-        case 2:
-        fitness = 1;
+        
+        //Geometric Mean of normalized values
+        case 1:
+        fitness = 1.0;        
         for (int t=0; t < trial_fitnesses.GetSize(); t++) 
         { 
-          fitness*=trial_fitnesses[t]; 
+           fitness*=trial_fitnesses[t] / max_trial_fitnesses[t]; 
         }
-        fitness = exp( (1/trial_fitnesses.GetSize()) * log(fitness) );
+        fitness = exp( (1.0/((double)trial_fitnesses.GetSize())) * log(fitness) );
         break;
         
-        //Addition of normalized values
-        case 3:
+        //Arithmetic Mean
+        case 2:
         fitness = 0;
         for (int t=0; t < trial_fitnesses.GetSize(); t++) 
         { 
-          if (max_trial_fitnesses[t] > 0) fitness+=trial_fitnesses[t] / max_trial_fitnesses[t]; 
+          fitness+=trial_fitnesses[t]; 
         }
+        fitness /= (double)trial_fitnesses.GetSize();
         break;
-                         
+      
         default:
         cout << "Unknown CompeteOrganisms method!" << endl;
         exit(1);
       }
       if (m_world->GetVerbosity() >= VERBOSE_DETAILS) cout << "Trial fitness in cell " << i << " = " << fitness << endl;
-      //-->Note: Setting fitness here will not print out the value of the last trial's fitness because the makeup of the population is going to change anyway.
-      //It will be printed out correctly only if NewTrial and PrintAverages are called on the same update, before CompeteDemes.
-      //p.SetCurBonus( fitness *  (p.GetTimeUsed() - p.GetGestationStart()) / p.GetCurMeritBase() ); //Indirectly set fitness over all trials...??
-     // p.SetCurBonus( trial_fitnesses[trial_fitnesses.GetSize()-1] *  (p.GetTimeUsed() - p.GetGestationStart()) / p.CalcSizeMerit() ); //Or to last trial
-
       org_fitness[i] = fitness;
       total_fitness += fitness;
       
@@ -3355,7 +3352,8 @@
       if ((lowest_fitness == -1.0) || (fitness < lowest_fitness)) lowest_fitness = fitness;
     } // end if occupied
   }
-  
+  average_fitness = total_fitness / num_competed_orgs;
+ 
   //Rescale by the geometric mean of the difference from the top score and the median
   if ( dynamic_scaling )
   {
@@ -3373,22 +3371,9 @@
           }
       }
     }
-    if (num_org_not_max > 0) scaled_time *= exp ( -(1.0/num_org_not_max) * dynamic_factor );
   }
   
-  cout << "Competition time " << scaled_time << " units" << endl;
-  total_fitness = 0;
-  for (int i = 0; i < num_cells; i++) 
-  {
-    if (GetCell(i).IsOccupied())
-    {
-        double fitness = exp(log(2.0) * scaled_time *  (org_fitness[i] - highest_fitness));
-        org_fitness[i] = fitness;
-        total_fitness += fitness;
-    }
-  }
-  
-   // Pick which orgs should be in the next generation. (Filling all cells)
+  // Pick which orgs should be in the next generation. (Filling all cells)
   tArray<int> new_orgs(num_cells);
   for (int i = 0; i < num_cells; i++) {
     double birth_choice = (double) m_world->GetRandom().GetDouble(total_fitness);
@@ -3400,10 +3385,13 @@
         if (m_world->GetVerbosity() >= VERBOSE_DETAILS) cout << "Propagating from cell " << test_org << " to " << i << endl;
         if ((highest_fitness_copied == -1.0) || (org_fitness[test_org] > highest_fitness_copied)) highest_fitness_copied = org_fitness[test_org];
         if ((lowest_fitness_copied == -1.0) || (org_fitness[test_org] < lowest_fitness_copied)) lowest_fitness_copied = org_fitness[test_org];
+        average_fitness_copied += org_fitness[test_org];
         break;
       }
     }
   }
+  // average assumes we fill all cells.
+  average_fitness_copied /= num_cells;
   
   // Track how many of each org we should have.
   tArray<int> org_count(num_cells);
@@ -3416,6 +3404,8 @@
   // -- but not the full reset if we are using trials, the trial reset should already cover things like task counts, etc.
   // calling that twice would erase this information before it could potentially be output between NewTrial and CompeteOrganisms events.
   for (int i = 0; i < num_cells; i++) {
+    if (m_world->GetVerbosity() >= VERBOSE_DETAILS) cout << "Cell " << i << " has " << org_count[i] << " copies in the next generation" << endl;  
+
     if (org_count[i] > 0) {
       different_orgs_copied++;
       cPhenotype& p = GetCell(i).GetOrganism()->GetPhenotype();
@@ -3435,27 +3425,32 @@
   is_init.SetAll(false);
   
   // Copy orgs until all org counts are 1.
+  int last_from_cell_id = 0;
+  int last_to_cell_id = 0;
   while (true) {
     // Find the next org to copy...
     int from_cell_id, to_cell_id;
-    for (from_cell_id = 0; from_cell_id < num_cells; from_cell_id++) {
+    for (from_cell_id = last_from_cell_id; from_cell_id < num_cells; from_cell_id++) {
       if (org_count[from_cell_id] > 1) break;
     }
+    last_from_cell_id = from_cell_id;
     
-    // Stop If we didn't find another org to copy
+    // Stop if we didn't find another org to copy
     if (from_cell_id == num_cells) break;
     
-    for (to_cell_id = 0; to_cell_id < num_cells; to_cell_id++) {
+    for (to_cell_id = last_to_cell_id; to_cell_id < num_cells; to_cell_id++) {
       if (org_count[to_cell_id] == 0) break;
     }
+    last_to_cell_id = to_cell_id;
     
-    // We now have both a from and a to org....
+    // We now have both a "from" and a "to" org....
     org_count[from_cell_id]--;
     org_count[to_cell_id]++;
     
     cOrganism * organism = GetCell(from_cell_id).GetOrganism();
     organism->ChildGenome() = organism->GetGenome();
-    InjectChild( to_cell_id, *organism );    
+    if (m_world->GetVerbosity() >= VERBOSE_DETAILS) cout << "Injecting Child " << from_cell_id << " to " << to_cell_id << endl;  
+    InjectChild( to_cell_id, *organism );  
     
     is_init[to_cell_id] = true;
   }
@@ -3464,15 +3459,23 @@
   {
     // Now create children from remaining cells into themselves
     for (int cell_id = 0; cell_id < num_cells; cell_id++) {
-      if (is_init[cell_id] == true) continue;
-      cOrganism * organism = GetCell(cell_id).GetOrganism();
-      organism->ChildGenome() = organism->GetGenome();
-      InjectChild( cell_id, *organism ); 
+      if (!is_init[cell_id])
+      {
+        cOrganism * organism = GetCell(cell_id).GetOrganism();
+        organism->ChildGenome() = organism->GetGenome();
+        if (m_world->GetVerbosity() >= VERBOSE_DETAILS) cout << "Re-injecting Self " << cell_id << " to " << cell_id << endl;  
+        InjectChild( cell_id, *organism ); 
+      }
     }
   }
   
-  cout << "Competed: Min fitness = " << lowest_fitness << ", Max fitness = " << highest_fitness << endl;
-  cout << "Copied  : Min fitness = " << lowest_fitness_copied << ", Max fitness = " << highest_fitness_copied << " (scaled to Max = 1.0)" << endl;
+  cout << "Competed: Min fitness = " << lowest_fitness << ", Avg fitness = " << average_fitness << " Max fitness = " << highest_fitness << endl;
+  cout << "Copied  : Min fitness = " << lowest_fitness_copied << ", Avg fitness = " << average_fitness_copied << ", Max fitness = " << highest_fitness_copied << endl;
   cout << "Copied  : Different organisms = " << different_orgs_copied << endl;
 
+  // copy stats to cStats, so that these can be remembered and printed
+  m_world->GetStats().SetCompetitionTrialFitnesses(avg_trial_fitnesses);
+  m_world->GetStats().SetCompetitionFitness(average_fitness);
+  m_world->GetStats().SetCompetitionOrgsReplicated(different_orgs_copied);
+  
 }

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2008-01-22 04:34:39 UTC (rev 2266)
+++ development/source/main/cPopulation.h	2008-01-22 04:37:23 UTC (rev 2267)
@@ -266,7 +266,7 @@
 
   // Trials and genetic algorithm @JEB
   void NewTrial();
-  void CompeteOrganisms(int competition_type, int parents_survive, double scaled_time, int dynamic_scaling);
+  void CompeteOrganisms(int competition_type, int parents_survive);
 };
 
 

Modified: development/source/main/cStats.cc
===================================================================
--- development/source/main/cStats.cc	2008-01-22 04:34:39 UTC (rev 2266)
+++ development/source/main/cStats.cc	2008-01-22 04:37:23 UTC (rev 2267)
@@ -116,6 +116,8 @@
   , num_used(0)
   , num_own_used(0)
   , sense_size(0)
+  , avg_competition_fitness(0)
+  , num_orgs_replicated(0)
 {
   const cEnvironment& env = m_world->GetEnvironment();
   const int num_tasks = env.GetNumTasks();
@@ -209,7 +211,6 @@
   }
   // End sense tracking initialization
 
-  
   genotype_map.Resize( m_world->GetConfig().WORLD_X.Get() * m_world->GetConfig().WORLD_Y.Get() );
 
   numAsleep.Resize(m_world->GetConfig().NUM_DEMES.Get());
@@ -1029,7 +1030,23 @@
   df.Endl();
 }
 
+void cStats::PrintCompetitionData(const cString& filename){
+  cDataFile& df = m_world->GetDataFile(filename);
 
+  df.WriteComment( "Competition results\n" );
+  df.WriteComment( "results of the current competitions" );
+  
+  df.Write( GetUpdate(), "update" );
+  df.Write( avg_competition_fitness, "overall competition fitness" );
+  df.Write( num_orgs_replicated, "number of organisms copied" );
+
+  for( int i=0; i < avg_trial_fitnesses.GetSize(); i++ ){
+    df.Write(avg_trial_fitnesses[i], cStringUtil::Stringf("trial.%d fitness", i));
+  }
+  df.Endl();
+}
+
+
 /*! This method is called whenever an organism successfully sends a message.  Success,
 in this case, means that the message has been delivered to the receive buffer of
 the organism that this message was sent to. */

Modified: development/source/main/cStats.h
===================================================================
--- development/source/main/cStats.h	2008-01-22 04:34:39 UTC (rev 2266)
+++ development/source/main/cStats.h	2008-01-22 04:37:23 UTC (rev 2267)
@@ -236,7 +236,6 @@
   int num_resamplings;
   int num_failedResamplings;
   
-  
   // State variables
   int last_update;
 
@@ -252,6 +251,11 @@
   tArray<int> sense_last_exe_count;
   tArray<cString> sense_names;
 
+  // Stats for competitions
+  tArray<double> avg_trial_fitnesses;
+  double avg_competition_fitness;
+  int num_orgs_replicated;
+
   tArray<int> numAsleep;
 
   cStats(); // @not_implemented
@@ -480,6 +484,10 @@
   void SetReactionName(int id, const cString & name) { reaction_names[id] = name; }
   void SetResourceName(int id, const cString & name) { resource_names[id] = name; }
 
+  void SetCompetitionTrialFitnesses(tArray<double> _in) { avg_trial_fitnesses = _in; }
+  void SetCompetitionFitness(double _in) { avg_competition_fitness = _in; }
+  void SetCompetitionOrgsReplicated(int _in) { num_orgs_replicated = _in; }
+
   //market info
   void AddMarketItemBought() { num_bought++;}
   void AddMarketItemSold() { num_sold++; }
@@ -615,7 +623,8 @@
   void PrintSenseData(const cString& filename);
   void PrintSenseExeData(const cString& filename);
   void PrintSleepData(const cString& filename);
-  
+  void PrintCompetitionData(const cString& filename);
+
   // -------- Messaging support --------
 public:
   //! Type for a list of pointers to message predicates.




More information about the Avida-cvs mailing list