[Avida-SVN] r2935 - in development: documentation source/cpu

blwalker at myxo.css.msu.edu blwalker at myxo.css.msu.edu
Tue Nov 11 14:15:08 PST 2008


Author: blwalker
Date: 2008-11-11 17:15:08 -0500 (Tue, 11 Nov 2008)
New Revision: 2935

Modified:
   development/documentation/code_instruction.html
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
Log:

- Abstracted collect functionality into helper function, DoCollect, which can be instructed whether to remove the resources from the environment and whether to add them to the internal resource bins
- Added two new instructions, Inst_CollectNoEnvRemove and Inst_CollectNoInternalAdd, to be used for testing what aspect of collection makes it attractive to the organisms
- Added a line in the instruction-implementation checklist documentation about flagging things for speculative stall, basically a copy of Dave's initial guidelines about which instructions should be so flagged.
- Changed the FindModifiedResources helper function to use the organism interface to get the number of resources rather than cWorld::GetNumResources(), since the latter is not very analyze-mode-friendly.

(test involving the collect instruction still passes -- no consistency changes)


Modified: development/documentation/code_instruction.html
===================================================================
--- development/documentation/code_instruction.html	2008-11-11 18:00:40 UTC (rev 2934)
+++ development/documentation/code_instruction.html	2008-11-11 22:15:08 UTC (rev 2935)
@@ -111,6 +111,12 @@
 to the location in memory that the method resides.
 </p>
 <p>
+<b>IMPORTANT:</b> If your instruction interacts with the population, resources,
+or IO, make sure to flag the instruction for speculative stall by adding a third
+ argument, <i>nInstFlags::STALL</i>.  For an example, look at the 'h-divide'
+ instruction.
+</p>
+<p>
 Compile again, and you should have your instruction ready for use.
 </p>
 

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2008-11-11 18:00:40 UTC (rev 2934)
+++ development/source/cpu/cHardwareCPU.cc	2008-11-11 22:15:08 UTC (rev 2935)
@@ -211,6 +211,8 @@
     tInstLibEntry<tMethod>("sense-m100", &cHardwareCPU::Inst_SenseMult100, nInstFlag::STALL),   // the names to cStats::cStats() @JEB
     tInstLibEntry<tMethod>("if-resources", &cHardwareCPU::Inst_IfResources, nInstFlag::STALL),
     tInstLibEntry<tMethod>("collect", &cHardwareCPU::Inst_Collect, nInstFlag::STALL),
+    tInstLibEntry<tMethod>("collect-no-env-remove", &cHardwareCPU::Inst_CollectNoEnvRemove),
+    tInstLibEntry<tMethod>("collect-no-internal-add", &cHardwareCPU::Inst_CollectNoInternalAdd, nInstFlag::STALL),
 
     tInstLibEntry<tMethod>("donate-rnd", &cHardwareCPU::Inst_DonateRandom),
     tInstLibEntry<tMethod>("donate-kin", &cHardwareCPU::Inst_DonateKin),
@@ -3303,11 +3305,6 @@
  * Mostly ripped from Jeff B.'s DoSense(); meant to be a helper function for
  * DoSense, Inst_Collect, and anything else that wants to use this type of
  * resource NOP-specification.
- * 
- * This relies on the assumption that m_world.GetNumResources() DOES NOT CHANGE
- * during the course of a run.  I know of no way that it can, since this is the
- * number of resources *defined* but I will check before this code goes to any
- * public branch.
  *
  * returns true if successful, false otherwise
  *
