[Avida-cvs] [avida-svn] r556 - in development/source: . analyze cpu main tools

goingssh@myxo.css.msu.edu goingssh at myxo.css.msu.edu
Mon Apr 3 17:57:08 PDT 2006


Author: goingssh
Date: 2006-04-03 20:57:01 -0400 (Mon, 03 Apr 2006)
New Revision: 556

Modified:
   development/source/analyze/cAnalyzeJobQueue.cc
   development/source/cpu/cHardwareBase.h
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/defs.h
   development/source/main/cEnvironment.cc
   development/source/main/cPhenotype.cc
   development/source/main/cPhenotype.h
   development/source/main/cPopulation.cc
   development/source/main/cPopulationCell.cc
   development/source/main/cReactionProcess.h
   development/source/main/cStats.cc
   development/source/main/cStats.h
   development/source/main/cTaskEntry.h
   development/source/main/cTaskLib.cc
   development/source/main/cTaskLib.h
   development/source/tools/cGenesis.cc
   development/source/tools/cThread.h
   development/source/tools/tArray.h
   development/source/tools/tBuffer.h
Log:
Added match strings capabilities (instruction and task).
Added ability for a reaction to take in an extra parameter from the environment file.
Changed limited resources so that task quality affects the amount of resource consumed and not just the bonus the org receives.
Added ability to print out new file on top of tasks.dat that displays the average and max task quality of orgs completing a task.
Fixed some windows bugs
Added pop() function to tBuffer.



Modified: development/source/analyze/cAnalyzeJobQueue.cc
===================================================================
--- development/source/analyze/cAnalyzeJobQueue.cc	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/analyze/cAnalyzeJobQueue.cc	2006-04-04 00:57:01 UTC (rev 556)
@@ -33,7 +33,7 @@
 
 void cAnalyzeJobQueue::Execute()
 {
-  const int num_workers = m_world->GetConfig().ANALYZE_MT_CONCURRENCY.Get();
+  const int num_workers = 1; //m_world->GetConfig().ANALYZE_MT_CONCURRENCY.Get();
   
   cAnalyzeJobWorker* workers[num_workers];
   

Modified: development/source/cpu/cHardwareBase.h
===================================================================
--- development/source/cpu/cHardwareBase.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/cpu/cHardwareBase.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -11,6 +11,7 @@
 #ifndef cHardwareBase_h
 #define cHardwareBase_h
 
+#include <cassert>
 #include <iostream>
 
 using namespace std;

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/cpu/cHardwareCPU.cc	2006-04-04 00:57:01 UTC (rev 556)
@@ -206,7 +206,7 @@
     cInstEntryCPU("put",       &cHardwareCPU::Inst_TaskPut),
     cInstEntryCPU("IO",        &cHardwareCPU::Inst_TaskIO, true,
                   "Output ?BX?, and input new number back into ?BX?"),
-    
+    cInstEntryCPU("match-strings", &cHardwareCPU::Inst_MatchStrings),
     cInstEntryCPU("send",      &cHardwareCPU::Inst_Send),
     cInstEntryCPU("receive",   &cHardwareCPU::Inst_Receive),
     cInstEntryCPU("sense",     &cHardwareCPU::Inst_Sense),
@@ -389,6 +389,7 @@
 , cur_thread(hardware_cpu.cur_thread)
 , mal_active(hardware_cpu.mal_active)
 , advance_ip(hardware_cpu.advance_ip)
+, executedmatchstrings(hardware_cpu.executedmatchstrings)
 #ifdef INSTRUCTION_COSTS
 , inst_cost(hardware_cpu.inst_cost)
 , inst_ft_cost(hardware_cpu.inst_ft_cost)
@@ -415,6 +416,7 @@
   cur_thread = 0;
   
   mal_active = false;
+  executedmatchstrings = false;
   
 #ifdef INSTRUCTION_COSTS
   // instruction cost arrays
@@ -2771,12 +2773,21 @@
   return true;
 }
 
+bool cHardwareCPU::Inst_MatchStrings(cAvidaContext& ctx)
+{
+	if (executedmatchstrings)
+		return false;
+	organism->DoOutput(ctx, 357913941);
+	executedmatchstrings = true;
+	return true;
+}
+
 bool cHardwareCPU::Inst_Send(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   organism->SendValue(GetRegister(reg_used));
-GetRegister(reg_used) = 0;
-return true;
+  GetRegister(reg_used) = 0;
+  return true;
 }
 
 bool cHardwareCPU::Inst_Receive(cAvidaContext& ctx)

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/cpu/cHardwareCPU.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -84,6 +84,7 @@
   // Flags...
   bool mal_active;         // Has an allocate occured since last divide?
   bool advance_ip;         // Should the IP advance after this instruction?
