[Avida-SVN] r1054 - in development/source: cpu main

ofria at myxo.css.msu.edu ofria at myxo.css.msu.edu
Tue Oct 17 19:52:33 PDT 2006


Author: ofria
Date: 2006-10-17 22:52:32 -0400 (Tue, 17 Oct 2006)
New Revision: 1054

Modified:
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/cpu/cTestCPUInterface.cc
   development/source/cpu/cTestCPUInterface.h
   development/source/main/cAvidaConfig.h
   development/source/main/cDeme.cc
   development/source/main/cDeme.h
   development/source/main/cEnvironment.cc
   development/source/main/cOrgInterface.h
   development/source/main/cOrganism.h
   development/source/main/cPhenotype.cc
   development/source/main/cPhenotype.h
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
   development/source/main/cPopulationInterface.cc
   development/source/main/cPopulationInterface.h
   development/source/main/cTaskLib.cc
Log:
Added a counter to each deme that keeps track of how many organisms are
currently alive in that deme.

Created a new instruction called 'spawn_deme' that will choose a random
individual in the current deme and spawn it into another deme chosen at
random.  This process entrails wiping out all indvididuals in the target
deme so that the spawned indivdual has no compition there.  This is being
created as an instruction, but I expect it will usually be used as the
result of a task; when the deme completes the task, it gets to spawn.

Removed the DIE_PROB setting in avida.cfg.  This was supposed to give a 
probability of death whenever the die instruction was executed, but this
same effect could be done by making the die instruction have a chance of 
failure (which is working fo all instructions currently).

Plus some minor code and comment cleanups...

Up Next: I'll be cleaning up the task code and creating some deme-level
tasks to associate the spawn-deme instruction with.



Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/cpu/cHardwareCPU.cc	2006-10-18 02:52:32 UTC (rev 1054)
@@ -340,6 +340,8 @@
     cInstEntryCPU("repro-X",    &cHardwareCPU::Inst_Repro),
     cInstEntryCPU("repro-Y",    &cHardwareCPU::Inst_Repro),
     cInstEntryCPU("repro-Z",    &cHardwareCPU::Inst_Repro),
+
+    cInstEntryCPU("spawn-deme", &cHardwareCPU::Inst_SpawnDeme),
     
     // Suicide
     cInstEntryCPU("kazi",	&cHardwareCPU::Inst_Kazi),
@@ -2449,6 +2451,12 @@
 }
 
 
+bool cHardwareCPU::Inst_SpawnDeme(cAvidaContext& ctx)
+{
+  organism->SpawnDeme();
+  return true;
+}
+
 bool cHardwareCPU::Inst_Kazi(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(REG_AX);
@@ -2467,8 +2475,7 @@
 
 bool cHardwareCPU::Inst_Die(cAvidaContext& ctx)
 {
-  const double die_prob = m_world->GetConfig().DIE_PROB.Get();
-  if(ctx.GetRandom().GetDouble() < die_prob) { organism->Die(); }
+  organism->Die();
   return true; 
 }
 
@@ -2641,6 +2648,7 @@
   organism->DoInput(value_in);
   return true;
 }
+
 bool cHardwareCPU::Inst_TaskIO_Feedback(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(REG_BX);

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/cpu/cHardwareCPU.h	2006-10-18 02:52:32 UTC (rev 1054)
@@ -368,6 +368,7 @@
   bool Inst_InjectRand(cAvidaContext& ctx);
   bool Inst_InjectThread(cAvidaContext& ctx);
   bool Inst_Repro(cAvidaContext& ctx);
+  bool Inst_SpawnDeme(cAvidaContext& ctx);
   bool Inst_Kazi(cAvidaContext& ctx);
   bool Inst_Kazi5(cAvidaContext& ctx);
   bool Inst_Die(cAvidaContext& ctx);

Modified: development/source/cpu/cTestCPUInterface.cc
===================================================================
--- development/source/cpu/cTestCPUInterface.cc	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/cpu/cTestCPUInterface.cc	2006-10-18 02:52:32 UTC (rev 1054)
@@ -71,6 +71,10 @@
   // record the probability it used.
 }
 
