[Avida-cvs] [avida-svn] r878 - in development/source: actions event

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Mon Aug 21 05:08:06 PDT 2006


Author: brysonda
Date: 2006-08-21 08:08:06 -0400 (Mon, 21 Aug 2006)
New Revision: 878

Modified:
   development/source/actions/PopulationActions.cc
   development/source/actions/PrintActions.cc
   development/source/event/cEventManager.cc
Log:
Transition sever_grid_(col/row) events into actions framework.

Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc	2006-08-19 21:37:35 UTC (rev 877)
+++ development/source/actions/PopulationActions.cc	2006-08-21 12:08:06 UTC (rev 878)
@@ -788,7 +788,201 @@
 };
 
 
+/*
+ Compete all of the demes using a basic genetic algorithm approach. Fitness
+ of each deme is determined differently depending on the competition_type: 
+ 0: deme fitness = 1 (control, random deme selection)
+ 1: deme fitness = number of births since last competition (default) 
+ 2: deme fitness = average organism fitness at the current update
+ 3: deme fitness = average mutation rate at the current update
+*/
+class cActionCompeteDemes : public cAction
+{
+private:
+  int m_type;
+public:
+  cActionCompeteDemes(cWorld* world, const cString& args) : cAction(world, args), m_type(1)
+  {
+    cString largs(args);
+    if (largs.GetSize()) m_type = largs.PopWord().AsInt();
+  }
+  
+  const cString GetDescription() { return "CompeteDemes [int type=1]"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    m_world->GetPopulation().CompeteDemes(m_type);
+  }
+};
 
