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