[Avida-cvs] [avida-svn] r464 - in trunk/source: . cpu event main support/config tools

goingssh@myxo.css.msu.edu goingssh at myxo.css.msu.edu
Mon Feb 6 10:27:41 PST 2006


Author: goingssh
Date: 2006-02-06 13:27:40 -0500 (Mon, 06 Feb 2006)
New Revision: 464

Modified:
   trunk/source/cpu/cHardwareCPU.cc
   trunk/source/cpu/cHardwareCPU.h
   trunk/source/defs.h
   trunk/source/event/cPopulationEventFactory.cc
   trunk/source/main/cEnvironment.cc
   trunk/source/main/cReactionProcess.h
   trunk/source/main/cReactionResult.cc
   trunk/source/main/cReactionResult.h
   trunk/source/main/cTaskEntry.cc
   trunk/source/main/cTaskEntry.h
   trunk/source/main/cTaskLib.cc
   trunk/source/main/cTaskLib.h
   trunk/source/support/config/environment.9resource
   trunk/source/tools/tBuffer.h
Log:
Added functionality for the string-matching problem

Modified: trunk/source/cpu/cHardwareCPU.cc
===================================================================
--- trunk/source/cpu/cHardwareCPU.cc	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/cpu/cHardwareCPU.cc	2006-02-06 18:27:40 UTC (rev 464)
@@ -202,7 +202,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),
@@ -385,6 +385,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)
@@ -420,6 +421,7 @@
   cur_thread = 0;
 
   mal_active = false;
+  executedmatchstrings = false;
 
 #ifdef INSTRUCTION_COSTS
   // instruction cost arrays
@@ -2827,6 +2829,15 @@
   return true;
 }
 
+bool cHardwareCPU::Inst_MatchStrings()
+{
+	if (executedmatchstrings)
+		return false;
+	organism->DoOutput(1234);
+	executedmatchstrings = true;
+	return true;
+}
+
 bool cHardwareCPU::Inst_Send()
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);

Modified: trunk/source/cpu/cHardwareCPU.h
===================================================================
--- trunk/source/cpu/cHardwareCPU.h	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/cpu/cHardwareCPU.h	2006-02-06 18:27:40 UTC (rev 464)
@@ -99,6 +99,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
@@ -393,6 +394,7 @@
   bool Inst_TaskStackLoad();
   bool Inst_TaskPut();
   bool Inst_TaskIO();
+  bool Inst_MatchStrings();
   bool Inst_Send();
   bool Inst_Receive();
   bool Inst_Sense();

Modified: trunk/source/defs.h
===================================================================
--- trunk/source/defs.h	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/defs.h	2006-02-06 18:27:40 UTC (rev 464)
@@ -161,7 +161,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: trunk/source/event/cPopulationEventFactory.cc
===================================================================
--- trunk/source/event/cPopulationEventFactory.cc	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/event/cPopulationEventFactory.cc	2006-02-06 18:27:40 UTC (rev 464)
@@ -584,6 +584,25 @@
       population->GetStats().PrintTasksExeData(fname);
     }
   };
+
+  class cEvent_print_reaction_data : public cPopulationEvent {
+  private:
+    cString fname;
+  public:
+    const cString GetName() const { return "print_reaction_data"; }
+    const cString GetDescription() const { return "print_reaction_data  [cString fname=\"reaction.dat\"]"; }
+    
+    void Configure(const cString& in_args)
+  {
+      m_args = in_args;
+      cString args(in_args);
+      if (args == "") fname="reaction.dat"; else fname=args.PopWord();
+  }
+    ///// print_tasks_exe_data /////
+    void Process(){
+      population->GetStats().PrintReactionData(fname);
+    }
+  };
   
   ///// print_resource_data /////
   

Modified: trunk/source/main/cEnvironment.cc
===================================================================
--- trunk/source/main/cEnvironment.cc	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/main/cEnvironment.cc	2006-02-06 18:27:40 UTC (rev 464)
@@ -237,6 +237,12 @@
 	return false;
       new_process->SetDetectionError(var_value.AsDouble());
     }
+	else if (var_name == "string") {
+		//if (!AssertInputValid(var_value, "string", var_type, var_value))
+		//	return false;
+		new_process->SetMatchString(var_value);
+	}
+
     else {
       cerr << "Error: Unknown process variable '" << var_name
 	   << "' in reaction '" << reaction->GetName() << "'" << endl;
@@ -440,10 +446,12 @@
   }
 
   // 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;
@@ -752,15 +760,15 @@
     // 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;
-    }
+		       reaction_count) == false) continue;
 
+	// Mark this task as performed...
+    result.MarkTask(task_id, task_quality);
+    
+
     // And lets process it!
     DoProcesses(cur_reaction->GetProcesses(), resource_count,
 		task_quality, result);

