[Avida-SVN] r1413 - in branches/energy: documentation source/actions source/cpu source/main

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Tue Mar 20 12:36:01 PDT 2007


Author: beckma24
Date: 2007-03-20 15:36:01 -0400 (Tue, 20 Mar 2007)
New Revision: 1413

Modified:
   branches/energy/documentation/inst_set.html
   branches/energy/source/actions/PrintActions.cc
   branches/energy/source/cpu/cHardwareCPU.cc
   branches/energy/source/cpu/cHardwareManager.cc
   branches/energy/source/main/cAvidaConfig.h
   branches/energy/source/main/cPhenotype.cc
   branches/energy/source/main/cPopulation.cc
   branches/energy/source/main/cPopulation.h
Log:
Added event to log sleep stats.
Added energy decay.
Fixed consistency test problem.

Modified: branches/energy/documentation/inst_set.html
===================================================================
--- branches/energy/documentation/inst_set.html	2007-03-20 16:23:02 UTC (rev 1412)
+++ branches/energy/documentation/inst_set.html	2007-03-20 19:36:01 UTC (rev 1413)
@@ -5,9 +5,7 @@
 </head>
 <body>
 
-<div class="revision">
-Revised 2006-09-05 DMB
-</div>
+<div class="revision">Revised 2007-03-20 BEB</div>
 
 <p><a href="index.html">Return to the Index</a></p>
 <hr />
@@ -22,7 +20,7 @@
 define how that instruction should be used.  The exact format is as follows:
 </p>
 <p>
-<code>inst-name redundancy cost ft_cost prob_fail</code>
+<code>inst-name redundancy cost ft_cost energy_cost prob_fail</code>
 </p>
 
 
@@ -50,13 +48,17 @@
   executed.  This is used to lower the diversity of instructions 
   inside an organism.  The default value here is 0.
 </dd>
-<dt><strong>prob_fail</strong></dt>
+<dt><strong>energy_cost</strong></dt>
 <dd>
+  The number of Energy units required to execute this instruction. Zero is the default if this value is not specified.
+</dd>
+<dt><strong>prob_fail</strong>	
+<dd>
   The probability of this instruction not working properly.  If an
   instruction fails it will simply do nothing, but still cost the
   CPU cycles to execute.  The defailt probability of failure is zero.
 </dd>
-</dl>
+		</dl>
 
 <p>
 Normally only the first column of numbers is used in the file.

Modified: branches/energy/source/actions/PrintActions.cc
===================================================================
--- branches/energy/source/actions/PrintActions.cc	2007-03-20 16:23:02 UTC (rev 1412)
+++ branches/energy/source/actions/PrintActions.cc	2007-03-20 19:36:01 UTC (rev 1413)
@@ -81,7 +81,6 @@
 STATS_OUT_FILE(PrintSenseData,         sense.dat           );
 STATS_OUT_FILE(PrintSenseExeData,      sense_exe.dat       );
 
-
 #define POP_OUT_FILE(METHOD, DEFAULT)                                                     /*  1 */ \
 class cAction ## METHOD : public cAction {                                                /*  2 */ \
 private:                                                                                  /*  3 */ \
@@ -275,7 +274,41 @@
   }
 };
 
