SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AGActivities.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Main class that manages activities taken in account and generates the
10 // inhabitants' trip list.
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2013 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 "AGActivities.h"
37 #include "AGWorkAndSchool.h"
38 #include "AGFreeTime.h"
39 #include "../city/AGTime.h"
40 #include <sstream>
42 
43 #define REBUILD_ITERATION_LIMIT 2
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 void
50 AGActivities::addTrip(AGTrip t, std::list<AGTrip>* tripSet) {
51  tripSet->push_back(t);
52 }
53 
54 void
55 AGActivities::addTrips(std::list<AGTrip> t, std::list<AGTrip>* tripSet) {
56  std::list<AGTrip>::iterator it;
57  for (it = t.begin(); it != t.end(); ++it) {
58  tripSet->push_back(*it);
59  }
60 }
61 
62 void
64  int numbErr;
68  numbErr = 0;
69  std::list<AGBusLine>::iterator itBL;
70  for (itBL = myCity->busLines.begin(); itBL != myCity->busLines.end(); ++itBL) {
71  if (! generateBusTraffic(*itBL)) {
72  ++numbErr;
73  }
74  }
75  if (numbErr != 0) {
76  std::cerr << "ERROR: " << numbErr << " bus lines couldn't been completely generated ( " << (float)numbErr * 100.0 / (float)myCity->busLines.size() << "% )..." << std::endl;
77  } else {
78  std::cout << "no problem during bus line trip generation..." << std::endl;
79  }
80 
81  std::cout << "after public transportation: " << trips.size() << std::endl;
86  numbErr = 0;
87  std::list<AGHousehold>::iterator itHH;
88  for (itHH = myCity->households.begin(); itHH != myCity->households.end(); ++itHH) {
89  if (! generateTrips(*itHH)) {
90  ++numbErr;
91  }
92  }
93  if (numbErr != 0) {
94  std::cout << "WARNING: " << numbErr << " ( " << (float)numbErr * 100.0 / (float)myCity->households.size() << "% ) households' trips haven't been generated: would probably need more iterations for rebuilding..." << std::endl;
95  } else {
96  std::cout << "no problem during households' trips generation..." << std::endl;
97  }
98 
99  std::cout << "after household activities: " << trips.size() << std::endl;
104  if (! generateInOutTraffic()) {
105  std::cerr << "ERROR while generating in/Out traffic..." << std::endl;
106  } else {
107  std::cout << "no problem during in/out traffic generation..." << std::endl;
108  }
109 
110  std::cout << "after incoming/outgoing traffic: " << trips.size() << std::endl;
115  if (! generateRandomTraffic()) {
116  std::cerr << "ERROR while generating random traffic..." << std::endl;
117  } else {
118  std::cout << "no problem during random traffic generation..." << std::endl;
119  }
120 
121  std::cout << "after random traffic: " << trips.size() << std::endl;
122 }
123 
124 bool
126  int iteration = 0;
127  bool generated = false;
128  std::list<AGTrip> temporaTrips;
129  while (!generated && iteration < REBUILD_ITERATION_LIMIT) {
130  if (!temporaTrips.empty()) {
131  temporaTrips.clear();
132  }
133  // Work and school activities
134  AGWorkAndSchool ws(&hh, &(myCity->statData), &temporaTrips);
135  generated = ws.generateTrips();
136  if (!generated) {
137  hh.regenerate();
138  ++iteration;
139  continue;
140  }
141  addTrips(ws.getPartialActivityTrips(), &temporaTrips);
142 
143  // free time activities
144  AGFreeTime ft(&hh, &(myCity->statData), &temporaTrips, nbrDays);
145  generated = ft.generateTrips();
146  if (!generated) {
147  hh.regenerate();
148  ++iteration;
149  continue;
150  }
151  addTrips(ft.getPartialActivityTrips(), &temporaTrips);
152  //cout << "after this hh: " << temporaTrips.size() << " we have: " << trips.size() << endl;
153  //trips of all activities generated:
154  addTrips(temporaTrips, &trips);
155  }
156  return generated;
157 }
158 
159 bool
161  std::list<AGBus>::iterator itB;
162  std::list<AGPosition>::iterator itS;
166  for (itB = bl.buses.begin(); itB != bl.buses.end(); ++itB) {
167  if (bl.stations.size() < 1) {
168  return false;
169  }
170  AGTrip t(bl.stations.front(), bl.stations.back(), *itB, itB->getDeparture());
171  for (itS = bl.stations.begin(); itS != bl.stations.end(); ++itS) {
172  if (*itS == t.getDep() || *itS == t.getArr()) {
173  continue;
174  }
175  t.addLayOver(*itS);
176  }
177  trips.push_back(t);
178  }
182  //verify that buses return back to the beginning
183  if (bl.revStations.empty()) {
184  return true; //in this case, no return way: everything is ok.
185  }
186  for (itB = bl.revBuses.begin(); itB != bl.revBuses.end(); ++itB) {
187  if (bl.revStations.size() < 1) {
188  return false;
189  }
190  AGTrip t(bl.revStations.front(), bl.revStations.back(), *itB, itB->getDeparture());
191  for (itS = bl.revStations.begin(); itS != bl.revStations.end(); ++itS) {
192  if (*itS == t.getDep() || *itS == t.getArr()) {
193  continue;
194  }
195  t.addLayOver(*itS);
196  }
197  trips.push_back(t);
198  }
199  return true;
200 }
201 
202 bool
209  if (myCity->peopleIncoming.empty()) {
210  return true;
211  }
212  if (myCity->cityGates.empty()) {
213  return false;
214  }
215  int num = 1;
216  std::list<AGAdult>::iterator itA;
217 
218  for (itA = myCity->peopleIncoming.begin(); itA != myCity->peopleIncoming.end(); ++itA) {
220  std::string nom(generateName(num, "carIn"));
221  AGTrip wayTrip(myCity->cityGates[posi], itA->getWorkPosition().getPosition(), nom, itA->getWorkPosition().getOpening());
222  //now we put the estimated time of entrance in the city.
223  wayTrip.setDepTime(wayTrip.estimateDepTime(wayTrip.getTime(), myCity->statData.speedTimePerKm));
224  AGTrip retTrip(itA->getWorkPosition().getPosition(), myCity->cityGates[posi], nom, itA->getWorkPosition().getClosing());
225  trips.push_back(wayTrip);
226  trips.push_back(retTrip);
227  ++num;
228  }
229  return true;
230 }
231 
232 std::string
233 AGActivities::generateName(int i, std::string prefix) {
234  std::ostringstream os;
235  os << i;
236  return prefix + os.str();
237 }
238 
239 bool
241  //total number of trips during the whole simulation
242  int totalTrips = 0, ttOneDayTrips = 0, ttDailyTrips = 0;
243  std::list<AGTrip>::iterator it;
244  for (it = trips.begin(); it != trips.end(); ++it) {
245  if (it->isDaily()) {
246  ++ttDailyTrips;
247  } else {
248  ++ttOneDayTrips;
249  }
250  }
251  totalTrips = ttOneDayTrips + ttDailyTrips * nbrDays;
252  //TESTS
253  std::cout << "Before Random traffic generation (days are still entire):" << std::endl;
254  std::cout << "- Total number of trips: " << totalTrips << std::endl;
255  std::cout << "- Total daily trips: " << ttDailyTrips << std::endl;
256  std::cout << "- Total one-day trips: " << ttOneDayTrips << std::endl;
257  //END OF TESTS
258 
259  //random uniform distribution:
260  int nbrRandUni = (int)((float)totalTrips * myCity->statData.uniformRandomTrafficRate / (1.0f - myCity->statData.uniformRandomTrafficRate));
261  //TESTS
262  std::cout << "added uniform random trips: " << nbrRandUni << std::endl;
263  //END OF TESTS
264  for (int i = 0; i < nbrRandUni; ++i) {
267  AGTime depTime(RandHelper::rand(nbrDays * 86400));
268  AGTrip rdtr(dep, arr, generateName(i, "randUni"), depTime.getTime() % 86400, depTime.getDay() + 1);
269  rdtr.setType("random");
270  trips.push_back(rdtr);
271  }
272 
273  //random proportional distribution:
274  //float proportionalPercentage = 0.05f;
275  //TODO generate a proportionally distributed random traffic
276 
277  return true;
278 }
279 
280 /****************************************************************************/