Modified: trunk/source/main/cReactionProcess.h
===================================================================
--- trunk/source/main/cReactionProcess.h	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/main/cReactionProcess.h	2006-02-06 18:27:40 UTC (rev 464)
@@ -7,6 +7,9 @@
 
 #ifndef REACTION_PROCESS_HH
 #define REACTION_PROCESS_HH
+#ifndef STRING_HH
+#include "cString.h"
+#endif
 
 #include <iostream>
 
@@ -23,8 +26,9 @@
   double max_fraction;   // Max fraction of avaiable resources useable.
   cResource * product;   // Output resource.
   double conversion;     // Conversion factor.
-  bool lethal;		 // Lethality of reaction
+  bool lethal;			 // Lethality of reaction
   int inst_id;           // Instruction to be triggered if reaction successful.
+  cString match_string;	 // Bit string to match if this is a match string reaction
 
   // Resource detection
   cResource * detect;    // Resource Measured
@@ -47,6 +51,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; }
@@ -61,6 +66,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: trunk/source/main/cReactionResult.cc
===================================================================
--- trunk/source/main/cReactionResult.cc	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/main/cReactionResult.cc	2006-02-06 18:27:40 UTC (rev 464)
@@ -15,6 +15,7 @@
   , resources_produced(num_resources)
   , resources_detected(num_resources)
   , tasks_done(num_tasks)
+  , tasks_quality(num_tasks)
   , receive_tasks_done(num_tasks)
   , send_tasks_done(num_tasks)
   , reactions_triggered(num_reactions)
@@ -41,6 +42,7 @@
   resources_produced.SetAll(0.0);
   resources_detected.SetAll(-1.0);
   tasks_done.SetAll(false);
+  tasks_quality.SetAll(0.0);
   receive_tasks_done.SetAll(false);
   send_tasks_done.SetAll(false);
   reactions_triggered.SetAll(false);
@@ -76,10 +78,11 @@
  lethal = flag;
 }
 
-void cReactionResult::MarkTask(int id)
+void cReactionResult::MarkTask(int id, const double quality)
 {
   ActivateReaction();
   tasks_done[id] = true;
+  tasks_quality[id]+=quality;
 }
 
 
@@ -159,6 +162,12 @@
   return tasks_done[id];
 }
 
+double cReactionResult::TaskQuality(int id)
+{
+	if (GetActive() == false) return false;
+	return tasks_quality[id];
+}
+
 double cReactionResult::GetAddBonus()
 {
   if (GetActive() == false) return 0.0;

Modified: trunk/source/main/cReactionResult.h
===================================================================
--- trunk/source/main/cReactionResult.h	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/main/cReactionResult.h	2006-02-06 18:27:40 UTC (rev 464)
@@ -20,6 +20,7 @@
   tArray<double> resources_produced;
   tArray<double> resources_detected;  //Initialize to -1.0
   tArray<bool> tasks_done;
+  tArray<bool> tasks_quality;
   tArray<bool> receive_tasks_done;
   tArray<bool> send_tasks_done;
   tArray<bool> reactions_triggered;
@@ -41,7 +42,7 @@
   void Produce(int id, double num);
   void Detect(int id, double num);
   void Lethal(bool flag);
-  void MarkTask(int id);
+  void MarkTask(int id, const double quality=1);
   void MarkReceiveTask(int id);
   void MarkSendTask(int id);
   void MarkReaction(int id);
@@ -55,6 +56,7 @@
   bool GetLethal();
   bool ReactionTriggered(int id);
   bool TaskDone(int id);
+  double TaskQuality(int id);
   double GetAddBonus();
   double GetMultBonus();
   tArray<int> & GetInstArray();

Modified: trunk/source/main/cTaskEntry.cc
===================================================================
--- trunk/source/main/cTaskEntry.cc	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/main/cTaskEntry.cc	2006-02-06 18:27:40 UTC (rev 464)
@@ -14,11 +14,12 @@
 ////////////////
 
 cTaskEntry::cTaskEntry(const cString & _name, const cString & _desc,
-		       int _id, tTaskTest _test_fun)
+		       int _id, tTaskTest _test_fun, const cString & _info)
   : name(_name)
   , desc(_desc)
   , id(_id)
   , test_fun(_test_fun)
+  , info(_info)
 {
 }
 

Modified: trunk/source/main/cTaskEntry.h
===================================================================
--- trunk/source/main/cTaskEntry.h	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/main/cTaskEntry.h	2006-02-06 18:27:40 UTC (rev 464)
@@ -21,15 +21,17 @@
   cString desc;  // For more human-understandable output...
   int id;
   tTaskTest test_fun;