+  bool executedmatchstrings;	// Have we already executed the match strings instruction?
 
   // Instruction costs...
 #ifdef INSTRUCTION_COSTS
@@ -298,6 +299,7 @@
   bool Inst_TaskStackLoad(cAvidaContext& ctx);
   bool Inst_TaskPut(cAvidaContext& ctx);
   bool Inst_TaskIO(cAvidaContext& ctx);
+  bool Inst_MatchStrings(cAvidaContext& ctx);
   bool Inst_Send(cAvidaContext& ctx);
   bool Inst_Receive(cAvidaContext& ctx);
   bool Inst_Sense(cAvidaContext& ctx);

Modified: development/source/defs.h
===================================================================
--- development/source/defs.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/defs.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -140,7 +140,7 @@
 
 // Number of distinct input and outputs stored in the IOBufs (to test tasks)
 #define INPUT_BUF_SIZE  3
-#define OUTPUT_BUF_SIZE 1
+#define OUTPUT_BUF_SIZE 3
 #define SEND_BUF_SIZE 3
 #define RECEIVE_BUF_SIZE 3
 

Modified: development/source/main/cEnvironment.cc
===================================================================
--- development/source/main/cEnvironment.cc	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cEnvironment.cc	2006-04-04 00:57:01 UTC (rev 556)
@@ -25,7 +25,7 @@
 #include "cTaskEntry.h"
 #include "cTools.h"
 #include "cWorld.h"
-
+#include <iostream>
 using namespace std;
 
 
@@ -204,6 +204,9 @@
         return false;
       new_process->SetDetectionError(var_value.AsDouble());
     }
+	else if (var_name == "string") {
+		new_process->SetMatchString(var_value);
+	}
     else {
       cerr << "Error: Unknown process variable '" << var_name
       << "' in reaction '" << reaction->GetName() << "'" << endl;
@@ -406,10 +409,11 @@
   }
   
   // Finish loading in this reaction.
-  const cString trigger = desc.PopWord();
+    cString trigger_info = desc.PopWord();
+	cString trigger = trigger_info.Pop('=');
   
   // Load the task trigger
-  cTaskEntry * cur_task = task_lib.AddTask(trigger);
+   cTaskEntry * cur_task = task_lib.AddTask(trigger, trigger_info);
   if (cur_task == NULL) {
     cerr << "...failed to find task in cTaskLib..." << endl;
     return false;
@@ -676,14 +680,14 @@
     // If this task wasn't performed, move on to the next one.
     if (task_quality == 0.0) continue;
     
-    // Mark this task as performed...
-    result.MarkTask(task_id);
-    
     // Examine requisites on this reaction
     if (TestRequisites(cur_reaction->GetRequisites(), task_count[task_id], reaction_count) == false) {
       continue;
     }
     
+	// Mark this task as performed...
+	result.MarkTask(task_id, task_quality);
+
     // And lets process it!
     DoProcesses(ctx, cur_reaction->GetProcesses(), resource_count, task_quality, result);
     
@@ -790,7 +794,7 @@
     
     // Test if infinite resource
     if (in_resource == NULL) {
-      consumed = max_consumed;
+      consumed = max_consumed * task_quality;
     }
     
     // Otherwise we're using a finite resource
@@ -802,6 +806,8 @@
       
       // Make sure we're not above the maximum consumption.
       if (consumed > max_consumed) consumed = max_consumed;
+
+	  consumed *= task_quality;  // modify consumed based on task quality
       
       // Test if we are below the minimum consumption.
       if (consumed < min_consumed) consumed = 0.0;
@@ -814,7 +820,7 @@
     }
     
     // Calculate the bonus
