[Avida-SVN] r1860 - in branches/energy_dev/source: cpu main

beckma24 at myxo.css.msu.edu beckma24 at myxo.css.msu.edu
Fri Jul 27 13:22:35 PDT 2007


Author: beckma24
Date: 2007-07-27 16:22:35 -0400 (Fri, 27 Jul 2007)
New Revision: 1860

Modified:
   branches/energy_dev/source/cpu/cTestCPU.cc
   branches/energy_dev/source/cpu/cTestCPU.h
   branches/energy_dev/source/cpu/cTestCPUInterface.cc
   branches/energy_dev/source/cpu/cTestCPUInterface.h
   branches/energy_dev/source/main/cDeme.h
   branches/energy_dev/source/main/cOrgInterface.h
   branches/energy_dev/source/main/cOrganism.cc
   branches/energy_dev/source/main/cPhenotype.cc
   branches/energy_dev/source/main/cPhenotype.h
   branches/energy_dev/source/main/cPopulation.cc
   branches/energy_dev/source/main/cPopulation.h
   branches/energy_dev/source/main/cPopulationCell.cc
   branches/energy_dev/source/main/cPopulationInterface.cc
   branches/energy_dev/source/main/cPopulationInterface.h
   branches/energy_dev/source/main/cResourceCount.cc
Log:
Checkpoint commit.  Added ablity to use a deme-level resource as part of a reaction.  Still need to work on deme-level spacial resources, but I think everything will work as long as the deme-level spacial grids are setup correctly.

