[Avida-SVN] r1347 - in branches/dkdev: Avida.xcodeproj source/cpu source/main

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Tue Feb 20 09:27:29 PST 2007


Author: dknoester
Date: 2007-02-20 12:27:28 -0500 (Tue, 20 Feb 2007)
New Revision: 1347

Added:
   branches/dkdev/source/main/cOrgMessage.h
Modified:
   branches/dkdev/Avida.xcodeproj/project.pbxproj
   branches/dkdev/source/cpu/cHardwareCPU.cc
   branches/dkdev/source/cpu/cHardwareCPU.h
   branches/dkdev/source/cpu/cTestCPUInterface.h
   branches/dkdev/source/main/cOrgInterface.h
   branches/dkdev/source/main/cOrganism.cc
   branches/dkdev/source/main/cOrganism.h
   branches/dkdev/source/main/cPopulationCell.cc
   branches/dkdev/source/main/cPopulationCell.h
   branches/dkdev/source/main/cPopulationInterface.cc
   branches/dkdev/source/main/cPopulationInterface.h
Log:
Merge: r975:976 from avida/branches/coopcomm; merge of messaging-related functionality only, no stats tracking.

Modified: branches/dkdev/Avida.xcodeproj/project.pbxproj
===================================================================
--- branches/dkdev/Avida.xcodeproj/project.pbxproj	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/Avida.xcodeproj/project.pbxproj	2007-02-20 17:27:28 UTC (rev 1347)
@@ -246,6 +246,7 @@
 		E03F28DC0B8A2840009966B8 /* cDeme.cc in Sources */ = {isa = PBXBuildFile; fileRef = E03F28DA0B8A2840009966B8 /* cDeme.cc */; };
 		E03F28DD0B8A2840009966B8 /* cDeme.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E03F28DB0B8A2840009966B8 /* cDeme.h */; };
 		E08178BA0B3DCB9600B474B6 /* cTopology.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E08178B90B3DCB9600B474B6 /* cTopology.h */; };
+		E0D1973A0B8B2F4C005EF7EE /* cOrgMessage.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = E0D197390B8B2F4C005EF7EE /* cOrgMessage.h */; };
 		E626209E0A372C2A00C07685 /* SaveLoadActions.cc in Sources */ = {isa = PBXBuildFile; fileRef = 708051A80A1F65FE00CBB8B6 /* SaveLoadActions.cc */; };
 /* End PBXBuildFile section */
 
@@ -291,6 +292,7 @@
 				7049F3730A66AD7E00640512 /* default-transsmt.org in CopyFiles */,
 				E08178BA0B3DCB9600B474B6 /* cTopology.h in CopyFiles */,
 				E03F28DD0B8A2840009966B8 /* cDeme.h in CopyFiles */,
+				E0D1973A0B8B2F4C005EF7EE /* cOrgMessage.h in CopyFiles */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -794,10 +796,11 @@
 		DCC315CF076253A5008F7A48 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = "<group>"; };
 		DCC315D0076253A5008F7A48 /* task_event_gen.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = task_event_gen.cc; sourceTree = "<group>"; };
 		DCC315D1076253A5008F7A48 /* task_event_gen.old.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = task_event_gen.old.cc; sourceTree = "<group>"; };
-		DCC3164D07626CF3008F7A48 /* avida */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = avida; sourceTree = BUILT_PRODUCTS_DIR; };
+		DCC3164D07626CF3008F7A48 /* avida */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = avida; sourceTree = BUILT_PRODUCTS_DIR; };
 		E03F28DA0B8A2840009966B8 /* cDeme.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cDeme.cc; sourceTree = "<group>"; };
 		E03F28DB0B8A2840009966B8 /* cDeme.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cDeme.h; sourceTree = "<group>"; };
 		E08178B90B3DCB9600B474B6 /* cTopology.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cTopology.h; sourceTree = "<group>"; };
+		E0D197390B8B2F4C005EF7EE /* cOrgMessage.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cOrgMessage.h; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -1283,6 +1286,7 @@
 				70B0864F08F4972600FC65FE /* cMxCodeArray.h */,
 				70B0868708F49EA800FC65FE /* cOrganism.cc */,
 				70B0868308F49E9700FC65FE /* cOrganism.h */,
