[Avida-SVN] r1949 - development/source/cpu

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Thu Aug 16 11:34:41 PDT 2007


Author: brysonda
Date: 2007-08-16 14:34:41 -0400 (Thu, 16 Aug 2007)
New Revision: 1949

Modified:
   development/source/cpu/cHardwareGX.cc
   development/source/cpu/cInstSet.cc
   development/source/cpu/cInstSet.h
Log:
More adjustment to cInstSet loading.  Add some more error checking and reporting.  Change sInstEntry storage to a tSmartArray to avoid repeated copying of the fairly sizable structs.

Modified: development/source/cpu/cHardwareGX.cc
===================================================================
--- development/source/cpu/cHardwareGX.cc	2007-08-16 17:43:10 UTC (rev 1948)
+++ development/source/cpu/cHardwareGX.cc	2007-08-16 18:34:41 UTC (rev 1949)
@@ -4170,8 +4170,8 @@
   if (in_limit > m_world->GetConfig().MAX_PROGRAMIDS.Get()) in_limit = m_world->GetConfig().MAX_PROGRAMIDS.Get();
 
   // Create executable programids up to the limit
-  const int genome_size = m_programids[m_promoter_update_head.GetMemSpace()]->GetMemory().GetSize();
-  const int inc = Min(genome_size, m_world->GetConfig().IMPLICIT_MAX_PROGRAMID_LENGTH.Get());
+  //const int genome_size = m_programids[m_promoter_update_head.GetMemSpace()]->GetMemory().GetSize();
+  //const int inc = Min(genome_size, m_world->GetConfig().IMPLICIT_MAX_PROGRAMID_LENGTH.Get());
 
   while ( m_programids.size() < (unsigned int)in_limit )
   {

Modified: development/source/cpu/cInstSet.cc
===================================================================
--- development/source/cpu/cInstSet.cc	2007-08-16 17:43:10 UTC (rev 1948)
+++ development/source/cpu/cInstSet.cc	2007-08-16 18:34:41 UTC (rev 1949)
@@ -56,58 +56,14 @@
   return true;
 }
 
+
 cInstruction cInstSet::GetRandomInst(cAvidaContext& ctx) const
 {
-  int inst_op = m_mutation_chart[ctx.GetRandom().GetUInt(m_mutation_chart.GetSize())];
-  return cInstruction(inst_op);
+  return cInstruction(m_mutation_chart[ctx.GetRandom().GetUInt(m_mutation_chart.GetSize())]);
 }
 
 