+
+/*
+ Designed to serve as a control for the compete_demes. Each deme is 
+ copied into itself and the parameters reset. 
+*/
+class cActionResetDemes : public cAction
+{
+public:
+  cActionResetDemes(cWorld* world, const cString& args) : cAction(world, args) { ; }
+  
+  const cString GetDescription() { return "ResetDemes"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    m_world->GetPopulation().ResetDemes();
+  }
+};
+
+
+class cActionCopyDeme : public cAction
+{
+private:
+  int m_id1;
+  int m_id2;
+public:
+  cActionCopyDeme(cWorld* world, const cString& args) : cAction(world, args), m_id1(0), m_id2(1)
+  {
+    cString largs(args);
+    if (largs.GetSize()) m_id1 = largs.PopWord().AsInt();
+    if (largs.GetSize()) m_id2 = largs.PopWord().AsInt();
+  }
+  
+  const cString GetDescription() { return "CopyDeme <int src_id> <int dest_id>"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    m_world->GetPopulation().CopyDeme(m_id1, m_id2);
+  }
+};
+
+
+/*
+ Remove the connections between cells along a column in an avida grid.
+ 
+ Arguments:
+   col_id:  indicates the number of columns to the left of the cut.
+            default (or -1) = cut population in half
+   min_row: First row to start cutting from
+            default = 0
+   max_row: Last row to cut to
+            default (or -1) = last row in population.
+*/
+class cActionSeverGridCol : public cAction
+{
+private:
+  int m_id;
+  int m_min;
+  int m_max;
+public:
+  cActionSeverGridCol(cWorld* world, const cString& args) : cAction(world, args), m_id(-1), m_min(0), m_max(-1)
+  {
+    cString largs(args);
+    if (largs.GetSize()) m_id = largs.PopWord().AsInt();
+    if (largs.GetSize()) m_min = largs.PopWord().AsInt();
+    if (largs.GetSize()) m_max = largs.PopWord().AsInt();
+  }
+  
+  const cString GetDescription() { return "SeverGridCol [int col_id=-1] [int min_row=0] [int max_row=-1]"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    const int world_x = m_world->GetPopulation().GetWorldX();
+    const int world_y = m_world->GetPopulation().GetWorldY();
+    if (m_id == -1) m_id = world_x / 2;
+    if (m_max == -1) m_max = world_y;
+    if (m_id < 0 || m_id >= world_x) {
+      cString err = cStringUtil::Stringf("Column ID %d out of range for SeverGridCol", m_id);
+      m_world->GetDriver().RaiseException(err);
+      return;
+    }
+    // Loop through all of the rows and make the cut on each...
+    for (int row_id = m_min; row_id < m_max; row_id++) {
+      int idA = row_id * world_x + m_id;
+      int idB  = GridNeighbor(idA, world_x, world_y, -1,  0);
+      int idA0 = GridNeighbor(idA, world_x, world_y,  0, -1);
+      int idA1 = GridNeighbor(idA, world_x, world_y,  0,  1);
+      int idB0 = GridNeighbor(idA, world_x, world_y, -1, -1);
+      int idB1 = GridNeighbor(idA, world_x, world_y, -1,  1);
+      cPopulationCell& cellA = m_world->GetPopulation().GetCell(idA);
+      cPopulationCell& cellB = m_world->GetPopulation().GetCell(idB);
+      tList<cPopulationCell>& cellA_list = cellA.ConnectionList();
+      tList<cPopulationCell>& cellB_list = cellB.ConnectionList();
+      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB));
+      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB0));
+      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB1));
+      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA));
+      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA0));
+      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA1));
+    }
+  }
+};
+
+
+///// sever_grid_row /////
+
+/*
+ Remove the connections between cells along a column in an avida grid.
+
+ Arguments:
+   row_id:  indicates the number of rows above the cut.
+            default (or -1) = cut population in half
+   min_col: First row to start cutting from
+            default = 0
+   max_col: Last row to cut to
+            default (or -1) = last row in population.
+*/
+class cActionSeverGridRow : public cAction
+{
+private:
+  int m_id;
+  int m_min;
+  int m_max;
+public:
+  cActionSeverGridRow(cWorld* world, const cString& args) : cAction(world, args), m_id(-1), m_min(0), m_max(-1)
+  {
+    cString largs(args);
+    if (largs.GetSize()) m_id = largs.PopWord().AsInt();
+    if (largs.GetSize()) m_min = largs.PopWord().AsInt();
+    if (largs.GetSize()) m_max = largs.PopWord().AsInt();
+  }
+  
+  const cString GetDescription() { return "SeverGridRow [int row_id=-1] [int min_col=0] [int max_col=-1]"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    const int world_x = m_world->GetPopulation().GetWorldX();
+    const int world_y = m_world->GetPopulation().GetWorldY();
+    if (m_id == -1) m_id = world_x / 2;
+    if (m_max == -1) m_max = world_y;
+    if (m_id < 0 || m_id >= world_x) {
+      cString err = cStringUtil::Stringf("Row ID %d out of range for SeverGridRow", m_id);
+      m_world->GetDriver().RaiseException(err);
+      return;
+    }
+    // Loop through all of the rows and make the cut on each...
+    for (int col_id = m_min; col_id < m_max; col_id++) {
+      int idA = col_id * world_x + m_id;
+      int idB  = GridNeighbor(idA, world_x, world_y,  0, -1);
+      int idA0 = GridNeighbor(idA, world_x, world_y, -1,  0);
+      int idA1 = GridNeighbor(idA, world_x, world_y,  1,  0);
+      int idB0 = GridNeighbor(idA, world_x, world_y, -1, -1);
+      int idB1 = GridNeighbor(idA, world_x, world_y,  1, -1);
+      cPopulationCell& cellA = m_world->GetPopulation().GetCell(idA);
+      cPopulationCell& cellB = m_world->GetPopulation().GetCell(idB);
+      tList<cPopulationCell>& cellA_list = cellA.ConnectionList();
+      tList<cPopulationCell>& cellB_list = cellB.ConnectionList();
+      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB));
+      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB0));
+      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB1));
+      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA));
+      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA0));
+      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA1));
+    }
+  }
+};
+
+
+
 void RegisterPopulationActions(cActionLibrary* action_lib)
 {
   action_lib->Register<cActionInject>("Inject");
@@ -809,6 +1003,12 @@
   action_lib->Register<cActionModMutProb>("ModMutProb");
   action_lib->Register<cActionZeroMuts>("ZeroMuts");
 
+  action_lib->Register<cActionCompeteDemes>("CompeteDemes");
+  action_lib->Register<cActionResetDemes>("ResetDemes");
+  action_lib->Register<cActionCopyDeme>("CopyDeme");
+  
+  action_lib->Register<cActionSeverGridCol>("SeverGridCol");
+  action_lib->Register<cActionSeverGridRow>("SeverGridRow");
 
   // @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.
   action_lib->Register<cActionInject>("inject");
@@ -823,4 +1023,11 @@
   action_lib->Register<cActionSerialTransfer>("serial_transfer");
 
   action_lib->Register<cActionZeroMuts>("zero_muts");
+  
+  action_lib->Register<cActionCompeteDemes>("compete_demes");
+  action_lib->Register<cActionResetDemes>("reset_demes");
+  action_lib->Register<cActionCopyDeme>("copy_deme");
+  
+  action_lib->Register<cActionSeverGridCol>("sever_grid_col");
+  action_lib->Register<cActionSeverGridRow>("sever_grid_row");
 }

