Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/activitygen/AGActivityGen.cpp
169666 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2010-2025 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
/****************************************************************************/
16
/// @file AGActivityGen.cpp
17
/// @author Piotr Woznica
18
/// @author Daniel Krajzewicz
19
/// @author Walter Bamberger
20
/// @author Michael Behrisch
21
/// @date July 2010
22
///
23
// Main class that handles City, Activities and Trips
24
/****************************************************************************/
25
#include <config.h>
26
27
#include <iostream>
28
#include <utils/xml/XMLSubSys.h>
29
#include <utils/common/MsgHandler.h>
30
#include <utils/common/RandHelper.h>
31
#include <sstream>
32
#include "AGActivityGen.h"
33
#include "AGActivityGenHandler.h"
34
#include "city/AGPosition.h"
35
#include "activities/AGActivities.h"
36
#include "AGActivityTripWriter.h"
37
#include "city/AGTime.h"
38
39
40
// ===========================================================================
41
// method definitions
42
// ===========================================================================
43
void
44
AGActivityGen::importInfoCity() {
45
AGActivityGenHandler handler(city, net);
46
PROGRESS_BEGIN_MESSAGE(TL("Reading input"));
47
if (!XMLSubSys::runParser(handler, inputFile)) {
48
PROGRESS_FAILED_MESSAGE();
49
throw ProcessError();
50
} else {
51
PROGRESS_DONE_MESSAGE();
52
}
53
54
PROGRESS_BEGIN_MESSAGE(TL("Consolidating statistics"));
55
city.statData.consolidateStat(); //some maps are still not
56
PROGRESS_DONE_MESSAGE();
57
58
PROGRESS_BEGIN_MESSAGE(TL("Building street representation"));
59
city.completeStreets();
60
PROGRESS_DONE_MESSAGE();
61
62
PROGRESS_BEGIN_MESSAGE(TL("Generating work positions"));
63
city.generateWorkPositions();
64
PROGRESS_DONE_MESSAGE();
65
66
PROGRESS_BEGIN_MESSAGE(TL("Building bus lines"));
67
city.completeBusLines();
68
PROGRESS_DONE_MESSAGE();
69
70
71
PROGRESS_BEGIN_MESSAGE(TL("Generating population"));
72
city.generatePopulation();
73
PROGRESS_DONE_MESSAGE();
74
75
PROGRESS_BEGIN_MESSAGE(TL("Allocating schools"));
76
city.schoolAllocation();
77
PROGRESS_DONE_MESSAGE();
78
79
PROGRESS_BEGIN_MESSAGE(TL("Allocating work places"));
80
city.workAllocation();
81
PROGRESS_DONE_MESSAGE();
82
83
PROGRESS_BEGIN_MESSAGE(TL("Allocating car places"));
84
city.carAllocation();
85
PROGRESS_DONE_MESSAGE();
86
}
87
88
bool
89
AGActivityGen::timeTripValidation(const AGTrip& trip) const {
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
113
AGActivityGen::varDepTime(AGTrip& trip) const {
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) {
133
AGActivityTripWriter atw(outputFile);
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;
177
/**
178
* making the activity trips
179
*/
180
AGActivities acts(&city, durationInDays + 1);
181
acts.generateActivityTrips();
182
183
/**
184
* validating the trips with the simulation's time limits
185
*/
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
236
/**
237
* re-ordering of trips: SUMO needs routes ordered by departure time.
238
*/
239
expTrips.sort(); //natural order of trips
240
std::cout << "...sorted by departure time.\n" << std::endl;
241
242
/**
243
* trip file generation
244
*/
245
generateOutputFile(expTrips);
246
}
247
248
249
/****************************************************************************/
250
251