[Avida-SVN] r1804 - in branches/energy: Avida.xcodeproj source/main source/tools

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Sat Jul 14 04:44:13 PDT 2007


Author: beckma24
Date: 2007-07-14 07:44:12 -0400 (Sat, 14 Jul 2007)
New Revision: 1804

Modified:
   branches/energy/Avida.xcodeproj/project.pbxproj
   branches/energy/source/main/cBirthChamber.cc
   branches/energy/source/main/cBirthChamber.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
   branches/energy/source/tools/cMerit.cc
   branches/energy/source/tools/cMerit.h
Log:
Added energy model to asexual pair and sexual reproduction.

Modified: branches/energy/Avida.xcodeproj/project.pbxproj
===================================================================
--- branches/energy/Avida.xcodeproj/project.pbxproj	2007-07-13 19:41:39 UTC (rev 1803)
+++ branches/energy/Avida.xcodeproj/project.pbxproj	2007-07-14 11:44:12 UTC (rev 1804)
@@ -208,6 +208,23 @@
 		};
 /* End PBXBuildRule section */
 
+/* Begin PBXBuildStyle section */
+		B5381D550C46841600DE97E0 /* Development */ = {
+			isa = PBXBuildStyle;
+			buildSettings = {
+				COPY_PHASE_STRIP = NO;
+			};
+			name = Development;
+		};
+		B5381D560C46841600DE97E0 /* Deployment */ = {
+			isa = PBXBuildStyle;
+			buildSettings = {
+				COPY_PHASE_STRIP = YES;
+			};
+			name = Deployment;
+		};
+/* End PBXBuildStyle section */
+
 /* Begin PBXContainerItemProxy section */
 		56F555DA0C3B36FC00E2E929 /* PBXContainerItemProxy */ = {
 			isa = PBXContainerItemProxy;
@@ -1754,6 +1771,12 @@
 		DCC30C4D0762532C008F7A48 /* Project object */ = {
 			isa = PBXProject;
 			buildConfigurationList = 702442D70859E0B00059BD9B /* Build configuration list for PBXProject "Avida" */;
+			buildSettings = {
+			};
+			buildStyles = (
+				B5381D550C46841600DE97E0 /* Development */,
+				B5381D560C46841600DE97E0 /* Deployment */,
+			);
 			hasScannedForEncodings = 0;
 			mainGroup = DCC30C490762532C008F7A48;
 			productRefGroup = DCC3164E07626CF3008F7A48 /* Products */;

Modified: branches/energy/source/main/cBirthChamber.cc
===================================================================
--- branches/energy/source/main/cBirthChamber.cc	2007-07-13 19:41:39 UTC (rev 1803)
+++ branches/energy/source/main/cBirthChamber.cc	2007-07-14 11:44:12 UTC (rev 1804)
@@ -158,36 +158,11 @@
   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);
-        
-    // adjust energy in parent
-    parent.GetPhenotype().ReduceEnergy(child_energy - energy_given_at_birth);
-
     // set child energy & merit
+    double child_energy = parent.GetPhenotype().ExtractParentEnergy();
     child_array[0]->GetPhenotype().SetEnergy(child_energy);
-    merit_array[0] = 100 * child_energy / (inst_2_exc);
+    merit_array[0].EnergyToMerit(child_energy, m_world);
     
-
-    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;
@@ -196,7 +171,6 @@
     merit_array[0] = parent.GetPhenotype().GetMerit();
   }
   
-
   // Setup the genotype for the child
   cGenotype * child_genotype = parent.GetGenotype();
   
@@ -227,8 +201,15 @@
 
   // Setup the merits for both children...
   merit_array.Resize(2);
+  child_array[0]->GetPhenotype().SetEnergy(old_entry.energy4Offspring);
   merit_array[0] = old_entry.merit;
-  merit_array[1] = parent.GetPhenotype().GetMerit();
+  if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
+    double energyForOffspring = parent.GetPhenotype().ExtractParentEnergy();
+    child_array[1]->GetPhenotype().SetEnergy(energyForOffspring);
+    merit_array[1].EnergyToMerit(energyForOffspring, m_world);
+  } else {
+    merit_array[1] = parent.GetPhenotype().GetMerit();
+  }
 
   // Setup the genotypes for both children...
   SetupGenotypeInfo(child_array[0], old_entry.parent_genotype, NULL);
@@ -257,7 +238,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 +267,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 +295,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 +326,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 +348,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;
@@ -507,7 +513,6 @@
   } 
 }
 
