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

hjg at myxo.css.msu.edu hjg at myxo.css.msu.edu
Sat Oct 18 08:00:57 PDT 2008


Author: hjg
Date: 2008-10-18 11:00:57 -0400 (Sat, 18 Oct 2008)
New Revision: 2854

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
   branches/hjg-dev/source/main/cTaskLib.h
Log:
added a consume raw materials task.

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-10-17 20:38:02 UTC (rev 2853)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-10-18 15:00:57 UTC (rev 2854)
@@ -457,13 +457,13 @@
     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),
+    tInstLibEntry<tMethod>("get-other-raw-mat-amount", &cHardwareCPU::Inst_GetAmountOfOtherRawMaterials, nInstFlag::STALL),
     tInstLibEntry<tMethod>("donation-scam", &cHardwareCPU::Inst_DonationScam, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("punish-neighbor", &cHardwareCPU::Inst_PunishNeighbor, nInstFlag::STALL),
     tInstLibEntry<tMethod>("praise-neighbor", &cHardwareCPU::Inst_PraiseNeighbor, nInstFlag::STALL),
     tInstLibEntry<tMethod>("inc-recip", &cHardwareCPU::Inst_IncRecipProb, nInstFlag::STALL),
     tInstLibEntry<tMethod>("dec-recip", &cHardwareCPU::Inst_DecRecipProb, nInstFlag::STALL),
 		tInstLibEntry<tMethod>("rotate-to-rep", &cHardwareCPU::Inst_RotateToGreatestReputation, nInstFlag::STALL),
-	
 		
 
     // Must always be the last instruction in the array
@@ -6849,6 +6849,14 @@
 }
 
 
+bool cHardwareCPU::Inst_GetAmountOfOtherRawMaterials(cAvidaContext& ctx)
+{
+	const int raw_mat_reg = FindModifiedRegister(REG_AX);
+	GetRegister(raw_mat_reg) = organism->GetOtherRawMaterials();
+	return true;
+}
+
+
 /*  Punish Neighbor - decrease reputation of neighbor */
 bool cHardwareCPU::Inst_PunishNeighbor(cAvidaContext& ctx) 
 {

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-10-17 20:38:02 UTC (rev 2853)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-10-18 15:00:57 UTC (rev 2854)
@@ -686,6 +686,9 @@
   bool Inst_GetReputation(cAvidaContext& ctx);
   // Get the organism's raw material level
   bool Inst_GetAmountOfRawMaterials(cAvidaContext& ctx);
+  // Get the number of raw materials the organism 
+	// has gotten from others
+  bool Inst_GetAmountOfOtherRawMaterials(cAvidaContext& ctx);	
   // Pretend to donate
   bool Inst_DonationScam(cAvidaContext& ctx);
 	// Punish Neighbor - increase reputation of neighbor

Modified: branches/hjg-dev/source/main/cAvidaConfig.h
===================================================================
--- branches/hjg-dev/source/main/cAvidaConfig.h	2008-10-17 20:38:02 UTC (rev 2853)
+++ branches/hjg-dev/source/main/cAvidaConfig.h	2008-10-18 15:00:57 UTC (rev 2854)
@@ -559,6 +559,7 @@
   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, bool, 1, "Is an organism's reputation automatically computed based on its donations");
+  CONFIG_ADD_VAR(OTHER_RAW_MAT_MULT, double, 2.00, "Number multiplied by the number of raw materials received from another organism to compute reward");
 
   
 #endif

Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc	2008-10-17 20:38:02 UTC (rev 2853)
+++ branches/hjg-dev/source/main/cOrganism.cc	2008-10-18 15:00:57 UTC (rev 2854)
@@ -751,13 +751,14 @@
 
 
 