Modified: development/source/actions/PrintActions.cc
===================================================================
--- development/source/actions/PrintActions.cc	2006-08-19 21:37:35 UTC (rev 877)
+++ development/source/actions/PrintActions.cc	2006-08-21 12:08:06 UTC (rev 878)
@@ -1365,6 +1365,21 @@
 };
 
 
+class cActionPrintDemeStats : public cAction
+{
+public:
+  cActionPrintDemeStats(cWorld* world, const cString& args) : cAction(world, args) { ; }
+  
+  const cString GetDescription() { return "PrintDemeStats"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    m_world->GetPopulation().PrintDemeStats();
+  }
+};
+
+
+
 void RegisterPrintActions(cActionLibrary* action_lib)
 {
   // Stats Out Files
@@ -1421,6 +1436,8 @@
   action_lib->Register<cActionDumpDonorGrid>("DumpDonorGrid");
   action_lib->Register<cActionDumpReceiverGrid>("DumpReceiverGrid");
 
+  action_lib->Register<cActionPrintDemeStats>("PrintDemeStats");
+
   // @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");
@@ -1471,4 +1488,6 @@
   action_lib->Register<cActionDumpTaskGrid>("dump_task_grid");
   action_lib->Register<cActionDumpDonorGrid>("dump_donor_grid");
   action_lib->Register<cActionDumpReceiverGrid>("dump_receiver_grid");
+
+  action_lib->Register<cActionPrintDemeStats>("print_deme_stats");
 }

Modified: development/source/event/cEventManager.cc
===================================================================
--- development/source/event/cEventManager.cc	2006-08-19 21:37:35 UTC (rev 877)
+++ development/source/event/cEventManager.cc	2006-08-21 12:08:06 UTC (rev 878)
@@ -41,250 +41,6 @@
 using namespace std;
 
 
