[Avida-SVN] r1088 - in branches/coopcomm/source: cpu main

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Mon Nov 20 08:04:12 PST 2006


Author: dknoester
Date: 2006-11-20 11:04:12 -0500 (Mon, 20 Nov 2006)
New Revision: 1088

Modified:
   branches/coopcomm/source/cpu/cHardwareCPU.cc
   branches/coopcomm/source/main/cOrganism.cc
   branches/coopcomm/source/main/cOrganism.h
   branches/coopcomm/source/main/cPopulation.cc
   branches/coopcomm/source/main/cStats.cc
   branches/coopcomm/source/main/cStats.h
Log:
First try at a demes-based approach for leader election.

Modified: branches/coopcomm/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/coopcomm/source/cpu/cHardwareCPU.cc	2006-11-19 18:21:08 UTC (rev 1087)
+++ branches/coopcomm/source/cpu/cHardwareCPU.cc	2006-11-20 16:04:12 UTC (rev 1088)
@@ -3619,8 +3619,10 @@
 */
 bool cHardwareCPU::Inst_SetLeader(cAvidaContext& ctx)
 {
-  organism->SetLeader();
-  return Inst_GetLeader(ctx);
+  const int id_reg = FindModifiedRegister(REG_BX);
+  organism->SetLeaderID(GetRegister(id_reg));
+  return true;
+//  return Inst_GetLeader(ctx);
 }
 
 
@@ -3633,16 +3635,19 @@
 */
 bool cHardwareCPU::Inst_GetLeader(cAvidaContext& ctx)
 {
-  cOrganism* org = organism->GetLeader();
   const int id_reg = FindModifiedRegister(REG_BX);
-  
-  if(org) {
-    GetRegister(id_reg) = org->GetRandomCellID();
-    return true;
-  } else {
-    GetRegister(id_reg) = 0;
-    return false;
-  }
+  GetRegister(id_reg) = organism->GetLeaderID();
+  return true;
+//  cOrganism* org = organism->GetLeader();
+//  const int id_reg = FindModifiedRegister(REG_BX);
+//  
+//  if(org) {
+//    GetRegister(id_reg) = org->GetRandomCellID();
+//    return true;
+//  } else {
+//    GetRegister(id_reg) = 0;
+//    return false;
+//  }
 }
 
 

Modified: branches/coopcomm/source/main/cOrganism.cc
===================================================================
--- branches/coopcomm/source/main/cOrganism.cc	2006-11-19 18:21:08 UTC (rev 1087)
+++ branches/coopcomm/source/main/cOrganism.cc	2006-11-20 16:04:12 UTC (rev 1088)
@@ -61,6 +61,7 @@
   , m_retrieve_pos(0)
   , last_cell_sent_to(-1)
   , m_pLeader(NULL)
+  , m_leaderID(0)
 {
   // Initialization of structures...
   m_hardware = m_world->GetHardwareManager().Create(this);
@@ -699,3 +700,13 @@
 {
   return m_pLeader;
 }
+
+
+int cOrganism::GetLeaderID() {
+  return m_leaderID;
+}
+
+
+bool cOrganism::SetLeaderID(int leader) {
+  m_leaderID = leader;
+}

Modified: branches/coopcomm/source/main/cOrganism.h
===================================================================
--- branches/coopcomm/source/main/cOrganism.h	2006-11-19 18:21:08 UTC (rev 1087)
+++ branches/coopcomm/source/main/cOrganism.h	2006-11-20 16:04:12 UTC (rev 1088)
@@ -152,6 +152,7 @@
   int last_cell_sent_to;
   cOrganism* m_pLeader; //!< A pointer to the organism that is this one's leader.
   t_organism_list m_lead; //!< A list of pointers to organisms that this organism leads.
+  int m_leaderID; //!< The ID of this organism's leader.
   
   cOrganism(); // @not_implemented
   cOrganism(const cOrganism&); // @not_implemented
@@ -323,6 +324,10 @@
   void ResetLeader() { m_pLeader = NULL; }
   //! Get the list of organisms lead by this one.
   t_organism_list& GetLeadOrganisms() { return m_lead; }
+  //! Returns the ID of this organism's leader.
+  int GetLeaderID();
+  //! Sets the ID of this organism's leader.
+  bool SetLeaderID(int leader);
 };
 
 

Modified: branches/coopcomm/source/main/cPopulation.cc
===================================================================
--- branches/coopcomm/source/main/cPopulation.cc	2006-11-19 18:21:08 UTC (rev 1087)
+++ branches/coopcomm/source/main/cPopulation.cc	2006-11-20 16:04:12 UTC (rev 1088)
@@ -41,6 +41,7 @@
 #include <vector>
 #include <algorithm>
 #include <set>
+#include <map>
 
 #include <float.h>
 #include <math.h>
@@ -744,10 +745,64 @@
       }
     }
       break;
