[Avida-SVN] r1110 - branches/coopcomm/source/main

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Mon Dec 4 10:44:10 PST 2006


Author: beckma24
Date: 2006-12-04 13:44:10 -0500 (Mon, 04 Dec 2006)
New Revision: 1110

Modified:
   branches/coopcomm/source/main/cOrganism.cc
   branches/coopcomm/source/main/cOrganism.h
   branches/coopcomm/source/main/cTaskLib.cc
   branches/coopcomm/source/main/cTaskLib.h
Log:
Added sent-toward-sink-multi-path task

Modified: branches/coopcomm/source/main/cOrganism.cc
===================================================================
--- branches/coopcomm/source/main/cOrganism.cc	2006-12-02 06:53:56 UTC (rev 1109)
+++ branches/coopcomm/source/main/cOrganism.cc	2006-12-04 18:44:10 UTC (rev 1110)
@@ -557,6 +557,8 @@
     // Stat-tracking.
     m_world->GetStats().SentMessage(msg);
 
+    sent_to.insert(last_cell_sent_to);
+
     int old_size = m_sent_messages.size();
   
     // store the message.  yes, we're storing it twice.  yes, we could save memory

Modified: branches/coopcomm/source/main/cOrganism.h
===================================================================
--- branches/coopcomm/source/main/cOrganism.h	2006-12-02 06:53:56 UTC (rev 1109)
+++ branches/coopcomm/source/main/cOrganism.h	2006-12-04 18:44:10 UTC (rev 1110)
@@ -14,7 +14,9 @@
 #include <fstream>
 #include <vector>
 #include <map>
+#include <set>
 
+
 #ifndef cCPUMemory_h
 #include "cCPUMemory.h"
 #endif
@@ -104,10 +106,11 @@
   tBuffer<int> input_buf;
   tBuffer<int> output_buf;
   tBuffer<int> send_buf;
+  std::set<int> sent_to;
   tBuffer<int> receive_buf;
   tBuffer<int> received_messages;
   tList<tListNode<cSaleItem> > sold_items;
-
+  
   // Communication
   int sent_value;         // What number is this org sending?
   bool sent_active;       // Has this org sent a number?
@@ -316,6 +319,8 @@
   int GetFacing() { assert(m_interface); return m_interface->GetFacing(); }
   //! Returns Cell ID of last cell sent to.
   int getLastSent(){ return last_cell_sent_to; }
+  //! Returns a set of all organism who have receive a message from this organism
+  std::set<int> getSentTo() {return sent_to;}
   //! Set the leader of this organism to the organism currently faced.
   bool SetLeader();
   //! Get the cOrganism that is currently the leader of this organism.

Modified: branches/coopcomm/source/main/cTaskLib.cc
===================================================================
--- branches/coopcomm/source/main/cTaskLib.cc	2006-12-02 06:53:56 UTC (rev 1109)
+++ branches/coopcomm/source/main/cTaskLib.cc	2006-12-04 18:44:10 UTC (rev 1110)
@@ -52,7 +52,7 @@
   
   // The following if blocks are grouped based on class of task.  Chaining too
   // many if block causes problems block nesting depth in Visual Studio.net 2003.
-
+  
   if (name == "echo")      NewTask(name, "Echo", &cTaskLib::Task_Echo);
   else if (name == "add")  NewTask(name, "Add",  &cTaskLib::Task_Add);
   else if (name == "sub")  NewTask(name, "Sub",  &cTaskLib::Task_Sub);
@@ -332,18 +332,18 @@
     cArgContainer* args = cArgContainer::Load(info, schema);
     if (args) NewTask(name, "Match Number", &cTaskLib::Task_MatchNumber, 0, args);
   }
-
-	// Communication Tasks
+  
+  // Communication Tasks
   if (name == "comm_echo")
     NewTask(name, "Echo of Neighbor's Input", &cTaskLib::Task_CommEcho, REQ_NEIGHBOR_INPUT);
   else if (name == "comm_not")
-	  NewTask(name, "Not of Neighbor's Input", &cTaskLib::Task_CommNot, REQ_NEIGHBOR_INPUT);
-
+    NewTask(name, "Not of Neighbor's Input", &cTaskLib::Task_CommNot, REQ_NEIGHBOR_INPUT);
+  
   // Network Tasks
   if (name == "net_send")
-	  NewTask(name, "Successfully Sent Network Message", &cTaskLib::Task_NetSend);
+    NewTask(name, "Successfully Sent Network Message", &cTaskLib::Task_NetSend);
   else if (name == "net_receive")
