avida-landscape computes 0-dimensional persistence of the fitness landscape
authorDmitriy Morozov <morozov@cs.duke.edu>
Sun, 17 Feb 2008 16:32:33 -0500
changeset 46 6fbe003aad9d
parent 45 7c15f27bfa1b
child 47 19249d6d9f2d
child 75 8fae3f57d750
avida-landscape computes 0-dimensional persistence of the fitness landscape
examples/fitness/avida-landscape.cpp
examples/fitness/avida-population-detail.h
--- a/examples/fitness/avida-landscape.cpp	Sun Feb 17 09:16:30 2008 -0500
+++ b/examples/fitness/avida-landscape.cpp	Sun Feb 17 16:32:33 2008 -0500
@@ -7,12 +7,18 @@
 #include <topology/lowerstarfiltration.h>
 
 
-typedef			AvidaPopulationDetail::OrganismIndex				OrganismIndex;
+typedef         AvidaPopulationDetail::OrganismIndex                OrganismIndex;
 struct          OrganismVertexType;
 typedef         std::vector<OrganismVertexType>                     VertexVector;
 typedef         VertexVector::iterator                              VertexIndex;
-typedef			LowerStarFiltration<VertexIndex>					LSFiltration;
-struct          OrganismVertexType: public LSFiltration::VertexType<OrganismIndex> {};
+typedef         LowerStarFiltration<VertexIndex>                    LSFiltration;
+typedef         LSFiltration::Simplex                               Simplex;
+
+struct          OrganismVertexType: public LSFiltration::VertexType<OrganismIndex> 
+{
+    typedef     LSFiltration::VertexType<OrganismIndex>             Parent;
+                OrganismVertexType(OrganismIndex i): Parent(i)      {}
+};
 
 struct          OrganismVertexComparison
 {
@@ -21,93 +27,88 @@
         { return i->index()->fitness() < j->index()->fitness(); }
 };
 
