Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
AGBusLine.cpp
Go to the documentation of this file.
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/****************************************************************************/
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"
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// ===========================================================================
45void
47 this->maxTripTime = time;
48}
49
50void
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
109std::string
111 ++busNbr; //initialized in setBusNames()
112 std::ostringstream os;
113 os << busNbr;
114 return "bl" + lineNumber + "b" + os.str();
115}
116
117int
119 AGTime current(time);
120 current.addSeconds(maxTripTime);
121 current.addMinutes(PAUSE_TIME);
122 return current.getTime();
123}
124
125int
127 return static_cast<int>(buses.size());
128}
129
130void
132 stations.push_back(pos);
133}
134
135void
137 revStations.push_back(pos);
138}
139
140void
141AGBusLine::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
151void
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/****************************************************************************/
#define PAUSE_TIME
Definition AGBusLine.cpp:39
Definition AGBus.h:34
void locateRevStation(AGPosition pos)
int busNbr
Definition AGBusLine.h:69
void locateStation(AGPosition pos)
int maxTripTime
Definition AGBusLine.h:68
int nbrBuses()
void printBuses()
std::list< AGPosition > stations
Definition AGBusLine.h:50
int getReady(int time)
std::string lineNumber
Definition AGBusLine.h:67
std::list< AGBus > buses
Definition AGBusLine.h:52
void generateBuses(int start, int stop, int rate)
void setBusNames()
Definition AGBusLine.cpp:51
std::string createName()
std::list< AGBus > revBuses
Definition AGBusLine.h:53
std::list< AGPosition > revStations
Definition AGBusLine.h:51
void setMaxTripTime(int time)
Definition AGBusLine.cpp:46
A location in the 2D plane freely positioned on a street.
Definition AGPosition.h:53
int getTime()
: returns the number of seconds from the beginning of the first day of simulation this includes
Definition AGTime.cpp:122
void addMinutes(int min)
addition of minutes to the current moment
Definition AGTime.cpp:174
void addSeconds(int sec)
addition of seconds to the current moment
Definition AGTime.cpp:179