[Avida-SVN] r1478 - in branches/energy: Avida.xcodeproj source/actions source/cpu source/main

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Tue Apr 17 10:23:04 PDT 2007


Author: beckma24
Date: 2007-04-17 13:23:04 -0400 (Tue, 17 Apr 2007)
New Revision: 1478

Modified:
   branches/energy/Avida.xcodeproj/project.pbxproj
   branches/energy/source/actions/EnvironmentActions.cc
   branches/energy/source/cpu/cHardwareCPU.cc
   branches/energy/source/main/cAvidaConfig.h
   branches/energy/source/main/cOrganism.cc
   branches/energy/source/main/cPhenotype.cc
   branches/energy/source/main/cPhenotype.h
   branches/energy/source/main/cPopulation.cc
Log:
Added event to change "NumInstBefore0Energy" within the energy model.

Added APPLY_ENERGY_METHOD config option.  Now energy can be applied to and organisms energy store and metabolic rate on divide, completion of a task, or when the sleep instruction is executed.



Modified: branches/energy/Avida.xcodeproj/project.pbxproj
===================================================================
--- branches/energy/Avida.xcodeproj/project.pbxproj	2007-04-17 16:36:39 UTC (rev 1477)
+++ branches/energy/Avida.xcodeproj/project.pbxproj	2007-04-17 17:23:04 UTC (rev 1478)
@@ -887,7 +887,7 @@
 		70DCAC77097AF730002F8733 /* key_chart */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = key_chart; sourceTree = "<group>"; };
 		70DCAC78097AF730002F8733 /* viewer.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = viewer.cc; sourceTree = "<group>"; };
 		70DCAC9B097AF7C0002F8733 /* primitive.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = primitive.cc; sourceTree = "<group>"; };
-		70DCAD1C097AF7CC002F8733 /* avida-s */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = "avida-s"; sourceTree = BUILT_PRODUCTS_DIR; };
+		70DCAD1C097AF7CC002F8733 /* avida-s */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "avida-s"; sourceTree = BUILT_PRODUCTS_DIR; };
 		70DCAD1F097AF81A002F8733 /* AvidaScript.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = AvidaScript.h; sourceTree = "<group>"; };
 		70DCAD20097AF81A002F8733 /* cLexer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cLexer.h; sourceTree = "<group>"; };
 		70DCAD21097AF81A002F8733 /* cLexer.l */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.lex; path = cLexer.l; sourceTree = "<group>"; };

Modified: branches/energy/source/actions/EnvironmentActions.cc
===================================================================
--- branches/energy/source/actions/EnvironmentActions.cc	2007-04-17 16:36:39 UTC (rev 1477)
+++ branches/energy/source/actions/EnvironmentActions.cc	2007-04-17 17:23:04 UTC (rev 1478)
@@ -305,6 +305,26 @@
   }
 };
 
