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

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Mon Jan 28 03:48:55 PST 2008


Author: brysonda
Date: 2008-01-28 06:48:55 -0500 (Mon, 28 Jan 2008)
New Revision: 2284

Modified:
   development/source/analyze/cAnalyze.cc
   development/source/cpu/cHardwareBase.cc
   development/source/cpu/cHardwareBase.h
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareExperimental.cc
   development/source/cpu/cInstSet.cc
   development/source/cpu/cInstSet.h
   development/source/main/cPopulationCell.cc
Log:
Some adjustments to cost handling.

Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc	2008-01-28 01:51:17 UTC (rev 2283)
+++ development/source/analyze/cAnalyze.cc	2008-01-28 11:48:55 UTC (rev 2284)
@@ -3740,8 +3740,8 @@
 	  else cout << "function is neither f1 or f2! doh!\n";
 	  double thresh = react->GetTask()->GetArguments().GetDouble(3);
 	  double threshMax = react->GetTask()->GetArguments().GetDouble(4);
-	  double maxFx = react->GetTask()->GetArguments().GetDouble(1);
-	  double minFx = react->GetTask()->GetArguments().GetDouble(2);
+	  //double maxFx = react->GetTask()->GetArguments().GetDouble(1);
+	  //double minFx = react->GetTask()->GetArguments().GetDouble(2);
 
 	  // and pull the concentration of this resource from resource object loaded from resource.dat
 	  double concentration = resources.at(index).second.at(i);

Modified: development/source/cpu/cHardwareBase.cc
===================================================================
--- development/source/cpu/cHardwareBase.cc	2008-01-28 01:51:17 UTC (rev 2283)
+++ development/source/cpu/cHardwareBase.cc	2008-01-28 11:48:55 UTC (rev 2284)
@@ -839,7 +839,7 @@
 {
 #if INSTRUCTION_COSTS
 
-  if(m_world->GetConfig().ENERGY_ENABLED.Get() > 0) {
+  if (m_world->GetConfig().ENERGY_ENABLED.Get() > 0) {
     // TODO:  Get rid of magic number. check avaliable energy first
     double energy_req = inst_energy_cost[cur_inst.GetOp()] * (organism->GetPhenotype().GetMerit().GetDouble() / 100.0); //compensate by factor of 100
 
@@ -875,21 +875,22 @@
   }
   
   // Next, look at the per use cost
-  
-  if (m_inst_cost > 1) { // Current cost being paid, decrement and return false
-    m_inst_cost--;
-    return false;
+  if (m_has_costs) {
+    if (m_inst_cost > 1) { // Current cost being paid, decrement and return false
+      m_inst_cost--;
+      return false;
+    }
+    
+    if (!m_inst_cost && m_inst_set->GetCost(cur_inst) > 1) {
+      // no current cost, but there are costs active, and this instruction has a cost, setup the counter and return false
+      m_inst_cost = m_inst_set->GetCost(cur_inst) - 1;
+      return false;
+    }
+
+    // If we fall to here, reset the current cost count to zero
+    m_inst_cost = 0;
   }
-  
-  if (!m_inst_cost && m_has_costs && m_inst_set->GetCost(cur_inst) > 1) {
-    // no current cost, but there are costs active, and this instruction has a cost, setup the counter and return false
-    m_inst_cost = m_inst_set->GetCost(cur_inst) - 1;
-    return false;
-  }
 
-  // If we fall to here, reset the current cost count to zero
-  m_inst_cost = 0;
-
   if (m_world->GetConfig().ENERGY_ENABLED.Get() > 0) {
     inst_energy_cost[cur_inst.GetOp()] = m_inst_set->GetEnergyCost(cur_inst); // reset instruction energy cost
   }
@@ -899,25 +900,17 @@
 
 void cHardwareBase::ResetInstructionCosts()
 {
-  const int num_inst_cost = m_inst_set->GetSize();
-  
   m_inst_cost = 0;
-  
-  inst_ft_cost.Resize(num_inst_cost);
-  inst_energy_cost.Resize(num_inst_cost);
-  
-  m_has_costs = false;
-  m_has_ft_costs = false;
-  m_has_energy_costs = false;
-  
-  for (int i = 0; i < num_inst_cost; i++) {
-    if (!m_has_costs && m_inst_set->GetCost(cInstruction(i))) m_has_costs = true;
-    
-    inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
-    if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
-    
-    inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));    
-    if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
+
+  const int num_inst_cost = m_inst_set->GetSize();
+
+  if (m_has_ft_costs) {
+    inst_ft_cost.Resize(num_inst_cost);
+    for (int i = 0; i < num_inst_cost; i++) inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
   }
   
+  if (m_has_energy_costs) {
+    inst_energy_cost.Resize(num_inst_cost);
+    for (int i = 0; i < num_inst_cost; i++) inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));
+  }
 }