Modified: branches/energy_dev/source/cpu/cTestCPU.cc
===================================================================
--- branches/energy_dev/source/cpu/cTestCPU.cc	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/cpu/cTestCPU.cc	2007-07-27 20:22:35 UTC (rev 1860)
@@ -61,6 +61,9 @@
  
 void cTestCPU::InitResources(int res_method, std::vector<std::pair<int, std::vector<double> > > * res, int update, int time_spent_offset)
 {  
+  //FOR DEMES
+  m_deme_resource_count.SetSize(0);
+
   m_res_method = (eTestCPUResourceMethod)res_method;
   // Make sure it's valid
   if(res_method < 0 ||  res_method >= RES_LAST) {

Modified: branches/energy_dev/source/cpu/cTestCPU.h
===================================================================
--- branches/energy_dev/source/cpu/cTestCPU.h	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/cpu/cTestCPU.h	2007-07-27 20:22:35 UTC (rev 1860)
@@ -74,6 +74,7 @@
   int m_res_time_spent_offset;
   int m_res_update;
   cResourceCount m_resource_count;
+  cResourceCount m_deme_resource_count;
 
   bool ProcessGestation(cAvidaContext& ctx, cCPUTestInfo& test_info, int cur_depth);
   bool TestGenome_Body(cAvidaContext& ctx, cCPUTestInfo& test_info, const cGenome& genome, int cur_depth);
@@ -102,7 +103,8 @@
   void ResetInputs(cAvidaContext& ctx);
 
   inline int GetReceiveValue();
-  inline const tArray<double>& GetResources();  
+  inline const tArray<double>& GetResources();
+  inline const tArray<double>& GetDemeResources(int deme_id);
   inline void SetResource(int id, double new_level);
   void InitResources(int res_method = RES_INITIAL, std::vector<std::pair<int, std::vector<double> > > * res = NULL, int update = 0, int time_spent_offset = 0);
   void SetResourceUpdate(int update, bool round_to_closest = false);
@@ -148,6 +150,11 @@
     return m_resource_count.GetResources();
 }
 
+inline const tArray<double>& cTestCPU::GetDemeResources(int deme_id)
+{
+    return m_deme_resource_count.GetResources();
+}
+
 inline void cTestCPU::SetResource(int id, double new_level)
 {
   m_resource_count.Set(id, new_level);

Modified: branches/energy_dev/source/cpu/cTestCPUInterface.cc
===================================================================
--- branches/energy_dev/source/cpu/cTestCPUInterface.cc	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/cpu/cTestCPUInterface.cc	2007-07-27 20:22:35 UTC (rev 1860)
@@ -70,6 +70,10 @@
   return m_testcpu->GetResources();  
 }
 
+const tArray<double>& cTestCPUInterface::GetDemeResources(int deme_id) {
+  return m_testcpu->GetDemeResources(deme_id);
+}
+
 void cTestCPUInterface::UpdateResources(const tArray<double>& res_change)
 {
    m_testcpu->ModifyResources(res_change);

Modified: branches/energy_dev/source/cpu/cTestCPUInterface.h
===================================================================
--- branches/energy_dev/source/cpu/cTestCPUInterface.h	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/cpu/cTestCPUInterface.h	2007-07-27 20:22:35 UTC (rev 1860)
@@ -45,7 +45,9 @@
   virtual ~cTestCPUInterface() { ; }
 
   int GetCellID() { return -1; }
+  int GetDemeID() { return -1; }
   void SetCellID(int in_id) { ; }
+  void SetDemeID(int in_id) { ; }
 
   bool Divide(cAvidaContext& ctx, cOrganism* parent, cGenome& child_genome);
   cOrganism* GetNeighbor();
@@ -56,7 +58,9 @@
   void ResetInputs(cAvidaContext& ctx);
   int Debug();
   const tArray<double>& GetResources();
+  const tArray<double>& GetDemeResources(int deme_id);
   void UpdateResources(const tArray<double>& res_change);
+  void UpdateDemeResources(const tArray<double>& res_change) {;}
   void Die();
   void Kaboom(int distance);
   void SpawnDeme();

Modified: branches/energy_dev/source/main/cDeme.h
===================================================================
--- branches/energy_dev/source/main/cDeme.h	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cDeme.h	2007-07-27 20:22:35 UTC (rev 1860)
@@ -48,11 +48,9 @@
   cDeme(const cDeme&); // @not_implemented
   
   cResourceCount deme_resource_count; //!< Resources available to the deme
-  cStringList deme_resource_names; //!< Name of resources used by this deme
-
   
 public:
-  cDeme() : width(0), birth_count(0), org_count(0), _age(0) { ; }
+  cDeme() : width(0), birth_count(0), org_count(0), _age(0), deme_resource_count(0) { ; }
   ~cDeme() { ; }
 
   void Setup(const tArray<int>& in_cells, int in_width = -1);
@@ -90,7 +88,8 @@
     updates since the last time Reset() was called. */
   int GetAge() const { return _age; }
   
+  cResourceCount& GetDemeResourceCount() { return deme_resource_count; }
+//  void SetDemeResourceCount(cResourceCount in_res_count) { deme_resource_count = in_res_count; }
 };
 
 #endif
-

Modified: branches/energy_dev/source/main/cOrgInterface.h
===================================================================
--- branches/energy_dev/source/main/cOrgInterface.h	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cOrgInterface.h	2007-07-27 20:22:35 UTC (rev 1860)
@@ -53,7 +53,9 @@
   virtual ~cOrgInterface() { ; }
 
   virtual int GetCellID() = 0;
+  virtual int GetDemeID() = 0;
   virtual void SetCellID(int in_id) = 0;
+  virtual void SetDemeID(int in_id) = 0;
 
   virtual bool Divide(cAvidaContext& ctx, cOrganism* parent, cGenome& child_genome) = 0;
   virtual cOrganism* GetNeighbor() = 0;
@@ -65,7 +67,9 @@
   virtual void ResetInputs(cAvidaContext& ctx) = 0;
   virtual int Debug() = 0;
   virtual const tArray<double>& GetResources() = 0;
+  virtual const tArray<double>& GetDemeResources(int deme_id) = 0;  
   virtual void UpdateResources(const tArray<double>& res_change) = 0;
+  virtual void UpdateDemeResources(const tArray<double>& res_change) = 0;
   virtual void Die() = 0;
   virtual void Kaboom(int distance) = 0;
   virtual void SpawnDeme() = 0;

Modified: branches/energy_dev/source/main/cOrganism.cc
===================================================================
--- branches/energy_dev/source/main/cOrganism.cc	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cOrganism.cc	2007-07-27 20:22:35 UTC (rev 1860)
@@ -37,6 +37,7 @@
 #include "cInjectGenotype.h"
 #include "cInstSet.h"
 #include "cOrgSinkMessage.h"
+#include "cPopulationCell.h"
 #include "cPopulation.h"
 #include "cStringUtil.h"
 #include "cTaskContext.h"
@@ -179,7 +180,9 @@
                          const int value,
                          const bool on_divide)
 {
-  const tArray<double> & resource_count = m_interface->GetResources();
+  const int deme_id = m_interface->GetDemeID();
+  const tArray<double> & global_resource_count = m_interface->GetResources();
+  const tArray<double> & deme_resource_count = m_interface->GetDemeResources(deme_id);
   
   tList<tBuffer<int> > other_input_list;
   tList<tBuffer<int> > other_output_list;
@@ -216,7 +219,8 @@
   // if on IO add value to m_output_buf, if on divide don't need to
   if(!on_divide) output_buffer.Add(value);
   
-  tArray<double> res_change(resource_count.GetSize());
+  tArray<double> global_res_change(global_resource_count.GetSize());
+  tArray<double> deme_res_change(deme_resource_count.GetSize());
   tArray<int> insts_triggered;
   
   tBuffer<int>* received_messages_point = &m_received_messages;
@@ -224,8 +228,13 @@
   
   cTaskContext taskctx(m_interface, input_buffer, output_buffer, other_input_list, 
                        other_output_list, net_valid, 0, on_divide, received_messages_point);
-  bool task_completed = m_phenotype.TestOutput(ctx, taskctx, resource_count, res_change, insts_triggered);
+  bool task_completed = m_phenotype.TestOutput(ctx, taskctx, global_resource_count, global_res_change, insts_triggered);
   
+  if(deme_resource_count.GetSize() > 0) {
+    // deme task checking
+    task_completed = m_phenotype.TestOutput(ctx, taskctx, deme_resource_count, deme_res_change, insts_triggered) || task_completed;
+  }
+  
   if(m_world->GetConfig().ENERGY_ENABLED.Get() && m_world->GetConfig().APPLY_ENERGY_METHOD.Get() == 1 && task_completed) {
     m_phenotype.RefreshEnergy();
     m_phenotype.ApplyToEnergyStore();
@@ -235,7 +244,10 @@
     }
   }
 
