SUMO - Simulation of Urban MObility
PublicTransportEdge.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // The PublicTransportEdge is a special intermodal edge connecting the stop edges with scheduled traffic
16 /****************************************************************************/
17 #ifndef PublicTransportEdge_h
18 #define PublicTransportEdge_h
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include "IntermodalEdge.h"
27 
28 
29 // ===========================================================================
30 // class definitions
31 // ===========================================================================
33 template<class E, class L, class N, class V>
34 class PublicTransportEdge : public IntermodalEdge<E, L, N, V> {
35 private:
36  struct Schedule {
37  Schedule(const std::string& _id, const SUMOTime _begin, const int _repetitionNumber, const SUMOTime _period, const SUMOTime _travelTime)
38  : ids( {
39  _id
40  }), begin(_begin), repetitionNumber(_repetitionNumber), period(_period), travelTime(_travelTime) {}
41  // the id of the vehicle or flow from which this schedule is generated
42  std::vector<std::string> ids;
43  const SUMOTime begin;
45  // the repetition period for a flow or -1 for a vehicle
48  private:
50  Schedule& operator=(const Schedule& src);
51  };
52 
53 public:
54  PublicTransportEdge(const std::string id, int numericalID, const IntermodalEdge<E, L, N, V>* entryStop, const E* endEdge, const std::string& line, const double length) :
55  IntermodalEdge<E, L, N, V>(line + ":" + (id != "" ? id : endEdge->getID()), numericalID, endEdge, line, length), myEntryStop(entryStop) { }
56 
57  bool includeInRoute(bool /* allEdges */) const {
58  return true;
59  }
60 
61  bool prohibits(const IntermodalTrip<E, N, V>* const trip) const {
62  return (trip->modeSet & SVC_BUS) == 0;
63  }
64 
66  return myEntryStop;
67  }
68 
69  bool hasSchedule(const SUMOTime begin) const {
70  return mySchedules.find(begin) != mySchedules.end();
71  }
72 
73  void addSchedule(const std::string id, const SUMOTime begin, const int repetitionNumber, const SUMOTime period, const SUMOTime travelTime) {
74  // try to merge with existing vehicle or flow
75  bool found = false;
76  for (auto& it : mySchedules) {
77  Schedule& s = it.second;
78  if (travelTime == s.travelTime) {
79  if (repetitionNumber == -1 && s.repetitionNumber == 1) {
80  if (begin > s.begin) {
81  s.period = begin - s.begin;
82  found = true;
83  }
84  } else if (begin == s.begin + s.repetitionNumber * s.period) {
85  found = true;
86  }
87  if (found) {
88  s.repetitionNumber += MAX2(repetitionNumber, 1);
89  s.ids.push_back(id);
90  break;
91  }
92  }
93  }
94  if (!found) {
95  mySchedules.insert(std::make_pair(begin, Schedule(id, begin, MAX2(repetitionNumber, 1), MAX2<SUMOTime>(period, 1), travelTime)));
96  }
97  }
98 
99  double getTravelTime(const IntermodalTrip<E, N, V>* const /* trip */, double time) const {
100  SUMOTime minArrival = SUMOTime_MAX;
101  const SUMOTime step = TIME2STEPS(time);
102  for (typename std::multimap<SUMOTime, Schedule>::const_iterator it = mySchedules.begin(); it != mySchedules.end(); ++it) {
103  const Schedule& s = it->second;
104  if (it->first > minArrival) {
105  break;
106  }
107  const SUMOTime offset = MAX2<SUMOTime>(0, step - s.begin);
108  int running = (int)(offset / s.period);
109  if (offset % s.period != 0) {
110  running++;
111  }
112  if (running < s.repetitionNumber) {
113  const SUMOTime nextDepart = s.begin + running * s.period;
114  minArrival = MIN2(nextDepart + s.travelTime, minArrival);
115  //std::cout << " edge=" << myEntryStop->getID() << "->" << this->getID() << " beg=" << s.begin << " end=" << s.end
116  // << " atTime=" << time
117  // << " running=" << running << " nextDepart=" << nextDepart
118  // << " minASec=" << minArrivalSec << " travelTime=" << minArrivalSec - time << "\n";
119  }
120  }
121  return STEPS2TIME(minArrival - step);
122  }
123 
124  double getIntended(const double time, std::string& intended) const {
126  SUMOTime minArrival = SUMOTime_MAX;
127  double bestDepartTime = std::numeric_limits<double>::max();
128  const SUMOTime step = TIME2STEPS(time);
129  for (typename std::multimap<SUMOTime, Schedule>::const_iterator it = mySchedules.begin(); it != mySchedules.end(); ++it) {
130  const Schedule& s = it->second;
131  if (it->first > minArrival) {
132  break;
133  }
134  const SUMOTime offset = MAX2<SUMOTime>(0, step - s.begin);
135  int running = (int)(offset / s.period);
136  if (offset % s.period != 0) {
137  running++;
138  }
139  if (running < s.repetitionNumber) {
140  const SUMOTime nextDepart = s.begin + running * s.period;
141  if (nextDepart + s.travelTime < minArrival) {
142  minArrival = nextDepart + s.travelTime;
143  bestDepartTime = STEPS2TIME(nextDepart);
144  // see naming scheme inMSInsertionControl::determineCandidates()
145  if (s.ids.size() == 1 || running >= (int)s.ids.size()) {
146  intended = s.repetitionNumber == 1 ? s.ids[0] : s.ids[0] + "." + toString(running);
147  } else {
148  intended = s.ids[running];
149  }
150  }
151  }
152  }
153  return bestDepartTime;
154  }
155 
156 private:
157  std::multimap<SUMOTime, Schedule> mySchedules;
159 
160 };
161 
162 
163 #endif
164 
165 /****************************************************************************/
long long int SUMOTime
Definition: SUMOTime.h:36
double getTravelTime(const IntermodalTrip< E, N, V > *const, double time) const
T MAX2(T a, T b)
Definition: StdDefs.h:76
bool hasSchedule(const SUMOTime begin) const
const IntermodalEdge< E, L, N, V > * getEntryStop() const
const std::string & getID() const
Returns the id.
Definition: Named.h:78
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
std::multimap< SUMOTime, Schedule > mySchedules
Schedule(const std::string &_id, const SUMOTime _begin, const int _repetitionNumber, const SUMOTime _period, const SUMOTime _travelTime)
std::vector< std::string > ids
const IntermodalEdge< E, L, N, V > *const myEntryStop
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
void addSchedule(const std::string id, const SUMOTime begin, const int repetitionNumber, const SUMOTime period, const SUMOTime travelTime)
#define STEPS2TIME(x)
Definition: SUMOTime.h:58
T MIN2(T a, T b)
Definition: StdDefs.h:70
the base edge type that is given to the internal router (SUMOAbstractRouter)
Schedule & operator=(const Schedule &src)
Invalidated assignment operator.
vehicle is a bus
const SVCPermissions modeSet
#define SUMOTime_MAX
Definition: SUMOTime.h:37
the public transport edge type connecting the stop edges
double getIntended(const double time, std::string &intended) const
get intended vehicle id and departure time of next public transport ride
bool includeInRoute(bool) const
bool prohibits(const IntermodalTrip< E, N, V > *const trip) const
the "vehicle" type that is given to the internal router (SUMOAbstractRouter)
PublicTransportEdge(const std::string id, int numericalID, const IntermodalEdge< E, L, N, V > *entryStop, const E *endEdge, const std::string &line, const double length)