-	  NewTask(name, "Successfully Received Network Message", &cTaskLib::Task_NetReceive);
+    NewTask(name, "Successfully Received Network Message", &cTaskLib::Task_NetReceive);
   
   // Coop-comm Tasks
   if(name == "max-id")
@@ -360,6 +360,8 @@
     NewTask(name, "Reward the sender for sending toward the sink", &cTaskLib::Task_SendTowardSink);
   else if(name == "send-not-toward-sink")
     NewTask(name, "Reward the sender for not sending toward the sink", &cTaskLib::Task_SendNotTowardSink);
+  else if(name == "send-toward-sink-multi-path")
+    NewTask(name, "Reward the sender for sending toward the sink using multiple closer neighbors", &cTaskLib::Task_SendTowardSinkMultiPath);
   else if(name == "sent-north")
     NewTask(name, "Reward the sender for sending north", &cTaskLib::Task_SentNorth);
   else if(name == "sent-northwest")
@@ -1775,62 +1777,62 @@
 
 double cTaskLib::Task_MatchStr(cTaskContext* ctx) const
 {
-	tBuffer<int> temp_buf(ctx->output_buffer);
-	if (temp_buf[0] != 357913941) return 0;
-
-	temp_buf.Pop(); // pop the signal value off of the buffer
-
-	const cString& string_to_match = ctx->task_entry->GetArguments().GetString(0);
-	int string_index;
-	int num_matched = 0;
-	int test_output;
+  tBuffer<int> temp_buf(ctx->output_buffer);
+  if (temp_buf[0] != 357913941) return 0;
+  
+  temp_buf.Pop(); // pop the signal value off of the buffer
+  
+  const cString& string_to_match = ctx->task_entry->GetArguments().GetString(0);
+  int string_index;
+  int num_matched = 0;
+  int test_output;
   int max_num_matched = 0;
-
-	if (temp_buf.GetNumStored() > 0) {
-		test_output = temp_buf[0];
-	
-		for (int j = 0; j < string_to_match.GetSize(); j++) {	
-			string_index = string_to_match.GetSize() - j - 1; // start with last char in string
-			int k = 1 << j;
-			if ((string_to_match[string_index] == '0' && !(test_output & k)) ||
+  
+  if (temp_buf.GetNumStored() > 0) {
+    test_output = temp_buf[0];
+    
+    for (int j = 0; j < string_to_match.GetSize(); j++) {	
+      string_index = string_to_match.GetSize() - j - 1; // start with last char in string
+      int k = 1 << j;
+      if ((string_to_match[string_index] == '0' && !(test_output & k)) ||
           (string_to_match[string_index] == '1' && (test_output & k))) num_matched++;
-		}
-		max_num_matched = num_matched;
-	}
-
-	bool used_received = false;
-	if (ctx->received_messages) {
-		tBuffer<int> received(*(ctx->received_messages));
-		for (int i = 0; i < received.GetNumStored(); i++) {
-			test_output = received[i];
-			num_matched = 0;
-			
+    }
+    max_num_matched = num_matched;
+  }
+  
+  bool used_received = false;
+  if (ctx->received_messages) {
+    tBuffer<int> received(*(ctx->received_messages));
+    for (int i = 0; i < received.GetNumStored(); i++) {
+      test_output = received[i];
+      num_matched = 0;
+      
       for (int j = 0; j < string_to_match.GetSize(); j++) {
-				string_index = string_to_match.GetSize() - j - 1; // start with last char in string
-				int k = 1 << j;
-				if ((string_to_match[string_index]=='0' && !(test_output & k)) ||
+        string_index = string_to_match.GetSize() - j - 1; // start with last char in string
+        int k = 1 << j;
+        if ((string_to_match[string_index]=='0' && !(test_output & k)) ||
             (string_to_match[string_index]=='1' && (test_output & k))) num_matched++;
-			}
-			
+      }
+      
       if (num_matched > max_num_matched) {
-				max_num_matched = num_matched;
-				used_received = true;
-			}
-		}
-	}
-
-	double bonus = 0.0;
-	// return value between 0 & 1 representing the percentage of string that was matched
-	double base_bonus = static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
-	
-	if (base_bonus > 0.0) {
-		bonus = pow(base_bonus, 2);
-		if (used_received)
+        max_num_matched = num_matched;
+        used_received = true;
+      }
+    }
+  }
+  
+  double bonus = 0.0;
+  // return value between 0 & 1 representing the percentage of string that was matched
+  double base_bonus = static_cast<double>(max_num_matched) * 2.0 / static_cast<double>(string_to_match.GetSize()) - 1;
+  
+  if (base_bonus > 0.0) {
+    bonus = pow(base_bonus, 2);
+    if (used_received)
       m_world->GetStats().AddMarketItemUsed();
-		else
-			m_world->GetStats().AddMarketOwnItemUsed();
-	}
-	return bonus;
+    else
+      m_world->GetStats().AddMarketOwnItemUsed();
+  }
+  return bonus;
 }
 
 
