[Avida-SVN] r2107 - in branches/energy/source: actions main

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Tue Sep 25 10:51:40 PDT 2007


Author: beckma24
Date: 2007-09-25 13:51:39 -0400 (Tue, 25 Sep 2007)
New Revision: 2107

Modified:
   branches/energy/source/actions/PrintActions.cc
   branches/energy/source/main/cDeme.cc
   branches/energy/source/main/cDeme.h
   branches/energy/source/main/cDemeCellEvent.cc
   branches/energy/source/main/cOrgMessagePredicate.h
   branches/energy/source/main/cStats.cc
   branches/energy/source/main/cStats.h
   branches/energy/source/main/cTaskContext.h
   branches/energy/source/main/cTaskLib.cc
Log:
Added print action which dumps the fraction of cell events detected within all demes

Modified: branches/energy/source/actions/PrintActions.cc
===================================================================
--- branches/energy/source/actions/PrintActions.cc	2007-09-25 13:35:53 UTC (rev 2106)
+++ branches/energy/source/actions/PrintActions.cc	2007-09-25 17:51:39 UTC (rev 2107)
@@ -93,8 +93,8 @@
 STATS_OUT_FILE(PrintSenseExeData,           sense_exe.dat       );
 STATS_OUT_FILE(PrintSleepData,              sleep.dat           );
 STATS_OUT_FILE(PrintMessageData,            message.dat         );
+STATS_OUT_FILE(PrintPredSatFracDump,        pred_sat_frac_dump.dat);
 
-
 #define POP_OUT_FILE(METHOD, DEFAULT)                                                     /*  1 */ \
 class cAction ## METHOD : public cAction {                                                /*  2 */ \
 private:                                                                                  /*  3 */ \
@@ -2697,6 +2697,9 @@
   action_lib->Register<cActionDumpCellDataGrid>("DumpCellDataGrid");
   action_lib->Register<cActionDumpSleepGrid>("DumpSleepGrid");
   
+  // Relative or cumulative deme grid dump
+  action_lib->Register<cActionPrintPredSatFracDump>("PrintPredSatFracDump");
+  
   // Print Settings
   action_lib->Register<cActionSetVerbose>("SetVerbose");
   

Modified: branches/energy/source/main/cDeme.cc
===================================================================
--- branches/energy/source/main/cDeme.cc	2007-09-25 13:35:53 UTC (rev 2106)
+++ branches/energy/source/main/cDeme.cc	2007-09-25 17:51:39 UTC (rev 2107)
@@ -38,8 +38,6 @@
   // If width is negative, set it to the full number of cells.
   width = in_width;
   if (width < 1) width = cell_ids.GetSize();
-  
-  // drain spacial energy resources and place energy in cells
 }
 
 
@@ -63,6 +61,14 @@
   return std::make_pair(cellid % GetWidth(), cellid / GetWidth());
 }
 