-    double bonus = consumed * cur_process->GetValue() * task_quality;
+    double bonus = consumed * cur_process->GetValue();
     
     switch (cur_process->GetType()) {
       case nReaction::PROCTYPE_ADD:

Modified: development/source/main/cPhenotype.cc
===================================================================
--- development/source/main/cPhenotype.cc	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cPhenotype.cc	2006-04-04 00:57:01 UTC (rev 556)
@@ -25,10 +25,12 @@
   : m_world(world)
   , initialized(false)
   , cur_task_count(m_world->GetEnvironment().GetTaskLib().GetSize())
+  , cur_task_quality(m_world->GetEnvironment().GetTaskLib().GetSize())
   , cur_reaction_count(m_world->GetEnvironment().GetReactionLib().GetSize())
   , cur_inst_count(world->GetHardwareManager().GetInstSet().GetSize())
   , sensed_resources(m_world->GetEnvironment().GetResourceLib().GetSize())
   , last_task_count(m_world->GetEnvironment().GetTaskLib().GetSize())
+  , last_task_quality(m_world->GetEnvironment().GetTaskLib().GetSize())
   , last_reaction_count(m_world->GetEnvironment().GetReactionLib().GetSize())
   , last_inst_count(world->GetHardwareManager().GetInstSet().GetSize())
 {
@@ -98,6 +100,7 @@
   cur_num_errors  = 0;
   cur_num_donates  = 0;
   cur_task_count.SetAll(0);
+  cur_task_quality.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_inst_count.SetAll(0);
   for (int j = 0; j < sensed_resources.GetSize(); j++)
@@ -110,6 +113,7 @@
   last_num_errors     = parent_phenotype.last_num_errors;
   last_num_donates    = parent_phenotype.last_num_donates;
   last_task_count     = parent_phenotype.last_task_count;
+  last_task_quality   = parent_phenotype.last_task_quality;
   last_reaction_count = parent_phenotype.last_reaction_count;
   last_inst_count     = parent_phenotype.last_inst_count;
   last_fitness        = last_merit_base * last_bonus / gestation_time;
@@ -180,6 +184,7 @@
   cur_num_errors  = 0;
   cur_num_donates  = 0;
   cur_task_count.SetAll(0);
+  cur_task_quality.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_inst_count.SetAll(0);
   sensed_resources.SetAll(0);
@@ -190,6 +195,7 @@
   last_num_errors = 0;
   last_num_donates = 0;
   last_task_count.SetAll(0);
+  last_task_quality.SetAll(0);
   last_reaction_count.SetAll(0);
   last_inst_count.SetAll(0);
 
@@ -258,6 +264,7 @@
   last_num_errors     = cur_num_errors;
   last_num_donates    = cur_num_donates;
   last_task_count     = cur_task_count;
+  last_task_quality   = cur_task_quality;
   last_reaction_count = cur_reaction_count;
   last_inst_count     = cur_inst_count;
 
@@ -266,6 +273,7 @@
   cur_num_errors  = 0;
   cur_num_donates  = 0;
   cur_task_count.SetAll(0);
+  cur_task_quality.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_inst_count.SetAll(0);
 
@@ -343,6 +351,7 @@
   last_num_errors     = cur_num_errors;
   last_num_donates    = cur_num_donates;
   last_task_count     = cur_task_count;
+  last_task_quality   = cur_task_quality;
   last_reaction_count = cur_reaction_count;
   last_inst_count     = cur_inst_count;
 
@@ -351,6 +360,7 @@
   cur_num_errors  = 0;
   cur_num_donates  = 0;
   cur_task_count.SetAll(0);
+  cur_task_quality.SetAll(0);
   cur_reaction_count.SetAll(0);
   cur_inst_count.SetAll(0);
   sensed_resources.SetAll(-1.0);
@@ -515,6 +525,9 @@
   for (int i = 0; i < num_tasks; i++) {
     if (result.TaskDone(i) == true) cur_task_count[i]++;
   }
+  for (int i = 0; i < num_tasks; i++) {
+	  if (result.TaskQuality(i) > 0) cur_task_quality[i]+= result.TaskQuality(i);
+  }
   for (int i = 0; i < num_reactions; i++) {
     if (result.ReactionTriggered(i) == true) cur_reaction_count[i]++;
   }

Modified: development/source/main/cPhenotype.h
===================================================================
--- development/source/main/cPhenotype.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cPhenotype.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -78,6 +78,7 @@
   int cur_num_errors;             // Total instructions executed illeagally.
   int cur_num_donates;            // Number of donations so far
   tArray<int> cur_task_count;     // Total times each task was performed
+  tArray<double> cur_task_quality;	  // Average (total?) quality with which each task was performed
   tArray<int> cur_reaction_count; // Total times each reaction was triggered.
   tArray<int> cur_inst_count;	  // Intruction exection counter
   tArray<double> sensed_resources; // Resources of which the organism is explictly aware
@@ -88,6 +89,7 @@
   int last_num_errors;
   int last_num_donates;
   tArray<int> last_task_count;
+  tArray<double> last_task_quality;
   tArray<int> last_reaction_count;
   tArray<int> last_inst_count;	  // Intruction exection counter
   double last_fitness;            // Used to determine sterilization.
@@ -157,7 +159,7 @@
 
   // Input and Output Reaction Tests
   bool TestInput(tBuffer<int>& inputs, tBuffer<int>& outputs);
-  bool TestOutput(cAvidaContext& ctx, cTaskContext& ctx, tBuffer<int>& send_buf, tBuffer<int>& receive_buf,
+  bool TestOutput(cAvidaContext& ctx, cTaskContext& taskctx, tBuffer<int>& send_buf, tBuffer<int>& receive_buf,
                   const tArray<double>& res_in, tArray<double>& res_change, tArray<int>& insts_triggered);
 
   // State saving and loading, and printing...
@@ -190,6 +192,7 @@
   int GetCurNumErrors() const { assert(initialized == true); return cur_num_errors; }
   int GetCurNumDonates() const { assert(initialized == true); return cur_num_donates; }
   const tArray<int>& GetCurTaskCount() const { assert(initialized == true); return cur_task_count; }
+  const tArray<double> & GetCurTaskQuality() const { assert(initialized == true); return cur_task_quality; }
   const tArray<int>& GetCurReactionCount() const { assert(initialized == true); return cur_reaction_count;}
   const tArray<int>& GetCurInstCount() const { assert(initialized == true); return cur_inst_count; }
   
@@ -201,6 +204,7 @@
   int GetLastNumErrors() const { assert(initialized == true); return last_num_errors; }
   int GetLastNumDonates() const { assert(initialized == true); return last_num_donates; }
   const tArray<int>& GetLastTaskCount() const { assert(initialized == true); return last_task_count; }
+    const tArray<double> & GetLastTaskQuality() const { assert(initialized == true); return last_task_quality; }
   const tArray<int>& GetLastReactionCount() const { assert(initialized == true); return last_reaction_count; }
   const tArray<int>& GetLastInstCount() const { assert(initialized == true); return last_inst_count; }
   double GetLastFitness() const { assert(initialized == true); return last_fitness; }

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cPopulation.cc	2006-04-04 00:57:01 UTC (rev 556)
@@ -114,7 +114,7 @@
     } else {
       right_flag = true;
     }
-    
+ 
     // Setup the connection list for each cell. (Clockwise from -1 to 1)
     
     tList<cPopulationCell> & conn_list=cell_array[cell_id].ConnectionList();
@@ -142,7 +142,7 @@
     if (left_flag) {
       conn_list.Push(&(cell_array[GridNeighbor(cell_id,world_x,world_y, -1,  0)]));
     }
-    
+
     // Setup the reaper queue...
     if (world->GetConfig().BIRTH_METHOD.Get() == POSITION_CHILD_FULL_SOUP_ELDEST) {
       reaper_queue.Push(&(cell_array[cell_id]));
@@ -172,7 +172,7 @@
                          res->GetOutflowY2() );
     m_world->GetStats().SetResourceName(i, res->GetName());
   }
-  
+ 
   // Give stats information about the environment...
   const cTaskLib & task_lib = environment.GetTaskLib();
   for (int i = 0; i < task_lib.GetSize(); i++) {
@@ -184,7 +184,7 @@
   for (int i = 0; i < inst_set.GetSize(); i++) {
     m_world->GetStats().SetInstName(i, inst_set.GetName(i));
   }
-  
+
   // Load a clone if one is provided, otherwise setup start organism.
   if (m_world->GetConfig().CLONE_FILE.Get() == "-" || m_world->GetConfig().CLONE_FILE.Get() == "") {
     cGenome start_org = cInstUtil::LoadGenome(m_world->GetConfig().START_CREATURE.Get(), world->GetHardwareManager().GetInstSet());
@@ -194,7 +194,7 @@
     ifstream fp(m_world->GetConfig().CLONE_FILE.Get());
     LoadClone(fp);
   }
-  
+
   // Load a saved population if one is provided.
   cString fname(m_world->GetConfig().POPULATION_FILE.Get());
   if (fname != "-" && fname != "") {
@@ -223,7 +223,6 @@
       LoadPopulation(fp);
     }
   }
-  
 }
 
 
