[avida-cvs] avida CVS commits: /current/source/main birth_chamber.cc birth_chamber.hh population.cc /current/source/tools functions.hh

mercere99 avida-cvs at alife.org
Wed Oct 15 23:18:14 PDT 2003


mercere99		Wed Oct 15 15:18:14 2003 EDT

  Modified files:              
    /avida/current/source/main	birth_chamber.cc birth_chamber.hh 
                              	population.cc 
    /avida/current/source/tools	functions.hh 
  Log:
  Implemented proportional merit with sex...
  
  
-------------- next part --------------
Index: avida/current/source/main/birth_chamber.cc
diff -u avida/current/source/main/birth_chamber.cc:1.6 avida/current/source/main/birth_chamber.cc:1.7
--- avida/current/source/main/birth_chamber.cc:1.6	Wed Oct  1 12:03:47 2003
+++ avida/current/source/main/birth_chamber.cc	Wed Oct 15 15:18:14 2003
@@ -27,7 +27,8 @@
 
 bool cBirthChamber::SubmitOffspring(const cGenome & child_genome,
 				    const cOrganism & parent,
-				    tArray<cOrganism *> & child_array)
+				    tArray<cOrganism *> & child_array,
+				    tArray<cMerit> & merit_array)
 {
   // Collect some info for building the child.
   const cPopulationInterface & pop_interface = parent.PopInterface();
@@ -39,6 +40,8 @@
     // just build the child and return.
     child_array.Resize(1);
     child_array[0] = new cOrganism(child_genome, pop_interface, environment);
+    merit_array.Resize(1);
+    merit_array[0] = parent.GetPhenotype().GetMerit();
     return true;
   }
 
@@ -47,7 +50,8 @@
   // See if there is anything for it to mate with.
   if (genome_waiting == false) {
     // There is nothing waiting!
-    wait_genome = child_genome;
+    wait_entry.genome = child_genome;
+    wait_entry.merit = parent.GetPhenotype().GetMerit();
     genome_waiting = true;
     return false;
   }
@@ -55,35 +59,41 @@
   // There is already someone in the birth chamber
   // We must now do a crossover between the two 
 
-  cCPUMemory genome0 = wait_genome;
-  cCPUMemory genome1 = child_genome;
   genome_waiting = false;
 
-  // Crossover...
-  // get (for now) one region to cross
-
-  int start0 = g_random.GetInt(genome0.GetSize()); 
-  int end0   = g_random.GetInt(genome0.GetSize());
-  int start1 = genome1.GetSize() * start0 / genome0.GetSize();
-  int end1   = genome1.GetSize() * end0 / genome0.GetSize();
-
   // How many crossovers should be do? For now, 0 or 1
   if (parent.GetPhenotype().CrossNum() ==0) {
     child_array.Resize(2);
-    child_array[0] = new cOrganism(genome0, pop_interface, environment);
-    child_array[1] = new cOrganism(genome1, pop_interface, environment);    
+    child_array[0] = new cOrganism(wait_entry.genome, pop_interface,
+				   environment);
+    child_array[1] = new cOrganism(child_genome, pop_interface, environment);
+    merit_array.Resize(2);
+    merit_array[0] = wait_entry.merit;
+    merit_array[1] = parent.GetPhenotype().GetMerit();
     return true;
   }
   else {
+    cCPUMemory genome0 = wait_entry.genome;
+    cCPUMemory genome1 = child_genome;
+
+    // Crossover...
+    // get (for now) one region to cross
+    
+    double start_frac = g_random.GetDouble();
+    double end_frac = g_random.GetDouble();
+
+    if (start_frac > end_frac) Swap(start_frac, end_frac);
+
+    int start0 = (int) (start_frac * genome0.GetSize());
+    int end0   = (int) (end_frac * genome0.GetSize());
+    int start1 = (int) (start_frac * genome1.GetSize());
+    int end1   = (int) (end_frac * genome1.GetSize());
+
     assert( start0 >= 0  &&  start0 < genome0.GetSize() );
     assert( end0   >= 0  &&  end0   < genome0.GetSize() ); 
     assert( start1 >= 0  &&  start1 < genome1.GetSize() );
     assert( end1   >= 0  &&  end1   < genome1.GetSize() );
   
-    // @CAO for the moment, force start to be less than end.
-    if (start0 > end0) Swap(start0, end0);
-    if (start1 > end1) Swap(start1, end1);
-  
     // Calculate size of sections crossing over...
     int size0 = end0 - start0;
     int size1 = end1 - start1;
@@ -116,6 +126,16 @@
     child_array.Resize(2);
     child_array[0] = new cOrganism(genome0, pop_interface, environment);
     child_array[1] = new cOrganism(genome1, pop_interface, environment);
+
+    double cut_frac = end_frac - start_frac;
+    double stay_frac = 1.0 - cut_frac;
+
+    double merit0 = wait_entry.merit.GetDouble();
+    double merit1 = parent.GetPhenotype().GetMerit().GetDouble();
+    merit_array.Resize(2);
+
+    merit_array[0] = stay_frac * merit0 + cut_frac * merit1;
+    merit_array[1] = stay_frac * merit1 + cut_frac * merit0;
 
     return true;
   }
