pops-core
0.9
PoPS (Pest or Pathogen Spread) Model Core C++ library
|
Go to the documentation of this file.
17 #ifndef POPS_SCHEDULING_HPP
18 #define POPS_SCHEDULING_HPP
51 inline friend std::ostream&
operator<<(std::ostream& os,
const Step& step);
59 os << step.start_date_ <<
" - " << step.end_date_;
81 std::map<std::string, StepUnit> mapping{
84 return mapping.at(text);
86 catch (
const std::out_of_range&) {
87 throw std::invalid_argument(
88 "step_unit_enum_from_string:"
90 + text +
"' provided");
115 unsigned simulation_num_units)
118 simulation_unit_(simulation_unit),
119 simulation_num_units_(simulation_num_units)
122 throw std::invalid_argument(
"Start date must be before end date");
123 if (simulation_num_units <= 0)
124 throw std::invalid_argument(
125 "Number of simulation units must be higher than zero");
129 throw std::invalid_argument(
130 "There must be at least one step between start and end date");
132 throw std::invalid_argument(
133 "If step unit is month, start date must start the first day of a "
138 while (date <= end_) {
143 steps.push_back(
Step(start, end));
162 return steps.at(index);
170 return std::make_tuple(simulation_num_units_, simulation_unit_);
180 std::vector<bool> schedule;
181 schedule.reserve(num_steps);
182 for (
Step step : steps) {
185 schedule.push_back(
true);
187 schedule.push_back(
false);
205 std::vector<bool> schedule;
206 schedule.reserve(num_steps);
207 for (
Step step : steps) {
208 Date st = step.start_date();
209 Date end = step.end_date();
211 if ((test >= st && test <= end))
212 schedule.push_back(
true);
214 schedule.push_back(
false);
226 std::vector<bool> schedule;
227 schedule.reserve(num_steps);
228 for (
Step step : steps) {
229 if (step.end_date().is_last_day_of_year())
230 schedule.push_back(
true);
232 schedule.push_back(
false);
244 std::vector<bool> schedule(num_steps,
false);
246 schedule[num_steps - 1] =
true;
260 std::vector<bool> schedule;
261 schedule.reserve(num_steps);
262 for (
unsigned i = 0; i < num_steps; i++) {
263 if ((i + 1) % n_steps == 0)
264 schedule.push_back(
true);
266 schedule.push_back(
false);
280 std::vector<bool> schedule;
281 schedule.reserve(num_steps);
282 for (
Step step : steps) {
283 Date st = step.start_date();
284 Date end = step.end_date();
286 schedule.push_back(
true);
288 schedule.push_back(
false);
303 for (
unsigned i = 0; i < num_steps; i++) {
304 if (date >= steps[i].start_date() && date <= steps[i].end_date())
307 throw std::invalid_argument(
"Date is outside of schedule");
315 for (
unsigned i = 0; i < num_steps; i++)
316 std::cout << steps[i] <<
": " << (schedule.at(i) ?
"true" :
"false")
321 for (
unsigned i = 0; i < num_steps; i++)
322 std::cout << steps[i] <<
": " << (n == i ?
"true" :
"false") << std::endl;
326 for (
unsigned i = 0; i < num_steps; i++)
327 std::cout << steps[i] << std::endl;
334 unsigned simulation_num_units_;
335 std::vector<Step> steps;
342 void increase_date(
Date& date)
348 for (
unsigned i = 0; i < simulation_num_units_; i++) {
353 for (
unsigned i = 0; i < simulation_num_units_; i++) {
375 std::vector<unsigned>
indices(action_schedule.size());
377 for (
unsigned i = 0; i < action_schedule.size(); i++) {
379 if (action_schedule[i])
392 return std::count(action_schedule.begin(), action_schedule.end(),
true);
407 const Scheduler& scheduler,
const std::string& frequency,
unsigned n = 0)
411 std::invalid_argument exception(
412 "Output frequency and simulation step are incompatible");
414 if (!frequency.empty()) {
415 if (frequency ==
"final_step")
417 else if (frequency ==
"year" || frequency ==
"yearly")
419 else if (frequency ==
"month" || frequency ==
"monthly")
421 else if (frequency ==
"week" || frequency ==
"weekly") {
438 else if (frequency ==
"day" || frequency ==
"daily") {
444 else if (frequency ==
"every_n_steps" && n > 0)
447 throw std::invalid_argument(
"Invalid value of output frequency");
457 #endif // POPS_SCHEDULING_HPP
Representation and manipulation of a date for the simulation.
std::vector< bool > schedule_action_end_of_simulation() const
Schedule an action at the end of simulation.
StepUnit step_unit_enum_from_string(const std::string &text)
Get step enum from string.
std::vector< bool > schedule_from_string(const Scheduler &scheduler, const std::string &frequency, unsigned n=0)
Get output (export) schedule based on frequency string ("year", "month", "week", "day",...
std::vector< bool > schedule_action_nsteps(unsigned n_steps) const
Schedule action every N simulation steps,.
friend std::ostream & operator<<(std::ostream &os, const Step &step)
std::vector< bool > schedule_spread(const Season &season) const
Schedule spread.
void debug_schedule(std::vector< bool > &schedule) const
Prints schedule for debugging purposes.
unsigned get_number_of_scheduled_actions(const std::vector< bool > &action_schedule)
Returns how many actions are scheduled.
void debug_schedule(unsigned n) const
Holds beginning and end of a season and decides what is in season.
void increased_by_month()
Step get_step(unsigned index) const
Get step based on index.
void increased_by_days(int num_days)
Increases the date by the num_days (specified by the user) except on the last timestep of the year,...
Scheduler(const Date &start, const Date &end, StepUnit simulation_unit, unsigned simulation_num_units)
Scheduler creates a vector of simulation steps based on start, end date, unit and number of units.
bool is_last_day_of_month()
void subtract_day()
Subtract 1 day from a date.
unsigned simulation_step_to_action_step(const std::vector< bool > &action_schedule, unsigned step)
Converts simulation step to step of actions.
unsigned schedule_action_date(const Date &date) const
Schedule action at a specific date (not repeated action).
Step(Date start_date, Date end_date)
std::vector< bool > schedule_action_monthly() const
Schedule action at the end of each month.
std::vector< bool > schedule_action_end_of_year() const
Schedule an action at the end of each year, e.g.
void increased_by_week()
Increases the date by one week (7 days) except on the last week of the year, which is increased by 8 ...
std::tuple< unsigned, StepUnit > get_step_length() const
Get length of simulation step as number of units and unit type.
std::vector< bool > schedule_action_yearly(int month, int day) const
Schedule an action at certain date each year, e.g.
StepUnit
Enum for step unit.
Representation and manipulation of a date for the simulation.
std::ostream & operator<<(std::ostream &os, const Date &d)
unsigned get_num_steps() const
Get number of simulation steps.
bool month_in_season(int month) const
Decides if a month is in season or not.
void debug_schedule() const