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

hjg at myxo.css.msu.edu hjg at myxo.css.msu.edu
Sat Sep 27 22:44:30 PDT 2008


Author: hjg
Date: 2008-09-28 01:44:26 -0400 (Sun, 28 Sep 2008)
New Revision: 2800

Modified:
   branches/hjg-dev/source/cpu/cHardwareCPU.cc
   branches/hjg-dev/source/cpu/cHardwareCPU.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:
super basic reputation-based instructions and task.

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-09-28 02:08:10 UTC (rev 2799)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.cc	2008-09-28 05:44:26 UTC (rev 2800)
@@ -449,7 +449,11 @@
     // These are STALLs because opinions are only relevant with respect to time.
     tInstLibEntry<tMethod>("set-opinion", &cHardwareCPU::Inst_SetOpinion, nInstFlag::STALL),
     tInstLibEntry<tMethod>("get-opinion", &cHardwareCPU::Inst_GetOpinion, nInstFlag::STALL),
+	
+	// Reputation instructions
+	tInstLibEntry<tMethod>("donate-frm", &cHardwareCPU::Inst_DonateFacingRawMaterials, nInstFlag::STALL),
 
+
     // Must always be the last instruction in the array
     tInstLibEntry<tMethod>("NULL", &cHardwareCPU::Inst_Nop, 0, "True no-operation instruction: does nothing"),
   };
@@ -6665,3 +6669,29 @@
   }
   return true;
 }
+
+
+/* Donate raw materials to the facing organism. */
+bool cHardwareCPU::Inst_DonateFacingRawMaterials(cAvidaContext& ctx)
+{
+  // hjg -- why would this be necessary?
+  /*if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
+    return false;
+  }*/
+  
+  bool isSuccessful = false;
+  
+  // Get faced neighbor
+  cOrganism * neighbor = organism->GetNeighbor();
+  
+  // Donate only if we have found a neighbor.
+  if (neighbor != NULL) {
+    // Subtract raw materials from the organism (currently subtracts 1 resource...)
+	// fails if the organism does not have any more resources
+	if (organism->SubtractSelfRawMaterials(1)) {
+		neighbor->AddOtherRawMaterials(1); 
+	}
+	isSuccessful = true;
+  }
+  return isSuccessful;
+}  

Modified: branches/hjg-dev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-09-28 02:08:10 UTC (rev 2799)
+++ branches/hjg-dev/source/cpu/cHardwareCPU.h	2008-09-28 05:44:26 UTC (rev 2800)
@@ -665,6 +665,16 @@
   bool Inst_SetOpinion(cAvidaContext& ctx);
   //! Retrieve this organism's current opinion.
   bool Inst_GetOpinion(cAvidaContext& ctx);
+  
+  
+  // -------- Reputation support --------
+  /* These instructions interact with the "reputation" support in cOrganism.h.  They 
+  are  based on the donate instructions. However, these instructions donate
+  "raw materials" rather than merit and will eventually be used to support 
+  reputation based cooperation.
+  */ 
+  bool Inst_DonateFacingRawMaterials(cAvidaContext& ctx);
+
 };
 
 

Modified: branches/hjg-dev/source/main/cOrganism.cc
===================================================================
--- branches/hjg-dev/source/main/cOrganism.cc	2008-09-28 02:08:10 UTC (rev 2799)
+++ branches/hjg-dev/source/main/cOrganism.cc	2008-09-28 05:44:26 UTC (rev 2800)
@@ -740,3 +740,30 @@
   InitOpinions();
   m_opinion->opinion_list.push_back(std::make_pair(opinion, m_world->GetStats().GetUpdate()));
 }