+				E0D197390B8B2F4C005EF7EE /* cOrgMessage.h */,
 				70B0869C08F49F4800FC65FE /* cPhenotype.cc */,
 				70B0869B08F49F3900FC65FE /* cPhenotype.h */,
 				70B0868908F49EA800FC65FE /* cPopulation.cc */,

Modified: branches/dkdev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.cc	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/cpu/cHardwareCPU.cc	2007-02-20 17:27:28 UTC (rev 1347)
@@ -29,6 +29,8 @@
 #include "cTestCPU.h"
 #include "cWorldDriver.h"
 #include "cWorld.h"
+#include "cPopulation.h"
+#include "cPopulationCell.h"
 
 #include <limits.h>
 #include <fstream>
@@ -348,11 +350,14 @@
     cInstEntryCPU("kazi5",	&cHardwareCPU::Inst_Kazi5),
     cInstEntryCPU("die",	&cHardwareCPU::Inst_Die),
     
-    
-    
     // Placebo instructions
     // nop-x (included with nops)
-    cInstEntryCPU("skip",      &cHardwareCPU::Inst_Skip)
+    cInstEntryCPU("skip",      &cHardwareCPU::Inst_Skip),
+
+    // Messaging instructions
+    cInstEntryCPU("send-msg", &cHardwareCPU::Inst_SendMessage),
+    cInstEntryCPU("rtrv-msg", &cHardwareCPU::Inst_RetrieveMessage),
+    cInstEntryCPU("cell-id", &cHardwareCPU::Inst_CellID)
   };
   
   const int n_size = sizeof(s_n_array)/sizeof(cNOPEntryCPU);
@@ -3371,3 +3376,48 @@
   return true;
 }
 
+
+/*! 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?.
+*/
+bool cHardwareCPU::Inst_SendMessage(cAvidaContext& ctx)
+{
+  const int label_reg = FindModifiedRegister(REG_BX);
+  const int data_reg = FindNextRegister(label_reg);
+  
+  cOrgMessage msg = cOrgMessage(organism);
+  msg.SetLabel(GetRegister(label_reg));
+  msg.SetData(GetRegister(data_reg));
+  return organism->SendMessage(ctx, msg);
+}
+
+
+/*! This method /attempts/ to retrieve a message -- It may not be possible, as in
+the case of an empty receive buffer.
+
+If a message is available, ?BX? is set to the message's label, and ~?BX? is set
+to its data.
+*/
+bool cHardwareCPU::Inst_RetrieveMessage(cAvidaContext& ctx) 
+{
+  const cOrgMessage* msg = organism->RetrieveMessage();
+  if(msg==NULL)
+    return false;
+  
+  const int label_reg = FindModifiedRegister(REG_BX);
+  const int data_reg = FindNextRegister(label_reg);
+  
+  GetRegister(label_reg) = msg->GetLabel();
+  GetRegister(data_reg) = msg->GetData();
+  return true;
+}
+
+
+/*! Places this organism's random-cell-id in register ?BX?.
+*/
+bool cHardwareCPU::Inst_CellID(cAvidaContext& ctx) {
+	const int reg_used = FindModifiedRegister(REG_BX);
+	GetRegister(reg_used) = m_world->GetPopulation().GetCell(organism->GetOrgInterface().GetCellID()).GetRandomCellID();
+	return true;
+}

Modified: branches/dkdev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.h	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/cpu/cHardwareCPU.h	2007-02-20 17:27:28 UTC (rev 1347)
@@ -471,6 +471,11 @@
 
   //// Placebo ////
   bool Inst_Skip(cAvidaContext& ctx);
+  
+  // Messaging
+  bool Inst_SendMessage(cAvidaContext& ctx);
+  bool Inst_RetrieveMessage(cAvidaContext& ctx);
+  bool Inst_CellID(cAvidaContext& ctx);
 };
 
 

