[Avida-SVN] r1837 - in development: Avida.xcodeproj source/cpu source/main source/tools

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Mon Jul 23 06:49:59 PDT 2007


Author: beckma24
Date: 2007-07-23 09:49:59 -0400 (Mon, 23 Jul 2007)
New Revision: 1837

Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/cpu/cHardwareBase.cc
   development/source/cpu/cHardwareBase.h
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/cpu/cHardwareExperimental.cc
   development/source/cpu/cHardwareExperimental.h
   development/source/cpu/cHardwareGX.cc
   development/source/cpu/cHardwareGX.h
   development/source/cpu/cHardwareSMT.cc
   development/source/cpu/cHardwareSMT.h
   development/source/cpu/cHardwareTransSMT.cc
   development/source/cpu/cHardwareTransSMT.h
   development/source/main/cAvidaConfig.h
   development/source/main/cBirthChamber.cc
   development/source/main/cBirthChamber.h
   development/source/main/cOrganism.cc
   development/source/main/cPhenotype.cc
   development/source/main/cPhenotype.h
   development/source/main/cPopulation.cc
   development/source/main/cResourceCount.cc
   development/source/tools/cMerit.cc
   development/source/tools/cMerit.h
Log:
Cleaned up and fixed bugs in energy model.  Added energy model to sexual replication all current hardware types. 

Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/Avida.xcodeproj/project.pbxproj	2007-07-23 13:49:59 UTC (rev 1837)
@@ -209,6 +209,23 @@
 		};
 /* End PBXBuildRule section */
 
+/* Begin PBXBuildStyle section */
+		B512934E0C4FCA11004B0E41 /* Development */ = {
+			isa = PBXBuildStyle;
+			buildSettings = {
+				COPY_PHASE_STRIP = NO;
+			};
+			name = Development;
+		};
+		B512934F0C4FCA11004B0E41 /* Deployment */ = {
+			isa = PBXBuildStyle;
+			buildSettings = {
+				COPY_PHASE_STRIP = YES;
+			};
+			name = Deployment;
+		};
+/* End PBXBuildStyle section */
+
 /* Begin PBXContainerItemProxy section */
 		56F555DA0C3B36FC00E2E929 /* PBXContainerItemProxy */ = {
 			isa = PBXContainerItemProxy;
@@ -1761,6 +1778,12 @@
 		DCC30C4D0762532C008F7A48 /* Project object */ = {
 			isa = PBXProject;
 			buildConfigurationList = 702442D70859E0B00059BD9B /* Build configuration list for PBXProject "Avida" */;
+			buildSettings = {
+			};
+			buildStyles = (
+				B512934E0C4FCA11004B0E41 /* Development */,
+				B512934F0C4FCA11004B0E41 /* Deployment */,
+			);
 			hasScannedForEncodings = 0;
 			mainGroup = DCC30C490762532C008F7A48;
 			productRefGroup = DCC3164E07626CF3008F7A48 /* Products */;

Modified: development/source/cpu/cHardwareBase.cc
===================================================================
--- development/source/cpu/cHardwareBase.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareBase.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -36,7 +36,10 @@
 #include "nMutation.h"
 #include "cOrganism.h"
 #include "cPhenotype.h"
+#include "cPopulation.h"
+#include "cPopulationCell.h"
 #include "cRandom.h"
+#include "cStats.h"
 #include "cTestCPU.h"
 #include "cWorld.h"
 #include "cWorldDriver.h"
@@ -718,3 +721,58 @@
   assert(1);
   return false;
 }
+
+// This method will test to see if all costs have been paid associated
+// with executing an instruction and only return true when that instruction
+// should proceed.
+bool cHardwareBase::SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst)
+{
+#if INSTRUCTION_COSTS
+  assert(cur_inst.GetOp() < inst_cost.GetSize());
+  
+  // 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(m_world->GetConfig().ENERGY_ENABLED.Get() > 0 && energy_req > 0.0) {
+    if(organism->GetPhenotype().GetStoredEnergy() >= energy_req) {
+      inst_energy_cost[cur_inst.GetOp()] = 0;
+      //subtract energy used from current org energy.
+      organism->GetPhenotype().ReduceEnergy(energy_req);  
+      
+      // tracking sleeping organisms
+      cString instName = m_world->GetHardwareManager().GetInstSet().GetName(cur_inst);
+      int cellID = organism->GetCellID();
+      if( instName == cString("sleep") || instName == cString("sleep1") || instName == cString("sleep2") ||
+          instName == cString("sleep3") || instName == cString("sleep4")) {
+        cPopulation& pop = m_world->GetPopulation();
+        if(m_world->GetConfig().LOG_SLEEP_TIMES.Get() == 1) {
+          pop.AddBeginSleep(cellID,m_world->GetStats().GetUpdate());
+        }
+        pop.GetCell(cellID).GetOrganism()->SetSleeping(true);
+        pop.incNumAsleep();
+      }
+    } else { // not enough energy
+      return false;
+    }
+  }
+  
+  // If first time cost hasn't been paid off...
+  if (m_has_ft_costs && inst_ft_cost[cur_inst.GetOp()] > 0) {
+    inst_ft_cost[cur_inst.GetOp()]--;       // dec cost
+    return false;
+  }
+  
+  // Next, look at the per use cost
+  if (m_has_costs && m_inst_set->GetCost(cur_inst) > 0) {
+    if (inst_cost[cur_inst.GetOp()] > 1) {  // if isn't paid off (>1)
+      inst_cost[cur_inst.GetOp()]--;        // dec cost
+      return false;
+    } else {                                // else, reset cost array
+      inst_cost[cur_inst.GetOp()] = m_inst_set->GetCost(cur_inst);
+    }
+  }
+  
+  inst_energy_cost[cur_inst.GetOp()] = m_inst_set->GetEnergyCost(cur_inst); //reset instruction energy cost
+#endif
+  return true;
+}

Modified: development/source/cpu/cHardwareBase.h
===================================================================
--- development/source/cpu/cHardwareBase.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareBase.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -58,6 +58,16 @@
   cInstSet* m_inst_set;      // Instruction set being used.
   cHardwareTracer* m_tracer; // Set this if you want execution traced.
 
+  // Instruction costs...
+//#if INSTRUCTION_COSTS
+  tArray<int> inst_cost;
+  tArray<int> inst_ft_cost;
+  tArray<int> inst_energy_cost;
+  bool m_has_costs;
+  bool m_has_ft_costs;
+  bool m_has_energy_costs;
+//#endif
+
   virtual int GetExecutedSize(const int parent_size);
   virtual int GetCopiedSize(const int parent_size, const int child_size) = 0;  
   