@@ -1838,51 +1840,51 @@
 {
   double quality = 0.0;
   const cArgContainer& args = ctx->task_entry->GetArguments();
-
+  
   int diff = abs(args.GetInt(0) - ctx->output_buffer[0]);
   int threshold = args.GetInt(1);
-    
+  
   if (threshold < 0 || diff <= abs(threshold)) { // Negative threshold == infinite
-    // If within threshold range, quality decays based on absolute difference
+                                                 // If within threshold range, quality decays based on absolute difference
     double halflife = -1.0 * fabs(args.GetDouble(0));
     quality = pow(2.0, static_cast<double>(diff) / halflife);
   }
-
+  
   return quality;
 }
 
 double cTaskLib::Task_CommEcho(cTaskContext* ctx) const
 {
-	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 == cur_buff[i]) return 1.0;
-		}
-	}
-
-	return 0.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 == cur_buff[i]) return 1.0;
+    }
+  }
+  
+  return 0.0;
 }
 
 double cTaskLib::Task_CommNot(cTaskContext* ctx) const
 {
-	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;
+  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;
 }
 
 double cTaskLib::Task_NetSend(cTaskContext* ctx) const
@@ -1908,7 +1910,7 @@
 {
   cOrgMessage* msg = ctx->GetMessage();
   if(msg == NULL) return 0.0;
-
+  
   cOrganism::t_sorted_messages& received_msgs = msg->GetSender()->GetSortedReceivedMessages();
   if(!cPopulationCell::IsRandomCellID(msg->GetData()) || received_msgs.empty()) {
     return 0.0;
@@ -1962,7 +1964,7 @@
 {
   cOrgMessage* msg = ctx->GetMessage();
   if(msg == NULL) return 0.0;
-
+  
   if(!cPopulationCell::IsRandomCellID(msg->GetData()))
     return 1.0;
   
@@ -1989,19 +1991,19 @@
     //catch IO
     return 0.0;
   }
-
+  
   tVector<int> BS_iter = m_world->GetPopulation().GetBaseStations();
-  int x_sender, y_sender, x_receiver, y_receiver, x_BS, y_BS;
   
+  //NEED FOR TORUS UNTIL REWRITTEN
+  int x_sender, y_sender, x_receiver, y_receiver, x_BS, y_BS;  
   ctx->GetMessage()->GetSender()->GetPosition(x_sender,y_sender);
   ctx->GetMessage()->GetReceiver()->GetPosition(x_receiver,y_receiver);
-
+  
   //check all base stations
   for(int i =0; i < BS_iter.Size(); i++) {
-    m_world->GetPopulation().GetCell(BS_iter[i]).GetPosition(x_BS,y_BS);
-
+    cPopulationCell& bsCell = m_world->GetPopulation().GetCell(BS_iter[i]);
     if(nGeometry::GRID == m_world->GetConfig().WORLD_GEOMETRY.Get()) {     //bounded grid
-      if(max(abs(x_receiver-x_BS), abs(y_receiver-y_BS)) < max(abs(x_sender-x_BS), abs(y_sender-y_BS))) {
+      if(hopWiseCloserGRID(ctx->GetMessage()->GetSender()->GetCell(), ctx->GetMessage()->GetReceiver()->GetCell(), &bsCell)) {
         //sent to closer organisms
         return 1.0;
       }
@@ -2010,7 +2012,7 @@
       int y_world = m_world->GetConfig().WORLD_Y.Get();
       std::cerr<<"TORUS cTaskLib::Task_SendTowardSink\n";
       if(min(max(abs(x_receiver-x_BS), abs(y_receiver-y_BS)), max(x_world-abs(x_receiver-x_BS), y_world-abs(y_receiver-y_BS))) <
-          min(max(abs(x_sender-x_BS), abs(y_sender-y_BS)), max(x_world-abs(x_sender-x_BS), y_world-abs(y_sender-y_BS)))) {
+         min(max(abs(x_sender-x_BS), abs(y_sender-y_BS)), max(x_world-abs(x_sender-x_BS), y_world-abs(y_sender-y_BS)))) {
         //sent to closer organisms
         return 1.0;        
       }
@@ -2032,10 +2034,94 @@
   return 0.0;
 }
 
+double cTaskLib::Task_SendTowardSinkMultiPath(cTaskContext* ctx) const {
+  if(ctx->GetMessage() == NULL ) {
+    //catch IO
+    return 0.0;
+  }
+  
+  std::set<int> senderSentTo = ctx->GetMessage()->GetSender()->getSentTo();
+  if(senderSentTo.size() <= 1) {
+    return 0.0;
+  }
+  
+  tVector<int> BS_iter = m_world->GetPopulation().GetBaseStations();
+  int orginalReceiver = ctx->GetMessage()->GetReceiver()->GetCellID();
+  bool sentToOrgCloser = false;
+  
+  //check all base stations
+  for(int i =0; i < BS_iter.Size(); i++) {
+    cPopulationCell& bsCell = m_world->GetPopulation().GetCell(BS_iter[i]);
+    if(nGeometry::GRID == m_world->GetConfig().WORLD_GEOMETRY.Get()) {     //bounded grid
+      if(hopWiseCloserGRID(ctx->GetMessage()->GetSender()->GetCell(), ctx->GetMessage()->GetReceiver()->GetCell(), &bsCell)) {
+        //sent to closer organisms
+        sentToOrgCloser = true;
+        break;
+      }
+    } else if(nGeometry::TORUS == m_world->GetConfig().WORLD_GEOMETRY.Get()) {    //torus  CLEAN UP (like grid)
+      cerr<< "ERROR: TORUS not implemented\n";
+      exit(-1);
+
+/*      int x_world = m_world->GetConfig().WORLD_X.Get();
+      int y_world = m_world->GetConfig().WORLD_Y.Get();
+      std::cerr<<"TORUS cTaskLib::Task_SendTowardSink\n";
+      if(min(max(abs(x_receiver-x_BS), abs(y_receiver-y_BS)), max(x_world-abs(x_receiver-x_BS), y_world-abs(y_receiver-y_BS))) <
+         min(max(abs(x_sender-x_BS), abs(y_sender-y_BS)), max(x_world-abs(x_sender-x_BS), y_world-abs(y_sender-y_BS)))) {
+        //sent to closer organisms
+        sentToOrgCloser = true;        
+      }*/
+    } else {    //other - error
+      cerr<< "ERROR: Unknown WORLD_GEOMETRY\n";
+      exit(-1);
+    }
+  }
+  
+  if(sentToOrgCloser) {
+    /////////////////////////HEERE    
+    if(nGeometry::GRID == m_world->GetConfig().WORLD_GEOMETRY.Get()) {     //bounded grid
+      for(int i =0; i < BS_iter.Size(); i++) {
+        for(std::set<int>::iterator iter = senderSentTo.begin(); iter != senderSentTo.end(); iter++) {
+          if(*iter != orginalReceiver) {
+            cPopulationCell& bsCell = m_world->GetPopulation().GetCell(BS_iter[i]);
+            if(hopWiseCloserGRID(ctx->GetMessage()->GetSender()->GetCell(), &(m_world->GetPopulation().GetCell(*iter)), &bsCell)) {
+              //sent to closer organisms
+              return 1.0;
+            }
+          }
+        }
+      }
+    } else if(nGeometry::TORUS == m_world->GetConfig().WORLD_GEOMETRY.Get()) {    //torus  CLEAN UP (like grid)
+      std::cerr<<"TORUS cTaskLib::Task_SendTowardSinkMiltiPath  NOT IMPLEMENTED\n";
+      exit(-2);
+    }
+  }  //else
+  return 0.0;
+}
+
+bool cTaskLib::hopWiseCloserGRID(cPopulationCell* sender, cPopulationCell* receiver, cPopulationCell* baseStation) const {
+  int x_sender, y_sender, x_receiver, y_receiver, x_BS, y_BS;
+  
+  sender->GetPosition(x_sender,y_sender);
+  receiver->GetPosition(x_receiver,y_receiver);
+  baseStation->GetPosition(x_BS,y_BS);
+
+  if(max(abs(x_receiver-x_BS), abs(y_receiver-y_BS)) < max(abs(x_sender-x_BS), abs(y_sender-y_BS))) {
+    //sent to closer organisms
+    return true;
+  }
+  return false;
+}
+
+bool cTaskLib::hopWiseCloserTORUS() const{
+  return false;
+}
+
+
+
 double cTaskLib::Task_SentNorth(cTaskContext* ctx) const {
   if(ctx->GetMessage() == NULL)
     return 0.0;
-
+  
   cPopulationCell* sender = ctx->GetMessage()->GetSender()->GetCell();
   if(sender->GetFacing() == 6)
     return 1.0;
@@ -2045,7 +2131,7 @@
 double cTaskLib::Task_SentNorthWest(cTaskContext* ctx) const {
   if(ctx->GetMessage() == NULL)
     return 0.0;
-
+  
   cPopulationCell* sender = ctx->GetMessage()->GetSender()->GetCell();
   if(sender->GetFacing() == 2)
     return 1.0;
@@ -2055,7 +2141,7 @@
 double cTaskLib::Task_SentWest(cTaskContext* ctx) const {
   if(ctx->GetMessage() == NULL)
     return 0.0;
-
+  
   cPopulationCell* sender = ctx->GetMessage()->GetSender()->GetCell();
   if(sender->GetFacing() == 3)
     return 1.0;
@@ -2065,7 +2151,7 @@
 double cTaskLib::Task_SentSouthWest(cTaskContext* ctx) const {
   if(ctx->GetMessage() == NULL)
     return 0.0;
-
+  
   cPopulationCell* sender = ctx->GetMessage()->GetSender()->GetCell();
   if(sender->GetFacing() == 1)
     return 1.0;
@@ -2075,7 +2161,7 @@
 double cTaskLib::Task_SentSouth(cTaskContext* ctx) const {
   if(ctx->GetMessage() == NULL)
     return 0.0;
-
+  
   cPopulationCell* sender = ctx->GetMessage()->GetSender()->GetCell();
   if(sender->GetFacing() == 0)
     return 1.0;
@@ -2085,7 +2171,7 @@
 double cTaskLib::Task_SentSouthEast(cTaskContext* ctx) const {
   if(ctx->GetMessage() == NULL)
     return 0.0;
-
+  
   cPopulationCell* sender = ctx->GetMessage()->GetSender()->GetCell();
   if(sender->GetFacing() == 4)
     return 1.0;
@@ -2095,7 +2181,7 @@
 double cTaskLib::Task_SentEast(cTaskContext* ctx) const {
   if(ctx->GetMessage() == NULL)
     return 0.0;
-
+  
   cPopulationCell* sender = ctx->GetMessage()->GetSender()->GetCell();
   if(sender->GetFacing() == 5)
     return 1.0;
@@ -2105,7 +2191,7 @@
 double cTaskLib::Task_SentNorthEast(cTaskContext* ctx) const {
   if(ctx->GetMessage() == NULL)
     return 0.0;
-
+  
   cPopulationCell* sender = ctx->GetMessage()->GetSender()->GetCell();
   if(sender->GetFacing() == 7)
     return 1.0;

Modified: branches/coopcomm/source/main/cTaskLib.h
===================================================================
--- branches/coopcomm/source/main/cTaskLib.h	2006-12-02 06:53:56 UTC (rev 1109)
+++ branches/coopcomm/source/main/cTaskLib.h	2006-12-04 18:44:10 UTC (rev 1110)
@@ -243,6 +243,8 @@
   double Task_SendTowardSink(cTaskContext* ctx) const;
   //! Reward an organism for sending message to organism who is not closer to the sink
   double Task_SendNotTowardSink(cTaskContext* ctx) const;
+  //! REward an organism for sending a message to multiple closer neighbors (by hop count)
+  double Task_SendTowardSinkMultiPath(cTaskContext* ctx) const;
   //! simple directional sending tasks
   double Task_SentNorth(cTaskContext* ctx) const;
   double Task_SentNorthWest(cTaskContext* ctx) const;
@@ -255,6 +257,10 @@
   
   //! Rewards an organism for being part of a tree.
   double Task_GraphEquTree(cTaskContext* ctx) const;
+  
+  //! helper functions
+  bool hopWiseCloserGRID(cPopulationCell*, cPopulationCell*, cPopulationCell*) const;
+  bool hopWiseCloserTORUS() const;
 };
 
 




More information about the Avida-cvs mailing list