MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
Route.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_DATASTRUCTURE_ROUTE_HPP
2#define CDA_RAIL_DATASTRUCTURE_ROUTE_HPP
3
4#include "Definitions.hpp"
7
8#include <algorithm>
9#include <cstddef>
10#include <filesystem>
11#include <optional>
12#include <string>
13#include <unordered_map>
14#include <unordered_set>
15#include <utility>
16#include <vector>
17
18namespace cda_rail {
19
27 std::pair<double, double> pos1;
28 std::pair<double, double> pos2;
29 std::unordered_set<size_t> edges;
30};
31
38class Route {
39public:
46 struct EdgePosition {
47 double source{};
48 double target{};
49 };
50
51private:
53
66 [[nodiscard]] std::optional<double>
67 get_pos_on_edges_impl(const cda_rail::index_set& edge_indices,
68 const Network& network, bool first_match) const;
69
77 void is_route_index_valid(size_t route_index) const;
78
83 [[nodiscard]] bool has_edges() const;
84
85public:
86 // -----------------------------
87 // CONSTRUCTORS
88 // -----------------------------
89 // Only default constructors (empty)
90
91 // -----------------------------
92 // GETTER
93 // -----------------------------
94
99 [[nodiscard]] size_t size() const { return m_edges.size(); };
100
108 [[nodiscard]] double length(const Network& network) const;
109
114 [[nodiscard]] bool empty() const { return m_edges.empty(); };
115
120 [[nodiscard]] const cda_rail::index_vector& get_edges() const {
121 return m_edges;
122 };
123
135 [[nodiscard]] EdgePosition edge_pos_on_route(Network::EdgeInput const& edge,
136 const Network& network) const;
137
153 [[nodiscard]] EdgePosition
154 edge_set_pos_on_route(const cda_rail::index_vector& edges_to_consider,
155 const Network& network) const;
156
167 [[nodiscard]] std::optional<double>
169 const Network& network) const;
170
181 [[nodiscard]] std::optional<double>
182 get_last_pos_on_edges(const cda_rail::index_set& edge_indices,
183 const Network& network) const;
184
197 [[nodiscard]] size_t get_edge_id_at_pos(double pos,
198 const Network& network) const;
199
208 [[nodiscard]] size_t get_edge_id(size_t route_index) const;
209
221 [[nodiscard]] const Edge& get_edge(size_t route_index,
222 const Network& network) const;
233 [[nodiscard]] const Edge& get_first_edge(const Network& network) const {
234 return get_edge(0, network);
235 }
236
244 [[nodiscard]] const Edge& get_last_edge(const Network& network) const {
245 return get_edge(size() - 1, network);
246 }
247
253 [[nodiscard]] bool contains_edge(size_t const edgeIndex) const {
254 return std::ranges::contains(m_edges, edgeIndex);
255 };
256
267 [[nodiscard]] bool contains_edge(Network::EdgeInput const& edge_input,
268 const Network& network) const {
269 return contains_edge(edge_input.resolve(&network));
270 };
271
278 [[nodiscard]] bool
279 contains_edge(std::optional<size_t> const edgeIndex) const {
280 return edgeIndex.has_value() && contains_edge(edgeIndex.value());
281 }
282
283 // -----------------------------
284 // EDITING FUNCTIONS
285 // -----------------------------
286
301 void push_back_edge(Network::EdgeInput const& new_edge,
302 const Network& network);
303
318 void push_front_edge(Network::EdgeInput const& new_edge,
319 const Network& network);
320
326 void remove_first_edge();
327
333 void remove_last_edge();
334
335 // -----------------------------
336 // HELPER
337 // -----------------------------
338
350 [[nodiscard]] bool check_consistency(const Network& network) const;
351
363 const std::vector<std::pair<size_t, cda_rail::index_vector>>& new_edges);
364};
365
374class RouteMap {
375private:
376 std::unordered_map<std::string, Route> m_routes;
377
378public:
379 // ----------------
380 // CONSTRUCTORS
381 // ----------------
382
386 RouteMap() = default;
387
404 RouteMap(const std::filesystem::path& p, const Network& network);
405
412 RouteMap(const std::string& path, const Network& network)
413 : RouteMap(std::filesystem::path(path), network) {};
414
421 RouteMap(const char* path, const Network& network)
422 : RouteMap(std::filesystem::path(path), network) {};
423
424 // Rule of 5
425 RouteMap(const RouteMap& other) = default;
426 RouteMap(RouteMap&& other) noexcept = default;
427 RouteMap& operator=(const RouteMap& other) = default;
434 RouteMap& operator=(RouteMap&& other) noexcept = default;
435 ~RouteMap() = default;
436
437 // ----------------
438 // ITERATORS
439 // ----------------
440
442 [[nodiscard]] auto begin() const { return m_routes.cbegin(); };
444 [[nodiscard]] auto end() const { return m_routes.cend(); };
448 [[nodiscard]] auto cbegin() const { return m_routes.cbegin(); };
450 [[nodiscard]] auto cend() const { return m_routes.cend(); };
451
452 // ----------------
453 // GETTER
454 // ----------------
455
460 [[nodiscard]] size_t size() const { return m_routes.size(); };
461
466 [[nodiscard]] bool empty() const { return m_routes.empty(); };
467
476 [[nodiscard]] const Route& get_route(const std::string& train_name) const;
477
489 [[nodiscard]] double route_length(const std::string& train_name,
490 const Network& network) const;
491
497 [[nodiscard]] bool has_route(const std::string& train_name) const {
498 return m_routes.contains(train_name);
499 };
500
501 // ----------------
502 // OVERLAP FUNCTIONS
503 // ----------------
504
524 [[nodiscard]] std::vector<ConflictPair>
525 get_parallel_overlaps(const std::string& train1, const std::string& train2,
526 const Network& network) const;
527
548 [[nodiscard]] std::vector<ConflictPair>
549 get_ttd_overlaps(const std::string& train1, const std::string& train2,
550 const Network& network) const;
551
574 [[nodiscard]] std::vector<ConflictPair>
575 get_reverse_overlaps(const std::string& train1, const std::string& train2,
576 const Network& network) const;
577
596 [[nodiscard]] std::vector<ConflictPair>
597 get_crossing_overlaps(const std::string& train1, const std::string& train2,
598 const Network& network) const;
599
600 // ------------------
601 // EDITING FUNCTIONS
602 // ------------------
603
614 void add_empty_route(const std::string& train_name);
615
627 void add_empty_route(const std::string& train_name, const TrainList& trains);
628
641 void push_back_edge(const std::string& train_name,
642 Network::EdgeInput const& new_edge,
643 const Network& network);
644
657 void push_front_edge(const std::string& train_name,
658 Network::EdgeInput const& new_edge,
659 const Network& network);
660
668 void remove_first_edge(const std::string& train_name);
669
677 void remove_last_edge(const std::string& train_name);
678
686 void remove_route(const std::string& train_name);
687
688 // ------------------
689 // IMPORT / EXPORT
690 // ------------------
691
704 void export_routes(const std::filesystem::path& p,
705 const Network& network) const;
706
712 void export_routes(const std::string& path, const Network& network) const {
713 export_routes(std::filesystem::path(path), network);
714 };
715
722 void export_routes(const char* path, const Network& network) const {
723 export_routes(std::filesystem::path(path), network);
724 };
725
733 [[nodiscard]] static RouteMap import_routes(const std::filesystem::path& p,
734 const Network& network) {
735 return {p, network};
736 };
737
745 [[nodiscard]] static RouteMap import_routes(const std::string& path,
746 const Network& network) {
747 return {path, network};
748 };
749
764 [[nodiscard]] static RouteMap import_routes(const char* path,
765 const Network& network) {
766 return {path, network};
767 };
768
769 // -----------------
770 // HELPER
771 // -----------------
772
791 [[nodiscard]] bool
792 check_consistency(const TrainList& trains, const Network& network,
793 bool every_train_must_have_route = true) const;
794
807 const std::vector<std::pair<size_t, cda_rail::index_vector>>& new_edges);
808
809private:
816 void throw_if_train_has_no_route(std::string const& train_name) const;
817};
818
819} // namespace cda_rail
820
821#endif // CDA_RAIL_DATASTRUCTURE_ROUTE_HPP
Directed multigraph representing a railway network.
Definition RailwayNetwork.hpp:163
std::vector< ConflictPair > get_reverse_overlaps(const std::string &train1, const std::string &train2, const Network &network) const
Finds segments where the two trains travel in opposite directions over the same physical track.
Definition Route.cpp:366
void remove_last_edge(const std::string &train_name)
Removes the last edge from the given train's route.
Definition Route.cpp:501
RouteMap(const std::string &path, const Network &network)
Convenience overload accepting a std::string path.
Definition Route.hpp:412
void export_routes(const std::string &path, const Network &network) const
Writes all routes to routes.json in the specified directory.
Definition Route.hpp:712
bool check_consistency(const TrainList &trains, const Network &network, bool every_train_must_have_route=true) const
Checks whether all stored routes are valid with respect to the given train list and network.
Definition Route.cpp:565
void push_front_edge(const std::string &train_name, Network::EdgeInput const &new_edge, const Network &network)
Prepends an edge at the beginning of the given train's route.
Definition Route.cpp:489
void remove_route(const std::string &train_name)
Removes the entire route for the given train from the map.
Definition Route.cpp:506
auto cend() const
Read-only iterator past the last route entry (explicit const).
Definition Route.hpp:450
static RouteMap import_routes(const char *path, const Network &network)
Loads train routes from a file at the given directory path.
Definition Route.hpp:764
auto end() const
Read-only iterator past the last route entry.
Definition Route.hpp:444
static RouteMap import_routes(const std::filesystem::path &p, const Network &network)
Loads routes from a directory into a new RouteMap.
Definition Route.hpp:733
const Route & get_route(const std::string &train_name) const
Returns the route assigned to the given train.
Definition Route.cpp:253
RouteMap(const RouteMap &other)=default
void update_after_discretization(const std::vector< std::pair< size_t, cda_rail::index_vector > > &new_edges)
Updates all routes after a network discretization step.
Definition Route.cpp:578
size_t size() const
Counts the number of stored train routes.
Definition Route.hpp:460
bool empty() const
Determines whether the map contains no routes.
Definition Route.hpp:466
void export_routes(const std::filesystem::path &p, const Network &network) const
Writes all routes to a routes.json file inside directory p.
Definition Route.cpp:511
double route_length(const std::string &train_name, const Network &network) const
Returns the total length of the route assigned to the given train.
Definition Route.cpp:258
void export_routes(const char *path, const Network &network) const
Exports routes to a directory specified by a C-string path.
Definition Route.hpp:722
auto begin() const
Read-only iterator to the first route entry.
Definition Route.hpp:442
RouteMap(RouteMap &&other) noexcept=default
static RouteMap import_routes(const std::string &path, const Network &network)
Static factory: convenience overload accepting a std::string path.
Definition Route.hpp:745
void add_empty_route(const std::string &train_name)
Registers an empty route for the given train name.
Definition Route.cpp:468
std::vector< ConflictPair > get_crossing_overlaps(const std::string &train1, const std::string &train2, const Network &network) const
Finds all segments where the two trains could potentially block each other, regardless of travel dire...
Definition Route.cpp:433
auto cbegin() const
Returns a const iterator to the beginning of the routes.
Definition Route.hpp:448
RouteMap & operator=(RouteMap &&other) noexcept=default
Move-assigns another RouteMap to this one.
std::vector< ConflictPair > get_parallel_overlaps(const std::string &train1, const std::string &train2, const Network &network) const
Finds segments where both trains travel in the same direction over the same track section.
Definition Route.cpp:264
void push_back_edge(const std::string &train_name, Network::EdgeInput const &new_edge, const Network &network)
Appends an edge at the end of the given train's route.
Definition Route.cpp:482
std::vector< ConflictPair > get_ttd_overlaps(const std::string &train1, const std::string &train2, const Network &network) const
Finds segments where both trains share an unbreakable (TTD) track section.
Definition Route.cpp:318
RouteMap & operator=(const RouteMap &other)=default
RouteMap(const char *path, const Network &network)
Convenience overload accepting a C-string path.
Definition Route.hpp:421
void remove_first_edge(const std::string &train_name)
Removes the first edge from the given train's route.
Definition Route.cpp:496
RouteMap()=default
Creates an empty route map with no routes.
bool has_route(const std::string &train_name) const
Checks whether a route for the given train is stored in this map.
Definition Route.hpp:497
Ordered list of network edges that a train follows.
Definition Route.hpp:38
const cda_rail::index_vector & get_edges() const
Provides the edges forming the route in travel order.
Definition Route.hpp:120
size_t get_edge_id_at_pos(double pos, const Network &network) const
Returns the edge that contains a given position on the route.
Definition Route.cpp:111
double length(const Network &network) const
Returns the total route length.
Definition Route.cpp:34
std::optional< double > get_first_pos_on_edges(const cda_rail::index_set &edge_indices, const Network &network) const
Returns where the first matching edge starts.
Definition Route.cpp:99
void remove_last_edge()
Removes the last edge from the route.
Definition Route.cpp:174
size_t get_edge_id(size_t route_index) const
Returns the edge id at a given route position.
Definition Route.cpp:135
const Edge & get_first_edge(const Network &network) const
Returns the first edge in the route.
Definition Route.hpp:233
EdgePosition edge_pos_on_route(Network::EdgeInput const &edge, const Network &network) const
Returns where a specific edge lies on this route.
Definition Route.cpp:43
void push_back_edge(Network::EdgeInput const &new_edge, const Network &network)
Adds an edge at the end of the route.
Definition Route.cpp:146
const Edge & get_last_edge(const Network &network) const
Returns the last edge in the route.
Definition Route.hpp:244
const Edge & get_edge(size_t route_index, const Network &network) const
Returns the full edge object at a given route position.
Definition Route.cpp:140
void remove_first_edge()
Removes the first edge from the route.
Definition Route.cpp:167
void push_front_edge(Network::EdgeInput const &new_edge, const Network &network)
Adds an edge at the beginning of the route.
Definition Route.cpp:156
bool contains_edge(Network::EdgeInput const &edge_input, const Network &network) const
Checks whether a network edge appears in this route.
Definition Route.hpp:267
void update_after_discretization(const std::vector< std::pair< size_t, cda_rail::index_vector > > &new_edges)
Updates the route after edge splitting in the network.
Definition Route.cpp:193
bool contains_edge(size_t const edgeIndex) const
Checks whether an edge id appears in this route.
Definition Route.hpp:253
size_t size() const
Gets the number of edges in the route.
Definition Route.hpp:99
bool check_consistency(const Network &network) const
Checks whether the route is connected step by step.
Definition Route.cpp:181
EdgePosition edge_set_pos_on_route(const cda_rail::index_vector &edges_to_consider, const Network &network) const
Returns one interval that covers all selected edges on the route.
Definition Route.cpp:68
std::optional< double > get_last_pos_on_edges(const cda_rail::index_set &edge_indices, const Network &network) const
Returns where the last matching edge ends.
Definition Route.cpp:105
bool contains_edge(std::optional< size_t > const edgeIndex) const
Determines if an optional edge id is present in this route.
Definition Route.hpp:279
bool empty() const
Checks whether the route is empty.
Definition Route.hpp:114
Container for trains with name-based indexing.
Definition Train.hpp:200
Definition CustomExceptions.hpp:15
std::unordered_set< size_t > index_set
Definition Definitions.hpp:22
std::vector< size_t > index_vector
Definition Definitions.hpp:21
One overlap interval between two routes.
Definition Route.hpp:26
std::pair< double, double > pos2
Definition Route.hpp:28
std::unordered_set< size_t > edges
Definition Route.hpp:29
std::pair< double, double > pos1
Definition Route.hpp:27
Represents a directed edge in the railway network graph.
Definition RailwayNetwork.hpp:79
Unified edge descriptor accepted by all public API functions.
Definition RailwayNetwork.hpp:207
Start and end position of one edge within the route.
Definition Route.hpp:46
double target
Definition Route.hpp:48
double source
Definition Route.hpp:47