@@ -94,6 +104,7 @@
   // --------  Core Functionality  --------
   virtual void Reset() = 0;
   virtual void SingleProcess(cAvidaContext& ctx) = 0;
+  virtual bool SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst);  //should be protected
   virtual void ProcessBonusInst(cAvidaContext& ctx, const cInstruction& inst) = 0;
   
   

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareCPU.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -403,15 +403,16 @@
 , m_mal_active(hardware_cpu.m_mal_active)
 , m_advance_ip(hardware_cpu.m_advance_ip)
 , m_executedmatchstrings(hardware_cpu.m_executedmatchstrings)
+{
 #if INSTRUCTION_COSTS
-, inst_cost(hardware_cpu.inst_cost)
-, inst_ft_cost(hardware_cpu.inst_ft_cost)
-, inst_energy_cost(hardware_cpu.inst_energy_cost)
-, m_has_costs(hardware_cpu.m_has_costs)
-, m_has_ft_costs(hardware_cpu.m_has_ft_costs)
-  // TODO - m_has_energy_costs
+  inst_cost = hardware_cpu.inst_cost;
+  inst_ft_cost = hardware_cpu.inst_ft_cost;
+  inst_energy_cost = hardware_cpu.inst_energy_cost;
+  m_has_costs = hardware_cpu.m_has_costs;
+  m_has_ft_costs = hardware_cpu.m_has_ft_costs;
+  m_has_energy_costs = hardware_cpu.m_has_energy_costs;
 #endif
-{
+
 }
 
 
@@ -438,7 +439,7 @@
   inst_energy_cost.Resize(num_inst_cost);
   m_has_costs = false;
   m_has_ft_costs = false;
-  // TODO - m_has_energy_costs
+  m_has_energy_costs = false;
   
   for (int i = 0; i < num_inst_cost; i++) {
     inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
@@ -448,10 +449,9 @@
     if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
 
     inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));    
-    // TODO - m_has_energy_costs  if()
+    if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
   }
-#endif 
-  
+#endif   
 }
 
 void cHardwareCPU::cLocalThread::operator=(const cLocalThread& in_thread)
@@ -579,65 +579,6 @@
   CheckImplicitRepro(ctx);
 }
 
-
-// This method will test to see if all costs have been paid associated
-// with executing an instruction and only return true when that instruction
-// should proceed.
-bool cHardwareCPU::SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst)
-{
-#if INSTRUCTION_COSTS
-  assert(cur_inst.GetOp() < inst_cost.GetSize());
-  
-  // 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(m_world->GetConfig().ENERGY_ENABLED.Get() == 1 && energy_req > 0.0) {
-    if(organism->GetPhenotype().GetStoredEnergy() >= energy_req) {
-      inst_energy_cost[cur_inst.GetOp()] = 0;
-      //subtract energy used from current org energy.
-      organism->GetPhenotype().ReduceEnergy(energy_req);  
-    
-    
-    // tracking sleeping organisms
-  cString instName = m_world->GetHardwareManager().GetInstSet().GetName(cur_inst);
-  int cellID = organism->GetCellID();
-  if( instName == cString("sleep") || instName == cString("sleep1") || instName == cString("sleep2") ||
-      instName == cString("sleep3") || instName == cString("sleep4")) {
-    cPopulation& pop = m_world->GetPopulation();
-    if(m_world->GetConfig().LOG_SLEEP_TIMES.Get() == 1) {
-      pop.AddBeginSleep(cellID,m_world->GetStats().GetUpdate());
-    }
-    pop.GetCell(cellID).GetOrganism()->SetSleeping(true);
-    pop.incNumAsleep();    //TODO - Fix me:  this functions get called repeatedly
-  }
-    
-    } else {
-      // not enough energy
-      return false;
-    }
-  }
-    
-  // If first time cost hasn't been paid off...
-  if (m_has_ft_costs && inst_ft_cost[cur_inst.GetOp()] > 0) {
-    inst_ft_cost[cur_inst.GetOp()]--;       // dec cost
-    return false;
-  }
-  
-  // Next, look at the per use cost
-  if (m_has_costs && m_inst_set->GetCost(cur_inst) > 0) {
-    if (inst_cost[cur_inst.GetOp()] > 1) {  // if isn't paid off (>1)
-      inst_cost[cur_inst.GetOp()]--;        // dec cost
-      return false;
-    } else {                                // else, reset cost array
-      inst_cost[cur_inst.GetOp()] = m_inst_set->GetCost(cur_inst);
-    }
-  }
-  
-  inst_energy_cost[cur_inst.GetOp()] = m_inst_set->GetEnergyCost(cur_inst); //reset instruction energy cost
-#endif
-  return true;
-}
-
 // This method will handle the actual execution of an instruction
 // within a single process, once that function has been finalized.
 bool cHardwareCPU::SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst) 
@@ -4121,12 +4062,8 @@
   pop.decNumAsleep();
   if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 2) {
     organism->GetPhenotype().RefreshEnergy();
-    double newMerit = organism->GetPhenotype().ApplyToEnergyStore();
-    if(newMerit != -1) {
-      std::cerr.precision(20);
-      std::cerr<<"[cHardwareCPU::Inst_Sleep] newMerit = "<< newMerit <<std::endl;
-      organism->GetOrgInterface().UpdateMerit(newMerit);
-    }
+    organism->GetPhenotype().ApplyToEnergyStore();
+    pop.UpdateMerit(organism->GetCellID(), cMerit::EnergyToMerit(organism->GetPhenotype().GetStoredEnergy(), m_world));
   }
   return true;
 }

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareCPU.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -137,19 +137,7 @@
   bool m_mal_active;         // Has an allocate occured since last divide?
   bool m_advance_ip;         // Should the IP advance after this instruction?
   bool m_executedmatchstrings;	// Have we already executed the match strings instruction?
-
-  // Instruction costs...
-#if INSTRUCTION_COSTS
-  tArray<int> inst_cost;
-  tArray<int> inst_ft_cost;
-  tArray<int> inst_energy_cost;
-  bool m_has_costs;
-  bool m_has_ft_costs;
-  bool m_has_energy_costs;
-#endif
   
-  
-  bool SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst);
   bool SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst);
   
   // --------  Stack Manipulation...  --------

