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