-
-///// compete_demes /////
-
-/**
-* Compete all of the demes using a basic genetic algorithm approach. Fitness
- * of each deme is determined differently depending on the competition_type: 
- * 0: deme fitness = 1 (control, random deme selection)
- * 1: deme fitness = number of births since last competition (default) 
- * 2: deme fitness = average organism fitness at the current update
- * 3: deme fitness = average mutation rate at the current update
- * Merit can optionally be passed in.
- **/
-
-
-class cEvent_compete_demes : public cEvent {
-private:
-  int competition_type;
-public:
-  const cString GetName() const { return "compete_demes"; }
-  const cString GetDescription() const { return "compete_demes  [int competition_type=1]"; }
-  
-  void Configure(cWorld* world, const cString& in_args)
-  {
-    m_world = world;
-    m_args = in_args;
-    cString args(in_args);
-    if (args == "") competition_type=1; else competition_type=args.PopWord().AsInt();
-  }
-  ///// compete_demes /////
-  void Process(){
-    m_world->GetPopulation().CompeteDemes(competition_type);
-  }
-};
-
-///// reset_demes /////
-
-/**
-* Designed to serve as a control for the compete_demes. Each deme is 
- * copied into itself and the parameters reset. 
- **/
-
-
-class cEvent_reset_demes : public cEvent {
-private:
-public:
-  const cString GetName() const { return "reset_demes"; }
-  const cString GetDescription() const { return "reset_demes"; }
-  
-  void Configure(cWorld* world, const cString& in_args)
-  {
-    m_world = world;
-    m_args = in_args;
-  }
-  ///// reset_demes /////
-  void Process(){
-    m_world->GetPopulation().ResetDemes();
-  }
-};
-
-///// print_deme_stats /////
-
-/**
-* Print stats about individual demes
- **/
-
-
-class cEvent_print_deme_stats : public cEvent {
-private:
-public:
-  const cString GetName() const { return "print_deme_stats"; }
-  const cString GetDescription() const { return "print_deme_stats"; }
-  
-  void Configure(cWorld* world, const cString& in_args)
-  {
-    m_world = world;
-    m_args = in_args;
-  }
-  ///// print_deme_stats /////
-  void Process(){
-    m_world->GetPopulation().PrintDemeStats();
-  }
-};
-
-///// copy_deme /////
-
-/**
-* Takes two numbers as arguments and copies the contents of the first deme
- * listed into the second.
- **/
-
-
-class cEvent_copy_deme : public cEvent {
-private:
-  int deme1_id;
-  int deme2_id;
-public:
-    const cString GetName() const { return "copy_deme"; }
-  const cString GetDescription() const { return "copy_deme  <int deme1_id> <int deme2_id>"; }
-  
-  void Configure(cWorld* world, const cString& in_args)
-  {
-    m_world = world;
-    m_args = in_args;
-    cString args(in_args);
-    deme1_id = args.PopWord().AsInt();
-    deme2_id = args.PopWord().AsInt();
-  }
-  ///// copy_deme /////
-  void Process(){
-    m_world->GetPopulation().CopyDeme(deme1_id, deme2_id);
-  }
-};
-
-
-///// sever_grid_col /////
-
-/**
-* Remove the connections between cells along a column in an avida grid.
- * Arguments:
- *  col_id:  indicates the number of columns to the left of the cut.
- *           default (or -1) = cut population in half
- *  min_row: First row to start cutting from
- *           default = 0
- *  max_row: Last row to cut to
- *           default (or -1) = last row in population.
- **/
-
-
-class cEvent_sever_grid_col : public cEvent {
-private:
-  int col_id;
-  int min_row;
-  int max_row;
-public:
-    const cString GetName() const { return "sever_grid_col"; }
-  const cString GetDescription() const { return "sever_grid_col  [int col_id=-1] [int min_row=0] [int max_row=-1]"; }
-  
-  void Configure(cWorld* world, const cString& in_args)
-  {
-    m_world = world;
-    m_args = in_args;
-    cString args(in_args);
-    if (args == "") col_id=-1; else col_id=args.PopWord().AsInt();
-    if (args == "") min_row=0; else min_row=args.PopWord().AsInt();
-    if (args == "") max_row=-1; else max_row=args.PopWord().AsInt();
-  }
-  ///// sever_grid_col /////
-  void Process(){
-    const int world_x = m_world->GetPopulation().GetWorldX();
-    const int world_y = m_world->GetPopulation().GetWorldY();
-    if (col_id == -1) col_id = world_x / 2;
-    if (max_row == -1) max_row = world_y;
-    if (col_id < 0 || col_id >= world_x) {
-      cerr << "Event Error: Column ID " << col_id
-      << " out of range for sever_grid_col" << endl;
-      return;
-    }
-    // Loop through all of the rows and make the cut on each...
-    for (int row_id = min_row; row_id < max_row; row_id++) {
-      int idA = row_id * world_x + col_id;
-      int idB  = GridNeighbor(idA, world_x, world_y, -1,  0);
-      int idA0 = GridNeighbor(idA, world_x, world_y,  0, -1);
-      int idA1 = GridNeighbor(idA, world_x, world_y,  0,  1);
-      int idB0 = GridNeighbor(idA, world_x, world_y, -1, -1);
-      int idB1 = GridNeighbor(idA, world_x, world_y, -1,  1);
-      cPopulationCell & cellA = m_world->GetPopulation().GetCell(idA);
-      cPopulationCell & cellB = m_world->GetPopulation().GetCell(idB);
-      tList<cPopulationCell> & cellA_list = cellA.ConnectionList();
-      tList<cPopulationCell> & cellB_list = cellB.ConnectionList();
-      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB));
-      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB0));
-      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB1));
-      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA));
-      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA0));
-      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA1));
-    }
-  }
-};
-
-///// sever_grid_row /////
-
-/**
-* Remove the connections between cells along a column in an avida grid.
- * Arguments:
- *  row_id:  indicates the number of rows above the cut.
- *           default (or -1) = cut population in half
- *  min_col: First row to start cutting from
- *           default = 0
- *  max_col: Last row to cut to
- *           default (or -1) = last row in population.
- **/
-
-
-class cEvent_sever_grid_row : public cEvent {
-private:
-  int row_id;
-  int min_col;
-  int max_col;
-public:
-    const cString GetName() const { return "sever_grid_row"; }
-  const cString GetDescription() const { return "sever_grid_row  [int row_id=-1] [int min_col=0] [int max_col=-1]"; }
-  
-  void Configure(cWorld* world, const cString& in_args)
-  {
-    m_world = world;
-    m_args = in_args;
-    cString args(in_args);
-    if (args == "") row_id=-1; else row_id=args.PopWord().AsInt();
-    if (args == "") min_col=0; else min_col=args.PopWord().AsInt();
-    if (args == "") max_col=-1; else max_col=args.PopWord().AsInt();
-  }
-  ///// sever_grid_row /////
-  void Process(){
-    const int world_x = m_world->GetPopulation().GetWorldX();
-    const int world_y = m_world->GetPopulation().GetWorldY();
-    if (row_id == -1) row_id = world_y / 2;
-    if (max_col == -1) max_col = world_x;
-    if (row_id < 0 || row_id >= world_y) {
-      cerr << "Event Error: Row ID " << row_id
-      << " out of range for sever_grid_row" << endl;
-      return;
-    }
-    // Loop through all of the cols and make the cut on each...
-    for (int col_id = min_col; col_id < max_col; col_id++) {
-      int idA = row_id * world_x + col_id;
-      int idB  = GridNeighbor(idA, world_x, world_y,  0, -1);
-      int idA0 = GridNeighbor(idA, world_x, world_y, -1,  0);
-      int idA1 = GridNeighbor(idA, world_x, world_y,  1,  0);
-      int idB0 = GridNeighbor(idA, world_x, world_y, -1, -1);
-      int idB1 = GridNeighbor(idA, world_x, world_y,  1, -1);
-      cPopulationCell & cellA = m_world->GetPopulation().GetCell(idA);
-      cPopulationCell & cellB = m_world->GetPopulation().GetCell(idB);
-      tList<cPopulationCell> & cellA_list = cellA.ConnectionList();
-      tList<cPopulationCell> & cellB_list = cellB.ConnectionList();
-      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB));
-      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB0));
-      cellA_list.Remove(&m_world->GetPopulation().GetCell(idB1));
-      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA));
-      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA0));
-      cellB_list.Remove(&m_world->GetPopulation().GetCell(idA1));
-    }
-  }
-};
-
 ///// join_grid_col /////
 
 /**
@@ -554,13 +310,6 @@
 cEventManager::cEventManager(cWorld* world) : m_world(world)
 {
   // Population events --> Population Actions
-  REGISTER(compete_demes);
-  REGISTER(reset_demes);
-  REGISTER(print_deme_stats);
-  REGISTER(copy_deme);
-  
-  REGISTER(sever_grid_col);
-  REGISTER(sever_grid_row);
   REGISTER(join_grid_col);
   REGISTER(join_grid_row);
   REGISTER(connect_cells);




More information about the Avida-cvs mailing list