[Avida-SVN] r2174 - in development/source: analyze cpu main

barrick at myxo.css.msu.edu barrick at myxo.css.msu.edu
Thu Nov 1 19:34:02 PDT 2007


Author: barrick
Date: 2007-11-01 22:34:02 -0400 (Thu, 01 Nov 2007)
New Revision: 2174

Modified:
   development/source/analyze/cAnalyze.cc
   development/source/analyze/cAnalyze.h
   development/source/analyze/cAnalyzeGenotype.cc
   development/source/analyze/cAnalyzeGenotype.h
   development/source/cpu/cCPUTestInfo.cc
   development/source/cpu/cCPUTestInfo.h
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareManager.h
   development/source/cpu/cTestCPU.cc
   development/source/cpu/cTestCPU.h
   development/source/main/cPhenPlastGenotype.cc
   development/source/main/cPhenPlastGenotype.h
Log:
Fixed Resources in Test CPU

Moved resource handling to cCPUTestInfo. Removed "global" m_test_cpu object from cAnalyze. Now: Create a new cTestCPU object every time that you need to test an org and initialize it with the test info object.


Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/analyze/cAnalyze.cc	2007-11-02 02:34:02 UTC (rev 2174)
@@ -106,25 +106,6 @@
   for (int i = 0; i < MAX_BATCHES; i++) {
     batch[i].Name().Set("Batch%d", i);
   }
-  
-  // Initialize the time oriented resource list to be just the initial
-  // concentrations of the resources in the environment.  This will only
-  // be changed if LOAD_RESOURCES analyze command is called.  If there are
-  // no resources in the environment or there is no environment, the list
-  // is empty then the all resources will default to 0.0
-  const cResourceLib &resource_lib = m_world->GetEnvironment().GetResourceLib();
-  if(resource_lib.GetSize() > 0) {
-    vector<double> r;
-    for(int i=0; i<resource_lib.GetSize(); i++) {
-      cResource *resource = resource_lib.GetResource(i);
-      assert(resource);
-      r.push_back(resource->GetInitial());
-    }
-    resources.push_back(make_pair(0, r));
-  }
-  m_resource_time_spent_offset = 0;
-  
-  m_testcpu = m_world->GetHardwareManager().CreateTestCPU();
 }
 
 
@@ -503,7 +484,7 @@
   double entropy = 0.0;
   
   // If the fitness is 0, the entropy is the length of genotype ...
-  genotype->Recalculate(m_ctx, m_testcpu);
+  genotype->Recalculate(m_ctx);
   if (genotype->GetFitness() == 0) {
     return genotype->GetLength();
   }
@@ -525,7 +506,7 @@
     for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
       mod_genome[line_no].SetOp(mod_inst);
       cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-      test_genotype.Recalculate(m_ctx, m_testcpu);
+      test_genotype.Recalculate(m_ctx);
       // Ajust fitness ...
       if (test_genotype.GetFitness() <= base_fitness) {
         test_fitness[mod_inst] = test_genotype.GetFitness();
@@ -583,7 +564,7 @@
   
   double entropy = 0.0;
   
-  genotype->Recalculate(m_ctx, m_testcpu);
+  genotype->Recalculate(m_ctx);
   
   // Calculate the stats for the genotype we're working with ...
   const int num_insts = inst_set.GetSize();
@@ -626,7 +607,7 @@
           mod_genome[line_1].SetOp(mod_inst_1);
           mod_genome[line_2].SetOp(mod_inst_2);
           cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-          test_genotype.Recalculate(m_ctx, m_testcpu);
+          test_genotype.Recalculate(m_ctx);
           // Adjust fitness ...
           if (test_genotype.GetFitness() <= base_fitness) {
             test_fitness[mod_inst_1][mod_inst_2] = test_genotype.GetFitness();
@@ -705,7 +686,7 @@
   double entropy = 0.0;
   
   // Calculate the stats for the genotype we're working with ...
-  genotype->Recalculate(m_ctx, m_testcpu);
+  genotype->Recalculate(m_ctx);
   const int num_insts = inst_set.GetSize();
   const int num_lines = genotype->GetLength();
   const cGenome & base_genome = genotype->GetGenome();
@@ -723,7 +704,7 @@
     for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
       mod_genome[line_no].SetOp(mod_inst);
       cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-      test_genotype.Recalculate(m_ctx, m_testcpu);
+      test_genotype.Recalculate(m_ctx);
       test_fitness[mod_inst] = test_genotype.GetFitness();
     }
     
@@ -798,7 +779,7 @@
     if (exit_on_error) exit(1);
   }
   
-  genotype1->Recalculate(m_ctx, m_testcpu);
+  genotype1->Recalculate(m_ctx);
   if (genotype1->GetFitness() == 0) {
     return 0.0;
   }
@@ -820,7 +801,7 @@
     for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
       genotype1_mod_genome[line_no].SetOp(mod_inst);
       cAnalyzeGenotype test_genotype(m_world, genotype1_mod_genome, inst_set);
-      test_genotype.Recalculate(m_ctx, m_testcpu);
+      test_genotype.Recalculate(m_ctx);
       // Ajust fitness ...
       if (test_genotype.GetFitness() <= genotype1_base_fitness) {
         test_fitness[mod_inst] = test_genotype.GetFitness();
@@ -869,7 +850,7 @@
     genotype1_mod_genome[line_no].SetOp(cur_inst);
   }
   