+std::pair<int, int> cDeme::GetRelativeCellPosition(int cellid) const 
+{
+  assert(cell_ids.GetSize()>0);
+  assert(GetWidth() > 0);
+  return std::make_pair(cellid % GetWidth(), ( cellid % cell_ids.GetSize() ) / GetWidth());
+}
+
+
 void cDeme::ProcessUpdate() {
   for(int i = 0; i < cell_events.GetSize(); i++) {
     cDemeCellEvent& event = cell_events[i];
@@ -72,6 +78,11 @@
       while(eventCell != -1) {
         // place event ID in cells' data
         m_world->GetPopulation().GetCell(GetCellID(eventCell)).SetCellData(event.GetEventID());
+
+        // record activation of each cell in stats
+        std::pair<int, int> pos = GetRelativeCellPosition(eventCell);
+        m_world->GetStats().IncEventCount(pos.first, pos.second);
+
         eventCell = event.GetNextEventCellID();
       }
     } else if(event.GetDelay()+event.GetDuration() == _age) {
@@ -183,8 +194,10 @@
 
 bool cDeme::MsgPredSatisfiedPreviously() {
   for(int i = 0; i < message_pred_list.GetSize(); i++) {
-    if(message_pred_list[i]->PreviouslySatisfied())
+    if(message_pred_list[i]->PreviouslySatisfied()) {
+      message_pred_list[i]->UpdateStats(m_world->GetStats());
       return true;
+    }
   }
   return false;
 }

Modified: branches/energy/source/main/cDeme.h
===================================================================
--- branches/energy/source/main/cDeme.h	2007-09-25 13:35:53 UTC (rev 2106)
+++ branches/energy/source/main/cDeme.h	2007-09-25 17:51:39 UTC (rev 2107)
@@ -69,6 +69,8 @@
   int GetCellID(int x, int y) const;
   //! Returns an (x,y) pair for the position of the passed-in cell ID.
   std::pair<int, int> GetCellPosition(int cellid) const;
+  //! Returns a relative (x,y) pair within the deme for the position of the passed-in cell ID.
+  std::pair<int, int> cDeme::GetRelativeCellPosition(int cellid) const;
 
   int GetWidth() const { return width; }
   int GetHeight() const { return cell_ids.GetSize() / width; }

Modified: branches/energy/source/main/cDemeCellEvent.cc
===================================================================
--- branches/energy/source/main/cDemeCellEvent.cc	2007-09-25 13:35:53 UTC (rev 2106)
+++ branches/energy/source/main/cDemeCellEvent.cc	2007-09-25 17:51:39 UTC (rev 2107)
@@ -42,7 +42,6 @@
 
 void cDemeCellEvent::ActivateEvent(cWorld* m_world) {
   m_eventID = m_world->GetRandom().GetInt(0x7FFFFFFF);
-  
   if(!m_static_pos) {
     //non-static event position
     int rand_x1 = m_world->GetRandom().GetInt(m_deme_width-m_event_width);

Modified: branches/energy/source/main/cOrgMessagePredicate.h
===================================================================
--- branches/energy/source/main/cOrgMessagePredicate.h	2007-09-25 13:35:53 UTC (rev 2106)
+++ branches/energy/source/main/cOrgMessagePredicate.h	2007-09-25 17:51:39 UTC (rev 2107)
@@ -31,6 +31,7 @@
 #include "cDemeCellEvent.h"
 #include "cOrgMessage.h"
 #include "cOrganism.h"
+#include "cStats.h"
 
 
 /*! \brief An STL-compatible predicate on cOrgMessages.  The intent here is to
@@ -45,6 +46,7 @@
   virtual void Reset() { }
   virtual bool PreviouslySatisfied() = 0;
   virtual cString GetName() = 0;
+  virtual void UpdateStats(cStats& stats) {}
 };
 
 
@@ -191,6 +193,16 @@
     return "EventReceived";
   }
 
+  virtual void UpdateStats(cStats& stats) {
+    if(m_event_received) {
+      int eventCell = m_event->GetNextEventCellID();
+      while(eventCell != -1) {
+        stats.IncPredSat(eventCell);
+        eventCell = m_event->GetNextEventCellID();
+      }
+    }
+  }
+  
   int m_base_station;
   bool m_event_received;
   cDemeCellEvent* m_event;

Modified: branches/energy/source/main/cStats.cc
===================================================================
--- branches/energy/source/main/cStats.cc	2007-09-25 13:35:53 UTC (rev 2106)
+++ branches/energy/source/main/cStats.cc	2007-09-25 17:51:39 UTC (rev 2107)
@@ -30,6 +30,7 @@
 #include "cHardwareManager.h"
 #include "cInstSet.h"
 #include "cPopulation.h"
+#include "cPopulationCell.h"
 #include "cStringUtil.h"
 #include "cWorld.h"
 #include "cWorldDriver.h"
@@ -214,6 +215,11 @@
 
   numAsleep.Resize(m_world->GetConfig().NUM_DEMES.Get());
   numAsleep.SetAll(0);
+  
+  relative_pos_event_count.ResizeClear(m_world->GetConfig().WORLD_X.Get(), m_world->GetConfig().WORLD_Y.Get() / m_world->GetConfig().NUM_DEMES.Get());
+  relative_pos_pred_sat.ResizeClear(m_world->GetConfig().WORLD_X.Get(), m_world->GetConfig().WORLD_Y.Get() / m_world->GetConfig().NUM_DEMES.Get());
+  relative_pos_event_count.SetAll(0);
+  relative_pos_pred_sat.SetAll(0);
 
   SetupPrintDatabase();
 }
@@ -440,6 +446,19 @@
   }
 }
 
+// deme predicate stats
+void cStats::IncEventCount(int x, int y) {
+  relative_pos_event_count.ElementAt(x,y)++;
+}
+
+void cStats::IncPredSat(int cell_id) {
+  cPopulation& pop = m_world->GetPopulation();
+  int deme_id = pop.GetCell(cell_id).GetDemeID();
+  std::pair<int, int> pos = pop.GetDeme(deme_id).GetRelativeCellPosition(cell_id);
+  relative_pos_pred_sat.ElementAt(pos.first, pos.second)++;
+}
+
+
 void cStats::ProcessUpdate()
 {
   // Increment the "avida_time"
@@ -1125,3 +1144,33 @@
   }
   df.Endl();  
 }
+
+void cStats::PrintPredSatFracDump(const cString& filename) {
+  cDataFile& df = m_world->GetDataFile(filename);
+  df.WriteComment( "Displays the fraction of events detected in cell since last print.\n" );
+  df.FlushComments();
+  cString UpdateStr = cStringUtil::Stringf( "%07i", GetUpdate() ) + " = [ ...";
+  df.WriteRaw(UpdateStr);
+
+  int rows = relative_pos_pred_sat.GetNumRows();
+  int cols = relative_pos_pred_sat.GetNumCols();
+  for (int x = 0; x < cols; x++) {
+    for (int y = 0; y < rows; y++) {
+      double data;
+      if(relative_pos_event_count.ElementAt(x,y) == 0) {
+        data = 0.0;
+      } else {
+        data = relative_pos_pred_sat.ElementAt(x,y) / relative_pos_event_count.ElementAt(x,y);
+      }
+//        df.Write(data, "hello");
+      df.WriteBlockElement(data, x*cols+y, cols);
+    }
+  }
+  df.WriteRaw("];");
+  df.Endl();
+  
+  relative_pos_pred_sat.SetAll(0);
+  relative_pos_event_count.SetAll(0);
+}
+
+

Modified: branches/energy/source/main/cStats.h
===================================================================
--- branches/energy/source/main/cStats.h	2007-09-25 13:35:53 UTC (rev 2106)
+++ branches/energy/source/main/cStats.h	2007-09-25 17:51:39 UTC (rev 2107)
@@ -52,6 +52,9 @@
 #ifndef tDataManager_h
 #include "tDataManager.h"
 #endif
+#ifndef tMatrix_h
+#include "tMatrix.h"
+#endif
 #ifndef nGeometry_h
 #include "nGeometry.h"
 #endif
@@ -258,6 +261,10 @@
   cIntSum sum_deme_age;
   cIntSum sum_deme_birth_count;
   cIntSum sum_deme_org_count;
+  
+  // deme predicate stats
+  tMatrix<int> relative_pos_event_count;
+  tMatrix<int> relative_pos_pred_sat;
 
   // message stats
   int num_msg_sent_no_one;
@@ -619,10 +626,15 @@
   cIntSum inbox_size;
 */
 
+  // sleep
   int getNumAsleep(int demeID) { return numAsleep[demeID]; }
   void incNumAsleep(int demeID) { numAsleep[demeID]++; }
   void decNumAsleep(int demeID) { numAsleep[demeID]--; }
 
+  // deme predicate stats
+  void IncEventCount(int x, int y);
+  void IncPredSat(int cell_id);
+
   // this value gets recorded when a creature with the particular
   // fitness value gets born. It will never change to a smaller value,
   // i.e., when the maximum fitness in the population drops, this value will
@@ -660,6 +672,7 @@
   void PrintSenseData(const cString& filename);
   void PrintSenseExeData(const cString& filename);
   void PrintSleepData(const cString& filename);
+  void PrintPredSatFracDump(const cString& filename);
   
   // -------- Messaging support --------
 public:

Modified: branches/energy/source/main/cTaskContext.h
===================================================================
--- branches/energy/source/main/cTaskContext.h	2007-09-25 13:35:53 UTC (rev 2106)
+++ branches/energy/source/main/cTaskContext.h	2007-09-25 17:51:39 UTC (rev 2107)
@@ -118,6 +118,8 @@
     return ret;
   }
   inline void AddTaskState(cTaskState* value) { m_task_states->Add(m_task_entry, value); }
+  
+  inline cOrgInterface* GetOrgInterface() { return m_interface; }
 };
 
 

Modified: branches/energy/source/main/cTaskLib.cc
===================================================================
--- branches/energy/source/main/cTaskLib.cc	2007-09-25 13:35:53 UTC (rev 2106)
+++ branches/energy/source/main/cTaskLib.cc	2007-09-25 17:51:39 UTC (rev 2107)
@@ -2737,3 +2737,20 @@
   if (ctx.NetIsValid()) return 1.0;
   return 0.0;
 }
+/*
+here
+double cTaskLib::Task_SentEventID(cTaskContext& ctx) const {
+  cOrganism* org = ctx.GetOrganism();
+  message_list_type sent_msg = org->GetSentMessages();
+  
+  cDeme deme = ctx.GetOrgInterface()->GetDeme();
+  for(int i = 0; i < deme.NumEvents(); i++) {
+    for(int j = 0; j < sent_msg.size(); j++) {
+      if(deme.GetEvent(i).GetEventID() == sent_msg[j]) {
+        return 1.0;
+      }
+    }
+  }
+  return 0.0;
+}
+*/




More information about the Avida-cvs mailing list