[Avida-SVN] r1378 - in branches/energy/source: cpu drivers main targets/avida-viewer

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Wed Feb 28 06:47:35 PST 2007


Author: beckma24
Date: 2007-02-28 09:47:35 -0500 (Wed, 28 Feb 2007)
New Revision: 1378

Modified:
   branches/energy/source/cpu/cHardwareCPU.cc
   branches/energy/source/cpu/cHardwareManager.cc
   branches/energy/source/cpu/cInstSet.cc
   branches/energy/source/cpu/cInstSet.h
   branches/energy/source/drivers/cDefaultRunDriver.cc
   branches/energy/source/main/cAvidaConfig.h
   branches/energy/source/main/cPhenotype.cc
   branches/energy/source/main/cPhenotype.h
   branches/energy/source/main/cPopulation.cc
   branches/energy/source/main/cPopulation.h
   branches/energy/source/targets/avida-viewer/cZoomScreen.cc
Log:
Yahoo, a working energy model.

An organism's merit is only updated on divide, but that will probably be changed shortly.  It will eventually occur every time additional energy is received by the organism.  

Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/cpu/cHardwareCPU.cc	2007-02-28 14:47:35 UTC (rev 1378)
@@ -400,6 +400,7 @@
   const int num_inst_cost = m_inst_set->GetSize();
   inst_cost.Resize(num_inst_cost);
   inst_ft_cost.Resize(num_inst_cost);
