[Avida-SVN] r1419 - development/source/main

goingssh at myxo.css.msu.edu goingssh at myxo.css.msu.edu
Wed Mar 21 11:25:49 PDT 2007


Author: goingssh
Date: 2007-03-21 14:25:49 -0400 (Wed, 21 Mar 2007)
New Revision: 1419

Modified:
   development/source/main/cTaskLib.cc
   development/source/main/cTaskLib.h
Log:
Readding function optimization tasks



Modified: development/source/main/cTaskLib.cc
===================================================================
--- development/source/main/cTaskLib.cc	2007-03-21 18:19:27 UTC (rev 1418)
+++ development/source/main/cTaskLib.cc	2007-03-21 18:25:49 UTC (rev 1419)
@@ -345,6 +345,9 @@
   else if (name == "fibonacci_seq")
     Load_FibonacciSequence(name, info, envreqs, errors);
 
+   // Optimization Tasks
+  if (name == "optimize")
+	  Load_Optimize(name, info, envreqs, errors);
 
   if (name == "mult")
     Load_Mult(name, info, envreqs, errors);
@@ -2111,8 +2114,83 @@
   return 0.0;
 }
 
+void cTaskLib::Load_Optimize(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
+{
+	cArgSchema schema;
 
+	// Integer Arguments
+	schema.AddEntry("function", 0, cArgSchema::SCHEMA_INT);
 
+	cArgContainer* args = cArgContainer::Load(argstr, schema, errors);
+	if (args) 
+	{
+		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);
+	}
+}
+
+double cTaskLib::Task_Optimize(cTaskContext& ctx) const
+{
+	// if the org hasn't output yet enough numbers, just return without completing any tasks
+	if (ctx.GetOutputBuffer().GetNumStored() < ctx.GetOutputBuffer().GetCapacity()) 
+		return 0;
+
+	double quality = 0.0;
+
+	// 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;
+
+	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;
+
+	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 < .001)
+		return .001;
+	else
+		return quality;
+	
+	return 0;
+}
+
 void cTaskLib::Load_Mult(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors)
 {
   cArgSchema schema;

Modified: development/source/main/cTaskLib.h
===================================================================
--- development/source/main/cTaskLib.h	2007-03-21 18:19:27 UTC (rev 1418)
+++ development/source/main/cTaskLib.h	2007-03-21 18:25:49 UTC (rev 1419)
@@ -246,6 +246,10 @@
   void Load_FibonacciSequence(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
   double Task_FibonacciSequence(cTaskContext& ctx) const;
   
+   // Optimization Tasks
+  void Load_Optimize(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
+  double Task_Optimize(cTaskContext& ctx) const;
+
   void Load_Mult(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);
   double Task_Mult(cTaskContext& ctx) const;
   void Load_Div(const cString& name, const cString& argstr, cEnvReqs& envreqs, tList<cString>* errors);




More information about the Avida-cvs mailing list