Modified: development/source/cpu/cHardwareExperimental.cc
===================================================================
--- development/source/cpu/cHardwareExperimental.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareExperimental.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -164,13 +164,15 @@
 , m_mal_active(hardware_cpu.m_mal_active)
 , m_advance_ip(hardware_cpu.m_advance_ip)
 , m_executedmatchstrings(hardware_cpu.m_executedmatchstrings)
+{
 #if INSTRUCTION_COSTS
-, inst_cost(hardware_cpu.inst_cost)
-, inst_ft_cost(hardware_cpu.inst_ft_cost)
-, m_has_costs(hardware_cpu.m_has_costs)
-, m_has_ft_costs(hardware_cpu.m_has_ft_costs)
+  inst_cost = hardware_cpu.inst_cost;
+  inst_ft_cost = hardware_cpu.inst_ft_cost;
+  inst_energy_cost = hardware_cpu.inst_energy_cost;
+  m_has_costs = hardware_cpu.m_has_costs;
+  m_has_ft_costs = hardware_cpu.m_has_ft_costs;
+  m_has_energy_costs = hardware_cpu.m_has_energy_costs;
 #endif
-{
 }
 
 
@@ -194,8 +196,10 @@
   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);
   m_has_costs = false;
   m_has_ft_costs = false;
+  m_has_energy_costs = false;
   
   for (int i = 0; i < num_inst_cost; i++) {
     inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
@@ -203,9 +207,11 @@
     
     inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
     if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
+
+    inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));    
+    if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
   }
 #endif 
-  
 }
 
 void cHardwareExperimental::cLocalThread::operator=(const cLocalThread& in_thread)
@@ -310,35 +316,6 @@
   CheckImplicitRepro(ctx);
 }
 
-
-// This method will test to see if all costs have been paid associated
-// with executing an instruction and only return true when that instruction
-// should proceed.
-bool cHardwareExperimental::SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst)
-{
-#if INSTRUCTION_COSTS
-  assert(cur_inst.GetOp() < inst_cost.GetSize());
-  
-  // If first time cost hasn't been paid off...
-  if (m_has_ft_costs && inst_ft_cost[cur_inst.GetOp()] > 0) {
-    inst_ft_cost[cur_inst.GetOp()]--;       // dec cost
-    return false;
-  }
-  
-  // Next, look at the per use cost
-  if (m_has_costs && m_inst_set->GetCost(cur_inst) > 0) {
-    if (inst_cost[cur_inst.GetOp()] > 1) {  // if isn't paid off (>1)
-      inst_cost[cur_inst.GetOp()]--;        // dec cost
-      return false;
-    } else {                                // else, reset cost array
-      inst_cost[cur_inst.GetOp()] = m_inst_set->GetCost(cur_inst);
-    }
-  }
-  
-#endif
-  return true;
-}
-
 // This method will handle the actuall execution of an instruction
 // within single process, once that function has been finalized.
 bool cHardwareExperimental::SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst) 

Modified: development/source/cpu/cHardwareExperimental.h
===================================================================
--- development/source/cpu/cHardwareExperimental.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareExperimental.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -136,16 +136,6 @@
   bool m_advance_ip;         // Should the IP advance after this instruction?
   bool m_executedmatchstrings;	// Have we already executed the match strings instruction?
 
-  // Instruction costs...
-#if INSTRUCTION_COSTS
-  tArray<int> inst_cost;
-  tArray<int> inst_ft_cost;
-  bool m_has_costs;
-  bool m_has_ft_costs;
-#endif
-  
-  
-  bool SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst);
   bool SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst);
   
   // --------  Stack Manipulation...  --------

Modified: development/source/cpu/cHardwareGX.cc
===================================================================
--- development/source/cpu/cHardwareGX.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareGX.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -407,10 +407,20 @@
   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);
+  m_has_costs = false;
+  m_has_ft_costs = false;
+  m_has_energy_costs = false;
   
   for (int i = 0; i < num_inst_cost; i++) {
     inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
+    if (!m_has_costs && inst_cost[i]) m_has_costs = true;
+    
     inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
+    if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
+
+    inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));    
+    if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
   }
 #endif
 }
@@ -573,36 +583,6 @@
 //  organism->SetRunning(false);
 //}
 
-
-// This method will test to see if all costs have been paid associated
-// with executing an instruction and only return true when that instruction
-// should proceed.
-bool cHardwareGX::SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst)
-{
-#if INSTRUCTION_COSTS
-  assert(cur_inst.GetOp() < inst_cost.GetSize());
-  
-  // If first time cost hasn't been paid off...
-  if ( inst_ft_cost[cur_inst.GetOp()] > 0 ) {
-    inst_ft_cost[cur_inst.GetOp()]--;       // dec cost
-    return false;
-  }
-  
-  // Next, look at the per use cost
-  if ( m_inst_set->GetCost(cur_inst) > 0 ) {
-    if ( inst_cost[cur_inst.GetOp()] > 1 ){  // if isn't paid off (>1)
-      inst_cost[cur_inst.GetOp()]--;         // dec cost
-      return false;
-    } else {                                 // else, reset cost array
-      inst_cost[cur_inst.GetOp()] = m_inst_set->GetCost(cur_inst);
-    }
-  }
-  
-#endif
-  return true;
-}
-
-
 /*! This method executes one instruction for one programid. */
 bool cHardwareGX::SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst) 
 {

Modified: development/source/cpu/cHardwareGX.h
===================================================================
--- development/source/cpu/cHardwareGX.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareGX.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -259,14 +259,6 @@
   bool m_reset_inputs; // Flag to make it easy for instructions to reset all inputs (force task modularity).
   bool m_reset_heads;  // Flas to make it easy for instructions to reset heads back (force task modularity).
 
-  // Instruction costs...
-#if INSTRUCTION_COSTS
-  tArray<int> inst_cost;
-  tArray<int> inst_ft_cost;
-#endif
-  
-  
-  bool SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst);
   bool SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst);
   
   // --------  Stack Manipulation...  --------

Modified: development/source/cpu/cHardwareSMT.cc
===================================================================
--- development/source/cpu/cHardwareSMT.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareSMT.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -172,17 +172,22 @@
   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);
   m_has_costs = false;
   m_has_ft_costs = false;
-	
+  m_has_energy_costs = false;
+  
   for (int i = 0; i < num_inst_cost; i++) {
     inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
     if (!m_has_costs && inst_cost[i]) m_has_costs = true;
     
     inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
     if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
+
+    inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));    
+    if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
   }
