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

barrick at myxo.css.msu.edu barrick at myxo.css.msu.edu
Thu Dec 7 13:32:51 PST 2006


Author: barrick
Date: 2006-12-07 16:32:51 -0500 (Thu, 07 Dec 2006)
New Revision: 1118

Modified:
   development/source/actions/EnvironmentActions.cc
   development/source/analyze/cAnalyze.cc
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareManager.cc
   development/source/cpu/cHardwareManager.h
   development/source/cpu/cTestCPU.cc
   development/source/cpu/cTestCPU.h
   development/source/cpu/cTestCPUInterface.cc
   development/source/main/cPopulation.h
   development/source/main/cResource.h
   development/source/main/cResourceCount.cc
   development/source/main/cResourceCount.h
   development/source/main/cSpatialCountElem.h
   development/source/main/cSpatialResCount.cc
   development/source/main/cSpatialResCount.h
Log:
New action: "SetCellResource"
Allows one to set how much of a specific resource is present in a given cell.

Beginning to refactor the way that the test CPU so that it can also handle more resource options. Specifically, allow resources to be set from a specific cell and be dynamic (inflow/outflow/depletion).



Modified: development/source/actions/EnvironmentActions.cc
===================================================================
--- development/source/actions/EnvironmentActions.cc	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/actions/EnvironmentActions.cc	2006-12-07 21:32:51 UTC (rev 1118)
@@ -133,7 +133,45 @@
   }
 };
 
+class cActionSetCellResource : public cAction
+{
+private:
+  tArray<int> m_cell_list;
+  cString m_res_name;
+  double m_res_count;
+  
+public:
+  cActionSetCellResource(cWorld* world, const cString& args) : cAction(world, args), m_cell_list(0), m_res_name(""), m_res_count(0.0)
+  {
+    cString largs(args);
+    if (largs.GetSize()) 
+    {
+      cString s = largs.PopWord();
+      m_cell_list = cStringUtil::ReturnArray(s);    
+    }
+    if (largs.GetSize()) m_res_name = largs.PopWord();
+    if (largs.GetSize()) m_res_count = largs.PopWord().AsDouble();
+  }
 
+  static const cString GetDescription() { return "Arguments: <int cell_id> <string res_name> <double res_count>"; }
+
+  void Process(cAvidaContext& ctx)
+  {
+    cResource* res = m_world->GetEnvironment().GetResourceLib().GetResource(m_res_name);
+    for(int i=0; i<m_cell_list.GetSize(); i++)
+    {
+      int m_cell_id = m_cell_list[i];
+      tArray<double> counts = m_world->GetPopulation().GetResourceCount().GetCellResources(m_cell_id);
+      if ((res != NULL) && (res->GetID() < counts.GetSize()))
+      {
+        counts[res->GetID()] = m_res_count;
+        m_world->GetPopulation().GetResourceCount().SetCellResources(m_cell_id, counts);
+      }
+    }
+  }
+};
+
+
 // Set the values associated with a specified reaction.  If the name of the
 // reaction used is "ALL" then all reactions will be changed.  If the name is
 // "RANDOM:3" then random reactions will be set to the specified value.  The
@@ -215,6 +253,7 @@
   action_lib->Register<cActionInjectScaledResource>("InjectScaledResource");
   action_lib->Register<cActionOutflowScaledResource>("OutflowScaledResource");
   action_lib->Register<cActionSetResource>("SetResource");
+  action_lib->Register<cActionSetCellResource>("SetCellResource");
 
   action_lib->Register<cActionSetReactionValue>("SetReactionValue");
   action_lib->Register<cActionSetReactionValueMult>("SetReactionValueMult");

Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/analyze/cAnalyze.cc	2006-12-07 21:32:51 UTC (rev 1118)
@@ -487,7 +487,7 @@
     temp[i] = resources[which].second[i];
   }
 
-  testcpu->SetupResourceArray(temp);
+  testcpu->SetResourcesFromArray(temp);
 }
 
 double cAnalyze::AnalyzeEntropy(cAnalyzeGenotype * genotype, double mu) 
