[Avida-SVN] r3112 - in development: Avida.xcodeproj source/analyze source/platform source/tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Mon Jan 12 23:04:58 PST 2009


Author: brysonda
Date: 2009-01-13 02:04:58 -0500 (Tue, 13 Jan 2009)
New Revision: 3112

Added:
   development/source/platform/tRLockPtr.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/analyze/cAnalyze.cc
   development/source/analyze/cAnalyze.h
   development/source/analyze/cAnalyzeGenotype.cc
   development/source/analyze/cAnalyzeGenotype.h
   development/source/analyze/cModularityAnalysis.cc
   development/source/analyze/cModularityAnalysis.h
   development/source/tools/tArrayMap.h
   development/source/tools/tDataEntry.h
   development/source/tools/tKVPair.h
Log:
Implement functional modularity analysis calculation and data reporting methods.

Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/Avida.xcodeproj/project.pbxproj	2009-01-13 07:04:58 UTC (rev 3112)
@@ -401,6 +401,7 @@
 		700D9C1A0F1A77EC002CC711 /* tKVPair.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tKVPair.h; sourceTree = "<group>"; };
 		700D9C440F1A8F34002CC711 /* cModularityAnalysis.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cModularityAnalysis.h; sourceTree = "<group>"; };
 		700D9C450F1A8F34002CC711 /* cModularityAnalysis.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cModularityAnalysis.cc; sourceTree = "<group>"; };
+		700D9CB20F1C55EB002CC711 /* tRLockPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tRLockPtr.h; sourceTree = "<group>"; };
 		700E28CF0859FFD700CF158A /* tObjectFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tObjectFactory.h; sourceTree = "<group>"; };
 		700E2B83085DE50C00CF158A /* avida-viewer */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "avida-viewer"; sourceTree = BUILT_PRODUCTS_DIR; };
 		7013845F09028B3E0087ED2E /* cAvidaConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cAvidaConfig.h; sourceTree = "<group>"; };
@@ -1321,6 +1322,7 @@
 				70436B260C36C64000A05ABA /* PlatformExpert.cc */,
 				70436B270C36C64000A05ABA /* PlatformExpert.h */,
 				70BB2ABF0EA4F896008269D2 /* tThreadSpecific.h */,
+				700D9CB20F1C55EB002CC711 /* tRLockPtr.h */,
 			);
 			path = platform;
 			sourceTree = "<group>";

Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/source/analyze/cAnalyze.cc	2009-01-13 07:04:58 UTC (rev 3112)
@@ -55,6 +55,7 @@
 #include "cInitFile.h"
 #include "cInstSet.h"
 #include "cLandscape.h"
+#include "cModularityAnalysis.h"
 #include "cPhenotype.h"
 #include "cPhenPlastGenotype.h"
 #include "cPlasticPhenotype.h"
@@ -70,6 +71,7 @@
 #include "cWorld.h"
 #include "cWorldDriver.h"
 #include "tAnalyzeJob.h"
+#include "tAnalyzeJobBatch.h"
 #include "tDataCommandManager.h"
 #include "tDataEntry.h"
 #include "tDataEntryCommand.h"
@@ -4922,6 +4924,23 @@
   }
 }
 
