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.sourceforge.net/
13 // Copyright (C) 2001-2012 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 (hh->adults.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 (hh->adults.front().getAge() >= ds->limitAgeRetirement && tReady == 0) {
108  val += DAY + EVENING;
109  } else {
110  if (hh->getPeopleNbr() > hh->getAdultNbr()) {
111  val += NIGHT;
112  }
113 
114  std::list<AGAdult>::iterator itA;
115  bool noBodyWorks = true;
116  for (itA = hh->adults.begin() ; itA != hh->adults.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 (hh->cars.empty()) {
136  return true;
137  }
138  AGPosition destination(hh->getTheCity()->getRandomStreet());
139  int depTime = randomTimeBetween(MAX2(backHome, TB_DAY), (TB_DAY + TE_DAY) / 2);
140  int arrTime = this->arrHour(hh->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(hh->getPosition(), destination, hh->cars.front().getName(), depTime, day);
146  AGTrip retTrip(destination, hh->getPosition(), hh->cars.front().getName(), retTime, day);
147 
148  this->partialActivityTrips.push_back(depTrip);
149  this->partialActivityTrips.push_back(retTrip);
150  return true;
151 }
152 
153 bool
155  int backHome = whenBackHomeThisDay(day);
156  if (hh->cars.empty()) {
157  return true;
158  }
159  AGPosition destination(hh->getTheCity()->getRandomStreet());
160  int depTime = randomTimeBetween(MAX2(backHome, TB_EVENING), TE_EVENING);
161  int arrTime = this->arrHour(hh->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(hh->getPosition(), destination, hh->cars.front().getName(), depTime, day);
167  AGTrip retTrip(destination, hh->getPosition(), hh->cars.front().getName(), retTime, day);
168 
169  this->partialActivityTrips.push_back(depTrip);
170  this->partialActivityTrips.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 (hh->cars.empty()) {
180  return true;
181  }
182  AGPosition destination(hh->getTheCity()->getRandomStreet());
183 
184  int depTime = randomTimeBetween(MAX2(backHome, TB_NIGHT), TE_NIGHT);
185  int arrTime = this->arrHour(hh->getPosition(), destination, depTime);
186  //we have to go back home before the beginning of next day activities.
187  int lastRetTime = this->depHour(destination, hh->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(hh->getPosition(), destination, hh->cars.front().getName(), departureTime.getTime(), day + nextDay);
197 
198  AGTime returnTime(depTime);
199  nextDay = returnTime.getDay();
200  returnTime.setDay(0);
201  AGTrip retTrip(destination, hh->getPosition(), hh->cars.front().getName(), returnTime.getTime(), day + nextDay);
202 
203  this->partialActivityTrips.push_back(depTrip);
204  this->partialActivityTrips.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  if (!this->previousTrips->empty()) {
240  std::list<AGTrip>::iterator itT;
241  for (itT = previousTrips->begin() ; itT != previousTrips->end() ; ++itT) {
242  if (timeBack < itT->getArrTime(this->timePerKm) && itT->isDaily()) {
243  timeBack = itT->getArrTime(this->timePerKm);
244  }
245  }
246  }
247  return timeBack;
248 }
249 
250 int
252  int timeBack = 0;
253  if (!this->previousTrips->empty()) {
254  std::list<AGTrip>::iterator itT;
255  for (itT = previousTrips->begin() ; itT != previousTrips->end() ; ++itT) {
256  if (timeBack < itT->getArrTime(this->timePerKm) && (itT->getDay() == day || itT->isDaily())) {
257  timeBack = itT->getArrTime(this->timePerKm);
258  }
259  }
260  }
261  return timeBack;
262 }
263 
264 int
266  AGTime timeBack(1, 0, 0);
267  if (!this->previousTrips->empty()) {
268  std::list<AGTrip>::iterator itT;
269  for (itT = previousTrips->begin() ; itT != previousTrips->end() ; ++itT) {
270  if (timeBack.getTime() > itT->getTime() && (itT->getDay() == (day + 1) || itT->isDaily())) {
271  timeBack.setTime(itT->getTime());
272  }
273  }
274  }
275  timeBack.addDays(1); // this the beginning of activities of the next day
276  return timeBack.getTime();
277 }
278 
279 /****************************************************************************/