+    
+    case 7:
+    {
+      // Competing demes based on the number of organisms that have agreed on a leader.
+      //
+      // For each deme, build up the number of organisms that have "elected"
+      // leaders.  Total fitness is the sum of the deme_fitness; deme_fitness is
+      // the max number of organisms that have agreed on a single leader.
+      //
+      for(int cur_deme=0; cur_deme<num_demes; ++cur_deme) {
+        // for each deme...
+        cDoubleSum single_deme_fitness;
+        std::map<int, std::set<int> > votes;
+        unsigned int max=0;
+        unsigned int org_count=0;
+        
+        for (int i=0; i<deme_size; ++i) {
+          // for each cell in the deme
+          int cur_cell = cur_deme * deme_size + i;
+          // ... that has an organism
+          if (cell_array[cur_cell].IsOccupied() == false) continue;
+          // ... record the vote:
+          ++org_count;
+          cOrganism* org = GetCell(cur_cell).GetOrganism();
+          // Skip orgs that haven't set a leader.
+          if(org->GetLeaderID() == 0) continue;
+          // And skip orgs that didn't set an organism ID
+          if(!cPopulationCell::IsRandomCellID(org->GetLeaderID())) continue;
+          if(votes.find(org->GetLeaderID()) == votes.end()) {
+            votes.insert(std::make_pair(org->GetLeaderID(), std::set<int>()));
+          }
+          votes[org->GetLeaderID()].insert(org->GetRandomCellID());
+          // ... and keep track of the winning organism.
+          if(votes[org->GetLeaderID()].size() > max) {
+            max = votes[org->GetLeaderID()].size();
+          }
+        }
+
+        if(org_count > 0) {
+          m_world->GetStats().Demes_Leadership((double)max/org_count);
+          deme_fitness[cur_deme] = (double)max/org_count;
+          total_fitness += deme_fitness[cur_deme];
+        } else {
+          deme_fitness[cur_deme] = 0;
+        }
+        
+        // Pull out the max number that have voted for an organism 
+
+        
+      }
+      break;      
+    }
   } 
   
   // Pick which demes should be in the next generation.
   tArray<int> new_demes(num_demes);
+
+  if(total_fitness > 0.0) {
   for (int i = 0; i < num_demes; i++) {
     double birth_choice = (double) m_world->GetRandom().GetDouble(total_fitness);
     double test_total = 0;
@@ -759,6 +814,11 @@
       }
     }
   }
+  } else {
+    for(int i=0; i<num_demes; ++i) {
+      new_demes[i] = i;
+    }
+  }
   
   // Track how many of each deme we should have.
   tArray<int> deme_count(num_demes);

Modified: branches/coopcomm/source/main/cStats.cc
===================================================================
--- branches/coopcomm/source/main/cStats.cc	2006-11-19 18:21:08 UTC (rev 1087)
+++ branches/coopcomm/source/main/cStats.cc	2006-11-20 16:04:12 UTC (rev 1088)
@@ -98,6 +98,7 @@
   , m_data_is_sender(0)
   , m_data_is_id_and_grt_sender(0)
   , m_data_is_leader(0)
+  , m_max_deme_leadership(0.0)
 {
   task_cur_count.Resize( m_world->GetNumTasks() );
   task_last_count.Resize( m_world->GetNumTasks() );
@@ -920,6 +921,15 @@
 }
 
 
+/*! This captures information related to using demes to elect a leader.
+*/
+void cStats::Demes_Leadership(double maxPct)
+{
+  if(maxPct > m_max_deme_leadership)
+    m_max_deme_leadership = maxPct;
+}
+
+
 /*! This captures all information related to an organism's death.
 
 This is different than RecordDeath() (above), in that we have a handle to the
@@ -1000,11 +1010,13 @@
   df.Write(m_data_is_id_and_grt_sender, "Count of messages where the data field carries an ID, and is greater than the sender's ID.");
   df.Write(m_data_is_leader, "Count of messages where the data field is the leader's ID.");
   df.Write(m_data_is_id_not_sender.Ave(), "Mean of message data fields where the data field is an ID that is not the sender's ID.");
+  df.Write(m_max_deme_leadership, "Maximum pct of organisms that agreed on a leader.");
   df.Endl();
   
   m_msg_data.Clear();
   m_data_is_id_not_sender.Clear();
   m_count_msgs = m_max_id = m_data_is_id = m_data_is_sender = m_data_is_id_and_grt_sender = m_data_is_leader = 0;
+  m_max_deme_leadership = 0.0;
 }
 
 

Modified: branches/coopcomm/source/main/cStats.h
===================================================================
--- branches/coopcomm/source/main/cStats.h	2006-11-19 18:21:08 UTC (rev 1087)
+++ branches/coopcomm/source/main/cStats.h	2006-11-20 16:04:12 UTC (rev 1088)
@@ -258,6 +258,7 @@
   cDoubleSum m_data_is_id_not_sender; //!< Sum of message data fields where the data field is an ID that is not the sender's ID.
   std::set<int> m_cell_sent_leader; //!< Set of all cells that sent a message carrying the leader's ID.
   t_predicate_list m_predicate_list; //!< The list of predicates used to choose which messages to track.
+  double m_max_deme_leadership; //!< The maximum percentage of organisms agreeing on a leader.
   
   cStats(); // @not_implemented
   cStats(const cStats&); // @not_implemented
@@ -605,6 +606,8 @@
   // Coop-comm.
   //! Capture information related to sending a message.
   void SentMessage(cOrgMessage& msg);
+  //! Info about leadership demes.
+  void Demes_Leadership(double maxPct);
   //! Capture information related to organism death.
   void OrganismDeath(cOrganism* org);
   //! Log leadership-specific data.




More information about the Avida-cvs mailing list