[Avida-SVN] r2208 - in development/source: analyze main

goingssh at myxo.css.msu.edu goingssh at myxo.css.msu.edu
Sun Nov 18 19:33:12 PST 2007


Author: goingssh
Date: 2007-11-18 22:33:12 -0500 (Sun, 18 Nov 2007)
New Revision: 2208

Modified:
   development/source/analyze/cAnalyze.cc
   development/source/analyze/cAnalyze.h
   development/source/main/cTaskLib.cc
Log:
Added analyze command to print what the fitness of each of a spread of possible solutions would be given the resource concentrations at a given update


Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc	2007-11-17 15:01:49 UTC (rev 2207)
+++ development/source/analyze/cAnalyze.cc	2007-11-19 03:33:12 UTC (rev 2208)
@@ -58,6 +58,8 @@
 #include "cPhenPlastGenotype.h"
 #include "cPlasticPhenotype.h"
 #include "cProbSchedule.h"
+#include "cReaction.h"
+#include "cReactionProcess.h"
 #include "cSchedule.h"
 #include "cSpecies.h"
 #include "cStringIterator.h"
@@ -440,9 +442,9 @@
   assert(resourceFile.good());
   
   // Read in each line of the resource file and process it
-  char line[2048];
+  char line[4096];
   while(!resourceFile.eof()) {
-    resourceFile.getline(line, 2047);
+    resourceFile.getline(line, 4095);
     
     // Get rid of the whitespace at the beginning of the stream, then 
     // see if the line begins with a #, if so move on to the next line.
@@ -3674,20 +3676,206 @@
   return;
 }
 
