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

dknoester at myxo.css.msu.edu dknoester at myxo.css.msu.edu
Mon Jul 16 07:02:47 PDT 2007


Author: dknoester
Date: 2007-07-16 10:02:47 -0400 (Mon, 16 Jul 2007)
New Revision: 1809

Modified:
   branches/dkdev/source/actions/PopulationActions.cc
   branches/dkdev/source/main/cPopulation.cc
   branches/dkdev/source/main/cStats.cc
   branches/dkdev/source/main/cStats.h
Log:
Added clustering coefficient replicate method.

Modified: branches/dkdev/source/actions/PopulationActions.cc
===================================================================
--- branches/dkdev/source/actions/PopulationActions.cc	2007-07-15 18:44:53 UTC (rev 1808)
+++ branches/dkdev/source/actions/PopulationActions.cc	2007-07-16 14:02:47 UTC (rev 1809)
@@ -899,6 +899,7 @@
     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 if (in_trigger == "clustering-coefficient") m_rep_trigger = 10;
     else {
       cString err("Unknown replication trigger '");
       err += in_trigger;

Modified: branches/dkdev/source/main/cPopulation.cc
===================================================================
--- branches/dkdev/source/main/cPopulation.cc	2007-07-15 18:44:53 UTC (rev 1808)
+++ branches/dkdev/source/main/cPopulation.cc	2007-07-16 14:02:47 UTC (rev 1809)
@@ -42,6 +42,8 @@
 #include <fstream>
 #include <vector>
 #include <algorithm>
+#include <numeric>
+#include <set>
 #include <boost/graph/connected_components.hpp>
 
 #include <float.h>
@@ -977,6 +979,58 @@
         
         break;
       }
+      case 10: {
+        // Replicate demes that are connected, with merit based on their clustering coefficient.
+        // Clustering coefficient is the number of links between a vertex's neighbors / number of possible links.
+        // This case is meant to be used with a completely connection topology, as the number of links between neighbors
+        // in other topologies is somewhat problematic.
+        m_world->GetStats().Topology(source_deme.GetNetwork());
+        if(!network_is_connected(source_deme)) continue;
+        m_world->GetStats().TopologyConnected(source_deme.GetNetwork());
+        m_world->GetStats().TopologyReplication(source_deme.GetNetwork());        
+        
+        // For each vertex in the graph, calculate the number of edges between vertices in the neighborhood.
+        cDeme::Network& network = source_deme.GetNetwork();
+        cDeme::Network::vertex_iterator vi, vi_end;
+        std::vector<double> cluster_coeffs;
+        for(tie(vi,vi_end)=vertices(network); vi!=vi_end; ++vi) {
+          // Get the list of vertices which are in the neighborhood of vi.
+          typedef cDeme::Network::adjacency_iterator adjacency_iterator;
+          std::pair<adjacency_iterator, adjacency_iterator> adjacent = boost::adjacent_vertices(*vi, network);
+          typedef std::set<cDeme::Network::vertex_descriptor> neighborhood;
+          neighborhood neighbors;
+          for(; adjacent.first!=adjacent.second; ++adjacent.first) {
+            neighbors.insert(*adjacent.first);
+          }
+          // Now, count the edges between vertices in the neighborhood.
+          unsigned int neighborhood_edge_count=0;
+          for(neighborhood::iterator i=neighbors.begin(); i!=neighbors.end(); ++i) {
+            typedef cDeme::Network::out_edge_iterator out_edge_iterator;
+            std::pair<out_edge_iterator,out_edge_iterator> oe = out_edges(*i,network);
+            for(; oe.first!=oe.second; ++oe.first) {
+              if(neighbors.find(target(*oe.first,network))!=neighbors.end()) {
+                ++neighborhood_edge_count;
+              }
+            }
+          }
+          neighborhood_edge_count /= 2;
+          cluster_coeffs.push_back((double)neighborhood_edge_count / (neighbors.size()*(neighbors.size()-1)/2));
+        }
+        
+        // Clustering coefficient:
+        double clustering_coeff = 
+          std::accumulate(cluster_coeffs.begin(), cluster_coeffs.end(), 0.0) / cluster_coeffs.size();
+        
+        m_world->GetStats().TopologyClusteringCoeff(clustering_coeff);
+        
+        // Max clustering coefficient is... 1, occuring when all possible edges are present.
+        // Let's try 0.5 first, scaled to 0-100.
+        clustering_coeff *= 100.0;
+        double target_clustering_coeff=50.0;
+        
+        source_germline_merit = pow(target_clustering_coeff - abs(target_clustering_coeff-clustering_coeff) + 1, 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-15 18:44:53 UTC (rev 1808)
+++ branches/dkdev/source/main/cStats.cc	2007-07-16 14:02:47 UTC (rev 1809)
@@ -884,6 +884,11 @@
 }
 
 
+void cStats::TopologyClusteringCoeff(double clustering_coefficient) {
+  _topo_clustering_coeff.Add(clustering_coefficient);
+}
+
+
 void cStats::PrintTopologyData(const cString& filename) {
 	cDataFile& df = m_world->GetDataFile(filename);
 	df.WriteComment( "Topology data\n" );
@@ -895,6 +900,7 @@
   df.Write(_topo_meanlsp.Average(), "mean mean longest shortest-path");
   df.Write(_topo_maxlsp.Average(), "mean max longest shortest-path");
   df.Write(_topo_maxlsp_frac.Average(), "mean (mean lsp / max lsp)");
+  df.Write(_topo_clustering_coeff.Average(), "mean clustering coefficient");
 	df.Endl();
   
   _topo_connected = 0;
@@ -903,6 +909,7 @@
   _topo_meanlsp.Clear();
   _topo_maxlsp.Clear();
   _topo_maxlsp_frac.Clear();
+  _topo_clustering_coeff.Clear();
 }
 
 

Modified: branches/dkdev/source/main/cStats.h
===================================================================
--- branches/dkdev/source/main/cStats.h	2007-07-15 18:44:53 UTC (rev 1808)
+++ branches/dkdev/source/main/cStats.h	2007-07-16 14:02:47 UTC (rev 1809)
@@ -208,6 +208,7 @@
   cDoubleSum _topo_meanlsp; //!< Mean longest shortest-path.
   cDoubleSum _topo_maxlsp; //!< Max longest shortest-path.
   cDoubleSum _topo_maxlsp_frac; //!< Mean LSP / Max LSP.
+  cDoubleSum _topo_clustering_coeff; //!< Clustering coefficient.
   cDeme::Network _topo_last_network; //!< The last constructed network.
 
   int _deme_repl_count; //!< Number of deme replications since the last stats output.
@@ -563,6 +564,7 @@
   void TopologyConnected(cDeme::Network& network);
   void TopologyLSP(double meanlsp, double maxlsp);
   void TopologyReplication(cDeme::Network& network);
+  void TopologyClusteringCoeff(double clustering_coefficient);
   void PrintTopologyData(const cString& filename);
   void PrintLastTopology();
   void PrintEachTopology();




More information about the Avida-cvs mailing list