@@ -1164,10 +1163,17 @@
     
     // Test what tasks this creatures has completed.
     for (int j=0; j < m_world->GetEnvironment().GetTaskLib().GetSize(); j++) {
-      if (phenotype.GetCurTaskCount()[j] > 0)  stats.AddCurTask(j);
-      if (phenotype.GetLastTaskCount()[j] > 0) stats.AddLastTask(j);
-      if (phenotype.GetLastTaskCount()[j] > 0) 
-        stats.IncTaskExeCount(j, phenotype.GetLastTaskCount()[j]);
+      if (phenotype.GetCurTaskCount()[j] > 0)
+	  {
+		  stats.AddCurTask(j);
+		  stats.AddCurTaskQuality(j, phenotype.GetCurTaskQuality()[j]);
+	  }
+      if (phenotype.GetLastTaskCount()[j] > 0)
+	  {
+		  stats.AddLastTask(j);
+		  stats.AddLastTaskQuality(j, phenotype.GetLastTaskQuality()[j]);
+		  stats.IncTaskExeCount(j, phenotype.GetLastTaskCount()[j]);
+	  } 
     }
     
     // Increment the counts for all qualities the organism has...

Modified: development/source/main/cPopulationCell.cc
===================================================================
--- development/source/main/cPopulationCell.cc	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cPopulationCell.cc	2006-04-04 00:57:01 UTC (rev 556)
@@ -64,7 +64,8 @@
 {
   m_world = world;
   cell_id = in_id;
-  if (mutation_rates == NULL) mutation_rates = new cMutationRates();
+  if (mutation_rates == NULL) 
+	  mutation_rates = new cMutationRates();
   mutation_rates->Copy(in_rates);
 }
 