-#endif	
+#endif
   
   organism->NetReset();
 }
@@ -232,12 +237,18 @@
     const cInstruction& cur_inst = IP().GetInst();
 		
     // Test if costs have been paid and it is okay to execute this now...
-    const bool exec = SingleProcess_PayCosts(ctx, cur_inst);
+    bool exec = SingleProcess_PayCosts(ctx, cur_inst);
 		
     // Now execute the instruction...
     if (exec == true) {
-      SingleProcess_ExecuteInst(ctx, cur_inst);
-			
+      
+      // Prob of exec (moved from SingleProcess_PayCosts so that we advance IP after a fail)
+      if ( m_inst_set->GetProbFail(cur_inst) > 0.0 ) {
+        exec = !( ctx.GetRandom().P(m_inst_set->GetProbFail(cur_inst)) );
+      }
+      
+      if (exec == true) SingleProcess_ExecuteInst(ctx, cur_inst);
+      			
       // Some instruction (such as jump) may turn advance_ip off.  Ususally
       // we now want to move to the next instruction in the memory.
       if (AdvanceIP() == true) IP().Advance();
@@ -255,39 +266,6 @@
   CheckImplicitRepro(ctx);
 }
 
-
-// This method will test to see if all costs have been paid associated
-// with executing an instruction and only return true when that instruction
-// should proceed.
-bool cHardwareSMT::SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst)
-{
-#if INSTRUCTION_COSTS
-  assert(cur_inst.GetOp() < inst_cost.GetSize());
-	
-  // If first time cost hasn't been paid off...
-  if (m_has_ft_costs && inst_ft_cost[cur_inst.GetOp()] > 0) {
-    inst_ft_cost[cur_inst.GetOp()]--;       // dec cost
-    return false;
-  }
-	
-  // Next, look at the per use cost
-  if (m_has_costs && m_inst_set->GetCost(cur_inst) > 0) {
-    if (inst_cost[cur_inst.GetOp()] > 1) {  // if isn't paid off (>1)
-      inst_cost[cur_inst.GetOp()]--;        // dec cost
-      return false;
-    } else {                                // else, reset cost array
-      inst_cost[cur_inst.GetOp()] = m_inst_set->GetCost(cur_inst);
-    }
-  }
-	
-  // Prob of exec
-  if ( m_inst_set->GetProbFail(cur_inst) > 0.0 ){
-    return !( ctx.GetRandom().P(m_inst_set->GetProbFail(cur_inst)) );
-  }
-#endif
-  return true;
-}
-
 // This method will handle the actual execution of an instruction
 // within single process, once that function has been finalized.
 bool cHardwareSMT::SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst) 

Modified: development/source/cpu/cHardwareSMT.h
===================================================================
--- development/source/cpu/cHardwareSMT.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareSMT.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -121,20 +121,9 @@
   // Threads
   tManagedPointerArray<cLocalThread> m_threads;
   tHashTable<int, int> m_thread_lbls;
-  int m_cur_thread;
-	
-  // Instruction costs...
-#if INSTRUCTION_COSTS
-  tArray<int> inst_cost;
-  tArray<int> inst_ft_cost;
-  bool m_has_costs;
-  bool m_has_ft_costs;
-#endif
-  
+  int m_cur_thread;  
   int m_cur_child;
 
-  
-  bool SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst);
   bool SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst);
   	
 

Modified: development/source/cpu/cHardwareTransSMT.cc
===================================================================
--- development/source/cpu/cHardwareTransSMT.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareTransSMT.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -169,17 +169,22 @@
   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);
   m_has_costs = false;
   m_has_ft_costs = false;
-	
+  m_has_energy_costs = false;
+  
   for (int i = 0; i < num_inst_cost; i++) {
     inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
     if (!m_has_costs && inst_cost[i]) m_has_costs = true;
     
     inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
     if (!m_has_ft_costs && inst_ft_cost[i]) m_has_ft_costs = true;
+
+    inst_energy_cost[i] = m_inst_set->GetEnergyCost(cInstruction(i));    
+    if(!m_has_energy_costs && inst_energy_cost[i]) m_has_energy_costs = true;
   }
-#endif	
+#endif
   
   organism->ClearParasites();
   organism->NetReset();
@@ -230,11 +235,16 @@
     const cInstruction& cur_inst = IP().GetInst();
 		
     // Test if costs have been paid and it is okay to execute this now...
-    const bool exec = SingleProcess_PayCosts(ctx, cur_inst);
+    bool exec = SingleProcess_PayCosts(ctx, cur_inst);
 		
     // Now execute the instruction...
     if (exec == true) {
-      SingleProcess_ExecuteInst(ctx, cur_inst);
+      // Prob of exec (moved from SingleProcess_PayCosts so that we advance IP after a fail)
+      if ( m_inst_set->GetProbFail(cur_inst) > 0.0 ) {
+        exec = !( ctx.GetRandom().P(m_inst_set->GetProbFail(cur_inst)) );
+      }
+      
+      if (exec == true) SingleProcess_ExecuteInst(ctx, cur_inst);
 			
       // Some instruction (such as jump) may turn advance_ip off.  Ususally
       // we now want to move to the next instruction in the memory.
@@ -253,39 +263,6 @@
   CheckImplicitRepro(ctx);
 }
 
-
-// This method will test to see if all costs have been paid associated
-// with executing an instruction and only return true when that instruction
-// should proceed.
-bool cHardwareTransSMT::SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst)
-{
-#if INSTRUCTION_COSTS
-  assert(cur_inst.GetOp() < inst_cost.GetSize());
-	
-  // If first time cost hasn't been paid off...
-  if (m_has_ft_costs && inst_ft_cost[cur_inst.GetOp()] > 0) {
-    inst_ft_cost[cur_inst.GetOp()]--;       // dec cost
-    return false;
-  }
-	
-  // Next, look at the per use cost
-  if (m_has_costs && m_inst_set->GetCost(cur_inst) > 0) {
-    if (inst_cost[cur_inst.GetOp()] > 1) {  // if isn't paid off (>1)
-      inst_cost[cur_inst.GetOp()]--;        // dec cost
-      return false;
-    } else {                                // else, reset cost array
-      inst_cost[cur_inst.GetOp()] = m_inst_set->GetCost(cur_inst);
-    }
-  }
-	
-  // Prob of exec
-  if (m_inst_set->GetProbFail(cur_inst) > 0.0) {
-    return !( ctx.GetRandom().P(m_inst_set->GetProbFail(cur_inst)) );
-  }
-#endif
-  return true;
-}
-
 // This method will handle the actual execution of an instruction
 // within single process, once that function has been finalized.
 bool cHardwareTransSMT::SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst) 

