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

hjg at myxo.css.msu.edu hjg at myxo.css.msu.edu
Wed Nov 5 18:43:54 PST 2008


Author: hjg
Date: 2008-11-05 21:43:54 -0500 (Wed, 05 Nov 2008)
New Revision: 2919

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/cStats.cc
   branches/hjg-dev/source/main/cTaskLib.cc
   branches/hjg-dev/source/main/cTaskLib.h
Log:
change reputation donations to use one raw material.

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-11-05 14:29:35 UTC (rev 2918)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-11-06 02:43:54 UTC (rev 2919)
@@ -452,6 +452,8 @@
     tInstLibEntry<tMethod>("get-neighbors-opinion", &cHardwareCPU::Inst_GetNeighborsOpinion, nInstFlag::STALL),
 	
 	// Reputation instructions
+	
+		tInstLibEntry<tMethod>("donate-mat", &cHardwareCPU::Inst_DonateFacingRawMaterialsOneType, nInstFlag::STALL),
   	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),		
@@ -6822,8 +6824,87 @@
   return true;
 }  
 
+/* Donate raw materials to the facing organism. One type of raw material*/
+bool cHardwareCPU::Inst_DonateFacingRawMaterialsOneType(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();
+	
+	// sometimes the donation will fail. 
+	// get the probability of failure
+	int prob_fail = m_world->GetConfig().DONATION_FAILURE_PERCENT.Get(); 
+	unsigned int rand_num = m_world->GetRandom().GetUInt(0, 100); 
+	// neighbor donates to organism.
+	if (rand_num < prob_fail) { 
+		// EXIT
+		return true; 
+	}
 
+	// Subtract raw materials from the organism (currently subtracts 1 resource...)
+	// fails if the organism does not have any more resources
+	int cost = m_world->GetConfig().ALT_COST.Get(); 
+	int benefit = m_world->GetConfig().ALT_BENEFIT.Get();
+	if (organism->SubtractSelfRawMaterials(cost)) {
+	
+		neighbor->AddRawMaterials(benefit, organism->GetID()); 
+		
+		// rotate recipient to face donor 
+		// by rotating until the recipient faces the donor
+		// adding a new comment.
+		if (m_world->GetConfig().ROTATE_ON_DONATE.Get()) {
+			while (neighbor->GetNeighbor() != organism) {
+				neighbor->Rotate(1);
+			}
+		}
+		
 
+		// track stats
+		organism->Donated();
+		
+		// update reputation to include this donation.
+		// get the current reputation; increment by 1.
+		// includes a concept of standing
+		if (m_world->GetConfig().AUTO_REPUTATION.Get() == 1) {
+			int my_rep = organism->GetReputation();
+			organism->SetReputation(my_rep +1);
+			// get neighbor reputation
+			int rep = neighbor->GetReputation(); 
+			// if the organism has not yet donated, put it into bad standing
+			if (rep == 0) neighbor->SetReputation(-1);
+		} else if (m_world->GetConfig().AUTO_REPUTATION.Get() == 2) {
+			// reputation is proportional to how much you have donated/received
+			int my_rep = organism->GetReputation();
+			organism->SetReputation(my_rep +1);
+			// get neighbor reputation
+			int rep = neighbor->GetReputation(); 
+			neighbor->SetReputation(rep-1);
+		} else if (m_world->GetConfig().AUTO_REPUTATION.Get() == 3)  {
+			// set rep to 1, since the organism donated
+			organism->SetReputation(1);
+			// get neighbor reputation
+			int rep = neighbor->GetReputation(); 
+			// if the organism has not yet donated, put it into bad standing
+			if (rep == 0) neighbor->SetReputation(-1);		
+		} else if (m_world->GetConfig().AUTO_REPUTATION.Get() == 4) {
+			// Similar to 1, except does not include standing.
+			int my_rep = organism->GetReputation();
+			organism->SetReputation(my_rep +1);
+		}
+		
+		}
+  }
+  return true;
+}  
+
+
+
 /* Donate raw materials to the facing organism that with a given 
 probability (set as reputation) will reciprocate. */
 bool cHardwareCPU::Inst_DonateProbReciprocate(cAvidaContext& ctx)

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-11-05 14:29:35 UTC (rev 2918)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-11-06 02:43:54 UTC (rev 2919)
@@ -678,6 +678,8 @@
   */ 
   // Donate a raw material to the neighbor
   bool Inst_DonateFacingRawMaterials(cAvidaContext& ctx);
