Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-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 : /****************************************************************************/
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 11 : AGBusLine::setMaxTripTime(int time) {
47 11 : this->maxTripTime = time;
48 11 : }
49 :
50 : void
51 11 : AGBusLine::setBusNames() {
52 11 : 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 1010 : while (it1 != buses.end() && it2 != revBuses.end()) {
59 999 : if (it1->getDeparture() > it2->getDeparture()) {
60 494 : if (drivingBuses2.size() == 0) {
61 0 : drivingBuses2.push_front(make_pair(it2->getDeparture(), createName()));
62 494 : } else if (drivingBuses2.front().first > it2->getDeparture()) {
63 54 : drivingBuses2.push_front(make_pair(it2->getDeparture(), createName()));
64 : }
65 : //here the first in drivingBuses2 is available for the trip
66 988 : 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 988 : drivingBuses1.push_back(make_pair(getReady(it2->getDeparture()), it2->getName()));
70 : it2++;
71 : } else {
72 505 : if (drivingBuses1.size() == 0) {
73 22 : drivingBuses1.push_front(make_pair(it1->getDeparture(), createName()));
74 494 : } else if (drivingBuses1.front().first > it1->getDeparture()) {
75 32 : drivingBuses1.push_front(make_pair(it1->getDeparture(), createName()));
76 : }
77 : //here the first in drivingBuses1 is available for the trip
78 1010 : 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 1010 : drivingBuses2.push_back(make_pair(getReady(it1->getDeparture()), it1->getName()));
82 : it1++;
83 : }
84 : }
85 11 : if (it1 != buses.end()) {
86 0 : if (drivingBuses1.size() == 0) {
87 0 : it1->setName(createName());
88 0 : } else if (drivingBuses1.front().first > it1->getDeparture()) {
89 0 : it1->setName(createName());
90 : } else {
91 0 : it1->setName(drivingBuses1.front().second);
92 : drivingBuses1.pop_front();
93 : }
94 : it1++;
95 : }
96 11 : if (it2 != revBuses.end()) {
97 11 : if (drivingBuses2.size() == 0) {
98 0 : it2->setName(createName());
99 11 : } else if (drivingBuses2.front().first > it2->getDeparture()) {
100 0 : it2->setName(createName());
101 : } else {
102 22 : it2->setName(drivingBuses2.front().second);
103 : drivingBuses2.pop_front();
104 : }
105 : it2++;
106 : }
107 11 : }
108 :
109 : std::string
110 54 : AGBusLine::createName() {
111 54 : ++busNbr; //initialized in setBusNames()
112 54 : std::ostringstream os;
113 54 : os << busNbr;
114 216 : return "bl" + lineNumber + "b" + os.str();
115 54 : }
116 :
117 : int
118 999 : AGBusLine::getReady(int time) {
119 : AGTime current(time);
120 999 : current.addSeconds(maxTripTime);
121 999 : current.addMinutes(PAUSE_TIME);
122 999 : return current.getTime();
123 : }
124 :
125 : int
126 0 : AGBusLine::nbrBuses() {
127 0 : return static_cast<int>(buses.size());
128 : }
129 :
130 : void
131 70 : AGBusLine::locateStation(AGPosition pos) {
132 70 : stations.push_back(pos);
133 70 : }
134 :
135 : void
136 70 : AGBusLine::locateRevStation(AGPosition pos) {
137 70 : revStations.push_back(pos);
138 70 : }
139 :
140 : void
141 30 : AGBusLine::generateBuses(int start, int stop, int rate) {
142 : int t = start;
143 535 : while (t < stop) {
144 505 : buses.push_back(AGBus(t)); //one direction
145 505 : revBuses.push_back(AGBus(t)); //return direction
146 505 : t += rate;
147 : }
148 30 : }
149 :
150 :
151 : void
152 0 : 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 0 : for (it = buses.begin(); it != buses.end(); ++it) {
157 0 : it->print();
158 : }
159 : std::cout << "\n -------------------------- Second way --------------------------\n" << std::endl;
160 0 : for (it = revBuses.begin(); it != revBuses.end(); ++it) {
161 0 : it->print();
162 : }
163 : std::cout << "\n ----------------------------------------------------------------\n" << std::endl;
164 0 : }
165 :
166 :
167 : /****************************************************************************/
|