Modified: branches/dkdev/source/cpu/cTestCPUInterface.h
===================================================================
--- branches/dkdev/source/cpu/cTestCPUInterface.h	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/cpu/cTestCPUInterface.h	2007-02-20 17:27:28 UTC (rev 1347)
@@ -53,6 +53,7 @@
   bool InjectParasite(cOrganism* parent, const cGenome& injected_code);
   bool UpdateMerit(double new_merit);
   bool TestOnDivide() { return false; }
+  bool SendMessage(cOrgMessage& msg) { return false; }
 };
 
 

Modified: branches/dkdev/source/main/cOrgInterface.h
===================================================================
--- branches/dkdev/source/main/cOrgInterface.h	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/main/cOrgInterface.h	2007-02-20 17:27:28 UTC (rev 1347)
@@ -24,6 +24,7 @@
 class cGenome;
 class cOrganism;
 class cOrgSinkMessage;
+class cOrgMessage;
 template <class T> class tArray;
 
 
@@ -61,6 +62,7 @@
   virtual bool InjectParasite(cOrganism* parent, const cGenome& injected_code) = 0;
   virtual bool UpdateMerit(double new_merit) = 0;
   virtual bool TestOnDivide() = 0;
+  virtual bool SendMessage(cOrgMessage& msg) = 0;
 };
 
 #endif

Added: branches/dkdev/source/main/cOrgMessage.h
===================================================================
--- branches/dkdev/source/main/cOrgMessage.h	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/main/cOrgMessage.h	2007-02-20 17:27:28 UTC (rev 1347)
@@ -0,0 +1,50 @@
+/*
+ *  cOrgMessage.h
+ *  Avida
+ *
+ *  Called "org_message.hh" prior to 12/5/05.
+ *  Copyright 2005-2006 Michigan State University. All rights reserved.
+ *  Copyright 1993-2003 California Institute of Technology.
+ *
+ */
+
+#ifndef cOrgMessage_h
+#define cOrgMessage_h
+
+#include "cOrganism.h"
+
+class cOrgMessage
+{
+private:
+  cOrganism* m_pSender;
+  cOrganism* m_pReceiver;
+  unsigned int m_data;
+  unsigned int m_label;
+  
+  cOrgMessage() 
+  : m_pSender(NULL), m_pReceiver(NULL), m_data(0), m_label(0) {
+  }
+  
+public:
+  cOrgMessage(cOrganism* sender)
+  : m_pSender(sender), m_pReceiver(NULL), m_data(0), m_label(0) {
+  }
+  
+  explicit cOrgMessage(int data, int label)
+  : m_pSender(NULL), m_pReceiver(NULL), m_data(data), m_label(label) {
+  }
+
+  static cOrgMessage EmptyMessage() { return cOrgMessage(); }
+  
+  cOrganism* GetSender() { return m_pSender; }
+  cOrganism* GetReceiver() { return m_pReceiver; }
+  void SetReceiver(cOrganism* recvr) { m_pReceiver = recvr; }
+  
+  unsigned int GetData() const { return m_data; }
+  unsigned int GetLabel() const { return m_label; }
+
+  void SetData(unsigned int data) { m_data = data; }
+  void SetLabel(unsigned int label) { m_label = label; }
+};
+
+#endif

Modified: branches/dkdev/source/main/cOrganism.cc
===================================================================
--- branches/dkdev/source/main/cOrganism.cc	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/main/cOrganism.cc	2007-02-20 17:27:28 UTC (rev 1347)
@@ -55,6 +55,7 @@
   , m_max_executed(-1)
   , m_is_running(false)
   , m_net(NULL)
+  , m_msg_rtrv_index(0)
 {
   // Initialization of structures...
   m_hardware = m_world->GetHardwareManager().Create(this);
@@ -488,3 +489,89 @@
 
   m_phenotype.IncErrors();
 }