+class cActionSetNumInstBefore0Energy : public cAction
+{
+private:
+  int newValue;
+
+public:
+  cActionSetNumInstBefore0Energy(cWorld* world, const cString& args) : cAction(world, args), newValue(0)
+  {
+    cString largs(args);
+    if (largs.GetSize()) newValue = largs.PopWord().AsInt();
+  }
+  
+  static const cString GetDescription() { return "Arguments: <int new_value>"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Set(newValue);
+  }
+};
+
 /*
 class cActionSetDoublePeriodicResource : public cAction
 {
@@ -352,6 +372,8 @@
   action_lib->Register<cActionSetResource>("SetResource");
   action_lib->Register<cActionSetCellResource>("SetCellResource");
   action_lib->Register<cActionSetPeriodicResource>("SetPeriodicResource");
+  action_lib->Register<cActionSetNumInstBefore0Energy>("SetNumInstBefore0Energy");
+  
 
   action_lib->Register<cActionSetReactionValue>("SetReactionValue");
   action_lib->Register<cActionSetReactionValueMult>("SetReactionValueMult");

Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc	2007-04-17 16:36:39 UTC (rev 1477)
+++ branches/energy/source/cpu/cHardwareCPU.cc	2007-04-17 17:23:04 UTC (rev 1478)
@@ -529,8 +529,8 @@
   
   // check avaliable energy first
   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) {
+  cerr << energy_req <<endl;
+  if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1 && energy_req > 0.0) {
 //  cerr<<"here";
   
     if(organism->GetPhenotype().GetStoredEnergy() >= energy_req) {
@@ -3621,6 +3621,11 @@
 
 bool cHardwareCPU::Inst_Sleep(cAvidaContext& ctx) {
   m_world->GetPopulation().AddEndSleep(organism->GetCellID(), m_world->GetStats().GetUpdate());
+  if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 2) {
+    organism->GetPhenotype().ApplyEnergy();
+    double newMerit = organism->GetPhenotype().RefreshEnergy();
+    organism->GetOrgInterface().UpdateMerit(newMerit);
+  }
   return true;
 }
 

Modified: branches/energy/source/main/cAvidaConfig.h
===================================================================
--- branches/energy/source/main/cAvidaConfig.h	2007-04-17 16:36:39 UTC (rev 1477)
+++ branches/energy/source/main/cAvidaConfig.h	2007-04-17 17:23:04 UTC (rev 1478)
@@ -327,7 +327,8 @@
   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(ENERGY_CAP, int, -1, "Maximun amount of energy that can be stored in an organism.  -1 means the cap is set to Max Int");
+  CONFIG_ADD_VAR(ENERGY_CAP, int, -1, "Maximun amount of energy that can be stored in an organism.  -1 means the cap is set to Max Int");  // TODO - is this done?
+  CONFIG_ADD_VAR(APPLY_ENERGY_METHOD, int, 0, "When should rewarded energy be applied to current energy.\n0 = on divide\n1 = on completion of task\n2 = on sleep");  
 //  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/cOrganism.cc
===================================================================
--- branches/energy/source/main/cOrganism.cc	2007-04-17 16:36:39 UTC (rev 1477)
+++ branches/energy/source/main/cOrganism.cc	2007-04-17 17:23:04 UTC (rev 1478)
@@ -209,7 +209,7 @@
   cTaskContext taskctx(m_interface, m_input_buf, m_output_buf, other_input_list, other_output_list, net_valid, 0, on_divide, received_messages_point);
   m_phenotype.TestOutput(ctx, taskctx, resource_count, res_change, insts_triggered, &  clear_input);
   
-  if(m_world->GetConfig().ENERGY_ENABLED.Get()) {
+  if(m_world->GetConfig().ENERGY_ENABLED.Get() && m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1) {
     double newMerit = m_phenotype.RefreshEnergy();
     if(newMerit != -1) {
 //      std::cerr<<GetCellID()<<" "<<newMerit<<std::endl;

Modified: branches/energy/source/main/cPhenotype.cc
===================================================================
--- branches/energy/source/main/cPhenotype.cc	2007-04-17 16:36:39 UTC (rev 1477)
+++ branches/energy/source/main/cPhenotype.cc	2007-04-17 17:23:04 UTC (rev 1478)
@@ -106,6 +106,7 @@
   // Copy divide values from parent, which should already be setup.
   merit           = parent_phenotype.merit;
   energy_store    = min((parent_phenotype.energy_store * m_world->GetConfig().FRAC_PARENT_ENERGY_GIVEN_AT_BIRTH.Get())+m_world->GetConfig().ENERGY_GIVEN_AT_BIRTH.Get(), (double) m_world->GetConfig().ENERGY_CAP.Get());
+  energy_tobe_applied = 0.0;
   genome_length   = _length;
   copied_size     = parent_phenotype.child_copied_size;
   executed_size   = parent_phenotype.executed_size;
@@ -200,6 +201,7 @@
   // Setup reasonable initial values injected organism...
   merit           = _length;
   energy_store    = min(m_world->GetConfig().ENERGY_GIVEN_ON_INJECT.Get(), m_world->GetConfig().ENERGY_CAP.Get());
+  energy_tobe_applied = 0.0;
   genome_length   = _length;
   copied_size     = _length;
   executed_size   = _length;
@@ -829,8 +831,16 @@
 
 double cPhenotype::RefreshEnergy() {
   if(cur_energy_bonus > 0) {
-    energy_store += cur_energy_bonus;
+    if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 0 || m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 2) {
+      energy_tobe_applied += cur_energy_bonus;
+    } else if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1) {
+      energy_store += cur_energy_bonus;
+    } else {
+      cerr<< "Unknown APPLY_ENERGY_METHOD value " << m_world->GetConfig().APPLY_ENERGY_METHOD.Get();
+      exit(-1);
+    }
     cur_energy_bonus = 0;
+
 //    std::cerr<<energy_store<<"/"<<m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get()<<std::endl;
     return min(100 * energy_store / (m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get()), (double) m_world->GetConfig().ENERGY_CAP.Get());
   }

Modified: branches/energy/source/main/cPhenotype.h
===================================================================
--- branches/energy/source/main/cPhenotype.h	2007-04-17 16:36:39 UTC (rev 1477)
+++ branches/energy/source/main/cPhenotype.h	2007-04-17 17:23:04 UTC (rev 1478)
@@ -88,6 +88,7 @@
   // 1. These are values calculated at the last divide (of self or offspring)
   cMerit merit;             // Relative speed of CPU
   double energy_store;      // Amount of energy.  Determines relative speed of CPU when turned on.
+  double energy_tobe_applied; //Energy that has not yet been added to energy store.
   int genome_length;        // Number of instructions in genome.
   int copied_size;          // Instructions copied into genome.
   int executed_size;        // Instructions executed from genome.
@@ -327,6 +328,7 @@
   bool& IsMultiThread() { assert(initialized == true); return is_multi_thread; }
   
   double RefreshEnergy();
+  double ApplyEnergy() { energy_store += energy_tobe_applied + cur_energy_bonus; }
 };
 
 

Modified: branches/energy/source/main/cPopulation.cc
===================================================================
--- branches/energy/source/main/cPopulation.cc	2007-04-17 16:36:39 UTC (rev 1477)
+++ branches/energy/source/main/cPopulation.cc	2007-04-17 17:23:04 UTC (rev 1478)
@@ -83,8 +83,10 @@
   if(m_world->GetConfig().ENERGY_CAP.Get() == -1)
     m_world->GetConfig().ENERGY_CAP.Set(INT_MAX);
   
-  if(m_world->GetConfig().ENERGY_ENABLED.Get()) 
+  if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1)  {
     sleep_log = new tVector<pair<int,int> >[world_x*world_y];
+    cout<<"IN\n";
+  }
   // Print out world details
   if (world->GetVerbosity() > VERBOSE_NORMAL) {
     cout << "Building world " << world_x << "x" << world_y << " = " << num_cells << " organisms." << endl;
@@ -346,6 +348,12 @@
     // Update the phenotypes of each child....
     const int child_length = child_array[i]->GetGenome().GetSize();
 
+    //apply energy if APPLY_ENERGY_METHOD is set to "no divide" (0)
+    if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 0) {
+      parent_phenotype.ApplyEnergy();
+    }
+
+
     //decay of energy
     parent_phenotype.ReduceEnergy(parent_phenotype.GetStoredEnergy() * m_world->GetConfig().FRAC_ENERGY_DECAY_AT_BIRTH.Get());
 




More information about the Avida-cvs mailing list