-
 void cBirthChamber::SetupGenotypeInfo(cOrganism* organism, cGenotype* parent0, cGenotype* parent1)
 {
   // Setup the genotypes for both children...
@@ -584,9 +589,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 +611,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 +641,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(meritOrEnergy0, m_world);
+      meritOrEnergy1 = cMerit::EnergyToMerit(meritOrEnergy1, 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 +663,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(meritOrEnergy0, 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(meritOrEnergy1, m_world);
+      }
+      merit_array[0] = meritOrEnergy1;
 
       // Setup the genotype for the child...
       SetupGenotypeInfo(child_array[0], parent1_genotype, parent0_genotype);
@@ -665,4 +692,3 @@
 
   return true;
 }
-

Modified: branches/energy/source/main/cBirthChamber.h
===================================================================
--- branches/energy/source/main/cBirthChamber.h	2007-07-13 19:41:39 UTC (rev 1803)
+++ branches/energy/source/main/cBirthChamber.h	2007-07-14 11:44:12 UTC (rev 1804)
@@ -45,6 +45,7 @@
 class cGenome;
 class cGenotype;
 class cOrganism;
+#include "cPhenotype.h";
 template <class T> class tArray;
 class cWorld;
 
@@ -54,6 +55,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.
@@ -104,7 +106,6 @@
 
   // Pick a random waiting genome from the nehighborhood for recombination
   int PickRandRecGenome(cAvidaContext& ctx, const int & parent_id, int world_x, int world_y);
-  
 
   cBirthChamber(); // @not_implemented
   cBirthChamber(const cBirthChamber&); // @not_implemented

Modified: branches/energy/source/main/cOrganism.cc
===================================================================
--- branches/energy/source/main/cOrganism.cc	2007-07-13 19:41:39 UTC (rev 1803)
+++ branches/energy/source/main/cOrganism.cc	2007-07-14 11:44:12 UTC (rev 1804)
@@ -226,7 +226,7 @@
                        other_output_list, net_valid, 0, on_divide, received_messages_point);
   bool task_completed = m_phenotype.TestOutput(ctx, taskctx, resource_count, res_change, insts_triggered);
   
-  if(m_world->GetConfig().ENERGY_ENABLED.Get() && m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1 && task_completed) {
+  if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1 && m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1 && task_completed) {
     m_phenotype.RefreshEnergy();
     double newMerit = m_phenotype.ApplyToEnergyStore();
     if(newMerit != -1) {

Modified: branches/energy/source/main/cPhenotype.cc
===================================================================
--- branches/energy/source/main/cPhenotype.cc	2007-07-13 19:41:39 UTC (rev 1803)
+++ branches/energy/source/main/cPhenotype.cc	2007-07-14 11:44:12 UTC (rev 1804)
@@ -1207,6 +1207,38 @@
   return min(100 * energy_store / (m_world->GetConfig().NUM_INST_EXC_BEFORE_0_ENERGY.Get()), (double) m_world->GetConfig().ENERGY_CAP.Get());
 }
 
+double cPhenotype::ExtractParentEnergy() {
+  assert(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();
+  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 offspring
+  double energyGivenToOffspring = min(GetStoredEnergy() * frac_parent_energy_given_at_birth + energy_given_at_birth, energy_cap);
+
+  // TODO: should the parent always get ENERGY_GIVEN_AT_BIRTH?
+  // adjust energy in parent
+  ReduceEnergy(energyGivenToOffspring - 2*energy_given_at_birth);
+
+  // adjust parent merit
+  cMerit parentMerit = cMerit(cMerit::EnergyToMerit(GetStoredEnergy(), m_world));
+  SetMerit(parentMerit);
+
+  return energyGivenToOffspring;
+}
+
 void cPhenotype::DecayAllPromoterRegulation()
 {
   for ( int i=0; i<cur_promoter_weights.GetSize(); i++)

Modified: branches/energy/source/main/cPhenotype.h
===================================================================
--- branches/energy/source/main/cPhenotype.h	2007-07-13 19:41:39 UTC (rev 1803)
+++ branches/energy/source/main/cPhenotype.h	2007-07-14 11:44:12 UTC (rev 1804)
@@ -380,7 +380,8 @@
   ////////////////////  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()); }
+  double ExtractParentEnergy();
+  void SetEnergy(const double value) { energy_store = min(value, (double) m_world->GetConfig().ENERGY_CAP.Get()); }
   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; }

Modified: branches/energy/source/main/cPopulation.cc
===================================================================
--- branches/energy/source/main/cPopulation.cc	2007-07-13 19:41:39 UTC (rev 1803)
+++ branches/energy/source/main/cPopulation.cc	2007-07-14 11:44:12 UTC (rev 1804)
@@ -2562,7 +2562,7 @@
   cPhenotype & phenotype = new_organism->GetPhenotype();
   phenotype.SetupInject(new_genotype->GetGenome());  //TODO  sets merit to lenght of genotype
   
-  if(m_world->GetConfig().ENERGY_ENABLED.Get()) {
+  if(m_world->GetConfig().ENERGY_ENABLED.Get() == 1) {
     double initial_energy = min(m_world->GetConfig().ENERGY_GIVEN_ON_INJECT.Get(), m_world->GetConfig().ENERGY_CAP.Get());
     phenotype.SetEnergy(initial_energy);
   }

Modified: branches/energy/source/tools/cMerit.cc
===================================================================
--- branches/energy/source/tools/cMerit.cc	2007-07-13 19:41:39 UTC (rev 1803)
+++ branches/energy/source/tools/cMerit.cc	2007-07-14 11:44:12 UTC (rev 1804)
@@ -71,8 +71,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: branches/energy/source/tools/cMerit.h
===================================================================
--- branches/energy/source/tools/cMerit.h	2007-07-13 19:41:39 UTC (rev 1803)
+++ branches/energy/source/tools/cMerit.h	2007-07-14 11:44:12 UTC (rev 1804)
@@ -30,7 +30,7 @@
 #include <cmath>
 #include <climits>
 #include <cassert>
-
+#include "cWorld.h"
 class cMerit
 {
 protected:
@@ -94,6 +94,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