SUMO - Simulation of Urban MObility
IntermodalRouter.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 /****************************************************************************/
16 // The IntermodalRouter builds a special network and (delegates to a SUMOAbstractRouter)
17 /****************************************************************************/
18 #ifndef IntermodalRouter_h
19 #define IntermodalRouter_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <vector>
29 #include <algorithm>
30 #include <assert.h>
32 #include <utils/common/SUMOTime.h>
33 #include <utils/common/ToString.h>
35 #include "SUMOAbstractRouter.h"
36 #include "DijkstraRouter.h"
37 #include "AStarRouter.h"
38 #include "IntermodalNetwork.h"
39 #include "EffortCalculator.h"
40 #include "CarEdge.h"
41 #include "StopEdge.h"
42 #include "PedestrianRouter.h"
43 
44 //#define IntermodalRouter_DEBUG_ROUTES
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
54 template<class E, class L, class N, class V>
55 class IntermodalRouter : public SUMOAbstractRouter<E, IntermodalTrip<E, N, V> > {
56 public:
58 
59 private:
64  typedef DijkstraRouter<IntermodalEdge<E, L, N, V>, IntermodalTrip<E, N, V>, SUMOAbstractRouterPermissions<IntermodalEdge<E, L, N, V>, IntermodalTrip<E, N, V> > > _InternalDijkstra;
65  typedef AStarRouter<IntermodalEdge<E, L, N, V>, IntermodalTrip<E, N, V>, SUMOAbstractRouterPermissions<IntermodalEdge<E, L, N, V>, IntermodalTrip<E, N, V> > > _InternalAStar;
66 
67 public:
68  struct TripItem {
69  TripItem(const std::string& _line = "") :
70  line(_line), intended(_line), depart(-1), traveltime(0.), cost(0.) {}
71  std::string line;
72  std::string destStop;
73  std::string intended; // intended public transport vehicle id
74  double depart; // intended public transport departure
75  std::vector<const E*> edges;
76  double traveltime;
77  double cost;
78  double departPos;
79  double arrivalPos;
80  std::string description;
81  };
82 
84  IntermodalRouter(CreateNetCallback callback, const int carWalkTransfer, const std::string& routingAlgorithm,
85  const int routingMode = 0, EffortCalculator* calc = nullptr) :
86  SUMOAbstractRouter<E, _IntermodalTrip>("IntermodalRouter"),
87  myAmClone(false), myInternalRouter(nullptr), myIntermodalNet(nullptr),
88  myCallback(callback), myCarWalkTransfer(carWalkTransfer), myRoutingAlgorithm(routingAlgorithm),
89  myRoutingMode(routingMode), myExternalEffort(calc) {
90  }
91 
93  virtual ~IntermodalRouter() {
94  delete myInternalRouter;
95  if (!myAmClone) {
96  delete myIntermodalNet;
97  }
98  }
99 
101  createNet();
103  }
104 
107  bool compute(const E* from, const E* to, const double departPos, const double arrivalPos,
108  const std::string stopID, const double speed,
109  const V* const vehicle, const SVCPermissions modeSet, const SUMOTime msTime,
110  std::vector<TripItem>& into, const double externalFactor = 0.) {
111  createNet();
112  _IntermodalTrip trip(from, to, departPos, arrivalPos, speed, msTime, 0, vehicle, modeSet, myExternalEffort, externalFactor);
113  std::vector<const _IntermodalEdge*> intoEdges;
114  const bool success = myInternalRouter->compute(myIntermodalNet->getDepartEdge(from, trip.departPos),
115  stopID != "" ? myIntermodalNet->getStopEdge(stopID) : myIntermodalNet->getArrivalEdge(to, trip.arrivalPos),
116  &trip, msTime, intoEdges);
117  if (success) {
118  std::string lastLine = "";
119  double time = STEPS2TIME(msTime);
120  double effort = 0.;
121  const _IntermodalEdge* prev = nullptr;
122  for (const _IntermodalEdge* iEdge : intoEdges) {
123  if (iEdge->includeInRoute(false)) {
124  if (iEdge->getLine() == "!stop") {
125  into.back().destStop = iEdge->getID();
126  if (lastLine == "!ped") {
127  lastLine = ""; // a stop always starts a new trip item
128  }
129  } else {
130  if (iEdge->getLine() != lastLine) {
131  lastLine = iEdge->getLine();
132  if (lastLine == "!car") {
133  into.push_back(TripItem(vehicle->getID()));
134  } else if (lastLine == "!ped") {
135  into.push_back(TripItem());
136  } else {
137  into.push_back(TripItem(lastLine));
138  into.back().depart = iEdge->getIntended(time, into.back().intended);
139  }
140  into.back().departPos = iEdge->getStartPos();
141  }
142  if (into.back().edges.empty() || into.back().edges.back() != iEdge->getEdge()) {
143  into.back().edges.push_back(iEdge->getEdge());
144  into.back().arrivalPos = iEdge->getEndPos();
145  }
146  }
147  }
148  const double prevTime = time, prevEffort = effort;
149  myInternalRouter->updateViaCost(prev, iEdge, &trip, time, effort);
150  prev = iEdge;
151  if (!into.empty()) {
152  into.back().traveltime += time - prevTime;
153  into.back().cost += effort - prevEffort;
154  }
155  }
156  }
157 #ifdef IntermodalRouter_DEBUG_ROUTES
158  double time = STEPS2TIME(msTime);
159  for (const _IntermodalEdge* iEdge : intoEdges) {
160  const double edgeEffort = myInternalRouter->getEffort(iEdge, &trip, time);
161  time += edgeEffort;
162  std::cout << iEdge->getID() << "(" << iEdge->getLine() << "): " << edgeEffort << std::endl;
163  }
164  std::cout << TIME2STEPS(msTime) << " trip from " << from->getID() << " to " << (to != nullptr ? to->getID() : stopID)
165  << " departPos=" << trip.departPos
166  << " arrivalPos=" << trip.arrivalPos
167  << " edges=" << toString(intoEdges)
168 // << " resultEdges=" << toString(into)
169  << " time=" << time
170  << "\n";
171 #endif
172  return success;
173  }
174 
177  bool compute(const E*, const E*, const _IntermodalTrip* const,
178  SUMOTime, std::vector<const E*>&) {
179  throw ProcessError("Do not use this method");
180  }
181 
182  void prohibit(const std::vector<E*>& toProhibit) {
183  createNet();
184  std::vector<_IntermodalEdge*> toProhibitPE;
185  for (typename std::vector<E*>::const_iterator it = toProhibit.begin(); it != toProhibit.end(); ++it) {
186  toProhibitPE.push_back(myIntermodalNet->getBothDirections(*it).first);
187  toProhibitPE.push_back(myIntermodalNet->getBothDirections(*it).second);
188  toProhibitPE.push_back(myIntermodalNet->getCarEdge(*it));
189  }
190  myInternalRouter->prohibit(toProhibitPE);
191  }
192 
194  createNet();
195  for (_IntermodalEdge* e : myIntermodalNet->getAllEdges()) {
196  dev.openTag(SUMO_TAG_EDGE);
197  dev.writeAttr(SUMO_ATTR_ID, e->getID());
198  dev.writeAttr(SUMO_ATTR_LINE, e->getLine());
199  dev.writeAttr(SUMO_ATTR_LENGTH, e->getLength());
200  dev.writeAttr("successors", toString(e->getSuccessors(SVC_IGNORING)));
201  dev.closeTag();
202  }
203  }
204 
206  createNet();
207  _IntermodalTrip trip(nullptr, nullptr, 0., 0., DEFAULT_PEDESTRIAN_SPEED, 0, 0, nullptr, SVC_PASSENGER | SVC_BICYCLE | SVC_BUS);
208  for (_IntermodalEdge* e : myIntermodalNet->getAllEdges()) {
209  dev.openTag(SUMO_TAG_EDGE);
210  dev.writeAttr(SUMO_ATTR_ID, e->getID());
211  dev.writeAttr("traveltime", e->getTravelTime(&trip, 0.));
212  dev.writeAttr("effort", e->getEffort(&trip, 0.));
213  dev.closeTag();
214  }
215  }
216 
217  Network* getNetwork() const {
218  return myIntermodalNet;
219  }
220 
221 private:
222  IntermodalRouter(Network* net, const int carWalkTransfer, const std::string& routingAlgorithm,
223  const int routingMode, EffortCalculator* calc) :
224  SUMOAbstractRouter<E, _IntermodalTrip>("IntermodalRouterClone"), myAmClone(true),
225  myInternalRouter(new _InternalDijkstra(net->getAllEdges(), true, &_IntermodalEdge::getTravelTimeStatic)),
226  myIntermodalNet(net), myCarWalkTransfer(carWalkTransfer), myRoutingAlgorithm(routingAlgorithm), myRoutingMode(routingMode), myExternalEffort(calc) {}
227 
228  static inline double getEffortAggregated(const _IntermodalEdge* const edge, const _IntermodalTrip* const trip, double time) {
229  return edge == nullptr || !edge->hasEffort() ? 0. : edge->getEffort(trip, time);
230  }
231 
232  static inline double getCombined(const _IntermodalEdge* const edge, const _IntermodalTrip* const trip, double time) {
233  return edge->getTravelTime(trip, time) + trip->externalFactor * trip->calc->getEffort(edge->getNumericalID());
234  }
235 
236  inline void createNet() {
237  if (myIntermodalNet == nullptr) {
238  myIntermodalNet = new Network(E::getAllEdges(), false, myCarWalkTransfer);
239  myIntermodalNet->addCarEdges(E::getAllEdges());
240  myCallback(*this);
241  switch (myRoutingMode) {
242  case 0:
243  if (myRoutingAlgorithm == "astar") {
245  } else {
247  }
248  break;
249  case 1:
251  break;
252  case 2:
254  break;
255  case 3:
256  if (myExternalEffort != nullptr) {
257  std::vector<std::string> edgeIDs;
258  for (const auto e : myIntermodalNet->getAllEdges()) {
259  edgeIDs.push_back(e->getID());
260  }
261  myExternalEffort->init(edgeIDs);
262  }
264  break;
265  }
266  }
267  }
268 
269 private:
270  const bool myAmClone;
271  _InternalRouter* myInternalRouter;
272  Network* myIntermodalNet;
274  const int myCarWalkTransfer;
275  const std::string myRoutingAlgorithm;
276  const int myRoutingMode;
278 
279 
280 private:
283 
284 };
285 
286 
287 #endif
288 
289 /****************************************************************************/
bool compute(const E *from, const E *to, const double departPos, const double arrivalPos, const std::string stopID, const double speed, const V *const vehicle, const SVCPermissions modeSet, const SUMOTime msTime, std::vector< TripItem > &into, const double externalFactor=0.)
Builds the route between the given edges using the minimum effort at the given time The definition of...
void updateViaCost(const E *const prev, const E *const e, const V *const v, double &time, double &effort) const
DijkstraRouter< IntermodalEdge< E, L, N, V >, IntermodalTrip< E, N, V >, SUMOAbstractRouterPermissions< IntermodalEdge< E, L, N, V >, IntermodalTrip< E, N, V > > > _InternalDijkstra
void writeWeights(OutputDevice &dev)
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
double getEffort(const E *const e, const V *const v, double t) const
const EdgePair & getBothDirections(const E *e) const
Returns the pair of forward and backward edge.
static double getEffortAggregated(const _IntermodalEdge *const edge, const _IntermodalTrip *const trip, double time)
static double getEffortStatic(const IntermodalEdge *const edge, const IntermodalTrip< E, N, V > *const trip, double time)
IntermodalNetwork< E, L, N, V > Network
_IntermodalEdge * getArrivalEdge(const E *e, const double pos) const
Returns the arriving intermodal edge.
long long int SUMOTime
Definition: SUMOTime.h:36
virtual ~IntermodalRouter()
Destructor.
static double getCombined(const _IntermodalEdge *const edge, const _IntermodalTrip *const trip, double time)
void writeNetwork(OutputDevice &dev)
const std::vector< _IntermodalEdge * > & getAllEdges()
static double getTravelTimeStatic(const IntermodalEdge *const edge, const IntermodalTrip< E, N, V > *const trip, double time)
IntermodalRouter(Network *net, const int carWalkTransfer, const std::string &routingAlgorithm, const int routingMode, EffortCalculator *calc)
virtual void init(const std::vector< std::string > &edges)=0
vehicle is a bicycle
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
_IntermodalEdge * getStopEdge(const std::string &stopId) const
Returns the associated stop edge.
int getNumericalID() const
Computes the shortest path through a network using the A* algorithm.
Definition: AStarRouter.h:77
virtual bool compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E *> &into)=0
Builds the route between the given edges using the minimum effort at the given time The definition of...
EffortCalculator *const myExternalEffort
SUMOAbstractRouterPermissions< IntermodalEdge< E, L, N, V >, IntermodalTrip< E, N, V > > _InternalRouter
void prohibit(const std::vector< E *> &toProhibit)
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
const EffortCalculator *const calc
TripItem(const std::string &_line="")
void prohibit(const std::vector< E *> &toProhibit)
const std::string myRoutingAlgorithm
the effort calculator interface
IntermodalEdge< E, L, N, V > _IntermodalEdge
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
Computes the shortest path through a network using the Dijkstra algorithm.
virtual double getEffort(const int edge) const =0
#define STEPS2TIME(x)
Definition: SUMOTime.h:58
const double departPos
IntermodalRouter(CreateNetCallback callback, const int carWalkTransfer, const std::string &routingAlgorithm, const int routingMode=0, EffortCalculator *calc=nullptr)
Constructor.
the intermodal network storing edges, connections and the mappings to the "real" edges ...
vehicle is a passenger car (a "normal" car)
const double arrivalPos
begin/end of the description of an edge
the base edge type that is given to the internal router (SUMOAbstractRouter)
_IntermodalEdge * getDepartEdge(const E *e, const double pos) const
Returns the departing intermodal edge.
vehicle is a bus
virtual double getEffort(const IntermodalTrip< E, N, V > *const, double) const
CreateNetCallback myCallback
const int myCarWalkTransfer
const double DEFAULT_PEDESTRIAN_SPEED
void(* CreateNetCallback)(IntermodalRouter< E, L, N, V > &)
std::vector< const E * > edges
bool compute(const E *, const E *, const _IntermodalTrip *const, SUMOTime, std::vector< const E *> &)
Builds the route between the given edges using the minimum effort at the given time The definition of...
Network * myIntermodalNet
void addCarEdges(const std::vector< E *> &edges)
const double externalFactor
SUMOAbstractRouter< E, _IntermodalTrip > * clone()
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
_IntermodalEdge * getCarEdge(const E *e) const
Returns the associated car edge.
AStarRouter< IntermodalEdge< E, L, N, V >, IntermodalTrip< E, N, V >, SUMOAbstractRouterPermissions< IntermodalEdge< E, L, N, V >, IntermodalTrip< E, N, V > > > _InternalAStar
IntermodalTrip< E, N, V > _IntermodalTrip
IntermodalRouter & operator=(const IntermodalRouter &s)
Invalidated assignment operator.
_InternalRouter * myInternalRouter
virtual bool hasEffort() const
the "vehicle" type that is given to the internal router (SUMOAbstractRouter)
vehicles ignoring classes
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Network * getNetwork() const
virtual double getTravelTime(const IntermodalTrip< E, N, V > *const, double) const