@@ -1665,7 +1665,6 @@
   }
   
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();  
-  testcpu->SetUseResources(useResources);
   
   if (useResources && update > -1) {
     FillResources(testcpu, update);
@@ -2678,7 +2677,6 @@
   ///////////////////////
   // Create Test CPU
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
-  testcpu->SetUseResources(true);
   FillResources(testcpu, update);
   
   ///////////////////////////////////////////////////////////////////////
@@ -3240,7 +3238,6 @@
   ///////////////////////
   // Backup test CPU data
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
-  testcpu->SetUseResources(true);
   FillResources(testcpu, update);
   
   
@@ -6638,7 +6635,6 @@
   }
   
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
-  testcpu->SetUseResources(useResources);
   
   ///////////////////////////////////////////////////////
   // Loop through all of the genotypes in this batch...
@@ -7044,7 +7040,6 @@
 
   
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
-  testcpu->SetUseResources(useResources);
   
   cCPUTestInfo *test_info = new cCPUTestInfo();
   test_info->UseRandomInputs(useRandomInputs==1); 

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/cpu/cHardwareCPU.cc	2006-12-07 21:32:51 UTC (rev 1118)
@@ -3273,6 +3273,9 @@
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   GetHead(head_used).Jump(GetRegister(REG_CX) );
+  // JEB - probably shouldn't advance inst ptr after jumping here?
+  // Any negative number jumps to the beginning of the genome (pos 0)
+  // and then we immediately advance past that first instruction.
   return true;
 }
 

Modified: development/source/cpu/cHardwareManager.cc
===================================================================
--- development/source/cpu/cHardwareManager.cc	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/cpu/cHardwareManager.cc	2006-12-07 21:32:51 UTC (rev 1118)
@@ -20,7 +20,7 @@
 #include "tDictionary.h"
 
 cHardwareManager::cHardwareManager(cWorld* world)
-: m_world(world), m_inst_set(world), m_type(world->GetConfig().HARDWARE_TYPE.Get()), m_testres(world)
+: m_world(world), m_inst_set(world), m_type(world->GetConfig().HARDWARE_TYPE.Get()) /*, m_testres(world) */
 {
   LoadInstSet(world->GetConfig().INST_SET.Get());
 }

Modified: development/source/cpu/cHardwareManager.h
===================================================================
--- development/source/cpu/cHardwareManager.h	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/cpu/cHardwareManager.h	2006-12-07 21:32:51 UTC (rev 1118)
@@ -27,7 +27,7 @@
   cWorld* m_world;
   cInstSet m_inst_set;
   int m_type;
-  cTestResources m_testres;
+//  cTestResources m_testres;
   
   cHardwareManager(); // @not_implemented
   cHardwareManager(const cHardwareManager&); // @not_implemented
@@ -38,7 +38,7 @@
   ~cHardwareManager() { ; }
   
   cHardwareBase* Create(cOrganism* in_org);
-  cTestCPU* CreateTestCPU() { return new cTestCPU(m_world, &m_testres); }
+  cTestCPU* CreateTestCPU() { return new cTestCPU(m_world /*, &m_testres*/); }
 
   void LoadInstSet(cString filename);
   

Modified: development/source/cpu/cTestCPU.cc
===================================================================
--- development/source/cpu/cTestCPU.cc	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/cpu/cTestCPU.cc	2006-12-07 21:32:51 UTC (rev 1118)
@@ -34,17 +34,24 @@
 
 using namespace std;
 
-cTestResources::cTestResources(cWorld* world)
+cTestCPU::cTestCPU(cWorld* world)
 {
-  // Setup the resources...
-  const cResourceLib& resource_lib = world->GetEnvironment().GetResourceLib();
+  m_world = world;
+  InitResources();
+}  
+ 
+void cTestCPU::InitResources()
+{  
+   // Setup the resources...
+  // unless another 'set' method is called, they default to zero
+  m_res_method = RES_STATIC;
+
+  const cResourceLib& resource_lib = m_world->GetEnvironment().GetResourceLib();
   assert(resource_lib.GetSize() >= 0);
 
   resource_count.SetSize(resource_lib.GetSize());
-  d_emptyDoubleArray.ResizeClear(resource_lib.GetSize());
   d_resources.ResizeClear(resource_lib.GetSize());
   for(int i=0; i<resource_lib.GetSize(); i++) {
-    d_emptyDoubleArray[i] = 0.0;
     d_resources[i] = 0.0;
   }
 
@@ -62,33 +69,63 @@
                            res->GetInflowY2(), res->GetOutflowX1(), 
                            res->GetOutflowX2(), res->GetOutflowY1(), 
                            res->GetOutflowY2(), res->GetCellListPtr(),
-                           world->GetVerbosity() );
+                           m_world->GetVerbosity() );
   }
