[Avida-SVN] r2901 - in branches/hjg-dev/source: cpu main

hjg at myxo.css.msu.edu hjg at myxo.css.msu.edu
Thu Oct 30 19:03:43 PDT 2008


Author: hjg
Date: 2008-10-30 22:03:43 -0400 (Thu, 30 Oct 2008)
New Revision: 2901

Modified:
   branches/hjg-dev/source/cpu/cHardwareCPU.cc
   branches/hjg-dev/source/cpu/cHardwareCPU.h
   branches/hjg-dev/source/main/cOrganism.cc
   branches/hjg-dev/source/main/cOrganism.h
   branches/hjg-dev/source/main/cPopulationInterface.cc
   branches/hjg-dev/source/main/cStats.cc
   branches/hjg-dev/source/main/cStats.h
Log:
direct reciprocity donate-if-donor instruction

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-10-31 01:59:20 UTC (rev 2900)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-10-31 02:03:43 UTC (rev 2901)
@@ -455,6 +455,7 @@
   	tInstLibEntry<tMethod>("donate-frm", &cHardwareCPU::Inst_DonateFacingRawMaterials, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("donate-k", &cHardwareCPU::Inst_DonateFacingConditionalOnK, nInstFlag::STALL),
   	tInstLibEntry<tMethod>("donate-pr", &cHardwareCPU::Inst_DonateProbReciprocate, nInstFlag::STALL),		
+  	tInstLibEntry<tMethod>("donate-if-donor", &cHardwareCPU::Inst_DonateIfDonor, nInstFlag::STALL),		
     tInstLibEntry<tMethod>("get-neighbors-reputation", &cHardwareCPU::Inst_GetNeighborsReputation, nInstFlag::STALL),
     tInstLibEntry<tMethod>("get-reputation", &cHardwareCPU::Inst_GetReputation, nInstFlag::STALL),
     tInstLibEntry<tMethod>("get-raw-mat-amount", &cHardwareCPU::Inst_GetAmountOfRawMaterials, nInstFlag::STALL),
@@ -469,7 +470,8 @@
 		tInstLibEntry<tMethod>("rotate-to-rep", &cHardwareCPU::Inst_RotateToGreatestReputation, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("rotate-to-rep-k", &cHardwareCPU::Inst_RotateToReputationK, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("rotate-to-rep-and-donate", &cHardwareCPU::Inst_RotateToGreatestReputationAndDonate, nInstFlag::STALL),
-		
+//		bool Inst_DonateIfDonor(cAvidaContext& ctx);	
+	
 
     // Must always be the last instruction in the array
     tInstLibEntry<tMethod>("NULL", &cHardwareCPU::Inst_Nop, 0, "True no-operation instruction: does nothing"),
@@ -6724,16 +6726,36 @@
 	return true;
 }
 
+/* Donate if the neighbor previously donated to the organism. */
+bool cHardwareCPU::Inst_DonateIfDonor(cAvidaContext& ctx)
+{
+  cOrganism * neighbor = organism->GetNeighbor();
+	if (neighbor != NULL) {
+		// check if the neighbor was a donor
+		if (organism->IsDonor(neighbor->GetID())) {
+			m_world->GetStats().IncDonateToDonor();
+			Inst_DonateFacingRawMaterials(ctx);	
+		}
+	}
 
+	return true;
+
+}
+
 /* Donate raw materials to the facing organism. */
 bool cHardwareCPU::Inst_DonateFacingRawMaterials(cAvidaContext& ctx)
 {
   
 	// Get faced neighbor
   cOrganism * neighbor = organism->GetNeighbor();
-  
+	
   // Donate only if we have found a neighbor.
   if (neighbor != NULL) {
+	
+	// update stats
+	m_world->GetStats().IncDonateToFacing();
+
+	
     // Subtract raw materials from the organism (currently subtracts 1 resource...)
 	// fails if the organism does not have any more resources
 	if (organism->SubtractSelfRawMaterials(1)) {

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-10-31 01:59:20 UTC (rev 2900)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-10-31 02:03:43 UTC (rev 2901)
@@ -678,6 +678,8 @@
   */ 
   // Donate a raw material to the neighbor
   bool Inst_DonateFacingRawMaterials(cAvidaContext& ctx);
+	// Donate a raw material to the neighbor if it was a prior donor
+	bool Inst_DonateIfDonor(cAvidaContext& ctx);	
 	// Donate a raw material to the neighbor, if its reputation > k
 	bool Inst_DonateFacingConditionalOnK(cAvidaContext& ctx);
 	// Rotate to the organims with the greatest reputation

Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc	2008-10-31 01:59:20 UTC (rev 2900)
+++ branches/hjg-dev/source/main/cOrganism.cc	2008-10-31 02:03:43 UTC (rev 2901)
@@ -847,6 +847,15 @@
 	}
 }
 
+/* Check if an organism has previously donated to this organism */
+bool cOrganism::IsDonor(int neighbor_id) 
+{
+	bool found = false;
+	if (donor_list.find(neighbor_id) != donor_list.end()) {
+		found = true;
+	}
+	return found;
+}
 
 
 

Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h	2008-10-31 01:59:20 UTC (rev 2900)
+++ branches/hjg-dev/source/main/cOrganism.h	2008-10-31 02:03:43 UTC (rev 2901)
@@ -482,6 +482,8 @@
 	void IncK() { m_k++; }
 	// decrease k
 	void DecK() { m_k--; }
+	// was the organism a donor
+	bool IsDonor(int neighbor_id); 
 	
 protected:
 	// The organism's own raw materials

Modified: branches/hjg-dev/source/main/cPopulationInterface.cc
===================================================================
--- branches/hjg-dev/source/main/cPopulationInterface.cc	2008-10-31 01:59:20 UTC (rev 2900)
+++ branches/hjg-dev/source/main/cPopulationInterface.cc	2008-10-31 02:03:43 UTC (rev 2901)
@@ -439,8 +439,8 @@
 
 					// if it has high reputation	
 					if (cur_neighbor->GetID() == high_org_id) {
-						int test_id = cur_neighbor->GetID();
-						int test_rep = cur_neighbor->GetReputation();
+//						int test_id = cur_neighbor->GetID();
+	//					int test_rep = cur_neighbor->GetReputation();
 						break;
 					}
 				}

Modified: branches/hjg-dev/source/main/cStats.cc
===================================================================
--- branches/hjg-dev/source/main/cStats.cc	2008-10-31 01:59:20 UTC (rev 2900)
+++ branches/hjg-dev/source/main/cStats.cc	2008-10-31 02:03:43 UTC (rev 2901)
@@ -133,6 +133,8 @@
   , m_spec_num(0)
   , m_spec_waste(0)
   , m_deme_num_repls(0)
+	, m_donate_to_donor (0)
+	, m_donate_to_facing (0)
 {
   const cEnvironment& env = m_world->GetEnvironment();
   const int num_tasks = env.GetNumTasks();
@@ -1782,6 +1784,8 @@
 	df.Write(num_alt, "Number of altruists [altruists]");
 	df.Write(num_coop, "Number of cooperators [cooperators]");
 	df.Write(k.Average(), "Avg. k of organisms [k]");
+	df.Write(m_donate_to_donor, "Number of donate to donor [donatedonor]");
+	df.Write(m_donate_to_facing, "Number of donate to facing [donatefacing]");
 
 
 	

Modified: branches/hjg-dev/source/main/cStats.h
===================================================================
--- branches/hjg-dev/source/main/cStats.h	2008-10-31 01:59:20 UTC (rev 2900)
+++ branches/hjg-dev/source/main/cStats.h	2008-10-31 02:03:43 UTC (rev 2901)
@@ -785,6 +785,11 @@
 public: 
   // Print statistics about reputation
   void PrintReputationData(const cString& filename);
+	void IncDonateToDonor() { m_donate_to_donor++; } 
+	void IncDonateToFacing() { m_donate_to_facing++; }
+protected: 
+	int m_donate_to_donor; 
+	int m_donate_to_facing;
 
   
   // -------- Deme replication support --------




More information about the Avida-cvs mailing list