+void cAnalyze::CommandCalcFunctionalModularity(cString cur_string)
+{
+  cout << "Calculating Functional Modularity..." << endl;
+
+  tList<cModularityAnalysis> mod_list;
+  tAnalyzeJobBatch<cModularityAnalysis> jobbatch(m_jobqueue);
+  tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
+  for (cAnalyzeGenotype* cur_genotype = batch_it.Next(); cur_genotype; cur_genotype = batch_it.Next()) {
+    cModularityAnalysis* mod = new cModularityAnalysis(cur_genotype);
+    mod_list.Push(mod);
+    jobbatch.AddJob(mod, &cModularityAnalysis::CalcFunctionalModularity);
+  }
+  jobbatch.RunBatch();
+  cModularityAnalysis* mod = NULL;
+  while ((mod = mod_list.Pop())) delete mod;
+}
+
 void cAnalyze::CommandAverageModularity(cString cur_string)
 {
   cout << "Average Modularity calculations" << endl;
@@ -9244,6 +9263,7 @@
   AddLibraryDef("MAP", &cAnalyze::CommandMapTasks);  // Deprecated...
   AddLibraryDef("MAP_TASKS", &cAnalyze::CommandMapTasks);
   AddLibraryDef("AVERAGE_MODULARITY", &cAnalyze::CommandAverageModularity);
+  AddLibraryDef("CALC_FUNCTIONAL_MODULARITY", &cAnalyze::CommandCalcFunctionalModularity);
   AddLibraryDef("ANALYZE_REDUNDANCY_BY_INST_FAILURE", &cAnalyze::CommandAnalyzeRedundancyByInstFailure);
   AddLibraryDef("MAP_MUTATIONS", &cAnalyze::CommandMapMutations);
   AddLibraryDef("ANALYZE_COMPLEXITY", &cAnalyze::AnalyzeComplexity);

Modified: development/source/analyze/cAnalyze.h
===================================================================
--- development/source/analyze/cAnalyze.h	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/source/analyze/cAnalyze.h	2009-01-13 07:04:58 UTC (rev 3112)
@@ -260,6 +260,7 @@
   // Individual Organism Analysis...
   void CommandFitnessMatrix(cString cur_string);
   void CommandMapTasks(cString cur_string);
+  void CommandCalcFunctionalModularity(cString cur_string);
   void CommandAverageModularity(cString cur_string);
   void CommandAnalyzeModularity(cString cur_string);
   void CommandAnalyzeRedundancyByInstFailure(cString cur_string);

Modified: development/source/analyze/cAnalyzeGenotype.cc
===================================================================
--- development/source/analyze/cAnalyzeGenotype.cc	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/source/analyze/cAnalyzeGenotype.cc	2009-01-13 07:04:58 UTC (rev 3112)
@@ -347,7 +347,19 @@
 }
 
 
+cAnalyzeGenotype::sGenotypeDatastore::~sGenotypeDatastore()
+{
+  for (tArrayMap<int, cGenotypeData*>::iterator it = dmap.begin(); it != dmap.end(); it++) delete it->Value();
+}
 
+void cAnalyzeGenotype::SetGenotypeData(int data_id, cGenotypeData* data)
+{
+  m_data->rwlock.WriteLock();
+  m_data->dmap.Set(data_id, data);
+  m_data->rwlock.WriteUnlock();
+}
+
+
 int cAnalyzeGenotype::CalcMaxGestation() const
 {
   return m_world->GetConfig().TEST_CPU_TIME_MOD.Get() * genome.GetSize();

Modified: development/source/analyze/cAnalyzeGenotype.h
===================================================================
--- development/source/analyze/cAnalyzeGenotype.h	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/source/analyze/cAnalyzeGenotype.h	2009-01-13 07:04:58 UTC (rev 3112)
@@ -103,6 +103,7 @@
 
 class cAnalyzeGenotype
 {
+  friend class ReadToken;
 private:
   cWorld* m_world;
   cGenome genome;            // Full Genome
@@ -112,12 +113,12 @@
   struct sGenotypeDatastore : public cRCObject
   {
     cRWLock rwlock;
-    tArrayMap<int, cGenotypeData*> dmap;
+    mutable tArrayMap<int, cGenotypeData*> dmap;
     
     sGenotypeDatastore() { ; }
     sGenotypeDatastore(const sGenotypeDatastore& ds) : cRCObject(ds) { ; } // Note that data objects are not copied right now
     
-    ~sGenotypeDatastore() { ; }
+    ~sGenotypeDatastore();
   };
   tRCPtr<sGenotypeDatastore> m_data;
   
@@ -255,7 +256,14 @@
   
   static void Initialize();
   static tDataCommandManager<cAnalyzeGenotype>& GetDataCommandManager();
+  
+  class ReadToken;
+  ReadToken* GetReadToken() const { m_data->rwlock.ReadLock(); return new ReadToken(this); }
 
+  void SetGenotypeData(int data_id, cGenotypeData* data);
+  cGenotypeData* GetGenotypeData(ReadToken* tk, int data_id) const { tk->Validate(this); return m_data->dmap.Get(data_id, NULL); }
+  
+
   void Recalculate(cAvidaContext& ctx, cCPUTestInfo* test_info = NULL, cAnalyzeGenotype* parent_genotype = NULL, int num_trials = 1);
   void PrintTasks(std::ofstream& fp, int min_task = 0, int max_task = -1);
   void PrintTasksQuality(std::ofstream& fp, int min_task = 0, int max_task = -1);
@@ -305,6 +313,8 @@
   void SetNULL(cString dummy) { (void) dummy; }
 
   // Accessors...
+  cWorld* GetWorld() { return m_world; }
+  
   const cGenome & GetGenome() const { return genome; }
   const cString& GetName() const { return name; }
   const cInstSet& GetInstructionSet() const { return m_inst_set; }
@@ -515,6 +525,25 @@
       return false;
     }
   }
