[Avida-SVN] r3218 - branches/bdc-coop/source/actions

connel42 at myxo.css.msu.edu connel42 at myxo.css.msu.edu
Thu Apr 23 21:04:03 PDT 2009


Author: connel42
Date: 2009-04-24 00:04:03 -0400 (Fri, 24 Apr 2009)
New Revision: 3218

Modified:
   branches/bdc-coop/source/actions/PopulationActions.cc
Log:
added kill within radius event

Modified: branches/bdc-coop/source/actions/PopulationActions.cc
===================================================================
--- branches/bdc-coop/source/actions/PopulationActions.cc	2009-04-22 18:24:26 UTC (rev 3217)
+++ branches/bdc-coop/source/actions/PopulationActions.cc	2009-04-24 04:04:03 UTC (rev 3218)
@@ -2710,6 +2710,142 @@
   };
 
 
+/*
+ Kill organisms within a N-cell radius of randomly-chosen cells if the level of the given resource
+ in the cell is below the configured threshold
+ 
+ Parameters:
+ - The number of kill zones to create (default: 0)
+ - The radius of the kill zone (default: 0)
+ - The name of the resource
+ - The amount of resource below which to execute the kill (default: 0)
+ */
+
+class cActionKillWithinRadiusBelowResourceThreshold : public cAction
+  {
+  private:
+    cString m_resname;
+    int m_numzones;
+    int m_radius;
+    double m_threshold;
+  public:
+    cActionKillWithinRadiusBelowResourceThreshold(cWorld* world, const cString& args) : cAction(world, args), m_numzones(0), m_radius(0)
+    {
+      cString largs(args);
+      if (largs.GetSize()) m_numzones = largs.PopWord().AsInt();
+      if (largs.GetSize()) m_radius = largs.PopWord().AsInt();
+      if (largs.GetSize()) m_resname = largs.PopWord();
+      if (largs.GetSize()) m_threshold = largs.PopWord().AsDouble();
+    }
+    
+    static const cString GetDescription() { return "Arguments: [int numzones=0, int radius=0, string resource name, double threshold=0]"; }
+    
+    void Process(cAvidaContext& ctx)
+    {
+      double level;
+      int target_cell;
+      
+      assert(m_numzones >= 0);
+      assert(m_radius >= 0);
+      
+      int res_id = m_world->GetPopulation().GetResourceCount().GetResourceCountID(m_resname);
+      assert(res_id != -1);
+      
+      for(int i=0; i < m_numzones; i++) {
+        target_cell = m_world->GetRandom().GetInt(0, m_world->GetPopulation().GetSize()-1);
+        cPopulationCell& cell = m_world->GetPopulation().GetCell(target_cell);
+        
+        std::set<cPopulationCell*> cell_set;
+        cell.GetNeighboringCells(cell_set, m_radius);
+        
+        //cout << "BDCDEBUG: num kill zones is " << m_numzones << endl;
+        //cout << "BDCDEBUG: kill center is at " << target_cell << endl;
+        //cout << "BDCDEBUG: radius is " << m_radius << endl;
+        //cout << "BDCDEBUG: size of neighboring cell set is " << cell_set.size() << endl;
+        
+        for(std::set<cPopulationCell*>::iterator i=cell_set.begin(); i!=cell_set.end(); ++i) {
+          level = m_world->GetPopulation().GetResourceCount().GetSpatialResource(res_id).GetAmount((*i)->GetID());
+          
+          if(level < m_threshold) {
+            //cout << "BDCDEBUG: organism at cell " << (*i)->GetID() << " has a resource level of " << level << " (below threshold of " << m_threshold << ") and will be killed" << endl;
+            m_world->GetPopulation().KillOrganism(m_world->GetPopulation().GetCell(target_cell));
+            m_world->GetStats().IncNumOrgsKilled();
+          }
+          //else { cout << "BDCDEBUG: organism at cell " << (*i)->GetID() << " has a resource level of " << level << " (above threshold of " << m_threshold << ") and will NOT be killed" << endl; }
+          
+        } //End iterating through list of neighbor cells
+
+      } //End iterating through kill zones
+      
+    } //End Process()
+  };
+
+
+/*
+ Kill organisms within a N-cell radius of randomly-chosen cells if the level of the given resource
+ in the cell is above the configured threshold
+ 
+ Parameters:
+ - The number of kill zones to create (default: 0)
+ - The radius of the kill zone (default: 0)
+ - The name of the resource
+ - The amount of resource below which to execute the kill (default: 0)
+ */
+
+class cActionKillWithinRadiusAboveResourceThreshold : public cAction
+  {
+  private:
+    cString m_resname;
+    int m_numzones;
+    int m_radius;
+    double m_threshold;
+  public:
+    cActionKillWithinRadiusAboveResourceThreshold(cWorld* world, const cString& args) : cAction(world, args), m_numzones(0), m_radius(0)
+    {
+      cString largs(args);
+      if (largs.GetSize()) m_numzones = largs.PopWord().AsInt();
+      if (largs.GetSize()) m_radius = largs.PopWord().AsInt();
+      if (largs.GetSize()) m_resname = largs.PopWord();
+      if (largs.GetSize()) m_threshold = largs.PopWord().AsDouble();
+    }
+    
+    static const cString GetDescription() { return "Arguments: [int numzones=0, int radius=0, string resource name, double threshold=0]"; }
+    
+    void Process(cAvidaContext& ctx)
+    {
+      double level;
+      int target_cell;
+      
+      assert(m_numzones >= 0);
+      assert(m_radius >= 0);
+      
+      int res_id = m_world->GetPopulation().GetResourceCount().GetResourceCountID(m_resname);
+      
+      assert(res_id != -1);
+      
+      for(int i=0; i < m_numzones; i++) {
+        target_cell = m_world->GetRandom().GetInt(0, m_world->GetPopulation().GetSize()-1);
+        cPopulationCell& cell = m_world->GetPopulation().GetCell(target_cell);
+        
+        std::set<cPopulationCell*> cell_set;
+        cell.GetNeighboringCells(cell_set, m_radius);
+        
+        for(std::set<cPopulationCell*>::iterator i=cell_set.begin(); i!=cell_set.end(); ++i) {
+          level = m_world->GetPopulation().GetResourceCount().GetSpatialResource(res_id).GetAmount((*i)->GetID());
+          
+          if(level > m_threshold) {
+            m_world->GetPopulation().KillOrganism(m_world->GetPopulation().GetCell(target_cell));
+            m_world->GetStats().IncNumOrgsKilled();
+          }
+          
+        } //End iterating through list of neighbor cells
+        
+      } //End iterating through kill zones
+      
+    } //End Process()
+  };
+
+
 void RegisterPopulationActions(cActionLibrary* action_lib)
 {
   action_lib->Register<cActionInject>("Inject");
@@ -2812,4 +2948,7 @@
   
   action_lib->Register<cActionKillNBelowResourceThreshold>("KillNBelowResourceThreshold");
   action_lib->Register<cActionKillNAboveResourceThreshold>("KillNAboveResourceThreshold");
+  action_lib->Register<cActionKillWithinRadiusBelowResourceThreshold>("KillWithinRadiusBelowResourceThreshold");
+  action_lib->Register<cActionKillWithinRadiusAboveResourceThreshold>("KillWithinRadiusAboveResourceThreshold");
+  
 }




More information about the Avida-cvs mailing list