Modified: development/source/main/cReactionProcess.h
===================================================================
--- development/source/main/cReactionProcess.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cReactionProcess.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -17,6 +17,9 @@
 #ifndef nReaction_h
 #include "nReaction.h"
 #endif
+#ifndef STRING_HH
+#include "cString.h"
+#endif
 
 #include <iostream>
 
@@ -35,6 +38,7 @@
   cResource* product;   // Output resource.
   double conversion;     // Conversion factor.
   bool lethal;		 // Lethality of reaction
+  cString match_string;	 // Bit string to match if this is a match string reaction
   int inst_id;           // Instruction to be triggered if reaction successful.
 
   // Resource detection
@@ -78,6 +82,7 @@
   cResource* GetDetect() const { return detect; }
   double GetDetectionThreshold() const { return detection_threshold; }
   double GetDetectionError() const { return detection_error; }
+  cString GetMatchString() const { return match_string; }
 
   void SetResource(cResource* _in) { resource = _in; }
   void SetValue(double _in) { value = _in; }
@@ -92,6 +97,7 @@
   void SetDetect(cResource* _in) { detect = _in; }
   void SetDetectionThreshold(double _in) { detection_threshold = _in; }
   void SetDetectionError(double _in) { detection_error = _in; }
+  void SetMatchString(cString _in) { match_string = _in; }
 };
 
 #endif