+
 }
 
-
-void cTestCPU::SetupResourceArray(const tArray<double> &resources)
+void cTestCPU::SetResourcesFromArray(const tArray<double> &resources)
 {
-  if (!m_localres) m_res = new cTestResources(*m_res);
-
-  for(int i = 0; i < m_res->d_resources.GetSize(); i++) {
+  // Set STATIC resources according to the input array
+  m_res_method = RES_STATIC;
+  
+  for(int i = 0; i < d_resources.GetSize(); i++) {
     if(i >= resources.GetSize()) {
-      m_res->d_resources[i] = 0.0;
+      d_resources[i] = 0.0;
     } else {
-      m_res->d_resources[i] = resources[i];
+      d_resources[i] = resources[i];
     }
   }
 }
 
-void cTestCPU::SetUseResources(bool use)
+
+void cTestCPU::SetResourcesFromCell(int cell_x, int cell_y)
 {
-  if (m_res->d_useResources != use) {
-    if (!m_localres) m_res = new cTestResources(*m_res);
-    m_res->d_useResources = use;
+  // Set DYNAMIC resources according to the input array
+  m_res_method = RES_DYNAMIC;
+
+  // Setup the resources...
+  const cResourceLib& resource_lib = m_world->GetEnvironment().GetResourceLib();
+  assert(resource_lib.GetSize() >= 0);
+
+  // Make a parallel world that consists of one cell
+  for (int i = 0; i < resource_lib.GetSize(); i++) {
+    cResource* res = resource_lib.GetResource(i);
+    const double decay = 1.0 - res->GetOutflow();
+    
+    // Set up the resource. 
+    // (1) Diffusion and gravity don't make sense in a one-cell world.
+    // (2) We offset all of the X, Y coords so this cell is at 0,0 (the only valid cell in the world)
+    resource_count.Setup(i, res->GetName(), res->GetInitial(), 
+                           res->GetInflow(), decay,
+                           res->GetGeometry(), 0.0 /*res->GetXDiffuse()*/,
+                           0.0 /*res->GetXGravity()*/, 0.0 /*res->GetYDiffuse()*/, 
+                           0.0 /*res->GetYGravity()*/, res->GetInflowX1() - cell_x, 
+                           res->GetInflowX2() - cell_x, res->GetInflowY1() - cell_y, 
+                           res->GetInflowY2() - cell_y, res->GetOutflowX1() - cell_x, 
+                           res->GetOutflowX2() - cell_x, res->GetOutflowY1() - cell_y, 
+                           res->GetOutflowY2() - cell_y, res->GetCellListPtr(),
+                           m_world->GetVerbosity() );
   }
 }
 
+void cTestCPU::ModifyResources(const tArray<double>& res_change)
+{
+  //We only let the testCPU modify the resources if we are using DYNAMIC ones.
+  if (m_res_method == RES_DYNAMIC) resource_count.Modify(res_change);
+}
 
+
 // NOTE: This method assumes that the organism is a fresh creation.
 bool cTestCPU::ProcessGestation(cAvidaContext& ctx, cCPUTestInfo& test_info, int cur_depth)
 {

Modified: development/source/cpu/cTestCPU.h
===================================================================
--- development/source/cpu/cTestCPU.h	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/cpu/cTestCPU.h	2006-12-07 21:32:51 UTC (rev 1118)
@@ -31,31 +31,6 @@
 class cGenotype;
 class cWorld;
 
-class cTestResources
-{
-  friend class cTestCPU;
-private:
-  cResourceCount resource_count;
-  bool d_useResources;
-  tArray<double> d_emptyDoubleArray;
-  tArray<double> d_resources;
-  
-public:
-  cTestResources(cWorld* world);
-};
-
-
-#ifdef ENABLE_UNIT_TESTS
-namespace nTestResources {
-  /**
-   * Run unit tests
-   *
-   * @param full Run full test suite; if false, just the fast tests.
-   **/
-  void UnitTests(bool full = false);
-}
-#endif
-
 class cTestCPU
 {
 private:
@@ -63,11 +38,13 @@
   tArray<int> input_array;
   tArray<int> receive_array;
   int cur_input;
-  int cur_receive;
-  
-  cTestResources* m_res;
-  bool m_localres;
+  int cur_receive;  
 
+  enum tTestCPUResourceMethod { RES_STATIC = 0, RES_DYNAMIC };  
+  tTestCPUResourceMethod m_res_method;
+  cResourceCount resource_count;
+  tArray<double> d_resources;
+
   bool ProcessGestation(cAvidaContext& ctx, cCPUTestInfo& test_info, int cur_depth);
   bool TestGenome_Body(cAvidaContext& ctx, cCPUTestInfo& test_info, const cGenome& genome, int cur_depth);
 
@@ -76,9 +53,8 @@
   cTestCPU& operator=(const cTestCPU&); // @not_implemented
   
 public:
-  cTestCPU(cWorld* world) : m_world(world), m_res(new cTestResources(world)), m_localres(true) { ; }
-  cTestCPU(cWorld* world, cTestResources* res) : m_world(world), m_res(res), m_localres(false) { ; }
-  ~cTestCPU() { if (m_localres) delete m_res; }
+  cTestCPU(cWorld* world);
+  ~cTestCPU() { }
   
   bool TestGenome(cAvidaContext& ctx, cCPUTestInfo& test_info, const cGenome& genome);
   bool TestGenome(cAvidaContext& ctx, cCPUTestInfo& test_info, const cGenome& genome, std::ofstream& out_fp);
@@ -89,12 +65,13 @@
   inline int GetInput();
   inline int GetInputAt(int & input_pointer);
   inline int GetReceiveValue();
-  inline const tArray<double>& GetResources();
+  inline const tArray<double>& GetResources();  
   inline void SetResource(int id, double new_level);
-  void SetupResourceArray(const tArray<double> &resources);
-  void SetUseResources(bool use);
-  bool GetUseResources() { return m_res->d_useResources; }
-  cResourceCount& GetResourceCount(void) { return m_res->resource_count; }
+  void InitResources();
+  void SetResourcesFromArray(const tArray<double> &resources);
+  void SetResourcesFromCell(int cell_x, int cell_y);
+  void ModifyResources(const tArray<double>& res_change);
+  cResourceCount& GetResourceCount() { return resource_count; }
 };
 
 #ifdef ENABLE_UNIT_TESTS
@@ -132,15 +109,14 @@
 
 inline const tArray<double>& cTestCPU::GetResources()
 {
-  if(m_res->d_useResources) return m_res->d_resources;
+  if(m_res_method == RES_STATIC) return d_resources;
   
-  return m_res->d_emptyDoubleArray;
+  return resource_count.GetResources();
 }
 
 inline void cTestCPU::SetResource(int id, double new_level)
 {
-  if (!m_localres) m_res = new cTestResources(*m_res);  // copy resources locally
-  m_res->resource_count.Set(id, new_level);
+  resource_count.Set(id, new_level);
 }
 
 #endif

Modified: development/source/cpu/cTestCPUInterface.cc
===================================================================
--- development/source/cpu/cTestCPUInterface.cc	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/cpu/cTestCPUInterface.cc	2006-12-07 21:32:51 UTC (rev 1118)
@@ -58,6 +58,7 @@
 
 void cTestCPUInterface::UpdateResources(const tArray<double>& res_change)
 {
+   m_testcpu->ModifyResources(res_change);
 }
 
 void cTestCPUInterface::Die()

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/main/cPopulation.h	2006-12-07 21:32:51 UTC (rev 1118)
@@ -178,6 +178,7 @@
   void UpdateCellResources(const tArray<double>& res_change, const int cell_id);
   void SetResource(int id, double new_level);
   double GetResource(int id) const { return resource_count.Get(id); }
+  cResourceCount& GetResourceCount() { return resource_count; }
 
   cEnvironment& GetEnvironment() { return environment; }
   int GetNumOrganisms() { return num_organisms; }

Modified: development/source/main/cResource.h
===================================================================
--- development/source/main/cResource.h	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/main/cResource.h	2006-12-07 21:32:51 UTC (rev 1118)
@@ -38,7 +38,7 @@
   double GetOutflow() const { return outflow; }
 };
 
-/* class to hold all infromation for a single resource */
+/* class to hold all information for a single resource */
 
 class cResource
 {

Modified: development/source/main/cResourceCount.cc
===================================================================
--- development/source/main/cResourceCount.cc	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/main/cResourceCount.cc	2006-12-07 21:32:51 UTC (rev 1118)
@@ -143,6 +143,25 @@
 {
 }
 
+void cResourceCount::SetCellResources(int cell_id, const tArray<double> & res)
+{
+  assert(resource_count.GetSize() == res.GetSize());
+
+  for (int i = 0; i < resource_count.GetSize(); i++) {
+    if (geometry[i] == nGeometry::GLOBAL) {
+      // Set global quantity of resource
+    } else {
+      spatial_resource_count[i].SetCellAmount(cell_id, res[i]);
+
+      /* Ideally the state of the cell's resource should not be set till
+         the end of the update so that all processes (inflow, outflow, 
+         diffision, gravity and organism demand) have the same weight.  However
+         waiting can cause problems with negative resources so we allow
+         the organism demand to work immediately on the state of the resource */ 
+    }
+  }
+}
+
 void cResourceCount::Setup(int id, cString name, double initial, double inflow,
                            double decay, int in_geometry, double in_xdiffuse,
                            double in_xgravity, double in_ydiffuse, 

Modified: development/source/main/cResourceCount.h
===================================================================
--- development/source/main/cResourceCount.h	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/main/cResourceCount.h	2006-12-07 21:32:51 UTC (rev 1118)
@@ -58,6 +58,7 @@
   const cResourceCount& operator=(const cResourceCount&);
 
   void SetSize(int num_resources);
+  void SetCellResources(int cell_id, const tArray<double> & res);
 
   void Setup(int id, cString name, double initial, double inflow, double decay,
              int in_geometry, double in_xdiffuse, double in_xgravity, 

Modified: development/source/main/cSpatialCountElem.h
===================================================================
--- development/source/main/cSpatialCountElem.h	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/main/cSpatialCountElem.h	2006-12-07 21:32:51 UTC (rev 1118)
@@ -29,7 +29,8 @@
   void Rate(double ratein) const { delta += ratein; }
   void State() { amount += delta; delta = 0.0; }
   const double GetAmount() const { return amount; }
-  void SetPtr(int innum, int inelempt, int inxdist, int  inydist, double indist);
+  const double SetAmount(double res) const { amount = res; }
+    void SetPtr(int innum, int inelempt, int inxdist, int  inydist, double indist);
   int GetElemPtr(int innum) { return elempt[innum]; }
   int GetPtrXdist(int innum) { return xdist[innum]; }
   int GetPtrYdist(int innum) { return ydist[innum]; }

Modified: development/source/main/cSpatialResCount.cc
===================================================================
--- development/source/main/cSpatialResCount.cc	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/main/cSpatialResCount.cc	2006-12-07 21:32:51 UTC (rev 1118)
@@ -394,3 +394,11 @@
     Rate((*cell_list_ptr)[i].GetId(), -deltaamount); 
   }
 }
+
+void cSpatialResCount::SetCellAmount(int cell_id, double res)
+{
+  if (cell_id >= 0 && cell_id < grid.GetSize())
+  {
+    Element(cell_id).SetAmount(res);
+  }
+}

Modified: development/source/main/cSpatialResCount.h
===================================================================
--- development/source/main/cSpatialResCount.h	2006-12-07 20:59:48 UTC (rev 1117)
+++ development/source/main/cSpatialResCount.h	2006-12-07 21:32:51 UTC (rev 1118)
@@ -49,7 +49,7 @@
   int GetX() { return world_x; }
   int GetY() { return world_y; }
   int GetCellListSize() { return cell_list_ptr->GetSize(); }
-  cSpatialCountElem Element(int x) { return grid[x]; }
+  cSpatialCountElem& Element(int x) { return grid[x]; }
   void Rate(int x, double ratein) const;
   void Rate(int x, int y, double ratein) const;
   void State(int x);
@@ -64,6 +64,7 @@
   void CellInflow() const;
   void Sink(double percent) const;
   void CellOutflow() const;
+  void SetCellAmount(int cell_id, double res);
   void SetGeometry(int in_geometry) { geometry = in_geometry; }
   void SetXdiffuse(double in_xdiffuse) { xdiffuse = in_xdiffuse; }
   void SetXgravity(double in_xgravity) { xgravity = in_xgravity; }




More information about the Avida-cvs mailing list