Modified: development/source/cpu/cHardwareTransSMT.h
===================================================================
--- development/source/cpu/cHardwareTransSMT.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/cpu/cHardwareTransSMT.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -121,19 +121,8 @@
   tManagedPointerArray<cLocalThread> m_threads;
   tHashTable<int, int> m_thread_lbls;
   int m_cur_thread;
-	
-  // Instruction costs...
-#if INSTRUCTION_COSTS
-  tArray<int> inst_cost;
-  tArray<int> inst_ft_cost;
-  bool m_has_costs;
-  bool m_has_ft_costs;
-#endif
-  
   int m_cur_child;
 
-  
-  bool SingleProcess_PayCosts(cAvidaContext& ctx, const cInstruction& cur_inst);
   bool SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst);
   	
 

Modified: development/source/main/cAvidaConfig.h
===================================================================
--- development/source/main/cAvidaConfig.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/main/cAvidaConfig.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -350,7 +350,7 @@
   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, "Maximum 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(ENERGY_VERBOSE, bool, 0, "Print energy and merit values. 0/1 (off/on)");
+//  CONFIG_ADD_VAR(ENERGY_VERBOSE, bool, 0, "Print energy and merit values. 0/1 (off/on)");
   CONFIG_ADD_VAR(LOG_SLEEP_TIMES, bool, 0, "Log sleep start and end times. 0/1 (off/on)\nWARNING: may use lots of memory.");
   //  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
 

Modified: development/source/main/cBirthChamber.cc
===================================================================
--- development/source/main/cBirthChamber.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/main/cBirthChamber.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -158,44 +158,15 @@
   merit_array.Resize(1);
   
   if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
-    // energy model config variables
-    double energy_given_at_birth = m_world->GetConfig().ENERGY_GIVEN_AT_BIRTH.Get();
-    double frac_parent_energy_given_at_birth = m_world->GetConfig().FRAC_PARENT_ENERGY_GIVEN_AT_BIRTH.Get();
-    double frac_energy_decay_at_birth = m_world->GetConfig().FRAC_ENERGY_DECAY_AT_BIRTH.Get();
-    int inst_2_exc = m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get();
-    double energy_cap = (double) m_world->GetConfig().ENERGY_CAP.Get();
-  
-    // apply energy if APPLY_ENERGY_METHOD is set to "on divide" (0)
-    if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 0) {
-      parent.GetPhenotype().RefreshEnergy();
-      parent.GetPhenotype().ApplyToEnergyStore();
-    }
-    
-    // decay of energy in parent
-    parent.GetPhenotype().ReduceEnergy(parent.GetPhenotype().GetStoredEnergy() * frac_energy_decay_at_birth);
-
     // calculate energy to be given to child
-    double child_energy = min(parent.GetPhenotype().GetStoredEnergy() * frac_parent_energy_given_at_birth + energy_given_at_birth, energy_cap);
+    double child_energy = parent.GetPhenotype().ExtractParentEnergy();
         
-    // adjust energy in parent
-    parent.GetPhenotype().ReduceEnergy(child_energy - energy_given_at_birth);
-
     // set child energy & merit
     child_array[0]->GetPhenotype().SetEnergy(child_energy);
-    merit_array[0] = 100 * child_energy / (inst_2_exc);
-    
-
-    cMerit parentMerit = cMerit(min(100 * (parent.GetPhenotype().GetStoredEnergy() + energy_given_at_birth)/(inst_2_exc), energy_cap));
-    parent.GetPhenotype().SetMerit(parentMerit);
-
-    if(m_world->GetConfig().ENERGY_VERBOSE.Get()) {
-      cerr<<"child merit: "<<merit_array[0]<<endl<<"child energy: "<< child_energy <<endl
-          <<"parent merit: "<<parent.GetPhenotype().GetMerit()<<endl<<"parent energy: "<< parent.GetPhenotype().GetStoredEnergy() <<endl;
-    }
+    merit_array[0] = cMerit::EnergyToMerit(child_array[0]->GetPhenotype().GetStoredEnergy(), m_world);
   } else {
     merit_array[0] = parent.GetPhenotype().GetMerit();
   }
-  
 
   // Setup the genotype for the child
   cGenotype * child_genotype = parent.GetGenotype();
@@ -257,7 +228,12 @@
     cGenotype * parent_genotype = parent.GetGenotype();
     parent_genotype->IncDeferAdjust();
     size_wait_entry[child_length].genome = child_genome;
-    size_wait_entry[child_length].merit = parent.GetPhenotype().GetMerit();
+    if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+      size_wait_entry[child_length].energy4Offspring = parent.GetPhenotype().ExtractParentEnergy();
+      size_wait_entry[child_length].merit.EnergyToMerit(size_wait_entry[child_length].energy4Offspring, m_world);
+    } else {
+      size_wait_entry[child_length].merit = parent.GetPhenotype().GetMerit();
+    }
     size_wait_entry[child_length].parent_genotype = parent_genotype;
     size_wait_entry[child_length].update_in = m_world->GetStats().GetUpdate();
     return NULL; 				
@@ -281,7 +257,12 @@
     cGenotype * parent_genotype = parent.GetGenotype();
     parent_genotype->IncDeferAdjust();
     mate_select_wait_entry[mate_id].genome = child_genome;
-    mate_select_wait_entry[mate_id].merit = parent.GetPhenotype().GetMerit();
+    if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+      mate_select_wait_entry[mate_id].energy4Offspring = parent.GetPhenotype().ExtractParentEnergy();
+      mate_select_wait_entry[mate_id].merit.EnergyToMerit(mate_select_wait_entry[mate_id].energy4Offspring, m_world);
+    } else {
+      mate_select_wait_entry[mate_id].merit = parent.GetPhenotype().GetMerit();
+    }
     mate_select_wait_entry[mate_id].parent_genotype = parent_genotype;
     mate_select_wait_entry[mate_id].update_in = m_world->GetStats().GetUpdate();
     return NULL;
@@ -304,7 +285,12 @@
     cGenotype * parent_genotype = parent.GetGenotype();
     parent_genotype->IncDeferAdjust();
     local_wait_entry[parent_id].genome = child_genome;
