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  handler.setFileName(inputFile);
59  XMLSubSys::init(false);
61  PROGRESS_BEGIN_MESSAGE("Reading input");
62  if (!XMLSubSys::runParser(handler, inputFile)) {
64  throw ProcessError();
65  } else {
67  }
68 
69  PROGRESS_BEGIN_MESSAGE("Consolidating statistics");
70  city.statData.consolidateStat(); //some maps are still not
72 
73  PROGRESS_BEGIN_MESSAGE("Building street representation");
76 
77  PROGRESS_BEGIN_MESSAGE("Generating work positions");
80 
81  PROGRESS_BEGIN_MESSAGE("Building bus lines");
84 
85 
86  PROGRESS_BEGIN_MESSAGE("Generating population");
89 
90  PROGRESS_BEGIN_MESSAGE("Allocating schools");
93 
94  PROGRESS_BEGIN_MESSAGE("Allocating work places");
97 
98  PROGRESS_BEGIN_MESSAGE("Allocating car places");
101 }
102 
103 bool
105  if (trip.getDay() > durationInDays + 1) {
106  return false;
107  }
108  if (trip.getDay() == 1) { //first day
109  if (trip.getTime() < beginTime) {
110  return false;
111  }
112  if (durationInDays == 0 && trip.getTime() > endTime) {
113  return false;
114  }
115  }
116  if (trip.getDay() == durationInDays + 1) { //last day
117  if (trip.getTime() > endTime) {
118  return false;
119  }
120  if (durationInDays == 0 && trip.getTime() < beginTime) {
121  return false;
122  }
123  }
124  return true;
125 }
126 
127 void
129  if (trip.getType() != "default") {
130  return;
131  }
132  //buses are on time and random are already spread
134  AGTime depTime(trip.getDay(), 0, 0, trip.getTime());
135  depTime += variation;
136  if (depTime.getDay() > 0) {
137  trip.setDay(depTime.getDay());
138  trip.setDepTime(depTime.getSecondsInCurrentDay());
139  } else {
140  trip.setDay(1);
141  trip.setDepTime(0);
142  }
143 }
144 
145 void
146 AGActivityGen::generateOutputFile(std::list<AGTrip>& trips) {
148  std::list<AGTrip>::iterator it;
149  //variables for TESTS:
150  int firstTrip = trips.front().getTime() + trips.front().getDay() * 86400;
151  int lastTrip = trips.front().getTime() + trips.front().getDay() * 86400;
152  std::map<int, int> histogram;
153  for (int i = 0 ; i < 100 ; ++i) {
154  histogram[i] = 0;
155  }
156  //END var TESTS
157  for (it = trips.begin() ; it != trips.end() ; ++it) {
158  atw.addTrip(*it);
159  //TEST
160  if (it->getTime() + 86400 * it->getDay() > lastTrip) {
161  lastTrip = it->getTime() + 86400 * it->getDay();
162  }
163  if (it->getTime() + 86400 * it->getDay() < firstTrip) {
164  firstTrip = it->getTime() + 86400 * it->getDay();
165  }
166  //++histogram[((it->getDay()-1)*86400 + it->getTime())/3600];
167  ++histogram[(it->getTime()) / 3600];
168  //END TEST
169  }
170  //PRINT TEST
171  AGTime first(firstTrip);
172  AGTime last(lastTrip);
173  std::cout << "first real trip: " << first.getDay() << ", " << first.getHour() << ":" << first.getMinute() << ":" << first.getSecond() << std::endl;
174  std::cout << "last real trip: " << last.getDay() << ", " << last.getHour() << ":" << last.getMinute() << ":" << last.getSecond() << std::endl;
175  for (int i = 0 ; i < 100 ; ++i) {
176  if (histogram[i] > 0) {
177  std::cout << "histogram[ hour " << i << " ] = " << histogram[i] << std::endl;
178  }
179  }
180  //END TEST
181  atw.writeOutputFile();
182 }
183 
184 void
185 AGActivityGen::makeActivityTrips(int days, int beginSec, int endSec) {
186  durationInDays = days;
187  beginTime = beginSec;
188  endTime = endSec;
192  AGActivities acts(&city, durationInDays + 1);
193  acts.generateActivityTrips();
194 
198  //list<Trip>* trips = &(acts.trips);
199  std::list<AGTrip> expTrips;
200  std::map<std::string, int> carUsed;
201  std::list<AGTrip>::iterator it;
202  //multiplication of days
203  for (it = acts.trips.begin() ; it != acts.trips.end() ; ++it) {
204  if (it->isDaily()) {
205  for (int currday = 1 ; currday < durationInDays + 2 ; ++currday) {
206  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), currday);
207  tr.setType(it->getType());
208  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
209  ++carUsed.find(tr.getVehicleName())->second;
210  } else {
211  carUsed[tr.getVehicleName()] = 1;
212  }
213  std::ostringstream os;
214  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
215  tr.setVehicleName(os.str());
216  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
217  varDepTime(tr); //slight variation on each "default" car
218  if (timeTripValidation(tr)) {
219  expTrips.push_back(tr);
220  }
221  //else
222  // std::cout << "trop tard 1 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
223  }
224  } else {
225  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), it->getDay());
226  tr.setType(it->getType());
227  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
228  ++carUsed.find(tr.getVehicleName())->second;
229  } else {
230  carUsed[tr.getVehicleName()] = 1;
231  }
232  std::ostringstream os;
233  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
234  tr.setVehicleName(os.str());
235  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
236  varDepTime(tr); //slight variation on each "default" car
237  if (timeTripValidation(tr)) {
238  expTrips.push_back(tr);
239  }
240  //else
241  // std::cout << "trop tard 2 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
242  }
243  }
244 
245  std::cout << "total trips generated: " << acts.trips.size() << std::endl;
246  std::cout << "total trips finally taken: " << expTrips.size() << std::endl;
247 
251  expTrips.sort(); //natural order of trips
252  std::cout << "...sorted by departure time.\n" << std::endl;
253 
257  generateOutputFile(expTrips);
258 }
259 
260 /****************************************************************************/