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

hjg at myxo.css.msu.edu hjg at myxo.css.msu.edu
Mon Dec 22 16:21:29 PST 2008


Author: hjg
Date: 2008-12-22 19:21:29 -0500 (Mon, 22 Dec 2008)
New Revision: 3072

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/cEnvironment.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/cTaskLib.cc
   branches/hjg-dev/source/main/cTaskLib.h
Log:
Modified the producing string model to be generalizable for various length and number of strings. 

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-12-23 00:21:29 UTC (rev 3072)
@@ -460,10 +460,7 @@
   	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>("donate-string-if-donor-rep",  &cHardwareCPU::Inst_DonateStringIfDonorRep, nInstFlag::STALL),
 
     tInstLibEntry<tMethod>("get-neighbors-reputation", &cHardwareCPU::Inst_GetNeighborsReputation, nInstFlag::STALL),
     tInstLibEntry<tMethod>("get-reputation", &cHardwareCPU::Inst_GetReputation, nInstFlag::STALL),
@@ -481,7 +478,6 @@
 		tInstLibEntry<tMethod>("rotate-to-rep-lineage", &cHardwareCPU::Inst_RotateToGreatestReputationWithDifferentLineage, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("rotate-to-tag", &cHardwareCPU::Inst_RotateToDifferentTag, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("if-donor",  &cHardwareCPU::Inst_IfDonor, nInstFlag::STALL),
-		tInstLibEntry<tMethod>("str-cat",  &cHardwareCPU::Inst_StrCatOrg, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("prod-string",  &cHardwareCPU::Inst_ProduceString, nInstFlag::STALL),
 
     // Must always be the last instruction in the array
@@ -7024,9 +7020,9 @@
   // Donate only if we have found a neighbor.
   if (neighbor != NULL) {
 
-    // Subtract raw materials from the organism (currently subtracts 1 resource...)
+    // Subtract raw materials from the organism
 	// fails if the organism does not have any more resources
-	if (organism->SubtractSelfRawMaterials(cost)) {
+	if (organism->SubtractRawMaterials(organism->GetLineageLabel(), cost)) {
 	
 		// sometimes the donation will fail. 
 		// get the probability of failure
@@ -7039,7 +7035,7 @@
 		}
 	
 	
-		neighbor->AddOtherRawMaterials(cost, organism->GetID()); 
+		neighbor->AddRawMaterials(organism->GetLineageLabel(), cost, organism->GetID()); 
 		
 		// rotate recipient to face donor 
 		// by rotating until the recipient faces the donor
@@ -7365,149 +7361,38 @@
 
 }
 
-/* This instruction enables an organism to donate a string to its
- facing neighbor. 
- 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().DONATION_RESTRICTIONS.Get() == 1) {
-			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(); 
-		}
-
-		// Update the organism's reputation on the basis of the string
-		int max = 0; 
-		int cur = 0; 
-		int best = 0;
-		vector<cString> strings_to_match = m_world->GetEnvironment().GetMatchStringsFromTask();
-		for (unsigned int i=0; i < strings_to_match.size(); i++) {
-			cur = organism->MatchStringsHelper(org_str, strings_to_match[i]);
-			if (cur > max) {
-				max = cur; 
-				best = i;
-			}
-		}
-		
-			
-		
-		// If the organism has the same tag as the neighbor, disable the 
-		// donation.
-		if (m_world->GetConfig().DONATION_RESTRICTIONS.Get() == 2) {
-			// Check if the donation is from one tag to another
-			if (organism->GetTagLabel() == neighbor->GetTagLabel()) 
-				return true;
-		}
-		
-		// update the reputation unless the organism is a str catter	
-		if (!organism->GetStringCatter()){
-
-			int len = 1; 
-			if (strings_to_match.size()) len = strings_to_match[0].GetSize();
-		//	organism->SetAverageReputation(max);
-			int rep = (max - floor((double)len/2));
-			
-			if (rep > 0) {
-				if (m_world->GetConfig().STRING_REPUTATION.Get() == 0) {
-					organism->SetAverageReputation(rep);
-				} else if (m_world->GetConfig().STRING_REPUTATION.Get() == 1){ 
-					organism->AddReputation(rep);
-				}
-			}
-		
-		
-		}
-		// 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;
-}
-
-
-/* Donate if the neighbor has a reputation of > 0 */
-bool cHardwareCPU::Inst_DonateStringIfDonorRep(cAvidaContext& ctx)
-{
-  cOrganism * neighbor = organism->GetNeighbor();
-	if (neighbor != NULL) {
-		// check if the neighbor was a donor
-		if (neighbor->GetReputation()) {
-			Inst_DonateString(ctx);	
-		}
-	}
-	return true;
-}
-
-
-/* This instruction does not do anything, but rather is used
- as a signal to indicate that the organism has chosen to perform 
- the string cat task. */
-bool cHardwareCPU::Inst_StrCatOrg(cAvidaContext& ctx)
-{
-	organism->SetStringCatter(true);
-	return true;
-}
-
-
 /* Check if the string in the organisms buffer corresponds to the 
  string it is producing. If so, -1 out the buffer and increment the 
  number of raw materials the organism has. Otherwise, do nothing. */
 bool cHardwareCPU::Inst_ProduceString(cAvidaContext& ctx)
 {
-
-	int num = 0;
+	int num = -1;
+	int size = 0;
 	// For the time being this instruction is hardcoded for 2 strings and
 	// two species.
-	if ((organism->GetLineageLabel() == 0)) {
-		num = organism->MatchOutputBuffer(cString("0000")); 
+	/* if ((organism->GetLineageLabel() == 0)) {
+		num = organism->MatchOutputBuffer(cString("00000000")); 
 	} else if ((organism->GetLineageLabel() == 1)){ 
-		num =  organism->MatchOutputBuffer(cString("1111")); 
-	}
+		num =  organism->MatchOutputBuffer(cString("11111111")); 
+	}*/ 
 	
-	if (num == 4) {
-		organism->AddSelfRawMaterials(1); 
+	map < int, cString> my_strings = 	m_world->GetEnvironment().GetMatchStringsFromTask();
+	// iterate through the vector until you find the right tag. 
+	// then check if the buffer matches it. 
+	// record the size
+	//for (unsigned int i=0; i < my_strings.size(); i++) { 
+		//if (organism->GetLineageLabel() == my_strings[i].first) { 
+			//num = organism->MatchOutputBuffer(my_strings[i].second); 
+			//size = my_strings[i].second.GetSize();
+		//}
+	//}
+	
+	num = organism->MatchOutputBuffer(my_strings[organism->GetTagLabel()]); 
+	size = my_strings[organism->GetTagLabel()].GetSize();
+	
+	if (num == size) {
+		organism->AddRawMaterials(organism->GetTagLabel(), 1, organism->GetID()); 
 		organism->SetOutputNegative1() ; 
 	}
 	return true;

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-12-23 00:21:29 UTC (rev 3072)
@@ -696,9 +696,9 @@
 	// Donate a raw material to the neighbor, if its reputation > k
 	bool Inst_DonateFacingConditionalOnK(cAvidaContext& ctx);
 	// Donate string
-	bool Inst_DonateString(cAvidaContext& ctx);
+	//bool Inst_DonateString(cAvidaContext& ctx);
 	// Donate a string to the neighbor if it was a donor
-	bool Inst_DonateStringIfDonor(cAvidaContext& ctx);
+	//bool Inst_DonateStringIfDonor(cAvidaContext& ctx);
 	
 	// Rotate to the organims with the greatest reputation
 	bool Inst_RotateToGreatestReputation(cAvidaContext& ctx);	
@@ -719,7 +719,7 @@
 	// Execute the following instruction if the facing neighbor was a donor
 	bool Inst_IfDonor(cAvidaContext& ctx);
 	// Indicate that the organism is a string catter
-	bool Inst_StrCatOrg(cAvidaContext& ctx);
+	//bool Inst_StrCatOrg(cAvidaContext& ctx);
 	// Produce string
 	bool Inst_ProduceString(cAvidaContext& ctx);
 	

Modified: branches/hjg-dev/source/main/cAvidaConfig.h
===================================================================
--- branches/hjg-dev/source/main/cAvidaConfig.h	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/main/cAvidaConfig.h	2008-12-23 00:21:29 UTC (rev 3072)
@@ -571,6 +571,7 @@
   CONFIG_ADD_VAR(DONATION_RESTRICTIONS, int, 0, "0=none\n1=inter-species only\n2=different tag only");
   CONFIG_ADD_VAR(INHERIT_REPUTATION, int, 0, "0=reputations are not inherited\n1=reputations are inherited\n2=tags are inherited");
   CONFIG_ADD_VAR(STRING_REPUTATION, int, 0, "0=reputation is average\n1=reputation is additive");
+  CONFIG_ADD_VAR(RAW_MATERIAL_CAP, int, -1, "-1=no cap");
   
 #endif
   

Modified: branches/hjg-dev/source/main/cEnvironment.h
===================================================================
--- branches/hjg-dev/source/main/cEnvironment.h	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/main/cEnvironment.h	2008-12-23 00:21:29 UTC (rev 3072)
@@ -144,7 +144,7 @@
   const cTaskEntry& GetTask(int id) const { return m_tasklib.GetTask(id); }
   bool UseNeighborInput() const { return m_tasklib.UseNeighborInput(); }
   bool UseNeighborOutput() const { return m_tasklib.UseNeighborOutput(); }
-	vector<cString> GetMatchStringsFromTask() { return m_tasklib.GetMatchStrings(); }
+	map <int, cString>  GetMatchStringsFromTask() { return m_tasklib.GetMatchStrings(); }
 
   const cResourceLib& GetResourceLib() const { return resource_lib; }
   const cReactionLib& GetReactionLib() const { return reaction_lib; }

Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/main/cOrganism.cc	2008-12-23 00:21:29 UTC (rev 3072)
@@ -85,7 +85,6 @@
 	, m_k(0)
   , m_failed_reputation_increases(0)
   , m_tag(make_pair(-1, 0))
-  , m_strcatter(0)
 {
   m_hardware = m_world->GetHardwareManager().Create(this);
 
@@ -769,10 +768,15 @@
 bool cOrganism::SubtractSelfRawMaterials (int amount)
 {
 	bool isSuccessful = false;
-	if (amount <= m_self_raw_materials) { 
+	if (amount <= m_raw_materials[GetLineageLabel()]) {
+		isSuccessful = true;
+		m_raw_materials[GetLineageLabel()] -= amount; 
+		
+	}
+/*	if (amount <= m_self_raw_materials) { 
 		isSuccessful = true; 
 		m_self_raw_materials -= amount;
-	}
+	}*/
 	return isSuccessful;
 }
 
@@ -824,6 +828,35 @@
 }
 
 
+/* Called to add raw materials of a given type. */
+bool cOrganism::AddRawMaterials (int mat, int amount, int donor_id) {
+	bool isSuccessful = true;
+	
+	if (((m_raw_materials[mat] + amount) < m_world->GetConfig().RAW_MATERIAL_CAP.Get()) ||
+		(m_world->GetConfig().RAW_MATERIAL_CAP.Get() == -1)){
+		m_raw_materials[mat] += amount; 
+		donor_list.insert(donor_id);	
+		if (donor_id != m_id) {
+			m_num_donate_received += amount;
+			m_amount_donate_received++;
+		}
+	}
+	return isSuccessful;
+}
+
+/* Called to subtract raw materials of a given type. */
+bool cOrganism::SubtractRawMaterials (int mat, int amount) {
+	bool isSuccessful = true;
+	
+	if (amount <= m_raw_materials[mat]) {
+		isSuccessful = true;
+		m_raw_materials[mat] -= amount; 
+		
+	}
+	
+	return isSuccessful;
+}
+
 /* Get an organism's reputation, which is expressed as an 
 	opinion. 0 is the default reputation (this should be refactored
 	to be cleaner). */
@@ -860,60 +893,6 @@
 }
 
 
-void cOrganism::ReceiveString(tBuffer<int> str, int org_id) 
-{ 
-//	m_all_received_messages.push_back(new_donation); 
-	donor_list.insert(org_id); 
-	m_all_received_messages.push_back(make_pair(str, org_id));
-	m_num_donate_received++;
-}
-
-/* Return an int that represents the largest number of bits
- matched by any of the strings received. */
-std::pair < int, int > cOrganism::MatchString(const cString& string_to_match) 
-{
-  int num_matched = 0;
-  int max_num_matched = 0;
-	int org_id = 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].first;
-			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++;
-			}*/
-			num_matched = MatchStringsHelper(received, string_to_match);
-			if (num_matched > max_num_matched){
-				max_num_matched = num_matched;
-				org_id = m_all_received_messages[k].second;
-			}
-		}
-		
-	}
-	return make_pair(max_num_matched, org_id);
-}
-
-/* Return the number of bits matched in the two strings. This is 
- generalized so that it can be used not just by the task, but also
- by the donate-string instruction. */
-int cOrganism::MatchStringsHelper(tBuffer<int> org_string, const cString& string_to_match)
-{
-	int num_matched = 0;
-	for (int j = 0; j < string_to_match.GetSize(); j++)
-	{
-		if (string_to_match[j]=='0' && org_string[j]==0 ||
-				string_to_match[j]=='1' && org_string[j]==1)
-			num_matched++;
-	}
-	return num_matched;
-}
-
-
 /* Increase the reputation of all of the organisms that donated to this
  organism. Then, clear the donors list.*/
 void cOrganism::PraiseDonors()
@@ -951,57 +930,6 @@
 }
 
 
-// Return how well the organism does on catting strings.
-int cOrganism::CheckStrCat(const cString& string_to_match)
-{
-	// for each donated string try this...
-	// Get org string & make right length
-	tBuffer<int> test_str(string_to_match.GetSize());
-	tBuffer<int> org_str (GetOutputBuf());
-	
-	int max_num_matched =0;
-	
-	// 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(); 
-	}
-	
-	int num_matched = 0;
-
-	if (m_all_received_messages.size()) {
-		// Check with org in first position...
-		for (unsigned int k = 0; k < m_all_received_messages.size(); k++) {
-			tBuffer<int> received = m_all_received_messages[k].first;
-			num_matched = 0;
-			test_str.Cat(org_str);
-			test_str.Cat(received);	
-			num_matched = MatchStringsHelper(test_str, string_to_match);
-			if (num_matched > max_num_matched){
-				max_num_matched = num_matched;
-			}
-		}
-			
-			// Check with org in first position...
-			for (unsigned int k = 0; k < m_all_received_messages.size(); k++) {
-				tBuffer<int> received = m_all_received_messages[k].first;
-				num_matched = 0;
-				test_str.Cat(received);	
-				test_str.Cat(org_str);
-				num_matched = MatchStringsHelper(test_str, string_to_match);
-				if (num_matched > max_num_matched){
-					max_num_matched = num_matched;
-				}
-			
-		}
-		
-	}
-	return max_num_matched;
-}
-
 /* See if the output buffer matches the string */
 int cOrganism::MatchOutputBuffer(cString string_to_match)
 {

Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/main/cOrganism.h	2008-12-23 00:21:29 UTC (rev 3072)
@@ -30,6 +30,7 @@
 #include <string>
 #include <vector>
 #include <set>
+#include <map>
 
 #ifndef cCPUMemory_h
 #include "cCPUMemory.h"
@@ -460,6 +461,16 @@
 	int GetSelfRawMaterials() { return m_self_raw_materials; }
 	// retrieve the amount of raw materials collected from others
 	int GetOtherRawMaterials() { return m_other_raw_materials; }
+	
+	// Add Raw Materials
+	bool AddRawMaterials (int mat, int amount, int donor_id);
+	// Subtract Raw Materials
+	bool SubtractRawMaterials (int mat, int amount);
+	// Get Raw Material Amount
+	int GetRawMaterialAmount (int mat) { return m_raw_materials[mat]; }
+	
+	
+	
 	// get the organism's reputation
 	int GetReputation(); 
 	// set the organism's reputation
@@ -493,14 +504,14 @@
 	// was the organism a donor
 	bool IsDonor(int neighbor_id); 
 	// Receive a string
-	void ReceiveString(tBuffer<int> str, int org_id); 
+//	void ReceiveString(tBuffer<int> str, int org_id); 
 	// Return the maximum number of characters and the organism id matched by the received strings
-	std::pair < int, int > MatchString(const cString& str); 
+//	std::pair < int, int > MatchString(const cString& str); 
 	//  Return the maximum number of characters matched by the strings
-	int MatchStringsHelper(tBuffer<int> org_string, const cString& string_to_match); 
+//	int MatchStringsHelper(tBuffer<int> org_string, const cString& string_to_match); 
 	
 	// Return how well the organism does on catting strings.
-	int CheckStrCat(const cString& string_to_match); 
+//	int CheckStrCat(const cString& string_to_match); 
 	// Check if buffer contains this string; return # bits correct
 	int MatchOutputBuffer(cString string_to_match);
 
@@ -521,16 +532,19 @@
 	// Get tag
 	int GetTagLabel() { return m_tag.first; }
 	pair < int, int > GetTag() { return m_tag; }
-	void SetStringCatter(bool val) { m_strcatter = val; }
-	bool GetStringCatter() { return m_strcatter; }
+//	void SetStringCatter(bool val) { m_strcatter = val; }
+//	bool GetStringCatter() { return m_strcatter; }
 	void SetOutputNegative1();
 
 	
 protected:
-	// The organism's own raw materials
+  // The organism's own raw materials
 	int m_self_raw_materials; 
 	// The raw materials an oranism has collected from others
-	int m_other_raw_materials;
+	int m_other_raw_materials; 
+	
+	
+	map< int, int > m_raw_materials;
   // Organisms that have donated to this organism
 	set<int> donor_list;
 	// number of donations
@@ -544,11 +558,11 @@
 	// reputation minimum for donation/rotation 
 	// based on Nowak89
 	int m_k;
-	vector < std::pair < tBuffer<int>, int > > m_all_received_messages;
+//	vector < std::pair < tBuffer<int>, int > > m_all_received_messages;
 	// int number of reputation increase failures
 	int m_failed_reputation_increases;
 	std::pair < int, int > m_tag;
-	bool m_strcatter; 
+//	bool m_strcatter; 
 	
   // -------- End of reputation support --------
 

Modified: branches/hjg-dev/source/main/cPopulationInterface.cc
===================================================================
--- branches/hjg-dev/source/main/cPopulationInterface.cc	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/main/cPopulationInterface.cc	2008-12-23 00:21:29 UTC (rev 3072)
@@ -431,7 +431,6 @@
 	if (high_rep_orgs.size() > 0) {
 		unsigned int rand_num = m_world->GetRandom().GetUInt(0, high_rep_orgs.size()); 
 		int high_org_id = high_rep_orgs[rand_num];
-		int found = false;
 		
 	/*	const cPopulationCell* faced_cell = cell.ConnectionList().GetFirst();
 		while (!found) { 
@@ -506,7 +505,6 @@
 	if (high_rep_orgs.size() > 0) {
 		unsigned int rand_num = m_world->GetRandom().GetUInt(0, high_rep_orgs.size()); 
 		int high_org_id = high_rep_orgs[rand_num];
-		int found = false;
 		
 		for(int i=0; i<cell.ConnectionList().GetSize(); ++i) {
 			const cPopulationCell* faced_cell = cell.ConnectionList().GetFirst();

Modified: branches/hjg-dev/source/main/cStats.cc
===================================================================
--- branches/hjg-dev/source/main/cStats.cc	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/main/cStats.cc	2008-12-23 00:21:29 UTC (rev 3072)
@@ -1845,7 +1845,27 @@
 	int num_alt =0;
 	int num_coop = 0;
 	
+	// Set up the stats tracking for the strings.
+	// print the tags
+	/*map<int, int>::iterator iter;   
+	stringstream ss; 
+  for(iter = m_tags.begin(); iter != m_tags.end(); iter++ ) {
+		ss << iter->first; 
+		string name = ss.str(); 
+		df.Write(iter->second, name.c_str()); 
+		iter->second = 0;
+  }*/
+	map < int, cString> my_strings = 	m_world->GetEnvironment().GetMatchStringsFromTask();
+	map < int, cDoubleSum > tag_results; 
+	map < int, cDoubleSum >::iterator iter2; 
+
+	map< int, cString >::const_iterator iter; 
+	for (iter = my_strings.begin(); iter != my_strings.end(); iter++) { 
+		tag_results[iter->first].Add(0);
+	}
 	
+
+	
 	df.WriteComment("Avida organism reputation information -- average donations, min donations, max donations");
 	df.WriteTimeStamp();
 	df.Write(m_update,   "Update [update]");
@@ -1865,8 +1885,13 @@
 		donations.Add(org->GetNumberOfDonations());
 		num_donations_received.Add(org->GetNumberOfDonationsReceived());
 		amount_donations_received.Add(org->GetAmountOfDonationsReceived());
-		own_raw_mat.Add(org->GetSelfRawMaterials());
-		other_raw_mat.Add(org->GetOtherRawMaterials());
+//		own_raw_mat.Add(org->GetSelfRawMaterials());
+//		other_raw_mat.Add(org->GetOtherRawMaterials());
+			
+		// Update raw materials. 
+			for (iter2 = tag_results.begin(); iter2 != tag_results.end(); iter2++) { 
+				tag_results[iter2->first].Add(org->GetRawMaterialAmount(iter2->first));
+			}		
 
 		reciprocations.Add(org->GetNumberOfReciprocations());
 		donors.Add(org->GetNumberOfDonors());
@@ -1897,9 +1922,14 @@
 
 	df.Write(num_alt, "Number of altruists [altruists]");
 	df.Write(num_coop, "Number of cooperators [cooperators]");
-	df.Write(own_raw_mat.Average(), "Avg. own raw mat [ownrawmat]");
-	df.Write(other_raw_mat.Average(), "Avg. other raw mat [otherrawmat]");
+//	df.Write(own_raw_mat.Average(), "Avg. own raw mat [ownrawmat]");
+//	df.Write(other_raw_mat.Average(), "Avg. other raw mat [otherrawmat]");
+	for (iter2 = tag_results.begin(); iter2 != tag_results.end(); iter2++) { 
+		//tag_results[iter->first] += organism->GetRawMaterialAmount(iter->first);
+		df.Write(iter2->second.Average(), my_strings[iter2->first]);
+	}		
 	
+	
 //	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/cTaskLib.cc
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.cc	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/main/cTaskLib.cc	2008-12-23 00:21:29 UTC (rev 3072)
@@ -44,6 +44,7 @@
 #include <climits>
 #include <iomanip>
 #include <string>
+#include <map>
 
 // Various workarounds for Visual Studio shortcomings
 #if AVIDA_PLATFORM(WINDOWS)
@@ -356,11 +357,6 @@
     Load_MatchStr(name, info, envreqs, errors);
   else if (name == "match_number")
     Load_MatchNumber(name, info, envreqs, errors);
-	
-	if (name == "strcat") 
-    Load_StrCat(name, info, envreqs, errors);
-	else if(name == "all_strings")
-		Load_AllStrings(name, info, envreqs, errors);	
   
   if (name == "sort_inputs")
     Load_SortInputs(name, info, envreqs, errors);
@@ -427,6 +423,8 @@
   // reputation based tasks
   else if(name == "use_raw_mat")
     NewTask(name, "Use raw materials from self and other", &cTaskLib::Task_UseRawMaterials);	
+  else if(name == "multi_raw_mat")
+    NewTask(name, "Use multiple raw materials from self and other", &cTaskLib::Task_MultiRawMaterials);		
 	else if(name == "good_reputation") 
     NewTask(name, "Maintain a good reputation", &cTaskLib::Task_GoodReputation);		
   else if(name == "use_many_raw_mat")
@@ -1917,6 +1915,11 @@
   return 0.0;
 }
 
+map < int, cString > cTaskLib::GetMatchStrings()
+{
+	return m_strings;
+}
+
 void cTaskLib::Load_MatchStr(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
 {
   cArgSchema schema;
@@ -1927,108 +1930,16 @@
 	schema.AddEntry("tag",2,-1);
   cArgContainer* args = cArgContainer::Load(argstr, schema, errors);	
   envreqs.SetMinOutputs(args->GetString(0).GetSize());
-	m_strings.push_back(args->GetString(0));
+	m_strings[args->GetInt(2)] = args->GetString(0);
 //	m_world->GetStats().AddTag(args->GetInt(2), 0);
 //	m_world->GetStats().AddTag(-1, 0);
   if (args) NewTask(name, "MatchStr", &cTaskLib::Task_MatchStr, 0, args);
 }
 
-/* This loads the string cat parameters.*/
-void cTaskLib::Load_StrCat(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
-{
-  cArgSchema schema;
-	schema.AddEntry("string", 0, cArgSchema::SCHEMA_STRING);
-  schema.AddEntry("partial",0, 0);
-  schema.AddEntry("binary",1,1);
-  schema.AddEntry("pow",0,2.0);
-  cArgContainer* args = cArgContainer::Load(argstr, schema, errors);	
-  if (args) NewTask(name, "StrCat", &cTaskLib::Task_StrCat, 0, args);
-}
 
-
-vector<cString> cTaskLib::GetMatchStrings()
-{
-	return m_strings;
-}
-
-
-/* This task checks to see if an organism can create a string by catting the
- string in its buffer with each of the strings it has received as a donation. */
-double cTaskLib::Task_StrCat(cTaskContext& ctx) const
-{
-	
-	//if (ctx.GetOrganism()->GetStringCatter()==0) return 0;
-
-	tBuffer<int> temp_buf(ctx.GetOutputBuffer());
-	
-	const cString& string_to_match = ctx.GetTaskEntry()->GetArguments().GetString(0);
-  int partial = ctx.GetTaskEntry()->GetArguments().GetInt(0);
-//  int binary = ctx.GetTaskEntry()->GetArguments().GetInt(1);  
-	//int string_index;
-  //int num_matched = 0;
-  //int test_output;
-  int max_num_matched = 0;
-  int num_real=0;
-  double mypow = ctx.GetTaskEntry()->GetArguments().GetDouble(0);
-	bool used_received = false;
-
-	
-	// Given the desired string, pass it to the organism which will 
-	// check all possibilities. ?
-		
-	for (unsigned int i=0; i<m_strings.size(); i++) { 
-		max_num_matched += ctx.GetOrganism()->MatchString(m_strings[i]).first;
-	}
-	
-	
-	// Update tag
-	ctx.GetOrganism()->UpdateTag(1, (int)(max_num_matched/m_strings.size()));
-	// Update reputation
-	int rep = (max_num_matched/m_strings.size());
-	ctx.GetOrganism()->SetAverageReputation(rep);
-	
-	cString name;
-	name = "[catted]";
-	m_world->GetStats().AddStringBitsMatchedValue(name, max_num_matched);
-	
-	if (max_num_matched == string_to_match.GetSize()) m_world->GetStats().IncPerfectMatch();
-	
-	
-	double bonus = 0.0;
-  // return value between 0 & 1 representing the percentage of string that was matched
-	
-  double base_bonus = 0; 
-	//static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
-//	if (m_world->GetConfig().MATCH_PERCENT.Get() == 0) {
-		// percent over 50% (Sherri)
-		base_bonus = static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
-//	} else if (m_world->GetConfig().MATCH_PERCENT.Get() == 1) {
-		// percent (hjg)
-	//	base_bonus = static_cast<double>(max_num_matched) / static_cast<double>(string_to_match.GetSize());
-	//}
-	
-	
-  if (partial)
-  {
-    base_bonus=double(max_num_matched)*2/double(num_real) -1;
-  }
-	
-  if (base_bonus > 0.0) {
-    bonus = pow(base_bonus,mypow);
-    if (used_received)
-      m_world->GetStats().AddMarketItemUsed();
-    else
-      m_world->GetStats().AddMarketOwnItemUsed();
-  }
-  return bonus;
-}
-
-
 double cTaskLib::Task_MatchStr(cTaskContext& ctx) const
 {
-	
-	if (ctx.GetOrganism()->GetStringCatter()) return 0;
-	
+		
 	// These even out the stats tracking.
 	m_world->GetStats().AddTag(ctx.GetTaskEntry()->GetArguments().GetInt(2), 0);
 	m_world->GetStats().AddTag(-1, 0);
@@ -2092,32 +2003,12 @@
 		ctx.GetOrganism()->SetReputation(0);
 	}
  
- 
-	std::pair < int, int > best_string; 
-	int donated_string;
-
-
-		best_string = ctx.GetOrganism()->MatchString(string_to_match);
-		num_matched = best_string.first;
-		donated_string = num_matched;
-		if (num_matched > max_num_matched) {
-			max_num_matched = num_matched;
-			//		max_org_id = best_string.second;
-		}
-
-
 	double bonus = 0.0;
 	double base_bonus = 0.0; 
 
-	//if (m_world->GetConfig().MATCH_PERCENT.Get() == 0) {
-		// percent over 50% (Sherri)
-		base_bonus = static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
-//	} else if (m_world->GetConfig().MATCH_PERCENT.Get() == 1) {
-		// percent (hjg)
-	//	base_bonus = static_cast<double>(max_num_matched) / static_cast<double>(string_to_match.GetSize());
-	//}
+
+	base_bonus = static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
 	
-	
   if (partial)
   {
     base_bonus=double(max_num_matched)*2/double(num_real) -1;
@@ -2130,74 +2021,6 @@
 	
 }
 
-/* This loads the string cat parameters.*/
-void cTaskLib::Load_AllStrings(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
-{
-  cArgSchema schema;
-  schema.AddEntry("returnval",0, 0);
-  cArgContainer* args = cArgContainer::Load(argstr, schema, errors);	
-  if (args) NewTask(name, "AllStrings", &cTaskLib::Task_AllStrings, 0, args);
-}
-
-double cTaskLib::Task_AllStrings(cTaskContext& ctx) const
-{
-	// Check if the organism either created or received as a donation ; 
-	// if it misses one, then fail it. 
-	tBuffer<int> temp_buf(ctx.GetOutputBuffer());
-	int return_type = ctx.GetTaskEntry()->GetArguments().GetInt(0);
-
-	
-	cString string_to_match; // = ctx.GetTaskEntry()->GetArguments().GetString(0);
-	//  int binary = ctx.GetTaskEntry()->GetArguments().GetInt(1);  
-	//int string_index;
-  int num_matched = 0;
-  //int test_output;
-  int max_num_matched = 0;
-	double sum_matched = 0;
-  int num_real=0;
-	double bonus = 0; 
-	double total_size = 0;
-	
-	
-	// Given the desired string, pass it to the organism which will 
-	// check all possibilities. ?
-	for (unsigned int i=0; i<m_strings.size(); i++) { 
-		num_matched = 0;
-		string_to_match = m_strings[i];
-		// Check if the organism created the string. 
-		for (int j = 0; j < string_to_match.GetSize(); j++)
-    {
-			if (string_to_match[j]!='9')
-				num_real++;
-			if (string_to_match[j]=='0' && temp_buf[j]==0 ||
-					string_to_match[j]=='1' && temp_buf[j]==1)
-				num_matched++;
-    }
-		
-		max_num_matched = num_matched; 
-		
-		num_matched = ctx.GetOrganism()->MatchString(m_strings[i]).first;
-		if (num_matched > max_num_matched) max_num_matched = num_matched; 
-		
-		sum_matched += max_num_matched; 
-		total_size += string_to_match.GetSize();
-	}
-	
-	
-	if (sum_matched == total_size) {
-		m_world->GetStats().IncPerfectMatch();
-	}
-
-	if (return_type) { 
-		bonus = sum_matched/total_size;
-	} else if (sum_matched == total_size) {
-		bonus = 1;
-	}
-	
-  return bonus;
-}
-
-
 void cTaskLib::Load_MatchNumber(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
 {
   cArgSchema schema;
@@ -3365,6 +3188,40 @@
 	return bonus;
 }
 
+double cTaskLib::Task_MultiRawMaterials(cTaskContext& ctx) const { 
+	double bonus = 0.0;
+	double other_mult_val = m_world->GetConfig().ALT_BENEFIT.Get();
+	double my_mult_val = m_world->GetConfig().ALT_COST.Get();
+	int org_tag = ctx.GetOrganism()->GetTagLabel();
+	
+	// reward for own stuff
+//	bonus += (my_mult_val * ctx.GetOrganism()->GetSelfRawMaterials()); 
+//	bonus += (other_mult_val * ctx.GetOrganism()->GetOtherRawMaterials()); 
+/*(	map<int, int>::iterator iter;   
+	stringstream ss; 
+  for(iter = m_tags.begin(); iter != m_tags.end(); iter++ ) {
+		ss << iter->first; 
+		string name = ss.str(); 
+		df.Write(iter->second, name.c_str()); 
+		iter->second = 0;
+  }*/
+	
+	map< int, cString >::const_iterator iter; 
+	for (iter = m_strings.begin(); iter != m_strings.end(); iter++) { 
+		if (iter->first == org_tag) { 
+			bonus += (my_mult_val * ctx.GetOrganism()->GetRawMaterialAmount(iter->first)); 
+		} else {
+			bonus += (other_mult_val * ctx.GetOrganism()->GetRawMaterialAmount(iter->first)); 
+		}
+		
+	}
+	
+	
+	return bonus;
+	
+	
+}
+
 /* Reward organisms for having a good reputation */
 double cTaskLib::Task_GoodReputation(cTaskContext& ctx) const { 
 //	double bonus = 0.0; 

Modified: branches/hjg-dev/source/main/cTaskLib.h
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.h	2008-12-22 18:41:21 UTC (rev 3071)
+++ branches/hjg-dev/source/main/cTaskLib.h	2008-12-23 00:21:29 UTC (rev 3072)
@@ -88,11 +88,11 @@
   bool UseNeighborOutput() const { return use_neighbor_output; }
 	
 	// Get the strings that parameterize the MatchString tasks
-	vector<cString> GetMatchStrings(); 
+	map < int, cString > GetMatchStrings(); 
 
 private: 
 	// Store the strings used by the MatchString tasks
-	vector<cString> m_strings; 
+	map < int, cString >  m_strings; 
   
   
 private:  // Direct task related methods
@@ -250,11 +250,6 @@
   double Task_MatchStr(cTaskContext& ctx) const;
   void Load_MatchNumber(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
   double Task_MatchNumber(cTaskContext& ctx) const;
-	void Load_StrCat(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
-  double Task_StrCat(cTaskContext& ctx) const;	
-	void Load_AllStrings(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
-  double Task_AllStrings(cTaskContext& ctx) const;	
-
 	
   void Load_SortInputs(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
   double Task_SortInputs(cTaskContext& ctx) const;
@@ -312,8 +307,7 @@
   // reputation
   double Task_UseRawMaterials(cTaskContext& ctx) const;
   double Task_SaveRawMaterials(cTaskContext& ctx) const;	
-	double Task_SaveRawMaterials1(cTaskContext& ctx) const;	
-  double Task_SaveRawMaterials2(cTaskContext& ctx) const;	
+  double Task_MultiRawMaterials(cTaskContext& ctx) const;	
 
 	double Task_GoodReputation(cTaskContext& ctx) const;
   double Task_ConsumeRawMaterials(cTaskContext& ctx) const;	




More information about the Avida-cvs mailing list