[Avida-SVN] r1805 - in branches/dkdev/source: actions cpu main

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Sat Jul 14 09:17:55 PDT 2007


Author: dknoester
Date: 2007-07-14 12:17:54 -0400 (Sat, 14 Jul 2007)
New Revision: 1805

Modified:
   branches/dkdev/source/actions/PopulationActions.cc
   branches/dkdev/source/actions/PrintActions.cc
   branches/dkdev/source/cpu/cHardwareCPU.cc
   branches/dkdev/source/cpu/cHardwareCPU.h
   branches/dkdev/source/main/cDeme.cc
   branches/dkdev/source/main/cDeme.h
   branches/dkdev/source/main/cOrganism.cc
   branches/dkdev/source/main/cPopulation.cc
   branches/dkdev/source/main/cStats.cc
   branches/dkdev/source/main/cStats.h
   branches/dkdev/source/main/cTaskContext.h
   branches/dkdev/source/main/cTaskLib.cc
   branches/dkdev/source/main/cTaskLib.h
Log:
New instruction, task, replicate deme method for satellite send.

Modified: branches/dkdev/source/actions/PopulationActions.cc
===================================================================
--- branches/dkdev/source/actions/PopulationActions.cc	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/actions/PopulationActions.cc	2007-07-14 16:17:54 UTC (rev 1805)
@@ -898,6 +898,7 @@
     else if (in_trigger == "topo-meanlsp-merit") m_rep_trigger = 6;
     else if (in_trigger == "topo-2cells-edge-merit") m_rep_trigger = 7;
     else if (in_trigger == "topo-2cells-path-merit") m_rep_trigger = 8;
+    else if (in_trigger == "message-collection") m_rep_trigger = 9;
     else {
       cString err("Unknown replication trigger '");
       err += in_trigger;

Modified: branches/dkdev/source/actions/PrintActions.cc
===================================================================
--- branches/dkdev/source/actions/PrintActions.cc	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/actions/PrintActions.cc	2007-07-14 16:17:54 UTC (rev 1805)
@@ -66,6 +66,7 @@
 STATS_OUT_FILE(PrintMarketData,		   market.dat		   );
 STATS_OUT_FILE(PrintTopologyData, topology.dat);
 STATS_OUT_FILE(PrintDemeData, deme.dat);
+STATS_OUT_FILE(PrintCollectionData, collection.dat);
 
 
 #define POP_OUT_FILE(METHOD, DEFAULT)                                                     /*  1 */ \
@@ -1556,6 +1557,9 @@
   action_lib->Register<cActionPrintLastTopology>("PrintLastTopology");
   action_lib->Register<cActionPrintLastGermline>("PrintLastGermline");
   action_lib->Register<cActionPrintEachTopology>("PrintEachTopology");
+  
+  // Satellite
+  action_lib->Register<cActionPrintCollectionData>("PrintCollectionData");  
 
   // @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.
   action_lib->Register<cActionPrintAverageData>("print_average_data");

Modified: branches/dkdev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.cc	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/cpu/cHardwareCPU.cc	2007-07-14 16:17:54 UTC (rev 1805)
@@ -372,7 +372,10 @@
     cInstEntryCPU("get-altitude", &cHardwareCPU::Inst_GetAltitude),
     cInstEntryCPU("rotate-cell", &cHardwareCPU::Inst_RotateCell),
     cInstEntryCPU("if-linked", &cHardwareCPU::Inst_IfLinked),
-    cInstEntryCPU("if-not-linked", &cHardwareCPU::Inst_IfNotLinked)
+    cInstEntryCPU("if-not-linked", &cHardwareCPU::Inst_IfNotLinked),
+    
+    // Satellite instructions.
+    cInstEntryCPU("sat-send", &cHardwareCPU::Inst_SatelliteSend)
   };
   
   const int n_size = sizeof(s_n_array)/sizeof(cNOPEntryCPU);
@@ -3780,3 +3783,13 @@
   
   return true;
 }
+
+
+bool cHardwareCPU::Inst_SatelliteSend(cAvidaContext& ctx) {
+  // pass the vector of all received messages through the deme for fitness calc.
+  if(organism->GetCellID()==-1) return false;
+  cPopulationCell& cell = m_world->GetPopulation().GetCell(organism->GetCellID());
+	cDeme& deme = m_world->GetPopulation().GetDeme(cell.GetDemeID());
+  deme.CollectMessages(organism->GetReceivedMessages());
+  return true;
+}

