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

hjg at myxo.css.msu.edu hjg at myxo.css.msu.edu
Tue Dec 30 08:01:19 PST 2008


Author: hjg
Date: 2008-12-30 11:01:19 -0500 (Tue, 30 Dec 2008)
New Revision: 3080

Modified:
   branches/hjg-dev/source/cpu/cHardwareCPU.cc
   branches/hjg-dev/source/cpu/cHardwareCPU.h
   branches/hjg-dev/source/main/cTaskLib.cc
   branches/hjg-dev/source/main/cTaskLib.h
Log:
Cleaned up string matching code in cTaskLib and cHardwareCPU

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-12-30 15:18:43 UTC (rev 3079)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-12-30 16:01:19 UTC (rev 3080)
@@ -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
@@ -7372,126 +7368,10 @@
  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. */

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-12-30 15:18:43 UTC (rev 3079)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-12-30 16:01:19 UTC (rev 3080)
@@ -695,10 +695,7 @@
 	bool Inst_DonateStringIfDonorRep(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);	
@@ -718,8 +715,6 @@
   bool Inst_GetReputation(cAvidaContext& ctx);	
 	// 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);
 	// Produce string
 	bool Inst_ProduceString(cAvidaContext& ctx);
 	

Modified: branches/hjg-dev/source/main/cTaskLib.cc
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.cc	2008-12-30 15:18:43 UTC (rev 3079)
+++ branches/hjg-dev/source/main/cTaskLib.cc	2008-12-30 16:01:19 UTC (rev 3080)
@@ -356,11 +356,7 @@
     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);
@@ -429,16 +425,13 @@
     NewTask(name, "Use raw materials from self and other", &cTaskLib::Task_UseRawMaterials);	
 	else if(name == "good_reputation") 
     NewTask(name, "Maintain a good reputation", &cTaskLib::Task_GoodReputation);		
-  else if(name == "use_many_raw_mat")
-    NewTask(name, "Use raw materials from self and other", &cTaskLib::Task_UseManyOrgsRawMaterials);		
   else if(name == "consume_raw_mat")
     NewTask(name, "Consume raw materials", &cTaskLib::Task_ConsumeRawMaterials);			
   else if(name == "save_raw_mat")
     NewTask(name, "Save raw materials", &cTaskLib::Task_SaveRawMaterials);	
-	else if (name == "lineage")
-    Load_LineageReward(name, info, envreqs, errors);
 
 
+
   // event tasks
   if(name == "move_to_event")
     NewTask(name, "Moved into cell containing event", &cTaskLib::Task_MoveToEvent);
@@ -1917,35 +1910,8 @@
   return 0.0;
 }
 
-void cTaskLib::Load_MatchStr(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);
-	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_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;
@@ -1965,75 +1931,18 @@
 }
 
 
-/* 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
+void cTaskLib::Load_MatchStr(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
 {
-	
-	//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;
+  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);
+	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));
+  if (args) NewTask(name, "MatchStr", &cTaskLib::Task_MatchStr, 0, args);
 }
 
 
@@ -2143,74 +2052,7 @@
 	
 }
 
-/* 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;
@@ -3326,18 +3168,6 @@
 }
 
 
-/* This task just checks that an organism has raw materials and that it has 
-	received a donation of raw materials from another organism. Specifically, 
-	the task rewards in proportion to the number of organisms that have
-	donated to it.*/
-double cTaskLib::Task_UseManyOrgsRawMaterials(cTaskContext& ctx) const {
-	double bonus = 0.0;
-	if ((ctx.GetOrganism()->GetSelfRawMaterials() > 0) && 
-		(ctx.GetOrganism()->GetNumberOfDonors() > 0)) {
-		 bonus = ctx.GetOrganism()->GetNumberOfDonors(); 
-	}
-	return bonus;
-}
 
 /* This task rewards organisms proportional to the raw materials
 	that it has accumulated.*/
@@ -3387,22 +3217,3 @@
 }
 
 
-/* Reward an organism for having a specific lineage */
-void cTaskLib::Load_LineageReward(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
-{
-  cArgSchema schema;
-  schema.AddEntry("lineage",0, -1);
-  cArgContainer* args = cArgContainer::Load(argstr, schema, errors);	
-  if (args) NewTask(name, "LineageReward", &cTaskLib::Task_LineageReward, 0, args);
-}
-
-double cTaskLib::Task_LineageReward(cTaskContext& ctx) const
-{
-	double bonus = 0.0;
-  int lineage = ctx.GetTaskEntry()->GetArguments().GetInt(0);
-	if (ctx.GetOrganism()->GetLineageLabel() == lineage) { 
-		bonus = 1.0;
-	}
-	return bonus;
-}
-

Modified: branches/hjg-dev/source/main/cTaskLib.h
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.h	2008-12-30 15:18:43 UTC (rev 3079)
+++ branches/hjg-dev/source/main/cTaskLib.h	2008-12-30 16:01:19 UTC (rev 3080)
@@ -252,11 +252,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,16 +307,13 @@
   double Task_EventKilled(cTaskContext& ctx) const;
   
   // 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_GoodReputation(cTaskContext& ctx) const;
   double Task_ConsumeRawMaterials(cTaskContext& ctx) const;	
-  double Task_UseManyOrgsRawMaterials(cTaskContext& ctx) const;
-	void Load_LineageReward(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
-  double Task_LineageReward(cTaskContext& ctx) const;	
+  double Task_UseRawMaterials(cTaskContext& ctx) const;
+  double Task_SaveRawMaterials(cTaskContext& ctx) const;
+		
 
 };
 




More information about the Avida-cvs mailing list