I want to set up params.arrival_rate every 3 hours of sim_time. This comprises a day but I would like to extend to N days. So, each time slot of 3 hours (in the code below is in minutes) of this N day, params.arrival_rate takes a value. Any optimal function to do so?
My actual code looks like this:
if(sim_time >= 0 && sim_time <= 180) //00:00 - 03:00
{
params.arrival_rate = 10;
}
else if(sim_time > 180 && sim_time <= 360) //03:00-06:00
{
params.arrival_rate = 9;
}
else if(sim_time > 360 && sim_time <= 540) //06:00-09:00
{
params.arrival_rate = 10.5;
}
else if(sim_time > 540 && sim_time <= 720) //09:00-12:00
{
params.arrival_rate = 12;
}
else if(sim_time > 720 && sim_time <= 900) //12:00-15:00
{
params.arrival_rate = 11.5;
}
else if(sim_time > 900 && sim_time <= 1080) //15:00-18:00
{
params.arrival_rate = 11;
}
else if(sim_time > 1080 && sim_time <= 1260) //18:00-21:00
{
params.arrival_rate = 10.5;
}
else if(sim_time > 1260 && sim_time <= 1440) //21:00-24:00
{
params.arrival_rate = 9;
}
else //by default
{
params.arrival_rate = 9;
}
Many thanks.
CodePudding user response:
Assuming you want the same pattern every day, it suffices to take the time modulo 1440 minutes:
int day_time = sim_time % 1440;
int day_no = sim_time / 1440;
Now you can use day_time
to get the time in the current day and use that to calculate params.arrival_rate
. day_no
will contain the day number (starting from 0).
As a further enhancement, consider using an array for your arrival rates instead of a long if-elseif chain:
const double arrival_rates[] = { 10, 9, 10.5, 12, 11.5, 11, 10.5, 9 };
params.arrival_rate = arrival_rates[day_time / (3 * 60)];
The calculation inside the brackets will round down the day_time to the start of the nearest three-hour block.