-  genotype2->Recalculate(m_ctx, m_testcpu);
+  genotype2->Recalculate(m_ctx);
   if (genotype2->GetFitness() == 0) {
     for (int line_no = 0; line_no < num_lines; ++ line_no) {
       increased_info += genotype1_info[line_no];
@@ -889,7 +870,7 @@
     for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
       genotype2_mod_genome[line_no].SetOp(mod_inst);
       cAnalyzeGenotype test_genotype(m_world, genotype2_mod_genome, inst_set);
-      test_genotype.Recalculate(m_ctx, m_testcpu);
+      test_genotype.Recalculate(m_ctx);
       // Ajust fitness ...
       if (test_genotype.GetFitness() <= genotype2_base_fitness) {
         test_fitness[mod_inst] = test_genotype.GetFitness();
@@ -1751,9 +1732,7 @@
     msg.Set("Tracing organisms.");
   m_world->GetDriver().NotifyComment(msg);
   
-  
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();  
-  testcpu->InitResources(use_resources, &resources, update, m_resource_time_spent_offset);
   
   tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
   cAnalyzeGenotype * genotype = NULL;
@@ -1774,7 +1753,8 @@
       test_info.UseManualInputs(manual_inputs);
     else
       test_info.UseRandomInputs(use_random_inputs); 
-    
+    test_info.SetResourceOptions(use_resources, &resources, update, m_resource_time_spent_offset);
+
     if (m_world->GetVerbosity() >= VERBOSE_ON){
       msg = cString("Tracing ") + filename;
       m_world->GetDriver().NotifyComment(msg);
@@ -2822,10 +2802,10 @@
   }
   
   ///////////////////////
-  // Create Test CPU
-  cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+  // Create Test Info 
   // No choice of use_resources for this analyze command...
-  testcpu->InitResources(cTestCPU::RES_CONSTANT, &resources, update, m_resource_time_spent_offset);
+  cCPUTestInfo test_info;
+  test_info.SetResourceOptions(RES_CONSTANT, &resources, update, m_resource_time_spent_offset);
   
   
   ///////////////////////////////////////////////////////////////////////
@@ -2949,7 +2929,7 @@
     genotype = sorted_community[i];
     
     // Skip the dead organisms
-    genotype->Recalculate(m_ctx, testcpu);
+    genotype->Recalculate(m_ctx, &test_info);
     if (genotype->GetFitness() == 0) {
       continue;
     }
@@ -3053,7 +3033,7 @@
         mod_genome = base_genome;
         mod_genome[line].SetOp(given_inst);
         cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-        test_genotype.Recalculate(m_ctx, testcpu);
+        test_genotype.Recalculate(m_ctx, &test_info);
         
         // Only when given inst make the genotype alive
         if (test_genotype.GetFitness() > 0) {
@@ -3076,7 +3056,7 @@
     vector< vector<bool> > neutral_mut(length_genome, one_line_neutral);
     vector< vector<bool> > alive_mut(length_genome, one_line_neutral);
     
-    genotype->Recalculate(m_ctx, testcpu);
+    genotype->Recalculate(m_ctx, &test_info);
     double base_fitness = genotype->GetFitness();
     cout << base_fitness << endl;
     const cGenome & base_genome = genotype->GetGenome();
@@ -3088,7 +3068,7 @@
       for (int mod_inst = 0; mod_inst < num_insts; ++ mod_inst) {
         mod_genome[line].SetOp(mod_inst);
         cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-        test_genotype.Recalculate(m_ctx, testcpu);
+        test_genotype.Recalculate(m_ctx, &test_info);
         if (test_genotype.GetFitness() >= base_fitness) {
           neutral_mut[line][mod_inst] = true;
         } 
@@ -3237,9 +3217,7 @@
     given_genotypes.push_back(genotype);
     
   }
-  
-  delete testcpu;
-  
+    
   m_world->GetDataFileManager().Remove(filename);
   return;
 }
@@ -3409,9 +3387,9 @@
   
   ///////////////////////
   // Backup test CPU data
-  cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+  cCPUTestInfo test_info;
   // No choice of use_resources for this analyze command...
-  testcpu->InitResources(cTestCPU::RES_CONSTANT, &resources, update, m_resource_time_spent_offset);
+  test_info.SetResourceOptions(RES_CONSTANT, &resources, update, m_resource_time_spent_offset);
   
   vector<cAnalyzeGenotype *> community;
   cAnalyzeGenotype * genotype = NULL;
@@ -3436,7 +3414,7 @@
       m_world->GetDataFileManager().Remove(filename);
       return;
     }
-    genotype->Recalculate(m_ctx, testcpu);
+    genotype->Recalculate(m_ctx, &test_info);
     int num_tasks = genotype->GetNumTasks();
     vector< vector<cAnalyzeGenotype *> > genotype_class(num_tasks);
     do {
@@ -3494,7 +3472,7 @@
     cout << "Test point mutation for genotype " << genotype->GetID() << endl;
     tMatrix<double> prob(length_genome, num_insts);
     
-    genotype->Recalculate(m_ctx, testcpu);
+    genotype->Recalculate(m_ctx, &test_info);
     double base_fitness = genotype->GetFitness();
     const cGenome & base_genome = genotype->GetGenome();
     cGenome mod_genome(base_genome);
@@ -3506,7 +3484,7 @@
       for (int mod_inst = 0; mod_inst < num_insts; ++ mod_inst) {
         mod_genome[line].SetOp(mod_inst);
         cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-        test_genotype.Recalculate(m_ctx, testcpu);
+        test_genotype.Recalculate(m_ctx, &test_info);
         if (test_genotype.GetFitness() >= base_fitness) {
           prob[line][mod_inst] = 1.0;
           num_neutral ++;
@@ -3565,7 +3543,7 @@
   int num_cpus = genotype->GetNumCPUs();
   total_cpus += num_cpus;
   cpx_fp << num_cpus << " " << total_cpus << "   ";
-  genotype->Recalculate(m_ctx, testcpu);
+  genotype->Recalculate(m_ctx, &test_info);
   genotype->PrintTasks(cpx_fp, 0, -1);
   cpx_fp << endl;
   
@@ -3580,7 +3558,7 @@
     }
     
     // Skip the dead organisms
-    genotype->Recalculate(m_ctx, testcpu);
+    genotype->Recalculate(m_ctx, &test_info);
     cout << genotype->GetID() << " " << genotype->GetFitness() << endl;
     if (genotype->GetFitness() == 0) {
       continue;
@@ -3692,9 +3670,6 @@
     given_genotypes.push_back(genotype);
   }
   
-  
-  delete testcpu;
-  
   m_world->GetDataFileManager().Remove(filename);
   return;
 }
@@ -4069,7 +4044,7 @@
     }
     
     // Collect basic state before and after the mutations...
-    genotype->Recalculate(m_ctx, m_testcpu);
+    genotype->Recalculate(m_ctx);
     double start_complexity = genotype->GetKO_Complexity();
     double start_fitness = genotype->GetFitness();
     int start_length = genotype->GetLength();
@@ -4078,7 +4053,7 @@
     const tArray< tArray<int> >& start_KO_task_counts = genotype->GetKO_TaskCounts();
     
     cAnalyzeGenotype new_genotype(m_world, mod_genome, inst_set);
-    new_genotype.Recalculate(m_ctx, m_testcpu);
+    new_genotype.Recalculate(m_ctx);
     double end_complexity = new_genotype.GetKO_Complexity();
     double end_fitness = new_genotype.GetFitness();
     int end_length = new_genotype.GetLength();
@@ -4261,7 +4236,7 @@
     if (m_world->GetVerbosity() >= VERBOSE_ON) cout << "  Knockout: " << genotype->GetName() << endl;
     
     // Calculate the stats for the genotype we're working with...
-    genotype->Recalculate(m_ctx, m_testcpu);
+    genotype->Recalculate(m_ctx);
     const double base_fitness = genotype->GetFitness();
     
     const int max_line = genotype->GetLength();
@@ -4280,7 +4255,7 @@
       int cur_inst = base_genome[line_num].GetOp();
       mod_genome[line_num] = null_inst;
       cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
-      ko_genotype.Recalculate(m_ctx, m_testcpu);
+      ko_genotype.Recalculate(m_ctx);
       
       double ko_fitness = ko_genotype.GetFitness();
       if (ko_fitness == 0.0) {
@@ -4312,7 +4287,7 @@
           mod_genome[line1] = null_inst;
           mod_genome[line2] = null_inst;
           cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
-          ko_genotype.Recalculate(m_ctx, m_testcpu);
+          ko_genotype.Recalculate(m_ctx);
           
           double ko_fitness = ko_genotype.GetFitness();
           
@@ -4536,14 +4511,12 @@
     }
     
     // Calculate the stats for the genotype we're working with...
-    cTestCPU* test_cpu = new cTestCPU(m_world);
-    test_cpu->InitResources(use_resources, &resources);
     cCPUTestInfo test_info;
     if (use_manual_inputs)
       test_info.UseManualInputs(manual_inputs);
+    test_info.SetResourceOptions(use_resources, &resources);
+    genotype->Recalculate(m_ctx, &test_info);
     
-    genotype->Recalculate(m_ctx, test_cpu, NULL, &test_info);
-    
     // Headers...
     if (file_type == FILE_TYPE_TEXT) {
       fp << "-1 "  << batch[cur_batch].Name() << " "
@@ -4652,11 +4625,8 @@
       
       mod_genome[line_num] = null_inst;
       cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
-      test_cpu->InitResources(use_resources, &resources);
+      test_genotype.Recalculate(m_ctx, &test_info);
       
-      
-      test_genotype.Recalculate(m_ctx, m_testcpu, NULL, &test_info);
-      
       if (file_type == FILE_TYPE_HTML) fp << "<tr><td align=right>";
       fp << (line_num + 1) << " ";
       if (file_type == FILE_TYPE_HTML) fp << "<td align=center>";
@@ -4847,7 +4817,7 @@
     if (m_world->GetVerbosity() >= VERBOSE_ON) cout << "  Mapping " << genotype->GetName() << endl;
     
     // Calculate the stats for the genotype we're working with...
-    genotype->Recalculate(m_ctx, m_testcpu);
+    genotype->Recalculate(m_ctx);
     
     // Check if the organism does any tasks. 
     int does_tasks = 0;
@@ -4902,7 +4872,7 @@
           mod_genome[line_num] = null_inst;
           cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
           cAnalyzeGenotype old_genotype(m_world, base_genome, map_inst_set);
-          test_genotype.Recalculate(m_ctx, m_testcpu);
+          test_genotype.Recalculate(m_ctx);
           
           // Print the individual columns...
           output_it.Reset();
@@ -5111,7 +5081,7 @@
     const int base_length = genotype->GetLength();
     const cGenome & base_genome = genotype->GetGenome();
     cGenome mod_genome(base_genome);
-    genotype->Recalculate(m_ctx, m_testcpu);
+    genotype->Recalculate(m_ctx);
     
     tMatrix<bool> task_matrix(num_traits, base_length);
     tArray<int> num_inst(num_traits);  // Number of instructions for each task
@@ -5127,7 +5097,7 @@
       // Determine what happens to this genotype when this line is knocked out
       mod_genome[line_num] = null_inst;
       cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
-      test_genotype.Recalculate(m_ctx, m_testcpu);
+      test_genotype.Recalculate(m_ctx);
       
       // Loop through the individual traits
       output_it.Reset();
@@ -5274,7 +5244,7 @@
     ofstream& fp = m_world->GetDataFileOFStream(filename);
     
     // Calculate the stats for the genotype we're working with...
-    genotype->Recalculate(m_ctx, m_testcpu);
+    genotype->Recalculate(m_ctx);
     const double base_fitness = genotype->GetFitness();
     const int num_insts = inst_set.GetSize();
     
@@ -5372,7 +5342,7 @@
         else {
           mod_genome[line_num].SetOp(mod_inst);
           cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-          test_genotype.Recalculate(m_ctx, m_testcpu);
+          test_genotype.Recalculate(m_ctx);
           const double test_fitness = test_genotype.GetFitness() / base_fitness;
           row_fitness += test_fitness;
           total_fitness += test_fitness;
@@ -5408,7 +5378,7 @@
       // Column: Knockout
       mod_genome[line_num] = null_inst;
       cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
-      test_genotype.Recalculate(m_ctx, m_testcpu);
+      test_genotype.Recalculate(m_ctx);
       const double test_fitness = test_genotype.GetFitness() / base_fitness;
       col_fitness[num_insts] += test_fitness;
       
@@ -6399,7 +6369,7 @@
     
     for (int i = 0; i <= total_diffs; i++) {
       cAnalyzeGenotype max_genotype(m_world, max_sequence[i], inst_set);
-      max_genotype.Recalculate(m_ctx, testcpu);
+      max_genotype.Recalculate(m_ctx);
       fp << i                                         << " "  //  1
         << test_count[i]                             << " "  //  2
         << total_fitness[i] / (double) test_count[i] << " "  //  3
@@ -6817,10 +6787,11 @@
     
     int updateBorn = -1;
     updateBorn = genotype->GetUpdateBorn();
-    testcpu->InitResources(useResources, &resources, updateBorn, m_resource_time_spent_offset);
+    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, testcpu);
+    genotype->Recalculate(m_ctx, &test_info);
     cout << genotype->GetFitness() << endl;
     const int num_insts = inst_set.GetSize();
     const int max_line = genotype->GetLength();
@@ -6841,7 +6812,7 @@
       for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
         mod_genome[line_num].SetOp(mod_inst);
         cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-        test_genotype.Recalculate(m_ctx, testcpu);
+        test_genotype.Recalculate(m_ctx);
         test_fitness[mod_inst] = test_genotype.GetFitness();
       }
       
@@ -7051,9 +7022,8 @@
 	//Our edit distance is already stored in the historical dump.
 	
 	//Test hardware
-	cTestCPU*     test_cpu  = m_world->GetHardwareManager().CreateTestCPU();
-	cCPUTestInfo* test_info = new cCPUTestInfo();
-	test_info->UseRandomInputs(true); 
+	cCPUTestInfo test_info;
+	test_info.UseRandomInputs(true); 
   
 	tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
   cAnalyzeGenotype* parent_genotype = batch_it.Next();
@@ -7117,9 +7087,8 @@
 					}
 					
 					cAnalyzeGenotype new_genotype(m_world, reverted, inst_set);  //Get likely fitness
-					new_genotype.Recalculate(m_ctx, test_cpu, NULL, test_info, 50);
+					new_genotype.Recalculate(m_ctx, &test_info, NULL, 50);
 					
-					
           FOT << other_genotype->GetID()			<< " "
             << other_genotype->GetFitness()		<< " "
             << other_genotype->GetUpdateBorn() << " "
@@ -7139,10 +7108,6 @@
 		}
 		parent_genotype = genotype;
   }
