[Avida-SVN] r1488 - in development/source: actions cpu main

goingssh at myxo.css.msu.edu goingssh at myxo.css.msu.edu
Thu Apr 19 05:14:42 PDT 2007


Author: goingssh
Date: 2007-04-19 08:14:41 -0400 (Thu, 19 Apr 2007)
New Revision: 1488

Modified:
   development/source/actions/PopulationActions.cc
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/main/cEnvironment.cc
   development/source/main/cGenomeUtil.cc
   development/source/main/cGenomeUtil.h
   development/source/main/cOrganism.h
   development/source/main/cTaskLib.cc
Log:
Added event to inject total population with random organisms plus repro command at end
Added event to modify optimize task min and max but isn't fully implemented yet
Added 2 instructions: IObuf-add1 and IObuf-add0 which simply put a 1 or 0 respectively in the output buffer of an org when executed without calling DoOutput
Added genomeutil function to return random org except without using instructions that are set to 0 weight in inst set, and that adds repro inst to end of genome
Added "addoutput" function to organism so can put val in output buffer without going through whole task checking function chain
Modified optimize task to work with binary genomes as well as the normal way still


Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc	2007-04-19 10:08:57 UTC (rev 1487)
+++ development/source/actions/PopulationActions.cc	2007-04-19 12:14:41 UTC (rev 1488)
@@ -129,6 +129,50 @@
 
 
 /*
+ Injects randomly generated genomes into the entire population, plus a repro inst on the end,
+ with the caveat that any instructions set to 0 probability of mutating into the genome 
+ are not included.
+ 
+ Parameters:
+   length (integer) [required]
+     Number of instructions in the randomly generated genome (actual length will be +1 with repro).
+   merit (double) default: -1
+     The initial merit of the organism. If set to -1, this is ignored.
+   lineage label (integer) default: 0
+     An integer that marks all descendants of this organism.
+   neutral metric (double) default: 0
+     A double value that randomly drifts over time.
+*/
+class cActionInjectAllRandomRepro : public cAction
+{
+private:
+  int m_length;
+  double m_merit;
+  int m_lineage_label;
+  double m_neutral_metric;
+public:
+  cActionInjectAllRandomRepro(cWorld* world, const cString& args) : cAction(world, args), m_merit(-1), m_lineage_label(0), m_neutral_metric(0)
+  {
+    cString largs(args);
+    m_length = largs.PopWord().AsInt();
+    if (largs.GetSize()) m_merit = largs.PopWord().AsDouble();
+    if (largs.GetSize()) m_lineage_label = largs.PopWord().AsInt();
+    if (largs.GetSize()) m_neutral_metric = largs.PopWord().AsDouble();
+  }
+  
+  static const cString GetDescription() { return "Arguments: <int length> [double merit=-1] [int lineage_label=0] [double neutral_metric=0]"; }
+
+  void Process(cAvidaContext& ctx)
+  {
+	  for (int i = 0; i < m_world->GetPopulation().GetSize(); i++)
+	  {
+		  cGenome genome = cGenomeUtil::RandomGenomeWithoutZeroRedundantsPlusRepro(ctx, m_length, m_world->GetHardwareManager().GetInstSet());
+		  m_world->GetPopulation().Inject(genome, i, m_merit, m_lineage_label, m_neutral_metric);
+	  }
+  }
+};
+
+/*
  Injects identical organisms into all cells of the population.
  
  Parameters:
@@ -1305,8 +1349,22 @@
   }
 };
 
+class cActionSetOptimizeMinMax : public cAction
+{
 
+public:
+  cActionSetOptimizeMinMax(cWorld* world, const cString& args) : cAction(world, args) { ; }
 
+  static const cString GetDescription() { return "No Arguments"; }
+
+  void Process(cAvidaContext& ctx)
+  {
+    for (int i = 0; i < m_world->GetPopulation().GetSize(); i++) {
+      m_world->GetPopulation().GetCell(i).GetOrganism();
+    }
+  }
+};
+
 void RegisterPopulationActions(cActionLibrary* action_lib)
 {
   action_lib->Register<cActionInject>("Inject");
@@ -1345,6 +1403,7 @@
   // @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.
   action_lib->Register<cActionInject>("inject");
   action_lib->Register<cActionInjectRandom>("inject_random");
+  action_lib->Register<cActionInjectAllRandomRepro>("inject_all_random_repro");
   action_lib->Register<cActionInjectAll>("inject_all");
   action_lib->Register<cActionInjectRange>("inject_range");
   action_lib->Register<cActionInject>("inject_sequence");
@@ -1368,4 +1427,6 @@
 
   action_lib->Register<cActionConnectCells>("connect_cells");
   action_lib->Register<cActionDisconnectCells>("disconnect_cells");
+
+  action_lib->Register<cActionSetOptimizeMinMax>("SetOptimizeMinMax");
 }

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2007-04-19 10:08:57 UTC (rev 1487)
+++ development/source/cpu/cHardwareCPU.cc	2007-04-19 12:14:41 UTC (rev 1488)
@@ -204,7 +204,10 @@
     tInstLibEntry<tMethod>("donate-kin", &cHardwareCPU::Inst_DonateKin),
     tInstLibEntry<tMethod>("donate-edt", &cHardwareCPU::Inst_DonateEditDist),
     tInstLibEntry<tMethod>("donate-NUL", &cHardwareCPU::Inst_DonateNULL),
-    
+
+	tInstLibEntry<tMethod>("IObuf-add1", &cHardwareCPU::Inst_IOBufAdd1),
+    tInstLibEntry<tMethod>("IObuf-add0", &cHardwareCPU::Inst_IOBufAdd0),
+
     tInstLibEntry<tMethod>("rotate-l", &cHardwareCPU::Inst_RotateL),
     tInstLibEntry<tMethod>("rotate-r", &cHardwareCPU::Inst_RotateR),
     
@@ -3167,6 +3170,16 @@
   return true;
 }
 
+bool cHardwareCPU::Inst_IOBufAdd1(cAvidaContext& ctx)
+{ 
+	organism->AddOutput(1);
+	return true;
+}
+bool cHardwareCPU::Inst_IOBufAdd0(cAvidaContext& ctx)
+{ 
+	organism->AddOutput(0);
+	return true; 
+}
 
 bool cHardwareCPU::Inst_RotateL(cAvidaContext& ctx)
 {

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2007-04-19 10:08:57 UTC (rev 1487)
+++ development/source/cpu/cHardwareCPU.h	2007-04-19 12:14:41 UTC (rev 1488)
@@ -437,6 +437,9 @@
   bool Inst_SearchB(cAvidaContext& ctx);
   bool Inst_MemSize(cAvidaContext& ctx);
 
+  bool Inst_IOBufAdd1(cAvidaContext& ctx);
+  bool Inst_IOBufAdd0(cAvidaContext& ctx);
+
   // Environment
 
   bool Inst_RotateL(cAvidaContext& ctx);

Modified: development/source/main/cEnvironment.cc
===================================================================
--- development/source/main/cEnvironment.cc	2007-04-19 10:08:57 UTC (rev 1487)
+++ development/source/main/cEnvironment.cc	2007-04-19 12:14:41 UTC (rev 1488)
@@ -886,12 +886,11 @@
     
     // Have all task counts been met?
     if (task_count < cur_req->GetMinTaskCount()) continue;
-    
-    // Have divide task reqs been met?
-    // If div_type is 0 we only check on IO, if 1 we only check on divide,
-    // if 2 we check always
     if (task_count >= cur_req->GetMaxTaskCount()) continue;
 
+	// Have divide task reqs been met?
+    // If div_type is 0 we only check on IO, if 1 we only check on divide,
+    // if 2 we check always
     int div_type = cur_req->GetDivideOnly();
     if (div_type == 1 && !on_divide) continue;
     if (div_type == 0 && on_divide) continue;

Modified: development/source/main/cGenomeUtil.cc
===================================================================
--- development/source/main/cGenomeUtil.cc	2007-04-19 10:08:57 UTC (rev 1487)
+++ development/source/main/cGenomeUtil.cc	2007-04-19 12:14:41 UTC (rev 1488)
@@ -328,4 +328,17 @@
   return genome;
 }
 
+cGenome cGenomeUtil::RandomGenomeWithoutZeroRedundantsPlusRepro(cAvidaContext& ctx, int length, const cInstSet& inst_set)
+{
+  cGenome genome(length+1);
+  for (int i = 0; i < length; i++) {
+	  cInstruction inst = inst_set.GetRandomInst(ctx);
+	  while (inst_set.GetRedundancy(inst)==0)
+		  inst = inst_set.GetRandomInst(ctx);
+    genome[i] = inst;
+  }
+  genome[length] = inst_set.GetInst("repro");
+  return genome;
+}
 
+

Modified: development/source/main/cGenomeUtil.h
===================================================================
--- development/source/main/cGenomeUtil.h	2007-04-19 10:08:57 UTC (rev 1487)
+++ development/source/main/cGenomeUtil.h	2007-04-19 12:14:41 UTC (rev 1488)
@@ -72,6 +72,7 @@
   
   // ========= Genome Construction =========
   static cGenome RandomGenome(cAvidaContext& ctx, int length, const cInstSet& inst_set);
+  static cGenome RandomGenomeWithoutZeroRedundantsPlusRepro(cAvidaContext& ctx, int length, const cInstSet& inst_set);
 };
 
 

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2007-04-19 10:08:57 UTC (rev 1487)
+++ development/source/main/cOrganism.h	2007-04-19 12:14:41 UTC (rev 1488)
@@ -197,6 +197,7 @@
   void DoInput(const int value);
   void DoOutput(cAvidaContext& ctx, const int value, const bool on_divide = false);
   void ClearInput() { m_input_buf.Clear(); }
+  void AddOutput(int val) { m_output_buf.Add(val); }
 
   // --------  Divide Methods  --------
   bool Divide_CheckViable();

Modified: development/source/main/cTaskLib.cc
===================================================================
--- development/source/main/cTaskLib.cc	2007-04-19 10:08:57 UTC (rev 1487)
+++ development/source/main/cTaskLib.cc	2007-04-19 12:14:41 UTC (rev 1488)
@@ -2120,19 +2120,34 @@
 
 	// Integer Arguments
 	schema.AddEntry("function", 0, cArgSchema::SCHEMA_INT);
+	schema.AddEntry("binary", 1, 0);
+	schema.AddEntry("varlength", 2, 8);
+	// Double Arguments
+	schema.AddEntry("max_Fx", 0, 1.0);
+	schema.AddEntry("min_Fx", 1, 0.0);
 
+
 	cArgContainer* args = cArgContainer::Load(argstr, schema, errors);
 	if (args) 
 	{
-		switch (args->GetInt(0))
+		if (args->GetInt(1))
 		{
-		case 1:
-			envreqs.SetMinOutputs(1);
-		case 2:
-			envreqs.SetMinOutputs(2);
-		case 3:
-			envreqs.SetMinOutputs(2);
-		};
+			envreqs.SetMinOutputs(args->GetInt(2)*2);
+		}
+		else 
+		{
+			// once have ability to change args should in each of these cases change the max/min
+			// to the appropriate defaults for this function
+			switch (args->GetInt(0))
+			{
+			case 1:
+				envreqs.SetMinOutputs(1);
+			case 2:
+				envreqs.SetMinOutputs(2);
+			case 3:
+				envreqs.SetMinOutputs(2);
+			};
+		}
 
 		NewTask(name, "Optimize", &cTaskLib::Task_Optimize, 0, args);
 	}
@@ -2148,46 +2163,65 @@
 
 	// which function are we currently checking?
 	const int function = ctx.GetTaskEntry()->GetArguments().GetInt(0);
-	
-	// always need x, at least for now, turn it into a double between 0 and 1
-	unsigned outy, outx = ctx.GetOutputBuffer()[0];
-	double y, x = (double)outx / 0xffffffff;
-	
-	switch(function)
-	{
-	case 1:
-		quality = 1 - x;
-		break;
 
-	case 2:
-		// for F2 need y as well
-		outy = ctx.GetOutputBuffer()[1];
-		y = (double)outy / 0xffff;
-		quality = 1 - ((1+y)*(1-sqrt(x/(1+y))))/2.0;	// F2
-		break;
+	 // always get x&y, at least for now, turn it into a double between 0 and 1
+	double y, x;
 
-	case 3:
-		// for F3 need y as well
-		outy = ctx.GetOutputBuffer()[1];
-		y = (double)outy / 0xffff;
-		quality = 1 - ((1+y)*(1-pow(x/(1+y),2)))/2.0;	// F3
-		break;
+	 const cArgContainer& args = ctx.GetTaskEntry()->GetArguments();
+	 if (args.GetInt(1))
+	 {
+		int len = args.GetInt(2);
+		int tempX = 0, tempY=0;
+		for (int i=len-1; i>=0; i--)
+		{
+			tempX += ctx.GetOutputBuffer()[i+len]*pow(2.0,(len-1)-i);
+			tempY += ctx.GetOutputBuffer()[i]*pow(2.0,(len-1)-i);
+		}
+		x = (double)tempX/(pow(2.0,len)-1);
+		y = (double)tempY/(pow(2.0,len)-1);
+	 }
+	 else
+	 {
+		x = (double)ctx.GetOutputBuffer()[0] / 0xffffffff;
+		y = (double)ctx.GetOutputBuffer()[1] / 0xffffffff;	
+	 }
 
-	default:
-		quality = .001;
-	}
+	 switch(function)
+	 {
+	 case 1:
+		 quality = 1 - x;
+		 break;
 
-	// because want org to only have 1 shot to use outputs for all functions at once, even if they
-	// output numbers that give a quality of 0 on a function, still want to mark it as completed
-	// so give it a very low quality instead of 0 (if using limited resources they still will get
-	// no reward because set the minimum consumed to max*.001, meaning even if they get the max
-	// possible fraction they'll be below minimum allowed consumed and will consume nothing
+	 case 2:
+		 quality = 1 - ((1+y)*(1-sqrt(x/(1+y))))/2.0;	// F2
+		 break;
+
+	 case 3:
+		 quality = 1 - ((1+y)*(1-pow(x/(1+y),2)))/2.0;	// F3
+		 break;
+
+	 case 4:
+		 quality = 1 - (( (1+y)*(1 - sqrt(x/(1+y)) - (x/(1+y))*sin(3.14159*x*10) ) +.76) / 2.76);
+		 break;
+
+	 default:
+		 quality = .001;
+	 }
+
+	 // because want org to only have 1 shot to use outputs for all functions at once, even if they
+	 // output numbers that give a quality of 0 on a function, still want to mark it as completed
+	 // so give it a very low quality instead of 0 (if using limited resources they still will get
+	 // no reward because set the minimum consumed to max*.001, meaning even if they get the max
+	 // possible fraction they'll be below minimum allowed consumed and will consume nothing
+
+	 if (quality > 1)
+		 cout << "\n\nquality > 1, wtf?!\n\n";
+	 if (quality < .001)
+		 return .001;
+	 else
+		 return quality;
+
 	
-	if (quality < .001)
-		return .001;
-	else
-		return quality;
-	
 	return 0;
 }
 




More information about the Avida-cvs mailing list