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