[Avida-cvs] [avida-svn] r977 - in branches/coopcomm/source: cpu main

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Sun Sep 24 08:48:54 PDT 2006


Author: dknoester
Date: 2006-09-24 11:48:53 -0400 (Sun, 24 Sep 2006)
New Revision: 977

Modified:
   branches/coopcomm/source/cpu/cHardwareCPU.cc
   branches/coopcomm/source/cpu/cHardwareCPU.h
   branches/coopcomm/source/cpu/cTestCPUInterface.h
   branches/coopcomm/source/main/cOrgInterface.h
   branches/coopcomm/source/main/cOrganism.h
   branches/coopcomm/source/main/cPopulation.cc
   branches/coopcomm/source/main/cPopulationCell.cc
   branches/coopcomm/source/main/cPopulationCell.h
   branches/coopcomm/source/main/cPopulationInterface.cc
   branches/coopcomm/source/main/cPopulationInterface.h
Log:
Current revision: 976

This commit completes the majority of the non-logging-related leader election changes.

Included in this commit are instructions for retrieving an organism's ID, facing, and position.



Modified: branches/coopcomm/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/coopcomm/source/cpu/cHardwareCPU.cc	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/cpu/cHardwareCPU.cc	2006-09-24 15:48:53 UTC (rev 977)
@@ -353,7 +353,10 @@
     
     // Coop-comm
     cInstEntryCPU("send-msg", &cHardwareCPU::Inst_SendMessage),
-    cInstEntryCPU("recv-msg", &cHardwareCPU::Inst_ReceiveMessage)
+    cInstEntryCPU("recv-msg", &cHardwareCPU::Inst_ReceiveMessage),
+    cInstEntryCPU("get-id", &cHardwareCPU::Inst_GetID),
+    cInstEntryCPU("get-pos", &cHardwareCPU::Inst_GetPosition),
+    cInstEntryCPU("get-facing", &cHardwareCPU::Inst_GetFacing)
   };
   
   const int n_size = sizeof(s_n_array)/sizeof(cNOPEntryCPU);
@@ -3405,3 +3408,40 @@
   GetRegister(data_reg) = msg->GetData();
   return true;
 }
+
+
+/*! Places this organism's random-cell-id in register ?BX?.
+*/
+bool cHardwareCPU::Inst_GetID(cAvidaContext& ctx) {
+	const int reg_used = FindModifiedRegister(REG_BX);
+	GetRegister(reg_used) = organism->GetRandomCellID();
+	return true;
+}
+
+/*! Places the x-coordinate of this organism's position in the ?BX? register, and the 
+y-coordinate in the !?BX? register.
+*/
+bool cHardwareCPU::Inst_GetPosition(cAvidaContext& ctx) {
+	const int xreg = FindModifiedRegister(REG_BX);
+	const int yreg = FindNextRegister(xreg);
+	organism->GetPosition(GetRegister(xreg),GetRegister(yreg));
+	return true;
+}
+
+/*! Places the facing of this organism into the ?BX? register.
+Note that facing is a number from -4..3, where the following represent the 
+cardinal directions:
+3 = NE
+2 = E
+1 = SE
+0 = S
+-1 = SW
+-2 = W
+-3 = NW
+-4 = N
+*/
+bool cHardwareCPU::Inst_GetFacing(cAvidaContext& ctx) {
+	const int facing = FindModifiedRegister(REG_BX);
+	GetRegister(facing) = organism->GetFacing();
+	return true;
+}

Modified: branches/coopcomm/source/cpu/cHardwareCPU.h
===================================================================
--- branches/coopcomm/source/cpu/cHardwareCPU.h	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/cpu/cHardwareCPU.h	2006-09-24 15:48:53 UTC (rev 977)
@@ -474,6 +474,9 @@
   // Coop-comm
   bool Inst_SendMessage(cAvidaContext& ctx); //!< Send a message to the faced organism.
   bool Inst_ReceiveMessage(cAvidaContext& ctx); //!< Receive a message.
+  bool Inst_GetID(cAvidaContext& ctx); //!< Retrieve this organism's ID.
+  bool Inst_GetPosition(cAvidaContext& ctx); //!< Retrieve this organism's position.
+  bool Inst_GetFacing(cAvidaContext& ctx); //!< Retrieve this organism's facing.
 };
 
 

Modified: branches/coopcomm/source/cpu/cTestCPUInterface.h
===================================================================
--- branches/coopcomm/source/cpu/cTestCPUInterface.h	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/cpu/cTestCPUInterface.h	2006-09-24 15:48:53 UTC (rev 977)
@@ -55,6 +55,8 @@
 
   bool SendMessage(cOrgMessage& mess) { return false; }
   int GetRandomCellID() { return 0; }
