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

dk at myxo.css.msu.edu dk at myxo.css.msu.edu
Sun Sep 28 16:08:44 PDT 2008


Author: dk
Date: 2008-09-28 19:08:43 -0400 (Sun, 28 Sep 2008)
New Revision: 2803

Modified:
   development/source/actions/PopulationActions.cc
   development/source/main/cDeme.h
   development/source/main/cOrganism.cc
   development/source/main/cStats.cc
   development/source/main/cStats.h
Log:
Almost done merging in the rest of the synchronization work.  Decided to
refactor most of the flash-tracking nonsense into cStats, as opposed to having
it all in cDeme, hence the merge is in stages.



Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc	2008-09-28 21:11:05 UTC (rev 2802)
+++ development/source/actions/PopulationActions.cc	2008-09-28 23:08:43 UTC (rev 2803)
@@ -1263,6 +1263,8 @@
   typedef std::map<int, std::set<int> > DataMap;
 	
   cAssignRandomCellData(cWorld* world, const cString& args) : cAction(world, args) { }
+	
+	virtual ~cAssignRandomCellData() { }
   
   static const cString GetDescription() { return "No Arguments"; }
   
@@ -1465,15 +1467,18 @@
  problem, so that consensus on as many values as possible is reached in the shortest
  amount of time.
  */