@@ -3319,7 +3316,7 @@
 
 bool cHardwareCPU::FindModifiedResource(int& start_index, int& end_index)
 {
-  int num_resources = m_world->GetNumResources();
+  int num_resources = organism->GetOrgInterface().GetResources().GetSize();
   
   //if there are no resources, translation cannot be successful; return false
   if (num_resources <= 0)
@@ -3353,7 +3350,13 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_Collect(cAvidaContext& ctx)
+/* Helper function to reduce code redundancy in the Inst_Collect variations.
+ * Does all the heavy lifting of external resource collection.  Use env_remove
+ * to specify whether the collected resources should be removed from the
+ * environment, and internal_add to specify whether the collected resources
+ * should be added to the organism's internal resources.
+ */
+bool cHardwareCPU::DoCollect(cAvidaContext& ctx, bool env_remove, bool internal_add)
 {
   int start_bin, end_bin, bin_used;
 
@@ -3376,7 +3379,7 @@
         bin_used = end_bin;
         break;
       case 3:
-        bin_used = -1; // not really, of course!  This just functions as a flag
+        bin_used = -1; // not really, of course!  This just functions as a flag to say we are using a range.
         break;
       default:
         bin_used = ctx.GetRandom().GetInt(start_bin, end_bin + 1);  // arbitrary choice of the default.  Shouldn't matter, since it shouldn't be needed...
@@ -3391,14 +3394,21 @@
   double total = organism->GetRBinsTotal();
   double max = m_world->GetConfig().MAX_TOTAL_STORED.Get();
 
-	// Add resources to rbins if this does not put the organism over the maximum total allowed
+	/* Remove resource(s) from environment if env_remove is set;
+   * add resource(s) to internal resource bins if internal_add is set
+   * (and this would not fill the bin beyond max).
+   */
   if(bin_used >= 0)
   {
     res_change[bin_used] = -1 * (res_count[bin_used] * m_world->GetConfig().ABSORB_RESOURCE_FRACTION.Get());
-    if(max < 0 || (total + -1 * res_change[bin_used]) <= max)
+    
+    if(internal_add && (max < 0 || (total + -1 * res_change[bin_used]) <= max))
     {organism->AddToRBin(bin_used, -1 * res_change[bin_used]);}
     else
     {res_change[bin_used] = 0.0;}
+    
+    if(!env_remove)
+    {res_change[bin_used] = 0.0;}
   }
   else
   {
@@ -3406,10 +3416,13 @@
     for(int i = start_bin; i <= end_bin; i++)
     {
       res_change[i] = -1 * (res_count[bin_used] * m_world->GetConfig().ABSORB_RESOURCE_FRACTION.Get() / num_bins);
-      if(max < 0 || (total + -1 * res_change[i]) <= max)
+      if(internal_add && (max < 0 || (total + -1 * res_change[i]) <= max))
       {organism->AddToRBin(i, -1 * res_change[i]);}
       else
       {res_change[i] = 0.0;}   
+      
+      if(!env_remove)
+      {res_change[i] = 0.0;}
     }
   }
   
@@ -3419,6 +3432,29 @@
   return true;
 }
 
+/* Takes resource(s) from the environment and adds them to the internal resource
+ * bins of the organism.
+ */
+bool cHardwareCPU::Inst_Collect(cAvidaContext& ctx)
+{
+  return DoCollect(ctx, true, true);
+}
+
+/* Like Inst_Collect, but the collected resources are not removed from the
+ * environment.
+ */
+bool cHardwareCPU::Inst_CollectNoEnvRemove(cAvidaContext& ctx)
+{
+  return DoCollect(ctx, false, true);
+}
+
+/* Like Inst_Collect, but the collected resources are not added to the organism.
+ */
+bool cHardwareCPU::Inst_CollectNoInternalAdd(cAvidaContext& ctx)
+{
+  return DoCollect(ctx, true, false);
+}
+
 /*! Sense the level of resources in this organism's cell, and if all of the 
 resources present are above the min level for that resource, execute the following
 intruction.  Otherwise, skip the following instruction.

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2008-11-11 18:00:40 UTC (rev 2934)
+++ development/source/cpu/cHardwareCPU.h	2008-11-11 22:15:08 UTC (rev 2935)
@@ -465,8 +465,10 @@
   bool Inst_SenseMult100(cAvidaContext& ctx);
   bool DoSense(cAvidaContext& ctx, int conversion_method, double base);
   bool FindModifiedResource(int& start_index, int& end_index);
+  bool DoCollect(cAvidaContext& ctx, bool env_remove, bool internal_add);
   bool Inst_Collect(cAvidaContext& ctx);
-  //! Execute the following instruction if all resources are above their min level.
+  bool Inst_CollectNoEnvRemove(cAvidaContext& ctx);
+  bool Inst_CollectNoInternalAdd(cAvidaContext& ctx);  //! Execute the following instruction if all resources are above their min level.
   bool Inst_IfResources(cAvidaContext& ctx);
 
   void DoDonate(cOrganism * to_org);




More information about the Avida-cvs mailing list