+
+
+/*! Attempt to send the message (which is actually based on the facing of the
+cell), and if successful, store the sent message, and check to see if sending this
+message completed any tasks.
+*/
+bool cOrganism::SendMessage(cAvidaContext& ctx, cOrgMessage& msg)
+{
+  assert(m_interface);
+  if(m_interface->SendMessage(msg)) {
+    // store the message.
+    m_msg_sent.push_back(msg);
+
+    const tArray<double> & resource_count = m_interface->GetResources();
+    
+    tList<tBuffer<int> > other_input_list;
+    tList<tBuffer<int> > other_output_list;
+    
+  // If tasks require us to consider neighbor inputs, collect them...
+    if (m_world->GetEnvironment().GetTaskLib().UseNeighborInput() == true) {
+      const int num_neighbors = m_interface->GetNumNeighbors();
+      for (int i = 0; i < num_neighbors; i++) {
+        m_interface->Rotate();
+        cOrganism * cur_neighbor = m_interface->GetNeighbor();
+        if (cur_neighbor == NULL) continue;
+        
+        other_input_list.Push( &(cur_neighbor->m_input_buf) );
+      }
+    }
+    
+  // If tasks require us to consider neighbor outputs, collect them...
+    if (m_world->GetEnvironment().GetTaskLib().UseNeighborOutput() == true) {
+      const int num_neighbors = m_interface->GetNumNeighbors();
+      for (int i = 0; i < num_neighbors; i++) {
+        m_interface->Rotate();
+        cOrganism * cur_neighbor = m_interface->GetNeighbor();
+        if (cur_neighbor == NULL) continue;
+        
+        other_output_list.Push( &(cur_neighbor->m_output_buf) );
+      }
+    }
+    
+    bool net_valid = false;
+    tArray<double> res_change(resource_count.GetSize());
+    tArray<int> insts_triggered;
+    
+    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);
+    m_phenotype.TestOutput(ctx, taskctx, m_send_buf, m_receive_buf, resource_count, res_change, insts_triggered);
+    m_interface->UpdateResources(res_change);
+    
+    for (int i = 0; i < insts_triggered.GetSize(); i++) {
+      const int cur_inst = insts_triggered[i];
+      m_hardware->ProcessBonusInst(ctx, cInstruction(cur_inst) );
+    }
+    
+    m_msg_sent.back().SetReceiver(NULL);
+    return true;
+  }
+  
+  return false;
+}
+
+
+/*! Add this message to the list of messages that this organism has received.
+*/
+bool cOrganism::ReceiveMessage(cOrgMessage& msg)
+{
+  m_msg_recv.push_back(msg);
+  return true;
+}
+
+
+/*! Check to see if this organism has actually received a message that can be
+retrieved, and if so, return it.
+*/
+const cOrgMessage* cOrganism::RetrieveMessage()
+{
+  if((m_msg_rtrv_index+1) > m_msg_recv.size()) {
+    return NULL;
+  } else {
+    return &m_msg_recv.at(m_msg_rtrv_index++);
+  }
+}

Modified: branches/dkdev/source/main/cOrganism.h
===================================================================
--- branches/dkdev/source/main/cOrganism.h	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/main/cOrganism.h	2007-02-20 17:27:28 UTC (rev 1347)
@@ -12,6 +12,7 @@
 #define cOrganism_h
 
 #include <iostream>
+#include <vector>
 
 #ifndef cCPUMemory_h
 #include "cCPUMemory.h"
@@ -53,6 +54,7 @@
 #include "tSmartArray.h"
 #endif
 
+#include "cOrgMessage.h"
 
 class cAvidaContext;
 class cCodeLabel;
@@ -67,6 +69,9 @@
 
 class cOrganism
 {
+public:
+  typedef std::vector<cOrgMessage> t_message_list; //!< Container for cOrgMessages.
+  
 protected:
   cWorld* m_world;
   cHardwareBase* m_hardware;              // The actual machinary running this organism.
@@ -116,6 +121,10 @@
   };
   cNetSupport* m_net;
 
+  // Messaging.
+  t_message_list m_msg_sent; //!< List of messages that this organism has sent, in order.
+  t_message_list m_msg_recv; //!< List of messages that this organism has received, in order.
+  t_message_list::size_type m_msg_rtrv_index; //!< Index of next message that can be retrieved.
 
   cOrganism(); // @not_implemented
   cOrganism(const cOrganism&); // @not_implemented
@@ -254,6 +263,19 @@
   bool GetSterilizePos() const;
   double GetNeutralMin() const;
   double GetNeutralMax() const;
