Eclipse SUMO - Simulation of Urban MObility
AGFreeTime.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2010-2024 German Aerospace Center (DLR) and others.
4 // activitygen module
5 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
6 // This program and the accompanying materials are made available under the
7 // terms of the Eclipse Public License 2.0 which is available at
8 // https://www.eclipse.org/legal/epl-2.0/
9 // This Source Code may also be made available under the following Secondary
10 // Licenses when the conditions for such availability set forth in the Eclipse
11 // Public License 2.0 are satisfied: GNU General Public License, version 2
12 // or later which is available at
13 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
15 /****************************************************************************/
23 // Generates trips related to after-work activities
24 // like visiting the family or party.
25 /****************************************************************************/
26 #include <config.h>
27 
28 #include <cmath>
30 #include <utils/common/StdDefs.h>
33 #include "AGFreeTime.h"
34 
35 
36 // ===========================================================================
37 // static member definitions
38 // ===========================================================================
39 const int AGFreeTime::DAY = 1;
40 const int AGFreeTime::EVENING = 2;
41 const int AGFreeTime::NIGHT = 4;
42 
43 const int AGFreeTime::TB_DAY = AGTime(0, 8, 0).getTime();
44 const int AGFreeTime::TE_DAY = AGTime(0, 18, 0).getTime();
45 const int AGFreeTime::TB_EVENING = AGTime(0, 19, 0).getTime();
46 const int AGFreeTime::TE_EVENING = AGTime(0, 23, 59).getTime();
47 const int AGFreeTime::TB_NIGHT = AGTime(0, 23, 0).getTime();
48 const int AGFreeTime::TE_NIGHT = AGTime(1, 5, 0).getTime();
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 int
56  if (myHousehold->getAdults().front().decide(freqOut)) {
57  int num_poss = 0; //(possibleType % 2) + (possibleType / 4) + ((possibleType / 2) % 2);
58  if (possibleType & DAY) {
59  ++num_poss;
60  }
61  if (possibleType & EVENING) {
62  ++num_poss;
63  }
64  if (possibleType & NIGHT) {
65  ++num_poss;
66  }
67 
68  if (num_poss == 0) {
69  return 0;
70  }
71  double alea = RandHelper::rand(); //(float)(rand() % 1000) / 1000.0;
72  int decision = (int)floor(alea * (double)num_poss);
73 
74  if (possibleType & DAY) {
75  if (decision == 0) {
76  return DAY;
77  } else {
78  --decision;
79  }
80  }
81  if (possibleType & EVENING) {
82  if (decision == 0) {
83  return EVENING;
84  } else {
85  --decision;
86  }
87  }
88  if (possibleType & NIGHT) {
89  if (decision == 0) {
90  return NIGHT;
91  }
92  }
93  }
94  return 0;
95 }
96 
97 int
99  int val = 0;
100  if (myHousehold->getAdults().front().getAge() >= myStatData->limitAgeRetirement && tReady == 0) {
101  val += DAY + EVENING;
102  } else {
104  val += NIGHT;
105  }
106 
107  std::list<AGAdult>::const_iterator itA;
108  bool noBodyWorks = true;
109  for (itA = myHousehold->getAdults().begin(); itA != myHousehold->getAdults().end(); ++itA) {
110  if (itA->isWorking()) {
111  noBodyWorks = false;
112  }
113  }
114  if (noBodyWorks) {
115  val += DAY;
116  }
117 
118  if (tReady < AGTime(0, 22, 0).getTime()) {
119  val += EVENING;
120  }
121  }
122  return val;
123 }
124 
125 bool
127  int backHome = whenBackHomeThisDay(day);
128  if (myHousehold->getCars().empty()) {
129  return true;
130  }
132  int depTime = randomTimeBetween(MAX2(backHome, TB_DAY), (TB_DAY + TE_DAY) / 2);
133  int arrTime = this->arrHour(myHousehold->getPosition(), destination, depTime);
134  int retTime = randomTimeBetween(arrTime, TE_DAY);
135  if (depTime < 0 || retTime < 0) {
136  return true; // not enough time during the day
137  }
138  AGTrip depTrip(myHousehold->getPosition(), destination, myHousehold->getCars().front().getName(), depTime, day);
139  AGTrip retTrip(destination, myHousehold->getPosition(), myHousehold->getCars().front().getName(), retTime, day);
140 
141  myPartialActivityTrips.push_back(depTrip);
142  myPartialActivityTrips.push_back(retTrip);
143  return true;
144 }
145 
146 bool
148  int backHome = whenBackHomeThisDay(day);
149  if (myHousehold->getCars().empty()) {
150  return true;
151  }
153  int depTime = randomTimeBetween(MAX2(backHome, TB_EVENING), TE_EVENING);
154  int arrTime = this->arrHour(myHousehold->getPosition(), destination, depTime);
155  int retTime = randomTimeBetween(arrTime, TE_EVENING);
156  if (depTime < 0 || retTime < 0) {
157  return true; // not enough time during the day
158  }
159  AGTrip depTrip(myHousehold->getPosition(), destination, myHousehold->getCars().front().getName(), depTime, day);
160  AGTrip retTrip(destination, myHousehold->getPosition(), myHousehold->getCars().front().getName(), retTime, day);
161 
162  myPartialActivityTrips.push_back(depTrip);
163  myPartialActivityTrips.push_back(retTrip);
164  return true;
165 }
166 
167 bool
169  int backHome = whenBackHomeThisDay(day);
170  int ActivitiesNextDay = whenBeginActivityNextDay(day); // is equal to 2 days if there is nothing the next day
171  int nextDay = 0;
172  if (myHousehold->getCars().empty()) {
173  return true;
174  }
176 
177  int depTime = randomTimeBetween(MAX2(backHome, TB_NIGHT), TE_NIGHT);
178  int arrTime = this->arrHour(myHousehold->getPosition(), destination, depTime);
179  //we have to go back home before the beginning of next day activities.
180  int lastRetTime = this->depHour(destination, myHousehold->getPosition(), MIN2(TE_NIGHT, ActivitiesNextDay));
181  int retTime = randomTimeBetween(arrTime, lastRetTime);
182  if (depTime < 0 || retTime < 0) {
183  return true; // not enough time during the day
184  }
185 
186  AGTime departureTime(depTime);
187  nextDay = departureTime.getDay();
188  departureTime.setDay(0);
189  AGTrip depTrip(myHousehold->getPosition(), destination, myHousehold->getCars().front().getName(), departureTime.getTime(), day + nextDay);
190 
191  AGTime returnTime(depTime);
192  nextDay = returnTime.getDay();
193  returnTime.setDay(0);
194  AGTrip retTrip(destination, myHousehold->getPosition(), myHousehold->getCars().front().getName(), returnTime.getTime(), day + nextDay);
195 
196  myPartialActivityTrips.push_back(depTrip);
197  myPartialActivityTrips.push_back(retTrip);
198  return true;
199 }
200 
201 bool
203  tReady = whenBackHome();
205  int type;
206 
207  for (int day = 1; day <= nbrDays; ++day) {
208  type = decideTypeOfTrip();
209  if (type == 0) {
210  continue;
211  } else if (type == DAY) {
212  if (!typeFromHomeDay(day)) {
213  return false;
214  }
215  } else if (type == EVENING) {
216  if (!typeFromHomeEvening(day)) {
217  return false;
218  }
219  } else if (type == NIGHT) {
220  if (!typeFromHomeNight(day)) {
221  return false;
222  }
223  }
224  }
225  genDone = true;
226  return genDone;
227 }
228 
229 int
231  int timeBack = 0;
232  for (std::list<AGTrip>::iterator itT = myPreviousTrips->begin(); itT != myPreviousTrips->end(); ++itT) {
233  if (timeBack < itT->getArrTime(this->timePerKm) && itT->isDaily()) {
234  timeBack = itT->getArrTime(this->timePerKm);
235  }
236  }
237  return timeBack;
238 }
239 
240 int
242  int timeBack = 0;
243  for (std::list<AGTrip>::iterator itT = myPreviousTrips->begin(); itT != myPreviousTrips->end(); ++itT) {
244  if (timeBack < itT->getArrTime(this->timePerKm) && (itT->getDay() == day || itT->isDaily())) {
245  timeBack = itT->getArrTime(this->timePerKm);
246  }
247  }
248  return timeBack;
249 }
250 
251 int
253  AGTime timeBack(1, 0, 0);
254  for (std::list<AGTrip>::iterator itT = myPreviousTrips->begin(); itT != myPreviousTrips->end(); ++itT) {
255  if (timeBack.getTime() > itT->getTime() && (itT->getDay() == (day + 1) || itT->isDaily())) {
256  timeBack.setTime(itT->getTime());
257  }
258  }
259  timeBack.addDays(1); // this the beginning of activities of the next day
260  return timeBack.getTime();
261 }
262 
263 
264 /****************************************************************************/
T MIN2(T a, T b)
Definition: StdDefs.h:76
T MAX2(T a, T b)
Definition: StdDefs.h:82
std::list< AGTrip > * myPreviousTrips
Definition: AGActivity.h:112
bool genDone
Definition: AGActivity.h:115
AGHousehold * myHousehold
Definition: AGActivity.h:108
std::list< AGTrip > myPartialActivityTrips
Definition: AGActivity.h:113
AGDataAndStatistics * myStatData
Definition: AGActivity.h:110
int depHour(AGPosition from, AGPosition to, int arrival)
Definition: AGActivity.cpp:110
double timePerKm
Definition: AGActivity.h:116
int arrHour(AGPosition from, AGPosition to, int departure)
Definition: AGActivity.cpp:118
int randomTimeBetween(int begin, int end)
Definition: AGActivity.cpp:124
const AGStreet & getRandomStreet()
Definition: AGCity.cpp:413
static const int TB_NIGHT
Definition: AGFreeTime.h:115
int whenBackHome()
Definition: AGFreeTime.cpp:230
double freqOut
Definition: AGFreeTime.h:91
int decideTypeOfTrip()
Definition: AGFreeTime.cpp:55
static const int DAY
Definition: AGFreeTime.h:107
bool typeFromHomeDay(int day)
Definition: AGFreeTime.cpp:126
static const int TB_EVENING
Definition: AGFreeTime.h:113
bool typeFromHomeEvening(int day)
Definition: AGFreeTime.cpp:147
int nbrDays
Definition: AGFreeTime.h:96
static const int NIGHT
Definition: AGFreeTime.h:109
bool generateTrips()
Definition: AGFreeTime.cpp:202
static const int EVENING
Definition: AGFreeTime.h:108
int whenBackHomeThisDay(int day)
Definition: AGFreeTime.cpp:241
int possibleType
Definition: AGFreeTime.h:105
static const int TE_EVENING
Definition: AGFreeTime.h:114
int whenBeginActivityNextDay(int day)
Definition: AGFreeTime.cpp:252
static const int TE_NIGHT
Definition: AGFreeTime.h:116
static const int TE_DAY
Definition: AGFreeTime.h:112
int possibleTypeOfTrip()
Definition: AGFreeTime.cpp:98
bool typeFromHomeNight(int day)
Definition: AGFreeTime.cpp:168
static const int TB_DAY
Definition: AGFreeTime.h:111
AGPosition getPosition()
const std::list< AGCar > & getCars() const
AGCity * getTheCity()
int getPeopleNbr()
Definition: AGHousehold.cpp:88
int getAdultNbr()
Definition: AGHousehold.cpp:93
const std::list< AGAdult > & getAdults() const
Definition: AGHousehold.cpp:98
A location in the 2D plane freely positioned on a street.
Definition: AGPosition.h:53
Definition: AGTime.h:34
int getTime()
: returns the number of seconds from the beginning of the first day of simulation this includes
Definition: AGTime.cpp:122
void setDay(int d)
Definition: AGTime.cpp:127
void setTime(int sec)
: sets the time from the beginning of the first day of simulation in seconds
Definition: AGTime.cpp:159
void addDays(int days)
addition of days to the current moment
Definition: AGTime.cpp:164
int getDay()
Definition: AGTime.cpp:97
Definition: AGTrip.h:38
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.cpp:94