+typedef         LSFiltration::Vineyard                              LSVineyard;
+class           StaticEvaluator: public LSVineyard::Evaluator
+{
+    public:
+        virtual RealType        value(const Simplex& s) const       { return s.get_attachment()->index()->fitness(); }
+};
+
+std::ostream& operator<<(std::ostream& out, VertexIndex i)
+{ return (out << (i->index())); }
+
 
 int main(int argc, char** argv)
 {
 #ifdef LOGGING
-	rlog::RLogInit(argc, argv);
-	//stdoutLog.subscribeTo(RLOG_CHANNEL("info"));
+    rlog::RLogInit(argc, argv);
+    //stdoutLog.subscribeTo(RLOG_CHANNEL("info"));
 #endif
 
-	if (argc < 3)
-	{
-		std::cout << "USAGE: avida FILENAME DISTANCE" << std::endl;
-		return 0;
-	}
+    if (argc < 3)
+    {
+        std::cout << "USAGE: avida FILENAME DISTANCE" << std::endl;
+        return 0;
+    }
+
+    AvidaPopulationDetail population(argv[1]);
+    AvidaOrganismDetail::DistanceType connected_distance;
+    std::istringstream(argv[2]) >> connected_distance;
+    const AvidaPopulationDetail::OrganismVector& organisms = population.get_organisms();
 
-	AvidaPopulationDetail population(argv[1]);
-	AvidaPopulationDetail::DistanceType connected_distance;
-	std::istringstream(argv[2]) >> connected_distance;
-	const AvidaPopulationDetail::OrganismVector& organisms = population.get_organisms();
+    rInfo("Number of organisms: %d", organisms.size());
+    for (int i = 0; i < population.get_organisms().size(); ++i)
+        rInfo("%d (%s) %f %d %d", organisms[i].id(),
+                                  organisms[i].genome().c_str(),
+                                  organisms[i].fitness(),
+                                  organisms[i].length(),
+                                  organisms[i].genome().size());
+
+    // Order vertices 
+    StaticEvaluator     evaluator;
+    LSVineyard          vineyard(&evaluator);
+    VertexVector        vertices;
+    for (OrganismIndex cur = organisms.begin(); cur != organisms.end(); ++cur)  vertices.push_back(cur);
+    LSFiltration        fitness_filtration(vertices.begin(), vertices.end(), OrganismVertexComparison(), &vineyard);
 
-	rInfo("Number of organisms: %d", organisms.size());
-	for (int i = 0; i < population.get_organisms().size(); ++i)
-		rInfo("%d (%s) %f %d %d", organisms[i].id(),
-								  organisms[i].genome().c_str(),
-								  organisms[i].fitness(),
-								  organisms[i].length(),
-								  organisms[i].genome().size());
+    // Add simplices
+    for (VertexIndex cur = vertices.begin(); cur != vertices.end(); ++cur)
+        for (VertexIndex link = boost::next(cur); link != vertices.end(); ++link)
+            if (cur->index()->genome_distance(*(link->index())) <= connected_distance)
+            {
+                Simplex s(2, cur); s.add(link);
+                fitness_filtration.append(s);
+            }
+    rInfo("Number of simplices: %d", fitness_filtration.size());
+
+    // Pair simplices
+    fitness_filtration.fill_simplex_index_map();
+    fitness_filtration.pair_simplices(false);            // pair simplices without storing trails
 
-	// Order vertices 
-    VertexVector vertices(organisms.begin(), organisms.end());
-	LSFiltration fitness_filtration(vertices.begin(), vertices.end(), OrganismVertexComparison());
+    //std::cout << "Outputting persistence pairs" << std::endl;
+    for (LSFiltration::Index i = fitness_filtration.begin(); i != fitness_filtration.end(); ++i)
+    {
+        if (i->is_paired())
+            if (i->sign())
+            {
+                AssertMsg(i->dimension() == 0, "Expecting only 0-dimensional diagram");
+                if (i->pair()->get_attachment() == i->vertices()[0]) continue;     // skip non-critical pairs
+                std::cout << i->dimension() << " " 
+                          << evaluator.value(*i) << " " 
+                          << evaluator.value(*(i->pair())) << std::endl;
+            }
+    }
 
 #if 0
-	//
-	for (AvidaOrganismDetail::CountType i = 0; i < organisms.size(); ++i)
-	{
-		Simplex s(2, v);
-
-		filtration.append(s);
-
-		simplices.push_back(organisms[i].fitness());
-		simplices.back().add(organisms[i].id());
-
-		for (AvidaOrganismDetail::CountType j = i+1; j < organisms.size(); ++j)
-		{
-			avg_distance += organisms[i].genome_distance(organisms[j]);
-			simplices.push_back(Simplex(organisms[i].genome_distance(organisms[j])));
-			simplices.back().add(organisms[i].id());
-			simplices.back().add(organisms[j].id());
-		}
-	}
-	std::sort(simplices.begin(), simplices.end(), DimensionValueComparison<Simplex>());
-	rInfo("Average distance: %f", float(avg_distance)/
-								  ((organisms.size()*organisms.size() - organisms.size())/2));
-
-	SimplexFiltration filtration;
-	for (SimplexVector::const_iterator cur = simplices.begin(); cur != simplices.end(); ++cur)
-	{
-		rInfo("Simplex: %s", tostring(*cur).c_str());
-		filtration.append(*cur);
-	}
-
-	filtration.fill_simplex_index_map();
-	filtration.pair_simplices(false);			// pair simplices without storing trails
-
-	std::cout << "Outputting histogram of death values" << std::endl;
-	typedef std::vector<RealType> DeathVector;
-	DeathVector deaths;
-	for (SimplexFiltration::Index i = filtration.begin(); i != filtration.end(); ++i)
-	{
-		if (i->is_paired())
-			if (i->sign())
-			{
-				AssertMsg(i->dimension() == 0, "Expecting only 0-dimensional diagram");
-				AssertMsg(i->get_value() == 0, "Expecting only 0 birth values in 0-D diagram ");
-				deaths.push_back(i->pair()->get_value());
-			}
-	}
-
-	// Produce histogram
-	std::sort(deaths.begin(), deaths.end());
-	for (DeathVector::iterator cur = deaths.begin(); cur != deaths.end(); )
-	{
-		DeathVector::iterator nw = std::find_if(cur, deaths.end(), 
-												std::bind2nd(std::greater<RealType>(), *cur));
-		std::cout << *cur << "\t" << (nw - cur) << std::endl;
-		cur = nw;
-	}
-	std::cout << "Total: " << deaths.size() + 1;		// +1 for the unpaired
+    // Produce histogram
+    std::sort(deaths.begin(), deaths.end());
+    for (DeathVector::iterator cur = deaths.begin(); cur != deaths.end(); )
+    {
+        DeathVector::iterator nw = std::find_if(cur, deaths.end(), 
+                                                std::bind2nd(std::greater<RealType>(), *cur));
+        std::cout << *cur << "\t" << (nw - cur) << std::endl;
+        cur = nw;
+    }
+    std::cout << "Total: " << deaths.size() + 1;        // +1 for the unpaired
 #endif
 }
--- a/examples/fitness/avida-population-detail.h	Sun Feb 17 09:16:30 2008 -0500
+++ b/examples/fitness/avida-population-detail.h	Sun Feb 17 16:32:33 2008 -0500
@@ -59,6 +59,9 @@
 		OrganismVector	organisms_;
 };
 
+std::ostream& operator<<(std::ostream& out, AvidaPopulationDetail::OrganismIndex i)
+{ return (out << (i->id())); }
+
 
 #include "avida-population-detail.hpp"