[Avida-cvs] [avida-svn] r502 - in development/source: analyze classification cpu event main
brysonda@myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Sat Mar 11 10:06:56 PST 2006
Author: brysonda
Date: 2006-03-11 13:06:56 -0500 (Sat, 11 Mar 2006)
New Revision: 502
Modified:
development/source/analyze/cAnalyze.cc
development/source/analyze/cAnalyze.h
development/source/analyze/cAnalyzeGenotype.cc
development/source/analyze/cAnalyzeGenotype.h
development/source/analyze/cAnalyzeUtil.cc
development/source/classification/cGenotype.cc
development/source/classification/cSpecies.cc
development/source/cpu/cHardware4Stack.cc
development/source/cpu/cHardwareCPU.cc
development/source/cpu/cHardwareManager.cc
development/source/cpu/cHardwareManager.h
development/source/cpu/cHardwareSMT.cc
development/source/cpu/cTestCPU.cc
development/source/cpu/cTestCPU.h
development/source/cpu/cTestUtil.cc
development/source/event/cEventManager.cc
development/source/main/cLandscape.cc
development/source/main/cLandscape.h
development/source/main/cMxCodeArray.cc
development/source/main/cWorld.cc
development/source/main/cWorld.h
Log:
Rework Test CPU usage. Create cTestCPU objects as needed, rather than sharing a (per world) singleton. This should allow multiple Test CPUs to be in use simultaneously.
In order to reduce instantiation cTestCPU overhead, cHardwareManager serves as a factory for test CPUs with a shared cTestResources object. This object is only copied into the local cTestCPU when changes to the resource state are made.
Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/analyze/cAnalyze.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -90,7 +90,9 @@
}
resources.push_back(make_pair(0, r));
}
- FillResources(0);
+ // @DMB - FillResources(0);
+
+ m_testcpu = m_world->GetHardwareManager().CreateTestCPU();
cInitFile analyze_file(m_world->GetConfig().ANALYZE_FILE.Get());
analyze_file.Load();
@@ -439,7 +441,7 @@
// Looks up the resource concentrations that are the closest to the
// given update and then fill in those concentrations into the environment.
-void cAnalyze::FillResources(int update)
+void cAnalyze::FillResources(cTestCPU* testcpu, int update)
{
// There must be some resources for at least one update
//assert(!resources.empty());
@@ -471,9 +473,8 @@
for(unsigned int i=0; i<resources[which].second.size(); i++) {
temp[i] = resources[which].second[i];
}
- m_world->GetTestCPU().SetupResourceArray(temp);
-
- return;
+
+ testcpu->SetupResourceArray(temp);
}
double cAnalyze::AnalyzeEntropy(cAnalyzeGenotype * genotype, double mu)
@@ -481,7 +482,7 @@
double entropy = 0.0;
// If the fitness is 0, the entropy is the length of genotype ...
- genotype->Recalculate();
+ genotype->Recalculate(m_testcpu);
if (genotype->GetFitness() == 0) {
return genotype->GetLength();
}
@@ -503,7 +504,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();
+ test_genotype.Recalculate(m_testcpu);
// Ajust fitness ...
if (test_genotype.GetFitness() <= base_fitness) {
test_fitness[mod_inst] = test_genotype.GetFitness();
@@ -561,7 +562,7 @@
double entropy = 0.0;
- genotype->Recalculate();
+ genotype->Recalculate(m_testcpu);
// Calculate the stats for the genotype we're working with ...
const int num_insts = inst_set.GetSize();
@@ -604,7 +605,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();
+ test_genotype.Recalculate(m_testcpu);
// Adjust fitness ...
if (test_genotype.GetFitness() <= base_fitness) {
test_fitness[mod_inst_1][mod_inst_2] = test_genotype.GetFitness();
@@ -683,7 +684,7 @@
double entropy = 0.0;
// Calculate the stats for the genotype we're working with ...
- genotype->Recalculate();
+ genotype->Recalculate(m_testcpu);
const int num_insts = inst_set.GetSize();
const int num_lines = genotype->GetLength();
const cGenome & base_genome = genotype->GetGenome();
@@ -701,7 +702,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();
+ test_genotype.Recalculate(m_testcpu);
test_fitness[mod_inst] = test_genotype.GetFitness();
}
@@ -776,7 +777,7 @@
exit(1);
}
- genotype1->Recalculate();
+ genotype1->Recalculate(m_testcpu);
if (genotype1->GetFitness() == 0) {
return 0.0;
}
@@ -798,7 +799,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();
+ test_genotype.Recalculate(m_testcpu);
// Ajust fitness ...
if (test_genotype.GetFitness() <= genotype1_base_fitness) {
test_fitness[mod_inst] = test_genotype.GetFitness();
@@ -847,7 +848,7 @@
genotype1_mod_genome[line_no].SetOp(cur_inst);
}
- genotype2->Recalculate();
+ genotype2->Recalculate(m_testcpu);
if (genotype2->GetFitness() == 0) {
for (int line_no = 0; line_no < num_lines; ++ line_no) {
increased_info += genotype1_info[line_no];
@@ -867,7 +868,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();
+ test_genotype.Recalculate(m_testcpu);
// Ajust fitness ...
if (test_genotype.GetFitness() <= genotype2_base_fitness) {
test_fitness[mod_inst] = test_genotype.GetFitness();
@@ -1601,17 +1602,9 @@
}
}
- bool backupUsage;
- tArray<double> backupResources;
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+ testcpu->SetUseResources(useResources);
- if(useResources) {
- // Backup test cpu data
- backupUsage = m_world->GetTestCPU().UseResources();
- backupResources = m_world->GetTestCPU().GetResources();
-
- m_world->GetTestCPU().UseResources() = true;
- }
-
tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
cAnalyzeGenotype * genotype = NULL;
while ((genotype = batch_it.Next()) != NULL) {
@@ -1632,19 +1625,13 @@
test_info.TestThreads();
test_info.SetTraceExecution(&trace_printer);
- m_world->GetTestCPU().TestGenome(test_info, genotype->GetGenome());
+ testcpu->TestGenome(test_info, genotype->GetGenome());
if (verbose >= nAnalyze::VERBOSE_ON) cout << " Tracing: " << filename << endl;
m_world->GetDataFileManager().Remove(filename);
}
- if(useResources) {
- // Set the test cpu back to the state it was in before we messed with it
- m_world->GetTestCPU().UseResources() = backupUsage;
- m_world->GetTestCPU().SetupResourceArray(backupResources);
- }
-
- return;
+ delete testcpu;
}
void cAnalyze::CommandPrintTasks(cString cur_string)
@@ -2636,11 +2623,10 @@
}
///////////////////////
- // Backup test CPU data
- bool backupUsage = m_world->GetTestCPU().UseResources();
- tArray<double> backupResources(m_world->GetTestCPU().GetResources());
- m_world->GetTestCPU().UseResources() = true;
- FillResources(update);
+ // Create Test CPU
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+ testcpu->SetUseResources(true);
+ FillResources(testcpu, update);
///////////////////////////////////////////////////////////////////////
// Choose the first n most abundant genotypes and put them in community
@@ -2763,7 +2749,7 @@
genotype = sorted_community[i];
// Skip the dead organisms
- genotype->Recalculate();
+ genotype->Recalculate(testcpu);
if (genotype->GetFitness() == 0) {
continue;
}
@@ -2867,7 +2853,7 @@
mod_genome = base_genome;
mod_genome[line].SetOp(given_inst);
cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
- test_genotype.Recalculate();
+ test_genotype.Recalculate(testcpu);
// Only when given inst make the genotype alive
if (test_genotype.GetFitness() > 0) {
@@ -2890,7 +2876,7 @@
vector< vector<bool> > neutral_mut(length_genome, one_line_neutral);
vector< vector<bool> > alive_mut(length_genome, one_line_neutral);
- genotype->Recalculate();
+ genotype->Recalculate(testcpu);
double base_fitness = genotype->GetFitness();
cout << base_fitness << endl;
const cGenome & base_genome = genotype->GetGenome();
@@ -2902,7 +2888,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();
+ test_genotype.Recalculate(testcpu);
if (test_genotype.GetFitness() >= base_fitness) {
neutral_mut[line][mod_inst] = true;
}
@@ -3052,9 +3038,7 @@
}
- // Set the test CPU back to the state it was
- m_world->GetTestCPU().UseResources() = backupUsage;
- m_world->GetTestCPU().SetupResourceArray(backupResources);
+ delete testcpu;
m_world->GetDataFileManager().Remove(filename);
return;
@@ -3104,10 +3088,9 @@
///////////////////////
// Backup test CPU data
- bool backupUsage = m_world->GetTestCPU().UseResources();
- tArray<double> backupResources(m_world->GetTestCPU().GetResources());
- m_world->GetTestCPU().UseResources() = true;
- FillResources(update);
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+ testcpu->SetUseResources(true);
+ FillResources(testcpu, update);
vector<cAnalyzeGenotype *> community;
@@ -3133,7 +3116,7 @@
m_world->GetDataFileManager().Remove(filename);
return;
}
- genotype->Recalculate();
+ genotype->Recalculate(testcpu);
int num_tasks = genotype->GetNumTasks();
vector< vector<cAnalyzeGenotype *> > genotype_class(num_tasks);
do {
@@ -3191,7 +3174,7 @@
cout << "Test point mutation for genotype " << genotype->GetID() << endl;
tMatrix<double> prob(length_genome, num_insts);
- genotype->Recalculate();
+ genotype->Recalculate(testcpu);
double base_fitness = genotype->GetFitness();
const cGenome & base_genome = genotype->GetGenome();
cGenome mod_genome(base_genome);
@@ -3203,7 +3186,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();
+ test_genotype.Recalculate(testcpu);
if (test_genotype.GetFitness() >= base_fitness) {
prob[line][mod_inst] = 1.0;
num_neutral ++;
@@ -3262,7 +3245,7 @@
int num_cpus = genotype->GetNumCPUs();
total_cpus += num_cpus;
cpx_fp << num_cpus << " " << total_cpus << " ";
- genotype->Recalculate();
+ genotype->Recalculate(testcpu);
genotype->PrintTasks(cpx_fp, 0, -1);
cpx_fp << endl;
@@ -3277,7 +3260,7 @@
}
// Skip the dead organisms
- genotype->Recalculate();
+ genotype->Recalculate(testcpu);
cout << genotype->GetID() << " " << genotype->GetFitness() << endl;
if (genotype->GetFitness() == 0) {
continue;
@@ -3388,11 +3371,9 @@
given_genotypes.push_back(genotype);
}
+
+ delete testcpu;
- // Set the test CPU back to the state it was
- m_world->GetTestCPU().UseResources() = backupUsage;
- m_world->GetTestCPU().SetupResourceArray(backupResources);
-
m_world->GetDataFileManager().Remove(filename);
return;
}
@@ -3606,6 +3587,9 @@
int fail_count = 0;
int match_fail_count = 0;
+ // Create a Test CPU
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
// Loop through all of the tests, picking random organisms each time and
// performing a random cross test.
cAnalyzeGenotype * genotype2 = NULL;
@@ -3665,12 +3649,13 @@
cCPUTestInfo test_info;
// Run each side, and determine viability...
- m_world->GetTestCPU().TestGenome(test_info, test_genome0);
+ testcpu->TestGenome(test_info, test_genome0);
if( test_info.IsViable() == false ) {
fail_count++;
if (same_mate_id == true) match_fail_count++;
}
}
+ delete testcpu;
// Do some calculations on the sizes of the mate groups...
const int num_mate_groups = mate_id_counts.GetSize();
@@ -3833,7 +3818,7 @@
}
// Collect basic state before and after the mutations...
- genotype->Recalculate();
+ genotype->Recalculate(m_testcpu);
double start_complexity = genotype->GetKO_Complexity();
double start_fitness = genotype->GetFitness();
int start_length = genotype->GetLength();
@@ -3842,7 +3827,7 @@
const tArray< tArray<int> >& start_KO_task_counts = genotype->GetKO_TaskCounts();
cAnalyzeGenotype new_genotype(m_world, mod_genome, inst_set);
- new_genotype.Recalculate();
+ new_genotype.Recalculate(m_testcpu);
double end_complexity = new_genotype.GetKO_Complexity();
double end_fitness = new_genotype.GetFitness();
int end_length = new_genotype.GetLength();
@@ -4036,7 +4021,7 @@
if (verbose >= nAnalyze::VERBOSE_ON) cout << " Knockout: " << genotype->GetName() << endl;
// Calculate the stats for the genotype we're working with...
- genotype->Recalculate();
+ genotype->Recalculate(m_testcpu);
const double base_fitness = genotype->GetFitness();
const int max_line = genotype->GetLength();
@@ -4055,7 +4040,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();
+ ko_genotype.Recalculate(m_testcpu);
double ko_fitness = ko_genotype.GetFitness();
if (ko_fitness == 0.0) {
@@ -4087,7 +4072,7 @@
mod_genome[line1] = null_inst;
mod_genome[line2] = null_inst;
cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
- ko_genotype.Recalculate();
+ ko_genotype.Recalculate(m_testcpu);
double ko_fitness = ko_genotype.GetFitness();
@@ -4287,7 +4272,7 @@
}
// Calculate the stats for the genotype we're working with...
- genotype->Recalculate();
+ genotype->Recalculate(m_testcpu);
// Headers...
if (file_type == FILE_TYPE_TEXT) {
@@ -4406,7 +4391,7 @@
mod_genome[line_num] = null_inst;
cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
- test_genotype.Recalculate();
+ test_genotype.Recalculate(m_testcpu);
if (file_type == FILE_TYPE_HTML) fp << "<tr><td align=right>";
fp << (line_num + 1) << " ";
@@ -4592,7 +4577,7 @@
if (verbose >= nAnalyze::VERBOSE_ON) cout << " Mapping " << genotype->GetName() << endl;
// Calculate the stats for the genotype we're working with...
- genotype->Recalculate();
+ genotype->Recalculate(m_testcpu);
// Check if the organism does any tasks.
int does_tasks = 0;
@@ -4663,7 +4648,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();
+ test_genotype.Recalculate(m_testcpu);
// Print the individual columns...
output_it.Reset();
@@ -4879,7 +4864,7 @@
const int base_length = genotype->GetLength();
const cGenome & base_genome = genotype->GetGenome();
cGenome mod_genome(base_genome);
- genotype->Recalculate();
+ genotype->Recalculate(m_testcpu);
tMatrix<bool> task_matrix(num_traits, base_length);
tArray<int> num_inst(num_traits); // Number of instructions for each task
@@ -4895,7 +4880,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();
+ test_genotype.Recalculate(m_testcpu);
// Loop through the individual traits
output_it.Reset();
@@ -5039,7 +5024,7 @@
ofstream& fp = m_world->GetDataFileOFStream(filename);
// Calculate the stats for the genotype we're working with...
- genotype->Recalculate();
+ genotype->Recalculate(m_testcpu);
const double base_fitness = genotype->GetFitness();
const int num_insts = inst_set.GetSize();
@@ -5152,7 +5137,7 @@
else {
mod_genome[line_num].SetOp(mod_inst);
cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
- test_genotype.Recalculate();
+ test_genotype.Recalculate(m_testcpu);
const double test_fitness = test_genotype.GetFitness() / base_fitness;
row_fitness += test_fitness;
total_fitness += test_fitness;
@@ -5188,7 +5173,7 @@
// Column: Knockout
mod_genome[line_num] = null_inst;
cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
- test_genotype.Recalculate();
+ test_genotype.Recalculate(m_testcpu);
const double test_fitness = test_genotype.GetFitness() / base_fitness;
col_fitness[num_insts] += test_fitness;
@@ -5464,6 +5449,8 @@
int total_fail = 0;
int total_count = 0;
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
tListIterator<cAnalyzeGenotype> list1_it(batch[batch1].List());
tListIterator<cAnalyzeGenotype> list2_it(batch[batch2].List());
@@ -5525,10 +5512,10 @@
// Run each side, and determine viability...
cCPUTestInfo test_info;
- m_world->GetTestCPU().TestGenome(test_info, test_genome0);
+ testcpu->TestGenome(test_info, test_genome0);
cross1_viable = test_info.IsViable();
- m_world->GetTestCPU().TestGenome(test_info, test_genome1);
+ testcpu->TestGenome(test_info, test_genome1);
cross2_viable = test_info.IsViable();
if (cross1_viable == false) fail_count++;
@@ -5540,6 +5527,9 @@
}
}
}
+
+ delete testcpu;
+
// Calculate the final answer
double ave_dist = (double) total_fail / (double) total_count;
cout << " ave distance = " << ave_dist
@@ -6124,6 +6114,8 @@
test_count[i] = 0;
}
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
// Loop through all of the combos...
const int combo_step = total_combos / 79;
for (int combo_id = 0; combo_id < total_combos; combo_id++) {
@@ -6146,11 +6138,11 @@
cGenome test_genome(test_sequence);
cCPUTestInfo test_info;
test_info.TestThreads();
- m_world->GetTestCPU().TestGenome(test_info, test_genome);
+ testcpu->TestGenome(test_info, test_genome);
const double fitness = test_info.GetGenotypeFitness();
//cAnalyzeGenotype test_genotype(test_sequence);
- //test_genotype.Recalculate();
+ //test_genotype.Recalculate(testcpu);
//const double fitness = test_genotype.GetFitness();
total_fitness[diff_count] += fitness;
@@ -6171,7 +6163,7 @@
for (int i = 0; i <= total_diffs; i++) {
cAnalyzeGenotype max_genotype(m_world, max_sequence[i], inst_set);
- max_genotype.Recalculate();
+ max_genotype.Recalculate(testcpu);
fp << i << " " // 1
<< test_count[i] << " " // 2
<< total_fitness[i] / (double) test_count[i] << " " // 3
@@ -6191,6 +6183,8 @@
delete [] max_fitness;
delete [] max_sequence;
delete [] test_count;
+
+ delete testcpu;
}
// If we can't scan through all combos, give wanring.
else {
@@ -6464,6 +6458,8 @@
// Open the output file...
ofstream& fp = m_world->GetDataFileOFStream(filename);
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
// Loop through all of the genotypes again, testing mutation reversions.
cAnalyzeGenotype * prev_genotype = batch_it.Next();
while ((genotype = batch_it.Next()) != NULL) {
@@ -6483,14 +6479,14 @@
cGenome test_genome = genotype->GetGenome();
cCPUTestInfo test_info;
- m_world->GetTestCPU().TestGenome(test_info, test_genome);
+ testcpu->TestGenome(test_info, test_genome);
const double base_fitness = test_info.GetGenotypeFitness();
for (int i = 0; i < size; i++) {
if (prev_inst[i] == -1) num_static++;
else {
test_genome[i].SetOp(prev_inst[i]);
- m_world->GetTestCPU().TestGenome(test_info, test_genome);
+ testcpu->TestGenome(test_info, test_genome);
const double cur_fitness = test_info.GetGenotypeFitness();
if (cur_fitness > base_fitness) num_detrimental++;
else if (cur_fitness < base_fitness) num_beneficial++;
@@ -6508,6 +6504,8 @@
prev_genotype = genotype;
}
+
+ delete testcpu;
}
void cAnalyze::AnalyzeComplexity(cString cur_string)
@@ -6552,19 +6550,9 @@
}
}
- // These are used to backup the state of the environment and resource_count
- // for the test cpu, and will be reset to their original values at the end
- bool backupUsage;
- tArray<double> backupResources;
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+ testcpu->SetUseResources(useResources);
- if(useResources) {
- // Backup test cpu data
- backupUsage = m_world->GetTestCPU().UseResources();
- backupResources = m_world->GetTestCPU().GetResources();
-
- m_world->GetTestCPU().UseResources() = true;
- }
-
///////////////////////////////////////////////////////
// Loop through all of the genotypes in this batch...
@@ -6594,11 +6582,11 @@
int updateBorn = -1;
if(useResources) {
updateBorn = genotype->GetUpdateBorn();
- FillResources(updateBorn);
+ FillResources(testcpu, updateBorn);
}
// Calculate the stats for the genotype we're working with ...
- genotype->Recalculate();
+ genotype->Recalculate(testcpu);
cout << genotype->GetFitness() << endl;
const int num_insts = inst_set.GetSize();
const int max_line = genotype->GetLength();
@@ -6619,7 +6607,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();
+ test_genotype.Recalculate(testcpu);
test_fitness[mod_inst] = test_genotype.GetFitness();
}
@@ -6699,14 +6687,8 @@
}
m_world->GetDataFileManager().Remove(lineage_filename);
-
- if(useResources) {
- // Set the test cpu back to the state it was in before we messed with it
- m_world->GetTestCPU().UseResources() = backupUsage;
- m_world->GetTestCPU().SetupResourceArray(backupResources);
- }
-
- return;
+
+ delete testcpu;
}
void cAnalyze::AnalyzePopComplexity(cString cur_string)
@@ -6922,17 +6904,9 @@
update = cur_string.PopWord().AsInt();
}
- bool backupUsage;
- tArray<double> backupResources;
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+ testcpu->SetUseResources(useResources);
- if(useResources) {
- // Backup test cpu data
- backupUsage = m_world->GetTestCPU().UseResources();
- backupResources = m_world->GetTestCPU().GetResources();
-
- m_world->GetTestCPU().UseResources() = true;
- }
-
if (verbose >= nAnalyze::VERBOSE_ON) {
cout << "Running batch " << cur_batch << " through test CPUs..." << endl;
} else cout << "Running through test CPUs..." << endl;
@@ -6943,36 +6917,31 @@
}
if (useResources && update > -1) {
- FillResources(update);
+ FillResources(testcpu, update);
}
tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
cAnalyzeGenotype * genotype = NULL;
cAnalyzeGenotype * last_genotype = NULL;
while ((genotype = batch_it.Next()) != NULL) {
-
// If use resources, load proper resource according to update_born
if(useResources && update == -1) {
int updateBorn = -1;
updateBorn = genotype->GetUpdateBorn();
- FillResources(updateBorn);
+ FillResources(testcpu, updateBorn);
}
// 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(last_genotype);
+ if (last_genotype != NULL && genotype->GetParentID() == last_genotype->GetID()) {
+ genotype->Recalculate(testcpu, last_genotype);
+ } else {
+ genotype->Recalculate(testcpu);
}
- else genotype->Recalculate();
last_genotype = genotype;
}
- if(useResources) {
- // Set the test cpu back to the state it was in before we messed with it
- m_world->GetTestCPU().UseResources() = backupUsage;
- m_world->GetTestCPU().SetupResourceArray(backupResources);
- }
+ delete testcpu;
return;
}
@@ -7079,19 +7048,19 @@
RunInteractive();
}
-void cAnalyze::PrintTestCPUResources(cString cur_string)
-{
- cout << "TestCPU is using resources: ";
- cout << m_world->GetTestCPU().UseResources() << endl;
- cout << "Resources currently in TestCPU: ";
- const tArray<double> &quantity = m_world->GetTestCPU().GetResources();
- for(int i=0; i<quantity.GetSize(); i++) {
- cout << quantity.ElementAt(i) << " ";
- }
- cout << endl;
-
- return;
-}
+//void cAnalyze::PrintTestCPUResources(cString cur_string)
+//{
+// cout << "TestCPU is using resources: ";
+// cout << m_world->GetTestCPU().GetUseResources() << endl;
+// cout << "Resources currently in TestCPU: ";
+// const tArray<double> &quantity = m_world->GetTestCPU().GetResources();
+// for(int i=0; i<quantity.GetSize(); i++) {
+// cout << quantity.ElementAt(i) << " ";
+// }
+// cout << endl;
+//
+// return;
+//}
void cAnalyze::FunctionCreate(cString cur_string,
tList<cAnalyzeCommand> & clist)
@@ -7829,7 +7798,7 @@
AddLibraryDef("INCLUDE", &cAnalyze::IncludeFile);
AddLibraryDef("SYSTEM", &cAnalyze::CommandSystem);
AddLibraryDef("INTERACTIVE", &cAnalyze::CommandInteractive);
- AddLibraryDef("PRINT_TEST_CPU_RESOURCES", &cAnalyze::PrintTestCPUResources);
+// AddLibraryDef("PRINT_TEST_CPU_RESOURCES", &cAnalyze::PrintTestCPUResources);
AddLibraryDef("LANDSCAPE_PREGEN", &cAnalyze::CommandLandscapePreGen);
// Functions...
Modified: development/source/analyze/cAnalyze.h
===================================================================
--- development/source/analyze/cAnalyze.h 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/analyze/cAnalyze.h 2006-03-11 18:06:56 UTC (rev 502)
@@ -59,6 +59,7 @@
template <class T> class tDataEntryCommand;
template <class T> class tListIterator;
class cEnvironment;
+class cTestCPU;
class cWorld;
class cAnalyze {
@@ -74,6 +75,7 @@
cWorld* m_world;
cInstSet& inst_set;
+ cTestCPU* m_testcpu;
// This is the storage for the resource information from resource.dat. It
// is a pair of the update and a vector of the resource concentrations
@@ -227,14 +229,14 @@
void IncludeFile(cString cur_string);
void CommandSystem(cString cur_string);
void CommandInteractive(cString cur_string);
- void PrintTestCPUResources(cString cur_string);
+// void PrintTestCPUResources(cString cur_string);
void CommandLandscapePreGen(cString cur_string);
// Functions...
void FunctionCreate(cString cur_string, tList<cAnalyzeCommand> & clist);
// Looks up the resource concentrations that are the closest to the
// given update and then fill in those concentrations into the environment.
- void FillResources(int update);
+ void FillResources(cTestCPU* testcpu, int update);
// Analyze the entropy of genotype under default environment
double AnalyzeEntropy(cAnalyzeGenotype * genotype, double mut_rate);
// Analyze the entropy of child given parent and default environment
Modified: development/source/analyze/cAnalyzeGenotype.cc
===================================================================
--- development/source/analyze/cAnalyzeGenotype.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/analyze/cAnalyzeGenotype.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -11,6 +11,7 @@
#include "cAnalyzeGenotype.h"
#include "cCPUTestInfo.h"
+#include "cHardwareManager.h"
#include "cInstSet.h"
#include "cLandscape.h"
#include "cOrganism.h"
@@ -177,17 +178,20 @@
return;
}
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
// Calculate the base fitness for the genotype we're working with...
// (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();
+ base_genotype.Recalculate(testcpu);
double base_fitness = base_genotype.GetFitness();
const tArray<int> base_task_counts( base_genotype.GetTaskCounts() );
// If the base fitness is 0, the organism is dead and has no complexity.
if (base_fitness == 0.0) {
knockout_stats->neut_count = length;
+ delete testcpu;
return;
}
@@ -221,7 +225,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();
+ ko_genotype.Recalculate(testcpu);
if (check_chart == true) {
const tArray<int> ko_task_counts( ko_genotype.GetTaskCounts() );
knockout_stats->task_counts[line_num] = ko_task_counts;
@@ -250,7 +254,10 @@
// Only continue from here if we are looking at all pairs of knockouts
// as well.
- if (check_pairs == false) return;
+ if (check_pairs == false) {
+ delete testcpu;
+ return;
+ }
tArray<int> ko_pair_effect(ko_effect);
for (int line1 = 0; line1 < length; line1++) {
@@ -278,7 +285,7 @@
mod_genome[line1] = null_inst;
mod_genome[line2] = null_inst;
cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
- ko_genotype.Recalculate();
+ ko_genotype.Recalculate(testcpu);
double ko_fitness = ko_genotype.GetFitness();
@@ -314,6 +321,7 @@
}
knockout_stats->has_pair_info = true;
+ delete testcpu;
}
void cAnalyzeGenotype::CalcLandscape() const
@@ -331,7 +339,7 @@
landscape_stats->ave_fitness = landscape.GetAveFitness();
}
-void cAnalyzeGenotype::Recalculate(cAnalyzeGenotype * parent_genotype)
+void cAnalyzeGenotype::Recalculate(cTestCPU* testcpu, cAnalyzeGenotype* parent_genotype)
{
// Build the test info for printing.
cCPUTestInfo test_info;
@@ -346,15 +354,15 @@
cInstSet env_inst_set_backup = m_world->GetHardwareManager().GetInstSet();
m_world->GetHardwareManager().GetInstSet() = inst_set;
- m_world->GetTestCPU().TestGenome(test_info, genome);
+ testcpu->TestGenome(test_info, genome);
// Restore the instruction set
m_world->GetHardwareManager().GetInstSet() = env_inst_set_backup;
viable = test_info.IsViable();
- cOrganism * test_organism = test_info.GetTestOrganism();
- cPhenotype & test_phenotype = test_organism->GetPhenotype();
+ cOrganism* test_organism = test_info.GetTestOrganism();
+ cPhenotype& test_phenotype = test_organism->GetPhenotype();
length = test_organism->GetGenome().GetSize();
copy_length = test_phenotype.GetCopiedSize();
Modified: development/source/analyze/cAnalyzeGenotype.h
===================================================================
--- development/source/analyze/cAnalyzeGenotype.h 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/analyze/cAnalyzeGenotype.h 2006-03-11 18:06:56 UTC (rev 502)
@@ -35,6 +35,7 @@
// cAnalyzeGenotype : Collection of information about loaded genotypes
class cInstSet;
+class cTestCPU;
class cWorld;
class cAnalyzeGenotype {
@@ -157,8 +158,8 @@
const cStringList & GetSpecialArgs() { return special_args; }
void SetSpecialArgs(const cStringList & _args) { special_args = _args; }
- void Recalculate(cAnalyzeGenotype * parent_genotype=NULL);
- void PrintTasks(std::ofstream& fp, int min_task=0, int max_task=-1);
+ void Recalculate(cTestCPU* testcpu, cAnalyzeGenotype* parent_genotype = NULL);
+ void PrintTasks(std::ofstream& fp, int min_task = 0, int max_task = -1);
void CalcLandscape() const;
// Set...
Modified: development/source/analyze/cAnalyzeUtil.cc
===================================================================
--- development/source/analyze/cAnalyzeUtil.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/analyze/cAnalyzeUtil.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -41,8 +41,10 @@
void cAnalyzeUtil::TestGenome(cWorld* world, const cGenome & genome, cInstSet & inst_set,
ofstream& fp, int update)
{
+ cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
- world->GetTestCPU().TestGenome(test_info, genome);
+ testcpu->TestGenome(test_info, genome);
+ delete testcpu;
cPhenotype &colony_phenotype = test_info.GetColonyOrganism()->GetPhenotype();
fp << update << " " // 1
@@ -61,6 +63,7 @@
const cInstSet & inst_set, const cGenome & in_genome,
int num_trials, int update)
{
+ cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
const cInstruction inst_none = inst_set.GetInst("instruction_none");
@@ -83,7 +86,7 @@
for (int j = 0; j < num_nops; j++) genome.Insert(ins_pos, inst_none);
// Test the genome and output stats
- if (world->GetTestCPU().TestGenome(test_info, genome)){ // Daughter viable...
+ if (testcpu->TestGenome(test_info, genome)){ // Daughter viable...
num_viable++;
const double child_size =
test_info.GetColonyOrganism()->GetGenome().GetSize();
@@ -93,6 +96,8 @@
}
} // for num_trials
+ delete testcpu;
+
fp << update << " "
<< (double) num_viable / num_trials << " "
<< (double) num_new_size / num_trials << " "
@@ -318,10 +323,13 @@
<< endl;
}
else {
+ cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
- world->GetTestCPU().TestGenome(test_info, con_genome);
- cPhenotype & colony_phenotype =
- test_info.GetColonyOrganism()->GetPhenotype();
+ testcpu->TestGenome(test_info, con_genome);
+ delete testcpu;
+
+ cPhenotype& colony_phenotype = test_info.GetColonyOrganism()->GetPhenotype();
+
fp << update << " " // 1
<< colony_phenotype.GetMerit() << " " // 2
<< colony_phenotype.GetGestationTime() << " " // 3
@@ -467,6 +475,8 @@
double max_fitness = -1; // we set this to -1, so that even 0 is larger...
cGenotype * max_f_genotype = NULL;
+ cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
+
for (int i = 0; i < pop->GetSize(); i++) {
if (pop->GetCell(i).IsOccupied() == false) continue; // One use organisms.
@@ -474,7 +484,7 @@
cGenotype * genotype = organism->GetGenotype();
cCPUTestInfo test_info;
- world->GetTestCPU().TestGenome( test_info, genotype->GetGenome() );
+ testcpu->TestGenome( test_info, genotype->GetGenome() );
// We calculate the fitness based on the current merit,
// but with the true gestation time. Also, we set the fitness
// to zero if the creature is not viable.
@@ -507,6 +517,8 @@
}
}
+ delete testcpu;
+
// determine the name of the maximum fitness genotype
cString max_f_name;
if ( max_f_genotype->GetThreshold() )
@@ -683,13 +695,15 @@
cPopulation* pop = &world->GetPopulation();
fp << "# (1) cell number\n# (2) number of rewarded tasks done so far\n# (3) total number of tasks done so far\n# (4) same as 2, but right before divide\n# (5) same as 3, but right before divide\n# (6) same as 2, but for parent\n# (7) same as 3, but for parent\n# (8) genotype fitness\n# (9) genotype name" << endl;
+ cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
+
for (int i = 0; i < pop->GetSize(); i++) {
if (pop->GetCell(i).IsOccupied() == false) continue;
cOrganism * organism = pop->GetCell(i).GetOrganism();
// create a test-cpu for the current creature
cCPUTestInfo test_info;
- world->GetTestCPU().TestGenome( test_info, organism->GetGenome() );
+ testcpu->TestGenome( test_info, organism->GetGenome() );
cPhenotype & test_phenotype = test_info.GetTestOrganism()->GetPhenotype();
cPhenotype & phenotype = organism->GetPhenotype();
@@ -730,11 +744,15 @@
<< organism->GetGenotype()->GetName() << " " // 9 genotype name
<< endl;
}
+
+ delete testcpu;
}
void cAnalyzeUtil::TaskGrid(cWorld* world, ofstream& fp)
{
cPopulation* pop = &world->GetPopulation();
+ cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
+
for (int i = 0; i < pop->GetWorldX(); i++) {
for (int j = 0; j < pop->GetWorldY(); j++) {
int task_sum = 0;
@@ -742,7 +760,7 @@
if (pop->GetCell(cell_num).IsOccupied() == true) {
cOrganism * organism = pop->GetCell(cell_num).GetOrganism();
cCPUTestInfo test_info;
- world->GetTestCPU().TestGenome( test_info, organism->GetGenome() );
+ testcpu->TestGenome( test_info, organism->GetGenome() );
cPhenotype & test_phenotype = test_info.GetTestOrganism()->GetPhenotype();
int num_tasks = world->GetEnvironment().GetTaskLib().GetSize();
for (int k = 0; k < num_tasks; k++) {
@@ -755,6 +773,8 @@
}
fp << endl;
}
+
+ delete testcpu;
}
/**
Modified: development/source/classification/cGenotype.cc
===================================================================
--- development/source/classification/cGenotype.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/classification/cGenotype.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -12,6 +12,7 @@
#include "cCPUTestInfo.h"
#include "cGenomeUtil.h"
+#include "cHardwareManager.h"
#include "cMerit.h"
#include "cOrganism.h"
#include "cPhenotype.h"
@@ -198,9 +199,11 @@
void cGenotype::CalcTestStats() const
{
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
- m_world->GetTestCPU().TestGenome(test_info, genome);
+ testcpu->TestGenome(test_info, genome);
test_data.is_viable = test_info.IsViable();
+ delete testcpu;
// Setup all possible test values.
cPhenotype & phenotype = test_info.GetTestOrganism()->GetPhenotype();
Modified: development/source/classification/cSpecies.cc
===================================================================
--- development/source/classification/cSpecies.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/classification/cSpecies.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -14,6 +14,7 @@
#include "functions.h"
#include "cGenotype.h"
#include "cGenomeUtil.h"
+#include "cHardwareManager.h"
#include "cTestCPU.h"
#include "cWorld.h"
@@ -75,12 +76,13 @@
int cSpecies::Compare(const cGenome & test_genome, int max_fail_count)
{
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
// First, make some phenotypic comparisons between organisms.
// For now, just check that they both copy-true.
- m_world->GetTestCPU().TestGenome(test_info, test_genome);
+ testcpu->TestGenome(test_info, test_genome);
// If the organisms aren't viable, return a -1...
if (test_info.IsViable() == false) {
@@ -114,10 +116,10 @@
cross_genome2[i] = genome[i];
// Run each side, and determine viability...
- m_world->GetTestCPU().TestGenome(test_info, cross_genome1);
+ testcpu->TestGenome(test_info, cross_genome1);
cross1_viable = test_info.IsViable();
- m_world->GetTestCPU().TestGenome(test_info, cross_genome2);
+ testcpu->TestGenome(test_info, cross_genome2);
cross2_viable = test_info.IsViable();
}
@@ -127,6 +129,7 @@
if (max_fail_count != -1 && fail_count > max_fail_count) break;
}
+ delete testcpu;
return fail_count;
}
Modified: development/source/cpu/cHardware4Stack.cc
===================================================================
--- development/source/cpu/cHardware4Stack.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/cpu/cHardware4Stack.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -15,6 +15,7 @@
#include "cGenomeUtil.h"
#include "cInstLibBase.h"
#include "cInstSet.h"
+#include "cHardwareManager.h"
#include "cHardwareTracer.h"
#include "cHardwareTracer_4Stack.h"
#include "cMutation.h"
@@ -1544,10 +1545,12 @@
const double neut_min = parent_fitness * nHardware::FITNESS_NEUTRAL_MIN;
const double neut_max = parent_fitness * nHardware::FITNESS_NEUTRAL_MAX;
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
test_info.UseRandomInputs();
- m_world->GetTestCPU().TestGenome(test_info, organism->ChildGenome());
+ testcpu->TestGenome(test_info, organism->ChildGenome());
const double child_fitness = test_info.GetGenotypeFitness();
+ delete testcpu;
bool revert = false;
bool sterilize = false;
Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/cpu/cHardwareCPU.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -15,6 +15,7 @@
#include "functions.h"
#include "cGenomeUtil.h"
#include "cGenotype.h"
+#include "cHardwareManager.h"
#include "cHardwareTracer.h"
#include "cHardwareTracer_CPU.h"
#include "cInstLibCPU.h"
@@ -1669,10 +1670,12 @@
const double neut_min = parent_fitness * nHardware::FITNESS_NEUTRAL_MIN;
const double neut_max = parent_fitness * nHardware::FITNESS_NEUTRAL_MAX;
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
test_info.UseRandomInputs();
- m_world->GetTestCPU().TestGenome(test_info, organism->ChildGenome());
+ testcpu->TestGenome(test_info, organism->ChildGenome());
const double child_fitness = test_info.GetGenotypeFitness();
+ delete testcpu;
bool revert = false;
bool sterilize = false;
Modified: development/source/cpu/cHardwareManager.cc
===================================================================
--- development/source/cpu/cHardwareManager.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/cpu/cHardwareManager.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -19,7 +19,8 @@
#include "cWorldDriver.h"
#include "tDictionary.h"
-cHardwareManager::cHardwareManager(cWorld* world) : m_world(world), m_inst_set(world), m_type(world->GetConfig().HARDWARE_TYPE.Get())
+cHardwareManager::cHardwareManager(cWorld* world)
+: m_world(world), m_inst_set(world), m_type(world->GetConfig().HARDWARE_TYPE.Get()), m_testres(world)
{
LoadInstSet(world->GetConfig().INST_SET.Get());
}
Modified: development/source/cpu/cHardwareManager.h
===================================================================
--- development/source/cpu/cHardwareManager.h 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/cpu/cHardwareManager.h 2006-03-11 18:06:56 UTC (rev 502)
@@ -11,8 +11,11 @@
#define cHardwareManager_h
#ifndef cInstSet_h
-#include "cInstSet.h";
+#include "cInstSet.h"
#endif
+#ifndef cTestCPU_h
+#include "cTestCPU.h"
+#endif
class cOrganism;
class cHardwareBase;
@@ -24,8 +27,8 @@
cWorld* m_world;
cInstSet m_inst_set;
int m_type;
+ cTestResources m_testres;
-
cHardwareManager(); // @not_implemented
cHardwareManager(const cHardwareManager&); // @not_implemented
cHardwareManager& operator=(const cHardwareManager&); // @not_implemented
@@ -35,6 +38,7 @@
~cHardwareManager() { ; }
cHardwareBase* Create(cOrganism* in_org);
+ cTestCPU* CreateTestCPU() { return new cTestCPU(m_world, &m_testres); }
void LoadInstSet(cString filename);
Modified: development/source/cpu/cHardwareSMT.cc
===================================================================
--- development/source/cpu/cHardwareSMT.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/cpu/cHardwareSMT.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -14,6 +14,7 @@
#include "cGenomeUtil.h"
#include "cInstLibBase.h"
#include "cInstSet.h"
+#include "cHardwareManager.h"
#include "cHardwareTracer.h"
#include "cHardwareTracer_SMT.h"
#include "cMutation.h"
@@ -1369,10 +1370,12 @@
const double neut_min = parent_fitness * nHardware::FITNESS_NEUTRAL_MIN;
const double neut_max = parent_fitness * nHardware::FITNESS_NEUTRAL_MAX;
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
test_info.UseRandomInputs();
- m_world->GetTestCPU().TestGenome(test_info, organism->ChildGenome());
+ testcpu->TestGenome(test_info, organism->ChildGenome());
const double child_fitness = test_info.GetGenotypeFitness();
+ delete testcpu;
bool revert = false;
bool sterilize = false;
Modified: development/source/cpu/cTestCPU.cc
===================================================================
--- development/source/cpu/cTestCPU.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/cpu/cTestCPU.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -32,10 +32,10 @@
using namespace std;
-void cTestCPU::SetupResources(void)
+cTestResources::cTestResources(cWorld* world)
{
// Setup the resources...
- const cResourceLib & resource_lib = m_world->GetEnvironment().GetResourceLib();
+ const cResourceLib& resource_lib = world->GetEnvironment().GetResourceLib();
assert(resource_lib.GetSize() >= 0);
resource_count.SetSize(resource_lib.GetSize());
@@ -49,7 +49,7 @@
resource_count.ResizeSpatialGrids(1, 1);
for (int i = 0; i < resource_lib.GetSize(); i++) {
- cResource * res = resource_lib.GetResource(i);
+ cResource* res = resource_lib.GetResource(i);
const double decay = 1.0 - res->GetOutflow();
resource_count.Setup(i, res->GetName(), res->GetInitial(),
res->GetInflow(), decay,
@@ -61,21 +61,31 @@
res->GetOutflowX2(), res->GetOutflowY1(),
res->GetOutflowY2() );
}
-
- return;
}
+
void cTestCPU::SetupResourceArray(const tArray<double> &resources)
{
- for(int i=0; i<d_resources.GetSize(); i++) {
+ if (!m_localres) m_res = new cTestResources(*m_res);
+
+ for(int i = 0; i < m_res->d_resources.GetSize(); i++) {
if(i >= resources.GetSize()) {
- d_resources[i] = 0.0;
+ m_res->d_resources[i] = 0.0;
} else {
- d_resources[i] = resources[i];
+ m_res->d_resources[i] = resources[i];
}
}
}
+void cTestCPU::SetUseResources(bool use)
+{
+ if (m_res->d_useResources != use) {
+ if (!m_localres) m_res = new cTestResources(*m_res);
+ m_res->d_useResources = use;
+ }
+}
+
+
// NOTE: This method assumes that the organism is a fresh creation.
bool cTestCPU::ProcessGestation(cCPUTestInfo & test_info, int cur_depth)
{
Modified: development/source/cpu/cTestCPU.h
===================================================================
--- development/source/cpu/cTestCPU.h 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/cpu/cTestCPU.h 2006-03-11 18:06:56 UTC (rev 502)
@@ -29,6 +29,19 @@
class cGenome;
class cWorld;
+class cTestResources
+{
+ friend class cTestCPU;
+private:
+ cResourceCount resource_count;
+ bool d_useResources;
+ tArray<double> d_emptyDoubleArray;
+ tArray<double> d_resources;
+
+public:
+ cTestResources(cWorld* world);
+};
+
class cTestCPU
{
private:
@@ -37,12 +50,10 @@
tArray<int> receive_array;
int cur_input;
int cur_receive;
+
+ cTestResources* m_res;
+ bool m_localres;
- cResourceCount resource_count;
- bool d_useResources;
- tArray<double> d_emptyDoubleArray;
- tArray<double> d_resources;
-
bool ProcessGestation(cCPUTestInfo & test_info, int cur_depth);
bool TestGenome_Body(cCPUTestInfo & test_info, const cGenome & genome, int cur_depth);
void SetupResources(void);
@@ -52,8 +63,9 @@
cTestCPU& operator=(const cTestCPU&); // @not_implemented
public:
- cTestCPU(cWorld* world) : m_world(world) { SetupResources(); }
- ~cTestCPU() { ; }
+ cTestCPU(cWorld* world) : m_world(world), m_res(new cTestResources(world)), m_localres(true) { ; }
+ cTestCPU(cWorld* world, cTestResources* res) : m_world(world), m_res(res), m_localres(false) { ; }
+ ~cTestCPU() { if (m_localres) delete m_res; }
bool TestGenome(cCPUTestInfo & test_info, const cGenome & genome);
bool TestGenome(cCPUTestInfo & test_info, const cGenome & genome, std::ofstream& out_fp);
@@ -71,8 +83,9 @@
inline const tArray<double>& GetResources();
inline void SetResource(int id, double new_level);
void SetupResourceArray(const tArray<double> &resources);
- bool& UseResources(void) { return d_useResources; }
- cResourceCount& GetResourceCount(void) { return resource_count; }
+ void SetUseResources(bool use);
+ bool GetUseResources() { return m_res->d_useResources; }
+ cResourceCount& GetResourceCount(void) { return m_res->resource_count; }
};
@@ -98,14 +111,15 @@
inline const tArray<double>& cTestCPU::GetResources()
{
- if(d_useResources) return d_resources;
+ if(m_res->d_useResources) return m_res->d_resources;
- return d_emptyDoubleArray;
+ return m_res->d_emptyDoubleArray;
}
inline void cTestCPU::SetResource(int id, double new_level)
{
- resource_count.Set(id, new_level);
+ if (!m_localres) m_res = new cTestResources(*m_res); // copy resources locally
+ m_res->resource_count.Set(id, new_level);
}
#endif
Modified: development/source/cpu/cTestUtil.cc
===================================================================
--- development/source/cpu/cTestUtil.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/cpu/cTestUtil.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -16,6 +16,7 @@
#include "cGenome.h"
#include "cGenotype.h"
#include "cHardwareBase.h"
+#include "cHardwareManager.h"
#include "cInjectGenotype.h"
#include "cInstUtil.h"
#include "cOrganism.h"
@@ -37,9 +38,11 @@
if (filename == "") filename.Set("%03d-unnamed", genome.GetSize());
// Build the test info for printing.
+ cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
test_info.TestThreads();
- world->GetTestCPU().TestGenome(test_info, genome);
+ testcpu->TestGenome(test_info, genome);
+ delete testcpu;
// Open the file...
ofstream& fp = world->GetDataFileOFStream(filename);
@@ -137,9 +140,11 @@
if (filename == "") filename.Set("p%03d-unnamed", genome.GetSize());
// Build the test info for printing.
+ cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
test_info.TestThreads();
- world->GetTestCPU().TestGenome(test_info, genome);
+ testcpu->TestGenome(test_info, genome);
+ delete testcpu;
// Open the file...
ofstream& fp = world->GetDataFileOFStream(filename);
Modified: development/source/event/cEventManager.cc
===================================================================
--- development/source/event/cEventManager.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/event/cEventManager.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -3133,7 +3133,9 @@
///// test_threads /////
void Process(){
- m_world->GetTestCPU().TestThreads(m_world->GetClassificationManager().GetBestGenotype()->GetGenome());
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+ testcpu->TestThreads(m_world->GetClassificationManager().GetBestGenotype()->GetGenome());
+ delete testcpu;
}
};
@@ -3156,7 +3158,9 @@
}
///// print_threads /////
void Process(){
- m_world->GetTestCPU().PrintThreads( m_world->GetClassificationManager().GetBestGenotype()->GetGenome() );
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+ testcpu->PrintThreads( m_world->GetClassificationManager().GetBestGenotype()->GetGenome() );
+ delete testcpu;
}
};
Modified: development/source/main/cLandscape.cc
===================================================================
--- development/source/main/cLandscape.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/main/cLandscape.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -13,6 +13,7 @@
#include "cCPUMemory.h"
#include "cEnvironment.h"
#include "cInstSet.h"
+#include "cHardwareManager.h"
#include "cOrganism.h"
#include "cPhenotype.h"
#include "cStats.h" // For GetUpdate in outputs...
@@ -80,9 +81,9 @@
neut_max = 0.0;
}
-void cLandscape::ProcessGenome(cGenome & in_genome)
+void cLandscape::ProcessGenome(cTestCPU* testcpu, cGenome & in_genome)
{
- m_world->GetTestCPU().TestGenome(test_info, in_genome);
+ testcpu->TestGenome(test_info, in_genome);
test_fitness = test_info.GetColonyFitness();
@@ -106,11 +107,11 @@
}
}
-void cLandscape::ProcessBase()
+void cLandscape::ProcessBase(cTestCPU* testcpu)
{
// Collect info on base creature.
- m_world->GetTestCPU().TestGenome(test_info, base_genome);
+ testcpu->TestGenome(test_info, base_genome);
cPhenotype & phenotype = test_info.GetColonyOrganism()->GetPhenotype();
base_fitness = test_info.GetColonyFitness();
@@ -127,13 +128,17 @@
void cLandscape::Process(int in_distance)
{
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
distance = in_distance;
// Get the info about the base creature.
- ProcessBase();
+ ProcessBase(testcpu);
// Now Process the new creature at the proper distance.
- Process_Body(base_genome, distance, 0);
+ Process_Body(testcpu, base_genome, distance, 0);
+
+ delete testcpu;
// Calculate the complexity...
@@ -150,8 +155,7 @@
// For distances greater than one, this needs to be called recursively.
-void cLandscape::Process_Body(cGenome & cur_genome, int cur_distance,
- int start_line)
+void cLandscape::Process_Body(cTestCPU* testcpu, cGenome& cur_genome, int cur_distance, int start_line)
{
const int max_line = base_genome.GetSize() - cur_distance + 1;
const int inst_size = inst_set.GetSize();
@@ -168,10 +172,10 @@
mod_genome[line_num].SetOp(inst_num);
if (cur_distance <= 1) {
- ProcessGenome(mod_genome);
+ ProcessGenome(testcpu, mod_genome);
if (test_info.GetColonyFitness() >= neut_min) site_count[line_num]++;
} else {
- Process_Body(mod_genome, cur_distance - 1, line_num + 1);
+ Process_Body(testcpu, mod_genome, cur_distance - 1, line_num + 1);
}
}
@@ -182,8 +186,10 @@
void cLandscape::ProcessDelete()
{
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
// Get the info about the base creature.
- ProcessBase();
+ ProcessBase(testcpu);
const int max_line = base_genome.GetSize();
cCPUMemory mod_genome(base_genome);
@@ -192,17 +198,20 @@
for (int line_num = 0; line_num < max_line; line_num++) {
int cur_inst = base_genome[line_num].GetOp();
mod_genome.Remove(line_num);
- ProcessGenome(mod_genome);
+ ProcessGenome(testcpu, mod_genome);
if (test_info.GetColonyFitness() >= neut_min) site_count[line_num]++;
mod_genome.Insert(line_num, cInstruction(cur_inst));
}
+ delete testcpu;
}
void cLandscape::ProcessInsert()
{
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
// Get the info about the base creature.
- ProcessBase();
+ ProcessBase(testcpu);
const int max_line = base_genome.GetSize();
const int inst_size = inst_set.GetSize();
@@ -214,24 +223,27 @@
// Loop through all instructions...
for (int inst_num = 0; inst_num < inst_size; inst_num++) {
mod_genome.Insert(line_num, cInstruction(inst_num));
- ProcessGenome(mod_genome);
+ ProcessGenome(testcpu, mod_genome);
if (test_info.GetColonyFitness() >= neut_min) site_count[line_num]++;
mod_genome.Remove(line_num);
}
}
-
+
+ delete testcpu;
}
// Prediction for a landscape where n sites are _randomized_.
void cLandscape::PredictWProcess(ostream& fp, int update)
{
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
distance = 1;
// Get the info about the base creature.
- ProcessBase();
+ ProcessBase(testcpu);
if (base_fitness == 0.0) return;
- BuildFitnessChart();
+ BuildFitnessChart(testcpu);
const int genome_size = fitness_chart.GetNumRows();
const int inst_size = fitness_chart.GetNumCols();
const double min_neut_fitness = 0.99;
@@ -336,19 +348,23 @@
total_entropy += (log((double) site_count[i] + 1) / max_ent);
}
complexity = base_genome.GetSize() - total_entropy;
+
+ delete testcpu;
}
// Prediction for a landscape where n sites are _mutated_.
void cLandscape::PredictNuProcess(ostream& fp, int update)
{
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
distance = 1;
// Get the info about the base creature.
- ProcessBase();
+ ProcessBase(testcpu);
if (base_fitness == 0.0) return;
- BuildFitnessChart();
+ BuildFitnessChart(testcpu);
const int genome_size = fitness_chart.GetNumRows();
const int inst_size = fitness_chart.GetNumCols();
const double min_neut_fitness = 0.99;
@@ -470,6 +486,8 @@
total_entropy += (log((double) site_count[i] + 1) / max_ent);
}
complexity = base_genome.GetSize() - total_entropy;
+
+ delete testcpu;
}
@@ -480,8 +498,10 @@
cGenome mod_genome(base_genome);
int genome_size = base_genome.GetSize();
+
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
- ProcessBase();
+ ProcessBase(testcpu);
// Loop through all the lines of genome, testing each line.
for (int line_num = 0; line_num < genome_size; line_num++) {
@@ -494,11 +514,13 @@
// Make the change, and test it!
mod_genome[line_num] = new_inst;
- ProcessGenome(mod_genome);
+ ProcessGenome(testcpu, mod_genome);
}
mod_genome[line_num] = cur_inst;
}
+
+ delete testcpu;
}
@@ -510,7 +532,9 @@
cGenome mod_genome(base_genome);
int genome_size = base_genome.GetSize();
- ProcessBase();
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
+ ProcessBase(testcpu);
int mut_num;
tArray<int> mut_lines(distance);
@@ -544,7 +568,7 @@
// And test it!
- ProcessGenome(mod_genome);
+ ProcessGenome(testcpu, mod_genome);
// And reset the genome.
@@ -554,11 +578,13 @@
}
trials = cur_trial;
+
+ delete testcpu;
return total_found;
- }
+}
-void cLandscape::BuildFitnessChart()
+void cLandscape::BuildFitnessChart(cTestCPU* testcpu)
{
// First, resize the fitness_chart.
const int max_line = base_genome.GetSize();
@@ -579,23 +605,24 @@
}
mod_genome[line_num].SetOp(inst_num);
- ProcessGenome(mod_genome);
+ ProcessGenome(testcpu, mod_genome);
fitness_chart(line_num, inst_num) = test_info.GetColonyFitness();
}
mod_genome[line_num].SetOp(cur_inst);
}
-
}
void cLandscape::TestPairs(int in_trials, ostream& fp)
{
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
trials = in_trials;
- ProcessBase();
+ ProcessBase(testcpu);
if (base_fitness == 0.0) return;
- BuildFitnessChart();
+ BuildFitnessChart(testcpu);
cGenome mod_genome(base_genome);
const int genome_size = base_genome.GetSize();
@@ -620,20 +647,22 @@
mut_insts[mut_num] = new_inst;
}
- TestMutPair(mod_genome, mut_lines[0], mut_lines[1], mut_insts[0],
+ TestMutPair(testcpu, mod_genome, mut_lines[0], mut_lines[1], mut_insts[0],
mut_insts[1], fp);
}
-
+ delete testcpu;
}
void cLandscape::TestAllPairs(ostream& fp)
{
- ProcessBase();
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
+ ProcessBase(testcpu);
if (base_fitness == 0.0) return;
- BuildFitnessChart();
+ BuildFitnessChart(testcpu);
const int max_line = base_genome.GetSize();
const int inst_size = inst_set.GetSize();
@@ -651,24 +680,27 @@
for (int inst2_num = 0; inst2_num < inst_size; inst2_num++) {
inst2.SetOp(inst2_num);
if (inst2 == base_genome[line2_num]) continue;
- TestMutPair(mod_genome, line1_num, line2_num, inst1, inst2, fp);
+ TestMutPair(testcpu, mod_genome, line1_num, line2_num, inst1, inst2, fp);
} // inst2_num loop
} //inst1_num loop;
} // line2_num loop
} // line1_num loop.
+ delete testcpu;
}
void cLandscape::HillClimb(ofstream& fp)
{
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
cGenome cur_genome(base_genome);
int gen = 0;
- HillClimb_Body(fp, cur_genome, gen);
+ HillClimb_Body(testcpu, fp, cur_genome, gen);
+ delete testcpu;
}
-void cLandscape::HillClimb_Body(ofstream& fp, cGenome & cur_genome,
+void cLandscape::HillClimb_Body(cTestCPU* testcpu, ofstream& fp, cGenome & cur_genome,
int & gen)
{
cCPUMemory mod_genome(base_genome);
@@ -695,7 +727,7 @@
// Loop through all instructions...
for (int inst_num = 0; inst_num < inst_size; inst_num++) {
mod_genome.Insert(line_num, cInstruction(inst_num));
- ProcessGenome(mod_genome);
+ ProcessGenome(testcpu, mod_genome);
mod_genome.Remove(line_num);
}
}
@@ -705,14 +737,14 @@
for (int line_num = 0; line_num < max_line; line_num++) {
int cur_inst = cur_genome[line_num].GetOp();
mod_genome.Remove(line_num);
- ProcessGenome(mod_genome);
+ ProcessGenome(testcpu, mod_genome);
mod_genome.Insert(line_num, cInstruction(cur_inst));
}
pos_frac = GetProbPos();
// Print the information on the current best.
- HillClimb_Print(fp, cur_genome, gen);
+ HillClimb_Print(testcpu, fp, cur_genome, gen);
// Move on to the peak genome found.
cur_genome = GetPeakGenome();
@@ -728,10 +760,10 @@
{
}
-void cLandscape::HillClimb_Print(ofstream& fp, const cGenome & _genome, const int gen) const
+void cLandscape::HillClimb_Print(cTestCPU* testcpu, ofstream& fp, const cGenome & _genome, const int gen) const
{
cCPUTestInfo test_info;
- m_world->GetTestCPU().TestGenome(test_info, _genome);
+ testcpu->TestGenome(test_info, _genome);
cPhenotype &colony_phenotype = test_info.GetColonyOrganism()->GetPhenotype();
fp << gen << " "
<< colony_phenotype.GetMerit().GetDouble() << " "
@@ -745,12 +777,12 @@
<< endl;
}
-double cLandscape::TestMutPair(cGenome & mod_genome, int line1, int line2,
- const cInstruction & mut1, const cInstruction & mut2, ostream& fp)
+double cLandscape::TestMutPair(cTestCPU* testcpu, cGenome& mod_genome, int line1, int line2,
+ const cInstruction& mut1, const cInstruction& mut2, ostream& fp)
{
mod_genome[line1] = mut1;
mod_genome[line2] = mut2;
- m_world->GetTestCPU().TestGenome(test_info, mod_genome);
+ testcpu->TestGenome(test_info, mod_genome);
double combo_fitness = test_info.GetColonyFitness() / base_fitness;
mod_genome[line1] = base_genome[line1];
Modified: development/source/main/cLandscape.h
===================================================================
--- development/source/main/cLandscape.h 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/main/cLandscape.h 2006-03-11 18:06:56 UTC (rev 502)
@@ -84,15 +84,15 @@
tMatrix<double> fitness_chart; // Chart of all one-step mutations.
- void BuildFitnessChart();
- void ProcessGenome(cGenome& in_genome);
- void ProcessBase();
- void Process_Body(cGenome& cur_genome, int cur_distance, int start_line);
+ void BuildFitnessChart(cTestCPU* testcpu);
+ void ProcessGenome(cTestCPU* testcpu, cGenome& in_genome);
+ void ProcessBase(cTestCPU* testcpu);
+ void Process_Body(cTestCPU* testcpu, cGenome& cur_genome, int cur_distance, int start_line);
- void HillClimb_Body(std::ofstream& fp, cGenome& cur_genome, int& gen);
- void HillClimb_Print(std::ofstream& fp, const cGenome& _genome, const int gen) const;
+ void HillClimb_Body(cTestCPU* testcpu, std::ofstream& fp, cGenome& cur_genome, int& gen);
+ void HillClimb_Print(cTestCPU* testcpu, std::ofstream& fp, const cGenome& _genome, const int gen) const;
- double TestMutPair(cGenome& mod_genome, int line1, int line2, const cInstruction& mut1,
+ double TestMutPair(cTestCPU* testcpu, cGenome& mod_genome, int line1, int line2, const cInstruction& mut1,
const cInstruction& mut2, std::ostream& fp);
cLandscape(); // @not_implemented
Modified: development/source/main/cMxCodeArray.cc
===================================================================
--- development/source/main/cMxCodeArray.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/main/cMxCodeArray.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -13,6 +13,7 @@
#include "cCPUTestInfo.h"
#include "cGenome.h"
#include "cInstSet.h"
+#include "cHardwareManager.h"
#include "MyCodeArrayLessThan.h"
#include "cOrganism.h"
#include "cTestCPU.h"
@@ -451,9 +452,13 @@
void cMxCodeArray::CalcFitness()
{
cGenome temp(1);
+
+ cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
cCPUTestInfo test_info;
CopyDataTo(temp);
- m_world->GetTestCPU().TestGenome(test_info, temp);
+ testcpu->TestGenome(test_info, temp);
+ delete testcpu;
+
if ( test_info.IsViable() )
m_gestation_time =
test_info.GetTestOrganism()->GetPhenotype().GetGestationTime();
Modified: development/source/main/cWorld.cc
===================================================================
--- development/source/main/cWorld.cc 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/main/cWorld.cc 2006-03-11 18:06:56 UTC (rev 502)
@@ -36,7 +36,6 @@
delete m_hw_mgr;
delete m_pop;
delete m_stats;
- delete m_test_cpu;
// cleanup driver object, if needed
if (m_own_driver) delete m_driver;
@@ -73,8 +72,7 @@
cerr << "Unable to load environment... aborting!" << endl;
ExitAvida(-1);
}
-
- m_test_cpu = new cTestCPU(this);
+
m_stats = new cStats(this);
m_pop = new cPopulation(this);
Modified: development/source/main/cWorld.h
===================================================================
--- development/source/main/cWorld.h 2006-03-10 05:35:08 UTC (rev 501)
+++ development/source/main/cWorld.h 2006-03-11 18:06:56 UTC (rev 502)
@@ -43,7 +43,6 @@
cHardwareManager* m_hw_mgr;
cPopulation* m_pop;
cStats* m_stats;
- cTestCPU* m_test_cpu;
cWorldDriver* m_driver;
cRandom m_rng;
@@ -76,7 +75,6 @@
cPopulation& GetPopulation() { return *m_pop; }
cRandom& GetRandom() { return m_rng; }
cStats& GetStats() { return *m_stats; }
- cTestCPU& GetTestCPU() { return *m_test_cpu; }
cWorldDriver& GetDriver() { return *m_driver; }
// Access to Data File Manager
More information about the Avida-cvs
mailing list