[Avida-SVN] r2403 - in development: source/cpu source/main tests/bgs_dyn_mutrates_births tests/bgs_dyn_mutrates_fit tests/bgs_dyn_mutrates_rank tests/sex/config tests/sex-asex/config tests/sex-modular/config

barrick at myxo.css.msu.edu barrick at myxo.css.msu.edu
Fri Feb 29 14:46:06 PST 2008


Author: barrick
Date: 2008-02-29 17:46:06 -0500 (Fri, 29 Feb 2008)
New Revision: 2403

Modified:
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
   development/tests/bgs_dyn_mutrates_births/test_list
   development/tests/bgs_dyn_mutrates_fit/test_list
   development/tests/bgs_dyn_mutrates_rank/test_list
   development/tests/sex-asex/config/avida.cfg
   development/tests/sex-modular/config/avida.cfg
   development/tests/sex/config/avida.cfg
Log:
BIRTH_METHOD 4 (mass action) and 6 (deme mass action) now honor the setting of PREFER_EMPTY. (Previously they did not prefer empty cells.) Several test configurations amended to go along with this change.

Copied David's bit consensus instructions from cExperimentalCPU into cHardwareCPU.


Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/source/cpu/cHardwareCPU.cc	2008-02-29 22:46:06 UTC (rev 2403)
@@ -374,8 +374,14 @@
     tInstLibEntry<tMethod>("s-regulate", &cHardwareCPU::Inst_SenseRegulate),
     tInstLibEntry<tMethod>("numberate", &cHardwareCPU::Inst_Numberate),
     tInstLibEntry<tMethod>("numberate-24", &cHardwareCPU::Inst_Numberate24),
+
+    // Bit Consensus
     tInstLibEntry<tMethod>("bit-cons", &cHardwareCPU::Inst_BitConsensus),
     tInstLibEntry<tMethod>("bit-cons-24", &cHardwareCPU::Inst_BitConsensus24),
+    tInstLibEntry<tMethod>("if-cons", &cHardwareCPU::Inst_IfConsensus, 0, "Execute next instruction if ?BX? in consensus, else skip it"),
+    tInstLibEntry<tMethod>("if-cons-24", &cHardwareCPU::Inst_IfConsensus24, 0, "Execute next instruction if ?BX[0:23]? in consensus , else skip it"),
+    tInstLibEntry<tMethod>("if-less-cons", &cHardwareCPU::Inst_IfLessConsensus, 0, "Execute next instruction if Count(?BX?) < Count(?CX?), else skip it"),
+    tInstLibEntry<tMethod>("if-less-cons-24", &cHardwareCPU::Inst_IfLessConsensus24, 0, "Execute next instruction if Count(?BX[0:23]?) < Count(?CX[0:23]?), else skip it"),
 
     // Energy usage
     tInstLibEntry<tMethod>("double-energy-usage", &cHardwareCPU::Inst_DoubleEnergyUsage, nInstFlag::STALL),
@@ -4654,40 +4660,69 @@
   return code;
 }
 
-/*! 
-  Sets BX to 1 if >=50% of the bits in the specified register places
-  are 1's and zero otherwise.
-*/
 
+//// Copied from cHardwareExperimental -- @JEB
+static const unsigned int CONSENSUS = (sizeof(int) * 8) / 2;
+static const unsigned int CONSENSUS24 = 12;
+static const unsigned int MASK24 = 0xFFFFFF;
+
+inline unsigned int cHardwareCPU::BitCount(unsigned int value) const
+{
+  const unsigned int w = value - ((value >> 1) & 0x55555555);
+  const unsigned int x = (w & 0x33333333) + ((w >> 2) & 0x33333333);
+  const unsigned int bit_count = ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
+  return bit_count;
+}
+
 bool cHardwareCPU::Inst_BitConsensus(cAvidaContext& ctx)
 {
-  return BitConsensus(ctx, sizeof(int) * 8);
+  const int reg_used = FindModifiedRegister(REG_BX);
+  const int op1 = FindModifiedNextRegister(reg_used);
+  GetRegister(reg_used) = (BitCount(GetRegister(op1)) >= CONSENSUS) ? 1 : 0;
+  return true; 
 }
 
