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:
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 vType;
73  std::string destStop;
74  std::string intended; // intended public transport vehicle id
75  double depart; // intended public transport departure
76  std::vector<const E*> edges;
77  double traveltime;
78  double cost;
79  double length;
80  double departPos;
81  double arrivalPos;
82  std::string description;
83  };
84 
86  IntermodalRouter(CreateNetCallback callback, const int carWalkTransfer, const std::string& routingAlgorithm,
87  const int routingMode = 0, EffortCalculator* calc = nullptr) :
88  SUMOAbstractRouter<E, _IntermodalTrip>("IntermodalRouter"),
89  myAmClone(false), myInternalRouter(nullptr), myIntermodalNet(nullptr),
90  myCallback(callback), myCarWalkTransfer(carWalkTransfer), myRoutingAlgorithm(routingAlgorithm),
91  myRoutingMode(routingMode), myExternalEffort(calc) {
92  }
93 
95  virtual ~IntermodalRouter() {
96  delete myInternalRouter;
97  if (!myAmClone) {
98  delete myIntermodalNet;
99  }
100  }
101 
103  createNet();
105  }
106 
109  bool compute(const E* from, const E* to, const double departPos, const double arrivalPos,
110  const std::string stopID, const double speed,
111  const V* const vehicle, const SVCPermissions modeSet, const SUMOTime msTime,
112  std::vector<TripItem>& into, const double externalFactor = 0.) {
113  createNet();
114  _IntermodalTrip trip(from, to, departPos, arrivalPos, speed, msTime, 0, vehicle, modeSet, myExternalEffort, externalFactor);
115  std::vector<const _IntermodalEdge*> intoEdges;
116  const bool success = myInternalRouter->compute(myIntermodalNet->getDepartEdge(from, trip.departPos),
117  stopID != "" ? myIntermodalNet->getStopEdge(stopID) : myIntermodalNet->getArrivalEdge(to, trip.arrivalPos),
118  &trip, msTime, intoEdges);
119  if (success) {
120  std::string lastLine = "";
121  double time = STEPS2TIME(msTime);
122  double effort = 0.;
123  double length = 0.;
124  const _IntermodalEdge* prev = nullptr;
125  for (const _IntermodalEdge* iEdge : intoEdges) {
126  if (iEdge->includeInRoute(false)) {
127  if (iEdge->getLine() == "!stop") {
128  into.back().destStop = iEdge->getID();
129  if (lastLine == "!ped") {
130  lastLine = ""; // a stop always starts a new trip item
131  }
132  } else {
133  if (iEdge->getLine() != lastLine) {
134  lastLine = iEdge->getLine();
135  if (lastLine == "!car") {
136  into.push_back(TripItem(vehicle->getID()));
137  into.back().vType = vehicle->getParameter().vtypeid;
138  } else if (lastLine == "!ped") {
139  into.push_back(TripItem());
140  } else {
141  into.push_back(TripItem(lastLine));
142  into.back().depart = iEdge->getIntended(time, into.back().intended);
143  }
144  into.back().departPos = iEdge->getStartPos();
145  }
146  if (into.back().edges.empty() || into.back().edges.back() != iEdge->getEdge()) {
147  into.back().edges.push_back(iEdge->getEdge());
148  into.back().arrivalPos = iEdge->getEndPos();
149  }
150  }
151  }
152  const double prevTime = time, prevEffort = effort, prevLength = length;
153  myInternalRouter->updateViaCost(prev, iEdge, &trip, time, effort, length);
154  prev = iEdge;
155  if (!into.empty()) {
156  into.back().traveltime += time - prevTime;
157  into.back().cost += effort - prevEffort;
158  into.back().length += length - prevLength;
159  }
160  }
161  }
162 #ifdef IntermodalRouter_DEBUG_ROUTES
163  double time = STEPS2TIME(msTime);
164  for (const _IntermodalEdge* iEdge : intoEdges) {
165  const double edgeEffort = myInternalRouter->getEffort(iEdge, &trip, time);
166  time += edgeEffort;
167  std::cout << iEdge->getID() << "(" << iEdge->getLine() << "): " << edgeEffort << std::endl;
168  }
169  std::cout << TIME2STEPS(msTime) << " trip from " << from->getID() << " to " << (to != nullptr ? to->getID() : stopID)
170  << " departPos=" << trip.departPos
171  << " arrivalPos=" << trip.arrivalPos
172  << " edges=" << toString(intoEdges)
173 // << " resultEdges=" << toString(into)
174  << " time=" << time
175  << "\n";
176 #endif
177  return success;
178  }
179 
182  bool compute(const E*, const E*, const _IntermodalTrip* const,
183  SUMOTime, std::vector<const E*>&) {
184  throw ProcessError("Do not use this method");
185  }
186 
187  void prohibit(const std::vector<E*>& toProhibit) {
188  createNet();
189  std::vector<_IntermodalEdge*> toProhibitPE;
190  for (typename std::vector<E*>::const_iterator it = toProhibit.begin(); it != toProhibit.end(); ++it) {
191  toProhibitPE.push_back(myIntermodalNet->getBothDirections(*it).first);
192  toProhibitPE.push_back(myIntermodalNet->getBothDirections(*it).second);
193  toProhibitPE.push_back(myIntermodalNet->getCarEdge(*it));
194  }
195  myInternalRouter->prohibit(toProhibitPE);
196  }
197 
199  createNet();
200  for (_IntermodalEdge* e : myIntermodalNet->getAllEdges()) {
201  dev.openTag(SUMO_TAG_EDGE);
202  dev.writeAttr(SUMO_ATTR_ID, e->getID());
203  dev.writeAttr(SUMO_ATTR_LINE, e->getLine());
204  dev.writeAttr(SUMO_ATTR_LENGTH, e->getLength());
205  dev.writeAttr("successors", toString(e->getSuccessors(SVC_IGNORING)));
206  dev.closeTag();
207  }
208  }
209 
211  createNet();
212  _IntermodalTrip trip(nullptr, nullptr, 0., 0., DEFAULT_PEDESTRIAN_SPEED, 0, 0, nullptr, SVC_PASSENGER | SVC_BICYCLE | SVC_BUS);
213  for (_IntermodalEdge* e : myIntermodalNet->getAllEdges()) {
214  dev.openTag(SUMO_TAG_EDGE);
215  dev.writeAttr(SUMO_ATTR_ID, e->getID());
216  dev.writeAttr("traveltime", e->getTravelTime(&trip, 0.));
217  dev.writeAttr("effort", e->getEffort(&trip, 0.));
218  dev.closeTag();
219  }
220  }
221 
222  Network* getNetwork() const {
223  return myIntermodalNet;
224  }
225 
226 private:
227  IntermodalRouter(Network* net, const int carWalkTransfer, const std::string& routingAlgorithm,
228  const int routingMode, EffortCalculator* calc) :
229  SUMOAbstractRouter<E, _IntermodalTrip>("IntermodalRouterClone"), myAmClone(true),
230  myInternalRouter(new _InternalDijkstra(net->getAllEdges(), true, &_IntermodalEdge::getTravelTimeStatic)),
231  myIntermodalNet(net), myCarWalkTransfer(carWalkTransfer), myRoutingAlgorithm(routingAlgorithm), myRoutingMode(routingMode), myExternalEffort(calc) {}
232 
233  static inline double getEffortAggregated(const _IntermodalEdge* const edge, const _IntermodalTrip* const trip, double time) {
234  return edge == nullptr || !edge->hasEffort() ? 0. : edge->getEffort(trip, time);
235  }
236 
237  static inline double getCombined(const _IntermodalEdge* const edge, const _IntermodalTrip* const trip, double time) {
238  return edge->getTravelTime(trip, time) + trip->externalFactor * trip->calc->getEffort(edge->getNumericalID());
239  }
240 
241  inline void createNet() {
242  if (myIntermodalNet == nullptr) {
243  myIntermodalNet = new Network(E::getAllEdges(), false, myCarWalkTransfer);
244  myIntermodalNet->addCarEdges(E::getAllEdges());
245  myCallback(*this);
246  switch (myRoutingMode) {
247  case 0:
248  if (myRoutingAlgorithm == "astar") {
250  } else {
252  }
253  break;
254  case 1:
256  break;
257  case 2:
259  break;
260  case 3:
261  if (myExternalEffort != nullptr) {
262  std::vector<std::string> edgeIDs;
263  for (const auto e : myIntermodalNet->getAllEdges()) {
264  edgeIDs.push_back(e->getID());
265  }
266  myExternalEffort->init(edgeIDs);
267  }
269  break;
270  }
271  }
272  }
273 
274 private:
275  const bool myAmClone;
276  _InternalRouter* myInternalRouter;
277  Network* myIntermodalNet;
279  const int myCarWalkTransfer;
280  const std::string myRoutingAlgorithm;
281  const int myRoutingMode;
283 
284 
285 private:
288 
289 };
290 
291 
292 #endif
293 
294 /****************************************************************************/
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 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:78
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
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)
void updateViaCost(const E *const prev, const E *const e, const V *const v, double &time, double &effort, double &length) const
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
DijkstraRouter< _IntermodalEdge, _IntermodalTrip, _InternalRouter > _InternalDijkstra
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)
SUMOAbstractRouterPermissions< _IntermodalEdge, _IntermodalTrip > _InternalRouter
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, _IntermodalTrip, _InternalRouter > _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