Transit Planner  1.0
An experiment on transfer patterns robustness
src/Dijkstra.h
00001 // Copyright 2011: Eugen Sawin, Philip Stahl, Jonas Sternisko
00002 #ifndef SRC_DIJKSTRA_H_
00003 #define SRC_DIJKSTRA_H_
00004 
00005 // #include <google/dense_hash_map>
00006 #include <vector>
00007 #include <string>
00008 #include <functional>
00009 #include <queue>
00010 #include <map>
00011 #include <set>
00012 #include "./HubSet.h"
00013 #include "./Label.h"
00014 #include "./Logger.h"
00015 #include "./QueryGraph.h"
00016 #include "./Utilities.h"
00017 
00018 using std::vector;
00019 using std::string;
00020 using std::make_pair;
00021 using std::map;
00022 using std::priority_queue;
00023 using std::greater;
00024 // using google::dense_hash_map;
00025 
00026 class TransitNetwork;
00027 
00028 // Stores results of a shortest path query.
00029 class QueryResult {
00030  public:
00031   // (label, stop ids) pair depicting a path.
00032   typedef std::pair<LabelVec::Hnd, vector<int> > Path;
00033   // comparator struct
00034   struct PathCmp {
00035     bool operator() (const Path& path1, const Path& path2) const {
00036       return path1.first.penalty() < path2.first.penalty();
00037     }
00038   };
00039 
00040   // describes a path by all it's labels
00041   typedef pair<LabelVec::Hnd, vector<LabelVec::Hnd> > ExplicitPath;
00042 
00043   QueryResult();
00044 
00045   // The costs of the optimal paths.
00046   LabelVec destLabels;
00047 
00048   // The costs of all nodes
00049   LabelMatrix matrix;
00050 
00051   // Number of settled labels.
00052   size_t numSettledLabels;
00053 
00054   // Clears all contents.
00055   void clear();
00056 
00057   // Returns the lowest cost from the optimal paths.
00058   int optimalCosts() const;
00059 
00060   // Returns the lowest penalty from the optimal paths.
00061   int optimalPenalty() const;
00062 
00063   // Returns all optimal paths using node ids. The two latter sort the paths by
00064   // their penalty value.
00065   set<ExplicitPath> traceBackOptimalPaths() const;
00066 
00067   // Path elements are node ids in the transit network
00068   vector<Path> optimalPaths(const TransitNetwork& network,
00069                             string* log = NULL) const;
00070 
00071   // Path elements are stop ids in the query graph
00072   vector<Path> optimalPaths(const QueryGraph& graph, string* log = NULL) const;
00073 
00074   // Returns for each optimal path a vector of its transfer stops.
00075   // Used for debugging / printing the optimal paths.
00076   set<vector<int> > transferStops(const TransitNetwork& network) const;
00077 
00078   // Returns the transfer pattern stops for the path ending with destLabel in
00079   // the given transit network.
00080   vector<int> getTransferPattern(const TransitNetwork& network,
00081                                  LabelVec::Hnd destLabel) const;
00082 
00083  private:
00084   QueryResult(const QueryResult& other) {}
00085   QueryResult& operator=(const QueryResult& other) { return *this; }
00086 };
00087 
00088 // The Dijkstra class
00089 class Dijkstra {
00090  public:
00091   typedef priority_queue<LabelMatrix::Hnd, vector<LabelMatrix::Hnd>,
00092                          LabelMatrix::Hnd::Comp> PriorityQueue;
00093 
00094   // Constructor
00095   explicit Dijkstra(const TransitNetwork& network);
00096 
00097   void findShortestPath(const vector<int>& depNodes, const int destStop,
00098                         QueryResult* result) const;
00099 
00100   // Set the logger to an external logger.
00101   void logger(const Logger* log);
00102 
00103   // Sets the maximum penalty considered during search.
00104   void maxPenalty(const unsigned char pen);
00105   // Returns the maximum penalty considered during search.
00106   unsigned char maxPenalty() const;
00107 
00108   // Sets the maximum penalty from hubs considered during search.
00109   void maxHubPenalty(const unsigned char pen);
00110   // Returns the maximum penalty from hubs considered during search.
00111   unsigned char maxHubPenalty() const;
00112 
00113   // Sets the maximum cost in seconds considered during search.
00114   void maxCost(const unsigned int cost);
00115 
00116   // Returns the maximum cost in seconds considered durchin search.
00117   unsigned int maxCost() const;
00118 
00119   // Sets the set of hubs.
00120   void hubs(const HubSet* hubs);
00121 
00122   // Returns a const pointer to the set hubs.
00123   const HubSet* hubs() const;
00124 
00125   // Sets the start time of the shortest path search on a network.
00126   void startTime(const int startTime);
00127 
00128   // Returns the set start time of the shortest path search.
00129   const int startTime() const;
00130 
00131  private:
00132   // Expands given node with its walkable successor nodes.
00133   void expandWalkNode(const LabelMatrix::Hnd& label,
00134                       const int destStop,
00135                       PriorityQueue* queue,
00136                       QueryResult* result,
00137                       int* numOpened, int* numInactive) const;
00138 
00139   // Expands given node using the given arc label.
00140   void expandNode(const LabelMatrix::Hnd& label,
00141                   PriorityQueue* queue,
00142                   QueryResult* result,
00143                   int* numOpened, int* numInactive) const;
00144 
00145   // Adds a successor label of given parent only if the resulting label is a
00146   // candidate for an optimal path and does not violate the maximum penalty and
00147   // maximum cost setting.
00148   void addSuccessor(const LabelMatrix::Hnd& parentLabel,
00149                     const unsigned int cost,
00150                     const unsigned char penalty, const bool walk,
00151                     const int succNode, PriorityQueue* queue,
00152                     QueryResult* result,
00153                     int* numOpened, int* numInactive) const;
00154 
00155   bool isHub(const int node) const;
00156   const TransitNetwork& _network;
00157   const Logger* _log;
00158   const HubSet* _hubs;
00159   unsigned char _maxPenalty;
00160   unsigned char _maxHubPenalty;
00161   unsigned int _maxCost;
00162   unsigned int _startTime;
00163 };
00164 
00165 
00166 class QuerySearch {
00167  public:
00168   // Constructor
00169   explicit QuerySearch(const QueryGraph& queryGraph,
00170                        const TransitNetwork& network);
00171 
00172   // Perform a shortest path Dijkstra search starting at time. Uses dc for
00173   // direct connection queries.
00174   void findOptimalPaths(const int startTime, const DirectConnection& dc,
00175                         QueryResult* resultPtr) const;
00176   void logger(Logger* log) { _log = log; }
00177  private:
00178   const QueryGraph& _graph;
00179   const TransitNetwork& _network;
00180   const int _maxPenalty;
00181   const Logger* _log;
00182 };
00183 #endif  // SRC_DIJKSTRA_H_
 All Classes