Eclipse 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.dev/sumo
3 // Copyright (C) 2010-2024 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 are made available under the
7 // terms of the Eclipse Public License 2.0 which is available at
8 // https://www.eclipse.org/legal/epl-2.0/
9 // This Source Code may also be made available under the following Secondary
10 // Licenses when the conditions for such availability set forth in the Eclipse
11 // Public License 2.0 are satisfied: GNU General Public License, version 2
12 // or later which is available at
13 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
15 /****************************************************************************/
23 // Main class that handles City, Activities and Trips
24 /****************************************************************************/
25 #include <config.h>
26 
27 #include <iostream>
28 #include <utils/xml/XMLSubSys.h>
31 #include <sstream>
32 #include "AGActivityGen.h"
33 #include "AGActivityGenHandler.h"
34 #include "city/AGPosition.h"
36 #include "AGActivityTripWriter.h"
37 #include "city/AGTime.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
43 void
45  AGActivityGenHandler handler(city, net);
46  PROGRESS_BEGIN_MESSAGE(TL("Reading input"));
47  if (!XMLSubSys::runParser(handler, inputFile)) {
49  throw ProcessError();
50  } else {
52  }
53 
54  PROGRESS_BEGIN_MESSAGE(TL("Consolidating statistics"));
55  city.statData.consolidateStat(); //some maps are still not
57 
58  PROGRESS_BEGIN_MESSAGE(TL("Building street representation"));
61 
62  PROGRESS_BEGIN_MESSAGE(TL("Generating work positions"));
65 
66  PROGRESS_BEGIN_MESSAGE(TL("Building bus lines"));
69 
70 
71  PROGRESS_BEGIN_MESSAGE(TL("Generating population"));
74 
75  PROGRESS_BEGIN_MESSAGE(TL("Allocating schools"));
78 
79  PROGRESS_BEGIN_MESSAGE(TL("Allocating work places"));
82 
83  PROGRESS_BEGIN_MESSAGE(TL("Allocating car places"));
86 }
87 
88 bool
90  if (trip.getDay() > durationInDays + 1) {
91  return false;
92  }
93  if (trip.getDay() == 1) { //first day
94  if (trip.getTime() < beginTime) {
95  return false;
96  }
97  if (durationInDays == 0 && trip.getTime() > endTime) {
98  return false;
99  }
100  }
101  if (trip.getDay() == durationInDays + 1) { //last day
102  if (trip.getTime() > endTime) {
103  return false;
104  }
105  if (durationInDays == 0 && trip.getTime() < beginTime) {
106  return false;
107  }
108  }
109  return true;
110 }
111 
112 void
114  if (trip.getType() != "default") {
115  return;
116  }
117  //buses are on time and random are already spread
118  int variation = (int)RandHelper::randNorm(0, city.statData.departureVariation);
119  AGTime depTime(trip.getDay(), 0, 0, trip.getTime());
120  depTime += variation;
121  if (depTime.getDay() > 0) {
122  trip.setDay(depTime.getDay());
123  trip.setDepTime(depTime.getSecondsInCurrentDay());
124  } else {
125  trip.setDay(1);
126  trip.setDepTime(0);
127  }
128 }
129 
130 
131 void
132 AGActivityGen::generateOutputFile(std::list<AGTrip>& trips) {
134  if (trips.size() != 0) {
135  std::list<AGTrip>::iterator it;
136  //variables for TESTS:
137  int firstTrip = trips.front().getTime() + trips.front().getDay() * 86400;
138  int lastTrip = trips.front().getTime() + trips.front().getDay() * 86400;
139  std::map<int, int> histogram;
140  for (int i = 0; i < 100; ++i) {
141  histogram[i] = 0;
142  }
143  //END var TESTS
144  for (it = trips.begin(); it != trips.end(); ++it) {
145  atw.addTrip(*it);
146  //TEST
147  if (it->getTime() + 86400 * it->getDay() > lastTrip) {
148  lastTrip = it->getTime() + 86400 * it->getDay();
149  }
150  if (it->getTime() + 86400 * it->getDay() < firstTrip) {
151  firstTrip = it->getTime() + 86400 * it->getDay();
152  }
153  //++histogram[((it->getDay()-1)*86400 + it->getTime())/3600];
154  ++histogram[(it->getTime()) / 3600];
155  //END TEST
156  }
157  //PRINT TEST
158  AGTime first(firstTrip);
159  AGTime last(lastTrip);
160  std::cout << "first real trip: " << first.getDay() << ", " << first.getHour() << ":" << first.getMinute() << ":" << first.getSecond() << std::endl;
161  std::cout << "last real trip: " << last.getDay() << ", " << last.getHour() << ":" << last.getMinute() << ":" << last.getSecond() << std::endl;
162  for (int i = 0; i < 100; ++i) {
163  if (histogram[i] > 0) {
164  std::cout << "histogram[ hour " << i << " ] = " << histogram[i] << std::endl;
165  }
166  }
167  } else {
168  std::cout << "No real trips were generated" << std::endl;
169  }
170 }
171 
172 void
173 AGActivityGen::makeActivityTrips(int days, int beginSec, int endSec) {
174  durationInDays = days;
175  beginTime = beginSec;
176  endTime = endSec;
180  AGActivities acts(&city, durationInDays + 1);
181  acts.generateActivityTrips();
182 
186  //list<Trip>* trips = &(acts.trips);
187  std::list<AGTrip> expTrips;
188  std::map<std::string, int> carUsed;
189  std::list<AGTrip>::iterator it;
190  //multiplication of days
191  for (it = acts.trips.begin(); it != acts.trips.end(); ++it) {
192  if (it->isDaily()) {
193  for (int currday = 1; currday < durationInDays + 2; ++currday) {
194  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), currday);
195  tr.setType(it->getType());
196  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
197  ++carUsed.find(tr.getVehicleName())->second;
198  } else {
199  carUsed[tr.getVehicleName()] = 1;
200  }
201  std::ostringstream os;
202  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
203  tr.setVehicleName(os.str());
204  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
205  varDepTime(tr); //slight variation on each "default" car
206  if (timeTripValidation(tr)) {
207  expTrips.push_back(tr);
208  }
209  //else
210  //std::cout << "trop tard 1 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
211  }
212  } else {
213  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), it->getDay());
214  tr.setType(it->getType());
215  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
216  ++carUsed.find(tr.getVehicleName())->second;
217  } else {
218  carUsed[tr.getVehicleName()] = 1;
219  }
220  std::ostringstream os;
221  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
222  tr.setVehicleName(os.str());
223  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
224  varDepTime(tr); //slight variation on each "default" car
225  if (timeTripValidation(tr)) {
226  expTrips.push_back(tr);
227  }
228  //else
229  //std::cout << "trop tard 2 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
230  }
231  }
232 
233  std::cout << "total trips generated: " << acts.trips.size() << std::endl;
234  std::cout << "total trips finally taken: " << expTrips.size() << std::endl;
235 
239  expTrips.sort(); //natural order of trips
240  std::cout << "...sorted by departure time.\n" << std::endl;
241 
245  generateOutputFile(expTrips);
246 }
247 
248 
249 /****************************************************************************/
#define TL(string)
Definition: MsgHandler.h:315
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:300
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:303
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:299
std::list< AGTrip > trips
Definition: AGActivities.h:53
void generateActivityTrips()
bool timeTripValidation(const AGTrip &trip) const
validation: compatibility of the given trip
std::string inputFile
Definition: AGActivityGen.h:85
void generateOutputFile(std::list< AGTrip > &trips)
generate the output file (trips or routes) using a trip list
void importInfoCity()
build the internal city
void varDepTime(AGTrip &trip) const
OutputDevice & outputFile
The generated routes.
Definition: AGActivityGen.h:87
void makeActivityTrips(int days=1, int beginTime=0, int endTime=0)
build activities and trips of the population and generate routes
void addTrip(const AGTrip &trip)
void workAllocation()
Definition: AGCity.cpp:278
void completeBusLines()
Definition: AGCity.cpp:156
void carAllocation()
Definition: AGCity.cpp:344
void generateWorkPositions()
Definition: AGCity.cpp:102
AGDataAndStatistics & statData
Definition: AGCity.h:78
void completeStreets()
Definition: AGCity.cpp:50
void schoolAllocation()
Definition: AGCity.cpp:257
void generatePopulation()
Definition: AGCity.cpp:165
Definition: AGTime.h:34
int getSecond()
Definition: AGTime.cpp:112
int getSecondsInCurrentDay()
Definition: AGTime.cpp:117
int getMinute()
Definition: AGTime.cpp:107
int getHour()
Definition: AGTime.cpp:102
int getDay()
Definition: AGTime.cpp:97
Definition: AGTrip.h:38
void setVehicleName(std::string name)
Definition: AGTrip.cpp:155
int getTime() const
Definition: AGTrip.cpp:106
void setDepTime(int time)
Definition: AGTrip.cpp:140
void addLayOverWithoutDestination(AGTrip &trip)
Definition: AGTrip.cpp:73
void setType(std::string type)
Definition: AGTrip.cpp:91
const std::string & getType() const
Definition: AGTrip.cpp:86
int getDay() const
Definition: AGTrip.cpp:175
void setDay(int day)
Definition: AGTrip.cpp:180
const std::string & getVehicleName() const
Definition: AGTrip.cpp:150
static double randNorm(double mean, double variance, SumoRNG *rng=nullptr)
Access to a random number from a normal distribution.
Definition: RandHelper.cpp:137
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:148