[Avida-SVN] r3236 - in development/source: cpu main

blwalker at myxo.css.msu.edu blwalker at myxo.css.msu.edu
Mon May 18 08:14:36 PDT 2009


Author: blwalker
Date: 2009-05-18 11:14:36 -0400 (Mon, 18 May 2009)
New Revision: 3236

Modified:
   development/source/cpu/cCodeLabel.cc
   development/source/cpu/cCodeLabel.h
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/main/cOrganism.cc
   development/source/main/cOrganism.h
   development/source/main/cPhenotype.cc
   development/source/main/cPhenotype.h
   development/source/main/cWorld.cc
   development/source/main/cWorld.h
Log:

Some initial architecture for tracking nop-specification use in "collect"-like instructions.


Modified: development/source/cpu/cCodeLabel.cc
===================================================================
--- development/source/cpu/cCodeLabel.cc	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/cpu/cCodeLabel.cc	2009-05-18 15:14:36 UTC (rev 3236)
@@ -140,7 +140,7 @@
 int cCodeLabel::AsIntDirect(const int base) const
 {
   int value = 0;
-
+  
   for (int i = 0; i < m_size; i++) {
     value *= base;
     value += m_nops[i];
@@ -149,6 +149,30 @@
   return value;
 }
 
+/* Translates a code label into a unique integer (given a base >= the number of nop types)
+ * Example: nops A, B, C with base 3
+ *   no nops = 0
+ *   A       = 1
+ *   B       = 2
+ *   AA      = 4
+ *   AC      = 6
+ *   BB      = 8
+ *   CA      = 12
+ *
+ * N.B.: Uniqueness will NOT be true if base < # of nop types
+ */
+int cCodeLabel::AsIntUnique(const int base) const
+{
+  int value = 0;
+  
+  for (int i = 0; i < m_size; i++) {
+    value *= base;
+    value += m_nops[i] + 1;
+  }
+  
+  return value;
+}
+
 int cCodeLabel::AsIntAdditivePolynomial(const int base) const
 {
   double value = 0.0;

Modified: development/source/cpu/cCodeLabel.h
===================================================================
--- development/source/cpu/cCodeLabel.h	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/cpu/cCodeLabel.h	2009-05-18 15:14:36 UTC (rev 3236)
@@ -82,6 +82,7 @@
   int AsInt(const int base) const;
   int AsIntGreyCode(const int base) const;
   int AsIntDirect(const int base) const;
+  int AsIntUnique(const int base) const;
   int AsIntAdditivePolynomial(const int base) const;
   int AsIntFib(const int base) const;
   int AsIntPolynomialCoefficent(const int base) const;

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/cpu/cHardwareCPU.cc	2009-05-18 15:14:36 UTC (rev 3236)
@@ -3462,9 +3462,11 @@
  * respective arguments; any int at all may be passed to these, as it will just 
  * get overwritten.  (Obviously, if the resource is fully specified, 
  * start_index == end_index.)
+ *
+ * spec_id is the id number of the specification
  */
 
-bool cHardwareCPU::FindModifiedResource(int& start_index, int& end_index)
+bool cHardwareCPU::FindModifiedResource(int& start_index, int& end_index, int& spec_id)
 {
   int num_resources = m_organism->GetOrgInterface().GetResources().GetSize();
   
@@ -3482,6 +3484,9 @@
   //find the length of the label that was actually read
   int real_label_length = GetLabel().GetSize();
   
+  // save the specification id
+  spec_id = GetLabel().AsIntUnique(num_nops);
+  
   /* find start and end resource indices specified by the label */
   
   cCodeLabel start_label = cCodeLabel(GetLabel());
@@ -3500,8 +3505,9 @@
   return true;
 }
 
-/* Helper function to reduce code redundancy in the Inst_Collect variations.
- * Does all the heavy lifting of external resource collection.
+/* Helper function to reduce code redundancy in the Inst_Collect variations,
+ * including Inst_Destroy.
+ * Does all the heavy lifting of external resource collection/destruction.
  *
  * env_remove   - specifies whether the collected resources should be removed from
  *                the environment
@@ -3510,10 +3516,13 @@
  */
 bool cHardwareCPU::DoCollect(cAvidaContext& ctx, bool env_remove, bool internal_add)
 {
-  int start_bin, end_bin, bin_used;
+  int start_bin, end_bin, bin_used, spec_id;
 
-  bool finite_resources_exist = FindModifiedResource(start_bin, end_bin);
+  bool finite_resources_exist = FindModifiedResource(start_bin, end_bin, spec_id);
   if(!finite_resources_exist) {return true;}
+  
+  // Add this specification
+  m_organism->IncCollectSpecCount(spec_id);
 
   if(start_bin == end_bin)  // resource completely specified
   {bin_used = start_bin;}

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/cpu/cHardwareCPU.h	2009-05-18 15:14:36 UTC (rev 3236)
@@ -490,7 +490,7 @@
   bool DoSense(cAvidaContext& ctx, int conversion_method, double base);
   
   // Resources
-  bool FindModifiedResource(int& start_index, int& end_index);
+  bool FindModifiedResource(int& start_index, int& end_index, int& spec_id);
   bool DoCollect(cAvidaContext& ctx, bool env_remove, bool internal_add);
   bool Inst_Collect(cAvidaContext& ctx);
   bool Inst_CollectNoEnvRemove(cAvidaContext& ctx);

Modified: development/source/main/cOrganism.cc
===================================================================
--- development/source/main/cOrganism.cc	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/main/cOrganism.cc	2009-05-18 15:14:36 UTC (rev 3236)
@@ -228,6 +228,12 @@
 	{ m_phenotype.AddToCurRBinTotal(index, value); }
 }  
 
+void cOrganism::IncCollectSpecCount(const int spec_id)
+{
+  int current_count = m_phenotype.GetCurCollectSpecCount(spec_id);
+  m_phenotype.SetCurCollectSpecCount(spec_id, current_count + 1);
+}
+
 double cOrganism::GetTestFitness(cAvidaContext& ctx)
 {
   assert(m_interface);

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/main/cOrganism.h	2009-05-18 15:14:36 UTC (rev 3236)
@@ -205,6 +205,7 @@
   void SetRBins(const tArray<double>& rbins_in);
   void SetRBin(const int index, const double value);
   void AddToRBin(const int index, const double value);
+  void IncCollectSpecCount(const int spec_id);
 
   int GetMaxExecuted() const { return m_max_executed; }
   

Modified: development/source/main/cPhenotype.cc
===================================================================
--- development/source/main/cPhenotype.cc	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/main/cPhenotype.cc	2009-05-18 15:14:36 UTC (rev 3236)
@@ -51,6 +51,7 @@
   , cur_internal_task_quality(m_world->GetEnvironment().GetNumTasks())
   , cur_rbins_total(m_world->GetEnvironment().GetResourceLib().GetSize())
   , cur_rbins_avail(m_world->GetEnvironment().GetResourceLib().GetSize())
+  , cur_collect_spec_counts(m_world->GetNumResourceSpecs())
   , cur_reaction_count(m_world->GetEnvironment().GetReactionLib().GetSize())
   , cur_reaction_add_reward(m_world->GetEnvironment().GetReactionLib().GetSize())
   , cur_sense_count(m_world->GetStats().GetSenseSize())
@@ -63,6 +64,7 @@
   , last_internal_task_quality(m_world->GetEnvironment().GetNumTasks())
   , last_rbins_total(m_world->GetEnvironment().GetResourceLib().GetSize())
   , last_rbins_avail(m_world->GetEnvironment().GetResourceLib().GetSize())
+  , last_collect_spec_counts()
   , last_reaction_count(m_world->GetEnvironment().GetReactionLib().GetSize())
   , last_reaction_add_reward(m_world->GetEnvironment().GetReactionLib().GetSize())  
   , last_sense_count(m_world->GetStats().GetSenseSize())
@@ -121,6 +123,7 @@
   cur_task_value           = in_phen.cur_task_value;			
   cur_rbins_total          = in_phen.cur_rbins_total;
   cur_rbins_avail          = in_phen.cur_rbins_avail;
+  cur_collect_spec_counts  = in_phen.cur_collect_spec_counts;
   cur_reaction_count       = in_phen.cur_reaction_count;            
   cur_reaction_add_reward  = in_phen.cur_reaction_add_reward;     
   cur_inst_count           = in_phen.cur_inst_count;                 
@@ -154,6 +157,7 @@
   last_task_value          = in_phen.last_task_value;
   last_rbins_total         = in_phen.last_rbins_total;
   last_rbins_avail         = in_phen.last_rbins_avail;
+  last_collect_spec_counts = in_phen.last_collect_spec_counts;
   last_reaction_count      = in_phen.last_reaction_count;
   last_reaction_add_reward = in_phen.last_reaction_add_reward; 
   last_inst_count          = in_phen.last_inst_count;	  
@@ -330,6 +334,7 @@
   // offspring gets that value too.
   for (int i = 0; i < cur_rbins_avail.GetSize(); i++)
         cur_rbins_avail[i] = parent_phenotype.cur_rbins_avail[i];
+  cur_collect_spec_counts.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_reaction_add_reward.SetAll(0);
   cur_inst_count.SetAll(0);
@@ -355,8 +360,9 @@
   last_task_quality         = parent_phenotype.last_task_quality;
   last_task_value           = parent_phenotype.last_task_value;
   last_internal_task_quality= parent_phenotype.last_internal_task_quality;
-  last_rbins_total           = parent_phenotype.last_rbins_total;
-  last_rbins_avail           = parent_phenotype.last_rbins_avail;
+  last_rbins_total          = parent_phenotype.last_rbins_total;
+  last_rbins_avail          = parent_phenotype.last_rbins_avail;
+  last_collect_spec_counts  = parent_phenotype.last_collect_spec_counts;
   last_reaction_count       = parent_phenotype.last_reaction_count;
   last_reaction_add_reward  = parent_phenotype.last_reaction_add_reward;
   last_inst_count           = parent_phenotype.last_inst_count;
@@ -491,6 +497,7 @@
   cur_internal_task_quality.SetAll(0);
   cur_rbins_total.SetAll(0);
   cur_rbins_avail.SetAll(0);
+  cur_collect_spec_counts.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_reaction_add_reward.SetAll(0);
   cur_inst_count.SetAll(0);
@@ -504,7 +511,7 @@
   trial_cpu_cycles_used = 0;
   cur_child_germline_propensity = m_world->GetConfig().DEMES_DEFAULT_GERMLINE_PROPENSITY.Get();
 
-  // Copy last values from parent
+  // New organism has no parent and so cannot use its last values; initialize as needed
   last_merit_base = genome_length;
   last_bonus      = 1;
   last_cpu_cycles_used = 0;
@@ -517,6 +524,7 @@
   last_internal_task_quality.SetAll(0);
   last_rbins_total.SetAll(0);
   last_rbins_avail.SetAll(0);
+  last_collect_spec_counts.SetAll(0);
   last_reaction_count.SetAll(0);
   last_reaction_add_reward.SetAll(0);
   last_inst_count.SetAll(0);
@@ -653,8 +661,9 @@
   last_task_quality         = cur_task_quality;
   last_task_value           = cur_task_value;
   last_internal_task_quality= cur_internal_task_quality;
-  last_rbins_total           = cur_rbins_total;
-  last_rbins_avail           = cur_rbins_avail;
+  last_rbins_total          = cur_rbins_total;
+  last_rbins_avail          = cur_rbins_avail;
+  last_collect_spec_counts  = cur_collect_spec_counts;
   last_reaction_count       = cur_reaction_count;
   last_reaction_add_reward  = cur_reaction_add_reward;
   last_inst_count           = cur_inst_count;
@@ -676,6 +685,7 @@
   cur_rbins_total.SetAll(0);  // total resources collected in lifetime
   // resources available are split in half -- the offspring gets the other half
   for (int i = 0; i < cur_rbins_avail.GetSize(); i++) {cur_rbins_avail[i] /= 2.0;}
+  cur_collect_spec_counts.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_reaction_add_reward.SetAll(0);
   cur_inst_count.SetAll(0);
@@ -809,6 +819,7 @@
   last_internal_task_quality= cur_internal_task_quality;
   last_rbins_total          = cur_rbins_total;
   last_rbins_avail          = cur_rbins_avail;
+  last_collect_spec_counts  = cur_collect_spec_counts;
   last_reaction_count       = cur_reaction_count;
   last_reaction_add_reward  = cur_reaction_add_reward;
   last_inst_count           = cur_inst_count;
@@ -829,6 +840,7 @@
   cur_rbins_total.SetAll(0);  // total resources collected in lifetime
   // resources available are split in half -- the offspring gets the other half
   for (int i = 0; i < cur_rbins_avail.GetSize(); i++) {cur_rbins_avail[i] /= 2.0;}
+  cur_collect_spec_counts.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_reaction_add_reward.SetAll(0);
   cur_inst_count.SetAll(0);
@@ -960,6 +972,7 @@
   eff_task_count.SetAll(0);
   cur_rbins_total.SetAll(0);
   cur_rbins_avail.SetAll(0);
+  cur_collect_spec_counts.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_reaction_add_reward.SetAll(0);
   cur_inst_count.SetAll(0);
@@ -984,6 +997,7 @@
   last_internal_task_count = clone_phenotype.last_internal_task_count;
   last_rbins_total         = clone_phenotype.last_rbins_total;
   last_rbins_avail         = clone_phenotype.last_rbins_avail;
+  last_collect_spec_counts = clone_phenotype.last_collect_spec_counts;
   last_reaction_count      = clone_phenotype.last_reaction_count;
   last_reaction_add_reward = clone_phenotype.last_reaction_add_reward;
   last_inst_count          = clone_phenotype.last_inst_count;
@@ -1513,6 +1527,7 @@
   last_task_value			      = cur_task_value;
   last_rbins_total          = cur_rbins_total;
   last_rbins_avail          = cur_rbins_avail;
+  last_collect_spec_counts  = cur_collect_spec_counts;
   last_reaction_count       = cur_reaction_count;
   last_reaction_add_reward  = cur_reaction_add_reward;
   last_inst_count           = cur_inst_count;
@@ -1532,6 +1547,7 @@
   cur_task_value.SetAll(0);
   cur_rbins_total.SetAll(0);
   cur_rbins_avail.SetAll(0);
+  cur_collect_spec_counts.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_reaction_add_reward.SetAll(0);
   cur_inst_count.SetAll(0);

Modified: development/source/main/cPhenotype.h
===================================================================
--- development/source/main/cPhenotype.h	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/main/cPhenotype.h	2009-05-18 15:14:36 UTC (rev 3236)
@@ -127,8 +127,9 @@
   tArray<double> cur_task_quality;            // Average (total?) quality with which each task was performed
   tArray<double> cur_task_value;              // Value with which this phenotype performs task
   tArray<double> cur_internal_task_quality;   // Average (total?) quaility with which each task using internal resources was performed
-  tArray<double> cur_rbins_total;              // Total amount of resources collected
-  tArray<double> cur_rbins_avail;              // Total amount of internal resources available
+  tArray<double> cur_rbins_total;             // Total amount of resources collected over the organism's life
+  tArray<double> cur_rbins_avail;             // Amount of internal resources available
+  tArray<int> cur_collect_spec_counts; // How many times each nop-specification was used in a collect-type instruction
   tArray<int> cur_reaction_count;             // Total times each reaction was triggered.  
   tArray<double> cur_reaction_add_reward;     // Bonus change from triggering each reaction.
   tArray<int> cur_inst_count;                 // Instruction exection counter
@@ -156,6 +157,7 @@
   tArray<double> last_internal_task_quality;
   tArray<double> last_rbins_total;
   tArray<double> last_rbins_avail;
+  tArray<int> last_collect_spec_counts;
   tArray<int> last_reaction_count;
   tArray<double> last_reaction_add_reward; 
   tArray<int> last_inst_count;	  // Instruction exection counter
@@ -353,6 +355,8 @@
   const tArray<int>& GetCurInstCount() const { assert(initialized == true); return cur_inst_count; }
   const tArray<int>& GetCurSenseCount() const { assert(initialized == true); return cur_sense_count; }
   double GetSensedResource(int _in) { assert(initialized == true); return sensed_resources[_in]; }
+  const tArray<int>& GetCurCollectSpecCounts() const { assert(initialized == true); return cur_collect_spec_counts; }
+  const int GetCurCollectSpecCount(int spec_id) const { assert(initialized == true); return cur_collect_spec_counts[spec_id]; }
   
   void  NewTrial(); //Save the current fitness, and reset the bonus. @JEB
   void  TrialDivideReset(const cGenome & _genome); //Subset of resets specific to division not done by NewTrial. @JEB
@@ -379,6 +383,8 @@
   const tArray<int>& GetLastSenseCount() const { assert(initialized == true); return last_sense_count; }
   double GetLastFitness() const { assert(initialized == true); return last_fitness; }
   double GetPermanentGermlinePropensity() const { assert(initialized == true); return permanent_germline_propensity; }
+  const tArray<int>& GetLastCollectSpecCounts() const { assert(initialized == true); return last_collect_spec_counts; }
+  const int GetLastCollectSpecCount(int spec_id) const { assert(initialized == true); return last_collect_spec_counts[spec_id]; }
 
   int GetNumDivides() const { assert(initialized == true); return num_divides;}
   int GetGeneration() const { assert(initialized == true); return generation; }
@@ -489,6 +495,7 @@
   void SetCurRBinTotal(int index, double val) { cur_rbins_total[index] = val; }
   void AddToCurRBinAvail(int index, double val) { cur_rbins_avail[index] += val; }
   void AddToCurRBinTotal(int index, double val) { cur_rbins_total[index] += val; }
+  void SetCurCollectSpecCount(int spec_id, int val) { cur_collect_spec_counts[spec_id] = val; }
 
   void SetIsDonorCur() { is_donor_cur = true; } 
   void SetIsDonorRand() { SetIsDonorCur(); is_donor_rand = true; }

Modified: development/source/main/cWorld.cc
===================================================================
--- development/source/main/cWorld.cc	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/main/cWorld.cc	2009-05-18 15:14:36 UTC (rev 3236)
@@ -157,6 +157,21 @@
   return m_env->GetResourceLib().GetSize();
 }
 
+// Given number of resources and number of nops, how many possible collect-type resource specifications exist?
+// If no nops or no resources, return 0
+int cWorld::GetNumResourceSpecs()
+{
+  int num_resources = (double)GetEnvironment().GetResourceLib().GetSize();
+  int num_nops = (double)GetHardwareManager().GetInstSet().GetNumNops();
+  
+  if (num_resources <= 0 || num_nops <= 0) { return 0; }
+  
+  double most_nops_needed = ceil(log((double)num_resources)/log((double)num_nops));
+  double numerator = pow(num_nops, most_nops_needed + 1) - 1;
+  double denominator = (double)(num_nops - 1);
+  return (int)(numerator / denominator);
+}
+
 void cWorld::SetDriver(cWorldDriver* driver, bool take_ownership)
 {
   // cleanup current driver, if needed

Modified: development/source/main/cWorld.h
===================================================================
--- development/source/main/cWorld.h	2009-05-14 15:11:40 UTC (rev 3235)
+++ development/source/main/cWorld.h	2009-05-18 15:14:36 UTC (rev 3236)
@@ -123,6 +123,7 @@
   // Convenience Accessors
   int GetNumInstructions();
   int GetNumResources();
+  int GetNumResourceSpecs();
   inline int GetVerbosity() { return m_conf->VERBOSITY.Get(); }
   inline void SetVerbosity(int v) { m_conf->VERBOSITY.Set(v); }
 




More information about the Avida-cvs mailing list