SUMO - Simulation of Urban MObility
CHRouter.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Shortest Path search using a Contraction Hierarchy
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef CHRouter_h
23 #define CHRouter_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
36 #include <functional>
37 #include <vector>
38 #include <set>
39 #include <limits>
40 #include <algorithm>
41 #include <iterator>
42 #include <utils/common/SysUtils.h>
44 #include <utils/common/StdDefs.h>
46 #include "SPTree.h"
47 
48 //#define CHRouter_DEBUG_QUERY
49 //#define CHRouter_DEBUG_QUERY_PERF
50 //#define CHRouter_DEBUG_CONTRACTION
51 //#define CHRouter_DEBUG_CONTRACTION_WITNESSES
52 //#define CHRouter_DEBUG_CONTRACTION_QUEUE
53 //#define CHRouter_DEBUG_CONTRACTION_DEGREE
54 //#define CHRouter_DEBUG_WEIGHTS
55 
56 // ===========================================================================
57 // class definitions
58 // ===========================================================================
73 template<class E, class V, class PF>
74 class CHRouter: public SUMOAbstractRouter<E, V>, public PF {
75 
76 public:
77  class EdgeInfo;
78 
80  typedef SUMOReal(* Operation)(const E* const, const V* const, SUMOReal);
81 
83  typedef std::pair<const EdgeInfo*, const EdgeInfo*> Meeting;
84 
86  typedef std::set<const E*> EdgeSet;
87 
89  typedef std::vector<const E*> Result;
90 
92  // forward connections are used only in forward search
93  // backward connections are used only in backwards search
94  class Connection {
95  public:
100  };
101 
107  class EdgeInfo {
108  public:
110  EdgeInfo(size_t id) :
111  edge(E::dictionary(id)),
112  traveltime(std::numeric_limits<SUMOReal>::max()),
113  prev(0),
114  visited(false)
115  {}
116 
118  const E* edge;
119 
122 
125 
127  bool visited;
128 
130  std::vector<Connection> upward;
131 
133  int rank;
134 
135  inline void reset() {
136  traveltime = std::numeric_limits<SUMOReal>::max();
137  visited = false;
138  }
139  };
140 
141 
146  class Unidirectional: public PF {
147  public:
149  Unidirectional(size_t numEdges, bool forward):
150  myAmForward(forward),
151  myVehicle(0) {
152  for (size_t i = 0; i < numEdges; i++) {
153  myEdgeInfos.push_back(EdgeInfo(i));
154  }
155  }
156 
157  inline bool found(const E* edge) const {
158  return myFound.count(edge) > 0;
159  }
160 
161  inline EdgeInfo* getEdgeInfo(const E* const edge) {
162  return &(myEdgeInfos[edge->getNumericalID()]);
163  }
164 
165  inline const EdgeInfo* getEdgeInfo(const E* const edge) const {
166  return &(myEdgeInfos[edge->getNumericalID()]);
167  }
168 
174  public:
176  bool operator()(const EdgeInfo* nod1, const EdgeInfo* nod2) const {
177  if (nod1->traveltime == nod2->traveltime) {
178  return nod1->edge->getNumericalID() > nod2->edge->getNumericalID();
179  }
180  return nod1->traveltime > nod2->traveltime;
181  }
182  };
183 
184 
185  void init(const E* const start, const V* const vehicle) {
186  assert(vehicle != 0);
187  // all EdgeInfos touched in the previous query are either in myFrontier or myFound: clean those up
188  for (typename std::vector<EdgeInfo*>::iterator i = myFrontier.begin(); i != myFrontier.end(); i++) {
189  (*i)->reset();
190  }
191  myFrontier.clear();
192  for (typename EdgeSet::iterator i = myFound.begin(); i != myFound.end(); i++) {
193  getEdgeInfo(*i)->reset();
194  }
195  myFound.clear();
196  myVehicle = vehicle;
197  EdgeInfo* startInfo = getEdgeInfo(start);
198  startInfo->traveltime = 0;
199  startInfo->prev = 0;
200  myFrontier.push_back(startInfo);
201  }
202 
203 
208  bool step(const Unidirectional& otherSearch, SUMOReal& minTTSeen, Meeting& meeting) {
209  // pop the node with the minimal length
210  EdgeInfo* const minimumInfo = myFrontier.front();
211  pop_heap(myFrontier.begin(), myFrontier.end(), myComparator);
212  myFrontier.pop_back();
213  // check for a meeting with the other search
214  const E* const minEdge = minimumInfo->edge;
215 #ifdef CHRouter_DEBUG_QUERY
216  std::cout << "DEBUG: " << (myAmForward ? "Forward" : "Backward") << " hit '" << minEdge->getID() << "' Q: ";
217  for (typename std::vector<EdgeInfo*>::iterator it = myFrontier.begin(); it != myFrontier.end(); it++) {
218  std::cout << (*it)->traveltime << "," << (*it)->edge->getID() << " ";
219  }
220  std::cout << "\n";
221 #endif
222  if (otherSearch.found(minEdge)) {
223  const EdgeInfo* const otherInfo = otherSearch.getEdgeInfo(minEdge);
224  const SUMOReal ttSeen = minimumInfo->traveltime + otherInfo->traveltime;
225 #ifdef CHRouter_DEBUG_QUERY
226  std::cout << "DEBUG: " << (myAmForward ? "Forward" : "Backward") << "-Search hit other search at '" << minEdge->getID() << "', tt: " << ttSeen << " \n";
227 #endif
228  if (ttSeen < minTTSeen) {
229  minTTSeen = ttSeen;
230  if (myAmForward) {
231  meeting.first = minimumInfo;
232  meeting.second = otherInfo;
233  } else {
234  meeting.first = otherInfo;
235  meeting.second = minimumInfo;
236  }
237  }
238  }
239  // prepare next steps
240  minimumInfo->visited = true;
241  // XXX we only need to keep found elements if they have a higher rank than the lowest rank in the other search queue
242  myFound.insert(minimumInfo->edge);
243  for (typename std::vector<Connection>::iterator it = minimumInfo->upward.begin(); it != minimumInfo->upward.end(); it++) {
244  EdgeInfo* upwardInfo = it->target;
245  const SUMOReal traveltime = minimumInfo->traveltime + it->cost;
246  const SUMOVehicleClass svc = myVehicle->getVClass();
247  // check whether it can be used
248  if ((it->permissions & svc) != svc) {
249  continue;
250  }
251  const SUMOReal oldTraveltime = upwardInfo->traveltime;
252  if (!upwardInfo->visited && traveltime < oldTraveltime) {
253  upwardInfo->traveltime = traveltime;
254  upwardInfo->prev = minimumInfo;
255  if (oldTraveltime == std::numeric_limits<SUMOReal>::max()) {
256  myFrontier.push_back(upwardInfo);
257  push_heap(myFrontier.begin(), myFrontier.end(), myComparator);
258  } else {
259  push_heap(myFrontier.begin(),
260  find(myFrontier.begin(), myFrontier.end(), upwardInfo) + 1,
261  myComparator);
262  }
263  }
264  }
265  // @note: this effectively does a full dijkstra search.
266  // the effort compared to the naive stopping criterion is thus
267  // quadrupled. We could implement a better stopping criterion (Holte)
268  // However since the search shall take place in a contracted graph
269  // it probably does not matter
270  return !myFrontier.empty() && myFrontier.front()->traveltime < minTTSeen;
271  }
272 
273 
274  // reset state before rebuilding the contraction hierarchy
275  void reset() {
276  for (typename std::vector<EdgeInfo>::iterator it = myEdgeInfos.begin(); it != myEdgeInfos.end(); ++it) {
277  it->upward.clear();
278  }
279  }
280 
281  private:
285  std::vector<EdgeInfo*> myFrontier;
287  EdgeSet myFound;
289  std::vector<EdgeInfo> myEdgeInfos;
290 
292 
293  const V* myVehicle;
294 
295  };
296 
297  class CHInfo;
298 
300  class CHConnection {
301  public:
303  target(t), cost(c), permissions(p), underlying(u) {}
309  };
310 
311  typedef std::vector<CHConnection> CHConnections;
312  typedef std::pair<const CHConnection*, const CHConnection*> CHConnectionPair;
313  typedef std::vector<CHConnectionPair> CHConnectionPairs;
314 
321  CHRouter(size_t numEdges, bool unbuildIsWarning, Operation operation,
322  const SUMOVehicleClass svc,
323  SUMOTime weightPeriod,
324  bool validatePermissions):
325  SUMOAbstractRouter<E, V>(operation, "CHRouter"),
326  myErrorMsgHandler(unbuildIsWarning ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance()),
327  myForwardSearch(numEdges, true),
328  myBackwardSearch(numEdges, false),
329  mySPTree(new SPTree<CHInfo, CHConnection>(4, validatePermissions)),
330  myWeightPeriod(weightPeriod),
331  myValidUntil(0),
332  mySVC(svc),
333  myUpdateCount(0) {
334  for (size_t i = 0; i < numEdges; i++) {
335  myCHInfos.push_back(CHInfo(i));
336  }
337  }
338 
340  virtual ~CHRouter() {
341  delete mySPTree;
342  }
343 
344 
345  virtual SUMOAbstractRouter<E, V>* clone() const {
347  mySVC, myWeightPeriod, mySPTree->validatePermissions());
348  }
349 
354  virtual void compute(const E* from, const E* to, const V* const vehicle,
355  SUMOTime msTime, Result& into) {
356  assert(from != 0 && to != 0);
357  assert(mySPTree->validatePermissions() || vehicle->getVClass() == mySVC || mySVC == SVC_IGNORING);
358  // do we need to rebuild the hierarchy?
359  if (msTime >= myValidUntil) {
360  while (msTime >= myValidUntil) {
362  }
364  }
365  // ready for routing
366  this->startQuery();
367  myForwardSearch.init(from, vehicle);
368  myBackwardSearch.init(to, vehicle);
370  Meeting meeting(static_cast<EdgeInfo*>(0), static_cast<EdgeInfo*>(0));
371  bool continueForward = true;
372  bool continueBackward = true;
373  int num_visited_fw = 0;
374  int num_visited_bw = 0;
375  while (continueForward || continueBackward) {
376  if (continueForward) {
377  continueForward = myForwardSearch.step(myBackwardSearch, minTTSeen, meeting);
378  num_visited_fw += 1;
379  }
380  if (continueBackward) {
381  continueBackward = myBackwardSearch.step(myForwardSearch, minTTSeen, meeting);
382  num_visited_bw += 1;
383  }
384  }
385  if (minTTSeen < std::numeric_limits<SUMOReal>::max()) {
386  buildPathFromMeeting(meeting, into);
387  } else {
388  myErrorMsgHandler->inform("No connection between '" + from->getID() + "' and '" + to->getID() + "' found.");
389  }
390 #ifdef CHRouter_DEBUG_QUERY_PERF
391  std::cout << "visited " << num_visited_fw + num_visited_bw << " edges (" << num_visited_fw << "," << num_visited_bw << ") ,final path length: " + toString(into.size()) + ")\n";
392 #endif
393  this->endQuery(num_visited_bw + num_visited_fw);
394  }
395 
396 
397  SUMOReal recomputeCosts(const std::vector<const E*>& edges, const V* const v, SUMOTime msTime) const {
398  const SUMOReal time = STEPS2TIME(msTime);
399  SUMOReal costs = 0;
400  for (typename std::vector<const E*>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
401  if (PF::operator()(*i, v)) {
402  return -1;
403  }
404  costs += this->getEffort(*i, v, time + costs);
405  }
406  return costs;
407  }
408 
410 
412  void buildPathFromMeeting(Meeting meeting, Result& into) {
413  std::deque<const E*> tmp;
414  const EdgeInfo* backtrack = meeting.first;
415  while (backtrack != 0) {
416  tmp.push_front((E*) backtrack->edge); // !!!
417  backtrack = backtrack->prev;
418  }
419  backtrack = meeting.second->prev; // don't use central edge twice
420  while (backtrack != 0) {
421  tmp.push_back((E*) backtrack->edge); // !!!
422  backtrack = backtrack->prev;
423  }
424  // expand shortcuts
425  const E* prev = 0;
426  while (!tmp.empty()) {
427  const E* cur = tmp.front();
428  tmp.pop_front();
429  if (prev == 0) {
430  into.push_back(cur);
431  prev = cur;
432  } else {
433  const E* via = getVia(prev, cur);
434  if (via == 0) {
435  into.push_back(cur);
436  prev = cur;
437  } else {
438  tmp.push_front(cur);
439  tmp.push_front(via);
440  }
441  }
442  }
443  }
444 
446  typedef std::pair<const E*, const E*> ConstEdgePair;
447  typedef std::pair<E*, E*> EdgePair;
448 
449  struct Shortcut {
450  Shortcut(EdgePair e, SUMOReal c, int u, SVCPermissions p):
451  edgePair(e), cost(c), underlying(u), permissions(p) {}
452  EdgePair edgePair;
456  };
457 
458  typedef std::vector<Shortcut> Shortcuts;
459  typedef std::map<ConstEdgePair, const E*> ShortcutVia;
460 
461  /* @brief container class to use when building the contraction hierarchy.
462  * instances are reused every time the hierarchy is rebuilt (new time slice)
463  * but they must be synchronized first */
464  class CHInfo {
465  public:
467  CHInfo(size_t id) :
468  edge(E::dictionary(id)),
470  rank(-1),
471  level(0),
472  underlyingTotal(0),
473  visited(false),
474  traveltime(std::numeric_limits<SUMOReal>::max())
475  {}
476 
479  if (spTree != 0) {
480  updateShortcuts(spTree);
481  updateLevel();
482  } else {
483  contractedNeighbors += 1; // called when a connected edge was contracted
484  }
485  const SUMOReal oldPriority = priority;
486  // priority term as used by abraham []
487  const int edge_difference = (int)followers.size() + (int)approaching.size() - 2 * (int)shortcuts.size();
488  priority = (SUMOReal)(2 * edge_difference - contractedNeighbors - underlyingTotal - 5 * level);
489  return priority != oldPriority;
490  }
491 
494  const bool validatePermissions = spTree->validatePermissions();
495 #ifdef CHRouter_DEBUG_CONTRACTION_DEGREE
496  const int degree = approaching.size() + followers.size();
497  std::cout << "computing shortcuts for '" + edge->getID() + "' with degree " + toString(degree) + "\n";
498 #endif
499  shortcuts.clear();
500  underlyingTotal = 0;
501  for (typename CHConnections::iterator it_a = approaching.begin(); it_a != approaching.end(); it_a++) {
502  CHConnection& aInfo = *it_a;
503  // build shortest path tree in a fixed neighborhood
504  spTree->rebuildFrom(aInfo.target, this);
505  for (typename CHConnections::iterator it_f = followers.begin(); it_f != followers.end(); it_f++) {
506  CHConnection& fInfo = *it_f;
507  const SUMOReal viaCost = aInfo.cost + fInfo.cost;
508  const SVCPermissions viaPermissions = (aInfo.permissions & fInfo.permissions);
509  if (fInfo.target->traveltime > viaCost) {
510  // found no faster path -> we need a shortcut via edge
511 #ifdef CHRouter_DEBUG_CONTRACTION_WITNESSES
512  debugNoWitness(aInfo, fInfo);
513 #endif
514  const int underlying = aInfo.underlying + fInfo.underlying;
515  underlyingTotal += underlying;
516  shortcuts.push_back(Shortcut(EdgePair(aInfo.target->edge, fInfo.target->edge),
517  viaCost, underlying, viaPermissions));
518 
519  } else if (validatePermissions) {
520  if ((fInfo.target->permissions & viaPermissions) != viaPermissions) {
521  // witness has weaker restrictions. try to find another witness
522  spTree->registerForValidation(&aInfo, &fInfo);
523  } else {
524 #ifdef CHRouter_DEBUG_CONTRACTION_WITNESSES
525  debugNoWitness(aInfo, fInfo);
526 #endif
527  }
528  } else {
529 #ifdef CHRouter_DEBUG_CONTRACTION_WITNESSES
530  debugNoWitness(aInfo, fInfo);
531 #endif
532  }
533  }
534  }
535  // insert shortcuts needed due to unmet permissions
536  if (validatePermissions) {
537  const CHConnectionPairs& pairs = spTree->getNeededShortcuts(this);
538  for (typename CHConnectionPairs::const_iterator it = pairs.begin(); it != pairs.end(); ++it) {
539  const CHConnection* aInfo = it->first;
540  const CHConnection* fInfo = it->second;
541  const SUMOReal viaCost = aInfo->cost + fInfo->cost;
542  const SVCPermissions viaPermissions = (aInfo->permissions & fInfo->permissions);
543  const int underlying = aInfo->underlying + fInfo->underlying;
544  underlyingTotal += underlying;
545  shortcuts.push_back(Shortcut(EdgePair(aInfo->target->edge, fInfo->target->edge),
546  viaCost, underlying, viaPermissions));
547  }
548  }
549  }
550 
551 
552  // update level as defined by Abraham
553  void updateLevel() {
554  int maxLower = std::numeric_limits<int>::min();
555  int otherRank;
556  for (typename CHConnections::iterator it = approaching.begin(); it != approaching.end(); it++) {
557  otherRank = it->target->rank;
558  if (otherRank < rank) {
559  maxLower = MAX2(rank, maxLower);
560  }
561  }
562  for (typename CHConnections::iterator it = followers.begin(); it != followers.end(); it++) {
563  otherRank = it->target->rank;
564  if (otherRank < rank) {
565  maxLower = MAX2(rank, maxLower);
566  }
567  }
568  if (maxLower == std::numeric_limits<int>::min()) {
569  level = 0;
570  } else {
571  level = maxLower + 1;
572  }
573  }
574 
575  // resets state before rebuilding the hierarchy
578  rank = -1;
579  level = 0;
580  underlyingTotal = 0;
581  shortcuts.clear();
582  followers.clear();
583  approaching.clear();
584  }
585 
586 
588  E* edge;
592  Shortcuts shortcuts;
595  int rank;
596  int level;
598 
600  CHConnections followers;
601  CHConnections approaching;
602 
603 
605  bool visited;
609  int depth;
611  // @note: we may miss some witness paths by making traveltime the only
612  // criteria durinng search
614 
615  inline void reset() {
616  traveltime = std::numeric_limits<SUMOReal>::max();
617  visited = false;
618  }
619 
620 
622  inline void debugNoWitness(const CHConnection& aInfo, const CHConnection& fInfo) {
623  std::cout << "adding shortcut between " << aInfo.target->edge->getID() << ", " << fInfo.target->edge->getID() << " via " << edge->getID() << "\n";
624  }
625 
626  inline void debugWitness(const CHConnection& aInfo, const CHConnection& fInfo) {
627  const SUMOReal viaCost = aInfo.cost + fInfo.cost;
628  std::cout << "found witness with lenght " << fInfo.target->traveltime << " against via " << edge->getID() << " (length " << viaCost << ") for " << aInfo.target->edge->getID() << ", " << fInfo.target->edge->getID() << "\n";
629  }
630 
631  };
632 
633 private:
634 
640  public:
642  bool operator()(const CHInfo* a, const CHInfo* b) const {
643  if (a->priority == b->priority) {
644  return a->edge->getNumericalID() > b->edge->getNumericalID();
645  } else {
646  return a->priority < b->priority;
647  };
648  }
649  };
650 
651 
652  inline CHInfo* getCHInfo(const E* const edge) {
653  return &(myCHInfos[edge->getNumericalID()]);
654  }
655 
656 
658  void synchronize(CHInfo& info, SUMOReal time, const V* const vehicle) {
659  // forward and backward connections are used only in forward search,
660  // thus approaching costs are those of the approaching edge and not of the edge itself
661  const bool prune = !mySPTree->validatePermissions();
662  const E* const edge = info.edge;
663  if (prune && ((edge->getPermissions() & mySVC) != mySVC)) {
664  return;
665  }
666  const SUMOReal cost = this->getEffort(edge, vehicle, time);
667 
668  const std::vector<E*>& successors = edge->getSuccessors(mySVC);
669  for (typename std::vector<E*>::const_iterator it = successors.begin(); it != successors.end(); ++it) {
670  const E* fEdge = *it;
671  if (prune && ((fEdge->getPermissions() & mySVC) != mySVC)) {
672  continue;
673  }
674  CHInfo* follower = getCHInfo(fEdge);
675  SVCPermissions permissions = (edge->getPermissions() & follower->edge->getPermissions());
676  info.followers.push_back(CHConnection(follower, cost, permissions, 1));
677  follower->approaching.push_back(CHConnection(&info, cost, permissions, 1));
678  }
679 #ifdef CHRouter_DEBUG_WEIGHTS
680  std::cout << time << ": " << edge->getID() << " cost: " << cost << "\n";
681 #endif
682  // @todo: check whether we even need to save approaching in ROEdge;
683  }
684 
685 
687  void disconnect(CHConnections& connections, CHInfo* other) {
688  for (typename CHConnections::iterator it = connections.begin(); it != connections.end(); it++) {
689  if (it->target == other) {
690  connections.erase(it);
691  return;
692  }
693  }
694  assert(false);
695  }
696 
697 
698  void buildContractionHierarchy(SUMOTime time, const V* const vehicle) {
699  const size_t numEdges = myCHInfos.size();
700  const std::string vClass = (mySPTree->validatePermissions() ?
701  "all vehicle classes " : "vClass='" + SumoVehicleClassStrings.getString(mySVC) + "' ");
702  PROGRESS_BEGIN_MESSAGE("Building Contraction Hierarchy for " + vClass
703  + "and time=" + time2string(time) + " (" + toString(numEdges) + " edges)\n");
704  const long startMillis = SysUtils::getCurrentMillis();
705  // init queue
706  std::vector<CHInfo*> queue; // max heap: edge to be contracted is front
707  myShortcuts.clear();
708  // reset previous connections etc
711  for (size_t i = 0; i < numEdges; i++) {
712  myCHInfos[i].resetContractionState();
713  }
714  // copy connections from the original net
715  const SUMOReal time_seconds = STEPS2TIME(time); // timelines store seconds!
716  for (size_t i = 0; i < numEdges; i++) {
717  synchronize(myCHInfos[i], time_seconds, vehicle);
718  }
719  // synchronization is finished. now we can compute priorities for the first time
720  for (size_t i = 0; i < numEdges; i++) {
721  myCHInfos[i].updatePriority(mySPTree);
722  queue.push_back(&(myCHInfos[i]));
723  }
724  make_heap(queue.begin(), queue.end(), myCmp);
725  int contractionRank = 0;
726  // contraction loop
727  while (!queue.empty()) {
728  while (tryUpdateFront(queue)) {}
729  CHInfo* max = queue.front();
730  max->rank = contractionRank;
731 #ifdef CHRouter_DEBUG_CONTRACTION
732  std::cout << "contracting '" << max->edge->getID() << "' with prio: " << max->priority << " (rank " << contractionRank << ")\n";
733 #endif
734  E* edge = max->edge;
735  // add outgoing connections to the forward search
736  EdgeInfo* edgeInfoFW = myForwardSearch.getEdgeInfo(edge);
737  edgeInfoFW->rank = contractionRank;
738  for (typename CHConnections::iterator it = max->followers.begin(); it != max->followers.end(); it++) {
739  CHConnection& con = *it;
740  EdgeInfo* followerInfoFW = myForwardSearch.getEdgeInfo(con.target->edge);
741  edgeInfoFW->upward.push_back(Connection(followerInfoFW, con.cost, con.permissions));
742  disconnect(con.target->approaching, max);
743  con.target->updatePriority(0);
744  }
745  // add incoming connections to the backward search
746  EdgeInfo* edgeInfoBW = myBackwardSearch.getEdgeInfo(edge);
747  edgeInfoBW->rank = contractionRank;
748  for (typename CHConnections::iterator it = max->approaching.begin(); it != max->approaching.end(); it++) {
749  CHConnection& con = *it;
750  EdgeInfo* approachingInfoBW = myBackwardSearch.getEdgeInfo(con.target->edge);
751  edgeInfoBW->upward.push_back(Connection(approachingInfoBW, con.cost, con.permissions));
752  disconnect(con.target->followers, max);
753  con.target->updatePriority(0);
754  }
755  // add shortcuts to the net
756  for (typename Shortcuts::iterator it = max->shortcuts.begin(); it != max->shortcuts.end(); it++) {
757  EdgePair& edgePair = it->edgePair;
758  myShortcuts[edgePair] = edge;
759  CHInfo* from = getCHInfo(edgePair.first);
760  CHInfo* to = getCHInfo(edgePair.second);
761  from->followers.push_back(CHConnection(to, it->cost, it->permissions, it->underlying));
762  to->approaching.push_back(CHConnection(from, it->cost, it->permissions, it->underlying));
763  }
764  // remove from queue
765  pop_heap(queue.begin(), queue.end(), myCmp);
766  queue.pop_back();
767  /*
768  if (contractionRank % 10000 == 0) {
769  // update all and rebuild queue
770  for (typename std::vector<CHInfo*>::iterator it = queue.begin(); it != queue.end(); ++it) {
771  (*it)->updatePriority(mySPTree);
772  }
773  make_heap(queue.begin(), queue.end(), myCmp);
774  }
775  */
776  contractionRank++;
777  }
778  // reporting
779  const long duration = SysUtils::getCurrentMillis() - startMillis;
780  WRITE_MESSAGE("Created " + toString(myShortcuts.size()) + " shortcuts.");
781  WRITE_MESSAGE("Recomputed priority " + toString(myUpdateCount) + " times.");
782  MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(duration) + "ms).");
784  // declare new validUntil (prevent overflow)
786  myValidUntil = time + myWeightPeriod;
787  } else {
789  }
790  myUpdateCount = 0;
791  }
792 
793  // retrieve the via edge for a shortcut
794  const E* getVia(const E* forwardFrom, const E* forwardTo) {
795  ConstEdgePair forward(forwardFrom, forwardTo);
796  typename ShortcutVia::iterator it = myShortcuts.find(forward);
797  if (it != myShortcuts.end()) {
798  return it->second;
799  } else {
800  return 0;
801  }
802  }
803 
804 
808  bool tryUpdateFront(std::vector<CHInfo*>& queue) {
809  myUpdateCount++;
810  CHInfo* max = queue.front();
811 #ifdef CHRouter_DEBUG_CONTRACTION_QUEUE
812  std::cout << "updating '" << max->edge->getID() << "'\n";
813  debugPrintQueue(queue);
814 #endif
815  if (max->updatePriority(mySPTree)) {
816  pop_heap(queue.begin(), queue.end(), myCmp);
817  push_heap(queue.begin(), queue.end(), myCmp);
818  return true;
819  } else {
820  return false;
821  }
822  }
823 
824  // helper method for debugging
825  void debugPrintQueue(std::vector<CHInfo*>& queue) {
826  for (typename std::vector<CHInfo*>::iterator it = queue.begin(); it != queue.end(); it++) {
827  CHInfo* chInfo = *it;
828  std::cout << "(" << chInfo->edge->getID() << "," << chInfo->priority << ") ";
829  }
830  std::cout << "\n";
831  }
832 
833 private:
836 
840 
842  ShortcutVia myShortcuts;
843 
845  std::vector<CHInfo> myCHInfos;
846 
849 
852 
855 
858 
861 
864 };
865 
866 
867 #endif
868 
869 /****************************************************************************/
870 
Computes the shortest path through a contracted network.
Definition: CHRouter.h:74
void debugWitness(const CHConnection &aInfo, const CHConnection &fInfo)
Definition: CHRouter.h:626
EdgeSet myFound
the set of visited (settled) Edges
Definition: CHRouter.h:287
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
CHInfoComparator myCmp
Comparator for contraction priority.
Definition: CHRouter.h:848
std::pair< E *, E * > EdgePair
Definition: CHRouter.h:447
std::pair< const E *, const E * > ConstEdgePair
contraction related members
Definition: CHRouter.h:446
int myUpdateCount
counters for performance logging
Definition: CHRouter.h:863
SPTree< CHInfo, CHConnection > * mySPTree
the shortest path tree to use when searching for shortcuts
Definition: CHRouter.h:851
int depth
number of edges from start
Definition: CHRouter.h:609
const SUMOTime myWeightPeriod
the validity duration of one weight interval
Definition: CHRouter.h:854
const EdgeInfo * getEdgeInfo(const E *const edge) const
Definition: CHRouter.h:165
EdgeInfo * getEdgeInfo(const E *const edge)
Definition: CHRouter.h:161
CHConnections followers
connections (only valid after synchronization)
Definition: CHRouter.h:600
bool step(const Unidirectional &otherSearch, SUMOReal &minTTSeen, Meeting &meeting)
explore on element from the frontier,update minTTSeen and meeting if an EdgeInfo found by the otherSe...
Definition: CHRouter.h:208
const E * getVia(const E *forwardFrom, const E *forwardTo)
Definition: CHRouter.h:794
#define min(a, b)
Definition: polyfonts.c:66
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
virtual ~CHRouter()
Destructor.
Definition: CHRouter.h:340
virtual void compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, Result &into)
Builds the route between the given edges using the minimum traveltime in the contracted graph...
Definition: CHRouter.h:354
std::set< const E * > EdgeSet
A set of (found) Edges.
Definition: CHRouter.h:86
Unidirectional myForwardSearch
the unidirectional search queues
Definition: CHRouter.h:838
void buildPathFromMeeting(Meeting meeting, Result &into)
normal routing methods
Definition: CHRouter.h:412
SUMOReal traveltime
Effort to reach the edge.
Definition: CHRouter.h:607
int SVCPermissions
void resetContractionState()
Definition: CHRouter.h:576
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
void synchronize(CHInfo &info, SUMOReal time, const V *const vehicle)
copy connections from the original net (modified destructively during contraction) ...
Definition: CHRouter.h:658
std::vector< EdgeInfo * > myFrontier
the min edge heap
Definition: CHRouter.h:285
SVCPermissions permissions
Definition: CHRouter.h:99
bool tryUpdateFront(std::vector< CHInfo * > &queue)
tries to update the priority of the first edge
Definition: CHRouter.h:808
bool visited
Whether the shortest path to this edge is already found.
Definition: CHRouter.h:127
T MAX2(T a, T b)
Definition: StdDefs.h:74
CHInfo * getCHInfo(const E *const edge)
Definition: CHRouter.h:652
ShortcutVia myShortcuts
map from (forward) shortcut to via-Edge
Definition: CHRouter.h:842
void updateShortcuts(SPTree< CHInfo, CHConnection > *spTree)
compute needed shortcuts when contracting this edge
Definition: CHRouter.h:493
std::vector< const E * > Result
The found route (used as output parameter)
Definition: CHRouter.h:89
int contractedNeighbors
priority subterms
Definition: CHRouter.h:594
bool operator()(const CHInfo *a, const CHInfo *b) const
Comparing method.
Definition: CHRouter.h:642
EdgeInfoByTTComparator myComparator
Definition: CHRouter.h:291
SUMOReal priority
The contraction priority.
Definition: CHRouter.h:590
int rank
the contraction rank (higher means more important)
Definition: CHRouter.h:133
Shortcut(EdgePair e, SUMOReal c, int u, SVCPermissions p)
Definition: CHRouter.h:450
E * edge
The current edge - not const since it may receive shortcut edges.
Definition: CHRouter.h:588
Unidirectional myBackwardSearch
Definition: CHRouter.h:839
SVCPermissions permissions
Definition: CHRouter.h:306
std::map< ConstEdgePair, const E * > ShortcutVia
Definition: CHRouter.h:459
#define new
Definition: debug_new.h:123
void registerForValidation(const C *aInfo, const C *fInfo)
save source/target pair for later validation
Definition: SPTree.h:142
std::vector< Shortcut > Shortcuts
Definition: CHRouter.h:458
std::vector< CHConnectionPair > CHConnectionPairs
Definition: CHRouter.h:313
std::vector< CHInfo > myCHInfos
static vector for lookup
Definition: CHRouter.h:845
#define max(a, b)
Definition: polyfonts.c:65
SUMOTime myValidUntil
the validity duration of the current hierarchy (exclusive)
Definition: CHRouter.h:857
Forward/backward connection with associated FORWARD cost.
Definition: CHRouter.h:300
std::vector< EdgeInfo > myEdgeInfos
The container of edge information.
Definition: CHRouter.h:289
CHInfo(size_t id)
Constructor.
Definition: CHRouter.h:467
const CHConnectionPairs & getNeededShortcuts(const E *excluded)
Definition: SPTree.h:150
EdgeInfo * target
Definition: CHRouter.h:97
void init(const E *const start, const V *const vehicle)
Definition: CHRouter.h:185
bool operator()(const EdgeInfo *nod1, const EdgeInfo *nod2) const
Comparing method.
Definition: CHRouter.h:176
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
EdgeInfo(size_t id)
Constructor.
Definition: CHRouter.h:110
SUMOReal traveltime
Effort to reach the edge.
Definition: CHRouter.h:121
std::vector< CHConnection > CHConnections
Definition: CHRouter.h:311
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:62
bool updatePriority(SPTree< CHInfo, CHConnection > *spTree)
recompute the contraction priority and report whether it changed
Definition: CHRouter.h:478
SUMOReal(* Operation)(const E *const, const V *const, SUMOReal)
Type of the function that is used to retrieve the edge effort.
Definition: CHRouter.h:80
void debugNoWitness(const CHConnection &aInfo, const CHConnection &fInfo)
debugging methods
Definition: CHRouter.h:622
std::pair< const EdgeInfo *, const EdgeInfo * > Meeting
A meeting point of the two search scopes.
Definition: CHRouter.h:83
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2, false)
Operation myOperation
The object's operation to perform.
CHConnection(CHInfo *t, SUMOReal c, SVCPermissions p, int u)
Definition: CHRouter.h:302
Unidirectional(size_t numEdges, bool forward)
Constructor.
Definition: CHRouter.h:149
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
SVCPermissions permissions
Definition: CHRouter.h:455
CHRouter(size_t numEdges, bool unbuildIsWarning, Operation operation, const SUMOVehicleClass svc, SUMOTime weightPeriod, bool validatePermissions)
Constructor.
Definition: CHRouter.h:321
SUMOVehicleClass mySVC
the permissions for which the hierarchy was constructed
Definition: CHRouter.h:860
bool visited
members used in SPTree
Definition: CHRouter.h:605
Definition: SPTree.h:46
Shortcuts shortcuts
The needed shortcuts.
Definition: CHRouter.h:592
SUMOReal recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime) const
Definition: CHRouter.h:397
MsgHandler *const myErrorMsgHandler
the handler for routing errors
Definition: CHRouter.h:835
Forward/backward connection with associated forward/backward cost.
Definition: CHRouter.h:94
void rebuildFrom(E *start, const E *excluded)
build a shortest path tree from start to a depth of myMaxdepth. The given edge is excluded from this ...
Definition: SPTree.h:95
CHConnections approaching
Definition: CHRouter.h:601
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
int underlying
the number of connections underlying this connection
Definition: CHRouter.h:308
int SUMOTime
Definition: SUMOTime.h:43
void disconnect(CHConnections &connections, CHInfo *other)
remove all connections to/from the given edge (assume it exists only once)
Definition: CHRouter.h:687
EdgePair edgePair
Definition: CHRouter.h:452
SVCPermissions permissions
the permissions when reaching this edge on the fastest path
Definition: CHRouter.h:613
bool myAmForward
the role of this search
Definition: CHRouter.h:283
#define SUMOReal
Definition: config.h:218
void endQuery(int visits)
bool found(const E *edge) const
Definition: CHRouter.h:157
bool validatePermissions()
whether permissions should be validated;
Definition: SPTree.h:137
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:50
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
void updateLevel()
Definition: CHRouter.h:553
void debugPrintQueue(std::vector< CHInfo * > &queue)
Definition: CHRouter.h:825
std::pair< const CHConnection *, const CHConnection * > CHConnectionPair
Definition: CHRouter.h:312
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
SUMOReal getEffort(const E *const e, const V *const v, SUMOReal t) const
Connection(EdgeInfo *t, SUMOReal c, SVCPermissions p)
Definition: CHRouter.h:96
const E * edge
The current edge.
Definition: CHRouter.h:118
vehicles ignoring classes
virtual SUMOAbstractRouter< E, V > * clone() const
Definition: CHRouter.h:345
void buildContractionHierarchy(SUMOTime time, const V *const vehicle)
Definition: CHRouter.h:698
std::vector< Connection > upward
Connections to higher ranked nodes.
Definition: CHRouter.h:130
EdgeInfo * prev
The previous edge.
Definition: CHRouter.h:124
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:131