-int cInstSet::AddInst(int lib_fun_id, int redundancy, int ft_cost, int cost, int energy_cost, double prob_fail, int addl_time_cost)
-{
-  if (lib_fun_id == m_inst_lib->GetInstNull().GetOp())
-    m_world->GetDriver().RaiseFatalException(1,"Invalid use of NULL instruction");
-  
-  const int inst_id = m_lib_name_map.GetSize();
 
-  assert(inst_id < 255);
-
-  // Increase the size of the array...
-  m_lib_name_map.Resize(inst_id + 1);
-
-  // Setup the new function...
-  m_lib_name_map[inst_id].lib_fun_id = lib_fun_id;
-  m_lib_name_map[inst_id].redundancy = redundancy;
-  m_lib_name_map[inst_id].cost = cost;
-  m_lib_name_map[inst_id].ft_cost = ft_cost;
-  m_lib_name_map[inst_id].energy_cost = energy_cost;
-  m_lib_name_map[inst_id].prob_fail = prob_fail;
-  m_lib_name_map[inst_id].addl_time_cost = addl_time_cost;
-
-  const int total_redundancy = m_mutation_chart.GetSize();
-  m_mutation_chart.Resize(total_redundancy + redundancy);
-  for (int i = 0; i < redundancy; i++) {
-    m_mutation_chart[total_redundancy + i] = inst_id;
-  }
-  total_energy_cost += energy_cost;
-
-  return inst_id;
-}
-
-
-int cInstSet::AddNop(int lib_nopmod_id, int redundancy, int ft_cost, int cost, int energy_cost, double prob_fail, int addl_time_cost)
-{
-  // Assert nops are at the _beginning_ of an inst_set.
-  assert(m_lib_name_map.GetSize() == m_lib_nopmod_map.GetSize());
-
-  const int inst_id = AddInst(lib_nopmod_id, redundancy, ft_cost, cost, energy_cost, prob_fail, addl_time_cost);
-
-  m_lib_nopmod_map.Resize(inst_id + 1);
-  m_lib_nopmod_map[inst_id] = lib_nopmod_id;
-
-  return inst_id;
-}
-
 cInstruction cInstSet::ActivateNullInst()
 {  
   const int inst_id = m_lib_name_map.GetSize();
@@ -189,9 +145,10 @@
   for (int line_id = 0; line_id < file.GetNumLines(); line_id++) {
     cString cur_line = file.GetLine(line_id);
     
+    // Lookup the instruction name in the library
     cString inst_name = cur_line.PopWord();
-    int inst_idx = m_inst_lib->GetIndex(inst_name);
-    if (inst_idx == -1) {
+    int fun_id = m_inst_lib->GetIndex(inst_name);
+    if (fun_id == -1) {
       // Oh oh!  Didn't find an instruction!
       cString* errorstr = new cString("Unknown instruction '");
       *errorstr += inst_name + "' (Best match = '" + m_inst_lib->GetNearMatch(inst_name) + "').";
@@ -200,26 +157,73 @@
       continue;
     }
     
+    // Load the arguments for this instruction
     cArgContainer* args = cArgContainer::Load(cur_line, schema, &errors);
     if (!args) {
       success = false;
       continue;
     }
     
+    // Check to make sure we are not inserting the special NULL instruction
+    if (fun_id == m_inst_lib->GetInstNull().GetOp()) {
+      errors.PushRear(new cString("Invalid use of NULL instruction"));
+      success = false;
+      continue;
+    }
+    
+    
     int redundancy = args->GetInt(0);
-    int cost = args->GetInt(1);
-    int ft_cost = args->GetInt(2);
-    int energy_cost = args->GetInt(3);
-    double prob_fail = args->GetDouble(0);
-    int addl_time_cost = args->GetInt(4);
+    if (redundancy < 0) {
+      m_world->GetDriver().NotifyWarning(cString("Instruction '") + inst_name + "' has negative redundancy, ignoring.");
+      continue;
+    }
+    if (redundancy > 256) {
+      cString msg("Max redundancy is 256.  Resetting redundancy of \"");
+      msg += inst_name; msg += "\" from "; msg += redundancy; msg += " to 256.";
+      m_world->GetDriver().NotifyWarning(msg);
+      redundancy = 256;
+    }
     
-    delete args;
     
-    if ((*m_inst_lib)[inst_idx].IsNop()) {
-      AddNop(inst_idx, redundancy, ft_cost, cost, energy_cost, prob_fail, addl_time_cost);
-    } else {
-      AddInst(inst_idx, redundancy, ft_cost, cost, energy_cost, prob_fail, addl_time_cost);
+    
+    // Get the ID of the new Instruction
+    const int inst_id = m_lib_name_map.GetSize();
+    assert(inst_id < 255);
+    
+    // Increase the size of the array...
+    m_lib_name_map.Resize(inst_id + 1);
+    
+    // Setup the new function...
+    m_lib_name_map[inst_id].lib_fun_id = fun_id;
+    m_lib_name_map[inst_id].redundancy = redundancy;
+    m_lib_name_map[inst_id].cost = args->GetInt(1);
+    m_lib_name_map[inst_id].ft_cost = args->GetInt(2);
+    m_lib_name_map[inst_id].energy_cost = args->GetInt(3);
+    m_lib_name_map[inst_id].prob_fail = args->GetDouble(0);
+    m_lib_name_map[inst_id].addl_time_cost = args->GetInt(4);
+    
+
+    // If this is a nop, add it to the proper mappings
+    if ((*m_inst_lib)[fun_id].IsNop()) {
+      // Assert nops are at the _beginning_ of an inst_set.
+      if (m_lib_name_map.GetSize() != (m_lib_nopmod_map.GetSize() + 1)) {
+        errors.PushRear(new cString("Invalid NOP placement, all NOPs must be listed first."));
+        success = false;
+      }
+
+      m_lib_nopmod_map.Resize(inst_id + 1);
+      m_lib_nopmod_map[inst_id] = fun_id;
     }
+    
+
+    const int total_redundancy = m_mutation_chart.GetSize();
+    m_mutation_chart.Resize(total_redundancy + redundancy);
+    for (int i = 0; i < redundancy; i++) {
+      m_mutation_chart[total_redundancy + i] = inst_id;
+    }
+    
+    // Clean up the argument container for this instruction
+    delete args;
   }
   
   if (!success) {
@@ -233,6 +237,7 @@
 }
 
 
+
 void cInstSet::LoadFromLegacyFile(const cString& filename)
 {
   cInitFile file(filename);
@@ -262,20 +267,49 @@
     
     // Otherwise, this instruction will be in the set.
     // First, determine if it is a nop...
-    int inst_idx = m_inst_lib->GetIndex(inst_name);
+    int fun_id = m_inst_lib->GetIndex(inst_name);
     
-    if (inst_idx == -1) {
+    if (fun_id == -1) {
       // Oh oh!  Didn't find an instruction!
       cString errorstr("Could not find instruction '");
       errorstr += inst_name + "'\n        (Best match = '" + m_inst_lib->GetNearMatch(inst_name) + "').";
       m_world->GetDriver().RaiseFatalException(1, errorstr);
     }
+
     
-    if ((*m_inst_lib)[inst_idx].IsNop()) {
-      AddNop(inst_idx, redundancy, ft_cost, cost, energy_cost, prob_fail, addl_time_cost);
-    } else {
-      AddInst(inst_idx, redundancy, ft_cost, cost, energy_cost, prob_fail, addl_time_cost);
+    
+    if (fun_id == m_inst_lib->GetInstNull().GetOp())
+      m_world->GetDriver().RaiseFatalException(1,"Invalid use of NULL instruction");
+    
+    const int inst_id = m_lib_name_map.GetSize();
+    
+    assert(inst_id < 255);
+    
+    // Increase the size of the array...
+    m_lib_name_map.Resize(inst_id + 1);
+    
+    // Setup the new function...
+    m_lib_name_map[inst_id].lib_fun_id = fun_id;
+    m_lib_name_map[inst_id].redundancy = redundancy;
+    m_lib_name_map[inst_id].cost = cost;
+    m_lib_name_map[inst_id].ft_cost = ft_cost;
+    m_lib_name_map[inst_id].energy_cost = energy_cost;
+    m_lib_name_map[inst_id].prob_fail = prob_fail;
+    m_lib_name_map[inst_id].addl_time_cost = addl_time_cost;
+    
+    const int total_redundancy = m_mutation_chart.GetSize();
+    m_mutation_chart.Resize(total_redundancy + redundancy);
+    for (int i = 0; i < redundancy; i++) {
+      m_mutation_chart[total_redundancy + i] = inst_id;
     }
+
+    if ((*m_inst_lib)[fun_id].IsNop()) {
+      // Assert nops are at the _beginning_ of an inst_set.
+      assert(m_lib_name_map.GetSize() == (m_lib_nopmod_map.GetSize() + 1));
+      
+      m_lib_nopmod_map.Resize(inst_id + 1);
+      m_lib_nopmod_map[inst_id] = fun_id;
+    }
   }
   
 }

Modified: development/source/cpu/cInstSet.h
===================================================================
--- development/source/cpu/cInstSet.h	2007-08-16 17:43:10 UTC (rev 1948)
+++ development/source/cpu/cInstSet.h	2007-08-16 18:34:41 UTC (rev 1949)
@@ -31,15 +31,18 @@
 #ifndef cString_h
 #include "cString.h"
 #endif
-#ifndef tArray_h
-#include "tArray.h"
+#ifndef cInstLib_h
+#include "cInstLib.h"
 #endif
 #ifndef cInstruction_h
 #include "cInstruction.h"
 #endif
-#ifndef cInstLib_h
-#include "cInstLib.h"
+#ifndef tArray_h
+#include "tArray.h"
 #endif
+#ifndef tSmartArray_h
+#include "tSmartArray.h"
+#endif
 
 using namespace std;
 
@@ -59,8 +62,8 @@
 public:
   cWorld* m_world;
   cInstLib* m_inst_lib;
-  class cInstEntry {
-  public:
+  
+  struct sInstEntry {
     int lib_fun_id;
     int redundancy;           // Weight in instruction set (not impl.)
     int cost;                 // additional time spent to exectute inst.
@@ -69,21 +72,15 @@
     double prob_fail;         // probability of failing to execute inst
     int addl_time_cost;       // additional time added to age for executing instruction
   };
-  tArray<cInstEntry> m_lib_name_map;
+  tSmartArray<sInstEntry> m_lib_name_map;
+  
   tArray<int> m_lib_nopmod_map;
   tArray<int> m_mutation_chart;     // ID's represented by redundancy values.
 
-  // Static components...
-  cInstruction m_inst_error;
-  cInstruction m_inst_default;
-
-  double total_energy_cost; // summation of energy costs of each instruction
-  
   cInstSet(); // @not_implemented
 
 public:
-  inline cInstSet(cWorld* world, cInstLib* inst_lib) : m_world(world), m_inst_lib(inst_lib),
-    m_inst_error(inst_lib->GetInstError()), m_inst_default(inst_lib->GetInstDefault()), total_energy_cost(0) { ; }
+  inline cInstSet(cWorld* world, cInstLib* inst_lib) : m_world(world), m_inst_lib(inst_lib) { ; }
   inline cInstSet(const cInstSet& is);
   inline ~cInstSet() { ; }
 
@@ -101,7 +98,6 @@
   double GetProbFail(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].prob_fail; }
   int GetRedundancy(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].redundancy; }
   int GetLibFunctionIndex(const cInstruction& inst) const { return m_lib_name_map[inst.GetOp()].lib_fun_id; }
-//  double GetAvgEnergyCostPerInst() const { return total_energy_cost/m_lib_name_map.GetSize(); }
 
   int GetNopMod(const cInstruction& inst) const
   {
@@ -120,9 +116,6 @@
   int IsLabel(const cInstruction& inst) const { return m_inst_lib->Get(GetLibFunctionIndex(inst)).IsLabel(); }
 
   // Insertion of new instructions...
-  int AddInst(int lib_fun_id, int redundancy = 1, int ft_cost = 0, int cost = 0, int energy_cost = 0, double prob_fail = 0.0, int addl_time_cost = 0);
-  int AddNop(int lib_nopmod_id, int redundancy = 1, int ft_cost = 0, int cost = 0, int energy_cost = 0, double prob_fail = 0.0, int addl_time_cost = 0);
-  
   cInstruction ActivateNullInst();
 
   // accessors for instruction library
@@ -132,8 +125,8 @@
   cString FindBestMatch(const cString& in_name) const;
   bool InstInSet(const cString& in_name) const;
 
-  cInstruction GetInstDefault() const { return m_inst_default; }
-  cInstruction GetInstError() const { return m_inst_error; }
+  cInstruction GetInstDefault() const { return m_inst_lib->GetInstDefault(); }
+  cInstruction GetInstError() const { return m_inst_lib->GetInstError(); }
   
   void LoadFromFile(const cString& filename);
   void LoadFromLegacyFile(const cString& filename);




More information about the Avida-cvs mailing list