Modified: branches/dkdev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.h	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/cpu/cHardwareCPU.h	2007-07-14 16:17:54 UTC (rev 1805)
@@ -490,6 +490,9 @@
   bool Inst_RotateCell(cAvidaContext& ctx); //!< Rotates the caller to face (as close as is possible) in the direction of one of twocells.
   bool Inst_IfLinked(cAvidaContext& ctx); //!< Executes the next instruction if the caller has a link in the faced direction.  
   bool Inst_IfNotLinked(cAvidaContext& ctx); //!< Executes the next instruction if the caller does not have a link in the faced direction.  
+  
+  // Satellite instructions.
+  bool Inst_SatelliteSend(cAvidaContext& ctx);
 };
 
 

Modified: branches/dkdev/source/main/cDeme.cc
===================================================================
--- branches/dkdev/source/main/cDeme.cc	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/main/cDeme.cc	2007-07-14 16:17:54 UTC (rev 1805)
@@ -7,9 +7,12 @@
  */
 
 #include "cDeme.h"
+#include "cPopulation.h"
+#include "cPopulationCell.h"
+#include "cTopology.h"
 #include "cWorld.h"
-#include "cTopology.h"
 
+
 #include <boost/graph/connected_components.hpp>
 
 cDeme::cDeme()
@@ -18,13 +21,14 @@
 , org_count(0)
 , _age(0)
 {
+  Reset();
 }
 
 cDeme::~cDeme()
 {
 }
 
-void cDeme::Setup(const tArray<int> & in_cells, int in_width, cWorld* world)
+void cDeme::Setup(const tArray<int> & in_cells, int in_width, cWorld* world, cPopulation* pop)
 {
   _world = world;
   cell_ids = in_cells;
@@ -39,6 +43,13 @@
   // If width is negative, set it to the full number of cells.
   width = in_width;
   if (width < 1) width = cell_ids.GetSize();
+  
+  // Load up the map of random cell ids.
+  if(_cellsToMsgCount.size()==0) {
+    for(int i=0; i<cell_ids.GetSize(); ++i) {
+      _cellsToMsgCount[pop->GetCell(cell_ids[i]).GetRandomCellID()] = 0;
+    }  
+  }  
 }
 
 int cDeme::GetCellID(int x, int y) const
@@ -67,6 +78,10 @@
   m_cvMap = CellVertexMap();
   _age = 0;
   _links = 20; // Start with extra.
+  _collect_count = 0;
+  for(CellCountMap::iterator i=_cellsToMsgCount.begin(); i!=_cellsToMsgCount.end(); ++i) {
+    i->second = 0;
+  }
 }
 
 
@@ -87,6 +102,22 @@
 }
 
 
+void cDeme::CollectMessages(const cOrganism::t_message_list& message_list) {
+  ++_collect_count;
+  
+  for(cOrganism::t_message_list::const_iterator i=message_list.begin(); i!=message_list.end(); ++i) {
+    CellCountMap::iterator id = _cellsToMsgCount.find(i->GetData());
+    if(id != _cellsToMsgCount.end()) {
+      ++id->second;
+    }
+    id = _cellsToMsgCount.find(i->GetLabel());
+    if(id != _cellsToMsgCount.end()) {
+      ++id->second;
+    }
+  }
+}
+
+
 bool network_is_connected(cDeme& deme) {
   cDeme::Network& network = deme.GetNetwork();
   

Modified: branches/dkdev/source/main/cDeme.h
===================================================================
--- branches/dkdev/source/main/cDeme.h	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/main/cDeme.h	2007-07-14 16:17:54 UTC (rev 1805)
@@ -22,9 +22,11 @@
 
 #include "tArray.h"
 #include "cGermline.h"
+#include "cOrganism.h"
 
 
 class cWorld;
+class cPopulation;
 
 
 class cDeme {
@@ -44,10 +46,13 @@
   //! A map of cell IDs to vertex descriptors.
   typedef std::map<int, Network::vertex_descriptor> CellVertexMap;
   
+  //! Map of cell IDs to counts.
+  typedef std::map<int, unsigned int> CellCountMap;
+  
   cDeme();
   ~cDeme();
 
-  void Setup(const tArray<int> & in_cells, int in_width, cWorld* world);
+  void Setup(const tArray<int> & in_cells, int in_width, cWorld* world, cPopulation* pop);
 
   int GetSize() const { return cell_ids.GetSize(); }
   int GetCellID(int pos) const { return cell_ids[pos]; }
@@ -89,6 +94,11 @@
   void ProcessUpdate() { ++_age; ++_links; }
   int GetAge() const { return _age; }
   
+  // -= Message coalescence =-
+  void CollectMessages(const cOrganism::t_message_list& message_list);
+  const CellCountMap& GetMessageCollection() const { return _cellsToMsgCount; }
+  unsigned int GetMessageCollectionCount() const { return _collect_count; }
+  
 private:
   tArray<int> cell_ids;
   int width;        // How wide is the deme?
@@ -103,6 +113,9 @@
   cWorld* _world; //!< Pointer to the world.
   int _links; //!< The number of links currently available to members of this deme.
   std::pair<int, int> _cellsToLink; //! The ids of cells that should be linked.
+  
+  unsigned int _collect_count; //!< The number of times CollectMessages has been used this compete period.
+  CellCountMap _cellsToMsgCount; //!< Map of random cell ids to the number of times that id has appeared in a message.
 };
 
 

Modified: branches/dkdev/source/main/cOrganism.cc
===================================================================
--- branches/dkdev/source/main/cOrganism.cc	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/main/cOrganism.cc	2007-07-14 16:17:54 UTC (rev 1805)
@@ -538,7 +538,7 @@
     tBuffer<int>* received_messages_point = &m_received_messages;
     if (!m_world->GetConfig().SAVE_RECEIVED.Get())
       received_messages_point = NULL;
-    cTaskContext taskctx(m_input_buf, m_output_buf, other_input_list, other_output_list, net_valid, 0, received_messages_point);
+    cTaskContext taskctx(m_input_buf, m_output_buf, other_input_list, other_output_list, net_valid, 0, received_messages_point, this);
     m_phenotype.TestOutput(ctx, taskctx, m_send_buf, m_receive_buf, resource_count, res_change, insts_triggered);
     m_interface->UpdateResources(res_change);
     

Modified: branches/dkdev/source/main/cPopulation.cc
===================================================================
--- branches/dkdev/source/main/cPopulation.cc	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/main/cPopulation.cc	2007-07-14 16:17:54 UTC (rev 1805)
@@ -41,6 +41,7 @@
 
 #include <fstream>
 #include <vector>
+#include <algorithm>
 #include <boost/graph/connected_components.hpp>
 
 #include <float.h>
@@ -102,7 +103,7 @@
 			deme_cells[offset] = cell_id;
 			cell_array[cell_id].SetDemeID(deme_id);
 		}