+  cString info;  // extra info (like the string or whatever to match)
 public:
   cTaskEntry(const cString & _name, const cString & _desc, int _id,
-	     tTaskTest _test_fun);
+	     tTaskTest _test_fun, const cString & info);
   ~cTaskEntry();
 
   const cString & GetName()    const { return name; }
   const cString & GetDesc() const { return desc; }
   const int       GetID()      const { return id; }
   const tTaskTest GetTestFun() const { return test_fun; }
+  const cString & GetInfo() const { return info; }
 };
 
 #endif

Modified: trunk/source/main/cTaskLib.cc
===================================================================
--- trunk/source/main/cTaskLib.cc	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/main/cTaskLib.cc	2006-02-06 18:27:40 UTC (rev 464)
@@ -17,6 +17,7 @@
 #include <math.h>
 #include <limits.h>
 }
+#include <iostream>
 
 using namespace std;
 
@@ -39,11 +40,12 @@
   }
 }
 
-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];
     }
@@ -52,6 +54,7 @@
   // Match up this name to its corresponding task
   const int start_size = task_array.GetSize();
 
+  
   if (name == "echo")      NewTask(name, "Echo", &cTaskLib::Task_Echo);
   else if (name == "add")  NewTask(name, "Add",  &cTaskLib::Task_Add);
   else if (name == "sub")  NewTask(name, "Sub",  &cTaskLib::Task_Sub);
@@ -318,6 +321,8 @@
   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,6 +439,7 @@
 
 double cTaskLib::TestOutput(const cTaskEntry & task) const
 {
+  cur_task = task.GetID();
   tTaskTest test_fun = task.GetTestFun();
   return (this->*test_fun)();
 }
@@ -444,14 +450,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);
 }
 
 
@@ -1666,6 +1672,42 @@
   return 0.0;
 }
 