-  
-  //Clean up
-	delete test_cpu;
-	delete test_info;
 	
   return;
 }
@@ -7343,15 +7308,13 @@
     }
   }
   
-  
-  cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
-  
-  cCPUTestInfo *test_info = new cCPUTestInfo();
+  cCPUTestInfo test_info;
   if (use_manual_inputs)
-    test_info->UseManualInputs(manual_inputs);
+    test_info.UseManualInputs(manual_inputs);
   else
-    test_info->UseRandomInputs(use_random_inputs); 
-  
+    test_info.UseRandomInputs(use_random_inputs); 
+  test_info.SetResourceOptions(use_resources, &resources, update, m_resource_time_spent_offset);
+
   if (m_world->GetVerbosity() >= VERBOSE_ON) {
     msg.Set("Running batch %d through test CPUs...", cur_batch);
     m_world->GetDriver().NotifyComment(msg);
@@ -7368,23 +7331,17 @@
   tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
   cAnalyzeGenotype * genotype = NULL;
   cAnalyzeGenotype * last_genotype = NULL;
-  while ((genotype = batch_it.Next()) != NULL) {
-    // Load proper resources according to update_born
-    testcpu->InitResources(use_resources, &resources, update, m_resource_time_spent_offset);
-    
+  while ((genotype = batch_it.Next()) != NULL) {    
     // If the previous genotype was the parent of this one, pass in a pointer
     // to it for improved recalculate (such as distance to parent, etc.)
     if (last_genotype != NULL && genotype->GetParentID() == last_genotype->GetID()) {
-      genotype->Recalculate(m_ctx, testcpu, last_genotype, test_info);
+      genotype->Recalculate(m_ctx, &test_info, last_genotype);
     } else {
-      genotype->Recalculate(m_ctx, testcpu, NULL, test_info);
+      genotype->Recalculate(m_ctx, &test_info);
     }
     last_genotype = genotype;
   }
-  
-  delete test_info;
-  delete testcpu;
-  
+    
   return;
 }
 
@@ -7438,12 +7395,12 @@
   if (use_manual_inputs)
     use_random_inputs = false;
   
-  cTestCPU*     test_cpu = m_world->GetHardwareManager().CreateTestCPU();
-  cCPUTestInfo *test_info = new cCPUTestInfo();
+  cCPUTestInfo test_info;
   if (use_manual_inputs)
-    test_info->UseManualInputs(manual_inputs);
+    test_info.UseManualInputs(manual_inputs);
   else
-    test_info->UseRandomInputs(use_random_inputs); 
+    test_info.UseRandomInputs(use_random_inputs); 
+  test_info.SetResourceOptions(use_resources, &resources, update, m_resource_time_spent_offset);
   
   // Notifications
   if (m_world->GetVerbosity() >= VERBOSE_ON) {
@@ -7461,23 +7418,17 @@
   tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
   cAnalyzeGenotype * genotype = NULL;
   cAnalyzeGenotype * last_genotype = NULL;
-  while ((genotype = batch_it.Next()) != NULL) {
-    // Load proper resources according to update_born
-    test_cpu->InitResources(use_resources, &resources, update, m_resource_time_spent_offset);
-    
+  while ((genotype = batch_it.Next()) != NULL) {    
     // If the previous genotype was the parent of this one, pass in a pointer
     // to it for improved recalculate (such as distance to parent, etc.)
     if (last_genotype != NULL && genotype->GetParentID() == last_genotype->GetID()) {
-      genotype->Recalculate(m_ctx, test_cpu, last_genotype, test_info, num_trials);
+      genotype->Recalculate(m_ctx, &test_info, last_genotype, num_trials);
     } else {
-      genotype->Recalculate(m_ctx, test_cpu, NULL, test_info, num_trials);
+      genotype->Recalculate(m_ctx, &test_info, NULL, num_trials);
     }
     last_genotype = genotype;
   }
   
-  delete test_info;
-  delete test_cpu;
-  
   return;
 }
 
@@ -7658,19 +7609,18 @@
   tArray<cMerit> fitness_array(parent_batch_size);
   cAnalyzeGenotype * genotype = NULL;
   
-  cTestCPU *testcpu = m_world->GetHardwareManager().CreateTestCPU();
-  cCPUTestInfo *test_info = new cCPUTestInfo();
+  cCPUTestInfo test_info;
   
   /*
    kgn at FIXME
    This should be settable by an optional argument.
    */
-  test_info->UseRandomInputs(true); 
+  test_info.UseRandomInputs(true); 
   
   int array_pos = 0;
   while ((genotype = batch_it.Next()) != NULL) {
     genotype_array[array_pos] = genotype;
-    genotype->Recalculate(m_world->GetDefaultContext(), testcpu, NULL, test_info);
+    genotype->Recalculate(m_world->GetDefaultContext(), &test_info, NULL);
     if(genotype->GetViable()){
       /*
        kgn at FIXME
@@ -7684,7 +7634,7 @@
        parent, if the parent is viable, so that genome of first descendent may
        differ from that of parent.
        */
-      offspring_genome_array[array_pos] = test_info->GetTestOrganism(0)->ChildGenome();
+      offspring_genome_array[array_pos] = test_info.GetTestOrganism(0)->ChildGenome();
     } else {
       fitness_array[array_pos] = 0.0;
     }
@@ -7758,8 +7708,6 @@
   batch[batch_to].SetLineage(false);
   batch[batch_to].SetAligned(false);
   
-  if(test_info){ delete test_info; test_info = 0; }
-  if(testcpu){ delete testcpu; testcpu = 0; }
   if(schedule){ delete schedule; schedule = 0; }
   
   return;

Modified: development/source/analyze/cAnalyze.h
===================================================================
--- development/source/analyze/cAnalyze.h	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/analyze/cAnalyze.h	2007-11-02 02:34:02 UTC (rev 2174)
@@ -111,7 +111,6 @@
 
   cWorld* m_world;
   cInstSet& inst_set;
-  cTestCPU* m_testcpu;
   cAvidaContext& m_ctx;
   cAnalyzeJobQueue m_jobqueue;
 

Modified: development/source/analyze/cAnalyzeGenotype.cc
===================================================================
--- development/source/analyze/cAnalyzeGenotype.cc	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/analyze/cAnalyzeGenotype.cc	2007-11-02 02:34:02 UTC (rev 2174)
@@ -213,7 +213,7 @@
   // (This may not have been run already, and cost negligiably more time
   // considering the number of knockouts we need to do.
   cAnalyzeGenotype base_genotype(m_world, genome, inst_set);
-  base_genotype.Recalculate(ctx, testcpu);
+  base_genotype.Recalculate(ctx);
   double base_fitness = base_genotype.GetFitness();
   const tArray<int> base_task_counts( base_genotype.GetTaskCounts() );
   
@@ -245,7 +245,7 @@
     int cur_inst = mod_genome[line_num].GetOp();
     mod_genome[line_num] = null_inst;
     cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
-    ko_genotype.Recalculate(ctx, testcpu);
+    ko_genotype.Recalculate(ctx);
     if (check_chart == true) {
       const tArray<int> ko_task_counts( ko_genotype.GetTaskCounts() );
       knockout_stats->task_counts[line_num] = ko_task_counts;
@@ -305,7 +305,7 @@
       mod_genome[line1] = null_inst;
       mod_genome[line2] = null_inst;
       cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
-      ko_genotype.Recalculate(ctx, testcpu);
+      ko_genotype.Recalculate(ctx);
       
       double ko_fitness = ko_genotype.GetFitness();
       
@@ -386,10 +386,8 @@
   m_land->Process(ctx);
 }
 
-void cAnalyzeGenotype::Recalculate(cAvidaContext& ctx, cTestCPU* testcpu, cAnalyzeGenotype* parent_genotype, cCPUTestInfo* test_info, int num_trials)
-{
-  // We are not going to use this testcpu ...
-  
+void cAnalyzeGenotype::Recalculate(cAvidaContext& ctx, cCPUTestInfo* test_info, cAnalyzeGenotype* parent_genotype, int num_trials)
+{  
   //Allocate our own test info if it wasn't provided
   cCPUTestInfo* temp_test_info = NULL;
   if (!test_info)

Modified: development/source/analyze/cAnalyzeGenotype.h
===================================================================
--- development/source/analyze/cAnalyzeGenotype.h	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/analyze/cAnalyzeGenotype.h	2007-11-02 02:34:02 UTC (rev 2174)
@@ -233,7 +233,7 @@
   const cStringList & GetSpecialArgs() { return special_args; }
   void SetSpecialArgs(const cStringList & _args) { special_args = _args; }
 
-  void Recalculate(cAvidaContext& ctx, cTestCPU* testcpu, cAnalyzeGenotype* parent_genotype = NULL, cCPUTestInfo* test_info = NULL, int num_trials = 1);
+  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);
   void CalcLandscape(cAvidaContext& ctx);

Modified: development/source/cpu/cCPUTestInfo.cc
===================================================================
--- development/source/cpu/cCPUTestInfo.cc	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/cpu/cCPUTestInfo.cc	2007-11-02 02:34:02 UTC (rev 2174)
@@ -39,6 +39,10 @@
   , use_manual_inputs(false)
   , m_tracer(NULL)
   , org_array(max_tests)
+  , m_res_method(RES_INITIAL)
+  , m_res(NULL)
+  , m_res_update(0)
+  , m_res_cpu_cycle_offset(0)
 {
   org_array.SetAll(NULL);
   Clear();

Modified: development/source/cpu/cCPUTestInfo.h
===================================================================
--- development/source/cpu/cCPUTestInfo.h	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/cpu/cCPUTestInfo.h	2007-11-02 02:34:02 UTC (rev 2174)
@@ -26,6 +26,8 @@
 #ifndef cCPUTestInfo_h
 #define cCPUTestInfo_h
 
+#include <vector>
+
 #ifndef nHardware_h
 #include "nHardware.h"
 #endif
@@ -41,6 +43,14 @@
 class cPhenotype;
 class cString;
 
+enum eTestCPUResourceMethod { RES_INITIAL = 0, RES_CONSTANT, RES_UPDATED_DEPLETABLE, RES_DYNAMIC, RES_LAST };  
+// Modes for how the test CPU handles resources:
+// OFF - all resources are at zero. (OLD: use_resources = 0)
+// CONSTANT - resources stay constant at input values for the specified update. (OLD: use_resources = 1)
+// UPDATED_DEPLETABLE - resources change every update according to resource data file (assuming an update
+//    is an average time slice). The organism also depletes these resources when using them.
+// DYNAMIC - UPDATED_DEPLETABLE + resources inflow/outflow (NOT IMPLEMENTED YET!)
+
 class cCPUTestInfo
 {
   friend class cTestCPU;
@@ -63,8 +73,13 @@
 	tArray<int> used_inputs; //Depth 0 inputs
 
   tArray<cOrganism*> org_array;
+  
+  // Information about how to handle resources
+  eTestCPUResourceMethod m_res_method;
+  std::vector<std::pair<int, std::vector<double> > > * m_res;
+  int m_res_update;
+  int m_res_cpu_cycle_offset;
 
-
   cCPUTestInfo(const cCPUTestInfo&); // @not_implemented
   cCPUTestInfo& operator=(const cCPUTestInfo&); // @not_implemented
 
@@ -80,7 +95,10 @@
 	void UseManualInputs(tArray<int> inputs) {use_manual_inputs = true; use_random_inputs = false; manual_inputs = inputs;}
 	void ResetInputMode() {use_manual_inputs = false; use_random_inputs = false;}
   void SetTraceExecution(cHardwareTracer *tracer = NULL);
+  void SetResourceOptions(int res_method = RES_INITIAL, std::vector<std::pair<int, std::vector<double> > > * res = NULL, int update = 0, int cpu_cycle_offset = 0)
+    { m_res_method = (eTestCPUResourceMethod)res_method; m_res = res; m_res_update = update; m_res_cpu_cycle_offset = cpu_cycle_offset; }
 
+
   // Input Accessors
   int GetGenerationTests() const { return generation_tests; }
   bool GetTraceTaskOrder() const { return trace_task_order; }

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/cpu/cHardwareCPU.cc	2007-11-02 02:34:02 UTC (rev 2174)
@@ -4519,8 +4519,11 @@
   assert(_num_bits <= (int)max_bits);
   if (_num_bits == 0) _num_bits = max_bits;
   
-  int j = _pos;
+  // Enforce a boundary, sometimes -1 can be passed for _pos
+  int j = _pos + GetMemory().GetSize();
   j %= GetMemory().GetSize();
+  assert(j >=0);
+  assert(j < GetMemory().GetSize());
   while (code_size < _num_bits)
   {
     unsigned int inst_code = (unsigned int) GetInstSet().GetInstructionCode( (GetMemory())[j] );

Modified: development/source/cpu/cHardwareManager.h
===================================================================
--- development/source/cpu/cHardwareManager.h	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/cpu/cHardwareManager.h	2007-11-02 02:34:02 UTC (rev 2174)
@@ -62,7 +62,7 @@
   ~cHardwareManager() { ; }
   
   cHardwareBase* Create(cOrganism* in_org);
-  cTestCPU* CreateTestCPU() { return new cTestCPU(m_world /*, &m_testres*/); }
+  cTestCPU* CreateTestCPU() { return new cTestCPU(m_world); }
 
   const cInstSet& GetInstSet() const { return *m_inst_set; }
   cInstSet& GetInstSet() { return *m_inst_set; }

Modified: development/source/cpu/cTestCPU.cc
===================================================================
--- development/source/cpu/cTestCPU.cc	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/cpu/cTestCPU.cc	2007-11-02 02:34:02 UTC (rev 2174)
@@ -55,11 +55,11 @@
 cTestCPU::cTestCPU(cWorld* world)
 {
   m_world = world;
-  InitResources();
 	m_use_manual_inputs = false;
 }  
+
  
-void cTestCPU::InitResources(int res_method, std::vector<std::pair<int, std::vector<double> > > * res, int update, int time_spent_offset)
+void cTestCPU::InitResources(int res_method, std::vector<std::pair<int, std::vector<double> > > * res, int update, int cpu_cycle_offset)
 {  
   //FOR DEMES
   m_deme_resource_count.SetSize(0);
@@ -72,14 +72,14 @@
   
   // Setup the resources...
   m_res = res;
-  m_res_time_spent_offset = time_spent_offset;
+  m_res_cpu_cycle_offset = cpu_cycle_offset;
   m_res_update = update;
 
   // Adjust updates if time_spent_offset is greater than a time slice
   int ave_time_slice = m_world->GetConfig().AVE_TIME_SLICE.Get();
-  m_res_update += m_res_time_spent_offset / ave_time_slice;
-  m_res_time_spent_offset %= ave_time_slice;
-  assert(m_res_time_spent_offset >= 0);
+  m_res_update += m_res_cpu_cycle_offset / ave_time_slice;
+  m_res_cpu_cycle_offset %= ave_time_slice;
+  assert(m_res_cpu_cycle_offset >= 0);
   
   // If they didn't send anything (usually during an avida run as opposed to analyze mode),
   // then we set up a static variable (or just point to it) that reflects the initial conditions of the run
@@ -125,6 +125,15 @@
   // Round down to the closest update to choose how to initializae resources
 }
 
+void cTestCPU::UpdateResources(int cpu_cycles_used)
+{
+    int ave_time_slice = m_world->GetConfig().AVE_TIME_SLICE.Get();
+    if ((m_res_method >= RES_UPDATED_DEPLETABLE) && (cpu_cycles_used % ave_time_slice == 0))
+    {
+      SetResourceUpdate(m_res_update+1);
+    }
+}
+
 void cTestCPU::SetResourceUpdate(int update, bool round_to_closest)
 {
   assert(m_res != NULL);
@@ -191,7 +200,7 @@
 
 void cTestCPU::ModifyResources(const tArray<double>& res_change)
 {
-  //We only let the testCPU modify the resources if we are using a DEPLETABLE options. @JEB
+  //We only let the testCPU modify the resources if we are using a DEPLETABLE option. @JEB
   if (m_res_method >= RES_UPDATED_DEPLETABLE) m_resource_count.Modify(res_change);
 }
 
@@ -205,19 +214,22 @@
 
   // Determine how long this organism should be tested for...
   int time_allocated = m_world->GetConfig().TEST_CPU_TIME_MOD.Get() * organism.GetGenome().GetSize();
-  time_allocated += m_res_time_spent_offset; // If the resource offset has us starting at a different time, adjust @JEB
+  time_allocated += m_res_cpu_cycle_offset; // If the resource offset has us starting at a different time, adjust @JEB
 
   // Prepare the inputs...
   cur_input = 0;
   cur_receive = 0;
 
+  // Prepare the resources
+  InitResources(test_info.m_res_method, test_info.m_res, test_info.m_res_update, test_info.m_res_cpu_cycle_offset);
+
   // Determine if we're tracing and what we need to print.
   cHardwareTracer* tracer = test_info.GetTraceExecution() ? (test_info.GetTracer()) : NULL;
   std::ostream * tracerStream = NULL;
   if (tracer != NULL) tracerStream = tracer->GetStream();
 
-  int time_used = m_res_time_spent_offset; // Note: this is zero by default if no resources being used @JEB
-  int ave_time_slice = m_world->GetConfig().AVE_TIME_SLICE.Get();
+  // This way of keeping track of time is only used to update resources...
+  int time_used = m_res_cpu_cycle_offset; // Note: the offset is zero by default if no resources being used @JEB
   
   organism.GetHardware().SetTrace(tracer);
   while (time_used < time_allocated && organism.GetHardware().GetMemory().GetSize() &&
@@ -227,11 +239,8 @@
     
     // @CAO Need to watch out for parasites.
     
-    // Assume an update is based on the average time slice for updating test cpu resources @JEB
-    if ((m_res_method >= RES_UPDATED_DEPLETABLE) && (time_used % ave_time_slice == 0))
-    {
-      SetResourceUpdate(m_res_update+1);
-    }
+    // Resources will be updates as if each update takes a number of cpu cycles equal to the average time slice
+    UpdateResources(time_used);
     
     // Add extra info to trace files so that we can watch resource changes. 
     // This is a clumsy way to insert it in the trace file, but works for my purposes @JEB

Modified: development/source/cpu/cTestCPU.h
===================================================================
--- development/source/cpu/cTestCPU.h	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/cpu/cTestCPU.h	2007-11-02 02:34:02 UTC (rev 2174)
@@ -38,9 +38,11 @@
 #ifndef cResourceCount_h
 #include "cResourceCount.h"
 #endif
+#ifndef cCPUTestInfo_h
+#include "cCPUTestInfo.h"
+#endif
 
 class cAvidaContext;
-class cCPUTestInfo;
 class cGenome;
 class cGenotype;
 class cInjectGenotype;
@@ -52,13 +54,6 @@
 class cTestCPU
 {
 public:
-  enum eTestCPUResourceMethod { RES_INITIAL = 0, RES_CONSTANT, RES_UPDATED_DEPLETABLE, RES_DYNAMIC, RES_LAST };  
-  // Modes for how the test CPU handles resources:
-  // OFF - all resources are at zero. (OLD: use_resources = 0)
-  // CONSTANT - resources stay constant at input values for the specified update. (OLD: use_resources = 1)
-  // UPDATED_DEPLETABLE - resources change every update according to resource data file (assuming an update
-  //    is an average time slice). The organism also depletes these resources when using them.
-  // DYNAMIC - UPDATED_DEPLETABLE + resources inflow/outflow (NOT IMPLEMENTED YET!)
 
 private:
   cWorld* m_world;
@@ -68,11 +63,14 @@
   int cur_receive;  
   bool m_use_random_inputs;
   bool m_use_manual_inputs;
-	
+  
+  // Resource settings. Reinitialized from cCPUTestInfo on each test.
   eTestCPUResourceMethod m_res_method;
   std::vector<std::pair<int, std::vector<double> > > * m_res;
-  int m_res_time_spent_offset;
   int m_res_update;
+  int m_res_cpu_cycle_offset;
+
+  // Actual CPU resources.
   cResourceCount m_resource_count;
   cResourceCount m_deme_resource_count;
 
@@ -86,6 +84,11 @@
   cTestCPU(const cTestCPU&); // @not_implemented
   cTestCPU& operator=(const cTestCPU&); // @not_implemented
   
+  // Internal methods for setting up and updating resources
+  void InitResources(int res_method = RES_INITIAL, std::vector<std::pair<int, std::vector<double> > > * res = NULL, int update = 0, int cpu_cycle_offset = 0);
+  void UpdateResources(int cpu_cycles_used);
+  void SetResourceUpdate(int update, bool round_to_closest = false);
+  inline void SetResource(int id, double new_level);
 public:
   cTestCPU(cWorld* world);
   ~cTestCPU() { }
@@ -106,9 +109,8 @@
   inline int GetReceiveValue();
   inline const tArray<double>& GetResources();
   inline const tArray<double>& GetDemeResources(int deme_id);
-  inline void SetResource(int id, double new_level);
-  void InitResources(int res_method = RES_INITIAL, std::vector<std::pair<int, std::vector<double> > > * res = NULL, int update = 0, int time_spent_offset = 0);
-  void SetResourceUpdate(int update, bool round_to_closest = false);
+  
+  // Used by cTestCPUInterface to get/update resources
   void ModifyResources(const tArray<double>& res_change);
   cResourceCount& GetResourceCount() { return m_resource_count; }
 };

Modified: development/source/main/cPhenPlastGenotype.cc
===================================================================
--- development/source/main/cPhenPlastGenotype.cc	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/main/cPhenPlastGenotype.cc	2007-11-02 02:34:02 UTC (rev 2174)
@@ -34,7 +34,7 @@
   Process(test_info, world, ctx);
 }
 
-cPhenPlastGenotype::cPhenPlastGenotype(const cGenome& in_genome, int num_trials, cCPUTestInfo& test_info, cWorld* world, cAvidaContext& ctx)
+cPhenPlastGenotype::cPhenPlastGenotype(const cGenome& in_genome, int num_trials, cCPUTestInfo& test_info,  cWorld* world, cAvidaContext& ctx)
 : m_genome(in_genome), m_num_trials(num_trials), m_world(world)
 {
   // Override input mode if more than one recalculation requested
@@ -54,9 +54,10 @@
 
 void cPhenPlastGenotype::Process(cCPUTestInfo& test_info, cWorld* world, cAvidaContext& ctx)
 {
+  cTestCPU * test_cpu = m_world->GetHardwareManager().CreateTestCPU();
+
   if (m_num_trials > 1)
     test_info.UseRandomInputs(true);
-  cTestCPU* test_cpu = m_world->GetHardwareManager().CreateTestCPU();
   for (int k = 0; k < m_num_trials; k++){
     test_cpu->TestGenome(ctx, test_info, m_genome);
     //Is this a new phenotype?
@@ -103,7 +104,8 @@
     m_phenotypic_entropy -= freq * log(freq) / log(2.0);
     ++uit;
   }
-  delete test_cpu;
+  
+  if (test_cpu) delete test_cpu;
 }
 
 

Modified: development/source/main/cPhenPlastGenotype.h
===================================================================
--- development/source/main/cPhenPlastGenotype.h	2007-11-02 00:37:56 UTC (rev 2173)
+++ development/source/main/cPhenPlastGenotype.h	2007-11-02 02:34:02 UTC (rev 2174)
@@ -62,7 +62,7 @@
 {
   private:
 
-  typedef set<cPhenotype*, cPhenotype::lt_phenotype  > UniquePhenotypes;  //Actually, these are cPlatsicPhenotypes*
+  typedef set<cPhenotype*, cPhenotype::lt_phenotype  > UniquePhenotypes;  //Actually, these are cPlasticPhenotypes*
   tList<cPlasticPhenotype> m_plastic_phenotypes;  //This will store a list of our unique plastic phenotype pointers  
   cGenome m_genome;
     int m_num_trials;  
@@ -82,7 +82,7 @@
 
   public:
       cPhenPlastGenotype(const cGenome& in_genome, int num_trials, cWorld* world, cAvidaContext& ctx);
-    cPhenPlastGenotype(const cGenome& in_genome, int num_trails, cCPUTestInfo& test_info, cWorld* world, cAvidaContext& ctx);
+    cPhenPlastGenotype(const cGenome& in_genome, int num_trails, cCPUTestInfo& test_info,  cWorld* world, cAvidaContext& ctx);
     ~cPhenPlastGenotype();
     
     // Accessors




More information about the Avida-cvs mailing list