SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AGFreeTime.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Generates trips related to after-work activities
11 // like visiting the family or party.
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2010-2014 DLR (http://www.dlr.de/) and contributors
15 // activitygen module
16 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
17 /****************************************************************************/
18 //
19 // This file is part of SUMO.
20 // SUMO is free software: you can redistribute it and/or modify
21 // it under the terms of the GNU General Public License as published by
22 // the Free Software Foundation, either version 3 of the License, or
23 // (at your option) any later version.
24 //
25 /****************************************************************************/
26 
27 
28 // ===========================================================================
29 // included modules
30 // ===========================================================================
31 #ifdef _MSC_VER
32 #include <windows_config.h>
33 #else
34 #include <config.h>
35 #endif
36 
37 #include <math.h>
39 #include <utils/common/StdDefs.h>
41 #include "AGFreeTime.h"
42 
43 
44 // ===========================================================================
45 // static member definitions
46 // ===========================================================================
47 const int AGFreeTime::DAY = 1;
48 const int AGFreeTime::EVENING = 2;
49 const int AGFreeTime::NIGHT = 4;
50 
51 const int AGFreeTime::TB_DAY = (new AGTime(0, 8, 0))->getTime();
52 const int AGFreeTime::TE_DAY = (new AGTime(0, 18, 0))->getTime();
53 const int AGFreeTime::TB_EVENING = (new AGTime(0, 19, 0))->getTime();
54 const int AGFreeTime::TE_EVENING = (new AGTime(0, 23, 59))->getTime();
55 const int AGFreeTime::TB_NIGHT = (new AGTime(0, 23, 0))->getTime();
56 const int AGFreeTime::TE_NIGHT = (new AGTime(1, 5, 0))->getTime();
57 
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
62 int
64  if (myHousehold->getAdults().front().decide(freqOut)) {
65  int num_poss = 0; //(possibleType % 2) + (possibleType / 4) + ((possibleType / 2) % 2);
66  if (possibleType & DAY) {
67  ++num_poss;
68  }
69  if (possibleType & EVENING) {
70  ++num_poss;
71  }
72  if (possibleType & NIGHT) {
73  ++num_poss;
74  }
75 
76  if (num_poss == 0) {
77  return 0;
78  }
79  SUMOReal alea = RandHelper::rand(); //(float)(rand() % 1000) / 1000.0;
80  int decision = (int)floor(alea * (SUMOReal)num_poss);
81 
82  if (possibleType & DAY) {
83  if (decision == 0) {
84  return DAY;
85  } else {
86  --decision;
87  }
88  }
89  if (possibleType & EVENING) {
90  if (decision == 0) {
91  return EVENING;
92  } else {
93  --decision;
94  }
95  }
96  if (possibleType & NIGHT) {
97  if (decision == 0) {
98  return NIGHT;
99  }
100  }
101  }
102  return 0;
103 }
104 
105 int
107  int val = 0;
108  if (myHousehold->getAdults().front().getAge() >= myStatData->limitAgeRetirement && tReady == 0) {
109  val += DAY + EVENING;
110  } else {
112  val += NIGHT;
113  }
114 
115  std::list<AGAdult>::const_iterator itA;
116  bool noBodyWorks = true;
117  for (itA = myHousehold->getAdults().begin(); itA != myHousehold->getAdults().end(); ++itA) {
118  if (itA->isWorking()) {
119  noBodyWorks = false;
120  }
121  }
122  if (noBodyWorks) {
123  val += DAY;
124  }
125 
126  if (tReady < (*(new AGTime(0, 22, 0))).getTime()) {
127  val += EVENING;
128  }
129  }
130  return val;
131 }
132 
133 bool
135  int backHome = whenBackHomeThisDay(day);
136  if (myHousehold->getCars().empty()) {
137  return true;
138  }
140  int depTime = randomTimeBetween(MAX2(backHome, TB_DAY), (TB_DAY + TE_DAY) / 2);
141  int arrTime = this->arrHour(myHousehold->getPosition(), destination, depTime);
142  int retTime = randomTimeBetween(arrTime, TE_DAY);
143  if (depTime < 0 || retTime < 0) {
144  return true; // not enough time during the day
145  }
146  AGTrip depTrip(myHousehold->getPosition(), destination, myHousehold->getCars().front().getName(), depTime, day);
147  AGTrip retTrip(destination, myHousehold->getPosition(), myHousehold->getCars().front().getName(), retTime, day);
148 
149  myPartialActivityTrips.push_back(depTrip);
150  myPartialActivityTrips.push_back(retTrip);
151  return true;
152 }
153 
154 bool
156  int backHome = whenBackHomeThisDay(day);
157  if (myHousehold->getCars().empty()) {
158  return true;
159  }
161  int depTime = randomTimeBetween(MAX2(backHome, TB_EVENING), TE_EVENING);
162  int arrTime = this->arrHour(myHousehold->getPosition(), destination, depTime);
163  int retTime = randomTimeBetween(arrTime, TE_EVENING);
164  if (depTime < 0 || retTime < 0) {
165  return true; // not enough time during the day
166  }
167  AGTrip depTrip(myHousehold->getPosition(), destination, myHousehold->getCars().front().getName(), depTime, day);
168  AGTrip retTrip(destination, myHousehold->getPosition(), myHousehold->getCars().front().getName(), retTime, day);
169 
170  myPartialActivityTrips.push_back(depTrip);
171  myPartialActivityTrips.push_back(retTrip);
172  return true;
173 }
174 
175 bool
177  int backHome = whenBackHomeThisDay(day);
178  int ActivitiesNextDay = whenBeginActivityNextDay(day); // is equal to 2 days if there is nothing the next day
179  int nextDay = 0;
180  if (myHousehold->getCars().empty()) {
181  return true;
182  }
184 
185  int depTime = randomTimeBetween(MAX2(backHome, TB_NIGHT), TE_NIGHT);
186  int arrTime = this->arrHour(myHousehold->getPosition(), destination, depTime);
187  //we have to go back home before the beginning of next day activities.
188  int lastRetTime = this->depHour(destination, myHousehold->getPosition(), MIN2(TE_NIGHT, ActivitiesNextDay));
189  int retTime = randomTimeBetween(arrTime, lastRetTime);
190  if (depTime < 0 || retTime < 0) {
191  return true; // not enough time during the day
192  }
193 
194  AGTime departureTime(depTime);
195  nextDay = departureTime.getDay();
196  departureTime.setDay(0);
197  AGTrip depTrip(myHousehold->getPosition(), destination, myHousehold->getCars().front().getName(), departureTime.getTime(), day + nextDay);
198 
199  AGTime returnTime(depTime);
200  nextDay = returnTime.getDay();
201  returnTime.setDay(0);
202  AGTrip retTrip(destination, myHousehold->getPosition(), myHousehold->getCars().front().getName(), returnTime.getTime(), day + nextDay);
203 
204  myPartialActivityTrips.push_back(depTrip);
205  myPartialActivityTrips.push_back(retTrip);
206  return true;
207 }
208 
209 bool
211  tReady = whenBackHome();
213  int type;
214 
215  for (int day = 1; day <= nbrDays; ++day) {
216  type = decideTypeOfTrip();
217  if (type == 0) {
218  continue;
219  } else if (type == DAY) {
220  if (!typeFromHomeDay(day)) {
221  return false;
222  }
223  } else if (type == EVENING) {
224  if (!typeFromHomeEvening(day)) {
225  return false;
226  }
227  } else if (type == NIGHT) {
228  if (!typeFromHomeNight(day)) {
229  return false;
230  }
231  }
232  }
233  genDone = true;
234  return genDone;
235 }
236 
237 int
239  int timeBack = 0;
240  for (std::list<AGTrip>::iterator itT = myPreviousTrips->begin(); itT != myPreviousTrips->end(); ++itT) {
241  if (timeBack < itT->getArrTime(this->timePerKm) && itT->isDaily()) {
242  timeBack = itT->getArrTime(this->timePerKm);
243  }
244  }
245  return timeBack;
246 }
247 
248 int
250  int timeBack = 0;
251  for (std::list<AGTrip>::iterator itT = myPreviousTrips->begin(); itT != myPreviousTrips->end(); ++itT) {
252  if (timeBack < itT->getArrTime(this->timePerKm) && (itT->getDay() == day || itT->isDaily())) {
253  timeBack = itT->getArrTime(this->timePerKm);
254  }
255  }
256  return timeBack;
257 }
258 
259 int
261  AGTime timeBack(1, 0, 0);
262  for (std::list<AGTrip>::iterator itT = myPreviousTrips->begin(); itT != myPreviousTrips->end(); ++itT) {
263  if (timeBack.getTime() > itT->getTime() && (itT->getDay() == (day + 1) || itT->isDaily())) {
264  timeBack.setTime(itT->getTime());
265  }
266  }
267  timeBack.addDays(1); // this the beginning of activities of the next day
268  return timeBack.getTime();
269 }
270 
271 /****************************************************************************/
int depHour(AGPosition from, AGPosition to, int arrival)
Definition: AGActivity.cpp:115
bool typeFromHomeDay(int day)
Definition: AGFreeTime.cpp:134
int whenBackHome()
Definition: AGFreeTime.cpp:238
int decideTypeOfTrip()
Definition: AGFreeTime.cpp:63
const std::list< AGCar > & getCars() const
void setDay(int d)
Definition: AGTime.cpp:136
Definition: AGTime.h:44
A location in the 2D plane freely positioned on a street.
Definition: AGPosition.h:63
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
bool typeFromHomeEvening(int day)
Definition: AGFreeTime.cpp:155
T MAX2(T a, T b)
Definition: StdDefs.h:72
AGDataAndStatistics * myStatData
Definition: AGActivity.h:112
void addDays(int days)
addition of days to the current moment
Definition: AGTime.cpp:173
static const int TE_DAY
Definition: AGFreeTime.h:122
const AGStreet & getRandomStreet()
Definition: AGCity.cpp:419
int whenBeginActivityNextDay(int day)
Definition: AGFreeTime.cpp:260
static const int TB_DAY
Definition: AGFreeTime.h:121
std::list< AGTrip > * myPreviousTrips
Definition: AGActivity.h:114
static const int TB_EVENING
Definition: AGFreeTime.h:123
SUMOReal freqOut
Definition: AGFreeTime.h:101
int randomTimeBetween(int begin, int end)
Definition: AGActivity.cpp:127
T MIN2(T a, T b)
Definition: StdDefs.h:66
AGHousehold * myHousehold
Definition: AGActivity.h:110
static const int DAY
Definition: AGFreeTime.h:117
const std::list< AGAdult > & getAdults() const
int whenBackHomeThisDay(int day)
Definition: AGFreeTime.cpp:249
AGPosition getPosition()
int possibleTypeOfTrip()
Definition: AGFreeTime.cpp:106
unsigned int getPeopleNbr()
Definition: AGHousehold.cpp:94
int arrHour(AGPosition from, AGPosition to, int departure)
Definition: AGActivity.cpp:122
static const int NIGHT
Definition: AGFreeTime.h:119
std::list< AGTrip > myPartialActivityTrips
Definition: AGActivity.h:115
bool typeFromHomeNight(int day)
Definition: AGFreeTime.cpp:176
AGCity * getTheCity()
unsigned int getAdultNbr()
Definition: AGHousehold.cpp:99
static const int TE_EVENING
Definition: AGFreeTime.h:124
int getTime()
: returns the number of seconds from the beginning of the first day of simulation this includes ...
Definition: AGTime.cpp:131
#define SUMOReal
Definition: config.h:215
static const int TE_NIGHT
Definition: AGFreeTime.h:126
SUMOReal timePerKm
Definition: AGActivity.h:118
static const int EVENING
Definition: AGFreeTime.h:118
void setTime(int sec)
: sets the time from the beginning of the first day of simulation in seconds
Definition: AGTime.cpp:168
Definition: AGTrip.h:48
int getDay()
Definition: AGTime.cpp:106
static const int TB_NIGHT
Definition: AGFreeTime.h:125
bool genDone
Definition: AGActivity.h:117
int possibleType
Definition: AGFreeTime.h:115
bool generateTrips()
Definition: AGFreeTime.cpp:210