+  
+  
+  class ReadToken
+  {
+    friend class cAnalyzeGenotype;
+  private:
+    const cAnalyzeGenotype* m_ptr;
+
+    ReadToken(const ReadToken&); // @not_implemented
+    ReadToken& operator=(const ReadToken&); // @not_implemented
+
+    inline ReadToken(const cAnalyzeGenotype* ptr) : m_ptr(ptr) { ; }
+    
+    inline void Validate(const cAnalyzeGenotype* ptr) { assert(ptr == m_ptr); }
+    
+  public:
+    ~ReadToken() { m_ptr->m_data->rwlock.ReadUnlock(); }
+  };
+    
 };
 
 #endif

Modified: development/source/analyze/cModularityAnalysis.cc
===================================================================
--- development/source/analyze/cModularityAnalysis.cc	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/source/analyze/cModularityAnalysis.cc	2009-01-13 07:04:58 UTC (rev 3112)
@@ -25,22 +25,316 @@
 #include "cModularityAnalysis.h"
 
 #include "cAnalyzeGenotype.h"
+#include "cOrganism.h"
+#include "cPhenotype.h"
 #include "tDataCommandManager.h"
 #include "tDataEntry.h"
 
+static const int MD_ID = 1;
 
 void cModularityAnalysis::Initialize()
 {
-//  tDataCommandManager<cAnalyzeGenotype>& dcm = cAnalyzeGenotype::GetDataCommandManager();
-  
+  tDataCommandManager<cAnalyzeGenotype>& dcm = cAnalyzeGenotype::GetDataCommandManager();
+  // A basic macro to link a keyword to a description and Get and Set methods in cAnalyzeGenotype.
+#define ADD_GDATA(KEYWORD, DESC, GET, COMP) \
+  dcm.Add(KEYWORD, new tDataEntryProxy<cAnalyzeGenotype, cFlexVar ()> \
+    (KEYWORD, DESC, &cModularityAnalysis::GET, COMP));
+#define ADD_GDATA_IDX(KEYWORD, DESC, GET, COMP) \
+  dcm.Add(KEYWORD, new tDataEntryProxy<cAnalyzeGenotype, cFlexVar (int)> \
+    (KEYWORD, &cModularityAnalysis::DESC, &cModularityAnalysis::GET, COMP));
+
+  ADD_GDATA("tasks_done", "Number of Tasks Performed", GetTasksDoneFor, 2);
+  ADD_GDATA("insts_tasks", "Number of Instructions Involved in Tasks", GetInstsInvolvedInTasksFor, 1);
+  ADD_GDATA("tasks_prop", "Proportion of Sites in Tasks", GetTaskProportionFor, 4);
+  ADD_GDATA("ave_tasks_per_site", "Average Number of Tasks Per Site", GetAveTasksPerSiteFor, 6);
+  ADD_GDATA("ave_sites_per_task", "Average Number of Sites Per Task", GetAveSitesPerTaskFor, 6);
+  ADD_GDATA("tasks_prop", "Average Proportion of the Non-overlapping Region of a Task", GetTaskProportionFor, 4);
+  ADD_GDATA_IDX("sites_per_task", DescSitesPerTask, GetSitesPerTaskFor, 1);
+  ADD_GDATA_IDX("sites_inv_x_tasks", DescSitesInvolvedInXTasks, GetSitesInvolvedInXTasksFor, 1);
+  ADD_GDATA_IDX("task_length", DescTaskLength, GetTaskLengthFor, 4);
+  ADD_GDATA_IDX("ave_task_position", DescAveTaskPosition, GetAveTaskPositionFor, 4);  
 }
 
 void cModularityAnalysis::CalcFunctionalModularity(cAvidaContext& ctx)
 {
+  cTestCPU* testcpu = m_genotype->GetWorld()->GetHardwareManager().CreateTestCPU();
+  cCPUTestInfo test_info;
+
+  const cGenome& base_genome = m_genotype->GetGenome();
+
+  // Calculate the stats for the genotype we're working with...
+  testcpu->TestGenome(ctx, test_info, base_genome);  
+  double base_fitness = test_info.GetColonyFitness();
   
+  // Check if the organism does any tasks
+  bool does_tasks = false;
+  const tArray<int>& base_tasks = test_info.GetColonyOrganism()->GetPhenotype().GetLastTaskCount();
+  const int num_tasks = base_tasks.GetSize();
+  for (int i = 0; i < num_tasks; i++) {
+    if (base_tasks[i] > 0) {
+      does_tasks = true;
+      break;
+    }
+  }
+  
+  // Don't calculate the modularity if the organism doesn't reproduce. i.e. if the fitness is 0
+  if (base_fitness > 0.0 && does_tasks) {
+    // Set up the instruction set for mapping
+    cInstSet map_inst_set(m_genotype->GetInstructionSet());
+    const cInstruction null_inst = map_inst_set.ActivateNullInst();
+    test_info.SetInstSet(&map_inst_set);
+
+    // Genome for testing
+    const int max_line = base_genome.GetSize();
+    cGenome mod_genome(base_genome);
+    
+    // Create and initialize the modularity matrix
+    tMatrix<int> mod_matrix(num_tasks, max_line);
+    mod_matrix.SetAll(0);
+    
+    tArray<int> site_num_tasks(max_line, 0);  // number of tasks instruction is used in
+    tArray<int> sites_per_task(num_tasks, 0); // number of sites involved in each task
+    
+    // Loop through all the lines of code, testing the removal of each.
+    for (int line_num = 0; line_num < max_line; line_num++) {
+      int cur_inst = base_genome[line_num].GetOp();
+      
+      mod_genome[line_num] = null_inst;
+      
+      // Run the modified genome through the Test CPU
+      testcpu->TestGenome(ctx, test_info, mod_genome);
+      
+      if (test_info.GetColonyFitness() > 0.0) {
+        const tArray<int>& test_tasks = test_info.GetColonyOrganism()->GetPhenotype().GetLastTaskCount();
+        
+        for (int cur_task = 0; cur_task < num_tasks; cur_task++) {
+          // This is done so that under 'binary' option it marks
+          // the task as being influenced by the mutation iff
+          // it is completely knocked out, not just decreased
+          if (test_tasks[cur_task] == 0) {
+            // If knocking out an instruction stops the expression of a particular task, mark that in the modularity matrix
+            // and add it to two counts
+            mod_matrix(cur_task, line_num) = 1;
+            sites_per_task[cur_task]++;
+            site_num_tasks[line_num]++;
+          }
+        }
+      }
+          
+      // Reset the mod_genome back to the original sequence.
+      mod_genome[line_num].SetOp(cur_inst);
+    }
+    
+    tArray<int> sites_inv_x_tasks(num_tasks + 1, 0);  // # of inst's involved in 0,1,2,3... tasks    
+    tArray<double> ave_task_position(num_tasks, 0.0); // mean positions of the tasks in the genome
+    tArray<int> task_length(num_tasks, 0);  // distance between first and last inst involved in a task
+ 
+    int total_task = 0;           // total number of tasks done
+    int total_inst = 0;           // total number of instructions involved in tasks
+    int total_all = 0;            // sum of mod_matrix
+    double sum_task_overlap = 0;  // sum of task overlap for for this genome
+
+
+    // Calculate instruction and task totals
+    for (int i = 0; i < num_tasks; i++) {
+      total_all += sites_per_task[i];
+      if (sites_per_task[i]) total_task++;
+    }
+    for (int i = 0; i < max_line; i++) {
+      sites_inv_x_tasks[site_num_tasks[i]]++;
+      if (site_num_tasks[i] != 0) total_inst++;
+    }
+    
+    
+    // Calculate average task overlap
+    // first construct num_task x num_task matrix with number of sites overlapping
+    tMatrix<int> task_overlap(num_tasks, num_tasks);
+    task_overlap.SetAll(0);
+
+    for (int i = 0; i < max_line; i++) {
+      for (int j = 0; j < num_tasks; j++) {
+        for (int k = j; k < num_tasks; k++) {
+          if (mod_matrix(j, i) > 0 && mod_matrix(k, i) > 0) {
+            task_overlap(j, k)++;
+            if (j != k) task_overlap(k, j)++;
+          }               
+        }
+      }
+    }
+    
+    // go though the task_overlap matrix, add and average everything up. 
+    if (total_task > 1) {
+      for (int i = 0; i < num_tasks; i++) {
+        if (task_overlap(i, i)) {
+          int overlap_per_task = 0;
+          for (int j = 0; j < num_tasks; j++) if (i != j) overlap_per_task += task_overlap(i,j);
+          sum_task_overlap += (double)overlap_per_task / (task_overlap(i, i) * (total_task - 1));
+        }
+      }
+    }
+    
+    
+    // Calculate the first/last position of a task, the task "spread"
+    // starting from the top look for the fist command that matters for a task
+    for (int i = 0; i < num_tasks; i++) { 
+      for (int j = 0; j < max_line; j++) {
+        if (mod_matrix(i, j) > 0 && task_length[i] == 0) {
+          task_length[i] = j;
+          break;
+        }
+      }
+    }
+    
+    // starting from the bottom look for the last command that matters for a task
+    // and subtract it from the first to get the task length
+    // add one in order to account for both the beginning and the end instruction
+    for (int i = 0; i < num_tasks; i++) {
+      for (int j = max_line - 1; j >= 0; j--) {
+        if (mod_matrix(i, j) > 0) {
+          task_length[i] = j - task_length[i] + 1;
+          break;
+        }
+      }
+    }
+    
+    
+    // Calculate mean positions of the tasks
+    tArray<int> task_position(num_tasks);
+    for (int i = 0; i < num_tasks; i++) {
+      task_position[i] = 0;
+      for (int j = 0; j < max_line; j++) if (mod_matrix(i,j) > 0) task_position[i] += j;
+    }
+    for (int i = 0; i < num_tasks; i++) ave_task_position[i] = (double)task_position[i] / sites_per_task[i];
+    
+    
+    cModularityData* mod_data = new cModularityData;
+    mod_data->tasks_done = total_task;
+    mod_data->insts_tasks = total_inst;
+    mod_data->tasks_prop = (double)total_inst / max_line;
+    mod_data->ave_tasks_per_site = (total_inst) ? (double)total_all / total_inst : 0.0;
+    mod_data->ave_sites_per_task = (total_task) ? (double)total_all / total_task : 0.0;
+    mod_data->ave_prop_nonoverlap = (total_task) ? sum_task_overlap / total_task : 0.0;
+    mod_data->sites_per_task = sites_per_task;
+    mod_data->sites_inv_x_tasks = sites_inv_x_tasks;
+    mod_data->task_length = task_length;
+    mod_data->ave_task_position = ave_task_position;
+    m_genotype->SetGenotypeData(MD_ID, mod_data);
+  }
 }
 
