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

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Thu Jun 18 14:09:54 PDT 2009


Author: beckma24
Date: 2009-06-18 17:09:54 -0400 (Thu, 18 Jun 2009)
New Revision: 3324

Modified:
   development/source/actions/PopulationActions.cc
   development/source/actions/PrintActions.cc
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/main/cDeme.cc
   development/source/main/cDeme.h
   development/source/main/cGenomeUtil.cc
   development/source/main/cOrganism.h
   development/source/main/cStats.cc
   development/source/main/cStats.h
Log:
Updated minimum distance between instructions instances in genome code.  Add print action PrintOpinionsSetPerDeme and instructions get-opinionOnly, clear-opinion, and if-opinion-set.

Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/actions/PopulationActions.cc	2009-06-18 21:09:54 UTC (rev 3324)
@@ -962,7 +962,7 @@
 
 				double killprob;
 				if(m_exponent == -1.0)
-					killprob = min(1.0/(m_exprWeight+ pow(M_E, -count)), 100.0)/100.0;  //sigmoid
+					killprob = min(1.0/(m_exprWeight+ exp(-count)), 100.0)/100.0;  //sigmoid
 				else
 					killprob = min(pow(m_exprWeight*count,m_exponent), 100.0)/100.0;  // linear and exponential
 				

Modified: development/source/actions/PrintActions.cc
===================================================================
--- development/source/actions/PrintActions.cc	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/actions/PrintActions.cc	2009-06-18 21:09:54 UTC (rev 3324)
@@ -132,6 +132,7 @@
 STATS_OUT_FILE(PrintPredicatedMessages,     messages.dat        );
 STATS_OUT_FILE(PrintCellData,               cell_data.dat       );
 STATS_OUT_FILE(PrintCurrentOpinions,        opinions.dat        );
+STATS_OUT_FILE(PrintOpinionsSetPerDeme,     opinions_set.dat    );
 STATS_OUT_FILE(PrintPerDemeGenPerFounderData,   deme_gen_between_founders.dat );
 STATS_OUT_FILE(PrintSynchronizationData,    sync.dat            );
 STATS_OUT_FILE(PrintDetailedSynchronizationData, sync-detail.dat);
@@ -3018,6 +3019,7 @@
 	action_lib->Register<cActionPrintPredicatedMessages>("PrintPredicatedMessages");
 	action_lib->Register<cActionPrintCellData>("PrintCellData");
 	action_lib->Register<cActionPrintCurrentOpinions>("PrintCurrentOpinions");
+	action_lib->Register<cActionPrintOpinionsSetPerDeme>("PrintOpinionsSetPerDeme");
 	action_lib->Register<cActionPrintSynchronizationData>("PrintSynchronizationData");
   action_lib->Register<cActionPrintDetailedSynchronizationData>("PrintDetailedSynchronizationData");
 	

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/cpu/cHardwareCPU.cc	2009-06-18 21:09:54 UTC (rev 3324)
@@ -525,6 +525,9 @@
     // These are STALLs because opinions are only relevant with respect to time.
     tInstLibEntry<tMethod>("set-opinion", &cHardwareCPU::Inst_SetOpinion, nInstFlag::STALL),
     tInstLibEntry<tMethod>("get-opinion", &cHardwareCPU::Inst_GetOpinion, nInstFlag::STALL),
+		tInstLibEntry<tMethod>("get-opinionOnly", &cHardwareCPU::Inst_GetOpinionOnly_ZeroIfNone, nInstFlag::STALL),
+		tInstLibEntry<tMethod>("clear-opinion", &cHardwareCPU::Inst_ClearOpinion, nInstFlag::STALL),
+		tInstLibEntry<tMethod>("if-opinion-set", &cHardwareCPU::Inst_IfOpinionSet, nInstFlag::STALL),
 		
 		// Data collection
 		tInstLibEntry<tMethod>("if-cell-data-changed", &cHardwareCPU::Inst_IfCellDataChanged, nInstFlag::STALL),
@@ -8243,7 +8246,36 @@
   return true;
 }
 
+bool cHardwareCPU::Inst_GetOpinionOnly_ZeroIfNone(cAvidaContext& ctx)
+{
+	assert(m_organism != 0);
+	const int opinion_reg = FindModifiedRegister(REG_BX);
+  if(m_organism->HasOpinion()) {
+    GetRegister(opinion_reg) = m_organism->GetOpinion().first;
+  } else {
+		GetRegister(opinion_reg) = 0;
+	}
+  return true;
+}
 