-class cIteratedConsensus : public cAbstractMonitoringCompeteDemes, ConsensusSupport {
+class cActionIteratedConsensus : public cAbstractMonitoringCompeteDemes, ConsensusSupport {
 public:
-	cIteratedConsensus(cWorld* world, const cString& args) : cAbstractMonitoringCompeteDemes(world, args), _replace(0) {
+	cActionIteratedConsensus(cWorld* world, const cString& args) : cAbstractMonitoringCompeteDemes(world, args), _replace(0) {
 		if(args.GetSize()) {
 			cString largs(args);
 			_replace = largs.PopWord().AsInt();
 		}
 	}
 	
+	//! Destructor.
+	virtual ~cActionIteratedConsensus() { }
+	
 	static const cString GetDescription() { return "Arguments: [int compete_period=100 [int replace_number=0]]"; }
 	
 	//! Calculate the current fitness of this deme.
@@ -1520,6 +1525,112 @@
 };
 
 
+/**** The below are merges-in-progress. ****/
+
+/*! Action to send an artificial flash to each deme in the population at a specified period.
+class cActionPacecarFlash : public cAction {
+public:
+	cActionPacecarFlash(cWorld* world, const cString& args): cAction(world, args), _pacecar(80) {
+		if(args.GetSize()) {
+			cString largs(args);
+			_pacecar = largs.PopWord().AsInt();
+		}
+	}
+	
+	//! Destructor.
+	virtual ~cActionPacecarFlash() { }
+	
+	static const cString GetDescription() { return "Arguments: [pacecar=80]"; }
+	
+ //! Send a flash 
+	virtual void Process(cAvidaContext& ctx) {
+	}
+	
+private:
+	int _pacecar; //!< Period for artificial flashes that will be sent to each deme.
+};
+ */
+
+
+/*! Compete demes based on the ability of their constituent organisms
+ to synchronize their flashes to a common phase and period.
+class cActionSynchronization : public cAbstractMonitoringCompeteDemes {
+public:
+  //! Constructor.
+  cActionSynchronization(cWorld* world, const cString& args) : cAbstractMonitoringCompeteDemes(world, args), _pacecar(0) {
+		if(args.GetSize()) {
+			cString largs(args);
+			_pacecar = largs.PopWord().AsInt();
+		}		
+  }
+  
+  //! Description of this event.
+  static const cString GetDescription() { return "No Arguments"; }
+  
+  //! Run this event on the population.
+  virtual void Process(cAvidaContext& ctx) {
+    std::vector<double> fitness;
+    for(int i=0; i<m_world->GetPopulation().GetNumDemes(); ++i) {
+      fitness.push_back(fitness_function(m_world->GetPopulation().GetDeme(i)));
+    }    
+    m_world->GetPopulation().CompeteDemes(fitness);
+  }
+  
+  //! Calculate the fitness of a deme (must return > 0.0).
+  virtual double fitness_function(const cDeme& deme) {
+    // How many unique organisms have flashed in the last SYNC_FITNESS_WINDOW updates?
+    int total_flashed = deme.GetUniqueFlashes();
+		
+    // If not everyone has flashed, fitness is just the number that have flashed:
+    if(total_flashed < deme.GetSize()) {
+      return 1.0 + total_flashed;
+    }
+    
+    // If everyone has flashed, fitness is the difference between max and average
+    // squared, added to the size of the deme.
+    const cDeme::flash_row& fr = deme.GetFlashRollingSum();
+    double avg = std::accumulate(fr.begin(), fr.end(), 0.0) / fr.size();
+    double max = *std::max_element(fr.begin(), fr.end());
+    
+    return 1.0 + deme.GetSize() + pow(max-avg, 2.0);
+  }
+};
+ */
+
+
+/*! 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).
+class cActionDesynchronization : public cActionCompeteDemesFlashTiming {
+public:
+  //! Constructor.
+  cActionCompeteDemesFlashTimingDesync(cWorld* world, const cString& args) : cActionCompeteDemesFlashTiming(world, args) {
+  }
+  
+  //! Description of this event.
+  static const cString GetDescription() { return "No Arguments"; }
+  
+  //! Calculate the fitness of a deme (must return > 0.0).
+  virtual double fitness_function(const cDeme& deme) {
+    // How many unique organisms have flashed in the last SYNC_FITNESS_WINDOW updates?
+    int total_flashed = deme.GetUniqueFlashes();
+    
+    // If not everyone has flashed, fitness is just the number that have flashed:
+    if(total_flashed < deme.GetSize()) {
+      return total_flashed + 1.0;
+    }
+    
+    // Let's find out the max number of flashes in any update:
+    const cDeme::flash_row& fr = deme.GetFlashRollingSum();
+    double max = *std::max_element(fr.begin(), fr.end());
+    
+    // And reward based on the difference:
+    return 1.0 + deme.GetSize() + pow(deme.GetSize()-max, 2.0);
+  }
+};
+ */
+
+
 class cAbstractCompeteDemes_AttackKillAndEnergyConserve : public cAbstractCompeteDemes {
 
   public:
@@ -2231,7 +2342,7 @@
 	/****AbstractCompeteDemes sub-classes****/
   action_lib->Register<cAbstractCompeteDemes_AttackKillAndEnergyConserve>("CompeteDemes_AttackKillAndEnergyConserve");
   action_lib->Register<cAssignRandomCellData>("AssignRandomCellData");
-  action_lib->Register<cIteratedConsensus>("IteratedConsensus");
+  action_lib->Register<cActionIteratedConsensus>("IteratedConsensus");
 	
   action_lib->Register<cActionNewTrial>("NewTrial");
   action_lib->Register<cActionCompeteOrganisms>("CompeteOrganisms");

Modified: development/source/main/cDeme.h
===================================================================
--- development/source/main/cDeme.h	2008-09-28 21:11:05 UTC (rev 2802)
+++ development/source/main/cDeme.h	2008-09-28 23:08:43 UTC (rev 2803)
@@ -232,7 +232,7 @@
   void SetupDemeRes(int id, cResource * res, int verbosity);
   void UpdateDemeRes() { deme_resource_count.GetResources(); }
   void Update(double time_step) { deme_resource_count.Update(time_step); }
-  int GetRelativeCellID(int absolute_cell_id) { return absolute_cell_id % GetSize(); } //!< assumes all demes are the same size
+  int GetRelativeCellID(int absolute_cell_id) const { return absolute_cell_id % GetSize(); } //!< assumes all demes are the same size
 
   void SetCellEventGradient(int x1, int y1, int x2, int y2, int delay, int duration, bool static_pos, int time_to_live);
   int GetNumEvents();

Modified: development/source/main/cOrganism.cc
===================================================================
--- development/source/main/cOrganism.cc	2008-09-28 21:11:05 UTC (rev 2802)
+++ development/source/main/cOrganism.cc	2008-09-28 23:08:43 UTC (rev 2803)
@@ -762,8 +762,5 @@
   // Flash not lost; continue.
   m_interface->SendFlash();
   m_world->GetStats().SentFlash(*this);
-  if(m_interface->GetDeme() != 0) {
-    //m_interface->GetDeme()->OrganismFlashed(*this);
-  }
   DoOutput(ctx);
 }

Modified: development/source/main/cStats.cc
===================================================================
--- development/source/main/cStats.cc	2008-09-28 21:11:05 UTC (rev 2802)
+++ development/source/main/cStats.cc	2008-09-28 23:08:43 UTC (rev 2803)
@@ -1749,10 +1749,17 @@
 	}	
 }
 