+  void GetPosition(int& x, int& y) { }
+  int GetFacing() { return 0; }
 };
 
 

Modified: branches/coopcomm/source/main/cOrgInterface.h
===================================================================
--- branches/coopcomm/source/main/cOrgInterface.h	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/main/cOrgInterface.h	2006-09-24 15:48:53 UTC (rev 977)
@@ -53,8 +53,10 @@
   virtual bool UpdateMerit(double new_merit) = 0;
   virtual bool TestOnDivide() = 0;
 
-  virtual bool SendMessage(cOrgMessage& msg) = 0;
+  virtual bool SendMessage(cOrgMessage& msg) = 0; //!< Sends a message to the organism that is currently faced.
   virtual int GetRandomCellID() = 0; //!< Returns the random cell ID for the cell in which this organism lives.
+  virtual void GetPosition(int& x, int& y) = 0; //!< Retrieves the position (x,y) coordinates of this organism.
+  virtual int GetFacing() = 0; //!< Returns the facing of this organism.
 };
 
 #endif

Modified: branches/coopcomm/source/main/cOrganism.h
===================================================================
--- branches/coopcomm/source/main/cOrganism.h	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/main/cOrganism.h	2006-09-24 15:48:53 UTC (rev 977)
@@ -278,12 +278,20 @@
   
   
   // Coop-comm
-  bool SendMessage(cAvidaContext& ctx, cOrgMessage& msg); //!< Called when this organism attempts to send a message.
-  bool ReceiveMessage(cOrgMessage& msg); //!< Called when this organism has been sent a message.
-  const cOrgMessage* RetrieveMessage(); //!< Called when this organism attempts to move a received message into its CPU.
+  //!< 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 random cell ID for the cell in which this organism lives.
   int GetRandomCellID() const { assert(m_interface); return m_interface->GetRandomCellID(); }
+  //! Returns the list of all messsages received by this organism.
   t_message_list& GetReceivedMessages() { return m_received_messages; }
+  //! Retrieves the position (x,y coordinates) of this organism.
+  void GetPosition(int& x, int& y) { assert(m_interface); m_interface->GetPosition(x, y); }
+  //! Returns the facing of this organism.
+  int GetFacing() { assert(m_interface); return m_interface->GetFacing(); }
 };
 
 

Modified: branches/coopcomm/source/main/cPopulation.cc
===================================================================
--- branches/coopcomm/source/main/cPopulation.cc	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/main/cPopulation.cc	2006-09-24 15:48:53 UTC (rev 977)
@@ -84,7 +84,7 @@
   for (int cell_id = 0; cell_id < num_cells; cell_id++) {
     int x = cell_id % world_x;
     int y = cell_id / world_x;
-    cell_array[cell_id].Setup(world, cell_id, environment.GetMutRates());
+    cell_array[cell_id].Setup(world, cell_id, environment.GetMutRates(), x, y);
     
     
     if ((y == 0) && (geometry == nGeometry::GRID)) {

Modified: branches/coopcomm/source/main/cPopulationCell.cc
===================================================================
--- branches/coopcomm/source/main/cPopulationCell.cc	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/main/cPopulationCell.cc	2006-09-24 15:48:53 UTC (rev 977)
@@ -65,10 +65,12 @@
   }
 }
 
-void cPopulationCell::Setup(cWorld* world, int in_id, const cMutationRates& in_rates)
+void cPopulationCell::Setup(cWorld* world, int in_id, const cMutationRates& in_rates, int x, int y)
 {
   m_world = world;
   cell_id = in_id;
+  m_x = x;
+  m_y = y;
   m_rand_id = (int)random();
   s_rand_ids.insert(std::make_pair(m_rand_id, this));
   
@@ -153,3 +155,41 @@
   
   return s_rand_ids.rbegin()->first;
 }
+
+/*! These values are chosen so as to make loops on the facing 'easy'.
+S=0, SW=-1, W=-2, NW=-3, N=-4, NE=3, E=2, SE=1.
+
+Facing is determined by the relative positions of this cell and the cell that 
+is currently faced. Note that we cannot differentiate between directions on a 2x2 
+torus.
+*/
+int cPopulationCell::GetFacing()
+{
+  // This whole function is a hack.
+	cPopulationCell* faced = ConnectionList().GetFirst();
+	
+	int x=0,y=0,lr=0,du=0;
+	faced->GetPosition(x,y);
+  
+	if((x==m_x-1) || (x>m_x+1))
+		lr = -1; //left
+	else if((x==m_x+1) || (x<m_x-1))
+		lr = 1; //right
+	
+	if((y==m_y-1) || (y>m_y+1))
+		du = -1; //down
+	else if((y==m_y+1) || (y<m_y-1))
+		du = 1; //up
+  
+	// This is hackish.
+	if(lr==0 && du==-1) return 0; //S
+	if(lr==-1 && du==-1) return -1; //SW
+	if(lr==-1 && du==0) return -2; //W
+	if(lr==-1 && du==1) return -3; //NW
+	if(lr==0 && du==1) return -4; //N
+	if(lr==1 && du==1) return 3; //NE
+	if(lr==1 && du==0) return 2; //E
+	if(lr==1 && du==-1) return 1; //SE
+  
+	assert(false);
+}