-// Looks at only the lower 24 bits
 bool cHardwareCPU::Inst_BitConsensus24(cAvidaContext& ctx)
 {
-  return BitConsensus(ctx, 24);
+  const int reg_used = FindModifiedRegister(REG_BX);
+  const int op1 = FindModifiedNextRegister(reg_used);
+  GetRegister(reg_used) = (BitCount(GetRegister(op1) & MASK24) >= CONSENSUS24) ? 1 : 0;
+  return true; 
 }
 
-bool cHardwareCPU::BitConsensus(cAvidaContext& ctx, const unsigned int num_bits)
+bool cHardwareCPU::Inst_IfConsensus(cAvidaContext& ctx)
 {
-  assert(num_bits <= sizeof(int) * 8);
+  const int op1 = FindModifiedRegister(REG_BX);
+  if (BitCount(GetRegister(op1)) <  CONSENSUS)  IP().Advance();
+  return true;
+}
 
-  const int reg_used = FindModifiedRegister(REG_BX);
-  int reg_val = GetRegister(REG_CX);
-  unsigned int bits_on = 0;
-  
-  for (unsigned int i = 0; i < num_bits; i++)
-  {
-    bits_on += (reg_val & 1);
-    reg_val >>= 1;
-  }
-  
-  GetRegister(reg_used) = ( bits_on >= (sizeof(int) * 8)/2 ) ? 1 : 0;
-  return true; 
+bool cHardwareCPU::Inst_IfConsensus24(cAvidaContext& ctx)
+{
+  const int op1 = FindModifiedRegister(REG_BX);
+  if (BitCount(GetRegister(op1) & MASK24) <  CONSENSUS24)  IP().Advance();
+  return true;
 }
 