-    local_wait_entry[parent_id].merit = parent.GetPhenotype().GetMerit();
+    if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+      local_wait_entry[parent_id].energy4Offspring = parent.GetPhenotype().ExtractParentEnergy();
+      local_wait_entry[parent_id].merit.EnergyToMerit(local_wait_entry[parent_id].energy4Offspring, m_world);
+    } else {
+      local_wait_entry[parent_id].merit = parent.GetPhenotype().GetMerit();
+    }
     local_wait_entry[parent_id].parent_genotype = parent_genotype;
     local_wait_entry[parent_id].update_in = m_world->GetStats().GetUpdate();
     return NULL; 				
@@ -330,7 +316,12 @@
     cGenotype * parent_genotype = parent.GetGenotype();
     parent_genotype->IncDeferAdjust();
     deme_wait_entry[parent_deme].genome = child_genome;
-    deme_wait_entry[parent_deme].merit = parent.GetPhenotype().GetMerit();
+    if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+      deme_wait_entry[parent_deme].energy4Offspring = parent.GetPhenotype().ExtractParentEnergy();
+      deme_wait_entry[parent_deme].merit.EnergyToMerit(deme_wait_entry[parent_deme].energy4Offspring, m_world);
+    } else {
+      deme_wait_entry[parent_deme].merit = parent.GetPhenotype().GetMerit();
+    }
     deme_wait_entry[parent_deme].parent_genotype = parent_genotype;
     deme_wait_entry[parent_deme].update_in = m_world->GetStats().GetUpdate();
     return NULL; 				
@@ -347,8 +338,13 @@
   if ( EvaluateEntry(global_wait_entry) == false ) {
     cGenotype * parent_genotype = parent.GetGenotype();
     parent_genotype->IncDeferAdjust();
-    global_wait_entry.genome = child_genome; 
-    global_wait_entry.merit = parent.GetPhenotype().GetMerit();
+    global_wait_entry.genome = child_genome;
+    if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+      global_wait_entry.energy4Offspring = parent.GetPhenotype().ExtractParentEnergy();
+      global_wait_entry.merit.EnergyToMerit(global_wait_entry.energy4Offspring, m_world);
+    } else {
+      global_wait_entry.merit = parent.GetPhenotype().GetMerit();
+    }
     global_wait_entry.parent_genotype = parent_genotype;
     global_wait_entry.update_in = m_world->GetStats().GetUpdate();
     return NULL;
@@ -584,9 +580,16 @@
   // If we made it this far, RECOMBINATION will happen!
   cCPUMemory genome0 = old_entry->genome;
   cCPUMemory genome1 = child_genome;
-  double merit0 = old_entry->merit.GetDouble();
-  double merit1 = parent_phenotype.GetMerit().GetDouble();
+  double meritOrEnergy0;
+  double meritOrEnergy1;
 
+  if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+    meritOrEnergy0 = old_entry->energy4Offspring;
+    meritOrEnergy1 = parent_phenotype.ExtractParentEnergy();
+  } else {
+    meritOrEnergy0 = old_entry->merit.GetDouble();
+    meritOrEnergy1 = parent_phenotype.GetMerit().GetDouble();
+  }
 
   // Check the modular recombination settings.  There are three variables:
   //  1: How many modules?  (0 = non-modular)
@@ -599,22 +602,22 @@
 
   // If we are NOT modular...
   if (num_modules == 0) {
-    DoBasicRecombination(ctx, genome0, genome1, merit0, merit1);
+    DoBasicRecombination(ctx, genome0, genome1, meritOrEnergy0, meritOrEnergy1);
   }
 
   // If we ARE modular, and continuous...
   else if (continuous_regions == 1) {
-    DoModularContRecombination(ctx, genome0, genome1, merit0, merit1);
+    DoModularContRecombination(ctx, genome0, genome1, meritOrEnergy0, meritOrEnergy1);
   }
 
   // If we are NOT continuous, but NO shuffling...
   else if (shuffle_regions == 0) {
-    DoModularNonContRecombination(ctx, genome0, genome1, merit0, merit1);
+    DoModularNonContRecombination(ctx, genome0, genome1, meritOrEnergy0, meritOrEnergy1);
   }
 
   // If there IS shuffling (NON-continuous required)
   else {
-    DoModularShuffleRecombination(ctx, genome0, genome1, merit0, merit1);
+    DoModularShuffleRecombination(ctx, genome0, genome1, meritOrEnergy0, meritOrEnergy1);
   }
 
   // Should there be a 2-fold cost to sex?
@@ -629,10 +632,17 @@
     child_array[0] = new cOrganism(m_world, ctx, genome0);
     child_array[1] = new cOrganism(m_world, ctx, genome1);
     
+    if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+      child_array[0]->GetPhenotype().SetEnergy(meritOrEnergy0);
+      child_array[1]->GetPhenotype().SetEnergy(meritOrEnergy1);
+      meritOrEnergy0 = cMerit::EnergyToMerit(child_array[0]->GetPhenotype().GetStoredEnergy(), m_world);
+      meritOrEnergy1 = cMerit::EnergyToMerit(child_array[1]->GetPhenotype().GetStoredEnergy(), m_world);
+    }
+    
     merit_array.Resize(2);
-    merit_array[0] = merit0;
-    merit_array[1] = merit1;
-
+    merit_array[0] = meritOrEnergy0;
+    merit_array[1] = meritOrEnergy1;
+    
     // Setup the genotypes for both children...
     SetupGenotypeInfo(child_array[0], parent0_genotype, parent1_genotype);
     SetupGenotypeInfo(child_array[1], parent1_genotype, parent0_genotype);
@@ -644,14 +654,22 @@
 
     if (ctx.GetRandom().GetDouble() < 0.5) {
       child_array[0] = new cOrganism(m_world, ctx, genome0);
-      merit_array[0] = merit0;
+      if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+        child_array[0]->GetPhenotype().SetEnergy(meritOrEnergy0);
+        meritOrEnergy0 = cMerit::EnergyToMerit(child_array[0]->GetPhenotype().GetStoredEnergy(), m_world);
+      }
+      merit_array[0] = meritOrEnergy0;
 
       // Setup the genotype for the child...
       SetupGenotypeInfo(child_array[0], parent0_genotype, parent1_genotype);
     } 
     else {
       child_array[0] = new cOrganism(m_world, ctx, genome1);
-      merit_array[0] = merit1;
+      if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+        child_array[0]->GetPhenotype().SetEnergy(meritOrEnergy1);
+        meritOrEnergy1 = cMerit::EnergyToMerit(child_array[1]->GetPhenotype().GetStoredEnergy(), m_world);
+      }
+      merit_array[0] = meritOrEnergy1;
 
       // Setup the genotype for the child...
       SetupGenotypeInfo(child_array[0], parent1_genotype, parent0_genotype);
