Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/activitygen/city/AGBusLine.cpp
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-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 AGBusLine.cpp
17
/// @author Piotr Woznica
18
/// @author Daniel Krajzewicz
19
/// @author Laura Bieker
20
/// @author Michael Behrisch
21
/// @author Walter Bamberger
22
/// @date July 2010
23
///
24
// Bus line of the city: contains all the buses of this line
25
/****************************************************************************/
26
#include <config.h>
27
28
#include <iostream>
29
#include <utility>
30
#include <sstream>
31
#include <string>
32
#include <list>
33
#include "AGBusLine.h"
34
#include "AGBus.h"
35
#include "AGPosition.h"
36
#include "AGTime.h"
37
#include <utils/common/StdDefs.h>
38
39
#define PAUSE_TIME 15 //time (in minutes) a bus waits before going in the opposite direction.
40
41
42
// ===========================================================================
43
// method definitions
44
// ===========================================================================
45
void
46
AGBusLine::setMaxTripTime(int time) {
47
this->maxTripTime = time;
48
}
49
50
void
51
AGBusLine::setBusNames() {
52
busNbr = 0;
53
std::list<AGBus>::iterator it1 = buses.begin(); //iterator on buses in the first direction
54
std::list<AGBus>::iterator it2 = revBuses.begin(); //iterator on buses in the second direction
55
56
std::list<std::pair<int, std::string> > drivingBuses1, drivingBuses2; //buses on the road or in the parking of the corresponding end: int: the time of availability
57
58
while (it1 != buses.end() && it2 != revBuses.end()) {
59
if (it1->getDeparture() > it2->getDeparture()) {
60
if (drivingBuses2.size() == 0) {
61
drivingBuses2.push_front(make_pair(it2->getDeparture(), createName()));
62
} else if (drivingBuses2.front().first > it2->getDeparture()) {
63
drivingBuses2.push_front(make_pair(it2->getDeparture(), createName()));
64
}
65
//here the first in drivingBuses2 is available for the trip
66
it2->setName(drivingBuses2.front().second);
67
drivingBuses2.pop_front();
68
//the same bus will be available for the main direction after some time (see function getReady):
69
drivingBuses1.push_back(make_pair(getReady(it2->getDeparture()), it2->getName()));
70
it2++;
71
} else {
72
if (drivingBuses1.size() == 0) {
73
drivingBuses1.push_front(make_pair(it1->getDeparture(), createName()));
74
} else if (drivingBuses1.front().first > it1->getDeparture()) {
75
drivingBuses1.push_front(make_pair(it1->getDeparture(), createName()));
76
}
77
//here the first in drivingBuses1 is available for the trip
78
it1->setName(drivingBuses1.front().second);
79
drivingBuses1.pop_front();
80
//the same bus will be available for the return way after some time (see function getReady):
81
drivingBuses2.push_back(make_pair(getReady(it1->getDeparture()), it1->getName()));
82
it1++;
83
}
84
}
85
if (it1 != buses.end()) {
86
if (drivingBuses1.size() == 0) {
87
it1->setName(createName());
88
} else if (drivingBuses1.front().first > it1->getDeparture()) {
89
it1->setName(createName());
90
} else {
91
it1->setName(drivingBuses1.front().second);
92
drivingBuses1.pop_front();
93
}
94
it1++;
95
}
96
if (it2 != revBuses.end()) {
97
if (drivingBuses2.size() == 0) {
98
it2->setName(createName());
99
} else if (drivingBuses2.front().first > it2->getDeparture()) {
100
it2->setName(createName());
101
} else {
102
it2->setName(drivingBuses2.front().second);
103
drivingBuses2.pop_front();
104
}
105
it2++;
106
}
107
}
108
109
std::string
110
AGBusLine::createName() {
111
++busNbr; //initialized in setBusNames()
112
std::ostringstream os;
113
os << busNbr;
114
return "bl" + lineNumber + "b" + os.str();
115
}
116
117
int
118
AGBusLine::getReady(int time) {
119
AGTime current(time);
120
current.addSeconds(maxTripTime);
121
current.addMinutes(PAUSE_TIME);
122
return current.getTime();
123
}
124
125
int
126
AGBusLine::nbrBuses() {
127
return static_cast<int>(buses.size());
128
}
129
130
void
131
AGBusLine::locateStation(AGPosition pos) {
132
stations.push_back(pos);
133
}
134
135
void
136
AGBusLine::locateRevStation(AGPosition pos) {
137
revStations.push_back(pos);
138
}
139
140
void
141
AGBusLine::generateBuses(int start, int stop, int rate) {
142
int t = start;
143
while (t < stop) {
144
buses.push_back(AGBus(t)); //one direction
145
revBuses.push_back(AGBus(t)); //return direction
146
t += rate;
147
}
148
}
149
150
151
void
152
AGBusLine::printBuses() {
153
std::list<AGBus>::iterator it;
154
std::cout << "\n ----------- BUS LINE " << lineNumber << " PRINTING -------------\n" << std::endl;
155
std::cout << "\n -------------------------- First way ---------------------------\n" << std::endl;
156
for (it = buses.begin(); it != buses.end(); ++it) {
157
it->print();
158
}
159
std::cout << "\n -------------------------- Second way --------------------------\n" << std::endl;
160
for (it = revBuses.begin(); it != revBuses.end(); ++it) {
161
it->print();
162
}
163
std::cout << "\n ----------------------------------------------------------------\n" << std::endl;
164
}
165
166
167
/****************************************************************************/
168
169