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-sim.org/
12 // Copyright (C) 2001-2013 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  if (trips.size() != 0) {
146  std::list<AGTrip>::iterator it;
147  //variables for TESTS:
148  int firstTrip = trips.front().getTime() + trips.front().getDay() * 86400;
149  int lastTrip = trips.front().getTime() + trips.front().getDay() * 86400;
150  std::map<int, int> histogram;
151  for (int i = 0; i < 100; ++i) {
152  histogram[i] = 0;
153  }
154  //END var TESTS
155  for (it = trips.begin(); it != trips.end(); ++it) {
156  atw.addTrip(*it);
157  //TEST
158  if (it->getTime() + 86400 * it->getDay() > lastTrip) {
159  lastTrip = it->getTime() + 86400 * it->getDay();
160  }
161  if (it->getTime() + 86400 * it->getDay() < firstTrip) {
162  firstTrip = it->getTime() + 86400 * it->getDay();
163  }
164  //++histogram[((it->getDay()-1)*86400 + it->getTime())/3600];
165  ++histogram[(it->getTime()) / 3600];
166  //END TEST
167  }
168  //PRINT TEST
169  AGTime first(firstTrip);
170  AGTime last(lastTrip);
171  std::cout << "first real trip: " << first.getDay() << ", " << first.getHour() << ":" << first.getMinute() << ":" << first.getSecond() << std::endl;
172  std::cout << "last real trip: " << last.getDay() << ", " << last.getHour() << ":" << last.getMinute() << ":" << last.getSecond() << std::endl;
173  for (int i = 0; i < 100; ++i) {
174  if (histogram[i] > 0) {
175  std::cout << "histogram[ hour " << i << " ] = " << histogram[i] << std::endl;
176  }
177  }
178  } else {
179  std::cout << "No real trips were generated" << std::endl;
180  }
181  //END TEST
182  atw.writeOutputFile();
183 }
184 
185 void
186 AGActivityGen::makeActivityTrips(int days, int beginSec, int endSec) {
187  durationInDays = days;
188  beginTime = beginSec;
189  endTime = endSec;
193  AGActivities acts(&city, durationInDays + 1);
194  acts.generateActivityTrips();
195 
199  //list<Trip>* trips = &(acts.trips);
200  std::list<AGTrip> expTrips;
201  std::map<std::string, int> carUsed;
202  std::list<AGTrip>::iterator it;
203  //multiplication of days
204  for (it = acts.trips.begin(); it != acts.trips.end(); ++it) {
205  if (it->isDaily()) {
206  for (int currday = 1; currday < durationInDays + 2; ++currday) {
207  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), currday);
208  tr.setType(it->getType());
209  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
210  ++carUsed.find(tr.getVehicleName())->second;
211  } else {
212  carUsed[tr.getVehicleName()] = 1;
213  }
214  std::ostringstream os;
215  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
216  tr.setVehicleName(os.str());
217  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
218  varDepTime(tr); //slight variation on each "default" car
219  if (timeTripValidation(tr)) {
220  expTrips.push_back(tr);
221  }
222  //else
223  // std::cout << "trop tard 1 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
224  }
225  } else {
226  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), it->getDay());
227  tr.setType(it->getType());
228  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
229  ++carUsed.find(tr.getVehicleName())->second;
230  } else {
231  carUsed[tr.getVehicleName()] = 1;
232  }
233  std::ostringstream os;
234  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
235  tr.setVehicleName(os.str());
236  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
237  varDepTime(tr); //slight variation on each "default" car
238  if (timeTripValidation(tr)) {
239  expTrips.push_back(tr);
240  }
241  //else
242  // std::cout << "trop tard 2 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
243  }
244  }
245 
246  std::cout << "total trips generated: " << acts.trips.size() << std::endl;
247  std::cout << "total trips finally taken: " << expTrips.size() << std::endl;
248 
252  expTrips.sort(); //natural order of trips
253  std::cout << "...sorted by departure time.\n" << std::endl;
254 
258  generateOutputFile(expTrips);
259 }
260 
261 /****************************************************************************/