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

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Thu Aug 23 19:02:02 PDT 2007


Author: beckma24
Date: 2007-08-23 22:02:02 -0400 (Thu, 23 Aug 2007)
New Revision: 1998

Modified:
   branches/energy/Avida.xcodeproj/project.pbxproj
   branches/energy/source/actions/PrintActions.cc
   branches/energy/source/cpu/cHardwareBase.cc
   branches/energy/source/cpu/cHardwareCPU.cc
   branches/energy/source/main/cOrganism.cc
   branches/energy/source/main/cPhenotype.cc
   branches/energy/source/main/cPhenotype.h
   branches/energy/source/main/cPopulationCell.cc
Log:
Organisms now die when they don't have enough energy to pay the energy cost of their next instruction.  Also, the double-energy-usage instruction now fails if it the executing organism does not have enough energy.

Modified: branches/energy/Avida.xcodeproj/project.pbxproj
===================================================================
--- branches/energy/Avida.xcodeproj/project.pbxproj	2007-08-24 01:14:44 UTC (rev 1997)
+++ branches/energy/Avida.xcodeproj/project.pbxproj	2007-08-24 02:02:02 UTC (rev 1998)
@@ -377,7 +377,7 @@
 		565121C60C3B055F009CBD3F /* cCoreView_Info.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cCoreView_Info.cc; sourceTree = "<group>"; };
 		565121C70C3B055F009CBD3F /* cCoreView_Info.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cCoreView_Info.h; sourceTree = "<group>"; };
 		565121C80C3B055F009CBD3F /* LAYOUT */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = LAYOUT; sourceTree = "<group>"; };
-		56F555D00C3B364E00E2E929 /* avida-text */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "avida-text"; sourceTree = BUILT_PRODUCTS_DIR; };
+		56F555D00C3B364E00E2E929 /* avida-text */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = "avida-text"; sourceTree = BUILT_PRODUCTS_DIR; };
 		56F555DF0C3B402A00E2E929 /* cDriver_TextViewer.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cDriver_TextViewer.cc; sourceTree = "<group>"; };
 		56F555E00C3B402A00E2E929 /* cDriver_TextViewer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cDriver_TextViewer.h; sourceTree = "<group>"; };
 		56F555E30C3B402A00E2E929 /* LAYOUT */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = LAYOUT; sourceTree = "<group>"; };

Modified: branches/energy/source/actions/PrintActions.cc
===================================================================
--- branches/energy/source/actions/PrintActions.cc	2007-08-24 01:14:44 UTC (rev 1997)
+++ branches/energy/source/actions/PrintActions.cc	2007-08-24 02:02:02 UTC (rev 1998)
@@ -2169,7 +2169,7 @@
     for (int i = 0; i < m_world->GetPopulation().GetWorldX(); i++) {
       for (int j = 0; j < m_world->GetPopulation().GetWorldY(); j++) {
         cPopulationCell& cell = m_world->GetPopulation().GetCell(j * m_world->GetPopulation().GetWorldX() + i);
-        double cell_executionRatio = (cell.IsOccupied()) ? cell.GetOrganism()->GetPhenotype().GetExecutionRatio() : 0.0;
+        double cell_executionRatio = (cell.IsOccupied()) ? cell.GetOrganism()->GetPhenotype().GetEnergyUsageRatio() : 0.0;
         fp << cell_executionRatio << " ";
       }
       fp << endl;

Modified: branches/energy/source/cpu/cHardwareBase.cc
===================================================================
--- branches/energy/source/cpu/cHardwareBase.cc	2007-08-24 01:14:44 UTC (rev 1997)
+++ branches/energy/source/cpu/cHardwareBase.cc	2007-08-24 02:02:02 UTC (rev 1998)
@@ -817,8 +817,11 @@
 }
 
 bool cHardwareBase::Inst_DoubleEnergyUsage(cAvidaContext& ctx) {
-  organism->GetPhenotype().DoubleEnergyUsage();
-  return true;
+  double energy_req = inst_energy_cost[IP().GetNextInst().GetOp()]
+                        * cMerit::EnergyToMerit(organism->GetPhenotype().GetStoredEnergy() * organism->GetPhenotype().GetEnergyUsageRatio() * 2.0, m_world)
+                        / 100.0; //compensate by factor of 100
+
+  return organism->GetPhenotype().DoubleEnergyUsage(energy_req);
 }
 
 bool cHardwareBase::Inst_HalfEnergyUsage(cAvidaContext& ctx) {
@@ -840,6 +843,7 @@
     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) { 
+      int cellID = organism->GetCellID();
       if (organism->GetPhenotype().GetStoredEnergy() >= energy_req) {
         inst_energy_cost[cur_inst.GetOp()] = 0;
         // subtract energy used from current org energy.
@@ -847,7 +851,6 @@
         
         // 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();
@@ -857,8 +860,8 @@
           pop.GetCell(cellID).GetOrganism()->SetSleeping(true);
           m_world->GetStats().incNumAsleep(pop.GetCell(cellID).GetDemeID());
         }
-      } else { // not enough energy
-        organism->UpdateMerit(0.0);  //remove from scheduler
+      } else {
+        organism->GetPhenotype().SetToDie();
         return false;
       }
     }