-		deme_array[deme_id].Setup(deme_cells, deme_size_x, m_world);
+		deme_array[deme_id].Setup(deme_cells, deme_size_x, m_world, this);
 	}
 	
 	
@@ -954,7 +955,28 @@
         m_world->GetStats().TopologyReplication(network);
         source_germline_merit = pow((double)boost::num_vertices(network) - 1 - dv[dst], 2);
         break;
-      }        
+      }       
+      case 9: {
+        // Replicate demes that have managed to collect all their constituent ids.
+        // The merit of the deme is in inverse proportion to the number of messages used.
+        
+        // Have we collected each id?
+        const cDeme::CellCountMap& ccmap = source_deme.GetMessageCollection();
+        bool replicate=true;
+        for(cDeme::CellCountMap::const_iterator i=ccmap.begin(); i!=ccmap.end(); ++i) {
+          replicate = replicate && (i->second>0);
+        }
+        if(!replicate) continue;
+        
+        // Merit in inverse proportion to the number of messages used.
+        // Merit will be in the range [1,deme size).
+        source_germline_merit = pow(1+
+                                    (double)source_deme.GetSize() - 
+                                    std::min((int)source_deme.GetMessageCollectionCount(), source_deme.GetSize())
+                                    , 2);
+        
+        break;
+      }
 //case 5: {
 //  // Replicate demes that are connectd, and set merit to f(edge count, diameter).
 //  

Modified: branches/dkdev/source/main/cStats.cc
===================================================================
--- branches/dkdev/source/main/cStats.cc	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/main/cStats.cc	2007-07-14 16:17:54 UTC (rev 1805)
@@ -1008,3 +1008,29 @@
   _deme_merit.Clear();
   _deme_age.Clear();
 }
+
+
+void cStats::PrintCollectionData(const cString& filename) {
+  cDataFile& df = m_world->GetDataFile(filename);
+  df.WriteComment("Message collection stats");
+  df.WriteTimeStamp();
+  df.Write(GetUpdate(), "update");
+  
+  cDoubleSum collection_count;
+  cDoubleSum cell_ids_collected;
+  for(int i=0; i<m_world->GetPopulation().GetNumDemes(); ++i) {
+    const cDeme& deme = m_world->GetPopulation().GetDeme(i);
+    const cDeme::CellCountMap& ccmap = deme.GetMessageCollection();
+    
+    collection_count.Add(deme.GetMessageCollectionCount());
+    unsigned int ids_collected=0;
+    for(cDeme::CellCountMap::const_iterator i=ccmap.begin(); i!=ccmap.end(); ++i) {
+      ids_collected += i->second;
+    }
+    cell_ids_collected.Add(ids_collected);
+  }
+  
+  df.Write(collection_count.Average(), "mean collection count (per deme)");
+  df.Write(cell_ids_collected.Average(), "mean cell ids collected (per deme)");
+  df.Endl();
+}

