pops-core  0.9
PoPS (Pest or Pathogen Spread) Model Core C++ library
treatments.hpp
Go to the documentation of this file.
1 /*
2  * PoPS model - treatments
3  *
4  * Copyright (C) 2015-2020 by the authors.
5  *
6  * Authors: Anna Petrasova <akratoc gmail com>
7  * Vaclav Petras <wenzeslaus gmail com>
8  *
9  * The code contained herein is licensed under the GNU General Public
10  * License. You may obtain a copy of the GNU General Public License
11  * Version 2 or later at the following locations:
12  *
13  * http://www.opensource.org/licenses/gpl-license.html
14  * http://www.gnu.org/copyleft/gpl.html
15  */
16 
17 #ifndef POPS_TREATMENTS_HPP
18 #define POPS_TREATMENTS_HPP
19 
20 #include "raster.hpp"
21 #include "date.hpp"
22 #include "scheduling.hpp"
23 #include "utils.hpp"
24 
25 #include <map>
26 #include <vector>
27 #include <string>
28 #include <functional>
29 #include <stdexcept>
30 
31 namespace pops {
32 
37 {
38  Ratio,
40 };
41 
49 {
50  std::map<std::string, TreatmentApplication> mapping{
51  {"ratio_to_all", TreatmentApplication::Ratio},
52  {"ratio", TreatmentApplication::Ratio},
53  {"all_infected_in_cell", TreatmentApplication::AllInfectedInCell},
54  {"all infected", TreatmentApplication::AllInfectedInCell}};
55  try {
56  return mapping.at(text);
57  }
58  catch (const std::out_of_range&) {
59  throw std::invalid_argument(
60  "treatment_application_enum_from_string:"
61  " Invalid value '"
62  + text + "' provided");
63  }
64 }
65 
78 template<typename IntegerRaster, typename FloatRaster>
80 {
81 public:
82  virtual unsigned get_start() = 0;
83  virtual unsigned get_end() = 0;
84  virtual bool should_start(unsigned step) = 0;
85  virtual bool should_end(unsigned step) = 0;
86  virtual void apply_treatment(
87  IntegerRaster& infected,
88  std::vector<IntegerRaster>& exposed,
89  IntegerRaster& susceptible,
90  IntegerRaster& resistant,
91  IntegerRaster& total_hosts,
92  const std::vector<std::vector<int>>& spatial_indeices) = 0;
93  virtual void end_treatment(
94  IntegerRaster& susceptible,
95  IntegerRaster& resistant,
96  const std::vector<std::vector<int>>& spatial_indeices) = 0;
97  virtual void apply_treatment_mortality(
98  IntegerRaster& infected,
99  const std::vector<std::vector<int>>& spatial_indeices) = 0;
100  virtual ~AbstractTreatment() {}
101 };
102 
107 template<typename IntegerRaster, typename FloatRaster>
108 class BaseTreatment : public AbstractTreatment<IntegerRaster, FloatRaster>
109 {
110 protected:
111  unsigned start_step_;
112  unsigned end_step_;
113  FloatRaster map_;
115 
116 public:
118  const FloatRaster& map,
119  unsigned start,
120  TreatmentApplication treatment_application)
121  : start_step_(start),
122  end_step_(start),
123  map_(map),
124  application_(treatment_application)
125  {}
126  unsigned get_start() override
127  {
128  return start_step_;
129  }
130  unsigned get_end() override
131  {
132  return end_step_;
133  }
135  IntegerRaster& infected,
136  const std::vector<std::vector<int>>& suitable_cells) override
137  {
138  for (auto indices : suitable_cells) {
139  int i = indices[0];
140  int j = indices[1];
142  infected(i, j) = infected(i, j) - (infected(i, j) * map_(i, j));
143  }
145  infected(i, j) = map_(i, j) ? 0 : infected(i, j);
146  }
147  }
148  }
149 };
150 
156 template<typename IntegerRaster, typename FloatRaster>
157 class SimpleTreatment : public BaseTreatment<IntegerRaster, FloatRaster>
158 {
159 public:
161  const FloatRaster& map,
162  unsigned start,
163  TreatmentApplication treatment_application)
164  : BaseTreatment<IntegerRaster, FloatRaster>(map, start, treatment_application)
165  {}
166  bool should_start(unsigned step) override
167  {
168  if (this->start_step_ == step)
169  return true;
170  return false;
171  }
172  bool should_end(unsigned) override
173  {
174  return false;
175  }
177  IntegerRaster& infected,
178  std::vector<IntegerRaster>& exposed,
179  IntegerRaster& susceptible,
180  IntegerRaster& resistant,
181  IntegerRaster& total_hosts,
182  const std::vector<std::vector<int>>& suitable_cells) override
183  {
184  for (auto indices : suitable_cells) {
185  int i = indices[0];
186  int j = indices[1];
187  int new_exposed_total = 0;
188  int new_infected = 0;
189  int new_susceptible = 0;
190  int new_exposed_individual = 0;
192  new_infected = infected(i, j) - (infected(i, j) * this->map_(i, j));
193  infected(i, j) = new_infected;
194  }
196  new_infected = this->map_(i, j) ? 0 : infected(i, j);
197  infected(i, j) = new_infected;
198  }
199  for (auto& raster : exposed) {
201  new_exposed_individual =
202  raster(i, j) - (raster(i, j) * this->map_(i, j));
203  raster(i, j) = new_exposed_individual;
204  new_exposed_total += new_exposed_individual;
205  }
206  else if (
208  new_exposed_individual =
209  raster(i, j) - (raster(i, j) * this->map_(i, j));
210  raster(i, j) = new_exposed_individual;
211  new_exposed_total += new_exposed_individual;
212  }
213  }
214  new_susceptible =
215  susceptible(i, j) - (susceptible(i, j) * this->map_(i, j));
216  susceptible(i, j) = new_susceptible;
217  total_hosts(i, j) =
218  new_infected + new_susceptible + new_exposed_total + resistant(i, j);
219  }
220  }
222  IntegerRaster&, IntegerRaster&, const std::vector<std::vector<int>>&) override
223  {
224  return;
225  }
226 };
227 
234 template<typename IntegerRaster, typename FloatRaster>
235 class PesticideTreatment : public BaseTreatment<IntegerRaster, FloatRaster>
236 {
237 public:
239  const FloatRaster& map,
240  unsigned start,
241  unsigned end,
242  TreatmentApplication treatment_application)
243  : BaseTreatment<IntegerRaster, FloatRaster>(map, start, treatment_application)
244  {
245  this->end_step_ = end;
246  }
247  bool should_start(unsigned step) override
248  {
249  if (this->start_step_ == step)
250  return true;
251  return false;
252  }
253  bool should_end(unsigned step) override
254  {
255  if (this->end_step_ == step)
256  return true;
257  return false;
258  }
259 
261  IntegerRaster& infected,
262  std::vector<IntegerRaster>& exposed_vector,
263  IntegerRaster& susceptible,
264  IntegerRaster& resistant,
265  IntegerRaster& total_hosts,
266  const std::vector<std::vector<int>>& suitable_cells) override
267  {
268  UNUSED(total_hosts);
269  for (auto indices : suitable_cells) {
270  int i = indices[0];
271  int j = indices[1];
272  int infected_resistant = 0;
273  int exposed_resistant_sum = 0;
274  int susceptible_resistant = susceptible(i, j) * this->map_(i, j);
275  int current_resistant = resistant(i, j);
277  infected_resistant = infected(i, j) * this->map_(i, j);
278  }
280  infected_resistant = this->map_(i, j) ? infected(i, j) : 0;
281  }
282  infected(i, j) -= infected_resistant;
283  for (auto& exposed : exposed_vector) {
284  int exposed_resistant = 0;
286  exposed_resistant = exposed(i, j) * this->map_(i, j);
287  }
288  else if (
290  exposed_resistant = this->map_(i, j) ? exposed(i, j) : 0;
291  }
292  exposed(i, j) -= exposed_resistant;
293  exposed_resistant_sum += exposed_resistant;
294  }
295  resistant(i, j) = infected_resistant + exposed_resistant_sum
296  + susceptible_resistant + current_resistant;
297  susceptible(i, j) -= susceptible_resistant;
298  }
299  }
301  IntegerRaster& susceptible,
302  IntegerRaster& resistant,
303  const std::vector<std::vector<int>>& suitable_cells) override
304  {
305  for (auto indices : suitable_cells) {
306  int i = indices[0];
307  int j = indices[1];
308  if (this->map_(i, j) > 0) {
309  susceptible(i, j) += resistant(i, j);
310  resistant(i, j) = 0;
311  }
312  }
313  }
314 };
315 
328 template<typename IntegerRaster, typename FloatRaster>
330 {
331 private:
332  std::vector<AbstractTreatment<IntegerRaster, FloatRaster>*> treatments;
333  Scheduler scheduler_;
334 
335 public:
336  Treatments(const Scheduler& scheduler) : scheduler_(scheduler) {}
338  {
339  for (auto item : treatments) {
340  delete item;
341  }
342  }
358  const FloatRaster& map,
359  const Date& start_date,
360  int num_days,
361  TreatmentApplication treatment_application)
362  {
363  unsigned start = scheduler_.schedule_action_date(start_date);
364  if (num_days == 0)
365  treatments.push_back(new SimpleTreatment<IntegerRaster, FloatRaster>(
366  map, start, treatment_application));
367  else {
368  Date end_date(start_date);
369  end_date.add_days(num_days);
370  unsigned end = scheduler_.schedule_action_date(end_date);
371  treatments.push_back(new PesticideTreatment<IntegerRaster, FloatRaster>(
372  map, start, end, treatment_application));
373  }
374  }
387  bool manage(
388  unsigned current,
389  IntegerRaster& infected,
390  std::vector<IntegerRaster>& exposed,
391  IntegerRaster& susceptible,
392  IntegerRaster& resistant,
393  IntegerRaster& total_hosts,
394  const std::vector<std::vector<int>>& suitable_cells)
395  {
396  bool changed = false;
397  for (unsigned i = 0; i < treatments.size(); i++) {
398  if (treatments[i]->should_start(current)) {
399  treatments[i]->apply_treatment(
400  infected,
401  exposed,
402  susceptible,
403  resistant,
404  total_hosts,
405  suitable_cells);
406  changed = true;
407  }
408  else if (treatments[i]->should_end(current)) {
409  treatments[i]->end_treatment(susceptible, resistant, suitable_cells);
410  changed = true;
411  }
412  }
413  return changed;
414  }
422  unsigned current,
423  IntegerRaster& infected,
424  const std::vector<std::vector<int>>& suitable_cells)
425  {
426  bool applied = false;
427  for (unsigned i = 0; i < treatments.size(); i++)
428  if (treatments[i]->should_start(current)) {
429  treatments[i]->apply_treatment_mortality(infected, suitable_cells);
430  applied = true;
431  }
432  return applied;
433  }
439  void clear_after_step(unsigned step)
440  {
441  for (auto& treatment : treatments) {
442  if (treatment->get_start() > step) {
443  delete treatment;
444  treatment = nullptr;
445  }
446  }
447  treatments.erase(
448  std::remove(treatments.begin(), treatments.end(), nullptr),
449  treatments.end());
450  }
451 };
452 
453 } // namespace pops
454 #endif // POPS_TREATMENTS_HPP
pops::AbstractTreatment::should_start
virtual bool should_start(unsigned step)=0
pops::PesticideTreatment::should_end
bool should_end(unsigned step) override
Definition: treatments.hpp:253
raster.hpp
pops::Date
Representation and manipulation of a date for the simulation.
Definition: date.hpp:32
pops::Treatments::manage_mortality
bool manage_mortality(unsigned current, IntegerRaster &infected, const std::vector< std::vector< int >> &suitable_cells)
Separately manage mortality infected cohorts.
Definition: treatments.hpp:421
pops::PesticideTreatment::should_start
bool should_start(unsigned step) override
Definition: treatments.hpp:247
utils.hpp
pops::AbstractTreatment::get_start
virtual unsigned get_start()=0
pops::SimpleTreatment::end_treatment
void end_treatment(IntegerRaster &, IntegerRaster &, const std::vector< std::vector< int >> &) override
Definition: treatments.hpp:221
pops::Treatments::Treatments
Treatments(const Scheduler &scheduler)
Definition: treatments.hpp:336
pops::BaseTreatment::apply_treatment_mortality
void apply_treatment_mortality(IntegerRaster &infected, const std::vector< std::vector< int >> &suitable_cells) override
Definition: treatments.hpp:134
pops::Treatments::~Treatments
~Treatments()
Definition: treatments.hpp:337
indices
Definition: utils.hpp:97
pops::BaseTreatment::BaseTreatment
BaseTreatment(const FloatRaster &map, unsigned start, TreatmentApplication treatment_application)
Definition: treatments.hpp:117
pops::SimpleTreatment::should_start
bool should_start(unsigned step) override
Definition: treatments.hpp:166
pops::PesticideTreatment::PesticideTreatment
PesticideTreatment(const FloatRaster &map, unsigned start, unsigned end, TreatmentApplication treatment_application)
Definition: treatments.hpp:238
pops::SimpleTreatment
Simple treatment class.
Definition: treatments.hpp:157
pops::Treatments
Treatments class manages all treatments.
Definition: treatments.hpp:329
pops::Treatments::add_treatment
void add_treatment(const FloatRaster &map, const Date &start_date, int num_days, TreatmentApplication treatment_application)
Add treatment, based on parameters it is distinguished which treatment it will be.
Definition: treatments.hpp:357
date.hpp
pops::TreatmentApplication::AllInfectedInCell
@ AllInfectedInCell
All infected individuals are removed, rest by ratio.
pops::TreatmentApplication
TreatmentApplication
The enum to decide how treatment is applied.
Definition: treatments.hpp:36
pops::AbstractTreatment::get_end
virtual unsigned get_end()=0
pops::BaseTreatment::start_step_
unsigned start_step_
Definition: treatments.hpp:111
pops::Scheduler
Definition: scheduling.hpp:94
pops::BaseTreatment
Base treatment class.
Definition: treatments.hpp:108
pops::SimpleTreatment::SimpleTreatment
SimpleTreatment(const FloatRaster &map, unsigned start, TreatmentApplication treatment_application)
Definition: treatments.hpp:160
pops::BaseTreatment::get_end
unsigned get_end() override
Definition: treatments.hpp:130
UNUSED
#define UNUSED(expr)
Macro to mark unused variables (including parameters) and silence the warning while documenting that ...
Definition: utils.hpp:33
pops::AbstractTreatment
Abstract interface for treatment classes.
Definition: treatments.hpp:79
pops::BaseTreatment::application_
TreatmentApplication application_
Definition: treatments.hpp:114
pops
Definition: cauchy_kernel.hpp:25
pops::Date::add_days
void add_days(unsigned n)
Adds N days to a date.
Definition: date.hpp:413
pops::TreatmentApplication::Ratio
@ Ratio
A ratio is applied to all treated rasters.
pops::AbstractTreatment::apply_treatment
virtual void apply_treatment(IntegerRaster &infected, std::vector< IntegerRaster > &exposed, IntegerRaster &susceptible, IntegerRaster &resistant, IntegerRaster &total_hosts, const std::vector< std::vector< int >> &spatial_indeices)=0
pops::BaseTreatment::map_
FloatRaster map_
Definition: treatments.hpp:113
pops::SimpleTreatment::should_end
bool should_end(unsigned) override
Definition: treatments.hpp:172
pops::treatment_app_enum_from_string
TreatmentApplication treatment_app_enum_from_string(const std::string &text)
Get treatment application enum from string.
Definition: treatments.hpp:48
pops::Scheduler::schedule_action_date
unsigned schedule_action_date(const Date &date) const
Schedule action at a specific date (not repeated action).
Definition: scheduling.hpp:301
pops::BaseTreatment::get_start
unsigned get_start() override
Definition: treatments.hpp:126
pops::AbstractTreatment::should_end
virtual bool should_end(unsigned step)=0
pops::PesticideTreatment
Pesticide treatment class.
Definition: treatments.hpp:235
pops::Treatments::manage
bool manage(unsigned current, IntegerRaster &infected, std::vector< IntegerRaster > &exposed, IntegerRaster &susceptible, IntegerRaster &resistant, IntegerRaster &total_hosts, const std::vector< std::vector< int >> &suitable_cells)
Do management if needed.
Definition: treatments.hpp:387
pops::SimpleTreatment::apply_treatment
void apply_treatment(IntegerRaster &infected, std::vector< IntegerRaster > &exposed, IntegerRaster &susceptible, IntegerRaster &resistant, IntegerRaster &total_hosts, const std::vector< std::vector< int >> &suitable_cells) override
Definition: treatments.hpp:176
pops::PesticideTreatment::apply_treatment
void apply_treatment(IntegerRaster &infected, std::vector< IntegerRaster > &exposed_vector, IntegerRaster &susceptible, IntegerRaster &resistant, IntegerRaster &total_hosts, const std::vector< std::vector< int >> &suitable_cells) override
Definition: treatments.hpp:260
scheduling.hpp
pops::Treatments::clear_after_step
void clear_after_step(unsigned step)
Used to remove treatments after certain step.
Definition: treatments.hpp:439
pops::AbstractTreatment::end_treatment
virtual void end_treatment(IntegerRaster &susceptible, IntegerRaster &resistant, const std::vector< std::vector< int >> &spatial_indeices)=0
pops::AbstractTreatment::~AbstractTreatment
virtual ~AbstractTreatment()
Definition: treatments.hpp:100
pops::BaseTreatment::end_step_
unsigned end_step_
Definition: treatments.hpp:112
pops::PesticideTreatment::end_treatment
void end_treatment(IntegerRaster &susceptible, IntegerRaster &resistant, const std::vector< std::vector< int >> &suitable_cells) override
Definition: treatments.hpp:300
pops::AbstractTreatment::apply_treatment_mortality
virtual void apply_treatment_mortality(IntegerRaster &infected, const std::vector< std::vector< int >> &spatial_indeices)=0