+#ifdef DEBUG
+#define GET_MD() cModularityData* data = dynamic_cast<cModularityData*>(genotype->GetGenotypeData(tok, MD_ID));
+#else
+#define GET_MD() cModularityData* data = static_cast<cModularityData*>(genotype->GetGenotypeData(tok, MD_ID));
+#endif
 
+cFlexVar cModularityAnalysis::GetTasksDoneFor(const cAnalyzeGenotype* genotype)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data) return cFlexVar(data->tasks_done);
+  return cFlexVar(0);
+}
+
+cFlexVar cModularityAnalysis::GetInstsInvolvedInTasksFor(const cAnalyzeGenotype* genotype)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data) return cFlexVar(data->insts_tasks);
+  return cFlexVar(0);
+}
+
+cFlexVar cModularityAnalysis::GetTaskProportionFor(const cAnalyzeGenotype* genotype)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data) return cFlexVar(data->tasks_prop);
+  return cFlexVar(0.0);
+}
+
+cFlexVar cModularityAnalysis::GetAveTasksPerSiteFor(const cAnalyzeGenotype* genotype)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data) return cFlexVar(data->ave_tasks_per_site);
+  return cFlexVar(0.0);
+}
+
+cFlexVar cModularityAnalysis::GetAveSitesPerTaskFor(const cAnalyzeGenotype* genotype)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data) return cFlexVar(data->ave_sites_per_task);
+  return cFlexVar(0.0);
+}
+
+cFlexVar cModularityAnalysis::GetPropNonoverlapFor(const cAnalyzeGenotype* genotype)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data) return cFlexVar(data->ave_prop_nonoverlap);
+  return cFlexVar(0.0);
+}
+
+
+cFlexVar cModularityAnalysis::GetSitesPerTaskFor(const cAnalyzeGenotype* genotype, int idx)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data && idx >= 0 && idx < data->sites_per_task.GetSize()) return cFlexVar(data->sites_per_task[idx]);
+  return cFlexVar(0);
+}
+
+cString cModularityAnalysis::DescSitesPerTask(const cAnalyzeGenotype* genotype, int idx)
+{
+  return cStringUtil::Stringf("Number of Sites Per Task %d", idx);
+}
+
+
+cFlexVar cModularityAnalysis::GetSitesInvolvedInXTasksFor(const cAnalyzeGenotype* genotype, int idx)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data && idx >= 0 && idx < data->sites_per_task.GetSize()) return cFlexVar(data->sites_inv_x_tasks[idx]);
+  return cFlexVar(0);
+}
+
+cString cModularityAnalysis::DescSitesInvolvedInXTasks(const cAnalyzeGenotype* genotype, int idx)
+{
+  return cStringUtil::Stringf("Number of Sites Involved in %d Tasks", idx);
+}
+
+
+cFlexVar cModularityAnalysis::GetTaskLengthFor(const cAnalyzeGenotype* genotype, int idx)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data && idx >= 0 && idx < data->sites_per_task.GetSize()) return cFlexVar(data->task_length[idx]);
+  return cFlexVar(0);
+}
+
+cString cModularityAnalysis::DescTaskLength(const cAnalyzeGenotype* genotype, int idx)
+{
+  return cStringUtil::Stringf("Task %d Length", idx);
+}
+
+
+cFlexVar cModularityAnalysis::GetAveTaskPositionFor(const cAnalyzeGenotype* genotype, int idx)
+{
+  cAnalyzeGenotype::ReadToken* tok = genotype->GetReadToken();
+  GET_MD();
+  if (data && idx >= 0 && idx < data->sites_per_task.GetSize()) return cFlexVar(data->ave_task_position[idx]);
+  return cFlexVar(0.0);
+}
+
+cString cModularityAnalysis::DescAveTaskPosition(const cAnalyzeGenotype* genotype, int idx)
+{
+  return cStringUtil::Stringf("Task %d Position", idx);
+}
+
+#undef GET_MD
+
+
 cModularityAnalysis::cModularityData::cModularityData()
 : tasks_done(0), insts_tasks(0), tasks_prop(0.0), ave_tasks_per_site(0.0), ave_sites_per_task(0.0), ave_prop_nonoverlap(0.0)
 {

Modified: development/source/analyze/cModularityAnalysis.h
===================================================================
--- development/source/analyze/cModularityAnalysis.h	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/source/analyze/cModularityAnalysis.h	2009-01-13 07:04:58 UTC (rev 3112)
@@ -34,6 +34,8 @@
 
 class cAvidaContext;
 class cAnalyzeGenotype;
+class cFlexVar;
+class cString;
 
 
 class cModularityAnalysis
@@ -48,21 +50,37 @@
   
   void CalcFunctionalModularity(cAvidaContext& ctx);
   
+  static cFlexVar GetTasksDoneFor(const cAnalyzeGenotype* genotype);
+  static cFlexVar GetInstsInvolvedInTasksFor(const cAnalyzeGenotype* genotype);
+  static cFlexVar GetTaskProportionFor(const cAnalyzeGenotype* genotype);
+  static cFlexVar GetAveTasksPerSiteFor(const cAnalyzeGenotype* genotype);
+  static cFlexVar GetAveSitesPerTaskFor(const cAnalyzeGenotype* genotype);
+  static cFlexVar GetPropNonoverlapFor(const cAnalyzeGenotype* genotype);
   
+  static cFlexVar GetSitesPerTaskFor(const cAnalyzeGenotype* genotype, int idx);
+  static cString DescSitesPerTask(const cAnalyzeGenotype* genotype, int idx);  
+  static cFlexVar GetSitesInvolvedInXTasksFor(const cAnalyzeGenotype* genotype, int idx);
+  static cString DescSitesInvolvedInXTasks(const cAnalyzeGenotype* genotype, int idx);
+  static cFlexVar GetTaskLengthFor(const cAnalyzeGenotype* genotype, int idx);
+  static cString DescTaskLength(const cAnalyzeGenotype* genotype, int idx);
+  static cFlexVar GetAveTaskPositionFor(const cAnalyzeGenotype* genotype, int idx);
+  static cString DescAveTaskPosition(const cAnalyzeGenotype* genotype, int idx);
+
+  
 private:
   class cModularityData : public cGenotypeData
   {
   public:
-    int tasks_done;
-    int insts_tasks;
-    double tasks_prop;
-    double ave_tasks_per_site;
-    double ave_sites_per_task;
-    double ave_prop_nonoverlap;
-    tArray<double> ave_sites_per_task_indv;
-    tArray<double> std_sites_per_task_indv;
-    tArray<double> ave_sites_inv_per_task;
-    tArray<double> ave_task_length;
+    int tasks_done;                           // number of tasks performed by this genotype
+    int insts_tasks;                          // number of instructions involved in task performance
+    double tasks_prop;                        // proportion of sites used in tasks
+    double ave_tasks_per_site;                // average tasks per site
+    double ave_sites_per_task;                // average sites per task
+    double ave_prop_nonoverlap;               // average proportion of nonoverlap
+    tArray<int> sites_per_task;               // number sites used for each task
+    tArray<int> sites_inv_x_tasks;            // Number of sites involved in 0, 1, 2, 3... tasks
+    tArray<int> task_length;                  // Length of each task from first to last instruction
+    tArray<double> ave_task_position;         // Average task position
     
     cModularityData();
   };

Added: development/source/platform/tRLockPtr.h
===================================================================
--- development/source/platform/tRLockPtr.h	                        (rev 0)
+++ development/source/platform/tRLockPtr.h	2009-01-13 07:04:58 UTC (rev 3112)
@@ -0,0 +1,72 @@
+/*
+ *  tRLockPtr.h
+ *  Avida
+ *
+ *  Created by David on 1/12/09.
+ *  Copyright 2009 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef tRLockPtr_h
+#define tRLockPtr_h
+
+#ifndef cRWLock_h
+#include "cRWLock.h"
+#endif
+
+
+template<class T> class tRLockPtr
+{
+private:
+  T* m_ptr;
+  cRWLock* m_rwlock;
+  
+  
+public:
+  tRLockPtr(T* ptr, cRWLock* rwlock) : m_ptr(ptr), m_rwlock(rwlock) { m_rwlock->ReadLock(); }
+  tRLockPtr(const tRLockPtr& p) : m_ptr(p.m_ptr), m_rwlock(p.m_rwlock) { m_rwlock->ReadLock(); }
+  ~tRLockPtr() { m_rwlock->Unlock(); }
+  
+  tRLockPtr& operator=(const tRLockPtr& rhs);
+
+  inline bool operator!() const { return !m_ptr; }
+  inline operator bool() const { return !operator!(); }
+  
+  inline T* operator->() const { return m_ptr; }
+  inline T& operator*() const { return *m_ptr; }
+  
+  template<class S> inline S* operator->() const { return m_ptr; }
+  template<class S> inline S& operator->() const { return *m_ptr; }
+  
+  template<class S> operator tRLockPtr<S>() { return tRLockPtr<S>(m_ptr, m_rwlock); }
+};
+
+template<class T> tRLockPtr<T>& tRLockPtr<T>::operator=(const tRLockPtr& rhs)
+{
+  if (m_ptr != rhs.m_ptr) {
+    m_rwlock->Unlock();
+    m_ptr = rhs.m_ptr;
+    m_rwlock = rhs.m_rwlock;
+    m_rwlock->ReadLock();
+  }
+  
+  return *this;
+}
+
+
+#endif

Modified: development/source/tools/tArrayMap.h
===================================================================
--- development/source/tools/tArrayMap.h	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/source/tools/tArrayMap.h	2009-01-13 07:04:58 UTC (rev 3112)
@@ -63,7 +63,7 @@
     return false;
   }
   
-  const ValueType& Get(const KeyType& key, const ValueType& default_value) const
+  const ValueType& Get(const KeyType& key, const ValueType& default_value)
   {
     for (int i = 0; i < m_map.GetSize(); i++) {
       if (m_map[i].Key() == key) return m_map[i].Value();
@@ -101,10 +101,10 @@
   typedef typename tArray<tKVPair<KeyType, ValueType> >::iterator iterator;
   typedef typename tArray<tKVPair<KeyType, ValueType> >::const_iterator const_iterator;
   
-  inline iterator begin() { m_map.begin(); }
-  inline iterator end() { m_map.end(); }
-  inline const_iterator begin() const { m_map.begin(); }
-  inline const_iterator end() const { m_map.end(); }
+  inline iterator begin() { return m_map.begin(); }
+  inline iterator end() { return m_map.end(); }
+  inline const_iterator begin() const { return m_map.begin(); }
+  inline const_iterator end() const { return m_map.end(); }
 };
 
 #endif

Modified: development/source/tools/tDataEntry.h
===================================================================
--- development/source/tools/tDataEntry.h	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/source/tools/tDataEntry.h	2009-01-13 07:04:58 UTC (rev 3112)
@@ -162,13 +162,13 @@
 class tDataEntryProxy<TargetType, cFlexVar ()> : public tDataEntry<TargetType>
 {
 protected:
-  cFlexVar (*DataRetrieval)(TargetType*);
+  cFlexVar (*DataRetrieval)(const TargetType*);
   
 public:
-  tDataEntryProxy(const cString& name,
-                  cFlexVar (*_funR)(TargetType*),
+  tDataEntryProxy(const cString& name, const cString& desc,
+                  cFlexVar (*_funR)(const TargetType*),
                   int compare_type = 0, const cString& null = "0", const cString& html_cell = "align=center")
-  : tDataEntry<TargetType>(name, name, compare_type, null, html_cell), DataRetrieval(_funR) { ; }
+  : tDataEntry<TargetType>(name, desc, compare_type, null, html_cell), DataRetrieval(_funR) { ; }
   
   cFlexVar Get(const TargetType* target, const cFlexVar& idx, const cStringList& args) const
   {
@@ -181,13 +181,13 @@
 class tDataEntryProxy<TargetType, cFlexVar (IdxType)> : public tDataEntry<TargetType>
 {
 protected:
-  cFlexVar (*DataRetrieval)(TargetType*, IdxType);
-  cString (*DescFunction)(TargetType*, IdxType);
+  cFlexVar (*DataRetrieval)(const TargetType*, IdxType);
+  cString (*DescFunction)(const TargetType*, IdxType);
   
 public:
   tDataEntryProxy(const cString& name,
-                  cString (*_funD)(TargetType*, IdxType),
-                  cFlexVar (*_funR)(TargetType*, IdxType),
+                  cString (*_funD)(const TargetType*, IdxType),
+                  cFlexVar (*_funR)(const TargetType*, IdxType),
                   int compare_type = 0, const cString& null = "0", const cString& html_cell = "align=center")
   : tDataEntry<TargetType>(name, name, compare_type, null, html_cell), DataRetrieval(_funR), DescFunction(_funD) { ; }
   
@@ -207,13 +207,13 @@
 class tDataEntryProxy<TargetType, cFlexVar (IdxType, const cStringList&)> : public tDataEntry<TargetType>
 {
 protected:
-  cFlexVar (*DataRetrieval)(TargetType*, IdxType, const cStringList&);
-  cString (*DescFunction)(TargetType*, IdxType);
+  cFlexVar (*DataRetrieval)(const TargetType*, IdxType, const cStringList&);
+  cString (*DescFunction)(const TargetType*, IdxType);
   
 public:
   tDataEntryProxy(const cString& name,
-                  cString (*_funD)(TargetType*, IdxType),
-                  cFlexVar (*_funR)(TargetType*, IdxType, const cStringList&),
+                  cString (*_funD)(const TargetType*, IdxType),
+                  cFlexVar (*_funR)(const TargetType*, IdxType, const cStringList&),
                   int compare_type = 0, const cString& null = "0", const cString& html_cell = "align=center")
   : tDataEntry<TargetType>(name, name, compare_type, null, html_cell), DataRetrieval(_funR), DescFunction(_funD) { ; }
   

Modified: development/source/tools/tKVPair.h
===================================================================
--- development/source/tools/tKVPair.h	2009-01-12 20:01:52 UTC (rev 3111)
+++ development/source/tools/tKVPair.h	2009-01-13 07:04:58 UTC (rev 3112)
@@ -35,7 +35,7 @@
   inline tKVPair(const K& key, const V& value) : m_key(key), m_value(value) { ; }
   inline tKVPair(const tKVPair& p) : m_key(p.m_key), m_value(p.m_value) { ; }
   
-  inline tKVPair& operator=(const tKVPair& rhs) { m_key = rhs.m_key; m_value = rhs.m_value; }
+  inline tKVPair& operator=(const tKVPair& rhs) { m_key = rhs.m_key; m_value = rhs.m_value; return *this; }
   
   inline K& Key() { return m_key; }
   inline const K& Key() const { return m_key; }




More information about the Avida-cvs mailing list