[Avida-cvs] [avida-svn] r984 - in branches/coopcomm/source: actions main

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Mon Sep 25 11:53:59 PDT 2006


Author: dknoester
Date: 2006-09-25 14:53:59 -0400 (Mon, 25 Sep 2006)
New Revision: 984

Modified:
   branches/coopcomm/source/actions/PopulationActions.cc
   branches/coopcomm/source/actions/PrintActions.cc
   branches/coopcomm/source/main/cPopulation.h
   branches/coopcomm/source/main/cPopulationCell.cc
   branches/coopcomm/source/main/cPopulationCell.h
   branches/coopcomm/source/main/cStats.cc
Log:
Added an event to reset random cell ids (all, max id, or #).  Also, removed last vestiges of random(), and fixed some stat tracking.



Modified: branches/coopcomm/source/actions/PopulationActions.cc
===================================================================
--- branches/coopcomm/source/actions/PopulationActions.cc	2006-09-25 17:39:00 UTC (rev 983)
+++ branches/coopcomm/source/actions/PopulationActions.cc	2006-09-25 18:53:59 UTC (rev 984)
@@ -6,6 +6,7 @@
  *  Copyright 2006 Michigan State University. All rights reserved.
  *
  */
+#include <set>
 
 #include "PopulationActions.h"
 
@@ -1185,6 +1186,7 @@
   }
 };
 
+
 /**
   Written by: Ben Beckmann
  Action to set an organism as a base station.  Multiple base stations are not allowed yet
@@ -1212,6 +1214,91 @@
   }
 };
 
+
+/*! \brief Defines an action "ResetRandomCellIDs" that changes the random ID of a cell.
+
+This action takes as an argument either the number of cells to reset, or a keyword.
+The following keywords are understood:
+all == Change the random ID of all cells.
+max == Change the random ID of the cell with the maximum ID only.
+
+If a number is given, the cells to have their ID changed will be selected at random.
+
+The argument format is: (\d+)|all|max
+
+Note that the numeric form of this action guarantees that n different cells will 
+have their ID changed.
+*/
+class cActionResetRandomCellIDs : public cAction
+{
+public:
+  enum t_resetType {ALL, MAX, PARTIAL};
+
+  //! Constructor.
+  cActionResetRandomCellIDs(cWorld* world, const cString& args) : cAction(world, args)
+  {
+    cString largs(args);
+    if(largs.GetSize()) {
+      cString type = largs.PopWord();
+      if(type == "all") {
+        m_resetType = ALL;
+      } else if(type == "max") {
+        m_resetType = MAX;
+      } else {
+        m_resetType = PARTIAL;
+        m_count = type.AsInt();
+      }        
+    }
+  }
+  
+  //! Return a string description of the arguments for this action.
+  static const cString GetDescription() { return "Arguments: <# of cells to reset | all | max>"; }
+
+  //! Perform this action.
+  void Process(cAvidaContext& ctx)
+  {
+    switch(m_resetType) {
+      case ALL: {
+        // loop through the list of all cells.
+        cPopulation::t_CellArray& cells = m_world->GetPopulation().GetCellArray();
+        for(int i=0; i<cells.GetSize(); ++i) {
+          cells[i].ResetRandomID();
+        }
+        break;
+      }
+      case MAX: {
+        // just reset the cell with the largest id.
+        m_world->GetPopulation().GetCell(cPopulationCell::GetMaxRandomCellID()).ResetRandomID();
+        break;
+      }
+      case PARTIAL: {
+        // reset a random set of cells.
+        cPopulation::t_CellArray& cells = m_world->GetPopulation().GetCellArray();
+        if(m_count >= (unsigned int)cells.GetSize()) {
+          m_resetType = ALL;
+          return Process(ctx);
+        }
+        std::set<unsigned int> reset;
+        while(reset.size() < m_count) {
+          unsigned int i = m_world->GetRandom().GetUInt(cells.GetSize());
+          cells[i].ResetRandomID();
+          reset.insert(i);
+        }
+        break;
+      }
+      default:
+        assert(false); //shouldn't ever get here.
+        break;
+    }        
+  }
+  
+private:
+  t_resetType m_resetType;
+  unsigned int m_count;
+};
+
+
+
 void RegisterPopulationActions(cActionLibrary* action_lib)
 {
   action_lib->Register<cActionInject>("Inject");
@@ -1244,9 +1331,10 @@
   action_lib->Register<cActionConnectCells>("ConnectCells");
   action_lib->Register<cActionDisconnectCells>("DisconnectCells");
 
+  // Coop-comm actions.
+  action_lib->Register<cActionResetRandomCellIDs>("ResetRandomCellIDs");
   action_lib->Register<cActionSetStaticBaseStation>("SetStaticBaseStation");
 
-
   // @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.
   action_lib->Register<cActionInject>("inject");
   action_lib->Register<cActionInjectRandom>("inject_random");

Modified: branches/coopcomm/source/actions/PrintActions.cc
===================================================================
--- branches/coopcomm/source/actions/PrintActions.cc	2006-09-25 17:39:00 UTC (rev 983)
+++ branches/coopcomm/source/actions/PrintActions.cc	2006-09-25 18:53:59 UTC (rev 984)
@@ -1521,8 +1521,12 @@
   // Print Settings
   action_lib->Register<cActionSetVerbose>("SetVerbose");
   
+  // Coop-comm actions.
+  action_lib->Register<cActionPrintLeaderData>("PrintLeaderData");
+  action_lib->Register<cActionPrintCellData>("PrintCellData");
+  action_lib->Register<cActionPrintMessageData>("PrintMessageData"); 
+  action_lib->Register<cActionPrintIDData>("PrintIDData");  
 
-
   // @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.
   action_lib->Register<cActionPrintAverageData>("print_average_data");
   action_lib->Register<cActionPrintErrorData>("print_error_data");
@@ -1577,10 +1581,4 @@
   action_lib->Register<cActionDumpReceiverGrid>("dump_receiver_grid");
 
   action_lib->Register<cActionSetVerbose>("VERBOSE");
-
-  // Coop-comm actions.
-  action_lib->Register<cActionPrintLeaderData>("PrintLeaderData");
-  action_lib->Register<cActionPrintCellData>("PrintCellData");
-  action_lib->Register<cActionPrintMessageData>("PrintMessageData"); 
-  action_lib->Register<cActionPrintIDData>("PrintIDData");  
 }