Modified: branches/coopcomm/source/main/cPopulationCell.h
===================================================================
--- branches/coopcomm/source/main/cPopulationCell.h	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/main/cPopulationCell.h	2006-09-24 15:48:53 UTC (rev 977)
@@ -48,6 +48,9 @@
 
   int m_rand_id; //!< Random identifier for this cell.
   static t_id_map s_rand_ids; //!< Container for m_rand_id -> cell* mappings.
+  
+  int m_x; //!< The x-coordinate of the position of this cell in the environment.
+  int m_y; //!< The y-coordinate of the position of this cell in the environment.
 
   void InsertOrganism(cOrganism & new_org);
   cOrganism* RemoveOrganism();
@@ -59,7 +62,7 @@
 
   void operator=(const cPopulationCell& in_cell);
 
-  void Setup(cWorld* world, int in_id, const cMutationRates & in_rates);
+  void Setup(cWorld* world, int in_id, const cMutationRates & in_rates, int x, int y);
   void Rotate(cPopulationCell & new_facing);
 
   cOrganism* GetOrganism() const { return organism; }
@@ -78,9 +81,14 @@
   
   //! Returns the random ID for this cell.
   int GetRandomCellID() const { return m_rand_id; }
+  //!< Retrieves the position (x,y) coordinates of this cell.
+  void GetPosition(int& x, int& y) { x = m_x; y = m_y; }
+  //!< Returns the facing of this cell.
+  int GetFacing();
   //! Returns a boolean representing whether or not the given ID belongs to a cell.
   static int IsRandomCellID(int id) { return s_rand_ids.find(id)!=s_rand_ids.end(); }
-  static int GetMaxRandomCellID(); //! Returns the current maximum random cell ID.
+  //! Returns the current maximum random cell ID.
+  static int GetMaxRandomCellID();
 };
 
 

Modified: branches/coopcomm/source/main/cPopulationInterface.cc
===================================================================
--- branches/coopcomm/source/main/cPopulationInterface.cc	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/main/cPopulationInterface.cc	2006-09-24 15:48:53 UTC (rev 977)
@@ -208,5 +208,23 @@
 
 int cPopulationInterface::GetRandomCellID()
 {
-  return m_world->GetPopulation().GetCell(m_cell_id).GetRandomCellID();
+	cPopulationCell& cell = m_world->GetPopulation().GetCell(m_cell_id);
+	assert(cell.IsOccupied());
+  return cell.GetRandomCellID();
 }
+
+
+void cPopulationInterface::GetPosition(int& x, int& y)
+{
+	cPopulationCell& cell = m_world->GetPopulation().GetCell(m_cell_id);
+	assert(cell.IsOccupied());
+	cell.GetPosition(x, y);
+}
+
+
+int cPopulationInterface::GetFacing()
+{
+	cPopulationCell& cell = m_world->GetPopulation().GetCell(m_cell_id);
+	assert(cell.IsOccupied());
+	return cell.GetFacing();
+}

Modified: branches/coopcomm/source/main/cPopulationInterface.h
===================================================================
--- branches/coopcomm/source/main/cPopulationInterface.h	2006-09-24 02:04:14 UTC (rev 976)
+++ branches/coopcomm/source/main/cPopulationInterface.h	2006-09-24 15:48:53 UTC (rev 977)
@@ -62,8 +62,14 @@
   bool UpdateMerit(double new_merit);
   bool TestOnDivide();
 
-  bool SendMessage(cOrgMessage& msg); //!< Sends a message to the organism that is currently faced.
-  int GetRandomCellID(); //!< Returns the random cell ID for the cell in which this organism lives.
+  //!< Sends a message to the organism that is currently faced.
+  bool SendMessage(cOrgMessage& msg);
+  //!< Returns the random cell ID for the cell in which this organism lives.
+  int GetRandomCellID();
+  //!< Retrieves the position (x,y) coordinates of this organism.
+  void GetPosition(int& x, int& y);
+  //!< Returns the facing of this organism.
+  int GetFacing();
 };
 
 




More information about the Avida-cvs mailing list