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