Index: avida/current/source/main/birth_chamber.hh
diff -u avida/current/source/main/birth_chamber.hh:1.4 avida/current/source/main/birth_chamber.hh:1.5
--- avida/current/source/main/birth_chamber.hh:1.4	Thu Apr  3 20:58:55 2003
+++ avida/current/source/main/birth_chamber.hh	Wed Oct 15 15:18:14 2003
@@ -9,6 +9,7 @@
 #define BIRTH_CHAMBER_HH
 
 #include "../tools/tList.hh"
+#include "../tools/merit.hh"
 
 #include "../cpu/cpu_memory.hh"
 #include "../cpu/label.hh"
@@ -25,8 +26,15 @@
 
 class cBirthChamber {
 private:
+  class cBirthEntry {
+  public:
+    cCPUMemory genome;
+    cMerit merit;
+  };
+
+  cBirthEntry wait_entry;
+
   // For the moment, the following variable is the only one being used.
-  cCPUMemory wait_genome;
   bool genome_waiting;  // Is there are genome waiting to be born?
   
 public:
@@ -36,7 +44,8 @@
   // Handle manipulations & tests of genome.  Return false if divide process
   // should halt.  Place offspring in child_array.
   bool SubmitOffspring(const cGenome & child_genome, const cOrganism & parent,
-		       tArray<cOrganism *> & child_array);
+		       tArray<cOrganism *> & child_array,
+		       tArray<cMerit> & merit_array);
 };
 
 #endif
Index: avida/current/source/main/population.cc
diff -u avida/current/source/main/population.cc:1.119 avida/current/source/main/population.cc:1.120
--- avida/current/source/main/population.cc:1.119	Tue Oct 14 19:57:58 2003
+++ avida/current/source/main/population.cc	Wed Oct 15 15:18:14 2003
@@ -250,7 +250,9 @@
   assert(&parent_organism != NULL);
 
   tArray<cOrganism *> child_array;
-  birth_chamber.SubmitOffspring(child_genome, parent_organism, child_array);
+  tArray<cMerit> merit_array;
+  birth_chamber.SubmitOffspring(child_genome, parent_organism,
+				child_array, merit_array);
 
   cPhenotype & parent_phenotype = parent_organism.GetPhenotype();
 
@@ -314,7 +316,9 @@
   // Go back into a for-loop and continue to deal with the children.
   for (int i = 0; i < child_array.GetSize(); i++) {
     const int child_length = child_array[i]->GetGenome().GetSize();
-    child_array[i]->GetPhenotype().SetupOffspring(parent_phenotype,child_length);
+    child_array[i]->GetPhenotype().
+      SetupOffspring(parent_phenotype,child_length);
+    child_array[i]->GetPhenotype().SetMerit(merit_array[i]);
   }
 
   // If we're not about to kill the parent, do some extra work on it.