@@ -665,4 +683,3 @@
 
   return true;
 }
-

Modified: development/source/main/cBirthChamber.h
===================================================================
--- development/source/main/cBirthChamber.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/main/cBirthChamber.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -54,6 +54,7 @@
   class cBirthEntry {
   public:
     cCPUMemory genome;
+    double energy4Offspring;
     cMerit merit;
     cGenotype * parent_genotype;
     int update_in;  // Update entry was created; Set to -1 if entry is empty.

Modified: development/source/main/cOrganism.cc
===================================================================
--- development/source/main/cOrganism.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/main/cOrganism.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -228,7 +228,8 @@
   
   if(m_world->GetConfig().ENERGY_ENABLED.Get() && m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1 && task_completed) {
     m_phenotype.RefreshEnergy();
-    double newMerit = m_phenotype.ApplyToEnergyStore();
+    m_phenotype.ApplyToEnergyStore();
+    double newMerit = cMerit::EnergyToMerit(GetPhenotype().GetStoredEnergy(), m_world);
     if(newMerit != -1) {
       m_interface->UpdateMerit(newMerit);
     }

Modified: development/source/main/cPhenotype.cc
===================================================================
--- development/source/main/cPhenotype.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/main/cPhenotype.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -384,8 +384,8 @@
     merit = cur_merit_base * cur_bonus;
   }
   
-  // update energy store
-  energy_store += cur_energy_bonus;
+  //BB:TODO update energy store
+  SetEnergy(energy_store + cur_energy_bonus);
   
   genome_length   = _genome.GetSize();
   (void) copied_size;          // Unchanged
@@ -1184,15 +1184,23 @@
   }
 }
 
+void cPhenotype::ReduceEnergy(const double cost) {
+  SetEnergy(energy_store - cost);
+}
+
+void cPhenotype::SetEnergy(const double value) {
+  energy_store = max(0.0, min(value, (double) m_world->GetConfig().ENERGY_CAP.Get()));
+}
+
 /**
-Credit organism with energy reward, but only update energy store if APPLY_ENERGY_METHOD = "no task completion" (1)
+Credit organism with energy reward, but only update energy store if APPLY_ENERGY_METHOD = "on task completion" (1)
  */
 void cPhenotype::RefreshEnergy() {
   if(cur_energy_bonus > 0) {
     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;
+      SetEnergy(energy_store + cur_energy_bonus);  //TODO: use SetEnergy
     } else {
       cerr<< "Unknown APPLY_ENERGY_METHOD value " << m_world->GetConfig().APPLY_ENERGY_METHOD.Get();
       exit(-1);
@@ -1201,10 +1209,9 @@
   }
 }
 
-double cPhenotype::ApplyToEnergyStore() {
-  energy_store += energy_tobe_applied;
+void cPhenotype::ApplyToEnergyStore() {
+  SetEnergy(energy_store + energy_tobe_applied);
   energy_tobe_applied = 0.0;
-  return min(100 * energy_store / (m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get()), (double) m_world->GetConfig().ENERGY_CAP.Get());
 }
 
 void cPhenotype::DecayAllPromoterRegulation()
@@ -1218,6 +1225,40 @@
   }
 }
 
+double cPhenotype::ExtractParentEnergy() {
+  assert(m_world->GetConfig().ENERGY_ENABLED.Get() > 0);
+  // energy model config variables
+  double energy_given_at_birth = m_world->GetConfig().ENERGY_GIVEN_AT_BIRTH.Get();
+  double frac_parent_energy_given_at_birth = m_world->GetConfig().FRAC_PARENT_ENERGY_GIVEN_AT_BIRTH.Get();
+  double frac_energy_decay_at_birth = m_world->GetConfig().FRAC_ENERGY_DECAY_AT_BIRTH.Get();
+  double energy_cap = (double) m_world->GetConfig().ENERGY_CAP.Get();
+  
+  // apply energy if APPLY_ENERGY_METHOD is set to "on divide" (0)
+  if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 0) {
+    RefreshEnergy();
+    ApplyToEnergyStore();
+  }
+  
+  // decay of energy in parent
+  ReduceEnergy(GetStoredEnergy() * frac_energy_decay_at_birth);
+  
+  // calculate energy to be given to child
+  double child_energy = min(GetStoredEnergy() * frac_parent_energy_given_at_birth + energy_given_at_birth, energy_cap);
+  
+  // adjust energy in parent
+  ReduceEnergy(child_energy - 2*energy_given_at_birth); // 2*energy_given_at_birth: 1 in child_energy & 1 for parent
+    
+  //TODO: add energy_given_at_birth to Stored_energy
+  cMerit parentMerit = cMerit(min(cMerit::EnergyToMerit(GetStoredEnergy(), m_world), energy_cap));
+  SetMerit(parentMerit);
+  
+/*  if(m_world->GetConfig().ENERGY_VERBOSE.Get()) {
+    cerr<<"child merit: "<<merit_array[0]<<endl<<"child energy: "<< child_energy <<endl
+    <<"parent merit: "<<GetMerit()<<endl<<"parent energy: "<< GetStoredEnergy() <<endl;
+  }*/
+  return child_energy;
+}
+
 void cPhenotype::RegulatePromoter(const int i, const bool up )
 {
   // Make sure we were initialized

Modified: development/source/main/cPhenotype.h
===================================================================
--- development/source/main/cPhenotype.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/main/cPhenotype.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -372,15 +372,15 @@
   bool CopyTrue() const   { assert(initialized == true); return copy_true; }
   bool DivideSex() const  { assert(initialized == true); return divide_sex; }
   int MateSelectID() const { assert(initialized == true); return mate_select_id; }
-  int  CrossNum() const  { assert(initialized == true); return cross_num; }
-  bool  ChildFertile() const { assert(initialized == true); return child_fertile;}
+  int CrossNum() const  { assert(initialized == true); return cross_num; }
+  bool ChildFertile() const { assert(initialized == true); return child_fertile;}
   int GetChildCopiedSize() const { assert(initialized == true); return child_copied_size; }
 
 
   ////////////////////  Accessors -- Modifying  ///////////////////
   void SetMerit(const cMerit& in_merit) { merit = in_merit; }
-  void ReduceEnergy(const double cost) { energy_store -= min(cost, (double) m_world->GetConfig().ENERGY_CAP.Get()); }
-  void SetEnergy(const double value) { energy_store = value; } //min(value, (double) m_world->GetConfig().ENERGY_CAP.Get()); }
+  void ReduceEnergy(const double cost);
+  void SetEnergy(const double 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; }
@@ -450,7 +450,8 @@
   bool& IsMultiThread() { assert(initialized == true); return is_multi_thread; }
   
   void RefreshEnergy();
-  double ApplyToEnergyStore();
+  void ApplyToEnergyStore();
+  double ExtractParentEnergy();
 };
 
 

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/main/cPopulation.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -236,12 +236,7 @@
   
   tArray<cOrganism*> child_array;
   tArray<cMerit> merit_array;