+/* prints grid with what the fitness of an org in each range box would be given the resource levels
+	at given update (10000 by default) SLG*/
+void cAnalyze::CommandPrintResourceFitnessMap(cString cur_string)
+{
+  // at what update do we want to use the resource concentrations from?
+  int update = 10000;
+  if (cur_string.GetSize() != 0) update = cur_string.PopWord().AsInt();
+  // what file to write data to
+  cString filename("resourcefitmap.dat");
+  if (cur_string.GetSize() != 0) filename = cur_string.PopWord();
+  ofstream& fp = m_world->GetDataFileOFStream(filename);
 
+  int f1=-1, f2=-1, rangecount[2]={0,0}, threshcount[2]={0,0};
+  double f1Max, f1Min, f2Max, f2Min;
 
+  // first need to find out how many thresh and range resources there are on each function
+  // NOTE! this only works for 2-obj. problems right now!
+  for (int i=0; i<m_world->GetEnvironment().GetReactionLib().GetSize(); i++)
+  {
+	  cReaction* react = m_world->GetEnvironment().GetReactionLib().GetReaction(i);
+	  int fun = react->GetTask()->GetArguments().GetInt(0);
+	  double thresh = react->GetTask()->GetArguments().GetDouble(3);
+	  double threshMax = react->GetTask()->GetArguments().GetDouble(4);
 
+	  if (i==0)
+	  {
+		  f1 = fun;
+		  f1Max = react->GetTask()->GetArguments().GetDouble(1);
+		  f1Min = react->GetTask()->GetArguments().GetDouble(2);
+	  }
+	  
+	     if (fun==f1 && threshMax>0)
+			 rangecount[0]++;
+		 else if (fun==f1 && thresh>0)
+			 threshcount[0]++;
+		 else if (fun!=f1 && threshcount[1]==0 && rangecount[1]==0)
+		 {
+			 f2=fun;
+			 f2Max = react->GetTask()->GetArguments().GetDouble(1);
+			 f2Min = react->GetTask()->GetArguments().GetDouble(2);
+		 }
+		 if (fun==f2 && threshMax>0)
+			 rangecount[1]++;
+		 else if (fun==f2 && thresh>0)
+			 threshcount[1]++;
+	  
+  }
+  int fsize[2];
+  fsize[0] = rangecount[0];
+  if (threshcount[0]>fsize[0])
+	  fsize[0]=threshcount[0];
+  fsize[1]=rangecount[1];
+  if (threshcount[1]>fsize[1])
+	  fsize[1]=threshcount[1];
 
+  double stepsize[2];
+  stepsize[0] = (f1Max-f1Min)/fsize[0];
+  stepsize[1] = (f2Max-f2Min)/fsize[1];
 
+  // this is our grid where we are going to calculate the fitness of an org in each box
+  // given current resource contributions
+  tArray< tArray<double> > fitnesses(fsize[0]+1);
+  for (int i=0; i<fitnesses.GetSize(); i++)
+	  fitnesses[i].Resize(fsize[1]+1,1);
+ 
+  // figure out what index into resources that we loaded goes with update we want
+  int index=-1;
+  for (int i=0; i<resources.size(); i++)
+  {
+	  if (resources.at(i).first == update)
+	  {
+		  index=i;
+		  i=resources.size();
+	  }
+  }
+  if (index<0) cout << "error, never found desired update in resource array!\n";
 
+  //go through each resource in environment
+  for (int i=0; i<m_world->GetEnvironment().GetResourceLib().GetSize(); i++)
+  {
+	  // first have to find reaction that matches this resource, so compare names
+	  cString name = m_world->GetEnvironment().GetResourceLib().GetResource(i)->GetName();
+	  cReaction* react;
+	  for (int j=0; j<m_world->GetEnvironment().GetReactionLib().GetSize(); j++)
+	  {
+		  if (m_world->GetEnvironment().GetReactionLib().GetReaction(j)->GetProcesses().GetPos(0)->GetResource()->GetName()
+			  == name)
+		  {
+			  react = m_world->GetEnvironment().GetReactionLib().GetReaction(j);
+			  j = m_world->GetEnvironment().GetReactionLib().GetSize();
+		  }
+	  }
+	  
+	  // now have proper reaction, pull all the data need from the reaction
+	  double frac = react->GetProcesses().GetPos(0)->GetMaxFraction(); 
+	  double max = react->GetProcesses().GetPos(0)->GetMaxNumber();
+	  double min = react->GetProcesses().GetPos(0)->GetMinNumber();
+	  double value = react->GetValue();
+	  int fun = react->GetTask()->GetArguments().GetInt(0);
+	  if (fun==f1)
+		  fun=0;
+	  else if (fun==f2)
+		  fun=1;
+	  else cout << "function is neither f1 or f2! doh!\n";
+	  double thresh = react->GetTask()->GetArguments().GetDouble(3);
+	  double threshMax = react->GetTask()->GetArguments().GetDouble(4);
+	  double maxFx = react->GetTask()->GetArguments().GetDouble(1);
+      double minFx = react->GetTask()->GetArguments().GetDouble(2);
 
+	  // and pull the concentration of this resource from resource object loaded from resource.dat
+	  double concentration = resources.at(index).second.at(i);
 
+	  // calculate the merit based on this resource concentration, fraction, and value
+	  double mer = concentration * frac * value;
+	  if (mer > max)
+		  mer=max;
+	  else if (mer < min)
+		  mer=0;
+	  double threshMaxAdjusted, threshAdjusted;
+	  // if this is a range reaction, need to update one entire row or column in fitnesses array
+	  if (threshMax>0)
+	  {			
+		  for (int k=0; k<fsize[fun]; k++)
+		  {
+			  // function f1
+			  if (fun==0)
+			  {
+				  threshMaxAdjusted = threshMax*(f1Max-f1Min) + f1Min;
+				  threshAdjusted = thresh*(f1Max-f1Min) + f1Min;
+				  double pos = stepsize[0]*k+f1Min+stepsize[0]/2.0;
+				  if (threshAdjusted <= pos && threshMaxAdjusted >= pos)
+				  {
+					  for (int z=0; z<fsize[1]+1; z++)
+						  fitnesses[k+1][z] *= pow(2,mer);
+				  // actually solutions right at min possible get range above them too
+					  if (k==0)
+						  for (int z=0; z<fsize[1]+1; z++)
+							  fitnesses[0][z] *= pow(2,mer);
+				  }
+			  }
+			  // function f2
+			  else 
+			  {
+				  threshMaxAdjusted = threshMax*(f2Max-f2Min) + f2Min;
+				  threshAdjusted = thresh*(f2Max-f2Min) + f2Min;
+				  double pos = stepsize[1]*k+f1Min+stepsize[1]/2.0;
+				  if (threshAdjusted <= pos && threshMaxAdjusted >= pos)
+				  {
+					  for (int z=0; z<fsize[0]+1; z++)
+						  fitnesses[z][k+1] *= pow(2,mer);
+				  // actually solutions right at min possible get range above them too
+					  if (k==0)
+						  for (int z=0; z<fsize[0]+1; z++)
+							  fitnesses[z][0] *= pow(2,mer);
+				  }
+			  }
+		  }
+	  }
+	  // threshold reaction, need to update all rows or columns above given threshold
+	  else if (thresh>=0)
+	  {
+		  for (int k=0; k<fsize[fun]+1; k++)
+		  {
+			  // function f1
+			  if (fun==0)
+			  {
+			      threshAdjusted = thresh*(f1Max-f1Min) + f1Min;
+			      double pos = stepsize[0]*k+f1Min-stepsize[0]/2.0;
+			      if (threshAdjusted >= pos)
+				{
+				  for (int z=0; z<fsize[1]+1; z++)
+				    {
+				      fitnesses[k][z] *= pow(2,mer);
+				      cout << "k: " << k << "z: " << z << "bonus: " << pow(2,mer) << endl;
+				    }
+				}
+				 
+			  }
+			  // function f2
+			  else 
+			  {				  
+			    threshAdjusted = thresh*(f2Max-f2Min) + f2Min;
+			    double pos = stepsize[1]*k+f1Min-stepsize[1]/2.0;
+			    if (threshAdjusted >= pos)
+			      {
+				for (int z=0; z<fsize[0]+1; z++)
+				  fitnesses[z][k] *= pow(2,mer);
+			      } 
+			  }
+		  }
+	  }
+	  
+  }
+  for (int i=0; i<fitnesses.GetSize(); i++)
+    for (int j=0; j<fitnesses[0].GetSize(); j++)
+      cout << i << j << " " << fitnesses[i][j] << endl;
 
+}
 
 
-
-
-
 //@ MRR @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 void cAnalyze::CommandPairwiseEntropy(cString cur_string)
 {
@@ -8426,6 +8614,7 @@
   AddLibraryDef("PRINT_DIVERSITY", &cAnalyze::CommandPrintDiversity);
   AddLibraryDef("PRINT_TREE_STATS", &cAnalyze::CommandPrintTreeStats);
   AddLibraryDef("COMMUNITY_COMPLEXITY", &cAnalyze::AnalyzeCommunityComplexity);
+  AddLibraryDef("PRINT_RESOURCE_FITNESS_MAP", &cAnalyze::CommandPrintResourceFitnessMap);
   
   // Individual organism analysis...
   AddLibraryDef("FITNESS_MATRIX", &cAnalyze::CommandFitnessMatrix);

Modified: development/source/analyze/cAnalyze.h
===================================================================
--- development/source/analyze/cAnalyze.h	2007-11-17 15:01:49 UTC (rev 2207)
+++ development/source/analyze/cAnalyze.h	2007-11-19 03:33:12 UTC (rev 2208)
@@ -258,6 +258,7 @@
   void CommandPrintTreeStats(cString cur_string);
   void PhyloCommunityComplexity(cString cur_string);
   void AnalyzeCommunityComplexity(cString cur_string);
+  void CommandPrintResourceFitnessMap(cString cur_string);
   
   // Individual Organism Analysis...
   void CommandFitnessMatrix(cString cur_string);

Modified: development/source/main/cTaskLib.cc
===================================================================
--- development/source/main/cTaskLib.cc	2007-11-17 15:01:49 UTC (rev 2207)
+++ development/source/main/cTaskLib.cc	2007-11-19 03:33:12 UTC (rev 2208)
@@ -2179,12 +2179,51 @@
    vars.Resize(args.GetInt(3));
 
    double Fx = 0.0;
-   
-  if (args.GetInt(1)) 
-  {
-    int len = args.GetInt(2);
-    double base_pow = args.GetDouble(0);
 
+   // some of the problems don't need double variables but use the bit string as a bit string
+   if (function==18)
+   {
+     int tot=0;
+     for (int i=0; i<30; i++)
+       tot+= ctx.GetOutputBuffer()[i];
+     Fx = 1+tot;
+   }
+   else if (function==19)
+   {
+     tArray<double> tempVars;
+     tempVars.Resize(args.GetInt(3));
+     for (int i=0; i<args.GetInt(3); i++)
+       tempVars[i]=0;
+     
+     for (int i=0; i<30; i++)
+       tempVars[0]+= ctx.GetOutputBuffer()[i];
+
+     int len = args.GetInt(2);
+     for (int i = len - 1; i >= 0; i--) 
+     {
+	for (int j=1; j<args.GetInt(3); j++)
+	{
+	  tempVars[j-1] += ctx.GetOutputBuffer()[30+i + len*(args.GetInt(3)-j-1)];
+	}
+     }
+
+     int Gx=0;
+     for (int i=1; i<args.GetInt(3); i++)
+     {
+       if (tempVars[i]==5)
+	  Gx += 1;
+       else 
+	 Gx += tempVars[i]+2;
+     }
+     Fx = Gx*(1/(1+tempVars[0]));
+   }
+   else 
+   {
+     if (args.GetInt(1)) 
+     {
+        int len = args.GetInt(2);
+        double base_pow = args.GetDouble(0);
+
 	tArray<double> tempVars;
 	tempVars.Resize(args.GetInt(3));
 	for (int i=0; i<args.GetInt(3); i++)
@@ -2202,22 +2241,22 @@
 	for (int i=0; i<args.GetInt(3); i++)
 		vars[i] = tempVars[i] / tot;
 	//	cout << "x: " << vars[0] << " ";
-  } 
-  else 
-  {
+    } 
+    else 
+    {
 	  for (int j=0; j<args.GetInt(3); j++)
 		  vars[j] = double(ctx.GetOutputBuffer()[j]) / 0xffffffff;
-  }
+    }
   
-  for (int j=0; j<args.GetInt(3); j++)
-  {
+    for (int j=0; j<args.GetInt(3); j++)
+    {
 	  if (vars[j] < 0)
 		  vars[j] = 0;
 	  else if (vars[j] > 1)
 		  vars[j] = 1;
-  }
+    }
 
-  switch(function) {
+    switch(function) {
     case 1:
 	  Fx = vars[0];		// F1
 	  //	  cout << "Fx1: " << Fx << " ";
@@ -2280,7 +2319,7 @@
     {
       double sum = 0;
       for (int i=1; i<args.GetInt(3); i++)
-		  sum += vars[i]/double(args.GetInt(3)-1);
+	  sum += vars[i]/double(args.GetInt(3)-1);
       double Gx = 1+9*sum;
       Fx = Gx * (1 - sqrt(vars[0]/Gx) - (vars[0]/Gx)*(sin(3.14159*vars[0]*10)));
       break;
@@ -2325,10 +2364,20 @@
       break;
     }
 
+    case 17:
+    {
+      double sum = 0;
+      for (int i=1; i<args.GetInt(3); i++)
+	sum += (pow((vars[i]*6-3),2)-10*cos(4*3.14159*(vars[i]*6-3)))/10.0;
+      double Gx = 10+sum;
+      Fx = Gx * (1.0 - sqrt(vars[0]/Gx));
+      break;
+    }
+
     default:
       quality = .001;
-  }
-
+    }
+   }
   ctx.SetTaskValue(Fx);
   if (args.GetDouble(3) < 0.0)
   {
@@ -2337,7 +2386,9 @@
     assert(q1 > 0.0);
     assert(q2 > 0.0);
     quality = q1 / q2;
-  } else {
+  } 
+  else 
+  {
     if (args.GetDouble(4) < 0.0)
     {
       if (Fx <= (args.GetDouble(1) - args.GetDouble(2))*args.GetDouble(3) + args.GetDouble(2))
@@ -2356,7 +2407,7 @@
 			quality = 1.0;
 		else
 			quality = 0.0;
-	}
+    }
   }
 
   // because want org to only have 1 shot to use outputs for all functions at once, even if they




More information about the Avida-cvs mailing list