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 /****************************************************************************/
10 // Main class that handles City, Activities and Trips
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2010-2014 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 <iostream>
37 #include <utils/xml/XMLSubSys.h>
40 #include <sstream>
41 #include "AGActivityGen.h"
42 #include "AGActivityGenHandler.h"
43 #include "city/AGPosition.h"
45 #include "AGActivityTripWriter.h"
46 #include "city/AGTime.h"
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 void
58  AGActivityGenHandler handler(city, net);
59  PROGRESS_BEGIN_MESSAGE("Reading input");
60  if (!XMLSubSys::runParser(handler, inputFile)) {
62  throw ProcessError();
63  } else {
65  }
66 
67  PROGRESS_BEGIN_MESSAGE("Consolidating statistics");
68  city.statData.consolidateStat(); //some maps are still not
70 
71  PROGRESS_BEGIN_MESSAGE("Building street representation");
74 
75  PROGRESS_BEGIN_MESSAGE("Generating work positions");
78 
79  PROGRESS_BEGIN_MESSAGE("Building bus lines");
82 
83 
84  PROGRESS_BEGIN_MESSAGE("Generating population");
87 
88  PROGRESS_BEGIN_MESSAGE("Allocating schools");
91 
92  PROGRESS_BEGIN_MESSAGE("Allocating work places");
95 
96  PROGRESS_BEGIN_MESSAGE("Allocating car places");
99 }
100 
101 bool
103  if (trip.getDay() > durationInDays + 1) {
104  return false;
105  }
106  if (trip.getDay() == 1) { //first day
107  if (trip.getTime() < beginTime) {
108  return false;
109  }
110  if (durationInDays == 0 && trip.getTime() > endTime) {
111  return false;
112  }
113  }
114  if (trip.getDay() == durationInDays + 1) { //last day
115  if (trip.getTime() > endTime) {
116  return false;
117  }
118  if (durationInDays == 0 && trip.getTime() < beginTime) {
119  return false;
120  }
121  }
122  return true;
123 }
124 
125 void
127  if (trip.getType() != "default") {
128  return;
129  }
130  //buses are on time and random are already spread
132  AGTime depTime(trip.getDay(), 0, 0, trip.getTime());
133  depTime += variation;
134  if (depTime.getDay() > 0) {
135  trip.setDay(depTime.getDay());
136  trip.setDepTime(depTime.getSecondsInCurrentDay());
137  } else {
138  trip.setDay(1);
139  trip.setDepTime(0);
140  }
141 }
142 
143 
144 void
145 AGActivityGen::generateOutputFile(std::list<AGTrip>& trips) {
147  if (trips.size() != 0) {
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  } else {
181  std::cout << "No real trips were generated" << std::endl;
182  }
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 /****************************************************************************/
int getHour()
Definition: AGTime.cpp:111
void generateActivityTrips()
void varDepTime(AGTrip &trip)
void completeStreets()
Definition: AGCity.cpp:56
Definition: AGTime.h:44
OutputDevice & outputFile
The generated routes.
Definition: AGActivityGen.h:91
AGDataAndStatistics & statData
Definition: AGCity.h:88
static SUMOReal randNorm(SUMOReal mean, SUMOReal variance, MTRand &rng=myRandomNumberGenerator)
Access to a random number from a normal distribution.
Definition: RandHelper.h:92
void setType(std::string type)
Definition: AGTrip.cpp:100
int getSecond()
Definition: AGTime.cpp:121
bool timeTripValidation(AGTrip)
validation: compatibility of the given trip
void makeActivityTrips(int days=1, int beginTime=0, int endTime=0)
build activities and trips of the population and generate routes
void generatePopulation()
Definition: AGCity.cpp:169
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:114
void completeBusLines()
Definition: AGCity.cpp:160
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:204
void setDay(int day)
Definition: AGTrip.cpp:189
void setDepTime(int time)
Definition: AGTrip.cpp:149
void schoolAllocation()
Definition: AGCity.cpp:271
void generateWorkPositions()
Definition: AGCity.cpp:105
std::list< AGTrip > trips
Definition: AGActivities.h:63
int getTime() const
Definition: AGTrip.cpp:115
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
void workAllocation()
Definition: AGCity.cpp:292
void generateOutputFile(std::list< AGTrip > &trips)
generate the output file (trips or routes) using a trip list
void carAllocation()
Definition: AGCity.cpp:350
void importInfoCity()
build the internal city
int getDay() const
Definition: AGTrip.cpp:184
std::string inputFile
Definition: AGActivityGen.h:89
int getMinute()
Definition: AGTime.cpp:116
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
Definition: AGTrip.h:48
int getDay()
Definition: AGTime.cpp:106
const std::string & getType() const
Definition: AGTrip.cpp:95