Modified: branches/energy/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/energy/source/cpu/cHardwareCPU.cc	2007-08-24 01:14:44 UTC (rev 1997)
+++ branches/energy/source/cpu/cHardwareCPU.cc	2007-08-24 02:02:02 UTC (rev 1998)
@@ -3112,12 +3112,12 @@
   
   //update energy store and merit of donor
   organism->GetPhenotype().ReduceEnergy(energy_given);
-  double senderMerit = cMerit::EnergyToMerit(organism->GetPhenotype().GetStoredEnergy(), m_world) * organism->GetPhenotype().GetExecutionRatio();
+  double senderMerit = cMerit::EnergyToMerit(organism->GetPhenotype().GetStoredEnergy()  * organism->GetPhenotype().GetEnergyUsageRatio(), m_world);
   organism->UpdateMerit(senderMerit);
   
   // update energy store and merit of donee
   to_org->GetPhenotype().ReduceEnergy(-1.0*energy_given);
-  double receiverMerit = cMerit::EnergyToMerit(to_org->GetPhenotype().GetStoredEnergy(), m_world) * to_org->GetPhenotype().GetExecutionRatio();
+  double receiverMerit = cMerit::EnergyToMerit(to_org->GetPhenotype().GetStoredEnergy() * to_org->GetPhenotype().GetEnergyUsageRatio(), m_world);
   to_org->UpdateMerit(receiverMerit);
 }
 
@@ -4226,7 +4226,7 @@
   if(m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 2) {
     organism->GetPhenotype().RefreshEnergy();
     organism->GetPhenotype().ApplyToEnergyStore();
-    double newMerit = cMerit::EnergyToMerit(organism->GetPhenotype().GetStoredEnergy(), m_world) * organism->GetPhenotype().GetExecutionRatio();
+    double newMerit = cMerit::EnergyToMerit(organism->GetPhenotype().GetStoredEnergy() * organism->GetPhenotype().GetEnergyUsageRatio(), m_world);
     pop.UpdateMerit(organism->GetCellID(), newMerit);
   }
   return true;

Modified: branches/energy/source/main/cOrganism.cc
===================================================================
--- branches/energy/source/main/cOrganism.cc	2007-08-24 01:14:44 UTC (rev 1997)
+++ branches/energy/source/main/cOrganism.cc	2007-08-24 02:02:02 UTC (rev 1998)
@@ -243,7 +243,7 @@
   if(m_world->GetConfig().ENERGY_ENABLED.Get() && m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1 && task_completed) {
     m_phenotype.RefreshEnergy();
     m_phenotype.ApplyToEnergyStore();
-    double newMerit = cMerit::EnergyToMerit(GetPhenotype().GetStoredEnergy(), m_world) * GetPhenotype().GetExecutionRatio();
+    double newMerit = cMerit::EnergyToMerit(GetPhenotype().GetStoredEnergy() * GetPhenotype().GetEnergyUsageRatio(), m_world);
     if(newMerit != -1) {
       m_interface->UpdateMerit(newMerit);
     }

Modified: branches/energy/source/main/cPhenotype.cc
===================================================================
--- branches/energy/source/main/cPhenotype.cc	2007-08-24 01:14:44 UTC (rev 1997)
+++ branches/energy/source/main/cPhenotype.cc	2007-08-24 02:02:02 UTC (rev 1998)
@@ -1377,6 +1377,19 @@
   energy_store = max(0.0, min(value, (double) m_world->GetConfig().ENERGY_CAP.Get()));
 }
 
