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