+
+
+
+/* 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. */
+bool cOrganism::SubtractSelfRawMaterials (int amount)
+{
+	bool isSuccessful = false;
+	InitReputation();
+	if (amount < m_self_raw_materials) { 
+		isSuccessful = true; 
+		m_self_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) {
+	bool isSuccessful = true;
+	InitReputation();
+	m_other_raw_materials += amount;
+	return isSuccessful;
+}

Modified: branches/hjg-dev/source/main/cOrganism.h
===================================================================
--- branches/hjg-dev/source/main/cOrganism.h	2008-09-28 02:08:10 UTC (rev 2799)
+++ branches/hjg-dev/source/main/cOrganism.h	2008-09-28 05:44:26 UTC (rev 2800)
@@ -432,6 +432,36 @@
   };
   cOpinionSupport* m_opinion; //!< Lazily-initialized pointer to the opinion data.
   // -------- End of opinion support --------
+  
+  
+  // -------- Reputation support --------
+  /* An organism's reputation should relate to whether or not it cooperates sucessfully with 
+  other organisms. 
+  
+   Currently, as a first step, organisms have been given different raw materials that 
+   they can either hoard or donate.
+  */
+  
+public: 
+	// donate raw materials to others
+	bool SubtractSelfRawMaterials(int amount); 
+	// receive raw materials from others
+	bool AddOtherRawMaterials(int amount);
+	// retrieve the organism's own amount of raw materials
+	int GetSelfRawMaterials() {InitReputation(); return m_self_raw_materials; }
+	// retrieve the amount of raw materials collected from others
+	int GetOtherRawMaterials() {InitReputation(); return m_other_raw_materials; }
+		
+protected:
+	// Initialize reputation support
+	inline void InitReputation() {if (!m_self_raw_materials) {m_self_raw_materials = 10; m_other_raw_materials =0; }}
+	// 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;
+  
+  // -------- End of reputation support --------
+
 };
 
 

Modified: branches/hjg-dev/source/main/cTaskLib.cc
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.cc	2008-09-28 02:08:10 UTC (rev 2799)
+++ branches/hjg-dev/source/main/cTaskLib.cc	2008-09-28 05:44:26 UTC (rev 2800)
@@ -417,12 +417,17 @@
   else if (name == "movebetweenevent")
     NewTask(name, "Move to a target area", &cTaskLib::Task_MoveBetweenMovementEvent);  
 
+  // reputation based tasks
+  else if(name == "use_raw_mat")
+    NewTask(name, "Use raw materials from self and other", &cTaskLib::Task_UseRawMaterials);
+
   // event tasks
   if(name == "move_to_event")
     NewTask(name, "Moved into cell containing event", &cTaskLib::Task_MoveToEvent);
   else if(name == "event_killed")
     NewTask(name, "Killed event", &cTaskLib::Task_EventKilled);
   
+  
   // Make sure we have actually found a task  
   if (task_array.GetSize() == start_size) {
     if (errors != NULL && errors->GetSize() == 0) {
@@ -3056,3 +3061,16 @@
     return 1.0;
   return 0.0;
 }
+
+
+// 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.*/
+double cTaskLib::Task_UseRawMaterials(cTaskContext& ctx) const {
+	double bonus = 0.0;
+	if ((ctx.GetOrganism()->GetSelfRawMaterials() > 1) && 
+		(ctx.GetOrganism()->GetOtherRawMaterials() > 1)) {
+		 bonus = 1.0; 
+	}
+	return bonus;
+}

Modified: branches/hjg-dev/source/main/cTaskLib.h
===================================================================
--- branches/hjg-dev/source/main/cTaskLib.h	2008-09-28 02:08:10 UTC (rev 2799)
+++ branches/hjg-dev/source/main/cTaskLib.h	2008-09-28 05:44:26 UTC (rev 2800)
@@ -296,6 +296,9 @@
   // movement
   double Task_MoveToEvent(cTaskContext& ctx) const;
   double Task_EventKilled(cTaskContext& ctx) const;
+  
+  // reputation
+  double Task_UseRawMaterials(cTaskContext& ctx) const;
 };
 
 




More information about the Avida-cvs mailing list