+  inst_energy_cost.Resize(num_inst_cost);
   
   for (int i = 0; i < num_inst_cost; i++) {
     inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
@@ -519,8 +520,8 @@
   assert(cur_inst.GetOp() < inst_cost.GetSize());
   
   // check avaliable energy first
-  int energy_req = m_inst_set->GetEnergyCost(cur_inst);
-  if(energy_req > 0) {
+  double energy_req = m_inst_set->GetEnergyCost(cur_inst) * (organism->GetPhenotype().GetMerit().GetDouble() / 100.0); //compensate by factor of 100
+  if(energy_req > 0.0) {
     if(organism->GetPhenotype().GetStoredEnergy() >= energy_req) {
       //subtract energy used from current energy.
       organism->GetPhenotype().ReduceEnergy(energy_req);

Modified: branches/energy/source/cpu/cHardwareManager.cc
===================================================================
--- branches/energy/source/cpu/cHardwareManager.cc	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/cpu/cHardwareManager.cc	2007-02-28 14:47:35 UTC (rev 1378)
@@ -88,6 +88,7 @@
     double prob_fail = cur_line.PopWord().AsDouble();
     int addl_time_cost = cur_line.PopWord().AsInt();
 
+//    cerr<< inst_name<<" "<<cost << " "<<energy_cost << " "<<endl; //TODO -remove
     // If this instruction has 0 redundancy, we don't want it!
     if (redundancy < 0) continue;
     if (redundancy > 256) {

Modified: branches/energy/source/cpu/cInstSet.cc
===================================================================
--- branches/energy/source/cpu/cInstSet.cc	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/cpu/cInstSet.cc	2007-02-28 14:47:35 UTC (rev 1378)
@@ -81,6 +81,7 @@
   for (int i = 0; i < redundancy; i++) {
     m_mutation_chart[total_redundancy + i] = inst_id;
   }
+  total_energy_cost += energy_cost;
 
   return inst_id;
 }
@@ -90,7 +91,7 @@
   // 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, int energy_cost, prob_fail, addl_time_cost);
+  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;

Modified: branches/energy/source/cpu/cInstSet.h
===================================================================
--- branches/energy/source/cpu/cInstSet.h	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/cpu/cInstSet.h	2007-02-28 14:47:35 UTC (rev 1378)
@@ -76,12 +76,14 @@
   // 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()) { ; }
+    m_inst_error(inst_lib->GetInstError()), m_inst_default(inst_lib->GetInstDefault()), total_energy_cost(0) { ; }
   inline cInstSet(const cInstSet& is);
   inline ~cInstSet() { ; }
 
@@ -99,6 +101,7 @@
   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
   {
@@ -116,8 +119,8 @@
   int IsNop(const cInstruction& inst) const { return (inst.GetOp() < m_lib_nopmod_map.GetSize()); }
 
   // Insertion of new instructions...
-  int AddInst(int lib_fun_id, int redundancy = 1, int ft_cost = 0, int 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, double prob_fail = 0.0, int addl_time_cost = 0);
+  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);
 
   // accessors for instruction library
   cInstLib* GetInstLib() { return m_inst_lib; }

Modified: branches/energy/source/drivers/cDefaultRunDriver.cc
===================================================================
--- branches/energy/source/drivers/cDefaultRunDriver.cc	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/drivers/cDefaultRunDriver.cc	2007-02-28 14:47:35 UTC (rev 1378)
@@ -117,6 +117,8 @@
       cout << "UD: " << setw(6) << stats.GetUpdate() << "  "
         << "Gen: " << setw(9) << setprecision(7) << stats.SumGeneration().Average() << "  "
         << "Fit: " << setw(9) << setprecision(7) << stats.GetAveFitness() << "  "
+      //  << "Energy: " << setw(9) << setprecision(7) << stats.GetAveEnergy() << "  "
+        << "Merit: " << setw(9) << setprecision(7) << stats.GetAveMerit() << "  "
         << "Size: " << population.GetNumOrganisms()
         << endl;
     }

Modified: branches/energy/source/main/cAvidaConfig.h
===================================================================
--- branches/energy/source/main/cAvidaConfig.h	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/main/cAvidaConfig.h	2007-02-28 14:47:35 UTC (rev 1378)
@@ -234,11 +234,8 @@
   CONFIG_ADD_VAR(REQUIRED_TASK, int, -1, "Task ID required for successful divide.");
   CONFIG_ADD_VAR(IMMUNITY_TASK, int, -1, "Task providing immunity from the required task.");
   CONFIG_ADD_VAR(REQUIRED_REACTION, int, -1, "Reaction ID required for successful divide.");
-  CONFIG_ADD_VAR(REQUIRED_BONUS, int, 0, "The bonus that an organism must accumulate to divide."); 
-  CONFIG_ADD_VAR(FRAC_ENERGY_GIVEN, int, 0.5, "Fraction of energy given to offspring."); 
-  CONFIG_ADD_VAR(INITITAL_ENERGY_GIVEN, int, -1, "Initial energy given to offspring.\n-1 = energy model off"); 
+  CONFIG_ADD_VAR(REQUIRED_BONUS, int, 0, "The bonus that an organism must accumulate to divide.");  
  
- 
   CONFIG_ADD_GROUP(MUTATION_GROUP, "Mutations");
   CONFIG_ADD_VAR(POINT_MUT_PROB, double, 0.0, "Mutation rate (per-location per update)");
   CONFIG_ADD_VAR(COPY_MUT_PROB, double, 0.0075, "Mutation rate (per copy)");
@@ -322,7 +319,14 @@
   CONFIG_ADD_VAR(MT_CONCURRENCY, int, 1, "Number of concurrent analyze threads");
   CONFIG_ADD_VAR(ANALYZE_OPTION_1, cString, "", "String variable accessible from analysis scripts");
   CONFIG_ADD_VAR(ANALYZE_OPTION_2, cString, "", "String variable accessible from analysis scripts");
+  
+  CONFIG_ADD_GROUP(ENERGY_GROUP, "Energy Settings");
+  CONFIG_ADD_VAR(ENERGY_ENABLED, bool, false, "Enable Energy Model");
+  CONFIG_ADD_VAR(FRAC_ENERGY_GIVEN, double, 0.5, "Fraction of energy given to offspring.");
+  CONFIG_ADD_VAR(INITIAL_ENERGY_GIVEN, int, 0, "Initial energy given to offspring.");
+  CONFIG_ADD_VAR(NUM_INST_EXC_BEFORE_0_ENERGY, int, 0, "Number of instructions executed before energy is exhausted."); 
 
+
 #endif
   
   void Load(const cString& filename, const bool& crash_if_not_found);

Modified: branches/energy/source/main/cPhenotype.cc
===================================================================
--- branches/energy/source/main/cPhenotype.cc	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/main/cPhenotype.cc	2007-02-28 14:47:35 UTC (rev 1378)
@@ -105,6 +105,7 @@
 {
   // Copy divide values from parent, which should already be setup.
   merit           = parent_phenotype.merit;
+  energy_store    = (parent_phenotype.energy_store * m_world->GetConfig().FRAC_ENERGY_GIVEN.Get())+m_world->GetConfig().INITIAL_ENERGY_GIVEN.Get();
   genome_length   = _length;
   copied_size     = parent_phenotype.child_copied_size;
   executed_size   = parent_phenotype.executed_size;
@@ -198,6 +199,7 @@
 {
   // Setup reasonable initial values injected organism...
   merit           = _length;
+  energy_store    = 0.0;  //TODO
   genome_length   = _length;
   copied_size     = _length;
   executed_size   = _length;
@@ -289,12 +291,8 @@
     merit = cur_merit_base * cur_bonus;
   }
   
-/*  double init_energy_give = m_world->GetConfig().INITITAL_ENERGY_GIVEN.Get()
-  if(init_energy_give >= 0.0) {  
-    // adjust energy
-    TODO : offspring energy depends on config file.
-    energy_store = cur_energy_bonus + init_energy_give;
-  }*/
+  // update energy store
+  energy_store += cur_energy_bonus;
   
   genome_length   = _length;
   (void) copied_size;          // Unchanged
@@ -306,7 +304,7 @@
   // Lock in cur values as last values.
   last_merit_base     = cur_merit_base;
   last_bonus          = cur_bonus;
-  last_energy         = cur_energy_bonus;
+//TODO?  last_energy         = cur_energy_bonus;
   last_num_errors     = cur_num_errors;
   last_num_donates    = cur_num_donates;
   last_task_count     = cur_task_count;
@@ -600,7 +598,7 @@
   cur_bonus += result.GetAddBonus();
   
   // Update the energy bonus
-  cur_energy_bonus += result.GetAddEnergy()
+  cur_energy_bonus += result.GetAddEnergy();
   
   // Denote consumed resources...
   for (int i = 0; i < res_in.GetSize(); i++) {

Modified: branches/energy/source/main/cPhenotype.h
===================================================================
--- branches/energy/source/main/cPhenotype.h	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/main/cPhenotype.h	2007-02-28 14:47:35 UTC (rev 1378)
@@ -278,7 +278,8 @@
 
   ////////////////////  Accessors -- Modifying  ///////////////////
   void SetMerit(const cMerit& in_merit) { merit = in_merit; }
-  void ReduceEnergy(const int cost) { energy_store -= cost; }
+  void ReduceEnergy(const double cost) { energy_store -= cost; }
+  void SetEnergy(const double value) { energy_store = value; }
   void SetGestationTime(int in_time) { gestation_time = in_time; }
   void SetTimeUsed(int in_time) { time_used = in_time; }
   void SetFault(const cString& in_fault) { fault_desc = in_fault; }

Modified: branches/energy/source/main/cPopulation.cc
===================================================================
--- branches/energy/source/main/cPopulation.cc	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/main/cPopulation.cc	2007-02-28 14:47:35 UTC (rev 1378)
@@ -290,6 +290,11 @@
   tArray<cOrganism*> child_array;
   tArray<cMerit> merit_array;
   
+  //for energy model
+  int init_energy_given = m_world->GetConfig().INITIAL_ENERGY_GIVEN.Get();
+  int inst_2_exc = m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get();
+
+  
   // Update the parent's phenotype.
   // This needs to be done before the parent goes into the brith chamber
   // or the merit doesn't get passed onto the child correctly
@@ -327,10 +332,25 @@
       }    
     }
     
-    // Update the phenotypes of each child....   TODO
+    // Update the phenotypes of each child....
     const int child_length = child_array[i]->GetGenome().GetSize();
     child_array[i]->GetPhenotype().SetupOffspring(parent_phenotype,child_length);
     
+    //TODO -- sexual replication
+    //TODO -- check for correctness
+    //TODO - find where merit is set for orginal ansestor and set (1 = merit) * NUM_INST_2_EXC_BEFORE_0 * AvgCostPerInst = energy_store
+    if(m_world->GetConfig().ENERGY_ENABLED.Get()) {
+      // adjust energy in parent
+//      double avg_cost_per_inst = m_world->GetHardwareManager().GetInstSet().GetAvgEnergyCostPerInst();  //should be total_energy/size of instruction set
+      //cerr<<avg_cost_per_inst <<endl;
+      double child_energy = child_array[i]->GetPhenotype().GetStoredEnergy();
+      
+      parent_phenotype.ReduceEnergy(child_energy - init_energy_given);  // trying to avoid floating point error
+      //TODO the average cost per instruction varies depending on the merit.
+      merit_array[i] = 100 * child_energy / (inst_2_exc); //TODO - make sure that instruction cost = merit * inst_cost (check)
+      cerr<<"child merit: "<<merit_array[i]<<endl<<"child energy: "<< child_energy <<endl;
+    }
+
     child_array[i]->GetPhenotype().SetMerit(merit_array[i]);
     
     // Do lineage tracking for the new organisms.
@@ -339,6 +359,14 @@
     
   }
   
+  //TODO set parents merit
+  // SEXUAL
+  if(m_world->GetConfig().ENERGY_ENABLED.Get()) {
+    parent_phenotype.ReduceEnergy(-1.0*m_world->GetConfig().INITIAL_ENERGY_GIVEN.Get());
+    cMerit parentMerit = cMerit(100 * parent_phenotype.GetStoredEnergy()/(inst_2_exc));
+    parent_phenotype.SetMerit(parentMerit);
+    cerr<<"parent merit: "<<parent_phenotype.GetMerit()<<endl<<"parent energy: "<< parent_phenotype.GetStoredEnergy() <<endl;
+  }
   
   // If we're not about to kill the parent, do some extra work on it.
   if (parent_alive == true) {
@@ -2016,7 +2044,7 @@
   InjectGenome(cell_id, genome, lineage_label);
   cPhenotype& phenotype = GetCell(cell_id).GetOrganism()->GetPhenotype();
   phenotype.SetNeutralMetric(neutral);
-  
+    
   if (merit > 0) phenotype.SetMerit(cMerit(merit));
   schedule->Adjust(cell_id, phenotype.GetMerit());
   
@@ -2202,9 +2230,21 @@
   
   // Setup the phenotype...
   cPhenotype & phenotype = new_organism->GetPhenotype();
-  phenotype.SetupInject(new_genotype->GetLength());
-  phenotype.SetMerit( cMerit(new_genotype->GetTestMerit(ctx)) );
+  phenotype.SetupInject(new_genotype->GetLength());  //TODO  sets merit to lenght of genotype
   
+  //TODO -- add energy here
+  double initial_energy = m_world->GetConfig().INITIAL_ENERGY_GIVEN.Get(); // PUT INSIDE IF
+  if(m_world->GetConfig().ENERGY_ENABLED.Get()) { //TODO -- check for correctness
+//    int inst_to_exc = m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get();
+//    double avg_energy_cost_per_inst = m_world->GetHardwareManager().GetInstSet().GetAvgEnergyCostPerInst();
+    phenotype.SetEnergy(initial_energy);
+//    phenotype.SetMerit(cMerit(1.0));    //merit = 1;
+  }
+  //else {
+    phenotype.SetMerit( cMerit(new_genotype->GetTestMerit(ctx)) );
+  //}
+  cerr<<"initial energy: " <<initial_energy<<endl<<"initial Merit: "<<phenotype.GetMerit().GetDouble()<<endl;
+  
   // @CAO are these really needed?
   phenotype.SetLinesCopied( new_genotype->GetTestCopiedSize(ctx) );
   phenotype.SetLinesExecuted( new_genotype->GetTestExecutedSize(ctx) );

Modified: branches/energy/source/main/cPopulation.h
===================================================================
--- branches/energy/source/main/cPopulation.h	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/main/cPopulation.h	2007-02-28 14:47:35 UTC (rev 1378)
@@ -62,6 +62,7 @@
 # endif
 #endif
 
+#include "cInstSet.h"
 
 class cAvidaContext;
 class cCodeLabel;

Modified: branches/energy/source/targets/avida-viewer/cZoomScreen.cc
===================================================================
--- branches/energy/source/targets/avida-viewer/cZoomScreen.cc	2007-02-27 21:01:37 UTC (rev 1377)
+++ branches/energy/source/targets/avida-viewer/cZoomScreen.cc	2007-02-28 14:47:35 UTC (rev 1378)
@@ -99,11 +99,12 @@
   
   Print(5, 0, "Fitness...:");
   Print(6, 0, "Gestation.:");
-  Print(7, 0, "CPU Speed.:");
-  Print(8, 0, "Cur Merit.:");
-  Print(9, 0, "GenomeSize:");
-  Print(10, 0, "Mem Size..:");
-  Print(11, 0, "Faults....:");
+  Print(7, 0, "Cur Energy:");
+  Print(8, 0, "CPU Speed.:");
+  Print(9, 0, "Cur Merit.:");
+  Print(10, 0, "GenomeSize:");
+  Print(11, 0, "Mem Size..:");
+  Print(12, 0, "Faults....:");
   
   Print(1,  27, "Location..:");
   
@@ -237,7 +238,7 @@
   Print(13, 52, "Location.....:");
   Print(14, 52, "Genotype ID..:");
   Print(15, 52, "Genotype Name:");
-  
+  Print(16, 52, "Cur Energy...:");  
   Print(17, 52, "Faults.......:");
   Print(18, 52, "Offspring....:");
   Print(19, 52, "Thread.......:");
@@ -425,12 +426,13 @@
   
   PrintDouble(5, 14, phenotype.GetFitness());
   Print(6, 15, "%6d ", phenotype.GetGestationTime());
-  PrintDouble(7, 14, phenotype.GetMerit().GetDouble());
-  PrintDouble(8, 14, cur_merit.GetDouble());
-  Print(9, 15, "%6d ", genotype ? genotype->GetLength() : 0);
-  Print(10, 15, "%6d ", hardware.GetMemory().GetSize());
+  PrintDouble(7, 14, phenotype.GetStoredEnergy());
+  PrintDouble(8, 14, phenotype.GetMerit().GetDouble());
+  PrintDouble(9, 14, cur_merit.GetDouble());
+  Print(10, 15, "%6d ", genotype ? genotype->GetLength() : 0);
+  Print(11, 15, "%6d ", hardware.GetMemory().GetSize());
   
-  Print(11, 15, "%6d ", phenotype.GetCurNumErrors());
+  Print(12, 15, "%6d ", phenotype.GetCurNumErrors());
   
   Print(4, 39, "%9d ", phenotype.GetGeneration());
   Print(5, 39, "%9d ", phenotype.GetAge());
@@ -599,8 +601,9 @@
   
   Print(14, 69, "%10d", info.GetActiveGenotypeID());
   Print(15, 69, "%10s", static_cast<const char*>(info.GetActiveName()));
-  
+
   cPhenotype& phenotype = info.GetActiveCell()->GetOrganism()->GetPhenotype();
+  PrintDouble(16, 69, phenotype.GetStoredEnergy());
   Print(17, 69, "%10d", phenotype.GetCurNumErrors());
   Print(18, 69, "%10d", phenotype.GetNumDivides());
   if (info.GetThreadLock() != -1) Print(19, 67, "LOCKED");




More information about the Avida-cvs mailing list