Modified: branches/coopcomm/source/main/cPopulation.h
===================================================================
--- branches/coopcomm/source/main/cPopulation.h	2006-09-25 17:39:00 UTC (rev 983)
+++ branches/coopcomm/source/main/cPopulation.h	2006-09-25 18:53:59 UTC (rev 984)
@@ -57,11 +57,14 @@
 
 class cPopulation
 {
+public:
+  typedef tArray<cPopulationCell> t_CellArray;
+  
 private:
   // Components...
   cWorld* m_world;
   cSchedule* schedule;                // Handles allocation of CPU cycles
-  tArray<cPopulationCell> cell_array;  // Local cells composing the population
+  t_CellArray cell_array;  // Local cells composing the population
   cResourceCount resource_count;       // Global resources available
   cBirthChamber birth_chamber;         // Global birth chamber.
   tArray<tList<cSaleItem> > market;   // list of lists of items for sale, each list goes with 1 label
@@ -162,12 +165,13 @@
   bool DumpMemorySummary(std::ofstream& fp);
 
   bool OK();
-
   int GetSize() { return cell_array.GetSize(); }
   int GetWorldX() { return world_x; }
   int GetWorldY() { return world_y; }
 
+  t_CellArray& GetCellArray() { return cell_array; }
   cPopulationCell& GetCell(int in_num);
+  
   const tArray<double>& GetResources() const { return resource_count.GetResources(); }
   const tArray<double>& GetCellResources(int cell_id) const { return resource_count.GetCellResources(cell_id); }
   cBirthChamber& GetBirthChamber(int id) { (void) id; return birth_chamber; }

Modified: branches/coopcomm/source/main/cPopulationCell.cc
===================================================================
--- branches/coopcomm/source/main/cPopulationCell.cc	2006-09-25 17:39:00 UTC (rev 983)
+++ branches/coopcomm/source/main/cPopulationCell.cc	2006-09-25 18:53:59 UTC (rev 984)
@@ -7,13 +7,12 @@
  *  Copyright 1993-2003 California Institute of Technology.
  *
  */
-#include <cstdlib>
-
 #include "cPopulationCell.h"
 
 #include "nHardware.h"
 #include "cOrganism.h"
 #include "cTools.h"
+#include "cWorld.h"
 
 using namespace std;
 
@@ -71,7 +70,7 @@
   cell_id = in_id;
   m_x = x;
   m_y = y;
-  m_rand_id = (int)random();
+  m_rand_id = m_world->GetRandom().GetUInt(UINT_MAX);
   s_rand_ids.insert(std::make_pair(m_rand_id, this));
   
   if (mutation_rates == NULL)
@@ -193,3 +192,15 @@
   
 	assert(false);
 }
+
+
+/*! There's a little bit of housekeeping that we need to do here in order to 
+reset a cell ID.  Remember to remove the current rand id->cell mapping before
+adding the new one.
+*/
+void cPopulationCell::ResetRandomID()
+{
+  s_rand_ids.erase(m_rand_id);
+  m_rand_id = m_world->GetRandom().GetUInt(UINT_MAX);
+  s_rand_ids.insert(std::make_pair(m_rand_id, this));
+}

Modified: branches/coopcomm/source/main/cPopulationCell.h
===================================================================
--- branches/coopcomm/source/main/cPopulationCell.h	2006-09-25 17:39:00 UTC (rev 983)
+++ branches/coopcomm/source/main/cPopulationCell.h	2006-09-25 18:53:59 UTC (rev 984)
@@ -89,6 +89,8 @@
   static int IsRandomCellID(int id) { return s_rand_ids.find(id)!=s_rand_ids.end(); }
   //! Returns the current maximum random cell ID.
   static int GetMaxRandomCellID();
+  //! Reset this cell's random ID.
+  void ResetRandomID();  
 };
 
 

Modified: branches/coopcomm/source/main/cStats.cc
===================================================================
--- branches/coopcomm/source/main/cStats.cc	2006-09-25 17:39:00 UTC (rev 983)
+++ branches/coopcomm/source/main/cStats.cc	2006-09-25 18:53:59 UTC (rev 984)
@@ -891,7 +891,7 @@
 /*! This captures all information related to an organism's death.
 
 This is different than RecordDeath() (above), in that we have a handle to the
-organism that is about to die.  This let us record things like the max message
+organism that is about to die.  This lets us record things like the max message
 sent by the organism, correlate that to the cell, etc.
 
 \todo mean max-valued message per cell.




More information about the Avida-cvs mailing list