SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AGActivityGen.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Main class that handles City, Activities and Trips
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 // activitygen module
14 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <iostream>
36 #include <utils/xml/XMLSubSys.h>
39 #include <sstream>
40 #include "AGActivityGen.h"
41 #include "AGActivityGenHandler.h"
42 #include "city/AGPosition.h"
44 #include "AGActivityTripWriter.h"
45 #include "city/AGTime.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 void
57  AGActivityGenHandler handler(city, net);
58  PROGRESS_BEGIN_MESSAGE("Reading input");
59  if (!XMLSubSys::runParser(handler, inputFile)) {
61  throw ProcessError();
62  } else {
64  }
65 
66  PROGRESS_BEGIN_MESSAGE("Consolidating statistics");
67  city.statData.consolidateStat(); //some maps are still not
69 
70  PROGRESS_BEGIN_MESSAGE("Building street representation");
73 
74  PROGRESS_BEGIN_MESSAGE("Generating work positions");
77 
78  PROGRESS_BEGIN_MESSAGE("Building bus lines");
81 
82 
83  PROGRESS_BEGIN_MESSAGE("Generating population");
86 
87  PROGRESS_BEGIN_MESSAGE("Allocating schools");
90 
91  PROGRESS_BEGIN_MESSAGE("Allocating work places");
94 
95  PROGRESS_BEGIN_MESSAGE("Allocating car places");
98 }
99 
100 bool
102  if (trip.getDay() > durationInDays + 1) {
103  return false;
104  }
105  if (trip.getDay() == 1) { //first day
106  if (trip.getTime() < beginTime) {
107  return false;
108  }
109  if (durationInDays == 0 && trip.getTime() > endTime) {
110  return false;
111  }
112  }
113  if (trip.getDay() == durationInDays + 1) { //last day
114  if (trip.getTime() > endTime) {
115  return false;
116  }
117  if (durationInDays == 0 && trip.getTime() < beginTime) {
118  return false;
119  }
120  }
121  return true;
122 }
123 
124 void
126  if (trip.getType() != "default") {
127  return;
128  }
129  //buses are on time and random are already spread
131  AGTime depTime(trip.getDay(), 0, 0, trip.getTime());
132  depTime += variation;
133  if (depTime.getDay() > 0) {
134  trip.setDay(depTime.getDay());
135  trip.setDepTime(depTime.getSecondsInCurrentDay());
136  } else {
137  trip.setDay(1);
138  trip.setDepTime(0);
139  }
140 }
141 
142 void
143 AGActivityGen::generateOutputFile(std::list<AGTrip>& trips) {
145  std::list<AGTrip>::iterator it;
146  //variables for TESTS:
147  int firstTrip = trips.front().getTime() + trips.front().getDay() * 86400;
148  int lastTrip = trips.front().getTime() + trips.front().getDay() * 86400;
149  std::map<int, int> histogram;
150  for (int i = 0; i < 100; ++i) {
151  histogram[i] = 0;
152  }
153  //END var TESTS
154  for (it = trips.begin(); it != trips.end(); ++it) {
155  atw.addTrip(*it);
156  //TEST
157  if (it->getTime() + 86400 * it->getDay() > lastTrip) {
158  lastTrip = it->getTime() + 86400 * it->getDay();
159  }
160  if (it->getTime() + 86400 * it->getDay() < firstTrip) {
161  firstTrip = it->getTime() + 86400 * it->getDay();
162  }
163  //++histogram[((it->getDay()-1)*86400 + it->getTime())/3600];
164  ++histogram[(it->getTime()) / 3600];
165  //END TEST
166  }
167  //PRINT TEST
168  AGTime first(firstTrip);
169  AGTime last(lastTrip);
170  std::cout << "first real trip: " << first.getDay() << ", " << first.getHour() << ":" << first.getMinute() << ":" << first.getSecond() << std::endl;
171  std::cout << "last real trip: " << last.getDay() << ", " << last.getHour() << ":" << last.getMinute() << ":" << last.getSecond() << std::endl;
172  for (int i = 0; i < 100; ++i) {
173  if (histogram[i] > 0) {
174  std::cout << "histogram[ hour " << i << " ] = " << histogram[i] << std::endl;
175  }
176  }
177  //END TEST
178  atw.writeOutputFile();
179 }
180 
181 void
182 AGActivityGen::makeActivityTrips(int days, int beginSec, int endSec) {
183  durationInDays = days;
184  beginTime = beginSec;
185  endTime = endSec;
189  AGActivities acts(&city, durationInDays + 1);
190  acts.generateActivityTrips();
191 
195  //list<Trip>* trips = &(acts.trips);
196  std::list<AGTrip> expTrips;
197  std::map<std::string, int> carUsed;
198  std::list<AGTrip>::iterator it;
199  //multiplication of days
200  for (it = acts.trips.begin(); it != acts.trips.end(); ++it) {
201  if (it->isDaily()) {
202  for (int currday = 1; currday < durationInDays + 2; ++currday) {
203  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), currday);
204  tr.setType(it->getType());
205  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
206  ++carUsed.find(tr.getVehicleName())->second;
207  } else {
208  carUsed[tr.getVehicleName()] = 1;
209  }
210  std::ostringstream os;
211  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
212  tr.setVehicleName(os.str());
213  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
214  varDepTime(tr); //slight variation on each "default" car
215  if (timeTripValidation(tr)) {
216  expTrips.push_back(tr);
217  }
218  //else
219  // std::cout << "trop tard 1 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
220  }
221  } else {
222  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), it->getDay());
223  tr.setType(it->getType());
224  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
225  ++carUsed.find(tr.getVehicleName())->second;
226  } else {
227  carUsed[tr.getVehicleName()] = 1;
228  }
229  std::ostringstream os;
230  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
231  tr.setVehicleName(os.str());
232  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
233  varDepTime(tr); //slight variation on each "default" car
234  if (timeTripValidation(tr)) {
235  expTrips.push_back(tr);
236  }
237  //else
238  // std::cout << "trop tard 2 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
239  }
240  }
241 
242  std::cout << "total trips generated: " << acts.trips.size() << std::endl;
243  std::cout << "total trips finally taken: " << expTrips.size() << std::endl;
244 
248  expTrips.sort(); //natural order of trips
249  std::cout << "...sorted by departure time.\n" << std::endl;
250 
254  generateOutputFile(expTrips);
255 }
256 
257 /****************************************************************************/