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

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Mon Mar 19 08:59:17 PDT 2007


Author: beckma24
Date: 2007-03-19 11:59:16 -0400 (Mon, 19 Mar 2007)
New Revision: 1409

Modified:
   branches/energy/source/actions/EnvironmentActions.cc
   branches/energy/source/cpu/cHardwareCPU.cc
   branches/energy/source/cpu/cHardwareCPU.h
   branches/energy/source/main/cPopulation.cc
   branches/energy/source/main/cPopulation.h
Log:
Added sleep instruction tracking

Modified: branches/energy/source/actions/EnvironmentActions.cc
===================================================================
--- branches/energy/source/actions/EnvironmentActions.cc	2007-03-19 02:18:38 UTC (rev 1408)
+++ branches/energy/source/actions/EnvironmentActions.cc	2007-03-19 15:59:16 UTC (rev 1409)
@@ -305,7 +305,45 @@
   }
 };
 
+/*
+class cActionSetDoublePeriodicResource : public cAction
+{
+private:
+  cString m_res_name;
+  double m_res_count;
+  double amplitude;
+  double frequency;
+  double phaseShift;
+  double initY;
 
+public:
+  cActionSetDoublePeriodicResource(cWorld* world, const cString& args): cAction(world, args), m_res_name(""), amplitude(1.0),
+                                                                  frequency(1.0), phaseShift(0.0), initY(0.0)
+  {
+    cString largs(args);
+    if (largs.GetSize()) m_res_name = largs.PopWord();
+    if (largs.GetSize()) amplitude = largs.PopWord().AsDouble();
+    if (largs.GetSize()) frequency = largs.PopWord().AsDouble();
+    if (largs.GetSize()) phaseShift = largs.PopWord().AsDouble();
+    if (largs.GetSize()) initY = largs.PopWord().AsDouble();
+  }
+  
+  static const cString GetDescription() { return "Arguments: <string reaction_name> <string amplitude> <string pi/frequence> <phaseShift*pi> <string initial_Y>"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    int time = m_world->GetStats().GetUpdate();
+    m_res_count = ampliture*(sin(pi/frequency1*x-pi*phaseShift1)+1+cos(pi/frequency2*x-pi*phaseShift1)+1)/4;
+    
+    std::cout << "Update " << time << " Y = " << m_res_count << std::endl;
+//    std::cout << m_res_count <<" = " << amplitude <<" * sin("<<frequency <<" * " << time <<" - "<< phaseShift<<") + "<<initY<<std::endl;
+    cResource* res = m_world->GetEnvironment().GetResourceLib().GetResource(m_res_name);
+    if (res != NULL) m_world->GetPopulation().SetResource(res->GetID(), m_res_count);
+
+  }
+};
+*/
+
 void RegisterEnvironmentActions(cActionLibrary* action_lib)
 {
   action_lib->Register<cActionInjectResource>("InjectResource");

Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc	2007-03-19 02:18:38 UTC (rev 1408)
+++ branches/energy/source/cpu/cHardwareCPU.cc	2007-03-19 15:59:16 UTC (rev 1409)
@@ -39,6 +39,7 @@
 #include "nMutation.h"
 #include "cOrganism.h"
 #include "cPhenotype.h"
+#include "cPopulation.h"
 #include "cStringUtil.h"
 #include "cTestCPU.h"
 #include "cWorldDriver.h"
@@ -353,7 +354,6 @@
 
 cHardwareCPU::cHardwareCPU(cWorld* world, cOrganism* in_organism, cInstSet* in_m_inst_set)
 : cHardwareBase(world, in_organism, in_m_inst_set)
-, sleep(0)
 {
   /* FIXME:  reorganize storage of m_functions.  -- kgn */
   m_functions = s_inst_slib->GetFunctions();
@@ -379,7 +379,6 @@
 , inst_ft_cost(hardware_cpu.inst_ft_cost)
 , inst_energy_cost(hardware_cpu.inst_energy_cost)
 #endif
-, sleep(0)
 {
 }
 
@@ -525,10 +524,18 @@
   
   // check avaliable energy first
   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.
+    
+      inst_energy_cost[cur_inst.GetOp()] = 0;
+      //subtract energy used from current org energy.
       organism->GetPhenotype().ReduceEnergy(energy_req);
+      
+      if( m_world->GetHardwareManager().GetInstSet().GetName(cur_inst) == cString("sleep"))
+        m_world->GetPopulation().AddBeginSleep(organism->GetCellID(),m_world->GetStats().GetUpdate());
+      
     } else {
       // not enough energy
       return false;
@@ -552,6 +559,7 @@
     }
   }
   
+  inst_energy_cost[cur_inst.GetOp()] = m_inst_set->GetEnergyCost(cur_inst); //reset instruction energy cost
 #endif
   return true;
 }
@@ -3605,7 +3613,7 @@
 }
 
 bool cHardwareCPU::Inst_Sleep(cAvidaContext& ctx) {
-  sleep++;
+  m_world->GetPopulation().AddEndSleep(organism->GetCellID(),m_world->GetStats().GetUpdate());
   return true;
 }
 

Modified: branches/energy/source/cpu/cHardwareCPU.h
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.h	2007-03-19 02:18:38 UTC (rev 1408)
+++ branches/energy/source/cpu/cHardwareCPU.h	2007-03-19 15:59:16 UTC (rev 1409)
@@ -144,8 +144,6 @@
   tArray<int> inst_ft_cost;
   tArray<int> inst_energy_cost;
 #endif
-
-  int sleep;
   
   
   bool SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst);

Modified: branches/energy/source/main/cPopulation.cc
===================================================================
--- branches/energy/source/main/cPopulation.cc	2007-03-19 02:18:38 UTC (rev 1408)
+++ branches/energy/source/main/cPopulation.cc	2007-03-19 15:59:16 UTC (rev 1409)
@@ -95,11 +95,15 @@
   resource_count.ResizeSpatialGrids(world_x, world_y);
   market.Resize(MARKET_SIZE);
   
+  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();
+    
     // If we're working with a bounded grid, we need to take care of it.
     bool bottom_flag = true;
     bool top_flag = true;

Modified: branches/energy/source/main/cPopulation.h
===================================================================
--- branches/energy/source/main/cPopulation.h	2007-03-19 02:18:38 UTC (rev 1408)
+++ branches/energy/source/main/cPopulation.h	2007-03-19 15:59:16 UTC (rev 1409)
@@ -91,6 +91,8 @@
   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;
+  
   // Data Tracking...
   tList<cPopulationCell> reaper_queue; // Death order in some mass-action runs
 
@@ -219,6 +221,18 @@
 
   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;
+ */ }
 };
 
 




More information about the Avida-cvs mailing list