+bool cHardwareCPU::Inst_IfLessConsensus(cAvidaContext& ctx)
+{
+  const int op1 = FindModifiedRegister(REG_BX);
+  const int op2 = FindModifiedNextRegister(op1);
+  if (BitCount(GetRegister(op1)) >=  BitCount(GetRegister(op2)))  IP().Advance();
+  return true;
+}
+
+bool cHardwareCPU::Inst_IfLessConsensus24(cAvidaContext& ctx)
+{
+  const int op1 = FindModifiedRegister(REG_BX);
+  const int op2 = FindModifiedNextRegister(op1);
+  if (BitCount(GetRegister(op1) & MASK24) >=  BitCount(GetRegister(op2) & MASK24))  IP().Advance();
+  return true;
+}
+
+//// End copied from cHardwareExperimental
+
+
 /*! Send a message to the organism that is currently faced by this cell,
 where the label field of sent message is from register ?BX?, and the data field
 is from register ~?BX?.

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/source/cpu/cHardwareCPU.h	2008-02-29 22:46:06 UTC (rev 2403)
@@ -579,13 +579,15 @@
   void NextPromoter();
   int  Numberate(int _pos, int _dir, int _num_bits = 0);
   
-  
   //// Bit consensus functions ////
+  inline unsigned int BitCount(unsigned int value) const;
   bool Inst_BitConsensus(cAvidaContext& ctx);
   bool Inst_BitConsensus24(cAvidaContext& ctx);
-  bool BitConsensus(cAvidaContext& ctx, const unsigned int num_bits);
-
-
+  bool Inst_IfConsensus(cAvidaContext& ctx);
+  bool Inst_IfConsensus24(cAvidaContext& ctx);  
+  bool Inst_IfLessConsensus(cAvidaContext& ctx);
+  bool Inst_IfLessConsensus24(cAvidaContext& ctx);
+  
   //// Messaging ////
   bool Inst_SendMessage(cAvidaContext& ctx);
   bool Inst_RetrieveMessage(cAvidaContext& ctx);

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/source/main/cPopulation.cc	2008-02-29 22:46:06 UTC (rev 2403)
@@ -133,6 +133,7 @@
   
   // Allocate the cells, resources, and market.
   cell_array.ResizeClear(num_cells);
+  empty_cell_id_array.ResizeClear(cell_array.GetSize());
   market.Resize(MARKET_SIZE);
   
   // Setup the cells.  Do things that are not dependent upon topology here.
@@ -2181,6 +2182,26 @@
   }
   // @AWC If not migrating try out global/full-deme birth methods first...
   else if (birth_method == POSITION_CHILD_FULL_SOUP_RANDOM) {
+   
+    // @JEB Look randomly within empty cells, if requested
+    if (m_world->GetConfig().PREFER_EMPTY.Get()) {
+      
+      // Note: empty_cell_id_array was resized to be large enough to hold
+      // all cells in the cPopulation when it was created. Using functions
+      // that resize it (like Push) will slow this code down considerably.
+      // Instead, we keep track of how much of this memory we are using.
+      
+      int num_empty_cells = 0;      
+      for (int i=0; i<cell_array.GetSize(); i++) {
+        if (GetCell(i).IsOccupied() == false) empty_cell_id_array[num_empty_cells++] = i;
+      }
+     
+       if (num_empty_cells > 0) {
+        int out_pos = m_world->GetRandom().GetUInt(num_empty_cells);
+        return GetCell(empty_cell_id_array[out_pos]);
+      } 
+    }
+
     int out_pos = m_world->GetRandom().GetUInt(cell_array.GetSize());
     while (parent_ok == false && out_pos == parent_cell.GetID()) {
       out_pos = m_world->GetRandom().GetUInt(cell_array.GetSize());
@@ -2196,18 +2217,39 @@
     return *out_cell;
   }
   else if (birth_method == POSITION_CHILD_DEME_RANDOM) {
-
     const int deme_id = parent_cell.GetDemeID();    
     const int deme_size = deme_array[deme_id].GetSize();
     
+    deme_array[deme_id].IncBirthCount();
+
+    // @JEB Look randomly within empty cells, if requested
+    if (m_world->GetConfig().PREFER_EMPTY.Get()) {
+      
+      // Note: empty_cell_id_array was resized to be large enough to hold
+      // all cells in the cPopulation when it was created. Using functions
+      // that resize it (like Push) will slow this code down considerably.
+      // Instead, we keep track of how much of this memory we are using.
+      
+      int num_empty_cells = 0; 
+      for (int i=0; i<deme_array[deme_id].GetSize(); i++) {
+        int cell_id = deme_array[deme_id].GetCellID(i);
+        if (!GetCell(cell_id).IsOccupied()) /*return cell;*/ empty_cell_id_array[num_empty_cells++] = cell_id;
+      }
+     
+      if (num_empty_cells > 0) {
+        int out_pos = m_world->GetRandom().GetUInt(num_empty_cells);
+        return GetCell(empty_cell_id_array[out_pos]);
+      } 
+    }
+
     int out_pos = m_world->GetRandom().GetUInt(deme_size);
     int out_cell_id = deme_array[deme_id].GetCellID(out_pos);
+    
     while (parent_ok == false && out_cell_id == parent_cell.GetID()) {
       out_pos = m_world->GetRandom().GetUInt(deme_size);
       out_cell_id = deme_array[deme_id].GetCellID(out_pos);
     }
     
-    deme_array[deme_id].IncBirthCount();
     return GetCell(out_cell_id);    
   }
   else if (birth_method == POSITION_CHILD_PARENT_FACING) {
@@ -3043,7 +3085,6 @@
   return cell_array[in_num];
 }
 
