SUMO - Simulation of Urban MObility
AGActivityGen.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2010-2017 German Aerospace Center (DLR) and others.
4 // activitygen module
5 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
6 /****************************************************************************/
7 //
8 // This program and the accompanying materials
9 // are made available under the terms of the Eclipse Public License v2.0
10 // which accompanies this distribution, and is available at
11 // http://www.eclipse.org/legal/epl-v20.html
12 //
13 /****************************************************************************/
22 // Main class that handles City, Activities and Trips
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 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 void
53  AGActivityGenHandler handler(city, net);
54  PROGRESS_BEGIN_MESSAGE("Reading input");
55  if (!XMLSubSys::runParser(handler, inputFile)) {
57  throw ProcessError();
58  } else {
60  }
61 
62  PROGRESS_BEGIN_MESSAGE("Consolidating statistics");
63  city.statData.consolidateStat(); //some maps are still not
65 
66  PROGRESS_BEGIN_MESSAGE("Building street representation");
69 
70  PROGRESS_BEGIN_MESSAGE("Generating work positions");
73 
74  PROGRESS_BEGIN_MESSAGE("Building bus lines");
77 
78 
79  PROGRESS_BEGIN_MESSAGE("Generating population");
82 
83  PROGRESS_BEGIN_MESSAGE("Allocating schools");
86 
87  PROGRESS_BEGIN_MESSAGE("Allocating work places");
90 
91  PROGRESS_BEGIN_MESSAGE("Allocating car places");
94 }
95 
96 bool
98  if (trip.getDay() > durationInDays + 1) {
99  return false;
100  }
101  if (trip.getDay() == 1) { //first day
102  if (trip.getTime() < beginTime) {
103  return false;
104  }
105  if (durationInDays == 0 && trip.getTime() > endTime) {
106  return false;
107  }
108  }
109  if (trip.getDay() == durationInDays + 1) { //last day
110  if (trip.getTime() > endTime) {
111  return false;
112  }
113  if (durationInDays == 0 && trip.getTime() < beginTime) {
114  return false;
115  }
116  }
117  return true;
118 }
119 
120 void
122  if (trip.getType() != "default") {
123  return;
124  }
125  //buses are on time and random are already spread
126  int variation = (int)RandHelper::randNorm(0, city.statData.departureVariation);
127  AGTime depTime(trip.getDay(), 0, 0, trip.getTime());
128  depTime += variation;
129  if (depTime.getDay() > 0) {
130  trip.setDay(depTime.getDay());
131  trip.setDepTime(depTime.getSecondsInCurrentDay());
132  } else {
133  trip.setDay(1);
134  trip.setDepTime(0);
135  }
136 }
137 
138 
139 void
140 AGActivityGen::generateOutputFile(std::list<AGTrip>& trips) {
142  if (trips.size() != 0) {
143  std::list<AGTrip>::iterator it;
144  //variables for TESTS:
145  int firstTrip = trips.front().getTime() + trips.front().getDay() * 86400;
146  int lastTrip = trips.front().getTime() + trips.front().getDay() * 86400;
147  std::map<int, int> histogram;
148  for (int i = 0; i < 100; ++i) {
149  histogram[i] = 0;
150  }
151  //END var TESTS
152  for (it = trips.begin(); it != trips.end(); ++it) {
153  atw.addTrip(*it);
154  //TEST
155  if (it->getTime() + 86400 * it->getDay() > lastTrip) {
156  lastTrip = it->getTime() + 86400 * it->getDay();
157  }
158  if (it->getTime() + 86400 * it->getDay() < firstTrip) {
159  firstTrip = it->getTime() + 86400 * it->getDay();
160  }
161  //++histogram[((it->getDay()-1)*86400 + it->getTime())/3600];
162  ++histogram[(it->getTime()) / 3600];
163  //END TEST
164  }
165  //PRINT TEST
166  AGTime first(firstTrip);
167  AGTime last(lastTrip);
168  std::cout << "first real trip: " << first.getDay() << ", " << first.getHour() << ":" << first.getMinute() << ":" << first.getSecond() << std::endl;
169  std::cout << "last real trip: " << last.getDay() << ", " << last.getHour() << ":" << last.getMinute() << ":" << last.getSecond() << std::endl;
170  for (int i = 0; i < 100; ++i) {
171  if (histogram[i] > 0) {
172  std::cout << "histogram[ hour " << i << " ] = " << histogram[i] << std::endl;
173  }
174  }
175  } else {
176  std::cout << "No real trips were generated" << std::endl;
177  }
178 }
179 
180 void
181 AGActivityGen::makeActivityTrips(int days, int beginSec, int endSec) {
182  durationInDays = days;
183  beginTime = beginSec;
184  endTime = endSec;
188  AGActivities acts(&city, durationInDays + 1);
189  acts.generateActivityTrips();
190 
194  //list<Trip>* trips = &(acts.trips);
195  std::list<AGTrip> expTrips;
196  std::map<std::string, int> carUsed;
197  std::list<AGTrip>::iterator it;
198  //multiplication of days
199  for (it = acts.trips.begin(); it != acts.trips.end(); ++it) {
200  if (it->isDaily()) {
201  for (int currday = 1; currday < durationInDays + 2; ++currday) {
202  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), currday);
203  tr.setType(it->getType());
204  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
205  ++carUsed.find(tr.getVehicleName())->second;
206  } else {
207  carUsed[tr.getVehicleName()] = 1;
208  }
209  std::ostringstream os;
210  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
211  tr.setVehicleName(os.str());
212  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
213  varDepTime(tr); //slight variation on each "default" car
214  if (timeTripValidation(tr)) {
215  expTrips.push_back(tr);
216  }
217  //else
218  //std::cout << "trop tard 1 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
219  }
220  } else {
221  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), it->getDay());
222  tr.setType(it->getType());
223  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
224  ++carUsed.find(tr.getVehicleName())->second;
225  } else {
226  carUsed[tr.getVehicleName()] = 1;
227  }
228  std::ostringstream os;
229  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
230  tr.setVehicleName(os.str());
231  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
232  varDepTime(tr); //slight variation on each "default" car
233  if (timeTripValidation(tr)) {
234  expTrips.push_back(tr);
235  }
236  //else
237  //std::cout << "trop tard 2 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
238  }
239  }
240 
241  std::cout << "total trips generated: " << acts.trips.size() << std::endl;
242  std::cout << "total trips finally taken: " << expTrips.size() << std::endl;
243 
247  expTrips.sort(); //natural order of trips
248  std::cout << "...sorted by departure time.\n" << std::endl;
249 
253  generateOutputFile(expTrips);
254 }
255 
256 /****************************************************************************/
int getHour()
Definition: AGTime.cpp:110
void generateActivityTrips()
void varDepTime(AGTrip &trip) const
void completeStreets()
Definition: AGCity.cpp:56
Definition: AGTime.h:43
OutputDevice & outputFile
The generated routes.
Definition: AGActivityGen.h:96
AGDataAndStatistics & statData
Definition: AGCity.h:87
void setType(std::string type)
Definition: AGTrip.cpp:99
int getSecond()
Definition: AGTime.cpp:120
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:167
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:109
void completeBusLines()
Definition: AGCity.cpp:158
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:204
void setDay(int day)
Definition: AGTrip.cpp:188
void setDepTime(int time)
Definition: AGTrip.cpp:148
static double randNorm(double mean, double variance, std::mt19937 *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:136
void schoolAllocation()
Definition: AGCity.cpp:269
void generateWorkPositions()
Definition: AGCity.cpp:103
std::list< AGTrip > trips
Definition: AGActivities.h:62
int getTime() const
Definition: AGTrip.cpp:114
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:201
void workAllocation()
Definition: AGCity.cpp:290
int getDay() const
Definition: AGTrip.cpp:183
bool timeTripValidation(const AGTrip &trip) const
validation: compatibility of the given trip
void generateOutputFile(std::list< AGTrip > &trips)
generate the output file (trips or routes) using a trip list
void addTrip(const AGTrip &trip)
void carAllocation()
Definition: AGCity.cpp:356
void importInfoCity()
build the internal city
std::string inputFile
Definition: AGActivityGen.h:94
int getMinute()
Definition: AGTime.cpp:115
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:202
const std::string & getType() const
Definition: AGTrip.cpp:94
Definition: AGTrip.h:47
int getDay()
Definition: AGTime.cpp:105