+void cTestCPUInterface::SpawnDeme()
+{
+}
+
 bool cTestCPUInterface::SendMessage(cOrgMessage& mess)
 {
   return false;

Modified: development/source/cpu/cTestCPUInterface.h
===================================================================
--- development/source/cpu/cTestCPUInterface.h	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/cpu/cTestCPUInterface.h	2006-10-18 02:52:32 UTC (rev 1054)
@@ -44,6 +44,7 @@
   void UpdateResources(const tArray<double>& res_change);
   void Die();
   void Kaboom(int distance);
+  void SpawnDeme();
   bool SendMessage(cOrgMessage& mess);
   cOrgSinkMessage* NetReceive() { return NULL; } // @DMB - todo: receive message
   bool NetRemoteValidate(cAvidaContext& ctx, cOrgSinkMessage* msg) { return false; } // @DMB - todo: validate message

Modified: development/source/main/cAvidaConfig.h
===================================================================
--- development/source/main/cAvidaConfig.h	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cAvidaConfig.h	2006-10-18 02:52:32 UTC (rev 1054)
@@ -200,7 +200,6 @@
   CONFIG_ADD_VAR(REQUIRED_TASK, int, -1, "Task ID required for successful divide.");
   CONFIG_ADD_VAR(IMMUNITY_TASK, int, -1, "Task providing immunity from the required task.");
   CONFIG_ADD_VAR(REQUIRED_REACTION, int, -1, "Reaction ID required for successful divide.");
-  CONFIG_ADD_VAR(DIE_PROB, double, 0.0, "probability of death when 'die' instruction is executed"); 
 
   CONFIG_ADD_GROUP(MUTATION_GROUP, "Mutations");
   CONFIG_ADD_VAR(POINT_MUT_PROB, double, 0.0, "Mutation rate (per-location per update)");

Modified: development/source/main/cDeme.cc
===================================================================
--- development/source/main/cDeme.cc	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cDeme.cc	2006-10-18 02:52:32 UTC (rev 1054)
@@ -11,6 +11,7 @@
 cDeme::cDeme()
   : width(0)
   , birth_count(0)
+  , org_count(0)
 {
 }
 
@@ -22,6 +23,7 @@
 {
   cell_ids = in_cells;
   birth_count = 0;
+  org_count = 0;
 
   // If width is negative, set it to the full number of cells.
   width = in_width;

Modified: development/source/main/cDeme.h
===================================================================
--- development/source/main/cDeme.h	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cDeme.h	2006-10-18 02:52:32 UTC (rev 1054)
@@ -22,8 +22,9 @@
 class cDeme {
 private:
   tArray<int> cell_ids;
-  int width;
-  int birth_count;
+  int width;        // How wide is the deme?
+  int birth_count;  // How many organisms have been born into deme since reset?
+  int org_count;    // How many organisms are currently in the deme?
 
 public:
   cDeme();
@@ -41,6 +42,10 @@
   void Reset() { birth_count = 0; }
   int GetBirthCount() const { return birth_count; }
   void IncBirthCount() { birth_count++; }
+
+  int GetOrgCount() const { return org_count; }
+  void IncOrgCount() { org_count++; }
+  void DecOrgCount() { org_count--; }
 };
 
 #endif

Modified: development/source/main/cEnvironment.cc
===================================================================
--- development/source/main/cEnvironment.cc	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cEnvironment.cc	2006-10-18 02:52:32 UTC (rev 1054)
@@ -639,10 +639,11 @@
 }
 
 
-bool cEnvironment::TestOutput(cAvidaContext& ctx, cReactionResult& result, cTaskContext& taskctx,
-                              const tBuffer<int>& send_buf, const tBuffer<int>& receive_buf,
-                              const tArray<int>& task_count, const tArray<int>& reaction_count,
-                              const tArray<double>& resource_count) const
+bool cEnvironment::TestOutput(cAvidaContext& ctx, cReactionResult& result,
+	      cTaskContext& taskctx,
+              const tBuffer<int>& send_buf, const tBuffer<int>& receive_buf,
+              const tArray<int>& task_count, const tArray<int>& reaction_count,
+	      const tArray<double>& resource_count) const
 {
   // Do setup for reaction tests...
   m_tasklib.SetupTests(taskctx);

Modified: development/source/main/cOrgInterface.h
===================================================================
--- development/source/main/cOrgInterface.h	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cOrgInterface.h	2006-10-18 02:52:32 UTC (rev 1054)
@@ -7,6 +7,15 @@
  *
  */
 
+// This class is responsible for establishing the interface that links an
+// organism back to its population.  It is a base class that is derived in:
+//
+//   cpu/cTestCPUInterface.{h,cc}      - Test CPU interface
+//   main/cPopulationInterface.{h,cc}  - Main population interface.
+//
+// Make sure that any new function you implment here also has versions in
+// those classes.
+
 #ifndef cOrgInterface_h
 #define cOrgInterface_h
 
@@ -44,6 +53,7 @@
   virtual void UpdateResources(const tArray<double>& res_change) = 0;
   virtual void Die() = 0;
   virtual void Kaboom(int distance) = 0;
+  virtual void SpawnDeme() = 0;
   virtual bool SendMessage(cOrgMessage& mess) = 0;
   virtual cOrgSinkMessage* NetReceive() = 0;
   virtual bool NetRemoteValidate(cAvidaContext& ctx, cOrgSinkMessage* msg) = 0;

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cOrganism.h	2006-10-18 02:52:32 UTC (rev 1054)
@@ -148,6 +148,7 @@
   int GetNextInput() { assert(m_interface); return m_interface->GetInputAt(input_pointer); }
   void Die() { assert(m_interface); m_interface->Die(); }
   void Kaboom(int dist) { assert(m_interface); m_interface->Kaboom(dist);}
+  void SpawnDeme() { assert(m_interface); m_interface->SpawnDeme(); }
   int GetCellID() { assert(m_interface); return m_interface->GetCellID(); }
   int GetDebugInfo() { assert(m_interface); return m_interface->Debug(); }
   int GetID() { return m_id; }

Modified: development/source/main/cPhenotype.cc
===================================================================
--- development/source/main/cPhenotype.cc	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cPhenotype.cc	2006-10-18 02:52:32 UTC (rev 1054)
@@ -498,9 +498,11 @@
   return false; // Nothing happened...
 }
 
-bool cPhenotype::TestOutput(cAvidaContext& ctx, cTaskContext& taskctx, tBuffer<int>& send_buf,
-                            tBuffer<int>& receive_buf, const tArray<double>& res_in,
-                            tArray<double>& res_change, tArray<int>& insts_triggered)
+bool cPhenotype::TestOutput(cAvidaContext& ctx, cTaskContext& taskctx,
+			    tBuffer<int>& send_buf, tBuffer<int>& receive_buf,
+			    const tArray<double>& res_in,
+                            tArray<double>& res_change,
+			    tArray<int>& insts_triggered)
 {
   assert(initialized == true);
 
@@ -524,10 +526,8 @@
   // Start with updating task and reaction counters
   for (int i = 0; i < num_tasks; i++) {
     if (result.TaskDone(i) == true) cur_task_count[i]++;
+    if (result.TaskQuality(i) > 0) cur_task_quality[i]+= result.TaskQuality(i);
   }
-  for (int i = 0; i < num_tasks; i++) {
-	  if (result.TaskQuality(i) > 0) cur_task_quality[i]+= result.TaskQuality(i);
-  }
   for (int i = 0; i < num_reactions; i++) {
     if (result.ReactionTriggered(i) == true) cur_reaction_count[i]++;
   }

Modified: development/source/main/cPhenotype.h
===================================================================
--- development/source/main/cPhenotype.h	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cPhenotype.h	2006-10-18 02:52:32 UTC (rev 1054)
@@ -159,8 +159,10 @@
 
   // Input and Output Reaction Tests
   bool TestInput(tBuffer<int>& inputs, tBuffer<int>& outputs);
-  bool TestOutput(cAvidaContext& ctx, cTaskContext& taskctx, tBuffer<int>& send_buf, tBuffer<int>& receive_buf,
-                  const tArray<double>& res_in, tArray<double>& res_change, tArray<int>& insts_triggered);
+  bool TestOutput(cAvidaContext& ctx, cTaskContext& taskctx,
+		  tBuffer<int>& send_buf, tBuffer<int>& receive_buf,
+                  const tArray<double>& res_in, tArray<double>& res_change,
+		  tArray<int>& insts_triggered);
 
   // State saving and loading, and printing...
   bool SaveState(std::ofstream& fp);

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cPopulation.cc	2006-10-18 02:52:32 UTC (rev 1054)
@@ -429,7 +429,7 @@
   // Update the contents of the target cell.
   KillOrganism(target_cell);
   target_cell.InsertOrganism(*in_organism);
-  
+
   // Setup the inputs in the target cell.
   environment.SetupInputs(ctx, target_cell.input_array);
   
@@ -450,7 +450,11 @@
     reaper_queue.Push(&target_cell);
   }
   
+  // Keep track of statistics for organism counts...
   num_organisms++;
+  if (deme_array.GetSize() > 0) {
+    deme_array[target_cell.GetDemeID()].IncOrgCount();
+  }
   
   // Statistics...
   m_world->GetStats().RecordBirth(target_cell.GetID(), in_genotype->GetID(),
@@ -483,7 +487,11 @@
   // Do the lineage handling
   if (m_world->GetConfig().LOG_LINEAGES.Get()) { m_world->GetClassificationManager().RemoveLineageOrganism(organism); }
   
+  // Update count statistics...
   num_organisms--;
+  if (deme_array.GetSize() > 0) {
+    deme_array[in_cell.GetDemeID()].DecOrgCount();
+  }
   genotype->RemoveOrganism();
 
   for (int i = 0; i < organism->GetNumParasites(); i++) {
@@ -890,20 +898,41 @@
 
 void cPopulation::SpawnDeme(int deme1_id, int deme2_id)
 {
+  // Must spawn into a different deme.
+  assert(deme1_id != deme2_id);
+
+  const int num_demes = deme_array.GetSize();
+
+  // If the second argument is a -1, choose a deme at random.
+  cRandom & random = m_world->GetRandom();
+  while (deme2_id == -1 || deme2_id == deme1_id) {
+    deme2_id = random.GetUInt(num_demes);
+  }
+
+  // Make sure we have all legal values...
+  assert(deme1_id >= 0 && deme1_id < num_demes);
+  assert(deme2_id >= 0 && deme2_id < num_demes);
+
+  // Find the demes that we're working with.
   cDeme & deme1 = deme_array[deme1_id];
   cDeme & deme2 = deme_array[deme2_id];
 
-  // Determine the cell to copy from and to.
-  cRandom & random = m_world->GetRandom();
+  // Make sure that the deme we're copying from has at least 1 organism.
+  assert(deme1.GetOrgCount() > 0);
+
+  // Determine the cell to copy from.
   int cell1_id = deme1.GetCellID( random.GetUInt(deme1.GetSize()) );
-  int cell2_id = deme2.GetCellID( random.GetUInt(deme2.GetSize()) );
-
+  while (cell_array[cell1_id].IsOccupied() == false) {
+    cell1_id = deme1.GetCellID( random.GetUInt(deme1.GetSize()) );
+  }
+  
   // Clear out existing cells in target deme.
   for (int i = 0; i < deme2.GetSize(); i++) {
     KillOrganism(cell_array[ deme2.GetCellID(i) ]);
   }
 
   // And do the spawning.
+  int cell2_id = deme2.GetCellID( random.GetUInt(deme2.GetSize()) );
   InjectClone( cell2_id, *(cell_array[cell1_id].GetOrganism()) );    
 }
 

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cPopulation.h	2006-10-18 02:52:32 UTC (rev 1054)
@@ -139,7 +139,7 @@
   void CompeteDemes(int competition_type);
   void ResetDemes();
   void CopyDeme(int deme1_id, int deme2_id);
-  void SpawnDeme(int deme1_id, int deme2_id);
+  void SpawnDeme(int deme1_id, int deme2_id=-1);
   void PrintDemeStats();
 
   // Process a single organism one instruction...
@@ -164,6 +164,7 @@
   int GetSize() { return cell_array.GetSize(); }
   int GetWorldX() { return world_x; }
   int GetWorldY() { return world_y; }
+  int GetNumDemes() { return deme_array.GetSize(); }
 
   cPopulationCell& GetCell(int in_num);
   const tArray<double>& GetResources() const { return resource_count.GetResources(); }

Modified: development/source/main/cPopulationInterface.cc
===================================================================
--- development/source/main/cPopulationInterface.cc	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cPopulationInterface.cc	2006-10-18 02:52:32 UTC (rev 1054)
@@ -102,6 +102,16 @@
   m_world->GetPopulation().Kaboom(cell, distance);
 }
 
+void cPopulationInterface::SpawnDeme()
+{
+  const int num_demes = m_world->GetPopulation().GetNumDemes();
+
+  // Spawn the current deme; no target ID will put it into a random deme.
+  const int deme_id = m_world->GetPopulation().GetCell(m_cell_id).GetDemeID();
+
+  m_world->GetPopulation().SpawnDeme(deme_id);
+}
+
 bool cPopulationInterface::SendMessage(cOrgMessage & mess)
 {
   mess.SetSenderID(m_cell_id);

Modified: development/source/main/cPopulationInterface.h
===================================================================
--- development/source/main/cPopulationInterface.h	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cPopulationInterface.h	2006-10-18 02:52:32 UTC (rev 1054)
@@ -53,6 +53,7 @@
   void UpdateResources(const tArray<double>& res_change);
   void Die();
   void Kaboom(int distance);
+  void SpawnDeme();
   bool SendMessage(cOrgMessage& mess);
   cOrgSinkMessage* NetReceive();
   bool NetRemoteValidate(cAvidaContext& ctx, cOrgSinkMessage* msg);

Modified: development/source/main/cTaskLib.cc
===================================================================
--- development/source/main/cTaskLib.cc	2006-10-17 18:13:14 UTC (rev 1053)
+++ development/source/main/cTaskLib.cc	2006-10-18 02:52:32 UTC (rev 1054)
@@ -1815,45 +1815,49 @@
   return quality;
 }
 
+
 double cTaskLib::Task_CommEcho(cTaskContext* ctx) const
 {
-	const int test_output = ctx->output_buffer[0];
+  const int test_output = ctx->output_buffer[0];
 
-	tConstListIterator<tBuffer<int> > buff_it(ctx->other_input_buffers);  
+  tConstListIterator<tBuffer<int> > buff_it(ctx->other_input_buffers);  
+  
+  while (buff_it.Next() != NULL) {
+    const tBuffer<int>& cur_buff = *(buff_it.Get());
+    const int buff_size = cur_buff.GetNumStored();
+    for (int i = 0; i < buff_size; i++) {
+      if (test_output == cur_buff[i]) return 1.0;
+    }
+  }
 
-	while (buff_it.Next() != NULL) {
-		const tBuffer<int>& cur_buff = *(buff_it.Get());
-		const int buff_size = cur_buff.GetNumStored();
-		for (int i = 0; i < buff_size; i++) {
-			if (test_output == cur_buff[i]) return 1.0;
-		}
-	}
-
-	return 0.0;
+  return 0.0;
 }
 
+
 double cTaskLib::Task_CommNot(cTaskContext* ctx) const
 {
-	const int test_output = ctx->output_buffer[0];
+  const int test_output = ctx->output_buffer[0];
+  
+  tConstListIterator<tBuffer<int> > buff_it(ctx->other_input_buffers);  
+  
+  while (buff_it.Next() != NULL) {
+    const tBuffer<int>& cur_buff = *(buff_it.Get());
+    const int buff_size = cur_buff.GetNumStored();
+    for (int i = 0; i < buff_size; i++) {
+      if (test_output == (0-(cur_buff[i]+1))) return 1.0;
+    }
+  }
+  
+  return 0.0;
+}
 
-	tConstListIterator<tBuffer<int> > buff_it(ctx->other_input_buffers);  
 
-	while (buff_it.Next() != NULL) {
-		const tBuffer<int>& cur_buff = *(buff_it.Get());
-		const int buff_size = cur_buff.GetNumStored();
-		for (int i = 0; i < buff_size; i++) {
-			if (test_output == (0-(cur_buff[i]+1))) return 1.0;
-		}
-	}
-
-	return 0.0;
-}
-
 double cTaskLib::Task_NetSend(cTaskContext* ctx) const
 {
   return 1.0 * ctx->net_completed;
 }
 
+
 double cTaskLib::Task_NetReceive(cTaskContext* ctx) const
 {
   if (ctx->net_valid) return 1.0;




More information about the Avida-cvs mailing list