Modified: development/source/cpu/cHardwareBase.h
===================================================================
--- development/source/cpu/cHardwareBase.h	2008-01-28 01:51:17 UTC (rev 2283)
+++ development/source/cpu/cHardwareBase.h	2008-01-28 11:48:55 UTC (rev 2284)
@@ -29,6 +29,9 @@
 #include <cassert>
 #include <iostream>
 
+#ifndef cInstSet_h
+#include "cInstSet.h"
+#endif
 #ifndef tBuffer_h
 #include "tBuffer.h"
 #endif
@@ -43,12 +46,10 @@
 class cHeadCPU;
 class cInjectGenotype;
 class cInstruction;
-class cInstSet;
 class cMutation;
 class cOrganism;
 class cString;
 class cWorld;
-//class cStats; //AWC 06/29/06
 
 class cHardwareBase
 {
@@ -62,6 +63,7 @@
   int m_inst_cost;
   tArray<int> inst_ft_cost;
   tArray<int> inst_energy_cost;
+  bool m_has_any_costs;
   bool m_has_costs;
   bool m_has_ft_costs;
   bool m_has_energy_costs;
@@ -93,7 +95,10 @@
 public:
   cHardwareBase(cWorld* world, cOrganism* in_organism, cInstSet* inst_set)
     : m_world(world), organism(in_organism), m_inst_set(inst_set), m_tracer(NULL)
+    , m_has_costs(inst_set->HasCosts()), m_has_ft_costs(inst_set->HasFTCosts())
+    , m_has_energy_costs(m_inst_set->HasEnergyCosts())
   {
+    m_has_any_costs = (m_has_costs | m_has_ft_costs | m_has_energy_costs);
     assert(organism != NULL);
   }
   virtual ~cHardwareBase() { ; }

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2008-01-28 01:51:17 UTC (rev 2283)
+++ development/source/cpu/cHardwareCPU.cc	2008-01-28 11:48:55 UTC (rev 2284)
@@ -547,7 +547,8 @@
     const cInstruction& cur_inst = IP().GetInst();
     
     // Test if costs have been paid and it is okay to execute this now...
-    bool exec = SingleProcess_PayCosts(ctx, cur_inst);
+    bool exec = true;
+    if (m_has_any_costs) exec = SingleProcess_PayCosts(ctx, cur_inst);
 
     // Constitutive regulation applied here
     if (m_world->GetConfig().CONSTITUTIVE_REGULATION.Get() == 1) {

Modified: development/source/cpu/cHardwareExperimental.cc
===================================================================
--- development/source/cpu/cHardwareExperimental.cc	2008-01-28 01:51:17 UTC (rev 2283)
+++ development/source/cpu/cHardwareExperimental.cc	2008-01-28 11:48:55 UTC (rev 2284)
@@ -306,7 +306,8 @@
     const cInstruction& cur_inst = IP().GetInst();
     
     // Test if costs have been paid and it is okay to execute this now...
-    bool exec = SingleProcess_PayCosts(ctx, cur_inst);
+    bool exec = true;
+    if (m_has_any_costs) exec = SingleProcess_PayCosts(ctx, cur_inst);
 
     // Constitutive regulation applied here
     if (m_world->GetConfig().CONSTITUTIVE_REGULATION.Get() == 1) {

Modified: development/source/cpu/cInstSet.cc
===================================================================
--- development/source/cpu/cInstSet.cc	2008-01-28 01:51:17 UTC (rev 2283)
+++ development/source/cpu/cInstSet.cc	2008-01-28 11:48:55 UTC (rev 2284)
@@ -232,7 +232,11 @@
     m_lib_name_map[inst_id].prob_fail = args->GetDouble(0);
     m_lib_name_map[inst_id].addl_time_cost = args->GetInt(4);
     
+    if (m_lib_name_map[inst_id].cost > 1) m_has_costs = true;
+    if (m_lib_name_map[inst_id].ft_cost) m_has_ft_costs = true;
+    if (m_lib_name_map[inst_id].energy_cost) m_has_energy_costs = true;
     
+    
     // Parse the instruction code
     cString inst_code = args->GetString(0);
     if (inst_code == "") {
@@ -361,6 +365,10 @@
     m_lib_name_map[inst_id].prob_fail = prob_fail;
     m_lib_name_map[inst_id].addl_time_cost = addl_time_cost;
     m_lib_name_map[inst_id].inst_code = 0;
+
+    if (m_lib_name_map[inst_id].cost > 1) m_has_costs = true;
+    if (m_lib_name_map[inst_id].ft_cost) m_has_ft_costs = true;
+    if (m_lib_name_map[inst_id].energy_cost) m_has_energy_costs = true;
     
     const int total_redundancy = m_mutation_chart.GetSize();
     m_mutation_chart.Resize(total_redundancy + redundancy);

Modified: development/source/cpu/cInstSet.h
===================================================================
--- development/source/cpu/cInstSet.h	2008-01-28 01:51:17 UTC (rev 2283)
+++ development/source/cpu/cInstSet.h	2008-01-28 11:48:55 UTC (rev 2284)
@@ -79,13 +79,18 @@
   tArray<int> m_lib_nopmod_map;
   tArray<int> m_mutation_chart;     // ID's represented by redundancy values.
   
+  bool m_has_costs;
+  bool m_has_ft_costs;
+  bool m_has_energy_costs;
   
+  
   void LoadWithStringList(const cStringList& sl);
 
   cInstSet(); // @not_implemented
 
 public:
-  inline cInstSet(cWorld* world, cInstLib* inst_lib) : m_world(world), m_inst_lib(inst_lib) { ; }
+  inline cInstSet(cWorld* world, cInstLib* inst_lib)
+    : m_world(world), m_inst_lib(inst_lib), m_has_costs(false), m_has_ft_costs(false), m_has_energy_costs(false) { ; }
   inline cInstSet(const cInstSet& is);
   inline ~cInstSet() { ; }
 
@@ -118,6 +123,10 @@
 
   int GetSize() const { return m_lib_name_map.GetSize(); }
   int GetNumNops() const { return m_lib_nopmod_map.GetSize(); }
+  
+  bool HasCosts() const { return m_has_costs; }
+  bool HasFTCosts() const { return m_has_ft_costs; }
+  bool HasEnergyCosts() const { return m_has_energy_costs; }
 
   // Instruction Analysis.
   int IsNop(const cInstruction& inst) const { return (inst.GetOp() < m_lib_nopmod_map.GetSize()); }
@@ -159,7 +168,8 @@
 
 inline cInstSet::cInstSet(const cInstSet& is)
 : m_world(is.m_world), m_inst_lib(is.m_inst_lib), m_lib_name_map(is.m_lib_name_map)
-,m_lib_nopmod_map(is.m_lib_nopmod_map), m_mutation_chart(is.m_mutation_chart)
+, m_lib_nopmod_map(is.m_lib_nopmod_map), m_mutation_chart(is.m_mutation_chart)
+, m_has_costs(is.m_has_costs), m_has_ft_costs(is.m_has_ft_costs), m_has_energy_costs(is.m_has_energy_costs)
 {
 }
 
@@ -169,6 +179,9 @@
   m_lib_name_map = _in.m_lib_name_map;
   m_lib_nopmod_map = _in.m_lib_nopmod_map;
   m_mutation_chart = _in.m_mutation_chart;
+  m_has_costs = _in.m_has_costs;
+  m_has_ft_costs = _in.m_has_ft_costs;
+  m_has_energy_costs = _in.m_has_energy_costs;
   return *this;
 }
 

Modified: development/source/main/cPopulationCell.cc
===================================================================
--- development/source/main/cPopulationCell.cc	2008-01-28 01:51:17 UTC (rev 2283)
+++ development/source/main/cPopulationCell.cc	2008-01-28 11:48:55 UTC (rev 2284)
@@ -152,7 +152,9 @@
 	if(lr==1 && du==0) return 5; //E
 	if(lr==1 && du==-1) return 4; //SE
   
-	assert(false);  
+	assert(false);
+  
+  return 0;
 }
 
 void cPopulationCell::ResetInputs(cAvidaContext& ctx) 




More information about the Avida-cvs mailing list