Modified: development/source/main/cStats.cc
===================================================================
--- development/source/main/cStats.cc	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cStats.cc	2006-04-04 00:57:01 UTC (rev 556)
@@ -86,9 +86,19 @@
 {
   task_cur_count.Resize( m_world->GetNumTasks() );
   task_last_count.Resize( m_world->GetNumTasks() );
+  task_cur_quality.Resize( m_world->GetNumTasks() );
+  task_last_quality.Resize( m_world->GetNumTasks() );
+  task_cur_max_quality.Resize( m_world->GetNumTasks() );
+  task_last_max_quality.Resize( m_world->GetNumTasks() );
   task_exe_count.Resize( m_world->GetNumTasks() );
   task_cur_count.SetAll(0);
+  task_cur_quality.SetAll(0);
+  task_cur_max_quality.SetAll(0);
+  task_last_max_quality.SetAll(0);
+  task_last_quality.SetAll(0);
   task_last_count.SetAll(0);
+  task_cur_max_quality.SetAll(0);
+  task_last_max_quality.SetAll(0);
   task_exe_count.SetAll(0);
 
 #ifdef INSTRUCTION_COUNT
@@ -204,7 +214,11 @@
 {
   for( int i=0; i < task_cur_count.GetSize(); i++ ){
     task_cur_count[i] = 0;
-    task_last_count[i] = 0;
+	task_last_count[i] = 0;
+	task_cur_quality[i] = 0;
+	task_last_quality[i] = 0;
+	task_last_max_quality[i] = 0;
+	task_cur_max_quality[i] = 0;
   }
 }
 
@@ -357,6 +371,10 @@
 
   task_cur_count.SetAll(0);
   task_last_count.SetAll(0);
+  task_cur_quality.SetAll(0);
+  task_last_quality.SetAll(0);
+  task_cur_max_quality.SetAll(0);
+  task_last_max_quality.SetAll(0);
   task_exe_count.SetAll(0);
 
   dom_merit = 0;
@@ -587,19 +605,40 @@
 
 void cStats::PrintTasksData(const cString & filename)
 {
-  cDataFile & df = m_world->GetDataFile(filename);
+	cString file = filename;
 
-  df.WriteComment( "Avida tasks data" );
-  df.WriteTimeStamp();
-  df.WriteComment( "First column gives the current update, all further columns give the number" );
-  df.WriteComment( "of organisms that have the particular task as a component of the merit." );
+	// flag to print both tasks.dat and taskquality.dat
+	if (filename == "tasksq.dat")
+	{
+		file = "tasks.dat";
+		cDataFile & df2 = m_world->GetDataFile("taskquality.dat");
+		df2.WriteComment( "First column gives the current update, rest give average and max task quality" );
+		df2.Write( GetUpdate(), "Update");
+		for(int i = 0; i < task_last_count.GetSize(); i++) {
+			double qual=0;
+			if (task_last_count[i] > 0)
+				qual = task_last_quality[i]/(double)task_last_count[i];
+			df2.Write( qual, task_names[i] );
+			df2.Write( task_last_max_quality[i], "Max" );
+		}
+		df2.Endl();
+	}
 
+	// print tasks.dat
+	cDataFile & df = m_world->GetDataFile(file);
+	df.WriteComment( "Avida tasks data" );
+	df.WriteTimeStamp();
+	df.WriteComment( "First column gives the current update, next columns give the number" );
+	df.WriteComment( "of organisms that have the particular task as a component of their merit" );
 
-  df.Write( GetUpdate(),   "Update");
-  for(int i = 0; i < task_last_count.GetSize(); i++) {
-    df.Write( task_last_count[i], task_names[i] );
-  }
-  df.Endl();
+	df.Write( GetUpdate(),   "Update");
+	for(int i = 0; i < task_last_count.GetSize(); i++) {
+		df.Write( task_last_count[i], task_names[i] );
+	}
+	df.Endl();
+
+  //cDataFile & df = m_world->GetDataFile(filename);
+
 }
 
 

Modified: development/source/main/cStats.h
===================================================================
--- development/source/main/cStats.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cStats.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -207,6 +207,10 @@
 
   tArray<int> task_cur_count;
   tArray<int> task_last_count;
+  tArray<double> task_cur_quality;
+  tArray<double> task_last_quality;
+  tArray<double> task_cur_max_quality;
+  tArray<double> task_last_max_quality;
   tArray<int> task_exe_count;
 
   tArray<double> reaction_count;
@@ -413,7 +417,19 @@
   void IncExecuted() { num_executed++; }
 
   void AddCurTask(int task_num) { task_cur_count[task_num]++; }
+  void AddCurTaskQuality(int task_num, double quality) 
+  {  
+	  task_cur_quality[task_num] += quality;
+	  if (quality > task_cur_max_quality[task_num])
+		  task_cur_max_quality[task_num] = quality;
+  }
   void AddLastTask(int task_num) { task_last_count[task_num]++; }
+  void AddLastTaskQuality(int task_num, double quality) 
+  { 
+	  task_last_quality[task_num] += quality; 
+	  if (quality > task_last_max_quality[task_num])
+		  task_last_max_quality[task_num] = quality;
+  }
   void IncTaskExeCount(int task_num, int task_count) 
     { task_exe_count[task_num] += task_count; }
   void ZeroTasks();
@@ -460,7 +476,11 @@
   int GetTotLineages() const        { return tot_lineages; }
 
   int GetTaskCurCount(int task_num) const { return task_cur_count[task_num]; }
+  double GetTaskCurQuality(int task_num) const { return task_cur_quality[task_num]/(double)task_cur_count[task_num]; }  
   int GetTaskLastCount(int task_num) const {return task_last_count[task_num];}
+  double GetTaskLastQuality(int task_num) const {return task_last_quality[task_num]/(double)task_last_count[task_num];}
+  double GetTaskMaxCurQuality(int task_num) const { return task_cur_max_quality[task_num];}
+  double GetTaskMaxLastQuality(int task_num) const { return task_last_max_quality[task_num];}
   int GetTaskExeCount(int task_num) const { return task_exe_count[task_num]; }
 
   const tArray<double> & GetReactions() const { return reaction_count; }

Modified: development/source/main/cTaskEntry.h
===================================================================
--- development/source/main/cTaskEntry.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cTaskEntry.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -30,10 +30,11 @@
   cString m_desc;  // For more human-understandable output...
   int m_id;
   tTaskTest m_test_fun;
+  cString m_info;  // extra info (like the string or whatever to match)
 
 public:
-  cTaskEntry(const cString& name, const cString& desc, int in_id, tTaskTest test_fun)
-    : m_name(name), m_desc(desc), m_id(in_id), m_test_fun(test_fun)
+  cTaskEntry(const cString& name, const cString& desc, int in_id, tTaskTest test_fun, const cString& info)
+    : m_name(name), m_desc(desc), m_id(in_id), m_test_fun(test_fun), m_info(info)
   {
   }
   ~cTaskEntry() { ; }
@@ -42,6 +43,7 @@
   const cString& GetDesc() const { return m_desc; }
   const int GetID() const { return m_id; }
   const tTaskTest GetTestFun() const { return m_test_fun; }
+  const cString & GetInfo() const { return m_info; }
 };
 
 #endif