@@ -543,53 +547,48 @@
 
 void cPopulation::Kaboom(cPopulationCell & in_cell)
 {
-
-	cOrganism * organism = in_cell.GetOrganism();
-    cGenotype * genotype = organism->GetGenotype();
-	cGenome genome = genotype->GetGenome();
-	int id = genotype->GetID();
-	
-	int radius = 2;
-	int distance = 0;
-	int count = 0;
-
-	for (int i=-1*radius; i<=radius; i++)
-	{
-		for (int j=-1*radius; j<=radius; j++)
-		{
-			cPopulationCell & death_cell = cell_array[Neighbor(in_cell.GetID(), world_x, world_y, i, j)];
-			//do we actually have something to kill?
-			if (death_cell.IsOccupied() == false) continue;
-				
-			cOrganism * org_temp = death_cell.GetOrganism();
-			cGenotype * gene_temp = org_temp->GetGenotype();
-		
-			if (distance == 0)
-			{
-				int temp_id = gene_temp->GetID();
-				if (temp_id != id)
-				{
-					KillOrganism(death_cell);
-					count++;
-				}
-			}
-			else
-			{
-				cGenome genome_temp = gene_temp->GetGenome();
-				int diff=0;
-				for (int i=0; i<genome_temp.GetSize(); i++)
-					if (genome_temp.AsString()[i] != genome.AsString()[i])
-						diff++;
-				if (diff > distance)
-				{
-					KillOrganism(death_cell);
-					count++;
-				}
-			}
-		}
+  cOrganism * organism = in_cell.GetOrganism();
+  cGenotype * genotype = organism->GetGenotype();
+  cGenome genome = genotype->GetGenome();
+  int id = genotype->GetID();
+  
+  int radius = 2;
+  int distance = 0;
+  int count = 0;
+  
+  for (int i=-1*radius; i<=radius; i++) {
+    for (int j=-1*radius; j<=radius; j++) {
+      cPopulationCell & death_cell =
+	cell_array[Neighbor(in_cell.GetID(), world_x, world_y, i, j)];
+      //do we actually have something to kill?
+      if (death_cell.IsOccupied() == false) continue;
+      
+      cOrganism * org_temp = death_cell.GetOrganism();
+      cGenotype * gene_temp = org_temp->GetGenotype();
+      
+      if (distance == 0) {
+	int temp_id = gene_temp->GetID();
+	if (temp_id != id) {
+	  KillOrganism(death_cell);
+	  count++;
 	}
-	KillOrganism(in_cell);
-	//my prediction = 92% and, 28 get equals
+      }
+      else {	
+	cGenome genome_temp = gene_temp->GetGenome();
+	int diff=0;
+	for (int i=0; i<genome_temp.GetSize(); i++)
+	  if (genome_temp.AsString()[i] != genome.AsString()[i])
+	    diff++;
+	if (diff > distance)
+	  {
+	    KillOrganism(death_cell);
+	    count++;
+	  }
+      }
+    }
+  }
+  KillOrganism(in_cell);
+  // @SLG my prediction = 92% and, 28 get equals
 }
 
 /**
Index: avida/current/source/tools/functions.hh
diff -u avida/current/source/tools/functions.hh:1.11 avida/current/source/tools/functions.hh:1.12
--- avida/current/source/tools/functions.hh:1.11	Mon May 19 12:30:13 2003
+++ avida/current/source/tools/functions.hh	Wed Oct 15 15:18:14 2003
@@ -40,6 +40,13 @@
   in2 = tmp;
 }
 
+inline void Swap(double & in1, double & in2)
+{
+  const double tmp = in1;
+  in1 = in2;
+  in2 = tmp;
+}
+
 inline bool ToggleBool(bool & in_bool)
 {
   if (in_bool == true) in_bool = false;


More information about the Avida-cvs mailing list