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