+
+bool cHardwareCPU::Inst_ClearOpinion(cAvidaContext& ctx) {
+	assert(m_organism != 0);
+	m_organism->ClearOpinion();
+	return true;
+}
+
+/*! If the organism has an opinion then execute the next instruction, else skip.
+ */
+bool cHardwareCPU::Inst_IfOpinionSet(cAvidaContext& ctx)
+{
+	assert(m_organism != 0);
+  if(!m_organism->HasOpinion()) IP().Advance();
+  return true;
+}
+
+
+
 /*! Collect this cell's data, and place it in ?BX?.  Set the flag indicating that
  this organism has collected cell data to true, and set the last collected cell data
  as well.

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/cpu/cHardwareCPU.h	2009-06-18 21:09:54 UTC (rev 3324)
@@ -802,6 +802,12 @@
   bool Inst_SetOpinion(cAvidaContext& ctx);
   //! Retrieve this organism's current opinion.
   bool Inst_GetOpinion(cAvidaContext& ctx);
+	//! Only get opinion.  If none then reg is set to zero
+	bool Inst_GetOpinionOnly_ZeroIfNone(cAvidaContext& ctx);
+	//! Clear this organism's current opinion.
+  bool Inst_ClearOpinion(cAvidaContext& ctx);
+	//! Execute next instruction is org has an opinion, otherwise skip
+	bool Inst_IfOpinionSet(cAvidaContext& ctx);
 
 	// -------- Cell Data Support --------
 public:

Modified: development/source/main/cDeme.cc
===================================================================
--- development/source/main/cDeme.cc	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/main/cDeme.cc	2009-06-18 21:09:54 UTC (rev 3324)
@@ -142,6 +142,17 @@
   return GetCell(pos).GetOrganism();
 }
 
+int cDeme::GetNumOrgsWithOpinion() const {
+	int demeSize = GetSize();
+	int count = 0;
+	
+	for(int pos = 0; pos < demeSize; ++pos) {
+		cPopulationCell& cell = GetCell(pos);
+		if(cell.IsOccupied() and cell.GetOrganism()->HasOpinion())
+			++count;
+	}
+	return count;
+}
 
 void cDeme::ProcessUpdate() {
 	// test deme predicate

Modified: development/source/main/cDeme.h
===================================================================
--- development/source/main/cDeme.h	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/main/cDeme.h	2009-06-18 21:09:54 UTC (rev 3324)
@@ -179,8 +179,10 @@
 
   int GetOrgCount() const { return cur_org_count; }
   int GetLastOrgCount() const { return last_org_count; }
+
 	double GetDensity() const { return static_cast<double>(cur_org_count) / static_cast<double>(GetSize()); }
-
+	int GetNumOrgsWithOpinion() const;
+	
   void IncOrgCount() { cur_org_count++; }
   void DecOrgCount() { cur_org_count--; }
 

Modified: development/source/main/cGenomeUtil.cc
===================================================================
--- development/source/main/cGenomeUtil.cc	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/main/cGenomeUtil.cc	2009-06-18 21:09:54 UTC (rev 3324)
@@ -51,47 +51,51 @@
 int cGenomeUtil::CountInst(const cGenome & gen, const cInstruction & inst)
 {
   int count = 0;
-  for(int i = 0; i < gen.GetSize(); i++) {
-    if (gen[i] == inst) count++;
+  for(int i = 0; i < gen.GetSize(); ++i) {
+    if (gen[i] == inst) ++count;
   }
 	
   return count;
 }
 
 // Returns minimum distance between two instance of inst respecting genome circularity.
-// If only one instance is found then lenght of genome is returned.
+// If zero or one instance is found then 0 is returned.
 int cGenomeUtil::MinDistBetween(const cGenome& genome, const cInstruction& inst) {
 	const int genomeSize = genome.GetSize();
 	int firstInstance(-1);
 	int secondInstance(-1);
 	int startIndex(0);
 	int minDist(genomeSize);
-
+	assert(startIndex < genomeSize);
+	
 	while(startIndex < genomeSize) {
 		firstInstance = FindInst(genome, inst, startIndex);
-		startIndex = firstInstance + 1;
-		
-		if(startIndex >= genomeSize)
+		if(firstInstance == -1 && startIndex == 0) {
+			// no instance of inst
+			return 0;
+		} else if(firstInstance == -1) {
+			// no more instances
 			return minDist;
+		}
 		
+		startIndex = firstInstance + 1;
 		secondInstance = FindInst(genome, inst, startIndex);
+		
+		if(secondInstance == -1) {
+			// no instance between startIndex and end
+			// search from begining
+			secondInstance = FindInst(genome, inst, 0);
+			// worst-case this finds same instance of inst as firstInstance
+		}			
 	
-		if(firstInstance != -1 and secondInstance != -1) {
-			minDist = min(min(secondInstance-firstInstance, firstInstance+genomeSize-secondInstance), minDist);
-		} else if(secondInstance != -1) {
-			secondInstance = FindInst(genome, inst, 0);
-			if(firstInstance == secondInstance)
-				return minDist;
-			else {
-				assert(secondInstance < firstInstance);
-				minDist = min(secondInstance+genomeSize-firstInstance, minDist);
-			}
-		} else {
-			return minDist;
-		}
-	}
-	assert(false);
-	return -1;
+		if(firstInstance != secondInstance) {
+			minDist = min(min(abs(firstInstance-secondInstance), secondInstance+genomeSize-firstInstance), minDist);
+			assert(minDist > 0);
+		} else { // they are equal, so there is only one instance of inst
+			return 0;
+		} 
+	}	
+	return minDist;
 }
 
 

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/main/cOrganism.h	2009-06-18 21:09:54 UTC (rev 3324)
@@ -481,6 +481,8 @@
   const DatedOpinionList& GetOpinions() { InitOpinions(); return m_opinion->opinion_list; }
   //! Return whether this organism has an opinion.
   bool HasOpinion() { InitOpinions(); return m_opinion->opinion_list.size(); }
+	//! remove all opinions
+	void ClearOpinion() { InitOpinions(); m_opinion->opinion_list.clear(); }
   
 private:
   //! Initialize opinion support.

Modified: development/source/main/cStats.cc
===================================================================
--- development/source/main/cStats.cc	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/main/cStats.cc	2009-06-18 21:09:54 UTC (rev 3324)
@@ -1466,7 +1466,7 @@
 		for(map<cString, int>::iterator iter = demeResourceThresholdPredicateMap.begin(); iter != demeResourceThresholdPredicateMap.end(); ++iter) {
 			df.Write(iter->second, iter->first);
 			iter->second = 0;
-			assert(iter->second == demeResourceThresholdPredicateMap[iter->first]);
+			assert(iter->second == 0 && demeResourceThresholdPredicateMap[iter->first] == 0);
 		}
 		df.Endl();
 	}	
@@ -2227,6 +2227,51 @@
 	}	
 }
 
+void cStats::PrintOpinionsSetPerDeme(const cString& filename) {
+	cDataFile& df = m_world->GetDataFile(filename);
+	df.WriteComment("Current fractions of opinions set in deme.");
+	df.WriteComment("This files shows data for both treatable and untreatable demes.");
+	df.WriteTimeStamp();
+	
+	cIntSum    treatableOpinionCounts, untreatableOpinionCounts;
+	cDoubleSum treatableDensityCounts, untreatableDensityCounts;
+	treatableOpinionCounts.Clear();
+	untreatableOpinionCounts.Clear();
+	treatableDensityCounts.Clear();
+	untreatableDensityCounts.Clear();
+	
+	for(int i=0; i<m_world->GetPopulation().GetNumDemes(); ++i) {
+    cDeme& deme = m_world->GetPopulation().GetDeme(i);
+		int demeSize = deme.GetSize();
+		if(deme.isTreatable()) {
+			// accumultate counts for treatable demes 
+			for(int orgID = 0; orgID < demeSize; ++orgID) {
+				treatableOpinionCounts.Add(deme.GetNumOrgsWithOpinion());
+				treatableDensityCounts.Add(deme.GetDensity());
+			}
+		} else {
+			// accumultate counts for untreatable demes 
+			for(int orgID = 0; orgID < demeSize; ++orgID) {
+				untreatableOpinionCounts.Add(deme.GetNumOrgsWithOpinion());
+				untreatableDensityCounts.Add(deme.GetDensity());
+			}
+		}
+	}
+	
+	df.Write(GetUpdate(), "Update [update]");
+	
+	if(treatableOpinionCounts.N() > 0 and untreatableOpinionCounts.N() > 0) {
+		df.Write(treatableOpinionCounts.Average(), "Average number of opinions set in Treatable demes");
+		df.Write(untreatableOpinionCounts.Average(), "Average number of opinions set in Unreatable demes");
+		df.Write(treatableDensityCounts.Average(), "Average density of Treatable demes");
+		df.Write(untreatableDensityCounts.Average(), "Average density of Unreatable demes");
+	} else {
+		df.Write(untreatableOpinionCounts.Average(), "Average number of opinions set in demes");
+		df.Write(untreatableDensityCounts.Average(), "Average density of demes");
+	}
+	df.Endl();
+}
+
 /*! Called when an organism issues a flash instruction.
  
  We do some pretty detailed tracking here in order to support the use of flash

Modified: development/source/main/cStats.h
===================================================================
--- development/source/main/cStats.h	2009-06-17 20:03:21 UTC (rev 3323)
+++ development/source/main/cStats.h	2009-06-18 21:09:54 UTC (rev 3324)
@@ -919,6 +919,8 @@
 public:
 	//! Prints the current opinions of all organisms in the population.
 	void PrintCurrentOpinions(const cString& filename);
+	//! Prints the average number of organism with set opinions
+	void PrintOpinionsSetPerDeme(const cString& filename);
 	
 	// -------- Synchronization support --------
 public:




More information about the Avida-cvs mailing list