1 /*
2  *  cResourceCount.h
3  *  Avida
4  *
5  *  Called "resource_count.hh" prior to 12/5/05.
6  *  Copyright 1999-2011 Michigan State University. All rights reserved.
7  *  Copyright 1993-2001 California Institute of Technology.
8  *
9  *
10  *  This file is part of Avida.
11  *
12  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
13  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
14  *
15  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
19  *  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef cResourceCount_h
24 #define cResourceCount_h
25 
26 #include "avida/Avida.h"
27 
28 #include "cSpatialResCount.h"
29 #include "cString.h"
30 #include "tArray.h"
31 #include "cAvidaContext.h"
32 #include "tMatrix.h"
33 #include "nGeometry.h"
34 #ifndef tArrayMap_h
35 #include "tArrayMap.h"
36 #endif
37 
38 class cWorld;
39 
40 class cResourceCount
41 {
42 private:
43   mutable tArray<cString> resource_name;
44   mutable tArray<double> resource_initial;  // Initial quantity of each resource
45   mutable tArray<double> resource_count;  // Current quantity of each resource
46   tArray<double> decay_rate;      // Multiplies resource count at each step
47   tArray<double> inflow_rate;     // An increment for resource at each step
48   tMatrix<double> decay_precalc;  // Precalculation of decay values
49   tMatrix<double> inflow_precalc; // Precalculation of inflow values
50   tArray<int> geometry;           // Spatial layout of each resource
51   mutable tArray<cSpatialResCount* > spatial_resource_count;
52   mutable tArray<double> curr_grid_res_cnt;
53   mutable tArray< tArray<double> > curr_spatial_res_cnt;
54   int verbosity;
55   tArray< tArray<int> > cell_lists;
56 
57   // Setup the update process to use lazy evaluation...
58   cWorld* m_world;
59   mutable double update_time;     // Portion of an update compleated...
60   mutable double spatial_update_time;
61   mutable int m_last_updated;
62   mutable int m_spatial_update;
63 
64   void DoUpdates(cAvidaContext& ctx, bool global_only = false) const;         // Update resource count based on update time
65 
66   // A few constants to describe update process...
67   static const double UPDATE_STEP;   // Fraction of an update per step
68   static const double EPSILON;       // Tolorance for round off errors
69   static const int PRECALC_DISTANCE; // Number of steps to precalculate
70 
71 public:
72   cResourceCount(int num_resources = 0);
73   cResourceCount(const cResourceCount&);
74   ~cResourceCount();
75 
76   const cResourceCount& operator=(const cResourceCount&);
77 
78   void SetSize(int num_resources);
79   void SetCellResources(int cell_id, const tArray<double> & res);
80 
81   void Setup(cWorld* world, const int& id, const cString& name, const double& initial, const double& inflow, const double& decay,
82 	   const int& in_geometry, const double& in_xdiffuse, const double& in_xgravity,
83 	   const double& in_ydiffuse, const double& in_ygravity,
84 	   const int& in_inflowX1, const int& in_inflowX2, const int& in_inflowY1, const int& in_inflowY2,
85 	   const int& in_outflowX1, const int& in_outflowX2, const int& in_outflowY1,
86 	   const int& in_outflowY2, tArray<cCellResource> *in_cell_list_ptr,
87 	   tArray<int> *in_cell_id_list_ptr, const int& verbosity_level,
88 	   const bool& isdynamic, const int& in_peaks,
89 	   const double& in_min_height, const double& in_min_radius, const double& in_radius_range,
90 	   const double& in_ah, const double& in_ar,
91 	   const double& in_acx, const double& in_acy,
92 	   const double& in_hstepscale, const double& in_rstepscale,
93 	   const double& in_cstepscalex, const double& in_cstepscaley,
94 	   const double& in_hstep, const double& in_rstep,
95 	   const double& in_cstepx, const double& in_cstepy,
96 	   const int& in_update_dynamic, const int& in_peakx, const int& in_peaky,
97 	   const int& in_height, const int& in_spread, const double& in_plateau, const int& in_decay,
98      const int& in_max_x, const int& in_min_x, const int& in_max_y, const int& in_min_y, const double& in_move_a_scaler,
99      const int& in_updatestep, const int& in_halo, const int& in_halo_inner_radius, const int& in_halo_width,
100      const int& in_halo_anchor_x, const int& in_halo_anchor_y, const int& in_move_speed,
101      const double& in_plateau_inflow, const double& in_plateau_outflow, const double& in_cone_inflow, const double& in_cone_outflow,
102      const double& in_gradient_inflow, const int& in_is_plateau_common, const double& in_floor, const int& in_habitat,
103      const int& in_min_size, const int& in_max_size, const int& in_config, const int& in_count, const double& in_resistance,
104      const double& in_init_plat, const double& in_threshold, const int& in_refuge, const bool& isgradient
105 	   );
106 
107   void SetGradientCount(cAvidaContext& ctx, cWorld* world, const int& res_id, const int& peakx, const int& peaky,
108     const int& height, const int& spread, const double& plateau, const int& decay,
109     const int& max_x, const int& min_x, const int& max_y, const int& min_y, const double& move_a_scaler,
110     const int& updatestep, const int& halo, const int& halo_inner_radius, const int& halo_width,
111     const int& halo_anchor_x, const int& halo_anchor_y, const int& move_speed,
112     const double& plateau_inflow, const double& plateau_outflow, const double& cone_inflow, const double& cone_outflow,
113     const double& gradient_inflow, const int& is_plateau_common, const double& floor, const int& habitat,
114     const int& min_size, const int& max_size, const int& config, const int& count, const double& resistance,
115     const double& plat_val, const double& threshold, const int& refuge);
116   void SetGradientPlatInflow(const int& res_id, const double& inflow);
117   void SetGradientPlatOutflow(const int& res_id, const double& outflow);
118   void SetGradientConeInflow(const int& res_id, const double& inflow);
119   void SetGradientConeOutflow(const int& res_id, const double& outflow);
120   void SetGradientInflow(const int& res_id, const double& inflow);
121   void SetGradPlatVarInflow(const int& res_id, const double& mean, const double& variance, const int& type);
122   void SetPredatoryResource(const int& res_id, const double& odds, const int& juvsper);
123   void SetProbabilisticResource(cAvidaContext& ctx, const int& res_id, const double& initial, const double& inflow,
124                                 const double& outflow, const double& lambda, const double& theta, const int& x, const int& y, const int& count);
125 
126   int GetResourceCountID(const cString& res_name);
127   double GetInflow(const cString& name);
128   void SetInflow(const cString& name, const double _inflow);
129   double GetDecay(const cString& name);
130   void SetDecay(const cString& name, const double _decay);
131 
132   void Update(double in_time);
133 
GetSize(void)134   int GetSize(void) const { return resource_count.GetSize(); }
ReadResources(void)135   const tArray<double>& ReadResources(void) const { return resource_count; }
136   const tArray<double>& GetResources(cAvidaContext& ctx) const;
137   const tArray<double>& GetCellResources(int cell_id, cAvidaContext& ctx) const;
138   const tArray<double>& GetFrozenResources(cAvidaContext& ctx, int cell_id) const;
139   const tArray<int>& GetResourcesGeometry() const;
GetResourceGeometry(int res_id)140   int GetResourceGeometry(int res_id) const { return geometry[res_id]; }
141   const tArray<tArray<double> >& GetSpatialRes(cAvidaContext& ctx);
GetCellIdLists()142   const tArray<tArray<int> >& GetCellIdLists() const { return cell_lists; }
143   void Modify(cAvidaContext& ctx, const tArray<double>& res_change);
144   void Modify(cAvidaContext& ctx, int id, double change);
145   void ModifyCell(cAvidaContext& ctx, const tArray<double> & res_change, int cell_id);
146   void Set(cAvidaContext& ctx, int id, double new_level);
147   double Get(cAvidaContext& ctx, int id) const;
148   void ResizeSpatialGrids(int in_x, int in_y);
GetSpatialResource(int id)149   cSpatialResCount GetSpatialResource(int id) { return *(spatial_resource_count[id]); }
GetSpatialResource(int id)150   const cSpatialResCount& GetSpatialResource(int id) const { return *(spatial_resource_count[id]); }
151   void ReinitializeResources(cAvidaContext& ctx, double additional_resource);
GetInitialResourceValue(int resourceID)152   double GetInitialResourceValue(int resourceID) const { return resource_initial[resourceID]; }
GetResName(int id)153   const cString& GetResName(int id) const { return resource_name[id]; }
IsSpatial(int id)154   bool IsSpatial(int id) const { return ((geometry[id] != nGeometry::GLOBAL) && (geometry[id] != nGeometry::PARTIAL)); }
155   int GetResourceByName(cString name) const;
156 
157   int GetCurrPeakX(cAvidaContext& ctx, int res_id) const;
158   int GetCurrPeakY(cAvidaContext& ctx, int res_id) const;
159   int GetFrozenPeakX(cAvidaContext& ctx, int res_id) const;
160   int GetFrozenPeakY(cAvidaContext& ctx, int res_id) const;
161   tArray<int>* GetWallCells(int res_id);
162   int GetMinUsedX(int res_id);
163   int GetMinUsedY(int res_id);
164   int GetMaxUsedX(int res_id);
165   int GetMaxUsedY(int res_id);
166 
SetSpatialUpdate(int update)167   void SetSpatialUpdate(int update) { m_spatial_update = update; }
UpdateGlobalResources(cAvidaContext & ctx)168   void UpdateGlobalResources(cAvidaContext& ctx) { DoUpdates(ctx, true); }
UpdateResources(cAvidaContext & ctx)169   void UpdateResources(cAvidaContext& ctx) { DoUpdates(ctx, false); }
170 };
171 
172 #endif
173