+double cTaskLib::Task_MatchStr() const
+{
+	if (output_buffer[0] != 1234)
+		return 0;
+
+	output_buffer.Pop(); // pop the 1234 value off of the buffer
+	if (output_buffer.GetNumStored() == 0)
+		return 0;
+
+	const cString & string_to_match = task_array[cur_task]->GetInfo();
+	int max_num_matched = 0;
+	//for (int i=0; i<output_buffer.GetNumStored(); i++)
+	//{
+		const int test_output = output_buffer[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;
+			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;
+	//}
+
+	// return # of bits matched over the expected 50% that would match just by chance 
+	// this value is multiplied into the bonus
+	int bonus = max_num_matched - string_to_match.GetSize()/2;
+	if (bonus < 0)
+		bonus = 0;
+	return (double) bonus;
+}
+
 double cTaskLib::Task_CommEcho() const
 {
   const int test_output = output_buffer[0];

Modified: trunk/source/main/cTaskLib.h
===================================================================
--- trunk/source/main/cTaskLib.h	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/main/cTaskLib.h	2006-02-06 18:27:40 UTC (rev 464)
@@ -38,6 +38,7 @@
   mutable tList<tBuffer<int> > other_input_buffers;
   mutable tList<tBuffer<int> > other_output_buffers;
   mutable int logic_id;
+  mutable int cur_task; // the ID of the task currently being tested
 
   enum req_list { REQ_NEIGHBOR_INPUT=1,
 		  REQ_NEIGHBOR_OUTPUT=2, 
@@ -52,7 +53,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(const tBuffer<int> & inputs,
@@ -66,7 +67,7 @@
 
 private:  // Direct task related methods
   void NewTask(const cString & name, const cString & desc,
-	       tTaskTest task_fun, int reqs=0);
+	       tTaskTest task_fun, int reqs=0, const cString & info="");
   void SetupLogicTests(const tBuffer<int> & inputs,
 		       const tBuffer<int> & outputs) const;
 
@@ -211,6 +212,8 @@
   double Task_Math3in_AL() const;
   double Task_Math3in_AM() const;
   
+  // match string tasks
+  double Task_MatchStr() const;
   // Communication Tasks...
   double Task_CommEcho() const;
   double Task_CommNot() const;

Modified: trunk/source/support/config/environment.9resource
===================================================================
--- trunk/source/support/config/environment.9resource	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/support/config/environment.9resource	2006-02-06 18:27:40 UTC (rev 464)
@@ -1,33 +1,38 @@
-##############################################################################
-#
-# This is the setup file for the task/resource system.  From here, you can
-# setup the available resources (including their inflow and outflow rates) as
-# well as the reactions that the organisms can trigger by performing tasks.
-#
-# This environmental setup is that of a chemostat with 9 resources, each
-# with a unique task corresponding to them.  All resources have a set inflow
-# of 100, and outflow rate of 0.01, setting their an equilibrium level at
-# 10,000.  Given a maximum population size of 3600 and neighborhood size of
-# 9, the fraction of the resource absorbable in each reaction is simply
-# 9/3600 or 0.0025.
-#
-# For information on how to use this file, see:  doc/environment.html
-# For other sample environments, see:  source/support/config/ 
-#
-##############################################################################
-
-RESOURCE  resNOT:inflow=100:outflow=0.01   resNAND:inflow=100:outflow=0.01
-RESOURCE  resAND:inflow=100:outflow=0.01   resORN:inflow=100:outflow=0.01
-RESOURCE  resOR:inflow=100:outflow=0.01    resANDN:inflow=100:outflow=0.01
-RESOURCE  resNOR:inflow=100:outflow=0.01   resXOR:inflow=100:outflow=0.01
-RESOURCE  resEQU:inflow=100:outflow=0.01
-
-REACTION  NOT  not   process:resource=resNOT:value=1.0:frac=0.0025:max=25
-REACTION  NAND nand  process:resource=resNAND:value=1.0:frac=0.0025:max=25
-REACTION  AND  and   process:resource=resAND:value=2.0:frac=0.0025:max=25
-REACTION  ORN  orn   process:resource=resORN:value=2.0:frac=0.0025:max=25
-REACTION  OR   or    process:resource=resOR:value=4.0:frac=0.0025:max=25
-REACTION  ANDN andn  process:resource=resANDN:value=4.0:frac=0.0025:max=25
-REACTION  NOR  nor   process:resource=resNOR:value=8.0:frac=0.0025:max=25
-REACTION  XOR  xor   process:resource=resXOR:value=8.0:frac=0.0025:max=25
-REACTION  EQU  equ   process:resource=resEQU:value=16.0:frac=0.0025:max=25
+##############################################################################
+#
+# This is the setup file for the task/resource system.  From here, you can
+# setup the available resources (including their inflow and outflow rates) as
+# well as the reactions that the organisms can trigger by performing tasks.
+#
+# This environmental setup is that of a chemostat with 9 resources, each
+# with a unique task corresponding to them.  All resources have a set inflow
+# of 100, and outflow rate of 0.01, setting their an equilibrium level at
+# 10,000.  Given a maximum population size of 3600 and neighborhood size of
+# 9, the fraction of the resource absorbable in each reaction is simply
+# 9/3600 or 0.0025.
+#
+# For information on how to use this file, see:  doc/environment.html
+# For other sample environments, see:  source/support/config/ 
+#
+##############################################################################
+
+RESOURCE  resNOT:inflow=100:outflow=0.01   resNAND:inflow=100:outflow=0.01
+RESOURCE  resAND:inflow=100:outflow=0.01   resORN:inflow=100:outflow=0.01
+RESOURCE  resOR:inflow=100:outflow=0.01    resANDN:inflow=100:outflow=0.01
+RESOURCE  resNOR:inflow=100:outflow=0.01   resXOR:inflow=100:outflow=0.01
+RESOURCE  resEQU:inflow=100:outflow=0.01
+
+REACTION  NOT  not   process:resource=resNOT:value=1.0:frac=0.0025:max=25
+REACTION  NAND nand  process:resource=resNAND:value=1.0:frac=0.0025:max=25
+REACTION  AND  and   process:resource=resAND:value=2.0:frac=0.0025:max=25
+REACTION  ORN  orn   process:resource=resORN:value=2.0:frac=0.0025:max=25
+REACTION  OR   or    process:resource=resOR:value=4.0:frac=0.0025:max=25
+REACTION  ANDN andn  process:resource=resANDN:value=4.0:frac=0.0025:max=25
+REACTION  NOR  nor   process:resource=resNOR:value=8.0:frac=0.0025:max=25
+REACTION  XOR  xor   process:resource=resXOR:value=8.0:frac=0.0025:max=25
+REACTION  EQU  equ   process:resource=resEQU:value=16.0:frac=0.0025:max=25
+
+
+REACTION  MATCHSTR matchstr   process:resource=resMATCHSTR:value=16.0:frac=0.0025:max=25:string=11111111111111111111111111111111
+
+

Modified: trunk/source/tools/tBuffer.h
===================================================================
--- trunk/source/tools/tBuffer.h	2006-02-05 15:32:59 UTC (rev 463)
+++ trunk/source/tools/tBuffer.h	2006-02-06 18:27:40 UTC (rev 464)
@@ -52,6 +52,12 @@
     while (offset >= data.GetSize()) offset -= data.GetSize();
   }
 
+  void Pop(){
+	  total--;
+	  offset--;
+	  while (offset < 0) offset += data.GetSize();
+  }
+
   void ZeroNumAdds() { total = 0; }
 
   T operator[] (int i) const {




More information about the Avida-cvs mailing list