+  
+  
+  // -------- Messaging Methods --------
+  //! Called when this organism attempts to send a message.
+  bool SendMessage(cAvidaContext& ctx, cOrgMessage& msg);
+  //! Called when this organism has been sent a message.
+  bool ReceiveMessage(cOrgMessage& msg);
+  //! Called when this organism attempts to move a received message into its CPU.
+  const cOrgMessage* RetrieveMessage();
+  //! Returns the list of all messsages received by this organism.
+  const t_message_list& GetReceivedMessages() { return m_msg_recv; }
+  //! Returns the list of all messages sent by this organism.
+  const t_message_list& GetSentMessages() { return m_msg_sent; }
 };
 
 

Modified: branches/dkdev/source/main/cPopulationCell.cc
===================================================================
--- branches/dkdev/source/main/cPopulationCell.cc	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/main/cPopulationCell.cc	2007-02-20 17:27:28 UTC (rev 1347)
@@ -13,6 +13,7 @@
 #include "nHardware.h"
 #include "cOrganism.h"
 #include "cTools.h"
+#include "cWorld.h"
 
 using namespace std;
 
@@ -67,6 +68,7 @@
 {
   m_world = world;
   cell_id = in_id;
+  m_rand_cell_id = m_world->GetRandom().GetInt(INT_MAX);
   deme_id = -1;
   
   if (mutation_rates == NULL)

Modified: branches/dkdev/source/main/cPopulationCell.h
===================================================================
--- branches/dkdev/source/main/cPopulationCell.h	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/main/cPopulationCell.h	2007-02-20 17:27:28 UTC (rev 1347)
@@ -40,6 +40,7 @@
   int cur_input;                           // Next input to give organism.
 
   int cell_id;           // Unique id for position of cell in population.
+  int m_rand_cell_id;    //!< A random identifier for this cell.
   int deme_id;           // ID of the deme that this cell is part of.
   int organism_count;    // Total number of orgs to ever inhabit this cell.
 
@@ -67,6 +68,7 @@
   int GetInputAt(int & input_pointer);
 
   int GetID() const { return cell_id; }
+  int GetRandomCellID() const { return m_rand_cell_id; }
   int GetDemeID() const { return deme_id; }
   int GetOrganismCount() const { return organism_count; }
 

Modified: branches/dkdev/source/main/cPopulationInterface.cc
===================================================================
--- branches/dkdev/source/main/cPopulationInterface.cc	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/main/cPopulationInterface.cc	2007-02-20 17:27:28 UTC (rev 1347)
@@ -198,3 +198,21 @@
 {
   return m_world->GetTestOnDivide();
 }
+
+/*! Send a message to the faced organism, failing if this cell does not have 
+neighbors or if the cell currently faced is not occupied.  Set the receiver
+parameter for the message being sent.
+*/
+bool cPopulationInterface::SendMessage(cOrgMessage& msg) {
+  cPopulationCell& cell = m_world->GetPopulation().GetCell(m_cell_id);
+  assert(cell.IsOccupied()); // this organism; sanity.
+  cPopulationCell* rcell = cell.ConnectionList().GetFirst();
+  assert(rcell != NULL); // cells should never be null.
+  // Fail if the cell we're facing is not occupied.
+  if(!rcell->IsOccupied())
+    return false;
+  cOrganism* recvr = rcell->GetOrganism();
+  assert(recvr != NULL);
+  msg.SetReceiver(recvr);  
+  return recvr->ReceiveMessage(msg);
+}

Modified: branches/dkdev/source/main/cPopulationInterface.h
===================================================================
--- branches/dkdev/source/main/cPopulationInterface.h	2007-02-20 17:17:33 UTC (rev 1346)
+++ branches/dkdev/source/main/cPopulationInterface.h	2007-02-20 17:27:28 UTC (rev 1347)
@@ -62,6 +62,7 @@
   bool InjectParasite(cOrganism* parent, const cGenome& injected_code);
   bool UpdateMerit(double new_merit);
   bool TestOnDivide();
+  bool SendMessage(cOrgMessage& msg);
 };
 
 




More information about the Avida-cvs mailing list