-  m_interface->UpdateResources(res_change);
+  m_interface->UpdateResources(global_res_change);
+  
+  //update deme resources
+  m_interface->UpdateDemeResources(deme_res_change);  
 
   //if(m_world->GetConfig().CLEAR_ON_OUTPUT.Get()) input_buffer.Clear();  @JEB Not fully implemented 
 
@@ -243,10 +255,8 @@
     const int cur_inst = insts_triggered[i];
     m_hardware->ProcessBonusInst(ctx, cInstruction(cur_inst));
   }
-  
 }
 
-
 void cOrganism::NetGet(cAvidaContext& ctx, int& value, int& seq)
 {
   assert(m_net);

Modified: branches/energy_dev/source/main/cPhenotype.cc
===================================================================
--- branches/energy_dev/source/main/cPhenotype.cc	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cPhenotype.cc	2007-07-27 20:22:35 UTC (rev 1860)
@@ -792,7 +792,7 @@
   cReactionResult result(num_resources, num_tasks, num_reactions);
 			
   // Run everything through the environment.
-  bool found = env.TestOutput(ctx, result, taskctx, eff_task_count, cur_reaction_count, res_in);
+  bool found = env.TestOutput(ctx, result, taskctx, eff_task_count, cur_reaction_count, res_in); //NEED different eff_task_count and cur_reaction_count for deme resource
 
   // If nothing was found, stop here.
   if (found == false) {

Modified: branches/energy_dev/source/main/cPhenotype.h
===================================================================
--- branches/energy_dev/source/main/cPhenotype.h	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cPhenotype.h	2007-07-27 20:22:35 UTC (rev 1860)
@@ -282,7 +282,7 @@
   int    GetCurBonusInstCount() const { assert(bonus_instruction_count >= 0); return bonus_instruction_count; }
 
   double GetCurMeritBase() const { assert(initialized == true); return CalcSizeMerit(); }
-  double GetStoredEnergy() const { assert(initialized == true); return energy_store; }
+  double GetStoredEnergy() const { return energy_store; }
   double GetEnergyBonus() const { assert(initialized == true); return cur_energy_bonus; }
   
   bool GetToDie() const { assert(initialized == true); return to_die; }

Modified: branches/energy_dev/source/main/cPopulation.cc
===================================================================
--- branches/energy_dev/source/main/cPopulation.cc	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cPopulation.cc	2007-07-27 20:22:35 UTC (rev 1860)
@@ -71,7 +71,7 @@
 cPopulation::cPopulation(cWorld* world)
 : m_world(world)
 , schedule(NULL)
-, resource_count(world->GetEnvironment().GetResourceLib().GetSize())
+//, resource_count(world->GetEnvironment().GetResourceLib().GetSize())
 , birth_chamber(world)
 , environment(world->GetEnvironment())
 , num_organisms(0)
@@ -118,7 +118,7 @@
   
   // Allocate the cells, resources, and market.
   cell_array.Resize(num_cells);
-  resource_count.ResizeSpatialGrids(world_x, world_y);
+//  resource_count.ResizeSpatialGrids(world_x, world_y);
   market.Resize(MARKET_SIZE);
   
   // Setup the cells.  Do things that are not dependent upon topology here.
@@ -177,10 +177,32 @@
   // Setup the resources...
   const cResourceLib & resource_lib = environment.GetResourceLib();
   int global_res_index = -1;
+  int deme_res_index = -1;
+  int num_deme_res = 0;
+
+  //setting size of global and deme-level resources
+  for(int i = 0; i < resource_lib.GetSize(); i++) {
+    cResource * res = resource_lib.GetResource(i);
+    if(res->GetDemeResource())
+      num_deme_res++;
+  }
+  cerr<< resource_lib.GetSize() << " " << num_deme_res << endl;
+  
+  cResourceCount tmp_res_count(resource_lib.GetSize() - num_deme_res);
+  resource_count = tmp_res_count;
+  resource_count.ResizeSpatialGrids(world_x, world_y);
+  
+  for(int i = 0; i < GetNumDemes(); i++) {
+    cResourceCount tmp_deme_res_count(num_deme_res);
+    GetDeme(i).GetDemeResourceCount() = tmp_deme_res_count;
+    //TODO: make sure grid is the right grid GetDeme(i).GetDemeResourceCount().ResizeSpatialGrids(world_x, world_y);
+  }
+
   for (int i = 0; i < resource_lib.GetSize(); i++) {
     cResource * res = resource_lib.GetResource(i);
     if (!res->GetDemeResource()) {
       global_res_index++;
+      cerr<<global_res_index << " " << i <<endl;
       const double decay = 1.0 - res->GetOutflow();
       resource_count.Setup(global_res_index, res->GetName(), res->GetInitial(), 
                            res->GetInflow(), decay,
@@ -193,21 +215,42 @@
                            res->GetOutflowY2(), res->GetCellListPtr(),
                            world->GetVerbosity() );
       m_world->GetStats().SetResourceName(global_res_index, res->GetName());
+    } else if(res->GetDemeResource()) {
+      deme_res_index++;
+      const double decay = 1.0 - res->GetOutflow();
+      for(int j = 0; j < GetNumDemes(); j++) {
+        GetDeme(j).GetDemeResourceCount().Setup(global_res_index, res->GetName(), res->GetInitial(), 
+                                                res->GetInflow(), decay,
+                                                res->GetGeometry(), res->GetXDiffuse(),
+                                                res->GetXGravity(), res->GetYDiffuse(), 
+                                                res->GetYGravity(), res->GetInflowX1(), 
+                                                res->GetInflowX2(), res->GetInflowY1(), 
+                                                res->GetInflowY2(), res->GetOutflowX1(), 
+                                                res->GetOutflowX2(), res->GetOutflowY1(), 
+                                                res->GetOutflowY2(), res->GetCellListPtr(),
+                                                world->GetVerbosity() );
+        // TODO: BB how should I keep stats on deme resources
+//        m_world->GetStats().SetResourceName(deme_res_index, res->GetName());
+      }
+    } else {
+      cerr<< "ERROR: Resource \"" << res->GetName() <<"\"is not a global or deme resource.  Exit";
+      exit(1);
     }
   }
-  
+
   // Load a clone if one is provided, otherwise setup start organism.
   if (m_world->GetConfig().CLONE_FILE.Get() == "-" || m_world->GetConfig().CLONE_FILE.Get() == "") {
     cGenome start_org(0);
     const cString& filename = m_world->GetConfig().START_CREATURE.Get();
-    
+
     if (filename != "-" && filename != "") {
       if (!cGenomeUtil::LoadGenome(filename, world->GetHardwareManager().GetInstSet(), start_org)) {
         cerr << "Error: Unable to load start creature" << endl;
         exit(-1);
       }
-      
-      if (start_org.GetSize() != 0) Inject(start_org);
+      if (start_org.GetSize() != 0) {
+        Inject(start_org);
+      }
       else cerr << "Warning: Zero length start organism, not injecting into initial population." << endl;
     } else {
       cerr << "Warning: No start organism specified." << endl;
@@ -2418,6 +2461,22 @@
   resource_count.ModifyCell(res_change, cell_id);
 }
 
+void cPopulation::UpdateDemeResources(const tArray<double> & res_change, int deme_id)
+{
+  GetDeme(deme_id).GetDemeResourceCount().Modify(res_change);
+}
+
+void cPopulation::UpdateDemeResource(int id, double change, int deme_id)
+{
+  GetDeme(deme_id).GetDemeResourceCount().Modify(id, change);
+}
+
+void cPopulation::UpdateDemeCellResources(const tArray<double> & res_change, 
+                                          const int cell_id)
+{
+  GetDeme(GetCell(cell_id).GetDemeID()).GetDemeResourceCount().ModifyCell(res_change, cell_id);
+}
+
 void cPopulation::SetResource(int id, double new_level)
 {
   resource_count.Set(id, new_level);
@@ -2758,7 +2817,6 @@
   if (new_merit <= old_merit) {
 	  phenotype.SetIsDonorCur(); }  
   else  { phenotype.SetIsReceiver(); } 
-  std::cerr<<"[cPopulation::UpdateMerit] phenotype.GetMerit() = "<<phenotype.GetMerit()<< " new_merit = " << new_merit << std::endl; 
   schedule->Adjust(cell_id, phenotype.GetMerit());
   
   return true;

Modified: branches/energy_dev/source/main/cPopulation.h
===================================================================
--- branches/energy_dev/source/main/cPopulation.h	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cPopulation.h	2007-07-27 20:22:35 UTC (rev 1860)
@@ -213,11 +213,16 @@
   cPopulationCell& GetCell(int in_num);
   const tArray<double>& GetResources() const { return resource_count.GetResources(); }
   const tArray<double>& GetCellResources(int cell_id) const { return resource_count.GetCellResources(cell_id); }
+  const tArray<double>& GetDemeResources(int deme_id) { return GetDeme(deme_id).GetDemeResourceCount().GetResources(); }
   cBirthChamber& GetBirthChamber(int id) { (void) id; return birth_chamber; }
 
   void UpdateResources(const tArray<double>& res_change);
   void UpdateResource(int id, double change);
   void UpdateCellResources(const tArray<double>& res_change, const int cell_id);
+  void UpdateDemeResources(const tArray<double> & res_change, int deme_id);
+  void UpdateDemeResource(int id, double change, int deme_id);
+  void UpdateDemeCellResources(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; }

Modified: branches/energy_dev/source/main/cPopulationCell.cc
===================================================================
--- branches/energy_dev/source/main/cPopulationCell.cc	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cPopulationCell.cc	2007-07-27 20:22:35 UTC (rev 1860)
@@ -183,6 +183,7 @@
 
   // Adjust the organism's attributes to match this cell.
   organism->GetOrgInterface().SetCellID(cell_id);
+  organism->GetOrgInterface().SetDemeID(deme_id);
 }
 
 cOrganism * cPopulationCell::RemoveOrganism()

Modified: branches/energy_dev/source/main/cPopulationInterface.cc
===================================================================
--- branches/energy_dev/source/main/cPopulationInterface.cc	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cPopulationInterface.cc	2007-07-27 20:22:35 UTC (rev 1860)
@@ -104,11 +104,21 @@
   return m_world->GetPopulation().GetCellResources(m_cell_id);
 }
 
+const tArray<double> & cPopulationInterface::GetDemeResources(int deme_id)
+{
+  return m_world->GetPopulation().GetDemeResources(deme_id);
+}
+
 void cPopulationInterface::UpdateResources(const tArray<double> & res_change)
 {
   return m_world->GetPopulation().UpdateCellResources(res_change, m_cell_id);
 }
 
+void cPopulationInterface::UpdateDemeResources(const tArray<double> & res_change)
+{
+  return m_world->GetPopulation().UpdateDemeCellResources(res_change, m_cell_id);
+}
+
 void cPopulationInterface::Die()
 {
   cPopulationCell & cell = m_world->GetPopulation().GetCell(m_cell_id);

Modified: branches/energy_dev/source/main/cPopulationInterface.h
===================================================================
--- branches/energy_dev/source/main/cPopulationInterface.h	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cPopulationInterface.h	2007-07-27 20:22:35 UTC (rev 1860)
@@ -44,17 +44,20 @@
 private:
   cWorld* m_world;
   int m_cell_id;
+  int m_deme_id;
 
   cPopulationInterface(); // @not_implemented
   cPopulationInterface(const cPopulationInterface&); // @not_implemented
   cPopulationInterface operator=(const cPopulationInterface&); // @not_implemented
   
 public:
-  cPopulationInterface(cWorld* world) : m_world(world), m_cell_id(-1) { ; }
+  cPopulationInterface(cWorld* world) : m_world(world), m_cell_id(-1), m_deme_id(-1) { ; }
   virtual ~cPopulationInterface() { ; }
 
   int GetCellID() { return m_cell_id; }
+  int GetDemeID() { return m_deme_id; }
   void SetCellID(int in_id) { m_cell_id = in_id; }
+  void SetDemeID(int in_id) { m_deme_id = in_id; }
 
   bool Divide(cAvidaContext& ctx, cOrganism* parent, cGenome& child_genome);
   cOrganism* GetNeighbor();
@@ -66,7 +69,9 @@
   void ResetInputs(cAvidaContext& ctx);
   int Debug();
   const tArray<double>& GetResources();
+  const tArray<double>& GetDemeResources(int deme_id);
   void UpdateResources(const tArray<double>& res_change);
+  void UpdateDemeResources(const tArray<double>& res_change);
   void Die();
   void Kaboom(int distance);
   void SpawnDeme();

Modified: branches/energy_dev/source/main/cResourceCount.cc
===================================================================
--- branches/energy_dev/source/main/cResourceCount.cc	2007-07-27 20:12:19 UTC (rev 1859)
+++ branches/energy_dev/source/main/cResourceCount.cc	2007-07-27 20:22:35 UTC (rev 1860)
@@ -150,6 +150,7 @@
   inflow_precalc.SetAll(0.0);
   geometry.SetAll(nGeometry::GLOBAL);
   curr_grid_res_cnt.SetAll(0.0);
+  //DO spacial resources need to be set to zero?
 }
 
 cResourceCount::~cResourceCount()




More information about the Avida-cvs mailing list