[Avida-cvs] [avida-svn] r731 - in development/source: actions analyze event

brysonda@myxo.css.msu.edu brysonda at myxo.css.msu.edu
Fri Jun 2 19:25:55 PDT 2006


Author: brysonda
Date: 2006-06-02 22:25:55 -0400 (Fri, 02 Jun 2006)
New Revision: 731

Modified:
   development/source/actions/PrintActions.cc
   development/source/analyze/cAnalyzeUtil.cc
   development/source/analyze/cAnalyzeUtil.h
   development/source/event/cEventManager.cc
Log:
Transition print_genetic_distance_data into the actions framework.

Modified: development/source/actions/PrintActions.cc
===================================================================
--- development/source/actions/PrintActions.cc	2006-06-02 21:06:48 UTC (rev 730)
+++ development/source/actions/PrintActions.cc	2006-06-03 02:25:55 UTC (rev 731)
@@ -14,11 +14,14 @@
 #include "cAnalyzeUtil.h"
 #include "cClassificationManager.h"
 #include "cCPUTestInfo.h"
+#include "cGenome.h"
+#include "cGenomeUtil.h"
 #include "cGenotype.h"
 #include "cHardwareBase.h"
 #include "cHardwareManager.h"
 #include "cInjectGenotype.h"
 #include "cInstSet.h"
+#include "cInstUtil.h"
 #include "cOrganism.h"
 #include "cPopulation.h"
 #include "cPopulationCell.h"
@@ -652,6 +655,82 @@
 };
 
 
+/*
+ This function goes through all genotypes currently present in the soup,
+ and writes into an output file the average Hamming distance between the
+ creatures in the population and a given reference genome.
+ 
+ Parameters
+   ref_creature_file (cString)
+     Filename for the reference genome, defaults to START_CREATURE
+   fname (cString)
+     Name of file to create, defaults to 'genetic_distance.dat'
+*/
+class cActionPrintGeneticDistanceData : public cAction
+{
+private:
+  cGenome m_reference;
+  cString m_filename;
+  
+public:
+  cActionPrintGeneticDistanceData(cWorld* world, const cString& args)
+    : cAction(world, args), m_filename("genetic_distance.dat")
+  {
+    cString creature_file;
+    cString largs(args);
+    
+    // Load the genome of the reference creature
+    if (largs.GetSize())
+      creature_file = largs.PopWord();
+    else
+      creature_file = m_world->GetConfig().START_CREATURE.Get();
+    m_reference = cInstUtil::LoadGenome(creature_file, world->GetHardwareManager().GetInstSet());
+    
+    if (largs.GetSize()) m_filename = largs.PopWord();
+  }
+  
+  const cString GetDescription() { return "PrintGeneticDistanceData [cString ref_creature_file='START_CREATURE'] [cString fname='genetic_distance.dat']"; }
+  
+  void Process(cAvidaContext& ctx)
+  {
+    double hamming_m1 = 0;
+    double hamming_m2 = 0;
+    int count = 0;
+    int dom_dist = 0;
+    
+    // get the info for the dominant genotype
+    cGenotype* cur_genotype = m_world->GetClassificationManager().GetBestGenotype();
+    cGenome genome = cur_genotype->GetGenome();
+    dom_dist = cGenomeUtil::FindHammingDistance(m_reference, genome);
+    hamming_m1 += dom_dist;
+    hamming_m2 += dom_dist*dom_dist;
+    count += cur_genotype->GetNumOrganisms();
+    // now cycle over the remaining genotypes
+    for (int i = 1; i < m_world->GetClassificationManager().GetGenotypeCount(); i++) {
+      cur_genotype = cur_genotype->GetNext();
+      cGenome genome = cur_genotype->GetGenome();
+      
+      int dist = cGenomeUtil::FindHammingDistance(m_reference, genome);
+      hamming_m1 += dist;
+      hamming_m2 += dist*dist;
+      count += cur_genotype->GetNumOrganisms();
+    }
+    
+    hamming_m1 /= static_cast<double>(count);
+    hamming_m2 /= static_cast<double>(count);
+
+    double hamming_best = cGenomeUtil::FindHammingDistance(m_reference, m_world->GetClassificationManager().GetBestGenotype()->GetGenome());
+
+    cDataFile& df = m_world->GetDataFile(m_filename);
+    df.Write(m_world->GetStats().GetUpdate(), "Update");
+    df.Write(hamming_m1, "Average Hamming Distance");
+    df.Write(sqrt((hamming_m2 - hamming_m1*hamming_m1) / static_cast<double>(count)), "Standard Error");
+    df.Write(hamming_best, "Best Genotype Hamming Distance");
+    df.Endl();
+  }
+};
+
+
 class cActionDumpMemory : public cAction
 {
 private:
@@ -714,6 +793,7 @@
   action_lib->Register<cActionPrintDominantGenotype>("PrintDominantGenotype");
   action_lib->Register<cActionPrintDominantParasiteGenotype>("PrintDominantParasiteGenotype");
   action_lib->Register<cActionPrintDetailedFitnessData>("PrintDetailedFitnessData");
+  action_lib->Register<cActionPrintGeneticDistanceData>("PrintGenericDistanceData");
   action_lib->Register<cActionPrintDebug>("PrintDebug");
 
   action_lib->Register<cActionPrintGenotypes>("PrintGenotypes");
@@ -754,6 +834,7 @@
   action_lib->Register<cActionPrintDominantGenotype>("print_dom");
   action_lib->Register<cActionPrintDominantParasiteGenotype>("print_dom_parasite");
   action_lib->Register<cActionPrintDetailedFitnessData>("print_detailed_fitness_data");
+  action_lib->Register<cActionPrintGeneticDistanceData>("print_genetic_distance_data");
   
   action_lib->Register<cActionPrintGenotypes>("print_genotypes");
   action_lib->Register<cActionDumpMemory>("dump_memory");

Modified: development/source/analyze/cAnalyzeUtil.cc
===================================================================
--- development/source/analyze/cAnalyzeUtil.cc	2006-06-02 21:06:48 UTC (rev 730)
+++ development/source/analyze/cAnalyzeUtil.cc	2006-06-03 02:25:55 UTC (rev 731)
@@ -406,59 +406,7 @@
 }
 
 
