[Avida-SVN] r3290 - in development/source: actions main

connel42 at myxo.css.msu.edu connel42 at myxo.css.msu.edu
Fri Jun 5 08:35:34 PDT 2009


Author: connel42
Date: 2009-06-05 11:35:34 -0400 (Fri, 05 Jun 2009)
New Revision: 3290

Modified:
   development/source/actions/PopulationActions.cc
   development/source/main/cDeme.cc
   development/source/main/cDeme.h
   development/source/main/cResourceCount.cc
   development/source/main/cResourceCount.h
Log:
Added environmental-based infection treatment, a few deme resource methods

Modified: development/source/actions/PopulationActions.cc
===================================================================
--- development/source/actions/PopulationActions.cc	2009-06-04 20:29:23 UTC (rev 3289)
+++ development/source/actions/PopulationActions.cc	2009-06-05 15:35:34 UTC (rev 3290)
@@ -1073,6 +1073,124 @@
 
 
 /*
+ Decay the given resource in treatable demes over time
+ 
+ Parameters:
+ - The name of resource to decay
+ - How the amount of decay decreases over time ['const', 'lin', 'taper'] -- CASE SENSITIVE!
+   - none: the amount of resource decayed remains constant over time
+   - lin: the amount of resource decayed decreases linearly throughout the duration
+   - taper: retains a slowly-decreasing amount of decay then drops off sharply at the end of the duration
+ - Base amount to be removed.
+ - Duration (how long this resource decay should last.  works only with lin and exp.  none lasts infinitely.)
+ */
+
+class cAction_TherapyDecayDemeResource : public cAction
+  {
+  private:
+    cString m_resname;
+    cString m_decrtype;
+    double m_amount;
+    double m_duration;
+  public:
+    cAction_TherapyDecayDemeResource(cWorld* world, const cString& args) : cAction(world, args), m_amount(0), m_duration(1)
+    {
+      cString largs(args);
+      if (largs.GetSize()) m_resname = largs.PopWord();
+      if (largs.GetSize()) m_decrtype = largs.PopWord();
+      if (largs.GetSize()) m_amount = largs.PopWord().AsDouble();
+      if (largs.GetSize()) m_duration = largs.PopWord().AsInt();
+      assert(m_amount >= 0);
+      assert(m_amount <= 1);
+      assert(m_duration >= 1);
+    }
+    
+    static const cString GetDescription() { return "Arguments: [string resource_name=resname, string decrease_type=(none|lin|exp), double amount=0.2]"; }
+    
+    void Process(cAvidaContext& ctx)
+    {
+      double adjusted_amount;
+      int time_since_treatment;
+      int latest_treatment_age;
+      std::set<int> treatment_ages;
+      int deme_age;
+      
+      //adjusted amount will be something like max(0, 1 - (m_amount * time_since_treatment)) for linear
+            
+      cPopulation& pop = m_world->GetPopulation();
+      
+      for (int d = 0; d < pop.GetNumDemes(); d++) {
+        
+        cDeme &deme = pop.GetDeme(d);
+        deme_age = deme.GetAge();
+        latest_treatment_age = -1;
+        time_since_treatment = INT_MAX;
+        
+        if(deme.isTreatable()) {
+                    
+          treatment_ages = deme.GetTreatmentAges();
+          
+          // Find out the last update at which this treatment was started
+          for (std::set<int>::iterator it = treatment_ages.begin(); it != treatment_ages.end(); it++) {
+            if( (*it < deme_age) && (*it > latest_treatment_age) ) {
+              latest_treatment_age = *it;
+              time_since_treatment = deme_age - latest_treatment_age;
+            }
+          }
+          
+          // If we haven't begun treatment on this deme, or if this treatment is over, skip it.
+          if ((latest_treatment_age == -1) || (time_since_treatment > m_duration)) {
+            continue;
+          }
+                          
+          // Find out how much to decrease the resource by
+          // none - as long as the treatment is ongoing, it is at full force
+          // lin - as the treatment continues, the amount decayed decreases linearly
+          // exp - as treatment continues, the amount decayed decreases exponentially
+          if(m_decrtype == "const") {
+            if(time_since_treatment >= m_duration) {
+              adjusted_amount = 0;
+            } else {
+              adjusted_amount = m_amount;
+            }
+          } else if (m_decrtype == "lin") {
+            adjusted_amount = max(0.0, 1 - (time_since_treatment/m_duration)) * m_amount;
+          } else if (m_decrtype == "taper") {
+            adjusted_amount = max(0.0, 1 - pow(time_since_treatment/m_duration, 2)) * m_amount;
+          } else {
+            adjusted_amount = 0;
+          }
+                    
+          cResourceCount res = deme.GetDemeResourceCount();
+          const int resid = res.GetResourceByName(m_resname);
+          
+          if(resid == -1)
+          {
+            //Resource doesn't exist for this deme.  This is a bad situation, but just go to next deme.
+            cerr << "Error: Resource \"" << m_resname << "\" not defined for this deme" << endl;
+            continue;
+          }
+          
+          if(res.IsSpatial(resid)) {
+            for (int c = 0; c < deme.GetWidth() * deme.GetHeight(); c++) {
+              deme.AdjustSpatialResource(c, resid, -1 * deme.GetSpatialResource(c, resid) * adjusted_amount); 
+            } //End iterating through all cells
+          }
+          else
+          {
+            deme.AdjustResource(resid, -1 * res.Get(resid) * adjusted_amount);
+          }
+          
+        } //End if deme is treatable
+        
+      } //End iterating through all demes
+      
+    } //End Process()
+    
+  };
+
+
+/*
  In avida.cfg, when BASE_MERIT_METHOD is set to 6 (Merit prop. to num times MERIT_BONUS_INST is in genome), 
  the merit is incremented by MERIT_BONUS_EFFECT if MERIT_BONUS_EFFECT is positive and decremented by
  MERIT_BONUS_EFFECT if it is negative. For positive values the counting starts at 1, for negative values it starts
@@ -3152,6 +3270,7 @@
 	// Theraputic deme actions
 	action_lib->Register<cAction_TherapyStructuralNumInst>("TherapyStructuralNumInst");
 	action_lib->Register<cAction_TherapyStructuralRatioDistBetweenNearest>("TherapyStructuralRatioDistBetweenNearest");
+  action_lib->Register< cAction_TherapyDecayDemeResource>("TherapyDecayDemeResource");
 	
 	
   action_lib->Register<cActionToggleRewardInstruction>("ToggleRewardInstruction");

Modified: development/source/main/cDeme.cc
===================================================================
--- development/source/main/cDeme.cc	2009-06-04 20:29:23 UTC (rev 3289)
+++ development/source/main/cDeme.cc	2009-06-05 15:35:34 UTC (rev 3290)
@@ -844,6 +844,38 @@
   
 } //End AddPheromone()
 
+double cDeme::GetSpatialResource(int rel_cellid, int resource_id) const
+{
+  assert(rel_cellid >= 0);
+  assert(rel_cellid < GetSize());
+  assert(resource_id >= 0);
+  assert(resource_id < deme_resource_count.GetSize());
+  
+  tArray<double> cell_resources = deme_resource_count.GetCellResources(rel_cellid);
+  return cell_resources[resource_id];
+}
+
+void cDeme::AdjustSpatialResource(int rel_cellid, int resource_id, double amount)
+{
+  assert(rel_cellid >= 0);
+  assert(rel_cellid < GetSize());
+  assert(resource_id >= 0);
+  assert(resource_id < deme_resource_count.GetSize());
+  
+  tArray<double> res_change;
+  res_change.Resize(deme_resource_count.GetSize(), 0);
+  res_change[resource_id] = amount;
+  
+  deme_resource_count.ModifyCell(res_change, rel_cellid);
+  
+}
+
+void cDeme::AdjustResource(int resource_id, double amount)
+{
+  double new_amount = deme_resource_count.Get(resource_id) + amount;
+  deme_resource_count.Set(resource_id, new_amount);
+}
+
 int cDeme::GetSlotFlowRate() const {
   vector<pair<int, int> >::const_iterator iter = event_slot_end_points.begin();
   while(iter != event_slot_end_points.end()) {

Modified: development/source/main/cDeme.h
===================================================================
--- development/source/main/cDeme.h	2009-06-04 20:29:23 UTC (rev 3289)
+++ development/source/main/cDeme.h	2009-06-05 15:35:34 UTC (rev 3290)
@@ -207,6 +207,7 @@
   void AddTreatmentAge(const int age) { treatment_ages.insert(age); }
   bool IsTreatableAtAge(const int age);
   bool IsTreatableNow() { return IsTreatableAtAge(_age); }
+  std::set<int> GetTreatmentAges() const { return treatment_ages; }
 
   int GetSlotFlowRate() const;
   int GetEventsTotal() const { return eventsTotal; }
@@ -259,6 +260,9 @@
   int GetAge() const { return _age; }
   
   const cResourceCount& GetDemeResourceCount() const { return deme_resource_count; }
+  double GetSpatialResource(int rel_cellid, int resource_id) const;
+  void AdjustSpatialResource(int rel_cellid, int resource_id, double amount);
+  void AdjustResource(int resource_id, double amount);
   void SetDemeResourceCount(const cResourceCount in_res) { deme_resource_count = in_res; }
   void ResizeSpatialGrids(const int in_x, const int in_y) { deme_resource_count.ResizeSpatialGrids(in_x, in_y); }
   void ModifyDemeResCount(const tArray<double> & res_change, const int absolute_cell_id);

Modified: development/source/main/cResourceCount.cc
===================================================================
--- development/source/main/cResourceCount.cc	2009-06-04 20:29:23 UTC (rev 3289)
+++ development/source/main/cResourceCount.cc	2009-06-05 15:35:34 UTC (rev 3290)
@@ -523,3 +523,19 @@
 
   } //End going through the resources
 }
+
+int cResourceCount::GetResourceByName(cString name) const
+{
+  int result = -1;
+  
+  for(int i = 0; i < resource_name.GetSize(); i++)
+  {
+    if(resource_name[i] == name)
+    {
+      result = i;
+    }
+  }
+  
+  return result;
+  
+}

Modified: development/source/main/cResourceCount.h
===================================================================
--- development/source/main/cResourceCount.h	2009-06-04 20:29:23 UTC (rev 3289)
+++ development/source/main/cResourceCount.h	2009-06-05 15:35:34 UTC (rev 3290)
@@ -41,6 +41,9 @@
 #ifndef defs_h
 #include "defs.h"
 #endif
+#ifndef nGeometry_h
+#include "nGeometry.h"
+#endif
 
 class cResourceCount
 {
@@ -98,6 +101,7 @@
   const tArray<double>& GetResources() const;
   const tArray<double>& GetCellResources(int cell_id) const;
   const tArray<int>& GetResourcesGeometry() const;
+  int GetResourceGeometry(int res_id) const { return geometry[res_id]; }
   const tArray<tArray<double> >& GetSpatialRes();
   const tArray<tArray<int> >& GetCellIdLists() const { return cell_lists; }
   void Modify(const tArray<double>& res_change);
@@ -111,6 +115,8 @@
   void ReinitializeResources(double additional_resource);
   double GetInitialResourceValue(int resourceID) const { return resource_initial[resourceID]; }
   const cString& GetResName(int id) const { return resource_name[id]; }
+  bool IsSpatial(int id) const { return ((geometry[id] != nGeometry::GLOBAL) && (geometry[id] != nGeometry::PARTIAL)); }
+  int GetResourceByName(cString name) const;
 };
 
 




More information about the Avida-cvs mailing list