[Avida-SVN] r2210 - in development/source: actions analyze

goingssh at myxo.css.msu.edu goingssh at myxo.css.msu.edu
Sat Nov 24 13:05:06 PST 2007


Author: goingssh
Date: 2007-11-24 16:05:06 -0500 (Sat, 24 Nov 2007)
New Revision: 2210

Modified:
   development/source/actions/EnvironmentActions.cc
   development/source/actions/PopulationActions.cc
   development/source/analyze/cAnalyze.cc
Log:
Added action to dynamically set min/max for optimization problems



Modified: development/source/actions/EnvironmentActions.cc
===================================================================
--- development/source/actions/EnvironmentActions.cc	2007-11-24 16:44:30 UTC (rev 2209)
+++ development/source/actions/EnvironmentActions.cc	2007-11-24 21:05:06 UTC (rev 2210)
@@ -32,6 +32,8 @@
 #include "cResource.h"
 #include "cStats.h"
 #include "cWorld.h"
+#include "cPhenotype.h"
+#include "cPopulationCell.h"
 
 class cActionInjectResource : public cAction
 {
@@ -465,7 +467,7 @@
   void Process(cAvidaContext& ctx)
   {
     int time = m_world->GetStats().GetUpdate();
-    m_res_count = (amplitude * sin(M_PI/frequency * time - phaseShift * M_PI) + initY) / 2;
+    m_res_count = (amplitude * sin(3.14159/frequency * time - phaseShift * 3.14159) + initY) / 2;
     cResource* res = m_world->GetEnvironment().GetResourceLib().GetResource(m_res_name);
     if (res != NULL) m_world->GetPopulation().SetResource(res->GetID(), m_res_count);
 
@@ -595,6 +597,37 @@
   }
 };
 
+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)
+  {
+	cEnvironment& env = m_world->GetEnvironment();
+    for (int j=0; j<env.GetNumTasks(); j++)
+	{
+		double maxFx=0, minFx=10000;
+		for (int i = 0; i < m_world->GetPopulation().GetSize(); i++) 
+		{
+			 cPopulationCell& cell = m_world->GetPopulation().GetCell(i);
+			if (cell.IsOccupied() == false) continue;
+			const cPhenotype& phen = cell.GetOrganism()->GetPhenotype();
+			double val = phen.GetLastTaskValue()[j];
+			if (val < minFx)
+				minFx = val;
+			if (val > maxFx)
+				maxFx = val;
+		}
+		env.GetTask(j).GetArguments().SetDouble(1,maxFx);
+		env.GetTask(j).GetArguments().SetDouble(2,minFx);
+	}
+  }
+};
+
 class cActionDelayedDemeEvent : public cAction
 {
 private:
@@ -652,6 +685,7 @@
   action_lib->Register<cActionSetTaskArgInt>("SetTaskArgInt");
   action_lib->Register<cActionSetTaskArgDouble>("SetTaskArgDouble");
   action_lib->Register<cActionSetTaskArgString>("SetTaskArgString");
+  action_lib->Register<cActionSetOptimizeMinMax>("SetOptimizeMinMax");
 
   // @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.
   action_lib->Register<cActionInjectResource>("inject_resource");

Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc	2007-11-24 16:44:30 UTC (rev 2209)
+++ development/source/actions/PopulationActions.cc	2007-11-24 21:05:06 UTC (rev 2210)
@@ -1533,23 +1533,6 @@
   }
 };
 
-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())->GetPhenotype();
-    }
-  }
-};
-
-
 class cActionSwapCells : public cAction
 {
 private:
@@ -1624,7 +1607,6 @@
 
   action_lib->Register<cActionConnectCells>("ConnectCells");
   action_lib->Register<cActionDisconnectCells>("DisconnectCells");
-  action_lib->Register<cActionSetOptimizeMinMax>("SetOptimizeMinMax");
   action_lib->Register<cActionSwapCells>("SwapCells");
 
   // @DMB - The following actions are DEPRECATED aliases - These will be removed in 2.7.

Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc	2007-11-24 16:44:30 UTC (rev 2209)
+++ development/source/analyze/cAnalyze.cc	2007-11-24 21:05:06 UTC (rev 2210)
@@ -3680,6 +3680,7 @@
 	at given update (10000 by default) SLG*/
 void cAnalyze::CommandPrintResourceFitnessMap(cString cur_string)
 {
+  cout << "creating resource fitness map...\n";
   // 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();
@@ -3690,7 +3691,7 @@
 
   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++)
@@ -3699,7 +3700,6 @@
 	  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;
@@ -3709,7 +3709,7 @@
 	  
 	     if (fun==f1 && threshMax>0)
 			 rangecount[0]++;
-		 else if (fun==f1 && thresh>0)
+		 else if (fun==f1 && thresh>=0)
 			 threshcount[0]++;
 		 else if (fun!=f1 && threshcount[1]==0 && rangecount[1]==0)
 		 {
@@ -3719,7 +3719,7 @@
 		 }
 		 if (fun==f2 && threshMax>0)
 			 rangecount[1]++;
-		 else if (fun==f2 && thresh>0)
+		 else if (fun==f2 && thresh>=0)
 			 threshcount[1]++;
 	  
   }
@@ -3731,17 +3731,18 @@
   if (threshcount[1]>fsize[1])
 	  fsize[1]=threshcount[1];
 
+  cout << "f1 size: " << fsize[0] << "  f2 size: " << fsize[1] << endl;
   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
+  
+   // 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++)
   {
@@ -3753,10 +3754,11 @@
   }
   if (index<0) cout << "error, never found desired update in resource array!\n";
 
-  //go through each resource in environment
+  else cout << "creating map using resources at update: " << update << endl;
+   
   for (int i=0; i<m_world->GetEnvironment().GetResourceLib().GetSize(); i++)
   {
-	  // first have to find reaction that matches this resource, so compare names
+  	  // 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++)
@@ -3768,7 +3770,8 @@
 			  j = m_world->GetEnvironment().GetReactionLib().GetSize();
 		  }
 	  }
-	  
+	  if (react==NULL)
+	    continue;
 	  // 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();
@@ -3783,7 +3786,7 @@
 	  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);
+	  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);
@@ -3849,7 +3852,6 @@
 				  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;
 				    }
 				}
 				 
@@ -3868,11 +3870,14 @@
 		  }
 	  }
 	  
+	  }
+   
+  for (int i=fitnesses[0].GetSize()-1; i>=0; i--)
+  {
+    for (int j=0; j<fitnesses.GetSize(); j++)
+	fp << fitnesses[j][i] << " ";
+    fp << endl;
   }
-  for (int i=0; i<fitnesses.GetSize(); i++)
-    for (int j=0; j<fitnesses[0].GetSize(); j++)
-      cout << i << j << " " << fitnesses[i][j] << endl;
-
 }
 
 




More information about the Avida-cvs mailing list