-/**
-* This function goes through all genotypes currently present in the soup,
- * and writes into an output file the average Hamming distance between the
- * creatures in the population and a given reference genome.
- *
- * @param fp The stream into which the data should be saved.
- * @param reference_genome The reference genome.
- * @param save_creatures A bool that indicates whether creatures should be
- * saved into the classmgr or not.
- **/
 
-void cAnalyzeUtil::PrintGeneticDistanceData(cWorld* world, ofstream& fp,
-                                            const char * creature_name)
-{
-  double hamming_m1 = 0;
-  double hamming_m2 = 0;
-  int count = 0;
-  int dom_dist = 0;
-  
-  // load the reference genome
-  cGenome reference_genome(cInstUtil::LoadGenome(creature_name, world->GetHardwareManager().GetInstSet()));
-  
-  // get the info for the dominant genotype
-  cGenotype * cur_genotype = world->GetClassificationManager().GetBestGenotype();
-  cGenome genome = cur_genotype->GetGenome();
-  dom_dist = cGenomeUtil::FindHammingDistance( reference_genome, genome );
-  hamming_m1 += dom_dist;
-  hamming_m2 += dom_dist*dom_dist;
-  count += cur_genotype->GetNumOrganisms();
-  // now cycle over the remaining genotypes
-  for (int i = 1; i < world->GetClassificationManager().GetGenotypeCount(); i++) {
-    cur_genotype = cur_genotype->GetNext();
-    cGenome genome = cur_genotype->GetGenome();
-    
-    int dist = cGenomeUtil::FindHammingDistance( reference_genome, genome );
-    hamming_m1 += dist;
-    hamming_m2 += dist*dist;
-    count += cur_genotype->GetNumOrganisms();
-  }
-  
-  hamming_m1 /= (double) count;
-  hamming_m2 /= (double) count;
-  
-  fp << world->GetStats().GetUpdate()          << " "  // 1 update
-    << hamming_m1 			     << " "  // ave. Hamming dist
-    << sqrt( ( hamming_m2 - hamming_m1*hamming_m1 ) / (double) count )
-    << " "  // std. error
-    << cGenomeUtil::FindHammingDistance( reference_genome,
-                                         world->GetClassificationManager().GetBestGenotype()->GetGenome() ) << " "
-    << endl;
-}
-
-
 /**
 * This function goes through all genotypes currently present in the soup,
  * and writes into an output file the names of the genotypes, the fitness

Modified: development/source/analyze/cAnalyzeUtil.h
===================================================================
--- development/source/analyze/cAnalyzeUtil.h	2006-06-02 21:06:48 UTC (rev 730)
+++ development/source/analyze/cAnalyzeUtil.h	2006-06-03 02:25:55 UTC (rev 731)
@@ -48,8 +48,6 @@
 				double sample_prob=1, bool landscape=false,
 				bool save_genotype=false);
 
-  static void PrintGeneticDistanceData(cWorld* world, std::ofstream& fp,
-				const char *creature_name );
   static void GeneticDistancePopDump(cWorld* world, std::ofstream& fp,
 		    const char * creature_name, bool save_creatures=false);
 

Modified: development/source/event/cEventManager.cc
===================================================================
--- development/source/event/cEventManager.cc	2006-06-02 21:06:48 UTC (rev 730)
+++ development/source/event/cEventManager.cc	2006-06-03 02:25:55 UTC (rev 731)
@@ -1028,36 +1028,6 @@
 };
 
 
-///// print_genetic_distance_data /////
-
-/**
-**/
-
-
-class cEvent_print_genetic_distance_data : public cEvent {
-private:
-  cString creature_name;
-  cString filename;
-public:
-    const cString GetName() const { return "print_genetic_distance_data"; }
-  const cString GetDescription() const { return "print_genetic_distance_data  [cString creature_name=\"\"] [cString filename=\"genetic_distance.dat\"]"; }
-  
-  void Configure(cWorld* world, const cString& in_args)
-  {
-    m_world = world;
-    m_args = in_args;
-    cString args(in_args);
-    if (args == "") creature_name=""; else creature_name=args.PopWord();
-    if (args == "") filename="genetic_distance.dat"; else filename=args.PopWord();
-  }
-  ///// print_genetic_distance_data /////
-  void Process(){
-    if( creature_name == "" || creature_name == "START_CREATURE" )
-      creature_name = m_world->GetConfig().START_CREATURE.Get();
-    cAnalyzeUtil::PrintGeneticDistanceData(m_world, m_world->GetDataFileOFStream(filename), creature_name);
-  }
-};
-
 ///// genetic_distance_pop_dump /////
 
 /**
@@ -2524,7 +2494,6 @@
   REGISTER(test_dom);
   REGISTER(analyze_population);
 
-  REGISTER(print_genetic_distance_data);
   REGISTER(genetic_distance_pop_dump);
   REGISTER(task_snapshot);
   REGISTER(print_viable_tasks_data);




More information about the Avida-cvs mailing list