Transit Planner  1.0
An experiment on transfer patterns robustness
src/TransferPatternRouter.h
00001 // Copyright 2011: Eugen Sawin, Philip Stahl, Jonas Sternisko
00002 #ifndef SRC_TRANSFERPATTERNROUTER_H_
00003 #define SRC_TRANSFERPATTERNROUTER_H_
00004 
00005 #include <boost/serialization/access.hpp>
00006 #include <google/dense_hash_set>
00007 #include <vector>
00008 #include <string>
00009 #include <map>
00010 #include <set>
00011 #include "./Dijkstra.h"
00012 #include "./DirectConnection.h"
00013 #include "./Label.h"
00014 #include "./Logger.h"
00015 #include "./TransferPatternsDB.h"
00016 #include "./TransitNetwork.h"
00017 #include "./QueryGraph.h"
00018 #include "./HubSet.h"
00019 
00020 using std::vector;
00021 using std::string;
00022 using std::map;
00023 using std::set;
00024 using google::dense_hash_set;
00025 
00026 // Used for sorting a vector of pairs <stop index, importance of stop>
00027 struct sortStopsByImportance {
00028   bool operator()(const std::pair<int, int>& left,
00029                   const std::pair<int, int>& right) {
00030     return left.second > right.second;
00031   }
00032 };
00033 
00034 
00035 // The TransferPatternRouter class
00036 class TransferPatternRouter {
00037  public:
00038   static const int TIME_LIMIT;
00039   static const unsigned char PENALTY_LIMIT;
00040 
00041   // Constructor
00042   explicit TransferPatternRouter(const TransitNetwork& network);
00043   FRIEND_TEST(TransferPatternRouterTest, Constructor);
00044 
00045   // Initializes the direct connection data structure and creates a time-inde-
00046   // pendent network used for hub selection.
00047   void prepare(const vector<Line>& lines);
00048 
00049   static
00050   set<vector<int> > computeTransferPatterns(const TransitNetwork& network,
00051                                             const int depStop,
00052                                             const HubSet& hubs);
00053   // Function solely used for testing.
00054   void computeAllTransferPatterns(TPDB* tpdb);
00055 
00056   // Computes all transferPatterns between the given departure Stop and the
00057   // given Hubs. PLUS: To all other stops in the network which can't be reached
00058   // over a hub
00059   static
00060   set<vector<int> > computeTransferPatternsToHubs(const TransitNetwork& network,
00061                                                   const int depStop,
00062                                                   const HubSet& hubs);
00063 
00064   // Computes the transfer patterns from the departure stop to any other stop.
00065   static
00066   set<vector<int> > computeTransferPatternsToAll(const TransitNetwork& network,
00067                                                  const int depStop);
00068 
00069   // Constructs the QueryGraph from one stop to another, maybe empty. Uses hubs.
00070   const QueryGraph queryGraph(int depStop, int destStop) const;
00071 
00072   // Searches the shortest path between two stops starting at a certain time.
00073   vector<QueryResult::Path> shortestPath(const int startStop, const int time,
00074                                          const int targetStop,
00075                                          string* log = NULL);
00076   FRIEND_TEST(TransferPatternRouterTest, dijkstraCompare_walkFirstStop);
00077   FRIEND_TEST(TransferPatternRouterTest, dijkstraCompare_walkIntermediateStop);
00078   FRIEND_TEST(TransferPatternRouterTest, dijkstraCompare_walkLastStop);
00079 
00080   // Computes the hubs in the network by taking the number of nodes of each
00081   // stop into account. The stop wich has got the most nodes, is a hub.
00082   set<int> findBasicHubs(int numHubs);
00083 
00084   // Increases the counter for all stops which are on the optimal paths
00085   // from the seed stop.
00086   // stopFreqs is a vector of (stop index, frequency) pairs.
00087   void countStopFreq(const int seedStop, vector<IntPair>* stopFreqs) const;
00088 
00089   // Set the logger to an external logger.
00090   void logger(Logger* log);
00091 
00092   // Get the set of hubs.
00093   const HubSet& hubs() const;
00094 
00095   // Set the hubs.
00096   void hubs(const HubSet&);
00097 
00098   // Get the transfer pattern db.
00099   const TPDB* transferPatternsDB() const;
00100 
00101   // Set the constant transfer pattern db.
00102   void transferPatternsDB(const TPDB& db);
00103 
00104   // Generates the set of transfer patterns between origin and destination using
00105   // the transfer patterns db. Used for debuging.
00106   set<vector<int> > generateTransferPatterns(const int orig, const int dest)
00107   const;
00108   // Generates all transfer patterns from the stop 'orig'. Used for debugging.
00109   set<vector<int> > generateAllTransferPatterns(const int orig) const;
00110 
00111   // Returns a string of the given patterns. Used for debugging and statistics.
00112   string printPatterns(const set<vector<int> >& patterns);
00113 
00114   const DirectConnection& directConnection();
00115 
00116  private:
00117   // Adds for every label at a transfer or departure node with walk == true a
00118   // new label to the matrix with costs = costs of the parent label + costs of
00119   // the walking arc between the stop of the parent label and the stop of the
00120   // label.
00121   static void adjustWalkingCosts(const TransitNetwork& network,
00122                                  LabelMatrix* matrix);
00123   // Performs the arrival loop algorithm. This method filters the vector
00124   // QueryResult.costs for costs of arrival nodes at the stop. For the labels of
00125   // these nodes, suboptimal paths are replaced by 'waiting from better paths'.
00126   // Returns remaining label sets as a vector of size = #arrival nodes at stop.
00127   static void arrivalLoop(const TransitNetwork& network, LabelMatrix* matrix,
00128                           const int stop);
00129   FRIEND_TEST(TransferPatternRouterTest, arrivalLoop_transitivity);
00130 
00131   // Computes all transfer patterns via backtracking of Labels in the matrix.
00132   static set<vector<int> >
00133   collectTransferPatterns(const TransitNetwork& network,
00134                           const LabelMatrix& matrix, const int depStop);
00135 
00136   // The network.
00137   const TransitNetwork& _network;
00138   // A compressed form of the network used to determine hubs.
00139   TransitNetwork _timeCompressedNetwork;
00140 
00141   // The search structure for direct connection queries.
00142   DirectConnection _connections;
00143 
00144   // Stores for every pair of stops a list of all transferPatterns from the
00145   // first stop to the second
00146   const TransferPatternsDB* _tpdb;
00147 
00148   // The hub stop indices (important stations).
00149   HubSet _hubs;
00150 
00151   Logger* _log;
00152 };
00153 typedef TransferPatternRouter TPR;
00154 
00155 #endif  // SRC_TRANSFERPATTERNROUTER_H_
 All Classes