-  
-  //for energy model
-/*  double init_energy_given = m_world->GetConfig().ENERGY_GIVEN_AT_BIRTH.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 birth chamber
   // or the merit doesn't get passed onto the child correctly
@@ -414,19 +409,19 @@
   // Setup the inputs in the target cell.
   environment.SetupInputs(ctx, target_cell.input_array);
   
-	
-	// Precalculate the merit if requested
-	if (m_world->GetConfig().PRECALC_MERIT.Get() > 0){
-		cCPUTestInfo test_info;
-		cTestCPU* test_cpu = m_world->GetHardwareManager().CreateTestCPU();
-		test_info.UseManualInputs(target_cell.input_array);                            // Test using what the environment will be
-		test_cpu->TestGenome(ctx, test_info, in_organism->GetHardware().GetMemory());  // Use the true genome
-		in_organism->GetPhenotype().SetMerit(test_info.GetTestPhenotype().GetMerit()); // Update merit
-		delete test_cpu;
-	}
+  
+  // Precalculate the merit if requested
+  if (m_world->GetConfig().PRECALC_MERIT.Get() > 0){
+    cCPUTestInfo test_info;
+    cTestCPU* test_cpu = m_world->GetHardwareManager().CreateTestCPU();
+    test_info.UseManualInputs(target_cell.input_array);                            // Test using what the environment will be
+    test_cpu->TestGenome(ctx, test_info, in_organism->GetHardware().GetMemory());  // Use the true genome
+    in_organism->GetPhenotype().SetMerit(test_info.GetTestPhenotype().GetMerit()); // Update merit
+    delete test_cpu;
+  }
   // Update the archive...
-	
-	
+  
+  
   in_genotype->AddOrganism();
   
   if (old_genotype != NULL) {
@@ -452,27 +447,27 @@
   // Statistics...
   m_world->GetStats().RecordBirth(target_cell.GetID(), in_genotype->GetID(),
                                   in_organism->GetPhenotype().ParentTrue());
-	
-	// @MRR Do coalescence clade set up for new organisms.
-	CCladeSetupOrganism(in_organism ); 
-	
+  
+  // @MRR Do coalescence clade set up for new organisms.
+  CCladeSetupOrganism(in_organism ); 
+  
   //count how many times MERIT_BONUS_INST (rewarded instruction) is in the genome
   //only relevant if merit is proportional to # times MERIT_BONUS_INST is in the genome
   int rewarded_instruction = m_world->GetConfig().MERIT_BONUS_INST.Get();
   int num_rewarded_instructions = 0;
   int genome_length = in_organism->GetGenome().GetSize();
-
+  
   if(rewarded_instruction == -1){
     //no key instruction, so no bonus 
     in_organism->GetPhenotype().SetCurBonusInstCount(0);
-    }
+  }
   else{
     for(int i = 1; i <= genome_length; i++){
       if(in_organism->GetGenome()[i-1].GetOp() == rewarded_instruction){
-          num_rewarded_instructions++;
+        num_rewarded_instructions++;
       }  
     } 
-     in_organism->GetPhenotype().SetCurBonusInstCount(num_rewarded_instructions);
+    in_organism->GetPhenotype().SetCurBonusInstCount(num_rewarded_instructions);
   }
   
 }

Modified: development/source/main/cResourceCount.cc
===================================================================
--- development/source/main/cResourceCount.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/main/cResourceCount.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -198,8 +198,12 @@
     geo_name = "GRID";
   } else if (in_geometry == nGeometry::TORUS) {
     geo_name = "TORUS";
+  } else {
+    cerr << "[cResourceCount::Setup] Unknown resource geometry " << in_geometry << ".  Exiting.";
+    exit(2);
   }
 
+
   /* If the verbose flag is set print out information about resources */
 
   if (verbosity_level > VERBOSE_NORMAL) {

Modified: development/source/tools/cMerit.cc
===================================================================
--- development/source/tools/cMerit.cc	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/tools/cMerit.cc	2007-07-23 13:49:59 UTC (rev 1837)
@@ -9,6 +9,7 @@
  */
 
 #include "cMerit.h"
+#include "cWorld.h"
 
 using namespace std;
 
@@ -71,8 +72,12 @@
            test_value >= value / (1 + 1 / UINT_MAX)));
 }
 
+double cMerit::EnergyToMerit(const double orgEnergy, cWorld* m_world) {
+  assert(m_world->GetConfig().ENERGY_ENABLED.Get() == 1);
+  int inst_2_exc = m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get();
+  return 100 * orgEnergy / (inst_2_exc);
+}
 
-
 ostream& operator<<(ostream& os, const cMerit& merit)
 {
   os << merit.GetDouble();

Modified: development/source/tools/cMerit.h
===================================================================
--- development/source/tools/cMerit.h	2007-07-23 13:31:24 UTC (rev 1836)
+++ development/source/tools/cMerit.h	2007-07-23 13:49:59 UTC (rev 1837)
@@ -31,6 +31,8 @@
 #include <climits>
 #include <cassert>
 
+class cWorld;
+
 class cMerit
 {
 protected:
@@ -94,6 +96,8 @@
   double CalcFitness(int gestation_time) const {
     return ( gestation_time != 0 ) ? value / ((double) gestation_time) : 0; }
 
+  static double EnergyToMerit(const double orgEnergy, cWorld* m_world);
+
   std::ostream& BinaryPrint(std::ostream& os = std::cout) const ;
 };
 




More information about the Avida-cvs mailing list