Modified: development/source/main/cTaskLib.cc
===================================================================
--- development/source/main/cTaskLib.cc	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cTaskLib.cc	2006-04-04 00:57:01 UTC (rev 556)
@@ -40,11 +40,12 @@
   return static_cast<double>(32 - bit_diff) / 32.0; 
 }
 
-cTaskEntry * cTaskLib::AddTask(const cString & name)
+cTaskEntry * cTaskLib::AddTask(const cString & name, const cString & info)
 {
   // Determine if this task is already in the active library.
   for (int i = 0; i < task_array.GetSize(); i++) {
-    if (task_array[i]->GetName() == name) {
+       if (task_array[i]->GetName() == name &&
+		   		task_array[i]->GetInfo() == info) {
       assert(task_array[i] != NULL);
       return task_array[i];
     }
@@ -319,6 +320,9 @@
   else if (name == "math_3AM")
     NewTask(name, "Math 3AM ((X+Y)^2+(Y+Z)^2)", &cTaskLib::Task_Math3in_AM);  
   
+  // match string task
+  if (name == "matchstr")  NewTask(name, "MatchStr", &cTaskLib::Task_MatchStr, 0, info);
+
 	// communication tasks
   else if (name == "comm_echo")
     NewTask(name, "Echo of Neighbor's Input", &cTaskLib::Task_CommEcho,
@@ -434,14 +438,14 @@
 ////////////////////////
 
 void cTaskLib::NewTask(const cString & name, const cString & desc,
-                       tTaskTest task_fun, int reqs)
+					   tTaskTest task_fun, int reqs, const cString & info)
 {
   if (reqs & REQ_NEIGHBOR_INPUT == true) use_neighbor_input = true;
   if (reqs & REQ_NEIGHBOR_OUTPUT == true) use_neighbor_output = true;
   
   const int id = task_array.GetSize();
   task_array.Resize(id+1);
-  task_array[id] = new cTaskEntry(name, desc, id, task_fun);
+  task_array[id] = new cTaskEntry(name, desc, id, task_fun, info);
 }
 
 
@@ -1734,6 +1738,44 @@
   return 0.0;
 }
 
+double cTaskLib::Task_MatchStr(cTaskContext* ctx) const
+{
+	tBuffer<int> temp_buf(ctx->output_buffer);
+	if (temp_buf[0] != 357913941)
+		return 0;
+
+	temp_buf.Pop(); // pop the signal value off of the buffer
+	if (temp_buf.GetNumStored() == 0)
+		return 0;
+
+	const cString & string_to_match = task_array[cur_task]->GetInfo();
+	int max_num_matched = 0;
+	//for (int i=0; i<temp_buf.GetNumStored(); i++)
+	//{
+		const int test_output = temp_buf[0];
+		int num_matched = 0;
+		int string_index;
+
+		for (int j=0; j<string_to_match.GetSize(); j++)
+		{	
+			string_index=string_to_match.GetSize()-j-1;		// start with last char in string
+			int k = 1 << j;
+			if ((string_to_match[string_index]=='0' && !(test_output & k)) || (string_to_match[string_index]=='1' && (test_output & k))) 
+				num_matched++;
+		}	
+		if (num_matched > max_num_matched)
+			max_num_matched = num_matched;
+	//}
+
+	double bonus = 0;
+	// return value between 0 & 1 representing the percentage of string that was matched
+	double base_bonus = double(max_num_matched)*2/(double)string_to_match.GetSize() - 1;
+	if (base_bonus > 0)
+		bonus = pow(base_bonus,2);
+	
+	return bonus;
+}
+
 double cTaskLib::Task_CommEcho(cTaskContext* ctx) const
 {
   const int test_output = ctx->output_buffer[0];

Modified: development/source/main/cTaskLib.h
===================================================================
--- development/source/main/cTaskLib.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/main/cTaskLib.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -36,6 +36,7 @@
   bool use_neighbor_output;
 
   // Active task information...
+  mutable int cur_task; // the ID of the task currently being tested
 
   enum req_list
   {
@@ -55,7 +56,7 @@
 
   int GetSize() const { return task_array.GetSize(); }
 
-  cTaskEntry* AddTask(const cString& name);
+  cTaskEntry * AddTask(const cString & name, const cString & info);
   const cTaskEntry& GetTask(int id) const;
   
   void SetupTests(cTaskContext& ctx) const;
@@ -65,7 +66,7 @@
   bool UseNeighborOutput() const { return use_neighbor_output; }
   
 private:  // Direct task related methods
-  void NewTask(const cString& name, const cString& desc, tTaskTest task_fun, int reqs = 0);
+  void NewTask(const cString& name, const cString& desc, tTaskTest task_fun, int reqs = 0, const cString& info="");
 
   inline double FractionalReward(unsigned int supplied, unsigned int correct);  
 
@@ -210,6 +211,8 @@
   double Task_Math3in_AL(cTaskContext* ctx) const;
   double Task_Math3in_AM(cTaskContext* ctx) const;
   
+  // match string tasks
+  double Task_MatchStr(cTaskContext* ctx) const;
   // Communication Tasks...
   double Task_CommEcho(cTaskContext* ctx) const;
   double Task_CommNot(cTaskContext* ctx) const;
@@ -221,6 +224,7 @@
 
 inline double cTaskLib::TestOutput(const cTaskEntry& task, cTaskContext* ctx) const
 {
+  cur_task = task.GetID();
   tTaskTest test_fun = task.GetTestFun();
   return (this->*test_fun)(ctx);
 }

Modified: development/source/tools/cGenesis.cc
===================================================================
--- development/source/tools/cGenesis.cc	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/tools/cGenesis.cc	2006-04-04 00:57:01 UTC (rev 556)
@@ -45,14 +45,22 @@
 void cGenesis::AddInput(const cString & in_name, int in_value)
 {
   cString in_string;
-  in_string.Set("%s %d", in_name(), in_value);
+  in_string.Set("%s %s, ");
+  in_string += in_name;
+  in_string += "(), ";
+  in_string += in_value;
+  in_string += "()";
   AddLine(in_string);
 }
 
 void cGenesis::AddInput(const cString & in_name, const cString & in_value)
 {
   cString in_string;
-  in_string.Set("%s %s", in_name(), in_value());
+  in_string.Set("%s %s, ");
+  in_string += in_name;
+  in_string += "(), ";
+  in_string += in_value;
+  in_string += "()";
   AddLine(in_string);
 }
 

Modified: development/source/tools/cThread.h
===================================================================
--- development/source/tools/cThread.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/tools/cThread.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -10,6 +10,10 @@
 #ifndef cThread_h
 #define cThread_h
 
+#ifndef NULL
+#define NULL 0
+#endif
+
 #include <pthread.h>
 
 class cThread

Modified: development/source/tools/tArray.h
===================================================================
--- development/source/tools/tArray.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/tools/tArray.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -11,6 +11,10 @@
 #ifndef tArray_h
 #define tArray_h
 
+#ifndef NULL
+#define NULL 0
+#endif
+
 #include <assert.h>
 
 #ifndef NULL

Modified: development/source/tools/tBuffer.h
===================================================================
--- development/source/tools/tBuffer.h	2006-04-03 22:20:00 UTC (rev 555)
+++ development/source/tools/tBuffer.h	2006-04-04 00:57:01 UTC (rev 556)
@@ -54,6 +54,12 @@
     while (offset >= data.GetSize()) offset -= data.GetSize();
   }
 
+  void Pop(){
+	  total--;
+	  offset--;
+	  while (offset < 0) offset += data.GetSize();
+  }
+
   T operator[](int i) const
   {
     int index = offset - i - 1;




More information about the Avida-cvs mailing list