-/*! Called after an organism flashes. */
+/*! Called when an organism issues a flash instruction.
+ 
+ We do some pretty detailed tracking here in order to support the use of flash
+ messages in deme competition.  All flashes are tracked per deme.
+ */
 void cStats::SentFlash(cOrganism& organism) {
-  ++m_flash_count;
-  m_flashed_cells.push_back(organism.GetOrgInterface().GetCellID());
+  ++m_flash_count;	
+	if(organism.GetOrgInterface().GetDeme() != 0) {
+		const cDeme* deme = organism.GetOrgInterface().GetDeme();
+		m_flash_times[GetUpdate()][deme->GetID()].push_back(deme->GetRelativeCellID(organism.GetCellID()));
+	}
 }
 
 
@@ -1767,7 +1774,7 @@
   df.Endl();
   
   m_flash_count = 0;
-  m_flashed_cells.clear();
+	m_flash_times.clear();
 }
 
 
@@ -1776,13 +1783,19 @@
   cDataFile& df = m_world->GetDataFile(filename);
   
   df.WriteComment("Detailed Avida synchronization data");
-  df.WriteComment("Rows are (update, cellid) tuples, representing the update at which that cell flashed.");
+  df.WriteComment("Rows are (update, demeid, cellid) tuples, representing the update at which that cell flashed.");
   df.WriteTimeStamp();
-  for(std::vector<int>::iterator i=m_flashed_cells.begin(); i!=m_flashed_cells.end(); ++i) {
-    df.Write(GetUpdate(), "Update [update]");
-    df.Write(*i, "Cellid [cellid]");
-    df.Endl();
-  }
-  
-  m_flashed_cells.clear();
+	
+	for(PopulationFlashes::iterator i=m_flash_times.begin(); i!=m_flash_times.end(); ++i) {
+		for(DemeFlashes::iterator j=i->second.begin(); j!=i->second.end(); ++j) {
+			for(CellFlashes::iterator k=j->second.begin(); k!=j->second.end(); ++k) {
+				df.Write(i->first, "Update [update]");
+				df.Write(j->first, "Deme ID [demeid]");
+				df.Write(*k, "Deme-relative cell ID that issued a flash at this update [relcellid]");
+				df.Endl();
+			}
+		}
+	}
+	
+	m_flash_times.clear();
 }

Modified: development/source/main/cStats.h
===================================================================
--- development/source/main/cStats.h	2008-09-28 21:11:05 UTC (rev 2802)
+++ development/source/main/cStats.h	2008-09-28 23:08:43 UTC (rev 2803)
@@ -849,15 +849,20 @@
 	
 	// -------- Synchronization support --------
 public:
+	typedef std::vector<int> CellFlashes; //!< Typedef for a list of cell IDs.
+	typedef std::map<int, CellFlashes> DemeFlashes; //!< Typedef for cell IDs (in this deme) -> list of cell IDs.
+	typedef std::map<int, DemeFlashes> PopulationFlashes; //!< Typedef for deme IDs -> flashes in that deme.
   //! Called immediately after an organism has issued a "flash" to its neighbors.
   void SentFlash(cOrganism& organism);
+	//! Retrieve the cell ID -> flash time map.
+	const PopulationFlashes& GetFlashTimes() { return m_flash_times; }
   //! Print statistics about synchronization flashes.
   void PrintSynchronizationData(const cString& filename);
   //! Print detailed information regarding synchronization flashes.
   void PrintDetailedSynchronizationData(const cString& filename);
 protected:
   int m_flash_count; //!< Number of flashes that have occured since last PrintSynchronizationData.
-  std::vector<int> m_flashed_cells; //!< List of cellids that have flashed since last PrintDetailedSynchronizationData.	
+	PopulationFlashes m_flash_times; //!< For tracking flashes that have occurred throughout this population.
 };
 
 




More information about the Avida-cvs mailing list