[Avida-cvs] [Avida2-svn] r240 - in trunk/source: cpu main
huangw10@myxo.css.msu.edu
huangw10 at myxo.css.msu.edu
Thu Jul 14 10:49:38 PDT 2005
Author: huangw10
Date: 2005-07-14 13:49:38 -0400 (Thu, 14 Jul 2005)
New Revision: 240
Modified:
trunk/source/cpu/cpu_test_info.cc
trunk/source/cpu/cpu_test_info.hh
trunk/source/cpu/hardware_4stack.cc
trunk/source/cpu/hardware_base.cc
trunk/source/cpu/hardware_base.hh
trunk/source/cpu/hardware_cpu.cc
trunk/source/cpu/test_cpu.cc
trunk/source/cpu/test_cpu.hh
trunk/source/main/analyze.cc
trunk/source/main/analyze.hh
Log:
Recalculate could use depletable resources. Analyze_complexity could use depletable resources also.
Modified: trunk/source/cpu/cpu_test_info.cc
===================================================================
--- trunk/source/cpu/cpu_test_info.cc 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/cpu/cpu_test_info.cc 2005-07-14 17:49:38 UTC (rev 240)
@@ -25,7 +25,6 @@
, trace_task_order(false)
, use_random_inputs(false)
, org_array(max_tests)
- , m_tracer(NULL)
{
org_array.SetAll(NULL);
Clear();
@@ -56,13 +55,15 @@
}
-void cCPUTestInfo::SetTraceExecution(cHardwareTracer *tracer)
+void cCPUTestInfo::SetTraceExecution(const cString & filename)
{
- trace_execution = (tracer)?(true):(false);
- m_tracer = tracer;
+ trace_execution = true;
+ trace_fp.open(filename);
+ assert (trace_fp.good() == true); // Unable to open trace file.
}
+
double cCPUTestInfo::GetGenotypeFitness()
{
if (org_array[0] != NULL) return org_array[0]->GetPhenotype().GetFitness();
Modified: trunk/source/cpu/cpu_test_info.hh
===================================================================
--- trunk/source/cpu/cpu_test_info.hh 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/cpu/cpu_test_info.hh 2005-07-14 17:49:38 UTC (rev 240)
@@ -8,6 +8,8 @@
#ifndef CPU_TEST_INFO_HH
#define CPU_TEST_INFO_HH
+#include <fstream>
+
#ifndef CPU_DEFS_HH
#include "cpu_defs.hh"
#endif
@@ -18,7 +20,6 @@
#include "tArray.hh"
#endif
-class cHardwareTracer;
class cOrganism;
class cString;
@@ -32,7 +33,7 @@
bool trace_execution; // Should we trace this CPU?
bool trace_task_order; // Should we keep track of ordering of tasks?
bool use_random_inputs; // Should we give the organism random inputs?
- cHardwareTracer *m_tracer;
+ std::ofstream trace_fp;
// Outputs...
bool is_viable; // Is this organism colony forming?
@@ -54,7 +55,7 @@
void PrintThreads(bool _print=true) { print_threads = _print; }
void TraceTaskOrder(bool _trace=true) { trace_task_order = _trace; }
void UseRandomInputs(bool _rand=true) { use_random_inputs = _rand; }
- void SetTraceExecution(cHardwareTracer *tracer = NULL);
+ void SetTraceExecution(const cString & filename="trace.dat");
// Input Accessors
int GetGenerationTests() const { return generation_tests; }
@@ -63,7 +64,7 @@
bool GetTraceTaskOrder() const { return trace_task_order; }
bool GetUseRandomInputs() const { return use_random_inputs; }
bool GetTraceExecution() const { return trace_execution; }
- cHardwareTracer *GetTracer() { return m_tracer; }
+ std::ofstream & GetTraceFP() { return trace_fp; }
// Output Accessors
Modified: trunk/source/cpu/hardware_4stack.cc
===================================================================
--- trunk/source/cpu/hardware_4stack.cc 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/cpu/hardware_4stack.cc 2005-07-14 17:49:38 UTC (rev 240)
@@ -376,13 +376,8 @@
#endif
// Print the status of this CPU at each step...
- if (m_tracer != NULL) {
- if (cHardwareTracer_4Stack * tracer
- = dynamic_cast<cHardwareTracer_4Stack *>(m_tracer)
- ){
- tracer->TraceHardware_4Stack(*this);
- }
- }
+ const cString & next_name = inst_set->GetName(IP().GetInst())();
+ if (trace_fp != NULL) organism->PrintStatus(*trace_fp, next_name);
// Find the instruction to be executed
const cInstruction & cur_inst = IP().GetInst();
@@ -489,14 +484,10 @@
// @CAO FIX PRINTING TO INDICATE THIS IS A BONUS
// Print the status of this CPU at each step...
- if (m_tracer != NULL) {
- if (cHardwareTracer_4Stack * tracer
- = dynamic_cast<cHardwareTracer_4Stack *>(m_tracer)
- ){
- tracer->TraceHardware_4StackBonus(*this);
- }
- }
-
+ cString next_name = cStringUtil::Stringf("%s (bonus instruction)",
+ inst_set->GetName(inst)());
+ if (trace_fp != NULL) organism->PrintStatus(*trace_fp, next_name);
+
SingleProcess_ExecuteInst(inst);
organism->SetRunning(prev_run_state);
Modified: trunk/source/cpu/hardware_base.cc
===================================================================
--- trunk/source/cpu/hardware_base.cc 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/cpu/hardware_base.cc 2005-07-14 17:49:38 UTC (rev 240)
@@ -17,7 +17,7 @@
: organism(in_organism)
, inst_set(in_inst_set)
, viewer_lock(-1)
- , m_tracer(NULL)
+ , trace_fp(NULL)
{
assert(inst_set->OK());
assert(organism != NULL);
Modified: trunk/source/cpu/hardware_base.hh
===================================================================
--- trunk/source/cpu/hardware_base.hh 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/cpu/hardware_base.hh 2005-07-14 17:49:38 UTC (rev 240)
@@ -31,7 +31,7 @@
int viewer_lock; // Used if the viewer should only lock onto
// one aspect of the hardware.
- cHardwareTracer * m_tracer; // Set this if you want execution traced.
+ ostream * trace_fp; // Set this if you want execution traced.
static int instance_count;
public:
@@ -73,7 +73,7 @@
virtual void SaveState(std::ostream & fp) = 0;
virtual void LoadState(std::istream & fp) = 0;
- void SetTrace(cHardwareTracer * tracer) { m_tracer = tracer; }
+ void SetTrace(ostream * in_fp) { trace_fp = in_fp; }
// -------- Mutations (Must be Virtual) --------
Modified: trunk/source/cpu/hardware_cpu.cc
===================================================================
--- trunk/source/cpu/hardware_cpu.cc 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/cpu/hardware_cpu.cc 2005-07-14 17:49:38 UTC (rev 240)
@@ -476,13 +476,9 @@
#endif
// Print the status of this CPU at each step...
- if (m_tracer != NULL) {
- if (cHardwareTracer_CPU * tracer
- = dynamic_cast<cHardwareTracer_CPU *>(m_tracer)
- ){
- tracer->TraceHardware_CPU(*this);
- }
- }
+ const cString & next_name = inst_set->GetName(IP().GetInst())();
+ if (trace_fp != NULL) organism->PrintStatus(*trace_fp, next_name);
+
// Find the instruction to be executed
const cInstruction & cur_inst = IP().GetInst();
@@ -590,13 +586,9 @@
// @CAO FIX PRINTING TO INDICATE THIS IS A BONUS
// Print the status of this CPU at each step...
- if (m_tracer != NULL) {
- if (cHardwareTracer_CPU * tracer
- = dynamic_cast<cHardwareTracer_CPU *>(m_tracer)
- ){
- tracer->TraceHardware_CPUBonus(*this);
- }
- }
+ cString next_name = cStringUtil::Stringf("%s (bonus instruction)",
+ inst_set->GetName(inst)());
+ if (trace_fp != NULL) organism->PrintStatus(*trace_fp, next_name);
SingleProcess_ExecuteInst(inst);
Modified: trunk/source/cpu/test_cpu.cc
===================================================================
--- trunk/source/cpu/test_cpu.cc 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/cpu/test_cpu.cc 2005-07-14 17:49:38 UTC (rev 240)
@@ -42,9 +42,9 @@
bool cTestCPU::initialized(false);
bool cTestCPU::d_useResources(false);
tArray<double> cTestCPU::d_emptyDoubleArray;
+tArray<double> cTestCPU::d_resources;
-
//////////////////////////////
// cTestCPU (Static Class)
//////////////////////////////
@@ -57,14 +57,17 @@
inst_set = in_inst_set;
environment = in_env;
resource_count.SetSize(in_env->GetResourceLib().GetSize());
- d_emptyDoubleArray.ResizeClear(in_env->GetResourceLib().GetSize());
+ //d_emptyDoubleArray.ResizeClear(in_env->GetResourceLib().GetSize());
+ //d_resources.ResizeClear(in_env->GetResourceLib().GetSize());
SetupResources();
test_interface = in_interface;
initialized = true;
+
}
void cTestCPU::SetupResources(void) {
- // Setup the resources...
+
+ // Setup the resources...
assert(environment);
const cResourceLib & resource_lib = environment->GetResourceLib();
@@ -72,6 +75,11 @@
resource_count.SetSize(resource_lib.GetSize());
d_emptyDoubleArray.ResizeClear(resource_lib.GetSize());
+ d_resources.ResizeClear(resource_lib.GetSize());
+ for(int i=0; i<resource_lib.GetSize(); i++) {
+ d_emptyDoubleArray[i] = 0.0;
+ d_resources[i] = 0.0;
+ }
//resource_count.ResizeSpatialGrids(cConfig::GetWorldX(),
// cConfig::GetWorldY());
resource_count.ResizeSpatialGrids(1, 1);
@@ -91,8 +99,21 @@
}
return;
+
}
+void cTestCPU::SetupResourceArray(const tArray<double> &resources) {
+ for(int i=0; i<d_resources.GetSize(); i++) {
+ if(i >= resources.GetSize()) {
+ d_resources[i] = 0.0;
+ } else {
+ d_resources[i] = resources[i];
+ }
+ }
+
+ return;
+}
+
void cTestCPU::SetInstSet(cInstSet * in_inst_set)
{
inst_set = in_inst_set;
@@ -121,18 +142,17 @@
// Prepare the inputs...
cur_input = 0;
- cur_receive = 0;
// Determine if we're tracing and what we need to print.
- cHardwareTracer * tracer =
- test_info.GetTraceExecution() ? (test_info.GetTracer()) : NULL;
+ ostream * trace_fp =
+ test_info.GetTraceExecution() ? &(test_info.GetTraceFP()) : NULL;
int time_used = 0;
while (time_used < time_allocated &&
organism.GetHardware().GetMemory().GetSize() &&
organism.GetPhenotype().GetNumDivides() == 0) {
time_used++;
- organism.GetHardware().SetTrace(tracer);
+ organism.GetHardware().SetTrace(trace_fp);
organism.GetHardware().SingleProcess();
organism.GetHardware().SetTrace(NULL);
//resource_count.Update(1/cConfig::GetAveTimeslice());
@@ -140,22 +160,24 @@
}
// Print out some final info in trace...
- if (tracer != NULL) {
- if (cHardwareTracer_TestCPU * tracer_test_cpu
- = dynamic_cast<cHardwareTracer_TestCPU *>(tracer)
- ){
- tracer_test_cpu->TraceHardware_TestCPU(
- time_used,
- time_allocated,
- organism.GetHardware().GetMemory().GetSize(),
- organism.GetHardware().GetMemory().AsString(),
- organism.ChildGenome().AsString()
- );
+ if (trace_fp != NULL) {
+ if (time_used == time_allocated) {
+ *trace_fp << endl << "TIMEOUT: No offspring produced." << endl;
}
+ else if (organism.GetHardware().GetMemory().GetSize() == 0) {
+ *trace_fp << endl << "ORGANISM DEATH: No offspring produced." << endl;
+ }
+ else {
+ *trace_fp << endl << "Final Memory: "
+ << organism.GetHardware().GetMemory().AsString() << endl
+ << "Child Memory: " << organism.ChildGenome().AsString()
+ << endl;
+ }
}
// For now, always return true.
return true;
+
}
@@ -396,11 +418,16 @@
const tArray<double> & cTestCPU::GetResources()
{
if(d_useResources) {
- return resource_count.GetResources();
+ //return resource_count.GetResources(); // Changed to use my own vector
+ return d_resources;
}
+
return d_emptyDoubleArray;
+ //assert(resource_count != NULL); // Original line
+ //return resource_count.GetResources(); // Original line
}
+
void cTestCPU::UpdateResources(const tArray<double> & res_change)
{
//resource_count.Modify(res_change);
Modified: trunk/source/cpu/test_cpu.hh
===================================================================
--- trunk/source/cpu/test_cpu.hh 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/cpu/test_cpu.hh 2005-07-14 17:49:38 UTC (rev 240)
@@ -42,6 +42,7 @@
static cResourceCount resource_count;
static bool d_useResources;
static tArray<double> d_emptyDoubleArray;
+ static tArray<double> d_resources;
static bool initialized;
@@ -84,6 +85,7 @@
const int cell_id);
static void SetResource(int id, double new_level);
static void SetupResources(void);
+ static void SetupResourceArray(const tArray<double> &resources);
static bool &UseResources(void) { return d_useResources; }
static cResourceCount &GetResourceCount(void) {return resource_count;}
};
Modified: trunk/source/main/analyze.cc
===================================================================
--- trunk/source/main/analyze.cc 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/main/analyze.cc 2005-07-14 17:49:38 UTC (rev 240)
@@ -52,9 +52,6 @@
using namespace std;
-
-
-
//////////////
// cAnalyze
//////////////
@@ -87,6 +84,7 @@
}
resources.push_back(make_pair(0, r));
}
+ FillResources(0);
cInitFile analyze_file(filename);
analyze_file.Load();
@@ -95,6 +93,8 @@
LoadCommandList(analyze_file, command_list);
ProcessCommands(command_list);
+
+ return;
}
cAnalyze::~cAnalyze()
@@ -386,14 +386,14 @@
cString filename = "resource.dat";
if(words >= 1) {
- cString filename = cur_string.PopWord();
+ filename = cur_string.PopWord();
}
cout << "Loading Resources from: " << filename << endl;
// Process the resource.dat, currently assuming this is the only possible
// input file
- ifstream resourceFile("resource.dat", ios::in);
+ ifstream resourceFile(filename, ios::in);
assert(resourceFile);
// Read in each line of the resource file and process it
@@ -420,8 +420,9 @@
// assuming a non-numeric is a comment denoting the rest of the line as
// not informational.
while(true) {
- ss >> ws; ss >> x; if(!ss.good()) { ss.clear(); break; }
+ ss >> ws; ss >> x;
tempValues.push_back(x);
+ if(!ss.good()) { ss.clear(); break; }
}
// Can't have no resources, so assert
if(tempValues.empty()) { assert(0); }
@@ -437,10 +438,11 @@
// 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, cEnvironment &environment)
+void cAnalyze::FillResources(int update)
{
// There must be some resources for at least one update
- assert(!resources.empty());
+ //assert(!resources.empty());
+ if(resources.empty()) { return; }
int which = -1;
// Assuming resource vector is sorted by update, front to back
@@ -463,17 +465,23 @@
}
if(which < 0) { assert(0); }
- cResourceLib &resLib = environment.GetResourceLib();
+ //cResourceLib &resLib = environment.GetResourceLib();
// The resources from the file and the original resources from
// environment.cfg must be the same size.
- assert((unsigned int)resLib.GetSize() == resources[which].second.size());
+ //assert((unsigned int)resLib.GetSize() == resources[which].second.size());
// Set the resource concentrations to the values for the appropriate
// update
+ //for(unsigned int i=0; i<resources[which].second.size(); i++) {
+ // cResource *res = resLib.GetResource(i);
+ // assert(res);
+ // res->SetInitial(resources[which].second[i]);
+ //}
+
+ tArray<double> temp(resources[which].second.size());
for(unsigned int i=0; i<resources[which].second.size(); i++) {
- cResource *res = resLib.GetResource(i);
- assert(res);
- res->SetInitial(resources[which].second[i]);
+ temp[i] = resources[which].second[i];
}
+ cTestCPU::SetupResourceArray(temp);
return;
}
@@ -1126,20 +1134,15 @@
useResources = 0;
}
}
- if(useResources) {
- assert(d_environment);
- }
bool backupUsage;
- cResourceCount originalResourceCount;
- cEnvironment *backupEnvironment;
+ tArray<double> backupResources;
+
if(useResources) {
+ // Backup test cpu data
backupUsage = cTestCPU::UseResources();
- originalResourceCount = cTestCPU::GetResourceCount();
- backupEnvironment = cTestCPU::GetEnvironment();
+ backupResources = cTestCPU::GetResources();
- cTestCPU::SetEnvironment(d_environment);
- cTestCPU::SetupResources();
cTestCPU::UseResources() = true;
}
@@ -1154,28 +1157,21 @@
break;
}
- // Build the hardware status printer for tracing.
- ofstream trace_fp;
- trace_fp.open(filename);
- assert (trace_fp.good() == true); // Unable to open trace file.
- cHardwareStatusPrinter trace_printer(trace_fp);
-
// Build the test info for printing.
cCPUTestInfo test_info;
test_info.TestThreads();
- test_info.SetTraceExecution(&trace_printer);
+ test_info.SetTraceExecution(filename);
cTestCPU::TestGenome(test_info, genotype->GetGenome());
if (verbose) cout << " Tracing: " << filename << endl;
- trace_fp.close();
}
if(useResources) {
- cTestCPU::SetEnvironment(backupEnvironment);
- cTestCPU::GetResourceCount() = originalResourceCount;
+ // Set the test cpu back to the state it was in before we messed with it
cTestCPU::UseResources() = backupUsage;
- }
+ cTestCPU::SetupResourceArray(backupResources);
+ }
return;
}
@@ -3281,7 +3277,7 @@
assert(num_compare!=0);
// And do the tests...
- for (int iter=0; iter < num_compare; iter++) {
+ for (int iter=1; iter < num_compare; iter++) {
cCPUMemory test_genome0 = genotype1->GetGenome();
cCPUMemory test_genome1 = genotype2->GetGenome();
@@ -4134,10 +4130,6 @@
useResources = 0;
}
}
- // It is an error to use resources, but have no environment
- if(useResources) {
- assert(d_environment);
- }
// Batch frequency begins with the first organism, but then skips that
// amount ahead in the batch. It defaults to 1, so that default analyzes
@@ -4153,18 +4145,13 @@
// 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;
- cEnvironment *originalEnvironment;
- cResourceCount originalResourceCount;
+ tArray<double> backupResources;
if(useResources) {
// Backup test cpu data
backupUsage = cTestCPU::UseResources();
- originalEnvironment = cTestCPU::GetEnvironment();
- originalResourceCount = cTestCPU::GetResourceCount();
+ backupResources = cTestCPU::GetResources();
- // Set the test cpu up with our environment and tell it to use resources
- cTestCPU::SetEnvironment(d_environment);
- cTestCPU::SetupResources();
cTestCPU::UseResources() = true;
}
@@ -4174,6 +4161,7 @@
tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
cAnalyzeGenotype * genotype = NULL;
+
bool islineage = false;
cString lineage_filename;
ofstream lineage_fp;
@@ -4204,19 +4192,16 @@
non_lineage_fp << genotype->GetID() << " ";
}
+ int updateBorn = -1;
if(useResources) {
- int updateBorn = genotype->GetUpdateBorn();
-
- cEnvironment *env = cTestCPU::GetEnvironment();
- assert(env);
- FillResources(updateBorn, *env);
- cTestCPU::SetupResources();
+ updateBorn = genotype->GetUpdateBorn();
+ FillResources(updateBorn);
}
// Calculate the stats for the genotype we're working with ...
genotype->Recalculate();
+ cout << genotype->GetFitness() << endl;
const int num_insts = inst_set.GetSize();
-
const int max_line = genotype->GetLength();
const cGenome & base_genome = genotype->GetGenome();
cGenome mod_genome(base_genome);
@@ -4328,9 +4313,8 @@
if(useResources) {
// Set the test cpu back to the state it was in before we messed with it
cTestCPU::UseResources() = backupUsage;
- cTestCPU::SetEnvironment(originalEnvironment);
- cTestCPU::GetResourceCount() = originalResourceCount;
- }
+ cTestCPU::SetupResourceArray(backupResources);
+ }
return;
}
@@ -4588,20 +4572,15 @@
useResources = 0;
}
}
- if(useResources) {
- assert(d_environment);
- }
bool backupUsage;
- cResourceCount originalResourceCount;
- cEnvironment *backupEnvironment;
+ tArray<double> backupResources;
+
if(useResources) {
+ // Backup test cpu data
backupUsage = cTestCPU::UseResources();
- originalResourceCount = cTestCPU::GetResourceCount();
- backupEnvironment = cTestCPU::GetEnvironment();
+ backupResources = cTestCPU::GetResources();
- cTestCPU::SetEnvironment(d_environment);
- cTestCPU::SetupResources();
cTestCPU::UseResources() = true;
}
@@ -4618,8 +4597,19 @@
cAnalyzeGenotype * genotype = NULL;
cAnalyzeGenotype * last_genotype = NULL;
while ((genotype = batch_it.Next()) != NULL) {
+
+ // If use resources, load proper resource according to update_born
+ int updateBorn = -1;
+ if(useResources) {
+ updateBorn = genotype->GetUpdateBorn();
+ FillResources(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 (verbose == true) {
+ PrintTestCPUResources("");
+ }
if (last_genotype != NULL &&
genotype->GetParentID() == last_genotype->GetID()) {
genotype->Recalculate(last_genotype);
@@ -4629,10 +4619,10 @@
}
if(useResources) {
- cTestCPU::SetEnvironment(backupEnvironment);
- cTestCPU::GetResourceCount() = originalResourceCount;
+ // Set the test cpu back to the state it was in before we messed with it
cTestCPU::UseResources() = backupUsage;
- }
+ cTestCPU::SetupResourceArray(backupResources);
+ }
return;
}
@@ -4729,8 +4719,7 @@
cout << "TestCPU is using resources: ";
cout << cTestCPU::UseResources() << endl;
cout << "Resources currently in TestCPU: ";
- const cResourceCount &resources = cTestCPU::GetResourceCount();
- const tArray<double> &quantity = resources.ReadResources();
+ const tArray<double> &quantity = cTestCPU::GetResources();
for(int i=0; i<quantity.GetSize(); i++) {
cout << quantity.ElementAt(i) << " ";
}
Modified: trunk/source/main/analyze.hh
===================================================================
--- trunk/source/main/analyze.hh 2005-07-14 17:44:35 UTC (rev 239)
+++ trunk/source/main/analyze.hh 2005-07-14 17:49:38 UTC (rev 240)
@@ -211,7 +211,7 @@
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, cEnvironment &environment);
+ void FillResources(int update);
// Flow Control...
void CommandForeach(cString cur_string, tList<cAnalyzeCommand> & clist);
More information about the Avida-cvs
mailing list