+  // Donate a raw material to the neighbor -- one type of raw material
+  bool Inst_DonateFacingRawMaterialsOneType(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

Modified: branches/hjg-dev/source/main/cAvidaConfig.h
===================================================================
--- branches/hjg-dev/source/main/cAvidaConfig.h	2008-11-05 14:29:35 UTC (rev 2918)
+++ branches/hjg-dev/source/main/cAvidaConfig.h	2008-11-06 02:43:54 UTC (rev 2919)
@@ -559,8 +559,8 @@
   CONFIG_ADD_CUSTOM_FORMAT(REPUTATION_GROUP, "Reputation Settings");
   CONFIG_ADD_VAR(RAW_MATERIAL_AMOUNT, int, 100, "Number of raw materials an organism starts with");
   CONFIG_ADD_VAR(AUTO_REPUTATION, int, 1, "Is an organism's reputation automatically computed based on its donations");
-  CONFIG_ADD_VAR(OTHER_RAW_MAT_MULT, double, 1.00, "Number multiplied by the number of raw materials received from another organism to compute reward");
-  CONFIG_ADD_VAR(MY_RAW_MAT_MULT, double, 1.00, "Number multiplied by the number of your raw materials");
+  CONFIG_ADD_VAR(ALT_BENEFIT, int, 1.00, "Number multiplied by the number of raw materials received from another organism to compute reward");
+  CONFIG_ADD_VAR(ALT_COST, int, 1.00, "Number multiplied by the number of your raw materials");
   CONFIG_ADD_VAR(ROTATE_ON_DONATE, int, 0, "Rotate an organism to face its donor 0/1 (off/on)");
   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");

Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc	2008-11-05 14:29:35 UTC (rev 2918)
+++ branches/hjg-dev/source/main/cOrganism.cc	2008-11-06 02:43:54 UTC (rev 2919)
@@ -83,6 +83,8 @@
 	, m_num_reciprocate(0)
 	, m_max_recip_prob(5)
 	, m_min_recip_prob(5)
+	, m_num_donate_received(0)
+	, m_amount_donate_received(0)
 	, m_k(0)
 {
   m_hardware = m_world->GetHardwareManager().Create(this);
@@ -798,10 +800,31 @@
 bool cOrganism::AddOtherRawMaterials (int amount, int donor_id) {
 	bool isSuccessful = true;
 	m_other_raw_materials += amount;
+	donor_list.insert(donor_id);
+	m_num_donate_received += amount;	
+	m_amount_donate_received++;	
+	return isSuccessful;
+}
+
+/* Called when raw materials are received from others. Amount 
+	is the number of resources received. The boolean flag is used 
+	to indicate if the reception was successful, which should always
+	be the case... 
+	
+	This version is used if there is only one resource that is both
+	donated and recieved.
+*/
+
+bool cOrganism::AddRawMaterials (int amount, int donor_id) {
+	bool isSuccessful = true;
+	m_self_raw_materials += amount;
 	donor_list.insert(donor_id);	
+	m_num_donate_received += amount;
+	m_amount_donate_received++;
 	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). */

Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h	2008-11-05 14:29:35 UTC (rev 2918)
+++ branches/hjg-dev/source/main/cOrganism.h	2008-11-06 02:43:54 UTC (rev 2919)
@@ -450,6 +450,8 @@
 	bool SubtractOtherRawMaterials(int amount); 
 	// receive raw materials from others
 	bool AddOtherRawMaterials(int amount, int donor_id);
+	// receive raw materials 
+	bool AddRawMaterials(int amount, int donor_id);
 	// retrieve the organism's own amount of raw materials
 	int GetSelfRawMaterials() { return m_self_raw_materials; }
 	// retrieve the amount of raw materials collected from others
@@ -470,6 +472,10 @@
 	void Donated(){m_num_donate++;}
 	// get number of donations
 	int GetNumberOfDonations() { return m_num_donate; }
+	// get number of donations received
+	int GetNumberOfDonationsReceived() { return m_num_donate_received; }
+	// get amout of donations received
+	int GetAmountOfDonationsReceived() { return m_amount_donate_received; }
 	// organism reciprocated
 	void Reciprocated() {m_num_reciprocate++;}
 	// get number of reciprocations
@@ -484,7 +490,9 @@
 	void DecK() { m_k--; }
 	// was the organism a donor
 	bool IsDonor(int neighbor_id); 
+	// get amount of donations received
 	
+	
 protected:
 	// The organism's own raw materials
 	int m_self_raw_materials; 
@@ -496,6 +504,10 @@
 	int m_recip_prob;
 	// number of donations
 	int m_num_donate;
+	// number of donations received
+	int m_num_donate_received;
+	// amount of donations received
+	int m_amount_donate_received;
 	// number of reciprocations
 	int m_num_reciprocate;
 	// max-reciprocation probability

Modified: branches/hjg-dev/source/main/cStats.cc
===================================================================
--- branches/hjg-dev/source/main/cStats.cc	2008-11-05 14:29:35 UTC (rev 2918)
+++ branches/hjg-dev/source/main/cStats.cc	2008-11-06 02:43:54 UTC (rev 2919)
@@ -1728,6 +1728,8 @@
 	cDoubleSum donors;
 	cDoubleSum recip_prob_change;
 	cDoubleSum k;
+	cDoubleSum num_donations_received; 
+	cDoubleSum amount_donations_received;
 
 	// difference between how many an organism donated & how many it received
 	cDoubleSum disparity;
@@ -1757,6 +1759,8 @@
 		if (max_rep < cur_rep) max_rep = cur_rep;
 		reputations.Add(cur_rep);
 		donations.Add(org->GetNumberOfDonations());
+		num_donations_received.Add(org->GetNumberOfDonationsReceived());
+		amount_donations_received.Add(org->GetAmountOfDonationsReceived());
 		reciprocations.Add(org->GetNumberOfReciprocations());
 		donors.Add(org->GetNumberOfDonors());
 		recip_prob_change.Add(org->GetChangeInRecipProb());
@@ -1776,6 +1780,8 @@
 //	df.Write(min_rep, "Minimum reputation");
 //	df.Write(max_rep, "Maximum reputation");	
 	df.Write(donations.Average(), "Avg. donations [donation]");
+	df.Write(num_donations_received.Average(), "Avg. donations received [received]");
+	df.Write(amount_donations_received.Average(), "Avg. number donations received [amount]");
 //	df.Write(reciprocations.Average(), "Avg. reciprocations [reciprocation]");
 	df.Write(disparity.Average(), "Disparity between donations and collections [disparity]");
 	df.Write(donors.Average(), "Avg. number of donor partners [partners]");

Modified: branches/hjg-dev/source/main/cTaskLib.cc
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.cc	2008-11-05 14:29:35 UTC (rev 2918)
+++ branches/hjg-dev/source/main/cTaskLib.cc	2008-11-06 02:43:54 UTC (rev 2919)
@@ -3077,8 +3077,8 @@
 	others. */
 double cTaskLib::Task_UseRawMaterials(cTaskContext& ctx) const {
 	double bonus = 0.0;
-	double other_mult_val = m_world->GetConfig().OTHER_RAW_MAT_MULT.Get();
-	double my_mult_val = m_world->GetConfig().MY_RAW_MAT_MULT.Get();
+	double other_mult_val = m_world->GetConfig().ALT_BENEFIT.Get();
+	double my_mult_val = m_world->GetConfig().ALT_COST.Get();
 	
 	//if ((ctx.GetOrganism()->GetSelfRawMaterials() > 0) && 
 		//(ctx.GetOrganism()->GetOtherRawMaterials() > 0)) {
@@ -3105,7 +3105,14 @@
 	return bonus;
 }
 
+/* This task rewards organisms proportional to the raw materials
+	that it has accumulated.*/
+double cTaskLib::Task_SaveRawMaterials(cTaskContext& ctx) const {
+	return ctx.GetOrganism()->GetSelfRawMaterials(); 
+}
 
+
+
 /* This task consumes an organism's raw materials. Specifically, 
 	if an organism has one of their own available it consumes it. 
 	If an organism has raw materials it has received in donations/
@@ -3115,7 +3122,7 @@
 */
 double cTaskLib::Task_ConsumeRawMaterials(cTaskContext& ctx) const {
 	double bonus = 0.0;
-	double mult_val = m_world->GetConfig().OTHER_RAW_MAT_MULT.Get();
+	double mult_val = m_world->GetConfig().ALT_COST.Get();
 	int num_mat =0;
 
 	if (ctx.GetOrganism()->GetSelfRawMaterials() > 0) {

Modified: branches/hjg-dev/source/main/cTaskLib.h
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.h	2008-11-05 14:29:35 UTC (rev 2918)
+++ branches/hjg-dev/source/main/cTaskLib.h	2008-11-06 02:43:54 UTC (rev 2919)
@@ -299,6 +299,7 @@
   
   // reputation
   double Task_UseRawMaterials(cTaskContext& ctx) const;
+  double Task_SaveRawMaterials(cTaskContext& ctx) const;	
 	double Task_GoodReputation(cTaskContext& ctx) const;
   double Task_ConsumeRawMaterials(cTaskContext& ctx) const;	
   double Task_UseManyOrgsRawMaterials(cTaskContext& ctx) const;




More information about the Avida-cvs mailing list