SUMO - Simulation of Urban MObility
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.dlr.de/
13 // Copyright (C) 2010-2017 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 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 void
54  AGActivityGenHandler handler(city, net);
55  PROGRESS_BEGIN_MESSAGE("Reading input");
56  if (!XMLSubSys::runParser(handler, inputFile)) {
58  throw ProcessError();
59  } else {
61  }
62 
63  PROGRESS_BEGIN_MESSAGE("Consolidating statistics");
64  city.statData.consolidateStat(); //some maps are still not
66 
67  PROGRESS_BEGIN_MESSAGE("Building street representation");
70 
71  PROGRESS_BEGIN_MESSAGE("Generating work positions");
74 
75  PROGRESS_BEGIN_MESSAGE("Building bus lines");
78 
79 
80  PROGRESS_BEGIN_MESSAGE("Generating population");
83 
84  PROGRESS_BEGIN_MESSAGE("Allocating schools");
87 
88  PROGRESS_BEGIN_MESSAGE("Allocating work places");
91 
92  PROGRESS_BEGIN_MESSAGE("Allocating car places");
95 }
96 
97 bool
99  if (trip.getDay() > durationInDays + 1) {
100  return false;
101  }
102  if (trip.getDay() == 1) { //first day
103  if (trip.getTime() < beginTime) {
104  return false;
105  }
106  if (durationInDays == 0 && trip.getTime() > endTime) {
107  return false;
108  }
109  }
110  if (trip.getDay() == durationInDays + 1) { //last day
111  if (trip.getTime() > endTime) {
112  return false;
113  }
114  if (durationInDays == 0 && trip.getTime() < beginTime) {
115  return false;
116  }
117  }
118  return true;
119 }
120 
121 void
123  if (trip.getType() != "default") {
124  return;
125  }
126  //buses are on time and random are already spread
127  int variation = (int)RandHelper::randNorm(0, city.statData.departureVariation);
128  AGTime depTime(trip.getDay(), 0, 0, trip.getTime());
129  depTime += variation;
130  if (depTime.getDay() > 0) {
131  trip.setDay(depTime.getDay());
132  trip.setDepTime(depTime.getSecondsInCurrentDay());
133  } else {
134  trip.setDay(1);
135  trip.setDepTime(0);
136  }
137 }
138 
139 
140 void
141 AGActivityGen::generateOutputFile(std::list<AGTrip>& trips) {
143  if (trips.size() != 0) {
144  std::list<AGTrip>::iterator it;
145  //variables for TESTS:
146  int firstTrip = trips.front().getTime() + trips.front().getDay() * 86400;
147  int lastTrip = trips.front().getTime() + trips.front().getDay() * 86400;
148  std::map<int, int> histogram;
149  for (int i = 0; i < 100; ++i) {
150  histogram[i] = 0;
151  }
152  //END var TESTS
153  for (it = trips.begin(); it != trips.end(); ++it) {
154  atw.addTrip(*it);
155  //TEST
156  if (it->getTime() + 86400 * it->getDay() > lastTrip) {
157  lastTrip = it->getTime() + 86400 * it->getDay();
158  }
159  if (it->getTime() + 86400 * it->getDay() < firstTrip) {
160  firstTrip = it->getTime() + 86400 * it->getDay();
161  }
162  //++histogram[((it->getDay()-1)*86400 + it->getTime())/3600];
163  ++histogram[(it->getTime()) / 3600];
164  //END TEST
165  }
166  //PRINT TEST
167  AGTime first(firstTrip);
168  AGTime last(lastTrip);
169  std::cout << "first real trip: " << first.getDay() << ", " << first.getHour() << ":" << first.getMinute() << ":" << first.getSecond() << std::endl;
170  std::cout << "last real trip: " << last.getDay() << ", " << last.getHour() << ":" << last.getMinute() << ":" << last.getSecond() << std::endl;
171  for (int i = 0; i < 100; ++i) {
172  if (histogram[i] > 0) {
173  std::cout << "histogram[ hour " << i << " ] = " << histogram[i] << std::endl;
174  }
175  }
176  } else {
177  std::cout << "No real trips were generated" << std::endl;
178  }
179 }
180 
181 void
182 AGActivityGen::makeActivityTrips(int days, int beginSec, int endSec) {
183  durationInDays = days;
184  beginTime = beginSec;
185  endTime = endSec;
189  AGActivities acts(&city, durationInDays + 1);
190  acts.generateActivityTrips();
191 
195  //list<Trip>* trips = &(acts.trips);
196  std::list<AGTrip> expTrips;
197  std::map<std::string, int> carUsed;
198  std::list<AGTrip>::iterator it;
199  //multiplication of days
200  for (it = acts.trips.begin(); it != acts.trips.end(); ++it) {
201  if (it->isDaily()) {
202  for (int currday = 1; currday < durationInDays + 2; ++currday) {
203  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), currday);
204  tr.setType(it->getType());
205  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
206  ++carUsed.find(tr.getVehicleName())->second;
207  } else {
208  carUsed[tr.getVehicleName()] = 1;
209  }
210  std::ostringstream os;
211  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
212  tr.setVehicleName(os.str());
213  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
214  varDepTime(tr); //slight variation on each "default" car
215  if (timeTripValidation(tr)) {
216  expTrips.push_back(tr);
217  }
218  //else
219  //std::cout << "trop tard 1 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
220  }
221  } else {
222  AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), it->getDay());
223  tr.setType(it->getType());
224  if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
225  ++carUsed.find(tr.getVehicleName())->second;
226  } else {
227  carUsed[tr.getVehicleName()] = 1;
228  }
229  std::ostringstream os;
230  os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
231  tr.setVehicleName(os.str());
232  tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
233  varDepTime(tr); //slight variation on each "default" car
234  if (timeTripValidation(tr)) {
235  expTrips.push_back(tr);
236  }
237  //else
238  //std::cout << "trop tard 2 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
239  }
240  }
241 
242  std::cout << "total trips generated: " << acts.trips.size() << std::endl;
243  std::cout << "total trips finally taken: " << expTrips.size() << std::endl;
244 
248  expTrips.sort(); //natural order of trips
249  std::cout << "...sorted by departure time.\n" << std::endl;
250 
254  generateOutputFile(expTrips);
255 }
256 
257 /****************************************************************************/
int getHour()
Definition: AGTime.cpp:111
void generateActivityTrips()
void varDepTime(AGTrip &trip) const
void completeStreets()
Definition: AGCity.cpp:57
Definition: AGTime.h:44
OutputDevice & outputFile
The generated routes.
Definition: AGActivityGen.h:97
AGDataAndStatistics & statData
Definition: AGCity.h:88
void setType(std::string type)
Definition: AGTrip.cpp:100
int getSecond()
Definition: AGTime.cpp:121
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:170
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:110
void completeBusLines()
Definition: AGCity.cpp:161
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:205
void setDay(int day)
Definition: AGTrip.cpp:189
void setDepTime(int time)
Definition: AGTrip.cpp:149
void schoolAllocation()
Definition: AGCity.cpp:272
void generateWorkPositions()
Definition: AGCity.cpp:106
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:293
int getDay() const
Definition: AGTrip.cpp:184
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:359
void importInfoCity()
build the internal city
std::string inputFile
Definition: AGActivityGen.h:95
int getMinute()
Definition: AGTime.cpp:116
static double randNorm(double mean, double variance, MTRand *rng=0)
Access to a random number from a normal distribution.
Definition: RandHelper.h:97
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
const std::string & getType() const
Definition: AGTrip.cpp:95
Definition: AGTrip.h:48
int getDay()
Definition: AGTime.cpp:106