[Avida-SVN] r2493 - development/source/analyze

cstrelioff at myxo.css.msu.edu cstrelioff at myxo.css.msu.edu
Sun Mar 23 11:47:51 PDT 2008


Author: cstrelioff
Date: 2008-03-23 14:47:51 -0400 (Sun, 23 Mar 2008)
New Revision: 2493

Modified:
   development/source/analyze/cAnalyze.cc
   development/source/analyze/cAnalyze.h
Log:
First commit for two site, all instructions fitness landscape code

Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc	2008-03-23 18:46:08 UTC (rev 2492)
+++ development/source/analyze/cAnalyze.cc	2008-03-23 18:47:51 UTC (rev 2493)
@@ -7049,6 +7049,136 @@
   delete testcpu;
 }
 
+void cAnalyze::AnalyzeFitnessLandscapeTwoSites(cString cur_string)
+{
+  cout << "Fitness for all instruction combinations at two sites..." << endl;
+  
+  /*
+   * Arguments:
+   * 1) directory (default: 'fitness_landscape_two_sites/'
+   * 2) useResources (default: 0 -- no)
+   * 3) batchFrequency (default: 1 -- all genotypes in batch)
+   * 
+   */
+  
+  // number of arguments provided  
+  int words = cur_string.CountNumWords();
+  if (m_world->GetVerbosity() >= VERBOSE_ON) {
+    cout << "  Number of arguments passed: " << words << endl;
+  }
+  
+  //
+  // argument 1 -- directory
+  //
+  cString dir = cur_string.PopWord();
+  cString defaultDirectory = "fitness_landscape_two_sites/";
+  cString directory = PopDirectory(dir, defaultDirectory);
+  if (m_world->GetVerbosity() >= VERBOSE_ON) {
+    cout << "  - Analysis results to directory: " << directory << endl;
+  }
+
+  //
+  // argument 2 -- use resources?
+  //
+  // Default for usage of resources is false
+  int useResources = 0;
+  if(words >= 2) {
+    useResources = cur_string.PopWord().AsInt();
+    // All non-zero values are considered false (Handled by testcpu->InitResources)
+  }
+  if (m_world->GetVerbosity() >= VERBOSE_ON) {
+    cout << "  - Use resorces set to: " << useResources << " (0=false, true other int)" << endl;
+  }
+  
+  //
+  // argument 3 -- batch frequncy
+  //   - default batchFrequency=1 (every organism analyzed)
+  //
+  int batchFrequency = 1;
+  if(words >= 3) {
+    batchFrequency = cur_string.PopWord().AsInt();
+    if(batchFrequency <= 0) {
+      batchFrequency = 1;
+    }
+  }
+  if (m_world->GetVerbosity() >= VERBOSE_ON) {
+    cout << "  - Batch frequency set to: " << batchFrequency << endl;
+  }
+
+  // test cpu
+  cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+  
+  // get current batch
+  tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
+  cAnalyzeGenotype * genotype = NULL;
+    
+  // analyze each genotype in the batch
+  while ((genotype = batch_it.Next()) != NULL) {
+    if (m_world->GetVerbosity() >= VERBOSE_ON) {
+      cout << "  Analyzing complexity for " << genotype->GetName() << endl;
+    }
+    
+    int updateBorn = -1;
+    updateBorn = genotype->GetUpdateBorn();
+    cCPUTestInfo test_info;
+    test_info.SetResourceOptions(useResources, &resources, updateBorn, m_resource_time_spent_offset);
+    
+    // Calculate the stats for the genotype we're working with ...
+    genotype->Recalculate(m_ctx, &test_info);
+    const int num_insts = inst_set.GetSize();
+    const int max_line = genotype->GetLength();
+    const cGenome & base_genome = genotype->GetGenome();
+    cGenome mod_genome(base_genome);
+
+    // run throught sites in genome
+    for (int site1 = 0; site1 < max_line; site1++) {
+      for (int site2 = site1+1; site2 < max_line; site2++) {
+        
+        // Construct filename for this site combination
+        cString fl_filename;
+        fl_filename.Set("%s%s_FitLand_sites-%d_and_%d.dat", static_cast<const char*>(directory), static_cast<const char*>(genotype->GetName()), site1, site2);
+        cDataFile & fit_land_fp = m_world->GetDataFile(fl_filename);
+        fit_land_fp.WriteComment( "Two-site fitness landscape, all possible instructions" );
+        fit_land_fp.WriteComment( cStringUtil::Stringf("Site 1: %d Site 2: %d", site1, site2) );
+        fit_land_fp.WriteComment( "Rows #- instruction, site 1" );
+        fit_land_fp.WriteComment( "Columns #- instruction, site 2" );
+        fit_land_fp.WriteTimeStamp();
+
+        // get current instructions at site 1 and site 2
+        int curr_inst1 = base_genome[site1].GetOp();
+        int curr_inst2 = base_genome[site2].GetOp();
+      
+        // get current fitness
+        //double curr_fitness = genotype->GetFitness();
+        
+        // run through all possible instruction combinations
+        // at two sites
+        for (int mod_inst1 = 0; mod_inst1 < num_insts; mod_inst1++) {
+          for (int mod_inst2 = 0; mod_inst2 < num_insts; mod_inst2++) {
+            // modify mod_genome at two sites
+            mod_genome[site1].SetOp(mod_inst1);
+            mod_genome[site2].SetOp(mod_inst2);
+            // analyze mod_genome
+            cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
+            test_genotype.Recalculate(m_ctx);
+            double mod_fitness = test_genotype.GetFitness();
+             
+            // write to file
+            fit_land_fp.Write(mod_fitness, cStringUtil::Stringf("Instruction, site 2: %d ", mod_inst2));
+          }
+          fit_land_fp.Endl();
+        }   
+        // Reset the mod_genome back to the original sequence.
+        mod_genome[site1].SetOp(curr_inst1);
+        mod_genome[site2].SetOp(curr_inst2);
+        
+        // close file
+        m_world->GetDataFileManager().Remove(fl_filename);
+      }
+    }
+  }  
+}
+
 void cAnalyze::AnalyzeComplexityTwoSites(cString cur_string)
 {
   cout << "Analyzing genome complexity (one and two sites)..." << endl;
@@ -9122,6 +9252,7 @@
   AddLibraryDef("AVERAGE_MODULARITY", &cAnalyze::CommandAverageModularity);
   AddLibraryDef("MAP_MUTATIONS", &cAnalyze::CommandMapMutations);
   AddLibraryDef("ANALYZE_COMPLEXITY", &cAnalyze::AnalyzeComplexity);
+  AddLibraryDef("ANALYZE_FITNESS_TWO_SITES", &cAnalyze::AnalyzeFitnessLandscapeTwoSites);
   AddLibraryDef("ANALYZE_COMPLEXITY_TWO_SITES", &cAnalyze::AnalyzeComplexityTwoSites);
   AddLibraryDef("ANALYZE_KNOCKOUTS", &cAnalyze::AnalyzeKnockouts);
   AddLibraryDef("ANALYZE_POP_COMPLEXITY", &cAnalyze::AnalyzePopComplexity);

Modified: development/source/analyze/cAnalyze.h
===================================================================
--- development/source/analyze/cAnalyze.h	2008-03-23 18:46:08 UTC (rev 2492)
+++ development/source/analyze/cAnalyze.h	2008-03-23 18:47:51 UTC (rev 2493)
@@ -295,6 +295,7 @@
   void AnalyzeBranching(cString cur_string);
   void AnalyzeMutationTraceback(cString cur_string);
   void AnalyzeComplexity(cString cur_string);
+  void AnalyzeFitnessLandscapeTwoSites(cString cur_string);
   void AnalyzeComplexityTwoSites(cString cur_string);
   void AnalyzeKnockouts(cString cur_string);
   void AnalyzePopComplexity(cString cur_string);




More information about the Avida-cvs mailing list