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

connel42 at myxo.css.msu.edu connel42 at myxo.css.msu.edu
Mon Nov 17 11:50:32 PST 2008


Author: connel42
Date: 2008-11-17 14:50:32 -0500 (Mon, 17 Nov 2008)
New Revision: 2948

Modified:
   development/source/actions/PopulationActions.cc
   development/source/actions/PrintActions.cc
   development/source/cpu/cHardwareCPU.cc
   development/source/main/cPhenotype.cc
   development/source/main/cPhenotype.h
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
Log:
* Added some stats for energy sharing\n* Added print action to print these stats\n* Added compete demes implementation for # of times echo task has been completed by living organisms

Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc	2008-11-17 14:31:32 UTC (rev 2947)
+++ development/source/actions/PopulationActions.cc	2008-11-17 19:50:32 UTC (rev 2948)
@@ -36,6 +36,7 @@
 #include "cStats.h"
 #include "cWorld.h"
 #include "cOrganism.h"
+#include "cEnvironment.h"
 
 #include <map>
 #include <set>
@@ -1630,6 +1631,49 @@
 };
 
 
+/*! Compete demes based on the number of times they've completed the echo task.
+    Fitness is 2^#echos
+ */
+class cActionCompeteDemesEcho : public cAbstractCompeteDemes {
+public:
+  //! Constructor.
+  cActionCompeteDemesEcho(cWorld* world, const cString& args) : cAbstractCompeteDemes(world, args) { }
+  
+	//! Destructor.
+	virtual ~cActionCompeteDemesEcho() { }
+	
+  //! Description of this event.
+  static const cString GetDescription() { return "No Arguments"; }
+	
+  virtual double Fitness(const cDeme& deme) {
+    int num_echos = 0;
+    const int num_task = m_world->GetEnvironment().GetNumTasks();
+
+    for(int i=0; i < deme.GetSize(); i++) {
+      int cur_cell = deme.GetCellID(i);
+      
+      // Since we only count echos from living organisms, this also creates a pressure
+      // for all of the organisms to stay alive
+      if (m_world->GetPopulation().GetCell(cur_cell).IsOccupied() == false) continue;
+      
+      cPhenotype & phenotype = m_world->GetPopulation().GetCell(cur_cell).GetOrganism()->GetPhenotype();
+      
+      for (int j = 0; j < num_task; j++) {        
+        if( (strcasecmp(m_world->GetEnvironment().GetTask(j).GetName(), "echo") == 0) &&
+           (phenotype.GetLastTaskCount()[j] > 0) ) {
+          cout << "BDCDEBUG: got a match!!!!" <<endl;
+          num_echos++;
+        }
+      }
+      
+    }
+    
+    return (double) (2^num_echos);
+  } 
+  
+}; //End cActionCompeteDemesEcho
+
+
 /*! Compete demes based on the ability of their constituent organisms
  to synchronize their flashes to a common period, and yet distribute themselves
  throughout phase-space (phase desynchronization).
@@ -2393,6 +2437,7 @@
   action_lib->Register<cActionIteratedConsensus>("IteratedConsensus");
 	action_lib->Register<cActionSynchronization>("Synchronization");
 	action_lib->Register<cActionDesynchronization>("Desynchronization");
+  action_lib->Register<cActionCompeteDemesEcho>("CompeteDemesEcho");
 	
   action_lib->Register<cActionNewTrial>("NewTrial");
   action_lib->Register<cActionCompeteOrganisms>("CompeteOrganisms");

Modified: development/source/actions/PrintActions.cc
===================================================================
--- development/source/actions/PrintActions.cc	2008-11-17 14:31:32 UTC (rev 2947)
+++ development/source/actions/PrintActions.cc	2008-11-17 19:50:32 UTC (rev 2948)
@@ -2535,6 +2535,19 @@
   }
 };
 
+class cActionPrintDemeEnergySharingStats : public cAction
+  {
+  public:
+    cActionPrintDemeEnergySharingStats(cWorld* world, const cString& args) : cAction(world, args) { ; }
+    
+    static const cString GetDescription() { return "No Arguments"; }
+    
+    void Process(cAvidaContext& ctx)
+    {
+      m_world->GetPopulation().PrintDemeEnergySharingStats();
+    }
+  };
+
 class cActionPrintDemeDonorStats : public cAction
 {
 public:
@@ -2708,6 +2721,7 @@
   // deme output files
   action_lib->Register<cActionPrintDemeAllStats>("PrintDemeAllStats");
   action_lib->Register<cActionPrintDemeAllStats>("PrintDemeStats"); //duplicate of previous
+  action_lib->Register<cActionPrintDemeEnergySharingStats>("PrintDemeEnergySharingStats");
   action_lib->Register<cActionPrintDemeDonorStats>("PrintDemeDonorStats");
   action_lib->Register<cActionPrintDemeSpacialEnergy>("PrintDemeSpacialEnergyStats");
   action_lib->Register<cActionPrintDemeSpacialSleep>("PrintDemeSpacialSleepStats");

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2008-11-17 14:31:32 UTC (rev 2947)
+++ development/source/cpu/cHardwareCPU.cc	2008-11-17 19:50:32 UTC (rev 2948)
@@ -4106,14 +4106,14 @@
   cOrganism* receiver = msg->GetSender();
 
   // If the requestor no longer exists, should the donor still lose energy???
-  if( (receiver == NULL) && (receiver->IsDead()) ) {
+  if( (receiver == NULL) || (receiver->IsDead()) ) {
     return false;
   }
   
-  //Note: could get fancier about fraction of energy to send
   DoEnergyDonatePercent(receiver, m_world->GetConfig().ENERGY_SHARING_PCT.Get());
   organism->GetPhenotype().IncDonates();
   GetOrganism()->GetOrgInterface().GetDeme()->IncEnergyDonationsMade();
+  GetOrganism()->GetPhenotype().SetIsEnergyDonor();
   
   return true;
   
@@ -4134,6 +4134,7 @@
   
   organism->BroadcastMessage(ctx, msg);
   GetOrganism()->GetOrgInterface().GetDeme()->IncEnergyRequestsMade();
+  GetOrganism()->GetPhenotype().SetIsEnergyRequestor();
   
   return true;
   

Modified: development/source/main/cPhenotype.cc
===================================================================
--- development/source/main/cPhenotype.cc	2008-11-17 14:31:32 UTC (rev 2947)
+++ development/source/main/cPhenotype.cc	2008-11-17 19:50:32 UTC (rev 2948)
@@ -161,6 +161,7 @@
   last_child_germline_propensity = in_phen.last_child_germline_propensity;
   total_energy_donated     = in_phen.total_energy_donated;
   total_energy_received    = in_phen.total_energy_received;
+  total_energy_applied     = in_phen.total_energy_applied;
 
   // 4. Records from this organisms life...
   num_divides              = in_phen.num_divides;      
@@ -222,11 +223,13 @@
   parent_sex              = in_phen.parent_sex;      
   parent_cross_num        = in_phen.parent_cross_num; 
   
+  is_energy_requestor     = in_phen.is_energy_requestor;
   is_energy_donor         = in_phen.is_energy_donor;
   is_energy_receiver      = in_phen.is_energy_receiver;
   has_used_donated_energy = in_phen.has_used_donated_energy;
   total_energy_donated    = in_phen.total_energy_donated;
   total_energy_received   = in_phen.total_energy_received;
+  total_energy_applied    = in_phen.total_energy_applied;
 
   // 6. Child information...
   copy_true               = in_phen.copy_true;       
@@ -417,11 +420,13 @@
   to_die = false;
   to_delete = false;
 
+  is_energy_requestor = false;
   is_energy_donor = false;
   is_energy_receiver = false;
   has_used_donated_energy = false;
   total_energy_donated = 0.0;
-  total_energy_received = 0.0;  
+  total_energy_received = 0.0; 
+  total_energy_applied = 0.0;
 
   // Setup child info...
   copy_true          = false;
@@ -570,11 +575,13 @@
   to_die = false;
   to_delete = false;
   
+  is_energy_requestor = false;
   is_energy_donor = false;
   is_energy_receiver = false;
   has_used_donated_energy = false;
   total_energy_donated = 0.0;
   total_energy_received = 0.0;
+  total_energy_applied = 0.0;
 
   // Setup child info...
   copy_true         = false;
@@ -1028,6 +1035,7 @@
   parent_cross_num    = clone_phenotype.cross_num;
   to_die = false;
   to_delete = false;
+  is_energy_requestor = false;
   is_energy_donor = false;
   is_energy_receiver = false;
   has_used_donated_energy = false;
@@ -1542,6 +1550,7 @@
   is_receiver_threshgb = false;
   is_receiver_quanta_threshgb_last = is_receiver_quanta_threshgb;
   is_receiver_quanta_threshgb = false;
+  is_energy_requestor = false;
   is_energy_donor = false;
   is_energy_receiver = false;
   (void) is_modifier;

Modified: development/source/main/cPhenotype.h
===================================================================
--- development/source/main/cPhenotype.h	2008-11-17 14:31:32 UTC (rev 2947)
+++ development/source/main/cPhenotype.h	2008-11-17 19:50:32 UTC (rev 2948)
@@ -192,6 +192,7 @@
   bool is_donor_threshgb_last;// Did this org's parent threshgbg_donate? 
   bool is_donor_quanta_threshgb;  // Has this organism quanta_threshgb_donated (true green beard)? 
   bool is_donor_quanta_threshgb_last;// Did this org's parent quanta_threshgbg_donate? 
+  bool is_energy_requestor; // Has this organism requested energy?
   bool is_energy_donor; // Has this organism donated energy?
   bool is_energy_receiver;  // Has this organism received an energy donation?
   bool has_used_donated_energy; // Has the organism actively used an energy donation?
@@ -389,6 +390,7 @@
   bool IsDonorThreshGbLast() const { assert(initialized == true); return is_donor_threshgb_last; }
   bool IsDonorQuantaThreshGb() const { assert(initialized == true); return is_donor_quanta_threshgb; }
   bool IsDonorQuantaThreshGbLast() const { assert(initialized == true); return is_donor_quanta_threshgb_last; }
+  bool IsEnergyRequestor() const { assert(initialized == true); return is_energy_requestor; }
   bool IsEnergyDonor() const { assert(initialized == true); return is_energy_donor; }
   bool IsEnergyReceiver() const { assert(initialized == true); return is_energy_receiver; }
   bool HasUsedEnergyDonation() const { assert(initialized == true); return has_used_donated_energy; }
@@ -447,6 +449,9 @@
   void IncreaseEnergyDonated(double amount) { assert(amount >=0); total_energy_donated += amount; }
   void IncreaseEnergyReceived(double amount) { assert(amount >=0); total_energy_received += amount; }
   void IncreaseEnergyApplied(double amount) { assert(amount >=0); total_energy_applied += amount; }
+  double GetAmountEnergyDonated() { return total_energy_donated; }
+  double GetAmountEnergyReceived() { return total_energy_received; }
+  double GetAmountEnergyApplied() { return total_energy_applied; }
   
   void SetCurRBinsAvail(const tArray<double>& in_avail) { cur_rbins_avail = in_avail; }
   void SetCurRbinsTotal(const tArray<double>& in_total) { cur_rbins_total = in_total; }
@@ -472,6 +477,7 @@
   void SetIsReceiverTrueGb() { SetIsReceiver(); is_receiver_truegb = true; } 
   void SetIsReceiverThreshGb() { SetIsReceiver(); is_receiver_threshgb = true; } 
   void SetIsReceiverQuantaThreshGb() { SetIsReceiver(); is_receiver_quanta_threshgb = true; } 
+  void SetIsEnergyRequestor() { is_energy_requestor = true; }
   void SetIsEnergyDonor() { is_energy_donor = true; }
   void SetIsEnergyReceiver() { is_energy_receiver = true; }
   void SetHasUsedDonatedEnergy() {has_used_donated_energy = true; }

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2008-11-17 14:31:32 UTC (rev 2947)
+++ development/source/main/cPopulation.cc	2008-11-17 19:50:32 UTC (rev 2948)
@@ -2559,6 +2559,50 @@
   stats.SumEnergyTestamentToNeighborOrganisms().Clear();
 }
 
+
+// Print some stats about the energy sharing behavior of each deme
+void cPopulation::PrintDemeEnergySharingStats() {
+  //TODO: BDC: move this from covering the population to per deme or deme average
+  const int num_demes = deme_array.GetSize();
+  cStats& stats = m_world->GetStats();
+  cDataFile & df_donor = m_world->GetDataFile("deme_energy_sharing.dat");
+  df_donor.WriteComment("Average energy donation statistics for each deme in population");
+  df_donor.WriteTimeStamp();
+  df_donor.Write(stats.GetUpdate(), "update");
+  
+  int num_requestors = 0;
+  int num_donors = 0;
+  int num_receivers = 0;
+  double amount_donated = 0.0;
+  double amount_received = 0.0;
+  double amount_applied = 0.0;  
+  
+  for (int deme_id = 0; deme_id < num_demes; deme_id++) {
+    const cDeme & cur_deme = deme_array[deme_id];
+    
+    for (int i = 0; i < cur_deme.GetSize(); i++) {
+      int cur_cell = cur_deme.GetCellID(i);
+      if (cell_array[cur_cell].IsOccupied() == false) continue;
+      cPhenotype & phenotype = GetCell(cur_cell).GetOrganism()->GetPhenotype();
+      if(phenotype.IsEnergyRequestor()) num_requestors++;
+      if(phenotype.IsEnergyDonor()) num_donors++;
+      if(phenotype.IsEnergyReceiver()) num_receivers++;
+      amount_donated += phenotype.GetAmountEnergyDonated();
+      amount_received += phenotype.GetAmountEnergyReceived();
+      amount_applied += phenotype.GetAmountEnergyApplied();
+    }
+  }
+  df_donor.Write(num_requestors/num_demes, "Average number of organisms that have requested energy");
+  df_donor.Write(num_donors/num_demes, "Average umber of organisms that have donated energy");
+  df_donor.Write(num_receivers/num_demes, "Average number of organisms that have received energy");
+  df_donor.Write(amount_donated/num_demes, "Average total amount of energy donated");
+  df_donor.Write(amount_received/num_demes, "Average total amount of energy received");
+  df_donor.Write(amount_applied/num_demes, "Average total amount of donated energy applied");
+  df_donor.Endl();  
+  
+}
+
+
 void cPopulation::PrintDemeDonor() {
   cStats& stats = m_world->GetStats();
   const int num_demes = deme_array.GetSize();

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2008-11-17 14:31:32 UTC (rev 2947)
+++ development/source/main/cPopulation.h	2008-11-17 19:50:32 UTC (rev 2948)
@@ -229,6 +229,7 @@
   // Deme-related stats methods
   void PrintDemeAllStats();
   void PrintDemeTestamentStats(const cString& filename);
+  void PrintDemeEnergySharingStats();
   void PrintDemeDonor();
   void PrintDemeFitness();
   void PrintDemeGestationTime();




More information about the Avida-cvs mailing list