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

hjg at myxo.css.msu.edu hjg at myxo.css.msu.edu
Fri Nov 21 13:45:49 PST 2008


Author: hjg
Date: 2008-11-21 16:45:48 -0500 (Fri, 21 Nov 2008)
New Revision: 2960

Modified:
   branches/hjg-dev/source/cpu/cHardwareCPU.cc
   branches/hjg-dev/source/cpu/cHardwareCPU.h
   branches/hjg-dev/source/main/cAvidaConfig.h
   branches/hjg-dev/source/main/cOrganism.cc
   branches/hjg-dev/source/main/cOrganism.h
   branches/hjg-dev/source/main/cTaskLib.cc
Log:
String reciprocity basic setup.

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-11-21 19:52:27 UTC (rev 2959)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-11-21 21:45:48 UTC (rev 2960)
@@ -457,7 +457,11 @@
   	tInstLibEntry<tMethod>("donate-frm", &cHardwareCPU::Inst_DonateFacingRawMaterials, nInstFlag::STALL),
   	tInstLibEntry<tMethod>("donate-spec", &cHardwareCPU::Inst_DonateFacingRawMaterialsOtherSpecies, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("donate-k", &cHardwareCPU::Inst_DonateFacingConditionalOnK, nInstFlag::STALL),
+		tInstLibEntry<tMethod>("donate-string", &cHardwareCPU::Inst_DonateString, nInstFlag::STALL),
   	tInstLibEntry<tMethod>("donate-if-donor", &cHardwareCPU::Inst_DonateIfDonor, nInstFlag::STALL),		
+		tInstLibEntry<tMethod>("donate-string-if-donor",  &cHardwareCPU::Inst_DonateStringIfDonor, 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),
@@ -471,7 +475,7 @@
 		tInstLibEntry<tMethod>("rotate-to-rep-k", &cHardwareCPU::Inst_RotateToReputationK, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("rotate-to-rep-and-donate", &cHardwareCPU::Inst_RotateToGreatestReputationAndDonate, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("if-donor",  &cHardwareCPU::Inst_IfDonor, nInstFlag::STALL),
-//		bool Inst_DonateIfDonor(cAvidaContext& ctx);	
+
 	
 
     // Must always be the last instruction in the array
@@ -7059,4 +7063,62 @@
 
 }
 
+/* This instruction enables an organism to donate a string to its
+ facing neighbor. It is sort of a meta-instruction that encapsulates
+ sending a string as a message using send-message and retrieve-message. 
+ So that we can do fun reciprocity-style experiments it also keeps
+ track of organisms that have donated strings to others. 
+ */
 
+bool cHardwareCPU::Inst_DonateString(cAvidaContext& ctx) 
+{
+	// Make sure the organism has a facing neighbor of the right sort
+	// (there is a config option that says you can only donate between
+	// species).
+	
+	cOrganism * neighbor = organism->GetNeighbor();
+	if (neighbor != NULL) {
+		
+		// Check if the donation is from one species to another
+		// If the config option is selected, then exit otherwise.
+		if (m_world->GetConfig().INTER_SPECIES_DONATION.Get()) {
+			int spec_self =  organism->GetLineageLabel();
+			int spec_neighbor = neighbor->GetLineageLabel();
+			if (spec_self == spec_neighbor) return true;
+		}
+		
+		// Get the organism's string from its output buffer
+		tBuffer<int> org_str (organism->GetOuptput());
+
+		// This will make the string the size of the capacity. 
+		int length = org_str.GetCapacity();
+		int org_str_length = org_str.GetTotal(); 
+	
+		while (org_str_length < length) { 
+			org_str.Add(-1);
+			org_str_length = org_str.GetTotal(); 
+		}
+		
+		// Donate the string
+		neighbor->ReceiveString(org_str, organism->GetID());
+	
+		// Update all the relevant stats.
+		organism->Donated();
+	}
+
+	return true;
+}
+
+
+/* Donate if the neighbor previously donated to the organism. */
+bool cHardwareCPU::Inst_DonateStringIfDonor(cAvidaContext& ctx)
+{
+  cOrganism * neighbor = organism->GetNeighbor();
+	if (neighbor != NULL) {
+		// check if the neighbor was a donor
+		if (organism->IsDonor(neighbor->GetID())) {
+			Inst_DonateString(ctx);	
+		}
+	}
+	return true;
+}

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-11-21 19:52:27 UTC (rev 2959)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-11-21 21:45:48 UTC (rev 2960)
@@ -689,6 +689,12 @@
 	bool Inst_DonateIfDonor(cAvidaContext& ctx);	
 	// Donate a raw material to the neighbor, if its reputation > k
 	bool Inst_DonateFacingConditionalOnK(cAvidaContext& ctx);
+	// Donate string
+	bool Inst_DonateString(cAvidaContext& ctx);
+	// Donate a string to the neighbor if it was a donor
+	bool Inst_DonateStringIfDonor(cAvidaContext& ctx);
+	
+	
 	// Rotate to the organims with the greatest reputation
 	bool Inst_RotateToGreatestReputation(cAvidaContext& ctx);	
 	// Rotate to the organims with the greatest reputation and donate

Modified: branches/hjg-dev/source/main/cAvidaConfig.h
===================================================================
--- branches/hjg-dev/source/main/cAvidaConfig.h	2008-11-21 19:52:27 UTC (rev 2959)
+++ branches/hjg-dev/source/main/cAvidaConfig.h	2008-11-21 21:45:48 UTC (rev 2960)
@@ -568,6 +568,7 @@
   CONFIG_ADD_VAR(REPUTATION_REWARD, int, 0, "Reward an organism for having a good reputation");
   CONFIG_ADD_VAR(DONATION_FAILURE_PERCENT, int, 0, "Percentage of times that a donation fails");
   CONFIG_ADD_VAR(RANDOMIZE_RAW_MATERIAL_AMOUNT, int, 0, "Should all the organisms receive the same amount 0/1 (off/on)");
+  CONFIG_ADD_VAR(INTER_SPECIES_DONATION, int, 0, "Only donations from one species to another are allowed 0/1 (off/on)");
   
 #endif
   

Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc	2008-11-21 19:52:27 UTC (rev 2959)
+++ branches/hjg-dev/source/main/cOrganism.cc	2008-11-21 21:45:48 UTC (rev 2960)
@@ -850,4 +850,35 @@
 }
 
 
+void cOrganism::ReceiveString(tBuffer<int> str, int org_id) 
+{ 
+	m_all_received_messages.push_back(str); 
+	donor_list.insert(org_id); 
+	m_num_donate_received++;
+}
 
+/* Return an int that represents the largest number of bits
+ matched by any of the strings received. */
+int cOrganism::MatchString(const cString& string_to_match) 
+{
+  int num_matched = 0;
+  int max_num_matched = 0;
+	
+	if (m_all_received_messages.size()) {
+	
+		for (unsigned int k = 0; k < m_all_received_messages.size(); k++) {
+			tBuffer<int> received = m_all_received_messages[k];
+			num_matched = 0;
+			for (int j = 0; j < string_to_match.GetSize(); j++)
+			{
+				if (string_to_match[j]=='0' && received[j]==0 ||
+						string_to_match[j]=='1' && received[j]==1)
+					num_matched++;
+			}
+			if (num_matched > max_num_matched) max_num_matched = num_matched;
+		}
+		
+	}
+	return max_num_matched;
+}
+

Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h	2008-11-21 19:52:27 UTC (rev 2959)
+++ branches/hjg-dev/source/main/cOrganism.h	2008-11-21 21:45:48 UTC (rev 2960)
@@ -113,6 +113,7 @@
   tBuffer<int> m_input_buf;
   tBuffer<int> m_output_buf;
   tBuffer<int> m_received_messages;
+	vector < tBuffer<int> >  m_all_received_messages;
   tList<tListNode<cSaleItem> > m_sold_items;
 
   // Communication
@@ -261,6 +262,7 @@
   void ClearInput() { m_input_buf.Clear(); }
   void ResetInput() {m_input_pointer = 0; m_input_buf.Clear(); };
   void AddOutput(int val) { m_output_buf.Add(val); }
+	tBuffer<int> GetOuptput() { return m_output_buf; }
 
   // --------  Divide Methods  --------
   bool Divide_CheckViable();
@@ -482,9 +484,13 @@
 	void DecK() { m_k--; }
 	// was the organism a donor
 	bool IsDonor(int neighbor_id); 
-	// get amount of donations received
+	// Receive a string
+	void ReceiveString(tBuffer<int> str, int org_id); 
+	// Return the maximum number of characters matched by the received strings
+	int MatchString(const cString& str); 
 	
 	
+	
 protected:
 	// The organism's own raw materials
 	int m_self_raw_materials; 

Modified: branches/hjg-dev/source/main/cTaskLib.cc
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.cc	2008-11-21 19:52:27 UTC (rev 2959)
+++ branches/hjg-dev/source/main/cTaskLib.cc	2008-11-21 21:45:48 UTC (rev 2960)
@@ -1963,9 +1963,10 @@
     max_num_matched = num_matched;
 		
   }
+
   
   bool used_received = false;
-  if (ctx.GetReceivedMessages()) {
+/*  if (ctx.GetReceivedMessages()) {
     tBuffer<int> received(*(ctx.GetReceivedMessages()));
     for (int i = 0; i < received.GetNumStored(); i++) {
       test_output = received[i];
@@ -1977,13 +1978,14 @@
         if ((string_to_match[string_index]=='0' && !(test_output & k)) ||
             (string_to_match[string_index]=='1' && (test_output & k))) num_matched++;
       }
-      
-      if (num_matched > max_num_matched) {
-        max_num_matched = num_matched;
-        used_received = true;
-      }
-    }
-  }
+*/ 
+	num_matched = ctx.GetOrganism()->MatchString(string_to_match);
+	if (num_matched > max_num_matched) {
+		max_num_matched = num_matched;
+		used_received = true;
+	}
+//    }
+//  }
   
   double bonus = 0.0;
   // return value between 0 & 1 representing the percentage of string that was matched
@@ -2001,6 +2003,7 @@
     else
       m_world->GetStats().AddMarketOwnItemUsed();
   }
+	
   return bonus;
 }
 




More information about the Avida-cvs mailing list