+bool cPhenotype::DoubleEnergyUsage(double energy_req) {
+  if(GetStoredEnergy() < energy_req) {
+    return false;
+  }
+  executionRatio *= 2.0;
+  return true;
+}
+
+void cPhenotype::HalfEnergyUsage() {
+  executionRatio *= 0.5;
+}
+
+
 /**
 Credit organism with energy reward, but only update energy store if APPLY_ENERGY_METHOD = "on task completion" (1)
  */
@@ -1424,7 +1437,7 @@
   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(cMerit::EnergyToMerit(GetStoredEnergy(), m_world) * GetExecutionRatio());
+  cMerit parentMerit = cMerit(cMerit::EnergyToMerit(GetStoredEnergy() * GetEnergyUsageRatio(), m_world));
   SetMerit(parentMerit);
   
   return child_energy;

Modified: branches/energy/source/main/cPhenotype.h
===================================================================
--- branches/energy/source/main/cPhenotype.h	2007-08-24 01:14:44 UTC (rev 1997)
+++ branches/energy/source/main/cPhenotype.h	2007-08-24 02:02:02 UTC (rev 1998)
@@ -278,7 +278,7 @@
 
   /////////////////////  Accessors -- Retrieving  ////////////////////
   const cMerit & GetMerit() const { assert(initialized == true); return merit; }
-  double GetExecutionRatio() const { assert(initialized == true); return executionRatio; }
+  double GetEnergyUsageRatio() const { assert(initialized == true); return executionRatio; }
   int GetGenomeLength() const { assert(initialized == true); return genome_length; }
   int GetCopiedSize() const { assert(initialized == true); return copied_size; }
   int GetExecutedSize() const { assert(initialized == true); return executed_size; }
@@ -465,8 +465,8 @@
   bool& ChildFertile() { assert(initialized == true); return child_fertile; }
   bool& IsMultiThread() { assert(initialized == true); return is_multi_thread; }
   
-  void DoubleEnergyUsage() { executionRatio *= 2.0; }
-  void HalfEnergyUsage() { executionRatio *= 0.5; }
+  bool DoubleEnergyUsage(double inst_energy_cost);
+  void HalfEnergyUsage();
   void RefreshEnergy();
   void ApplyToEnergyStore();
   double ExtractParentEnergy();

Modified: branches/energy/source/main/cPopulationCell.cc
===================================================================
--- branches/energy/source/main/cPopulationCell.cc	2007-08-24 01:14:44 UTC (rev 1997)
+++ branches/energy/source/main/cPopulationCell.cc	2007-08-24 02:02:02 UTC (rev 1998)
@@ -176,7 +176,7 @@
     if(uptake_energy != 0.0) {
       // update energy and merit
       m_organism->GetPhenotype().ReduceEnergy(-1.0 * uptake_energy);
-      m_organism->GetPhenotype().SetMerit(cMerit(cMerit::EnergyToMerit(m_organism->GetPhenotype().GetStoredEnergy(), m_world) * m_organism->GetPhenotype().GetExecutionRatio()));
+      m_organism->GetPhenotype().SetMerit(cMerit(cMerit::EnergyToMerit(m_organism->GetPhenotype().GetStoredEnergy() * m_organism->GetPhenotype().GetEnergyUsageRatio(), m_world)));
     }
   }
 }




More information about the Avida-cvs mailing list