-
 void cPopulation::UpdateResources(const tArray<double> & res_change)
 {
   resource_count.Modify(res_change);

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/source/main/cPopulation.h	2008-02-29 22:46:06 UTC (rev 2403)
@@ -90,6 +90,7 @@
   cWorld* m_world;
   cSchedule* schedule;                // Handles allocation of CPU cycles
   tArray<cPopulationCell> cell_array;  // Local cells composing the population
+  tArray<int> empty_cell_id_array;     // Used for PREFER_EMPTY birth methods
   cResourceCount resource_count;       // Global resources available
   cBirthChamber birth_chamber;         // Global birth chamber.
   tArray<tList<cSaleItem> > market;   // list of lists of items for sale, each list goes with 1 label

Modified: development/tests/bgs_dyn_mutrates_births/test_list
===================================================================
--- development/tests/bgs_dyn_mutrates_births/test_list	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/tests/bgs_dyn_mutrates_births/test_list	2008-02-29 22:46:06 UTC (rev 2403)
@@ -1,7 +1,7 @@
 ;--- Begin Test Configuration File (test_list) ---
 [main]
 ; Command line arguments to pass to the application
-args = -s 1 -set BIRTH_METHOD 6 -set NUM_DEMES 30 -set EVENT_FILE events-Test-NumBirthsSelection.cfg -set INST_SET inst_set.divide -set ENVIRONMENT_FILE environment.77 -set WORLD_X 15 -set WORLD_Y 60 -set CHILD_SIZE_RANGE 1.0 -set COPY_MUT_PROB 0.0 -set DIVIDE_INS_PROB 0.0 -set DIVIDE_DEL_PROB 0.0 -set DIV_MUT_PROB 0.03 -set START_CREATURE org.div1
+args = -s 1 -set BIRTH_METHOD 6 -set PREFER_EMPTY 0 -set NUM_DEMES 30 -set EVENT_FILE events-Test-NumBirthsSelection.cfg -set INST_SET inst_set.divide -set ENVIRONMENT_FILE environment.77 -set WORLD_X 15 -set WORLD_Y 60 -set CHILD_SIZE_RANGE 1.0 -set COPY_MUT_PROB 0.0 -set DIVIDE_INS_PROB 0.0 -set DIVIDE_DEL_PROB 0.0 -set DIV_MUT_PROB 0.03 -set START_CREATURE org.div1
 app = %(default_app)s
 
 nonzeroexit = disallow   ; Exit code handling (disallow, allow, or require)

Modified: development/tests/bgs_dyn_mutrates_fit/test_list
===================================================================
--- development/tests/bgs_dyn_mutrates_fit/test_list	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/tests/bgs_dyn_mutrates_fit/test_list	2008-02-29 22:46:06 UTC (rev 2403)
@@ -1,7 +1,7 @@
 ;--- Begin Test Configuration File (test_list) ---
 [main]
 ; Command line arguments to pass to the application
-args = -s 1 -set BIRTH_METHOD 6 -set NUM_DEMES 30 -set EVENT_FILE events-Test-ParentFitSelection.cfg -set INST_SET inst_set.divide -set ENVIRONMENT_FILE environment.77 -set WORLD_X 15 -set WORLD_Y 60 -set CHILD_SIZE_RANGE 1.0 -set COPY_MUT_PROB 0.0 -set DIVIDE_INS_PROB 0.0 -set DIVIDE_DEL_PROB 0.0 -set DIV_MUT_PROB 0.03 -set START_CREATURE org.div1
+args = -s 1 -set BIRTH_METHOD 6 -set PREFER_EMPTY 0 -set NUM_DEMES 30 -set EVENT_FILE events-Test-ParentFitSelection.cfg -set INST_SET inst_set.divide -set ENVIRONMENT_FILE environment.77 -set WORLD_X 15 -set WORLD_Y 60 -set CHILD_SIZE_RANGE 1.0 -set COPY_MUT_PROB 0.0 -set DIVIDE_INS_PROB 0.0 -set DIVIDE_DEL_PROB 0.0 -set DIV_MUT_PROB 0.03 -set START_CREATURE org.div1
 
 app = %(default_app)s
 nonzeroexit = disallow   ; Exit code handling (disallow, allow, or require)

Modified: development/tests/bgs_dyn_mutrates_rank/test_list
===================================================================
--- development/tests/bgs_dyn_mutrates_rank/test_list	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/tests/bgs_dyn_mutrates_rank/test_list	2008-02-29 22:46:06 UTC (rev 2403)
@@ -1,7 +1,7 @@
 ;--- Begin Test Configuration File (test_list) ---
 [main]
 ; Command line arguments to pass to the application
-args = -s 1 -set BIRTH_METHOD 6 -set NUM_DEMES 30 -set EVENT_FILE events-Test-RankSelection.cfg -set INST_SET inst_set.divide -set ENVIRONMENT_FILE environment.77 -set WORLD_X 15 -set WORLD_Y 60 -set CHILD_SIZE_RANGE 1.0 -set COPY_MUT_PROB 0.0 -set DIVIDE_INS_PROB 0.0 -set DIVIDE_DEL_PROB 0.0 -set DIV_MUT_PROB 0.03 -set START_CREATURE org.div1
+args = -s 1 -set BIRTH_METHOD 6 -set PREFER_EMPTY 0 -set NUM_DEMES 30 -set EVENT_FILE events-Test-RankSelection.cfg -set INST_SET inst_set.divide -set ENVIRONMENT_FILE environment.77 -set WORLD_X 15 -set WORLD_Y 60 -set CHILD_SIZE_RANGE 1.0 -set COPY_MUT_PROB 0.0 -set DIVIDE_INS_PROB 0.0 -set DIVIDE_DEL_PROB 0.0 -set DIV_MUT_PROB 0.03 -set START_CREATURE org.div1
 
 app = %(default_app)s
 nonzeroexit = disallow   ; Exit code handling (disallow, allow, or require)

Modified: development/tests/sex/config/avida.cfg
===================================================================
--- development/tests/sex/config/avida.cfg	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/tests/sex/config/avida.cfg	2008-02-29 22:46:06 UTC (rev 2403)
@@ -45,7 +45,7 @@
                          # 4 = Replace random from population (Mass Action)
                          # 5 = Replace oldest in entire population (like Tierra)
                          # 6 = Replace random within deme
-PREFER_EMPTY 1           # Give empty cells preference in offsping placement?
+PREFER_EMPTY 0           # Give empty cells preference in offsping placement?
 DEATH_METHOD 2           # 0 = Never die of old age.
                          # 1 = Die when inst executed = AGE_LIMIT (+deviation)
                          # 2 = Die when inst executed = length*AGE_LIMIT (+dev)

Modified: development/tests/sex-asex/config/avida.cfg
===================================================================
--- development/tests/sex-asex/config/avida.cfg	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/tests/sex-asex/config/avida.cfg	2008-02-29 22:46:06 UTC (rev 2403)
@@ -45,7 +45,7 @@
                          # 4 = Replace random from population (Mass Action)
                          # 5 = Replace oldest in entire population (like Tierra)
                          # 6 = Replace random within deme
-PREFER_EMPTY 1           # Give empty cells preference in offsping placement?
+PREFER_EMPTY 0           # Give empty cells preference in offsping placement?
 DEATH_METHOD 2           # 0 = Never die of old age.
                          # 1 = Die when inst executed = AGE_LIMIT (+deviation)
                          # 2 = Die when inst executed = length*AGE_LIMIT (+dev)

Modified: development/tests/sex-modular/config/avida.cfg
===================================================================
--- development/tests/sex-modular/config/avida.cfg	2008-02-29 21:54:46 UTC (rev 2402)
+++ development/tests/sex-modular/config/avida.cfg	2008-02-29 22:46:06 UTC (rev 2403)
@@ -45,7 +45,7 @@
                          # 4 = Replace random from population (Mass Action)
                          # 5 = Replace oldest in entire population (like Tierra)
                          # 6 = Replace random within deme
-PREFER_EMPTY 1           # Give empty cells preference in offsping placement?
+PREFER_EMPTY 0           # Give empty cells preference in offsping placement?
 DEATH_METHOD 2           # 0 = Never die of old age.
                          # 1 = Die when inst executed = AGE_LIMIT (+deviation)
                          # 2 = Die when inst executed = length*AGE_LIMIT (+dev)




More information about the Avida-cvs mailing list