-/* Called when raw materials are donated to others. Amount is the number of resources donated. 
-The boolean flag is used to indicate if the donation was successful... It would fail if the
-organism did not have that many resources. */
+/* Called when raw materials are donated to others or when the 
+	raw materials are consumed. Amount is the number of resources 
+	donated. The boolean flag is used to indicate if the donation 
+	was successful... It would fail if the organism did not have 
+	that many resources. */
 bool cOrganism::SubtractSelfRawMaterials (int amount)
 {
 	bool isSuccessful = false;
-//	InitReputation();
 	if (amount <= m_self_raw_materials) { 
 		isSuccessful = true; 
 		m_self_raw_materials -= amount;
@@ -765,22 +766,38 @@
 	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... */
 
+/* Called when other raw materials are consumed. Amount is the 
+	number of resources consumed. The boolean flag is used to 
+	indicate if the donation was successful... It would fail if 
+	the organism did not have that many resources. */
+bool cOrganism::SubtractOtherRawMaterials (int amount)
+{
+	bool isSuccessful = false;
+	if (amount <= m_other_raw_materials) { 
+		isSuccessful = true; 
+		m_other_raw_materials -= amount;
+	}
+	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... */
+
 bool cOrganism::AddOtherRawMaterials (int amount, int donor_id) {
 	bool isSuccessful = true;
-//	InitReputation();
 	m_other_raw_materials += amount;
 	donor_list.insert(donor_id);	
 	return isSuccessful;
 }
 
-// Get an organism's reputation
+/* Get an organism's reputation, which is expressed as an 
+	opinion. 5 is the default reputation (this should be refactored
+	to be cleaner). */
 int cOrganism::GetReputation() {
 	int rep =5;
-	
 	if (HasOpinion()) {
 		rep = GetOpinion().first;
 	}
@@ -788,27 +805,36 @@
 }
 
 
-// Increase an organism's probability of donating
-// by 10%
+/* Increase an organism's probability of donating by 10% 
+	Essentially, the reputation can vary from 0-10, where 0
+	indicates the organism never reciprocates and 10 indicates
+	the organism always reciprocates.  */
 void cOrganism::IncRecipProb() {
 	if (m_recip_prob < 10) { 
 		m_recip_prob++;
 		
 		if (m_recip_prob > m_max_recip_prob) m_max_recip_prob = m_recip_prob;
+		
+		// If reputation is automatic and should be tied to the
+		// reciprocity probability, then update the organism's opinion.
 		if (m_world->GetConfig().AUTO_REPUTATION.Get()) {
 			SetOpinion(m_recip_prob);
 		}
 	}
 }
 
-// decrease an organism's probaility of donating
-// by 10%
+/* Decrease an organism's probability of donating by 10% 
+	Essentially, the reputation can vary from 0-10, where 0
+	indicates the organism never reciprocates and 10 indicates
+	the organism always reciprocates.  */
 void cOrganism::DecRecipProb() { 
 	if (m_recip_prob > 0) { 
 		m_recip_prob--;
 		
 		if (m_recip_prob < m_min_recip_prob) m_min_recip_prob = m_recip_prob;
 
+		// If reputation is automatic and should be tied to the
+		// reciprocity probability, then update the organism's opinion.
 		if (m_world->GetConfig().AUTO_REPUTATION.Get()) {
 			SetOpinion(m_recip_prob);
 		}

Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h	2008-10-17 20:38:02 UTC (rev 2853)
+++ branches/hjg-dev/source/main/cOrganism.h	2008-10-18 15:00:57 UTC (rev 2854)
@@ -444,8 +444,10 @@
   */
   
 public: 
-	// donate raw materials to others
+	// Deduct amount number of self raw materials
 	bool SubtractSelfRawMaterials(int amount); 
+	// Deduct amount number of other raw materials
+	bool SubtractOtherRawMaterials(int amount); 
 	// receive raw materials from others
 	bool AddOtherRawMaterials(int amount, int donor_id);
 	// retrieve the organism's own amount of raw materials

Modified: branches/hjg-dev/source/main/cTaskLib.cc
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.cc	2008-10-17 20:38:02 UTC (rev 2853)
+++ branches/hjg-dev/source/main/cTaskLib.cc	2008-10-18 15:00:57 UTC (rev 2854)
@@ -422,8 +422,11 @@
     NewTask(name, "Use raw materials from self and other", &cTaskLib::Task_UseRawMaterials);
   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);			
 
 
+
   // event tasks
   if(name == "move_to_event")
     NewTask(name, "Moved into cell containing event", &cTaskLib::Task_MoveToEvent);
@@ -3068,26 +3071,59 @@
 
 // For reputation-based work. See cOrganism.
 /* This task just checks that an organism has raw materials and that it has 
-received a donation of raw materials from another organism.*/
+	received a donation of raw materials from another organism.*/
 double cTaskLib::Task_UseRawMaterials(cTaskContext& ctx) const {
 	double bonus = 0.0;
-	if ((ctx.GetOrganism()->GetSelfRawMaterials() > 1) && 
-		(ctx.GetOrganism()->GetOtherRawMaterials() > 1)) {
+	if ((ctx.GetOrganism()->GetSelfRawMaterials() > 0) && 
+		(ctx.GetOrganism()->GetOtherRawMaterials() > 0)) {
 		 bonus = ctx.GetOrganism()->GetOtherRawMaterials(); 
 	}
 	return bonus;
 }
 
-// For reputation-based work. See cOrganism.
 /* 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.*/
+	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() > 1) && 
-		(ctx.GetOrganism()->GetNumberOfDonors() > 1)) {
+	if ((ctx.GetOrganism()->GetSelfRawMaterials() > 0) && 
+		(ctx.GetOrganism()->GetNumberOfDonors() > 0)) {
 		 bonus = ctx.GetOrganism()->GetNumberOfDonors(); 
 	}
 	return bonus;
 }
+
+
+/* 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/
+	reciprocations, then these are also consumed, but are worth more. 
+	For an organism to receive any reward, it MUST have at least
+	one of its own raw materials.
+*/
+double cTaskLib::Task_ConsumeRawMaterials(cTaskContext& ctx) const {
+	double bonus = 0.0;
+	double mult_val = m_world->GetConfig().OTHER_RAW_MAT_MULT.Get();
+	int num_mat =0;
+
+	if (ctx.GetOrganism()->GetSelfRawMaterials() > 0) {
+			ctx.GetOrganism()->SubtractSelfRawMaterials(1);
+			bonus = 1.0;
+			
+			num_mat = ctx.GetOrganism()->GetOtherRawMaterials(); 
+		if (num_mat > 0) { 
+			// subtract the raw materials
+			ctx.GetOrganism()->SubtractOtherRawMaterials(num_mat);
+			// multiply number by config param & add to bonus
+			bonus += (mult_val * num_mat);
+		}
+	}
+	
+	return bonus;
+}
+
+
+
+
+

Modified: branches/hjg-dev/source/main/cTaskLib.h
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.h	2008-10-17 20:38:02 UTC (rev 2853)
+++ branches/hjg-dev/source/main/cTaskLib.h	2008-10-18 15:00:57 UTC (rev 2854)
@@ -299,6 +299,7 @@
   
   // reputation
   double Task_UseRawMaterials(cTaskContext& ctx) const;
+  double Task_ConsumeRawMaterials(cTaskContext& ctx) const;	
   double Task_UseManyOrgsRawMaterials(cTaskContext& ctx) const;
 
 };




More information about the Avida-cvs mailing list