Eclipse SUMO - Simulation of Urban MObility
AGBusLine.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 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
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 // SPDX-License-Identifier: EPL-2.0
11 /****************************************************************************/
20 // Bus line of the city: contains all the buses of this line
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <iostream>
30 #include <utility>
31 #include <sstream>
32 #include <string>
33 #include <list>
34 #include "AGBusLine.h"
35 #include "AGBus.h"
36 #include "AGPosition.h"
37 #include "AGTime.h"
38 #include <utils/common/StdDefs.h>
39 
40 #define PAUSE_TIME 15 //time (in minutes) a bus waits before going in the opposite direction.
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 void
48  this->maxTripTime = time;
49 }
50 
51 void
53  busNbr = 0;
54  std::list<AGBus>::iterator it1 = buses.begin(); //iterator on buses in the first direction
55  std::list<AGBus>::iterator it2 = revBuses.begin(); //iterator on buses in the second direction
56 
57  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
58 
59  while (it1 != buses.end() && it2 != revBuses.end()) {
60  if (it1->getDeparture() > it2->getDeparture()) {
61  if (drivingBuses2.size() == 0) {
62  drivingBuses2.push_front(make_pair(it2->getDeparture(), createName()));
63  } else if (drivingBuses2.front().first > it2->getDeparture()) {
64  drivingBuses2.push_front(make_pair(it2->getDeparture(), createName()));
65  }
66  //here the first in drivingBuses2 is available for the trip
67  it2->setName(drivingBuses2.front().second);
68  drivingBuses2.pop_front();
69  //the same bus will be available for the main direction after some time (see function getReady):
70  drivingBuses1.push_back(make_pair(getReady(it2->getDeparture()), it2->getName()));
71  it2++;
72  } else {
73  if (drivingBuses1.size() == 0) {
74  drivingBuses1.push_front(make_pair(it1->getDeparture(), createName()));
75  } else if (drivingBuses1.front().first > it1->getDeparture()) {
76  drivingBuses1.push_front(make_pair(it1->getDeparture(), createName()));
77  }
78  //here the first in drivingBuses1 is available for the trip
79  it1->setName(drivingBuses1.front().second);
80  drivingBuses1.pop_front();
81  //the same bus will be available for the return way after some time (see function getReady):
82  drivingBuses2.push_back(make_pair(getReady(it1->getDeparture()), it1->getName()));
83  it1++;
84  }
85  }
86  if (it1 != buses.end()) {
87  if (drivingBuses1.size() == 0) {
88  it1->setName(createName());
89  } else if (drivingBuses1.front().first > it1->getDeparture()) {
90  it1->setName(createName());
91  } else {
92  it1->setName(drivingBuses1.front().second);
93  drivingBuses1.pop_front();
94  }
95  it1++;
96  }
97  if (it2 != revBuses.end()) {
98  if (drivingBuses2.size() == 0) {
99  it2->setName(createName());
100  } else if (drivingBuses2.front().first > it2->getDeparture()) {
101  it2->setName(createName());
102  } else {
103  it2->setName(drivingBuses2.front().second);
104  drivingBuses2.pop_front();
105  }
106  it2++;
107  }
108 }
109 
110 std::string
112  ++busNbr; //initialized in setBusNames()
113  std::ostringstream os;
114  os << busNbr;
115  return "bl" + lineNumber + "b" + os.str();
116 }
117 
118 int
120  AGTime current(time);
121  current.addSeconds(maxTripTime);
122  current.addMinutes(PAUSE_TIME);
123  return current.getTime();
124 }
125 
126 int
128  return static_cast<int>(buses.size());
129 }
130 
131 void
133  stations.push_back(pos);
134 }
135 
136 void
138  revStations.push_back(pos);
139 }
140 
141 void
142 AGBusLine::generateBuses(int start, int stop, int rate) {
143  int t = start;
144  while (t < stop) {
145  buses.push_back(AGBus(t)); //one direction
146  revBuses.push_back(AGBus(t)); //return direction
147  t += rate;
148  }
149 }
150 
151 
152 void
154  std::list<AGBus>::iterator it;
155  std::cout << "\n ----------- BUS LINE " << lineNumber << " PRINTING -------------\n" << std::endl;
156  std::cout << "\n -------------------------- First way ---------------------------\n" << std::endl;
157  for (it = buses.begin(); it != buses.end(); ++it) {
158  it->print();
159  }
160  std::cout << "\n -------------------------- Second way --------------------------\n" << std::endl;
161  for (it = revBuses.begin(); it != revBuses.end(); ++it) {
162  it->print();
163  }
164  std::cout << "\n ----------------------------------------------------------------\n" << std::endl;
165 }
166 
167 /****************************************************************************/
AGTime::addSeconds
void addSeconds(int sec)
addition of seconds to the current moment
Definition: AGTime.cpp:180
AGBusLine::maxTripTime
int maxTripTime
Definition: AGBusLine.h:70
AGTime::getTime
int getTime()
: returns the number of seconds from the beginning of the first day of simulation this includes
Definition: AGTime.cpp:123
AGTime
Definition: AGTime.h:36
AGBusLine.h
PAUSE_TIME
#define PAUSE_TIME
Definition: AGBusLine.cpp:40
AGBusLine::lineNumber
std::string lineNumber
Definition: AGBusLine.h:69
AGBusLine::locateRevStation
void locateRevStation(AGPosition pos)
Definition: AGBusLine.cpp:137
AGBusLine::setBusNames
void setBusNames()
Definition: AGBusLine.cpp:52
AGBusLine::nbrBuses
int nbrBuses()
Definition: AGBusLine.cpp:127
AGTime.h
AGBusLine::revStations
std::list< AGPosition > revStations
Definition: AGBusLine.h:53
AGBusLine::printBuses
void printBuses()
Definition: AGBusLine.cpp:153
AGTime::addMinutes
void addMinutes(int min)
addition of minutes to the current moment
Definition: AGTime.cpp:175
AGBusLine::stations
std::list< AGPosition > stations
Definition: AGBusLine.h:52
AGPosition.h
AGBusLine::setMaxTripTime
void setMaxTripTime(int time)
Definition: AGBusLine.cpp:47
AGBusLine::busNbr
int busNbr
Definition: AGBusLine.h:71
AGPosition
A location in the 2D plane freely positioned on a street.
Definition: AGPosition.h:55
AGBusLine::getReady
int getReady(int time)
Definition: AGBusLine.cpp:119
AGBus
Definition: AGBus.h:35
AGBusLine::locateStation
void locateStation(AGPosition pos)
Definition: AGBusLine.cpp:132
AGBusLine::buses
std::list< AGBus > buses
Definition: AGBusLine.h:54
AGBus.h
config.h
StdDefs.h
AGBusLine::generateBuses
void generateBuses(int start, int stop, int rate)
Definition: AGBusLine.cpp:142
AGBusLine::revBuses
std::list< AGBus > revBuses
Definition: AGBusLine.h:55
AGBusLine::createName
std::string createName()
Definition: AGBusLine.cpp:111