Modified: branches/dkdev/source/main/cStats.h
===================================================================
--- branches/dkdev/source/main/cStats.h	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/main/cStats.h	2007-07-14 16:17:54 UTC (rev 1805)
@@ -571,6 +571,9 @@
   void DemeReplication(cDeme& deme);
   void PrintDemeData(const cString& filename);
   void PrintLastGermline();
+  
+  // Satellite
+  void PrintCollectionData(const cString& filename);
 };
 
 

Modified: branches/dkdev/source/main/cTaskContext.h
===================================================================
--- branches/dkdev/source/main/cTaskContext.h	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/main/cTaskContext.h	2007-07-14 16:17:54 UTC (rev 1805)
@@ -10,12 +10,9 @@
 #ifndef cTaskContext_h
 #define cTaskContext_h
 
-#ifndef tBuffer_h
+#include "cOrganism.h"
 #include "tBuffer.h"
-#endif
-#ifndef tList_h
 #include "tList.h"
-#endif
 
 class cTaskEntry;
 
@@ -32,19 +29,22 @@
   int net_completed;
   tBuffer<int>* received_messages;
   int logic_id;
+  cOrganism* m_organism;
   
   cTaskEntry* task_entry;
 
 public:
   cTaskContext(const tBuffer<int>& inputs, const tBuffer<int>& outputs, const tList<tBuffer<int> >& other_inputs,
                const tList<tBuffer<int> >& other_outputs, bool in_net_valid, int in_net_completed, 
-               tBuffer<int>* in_received_messages = NULL)
+               tBuffer<int>* in_received_messages = NULL, cOrganism* org=NULL)
     : input_buffer(inputs), output_buffer(outputs), other_input_buffers(other_inputs),
     other_output_buffers(other_outputs), net_valid(in_net_valid), net_completed(in_net_completed), 
-    received_messages(in_received_messages), logic_id(0), task_entry(NULL)
+    received_messages(in_received_messages), logic_id(0), task_entry(NULL), m_organism(org)
   {
   }
   
+  cOrganism* GetOrganism() { return m_organism; }
+  
   void SetTaskEntry(cTaskEntry* in_entry) { task_entry = in_entry; }
 };
 

Modified: branches/dkdev/source/main/cTaskLib.cc
===================================================================
--- branches/dkdev/source/main/cTaskLib.cc	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/main/cTaskLib.cc	2007-07-14 16:17:54 UTC (rev 1805)
@@ -9,7 +9,11 @@
  */
 
 #include "cTaskLib.h"
+#include "cDeme.h"
+#include "cPopulation.h"
+#include "cPopulationCell.h"
 
+
 extern "C" {
 #include <math.h>
 #include <limits.h>
@@ -322,6 +326,8 @@
   else if (name == "net_receive")
 	  NewTask(name, "Successfully Received Network Message", &cTaskLib::Task_NetReceive);
   
+  if(name == "satellite")
+    NewTask(name, "Sent a message from the perimeter to the satellite", &cTaskLib::Task_SentIdFromPerimeter);
   
   
   // Make sure we have actually found a task  
@@ -1831,3 +1837,20 @@
   if (ctx->net_valid) return 1.0;
   return 0.0;
 }
+
+
+/*! Reward an organism for sending its random cell id if it lives on the perimeter
+of the environment.  This reduces to rewarding an organism if its location has a 0-component
+within its deme.*/
+double cTaskLib::Task_SentIdFromPerimeter(cTaskContext* ctx) const
+{
+  cOrganism* organism = ctx->GetOrganism();
+  if(organism==0) return 0.0;
+  
+  if(organism->GetSentMessages().size()==0) return 0.0;
+  
+  cPopulationCell& cell = m_world->GetPopulation().GetCell(organism->GetCellID());
+	cDeme& deme = m_world->GetPopulation().GetDeme(cell.GetDemeID());
+  std::pair<int,int> location = deme.GetCellPosition(organism->GetCellID());
+  return (location.first==0 || location.second==0);
+}

Modified: branches/dkdev/source/main/cTaskLib.h
===================================================================
--- branches/dkdev/source/main/cTaskLib.h	2007-07-14 11:44:12 UTC (rev 1804)
+++ branches/dkdev/source/main/cTaskLib.h	2007-07-14 16:17:54 UTC (rev 1805)
@@ -224,6 +224,8 @@
   
   double Task_NetSend(cTaskContext* ctx) const;
   double Task_NetReceive(cTaskContext* ctx) const;
+  
+  double Task_SentIdFromPerimeter(cTaskContext* ctx) const;
 };
 
 




More information about the Avida-cvs mailing list