+/********* BEGIN ********/
+class cActionPrintSleepData : public cAction
+{
+private:
+  cString m_filename;
+public:
+  cActionPrintSleepData(cWorld* world, const cString& args) : cAction(world, args) { }
+  
+  static const cString GetDescription() { return "Arguments: [string fname=\"sleep.dat\"]"; }
+  
+  void Process(cAvidaContext& ctx)
+  {    
+    int x = m_world->GetConfig().WORLD_X.Get();
+    int y = m_world->GetConfig().WORLD_Y.Get();
+    m_filename.Set("sleep.%d.dat", m_world->GetStats().GetUpdate());
+    cDataFile& df = m_world->GetDataFile(m_filename);
+    
+    df.WriteComment("Cell sleep log");
+    df.WriteTimeStamp();
 
+    for(int i = 0; i < x*y; i++) {
+      tVector<pair<int,int> > cell_log = m_world->GetPopulation().getCellLog(i);
+      
+      for(int j = 0; j < cell_log.Size(); j++) {
+        df.Write(i, "Cell");
+        df.Write(cell_log[j].first, "Sleep start time");
+        df.Write(cell_log[j].second, "Sleep stop time");
+        df.Endl();
+      }
+    }
+    df.Flush();
+  }
+};
+/********END**********/
+
 class cActionPrintSpeciesAbundanceHistogram : public cAction
 {
 private:
@@ -1544,8 +1577,9 @@
   // Print Settings
   action_lib->Register<cActionSetVerbose>("SetVerbose");
   
+  // Print Sleep
+  action_lib->Register<cActionPrintSleepData>("PrintSleepLog");
 
-
   // @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.
   action_lib->Register<cActionPrintAverageData>("print_average_data");
   action_lib->Register<cActionPrintErrorData>("print_error_data");

Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc	2007-03-20 16:23:02 UTC (rev 1412)
+++ branches/energy/source/cpu/cHardwareCPU.cc	2007-03-20 19:36:01 UTC (rev 1413)
@@ -523,7 +523,7 @@
   assert(cur_inst.GetOp() < inst_cost.GetSize());
   
   // check avaliable energy first
-  double energy_req = m_inst_set->GetEnergyCost(cur_inst) * (organism->GetPhenotype().GetMerit().GetDouble() / 100.0); //compensate by factor of 100
+  double energy_req = inst_energy_cost[cur_inst.GetOp()] * (organism->GetPhenotype().GetMerit().GetDouble() / 100.0); //compensate by factor of 100
   
   if(energy_req > 0.0) {
   
@@ -3613,7 +3613,7 @@
 }
 
 bool cHardwareCPU::Inst_Sleep(cAvidaContext& ctx) {
-  m_world->GetPopulation().AddEndSleep(organism->GetCellID(),m_world->GetStats().GetUpdate());
+  m_world->GetPopulation().AddEndSleep(organism->GetCellID(), m_world->GetStats().GetUpdate());
   return true;
 }
 

Modified: branches/energy/source/cpu/cHardwareManager.cc
===================================================================
--- branches/energy/source/cpu/cHardwareManager.cc	2007-03-20 16:23:02 UTC (rev 1412)
+++ branches/energy/source/cpu/cHardwareManager.cc	2007-03-20 19:36:01 UTC (rev 1413)
@@ -88,7 +88,6 @@
     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/main/cAvidaConfig.h
===================================================================
--- branches/energy/source/main/cAvidaConfig.h	2007-03-20 16:23:02 UTC (rev 1412)
+++ branches/energy/source/main/cAvidaConfig.h	2007-03-20 19:36:01 UTC (rev 1413)
@@ -323,10 +323,11 @@
   CONFIG_ADD_GROUP(ENERGY_GROUP, "Energy Settings");
   CONFIG_ADD_VAR(ENERGY_ENABLED, bool, 0, "Enable Energy Model. 0/1 (off/on)");
   CONFIG_ADD_VAR(ENERGY_GIVEN_ON_INJECT, int, 0, "Energy given to organism upon injection.");
-  CONFIG_ADD_VAR(FRAC_ENERGY_GIVEN, double, 0.5, "Fraction of energy given to offspring.");
   CONFIG_ADD_VAR(ENERGY_GIVEN_AT_BIRTH, int, 0, "Energy given to offspring upon birth.");
+  CONFIG_ADD_VAR(FRAC_PARENT_ENERGY_GIVEN_AT_BIRTH, double, 0.5, "Fraction of energy given to offspring.");
+  CONFIG_ADD_VAR(FRAC_ENERGY_DECAY_AT_BIRTH, double, 0.0, "Fraction of energy lost due to decay during reproduction.");
   CONFIG_ADD_VAR(NUM_INST_EXC_BEFORE_0_ENERGY, int, 0, "Number of instructions executed before energy is exhausted.");
-  CONFIG_ADD_VAR(FIX_METABOLIC_RATE, bool, 0, "When activated the metabolic rate of all orgiansims are equal. 0/1 (off/on)"); // TODO - check for correctness
+//  CONFIG_ADD_VAR(FIX_METABOLIC_RATE, bool, 0, "When activated the metabolic rate of all orgiansims are equal. 0/1 (off/on)"); // TODO - check for correctness
   CONFIG_ADD_VAR(ENERGY_VERBOSE, bool, 0, "Print energy and merit values. 0/1 (off/on)");
 
 

Modified: branches/energy/source/main/cPhenotype.cc
===================================================================
--- branches/energy/source/main/cPhenotype.cc	2007-03-20 16:23:02 UTC (rev 1412)
+++ branches/energy/source/main/cPhenotype.cc	2007-03-20 19:36:01 UTC (rev 1413)
@@ -105,7 +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().ENERGY_GIVEN_AT_BIRTH.Get();
+  energy_store    = (parent_phenotype.energy_store * m_world->GetConfig().FRAC_PARENT_ENERGY_GIVEN_AT_BIRTH.Get())+m_world->GetConfig().ENERGY_GIVEN_AT_BIRTH.Get();
   genome_length   = _length;
   copied_size     = parent_phenotype.child_copied_size;
   executed_size   = parent_phenotype.executed_size;

Modified: branches/energy/source/main/cPopulation.cc
===================================================================
--- branches/energy/source/main/cPopulation.cc	2007-03-20 16:23:02 UTC (rev 1412)
+++ branches/energy/source/main/cPopulation.cc	2007-03-20 19:36:01 UTC (rev 1413)
@@ -95,14 +95,14 @@
   resource_count.ResizeSpatialGrids(world_x, world_y);
   market.Resize(MARKET_SIZE);
   
-  sleep_log.Resize(num_cells);
+  //sleep_log.Resize(num_cells);
   
   for (int cell_id = 0; cell_id < num_cells; cell_id++) {
     int x = cell_id % world_x;
     int y = cell_id / world_x;
     cell_array[cell_id].Setup(world, cell_id, environment.GetMutRates());
     
-    sleep_log[cell_id].Clear();
+//    sleep_log[cell_id] = tList<pair<int,int> >();
     
     // If we're working with a bounded grid, we need to take care of it.
     bool bottom_flag = true;
@@ -338,19 +338,17 @@
     
     // Update the phenotypes of each child....
     const int child_length = child_array[i]->GetGenome().GetSize();
+    //decay of energy
+    parent_phenotype.ReduceEnergy(parent_phenotype.GetStoredEnergy() * (1 - m_world->GetConfig().FRAC_ENERGY_DECAY_AT_BIRTH.Get()));
     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()) {
+      double child_energy = child_array[i]->GetPhenotype().GetStoredEnergy();
       // 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
       
-      parent_phenotype.ReduceEnergy(child_energy - init_energy_given);  // trying to avoid floating point error
-
 //      if(m_world->GetConfig().FIX_METABOLIC_RATE.Get()) {  //TODO check correctness
 //        merit_array[i] = parent_phenotype.GetMerit();
 //      } else {
@@ -368,8 +366,7 @@
     
   }
   
-  //TODO set parents merit
-  // SEXUAL
+  // TODO: SEXUAL
   if(m_world->GetConfig().ENERGY_ENABLED.Get()) {
     parent_phenotype.ReduceEnergy(-1.0*init_energy_given);
     
@@ -2245,9 +2242,9 @@
   cPhenotype & phenotype = new_organism->GetPhenotype();
   phenotype.SetupInject(new_genotype->GetLength());  //TODO  sets merit to lenght of genotype
   
-  //TODO -- add energy here
-  double initial_energy = m_world->GetConfig().ENERGY_GIVEN_ON_INJECT.Get(); // PUT INSIDE IF
   if(m_world->GetConfig().ENERGY_ENABLED.Get()) { //TODO -- check for correctness
+    //TODO -- add energy here
+    double initial_energy = m_world->GetConfig().ENERGY_GIVEN_ON_INJECT.Get(); // PUT INSIDE IF
     phenotype.SetEnergy(initial_energy);
   }
   // BB - Don't need to fix metabolic rate here, only on birth
@@ -2457,3 +2454,13 @@
 cChangeList *cPopulation::GetChangeList(){
   return schedule->GetChangeList();
 }
+
+void cPopulation::AddBeginSleep(int cellID, int start_time) {
+  sleep_log[cellID].Add(make_pair(start_time,-1));
+}
+  
+void cPopulation::AddEndSleep(int cellID, int end_time) {
+  pair<int,int> p = sleep_log[cellID][sleep_log[cellID].Size()-1];
+  sleep_log[cellID].RemoveAt(sleep_log[cellID].Size()-1);
+  sleep_log[cellID].Add(make_pair(p.first, end_time));
+}

Modified: branches/energy/source/main/cPopulation.h
===================================================================
--- branches/energy/source/main/cPopulation.h	2007-03-20 16:23:02 UTC (rev 1412)
+++ branches/energy/source/main/cPopulation.h	2007-03-20 19:36:01 UTC (rev 1413)
@@ -55,6 +55,9 @@
 #ifndef tList_h
 #include "tList.h"
 #endif
+#ifndef tVector_h
+#include "tVector.h"
+#endif
 
 #if USE_tMemTrack
 # ifndef tMemTrack_h
@@ -91,7 +94,7 @@
   cBirthChamber birth_chamber;         // Global birth chamber.
   tArray<tList<cSaleItem> > market;   // list of lists of items for sale, each list goes with 1 label
 
-  tArray<tList<pair<int,int> > > sleep_log;
+  tVector<pair<int,int> > sleep_log[2500];
   
   // Data Tracking...
   tList<cPopulationCell> reaper_queue; // Death order in some mass-action runs
@@ -222,17 +225,10 @@
   void SetChangeList(cChangeList* change_list);
   cChangeList* GetChangeList();
   
-  void AddBeginSleep(int cellID, int start_time) {
-    pair<int,int> *p  = new pair<int,int>(start_time,0);
-    sleep_log[cellID].PushRear(p);
-  }
-  void AddEndSleep(int cellID, int end_time) {
-    pair<int,int> *p = sleep_log[cellID].GetLast();
-    p->second = end_time;
-  /*  pair<int,int> *q = new pair<int,int>(start, end_time);
-    sleep_log[cellID].PushRear(q);
-    delete p;
- */ }
+  void AddBeginSleep(int cellID, int start_time);
+  void AddEndSleep(int cellID, int end_time);
+ 
+  tVector<pair<int,int> > getCellLog(int i) { return sleep_log[i]; }
 };
 
 




More information about the Avida-cvs mailing list