[Avida-SVN] r3374 - in development/source: actions main tools

dk at myxo.css.msu.edu dk at myxo.css.msu.edu
Sat Aug 22 07:41:46 PDT 2009


Author: dk
Date: 2009-08-22 10:41:46 -0400 (Sat, 22 Aug 2009)
New Revision: 3374

Modified:
   development/source/actions/PopulationActions.cc
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
   development/source/tools/cRandom.h
Log:
Added MixPopulation event, to support biologically-inspired models for group selection.

Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc	2009-08-21 13:57:13 UTC (rev 3373)
+++ development/source/actions/PopulationActions.cc	2009-08-22 14:41:46 UTC (rev 3374)
@@ -2575,6 +2575,9 @@
  BASE_CONST_MERIT 0 (Use a base merit of zero, hence all merits = 0)
  
  These settings will make sure that all merit will be set by this action.
+ 
+ FYI: This model is appears to be similar to Traulsen's model of group selection:
+ 
  */
 
 class cActionDivideDemes : public cAction
@@ -2596,6 +2599,25 @@
 	};
 
 
+/*! Mix all organisms in the population.
+ 
+ This event, in combination with a method for deme competition, can be used to model
+ the different biologically-inspired approaches to group selection, specifically
+ Wilson's and Traulsen's models.
+*/
+class cActionMixPopulation : public cAction {
+public:
+	cActionMixPopulation(cWorld* world, const cString& args) : cAction(world, args) {
+	}
+	
+	static const cString GetDescription() { return "No arguments."; }
+	
+	void Process(cAvidaContext& ctx) {
+		m_world->GetPopulation().MixPopulation();
+	}
+};
+
+
 /*
  Designed to serve as a control for the compete_demes. Each deme is 
  copied into itself and the parameters reset. 
@@ -3586,6 +3608,7 @@
   action_lib->Register<cActionDivideDemes>("DivideDemes");
   action_lib->Register<cActionResetDemes>("ResetDemes");
   action_lib->Register<cActionCopyDeme>("CopyDeme");
+  action_lib->Register<cActionMixPopulation>("MixPopulation");
 	
 	action_lib->Register<cActionDecayPoints>("DecayPoints");
 	

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2009-08-21 13:57:13 UTC (rev 3373)
+++ development/source/main/cPopulation.cc	2009-08-22 14:41:46 UTC (rev 3374)
@@ -5726,3 +5726,37 @@
 void cPopulation::AdjustHGTResource(double delta) {
 	resource_count.Modify(m_hgt_resid, delta);
 }
+
+/*! Mix all organisms in the population.
+ 
+ This method rearranges the relationship between organisms and cells.  Specifically,
+ we take all organisms in the population, and assign them to different randomly-selected
+ cells.
+ 
+ This isn't really useful in a single population run.  However, a mixing stage is a
+ key component of biologically-inspired approaches to group selection (ie, Wilson's 
+ and Traulsen's models).
+ 
+ \warning THIS METHOD CHANGES THE ORGANISM POINTERS OF CELLS.  IT'S QUITE POSSIBLY
+ DANGEROUS, ESPECIALLY IF STATEFUL.
+ */
+void cPopulation::MixPopulation() {
+	// Get the list of all organism pointers, including nulls:
+	std::vector<cOrganism*> population(cell_array.GetSize());
+	for(int i=0; i<cell_array.GetSize(); ++i) {
+		population[i] = cell_array[i].GetOrganism();
+	}
+	
+	// Shuffle them:
+	cRandomStdAdaptor adapted_rng(m_world->GetRandom());
+	std::random_shuffle(population.begin(), population.end(), adapted_rng);
+	
+	// Reset the organism pointers of all cells:
+	for(int i=0; i<cell_array.GetSize(); ++i) {
+		cell_array[i].RemoveOrganism();
+		// Can't insert null, 'cause InsertOrganism asserts.
+		if(population[i] != 0) {
+			cell_array[i].InsertOrganism(population[i]);
+		}
+	}
+}

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2009-08-21 13:57:13 UTC (rev 3373)
+++ development/source/main/cPopulation.h	2009-08-22 14:41:46 UTC (rev 3374)
@@ -370,7 +370,12 @@
 	int m_hgt_resid; //!< HGT resource ID.
 public:
 	//! Modify current level of the HGT resource.
-	void AdjustHGTResource(double delta);	
+	void AdjustHGTResource(double delta);
+	
+	// -------- Population mixing support --------
+public:
+	//! Mix all organisms in the population.
+	void MixPopulation();
 };
 
 

Modified: development/source/tools/cRandom.h
===================================================================
--- development/source/tools/cRandom.h	2009-08-21 13:57:13 UTC (rev 3373)
+++ development/source/tools/cRandom.h	2009-08-22 14:41:46 UTC (rev 3374)
@@ -24,6 +24,7 @@
 #include <ctime>
 #include <climits>
 #include <cmath>
+#include <iterator>
 
 /**
  * A versatile and fast pseudo random number generator.
@@ -241,6 +242,21 @@
 };
 
 
+/*! This is an adaptor to make cRandom behave like a proper STL random number
+ generator.
+ */
+struct cRandomStdAdaptor {
+	typedef int argument_type;
+	typedef int result_type;
+	
+	cRandomStdAdaptor(cRandom& rng) : _rng(rng) { }
+	int operator()(int n) { return _rng.GetInt(n); }
+	
+	cRandom& _rng;
+};
+
+
+
 #ifdef ENABLE_UNIT_TESTS
 namespace nRandom {
   /**




More information about the Avida-cvs mailing list