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-2018 German Aerospace Center (DLR) and others.
4 // activitygen module
5 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 // SPDX-License-Identifier: EPL-2.0
11 /****************************************************************************/
20 // Main class that handles City, Activities and Trips
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <iostream>
30 #include <utils/xml/XMLSubSys.h>
33 #include <sstream>
34 #include "AGActivityGen.h"
35 #include "AGActivityGenHandler.h"
36 #include "city/AGPosition.h"
38 #include "AGActivityTripWriter.h"
39 #include "city/AGTime.h"
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 void
47  AGActivityGenHandler handler(city, net);
48  PROGRESS_BEGIN_MESSAGE("Reading input");
49  if (!XMLSubSys::runParser(handler, inputFile)) {
51  throw ProcessError();
52  } else {
54  }
55 
56  PROGRESS_BEGIN_MESSAGE("Consolidating statistics");
57  city.statData.consolidateStat(); //some maps are still not
59 
60  PROGRESS_BEGIN_MESSAGE("Building street representation");
63 
64  PROGRESS_BEGIN_MESSAGE("Generating work positions");
67 
68  PROGRESS_BEGIN_MESSAGE("Building bus lines");
71 
72 
73  PROGRESS_BEGIN_MESSAGE("Generating population");
76 
77  PROGRESS_BEGIN_MESSAGE("Allocating schools");
80 
81  PROGRESS_BEGIN_MESSAGE("Allocating work places");
84 
85  PROGRESS_BEGIN_MESSAGE("Allocating car places");
88 }
89 
90 bool
92  if (trip.getDay() > durationInDays + 1) {
93  return false;
94  }
95  if (trip.getDay() == 1) { //first day
96  if (trip.getTime() < beginTime) {
97  return false;
98  }
99  if (durationInDays == 0 && trip.getTime() > endTime) {
100  return false;
101  }
102  }
103  if (trip.getDay() == durationInDays + 1) { //last day
104  if (trip.getTime() > endTime) {
105  return false;
106  }
107  if (durationInDays == 0 && trip.getTime() < beginTime) {
108  return false;
109  }
110  }
111  return true;
112 }
113 
114 void
116  if (trip.getType() != "default") {
117  return;
118  }
119  //buses are on time and random are already spread
120  int variation = (int)RandHelper::randNorm(0, city.statData.departureVariation);
121  AGTime depTime(trip.getDay(), 0, 0, trip.getTime());
122  depTime += variation;
123  if (depTime.getDay() > 0) {
124  trip.setDay(depTime.getDay());
125  trip.setDepTime(depTime.getSecondsInCurrentDay());
126  } else {
127  trip.setDay(1);
128  trip.setDepTime(0);
129  }
130 }
131 
132 
133 void
134 AGActivityGen::generateOutputFile(std::list<AGTrip>& trips) {
136  if (trips.size() != 0) {
137  std::list<AGTrip>::iterator it;
138  //variables for TESTS:
139  int firstTrip = trips.front().getTime() + trips.front().getDay() * 86400;
140  int lastTrip = trips.front().getTime() + trips.front().getDay() * 86400;
141  std::map<int, int> histogram;
142  for (int i = 0; i < 100; ++i) {
143  histogram[i] = 0;
144  }
145  //END var TESTS
146  for (it = trips.begin(); it != trips.end(); ++it) {
147  atw.addTrip(*it);
148  //TEST
149  if (it->getTime() + 86400 * it->getDay() > lastTrip) {
150  lastTrip = it->getTime() + 86400 * it->getDay();
151  }
152  if (it->getTime() + 86400 * it->getDay() < firstTrip) {
153  firstTrip = it->getTime() + 86400 * it->getDay();
154  }
155  //++histogram[((it->getDay()-1)*86400 + it->getTime())/3600];
156  ++histogram[(it->getTime()) / 3600];
157  //END TEST
158  }
159  //PRINT TEST
160  AGTime first(firstTrip);
161  AGTime last(lastTrip);
162  std::cout << "first real trip: " << first.getDay() << ", " << first.getHour() << ":" << first.getMinute() << ":" << first.getSecond() << std::endl;
163  std::cout << "last real trip: " << last.getDay() << ", " << last.getHour() << ":" << last.getMinute() << ":" << last.getSecond() << std::endl;
164  for (int i = 0; i < 100; ++i) {
165  if (histogram[i] > 0) {
166  std::cout << "histogram[ hour " << i << " ] = " << histogram[i] << std::endl;
167  }
168  }
169  } else {
170  std::cout << "No real trips were generated" << std::endl;
171  }
172 }
173 
174 void
175 AGActivityGen::makeActivityTrips(int days, int beginSec, int endSec) {
176  durationInDays = days;
177  beginTime = beginSec;
178  endTime = endSec;
182  AGActivities acts(&city, durationInDays + 1);
183  acts.generateActivityTrips();
184 
188  //list<Trip>* trips = &(acts.trips);
189  std::list<AGTrip> expTrips;
190  std::map<std::string, int> carUsed;
191  std::list<AGTrip>::iterator it;
192  //multiplication of days
193  for (it = acts.trips.begin(); it != acts.trips.end(); ++it) {
194  if (it->isDaily()) {
195  for (int currday = 1; currday < durationInDays + 2; ++currday) {
196  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), currday);
197  tr.setType(it->getType());
198  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
199  ++carUsed.find(tr.getVehicleName())->second;
200  } else {
201  carUsed[tr.getVehicleName()] = 1;
202  }
203  std::ostringstream os;
204  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
205  tr.setVehicleName(os.str());
206  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
207  varDepTime(tr); //slight variation on each "default" car
208  if (timeTripValidation(tr)) {
209  expTrips.push_back(tr);
210  }
211  //else
212  //std::cout << "trop tard 1 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
213  }
214  } else {
215  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), it->getDay());
216  tr.setType(it->getType());
217  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
218  ++carUsed.find(tr.getVehicleName())->second;
219  } else {
220  carUsed[tr.getVehicleName()] = 1;
221  }
222  std::ostringstream os;
223  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
224  tr.setVehicleName(os.str());
225  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
226  varDepTime(tr); //slight variation on each "default" car
227  if (timeTripValidation(tr)) {
228  expTrips.push_back(tr);
229  }
230  //else
231  //std::cout << "trop tard 2 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
232  }
233  }
234 
235  std::cout << "total trips generated: " << acts.trips.size() << std::endl;
236  std::cout << "total trips finally taken: " << expTrips.size() << std::endl;
237 
241  expTrips.sort(); //natural order of trips
242  std::cout << "...sorted by departure time.\n" << std::endl;
243 
247  generateOutputFile(expTrips);
248 }
249 
250 /****************************************************************************/
int getHour()
Definition: AGTime.cpp:104
void generateActivityTrips()
void varDepTime(AGTrip &trip) const
void completeStreets()
Definition: AGCity.cpp:50
Definition: AGTime.h:37
OutputDevice & outputFile
The generated routes.
Definition: AGActivityGen.h:90
AGDataAndStatistics & statData
Definition: AGCity.h:81
void setType(std::string type)
Definition: AGTrip.cpp:93
int getSecond()
Definition: AGTime.cpp:114
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:161
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:113
void completeBusLines()
Definition: AGCity.cpp:152
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:246
void setDay(int day)
Definition: AGTrip.cpp:182
void setDepTime(int time)
Definition: AGTrip.cpp:142
static double randNorm(double mean, double variance, std::mt19937 *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:141
void schoolAllocation()
Definition: AGCity.cpp:263
void generateWorkPositions()
Definition: AGCity.cpp:97
std::list< AGTrip > trips
Definition: AGActivities.h:56
int getTime() const
Definition: AGTrip.cpp:108
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:243
void workAllocation()
Definition: AGCity.cpp:284
int getDay() const
Definition: AGTrip.cpp:177
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:350
void importInfoCity()
build the internal city
std::string inputFile
Definition: AGActivityGen.h:88
int getMinute()
Definition: AGTime.cpp:109
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:244
const std::string & getType() const
Definition: AGTrip.cpp:88
Definition: AGTrip.h:41
int getDay()
Definition: AGTime.cpp:99