MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
RailwayNetwork.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_DATASTRUCTURE_RAILWAYNETWORK_HPP
2#define CDA_RAIL_DATASTRUCTURE_RAILWAYNETWORK_HPP
3
5#include "Definitions.hpp"
6#include "StringHelper.hpp"
7#include "VSSModel.hpp"
8
9#include <cstddef>
10#include <filesystem>
11#include <optional>
12#include <string>
13#include <string_view>
14#include <tinyxml2.h>
15#include <unordered_map>
16#include <unordered_set>
17#include <utility>
18#include <variant>
19#include <vector>
20
21namespace cda_rail {
22
30struct Vertex {
31 constexpr static double HEADWAY_DEFAULT{0.0};
32
33 std::string name;
36
45 Vertex(std::string_view const name, VertexType const type,
46 double const headway = HEADWAY_DEFAULT)
47 : name(name), type(type), headway(headway) {};
48
56 Vertex(std::string_view const name, VertexType const type,
57 std::optional<double> const& headway)
58 : name(name), type(type), headway(headway.value_or(HEADWAY_DEFAULT)) {};
59
60 // Operators
67 bool operator==(Vertex const& other) const {
68 return name == other.name && type == other.type && headway == other.headway;
69 }
70};
71
79struct Edge {
80 constexpr static bool BREAKABLE_DEFAULT = true;
81 constexpr static double MIN_BLOCK_LENGTH_DEFAULT = 1.0;
82 constexpr static double MIN_STOP_BLOCK_LENGTH_DEFAULT = 100.0;
83
84 size_t source;
85 size_t target;
86 double length;
87 double max_speed;
91
106 Edge(size_t const source, size_t const target, double const length,
107 double const maxSpeed, bool const breakable = BREAKABLE_DEFAULT,
108 double const minBlockLength = MIN_BLOCK_LENGTH_DEFAULT,
109 double const minStopBlockLength = MIN_STOP_BLOCK_LENGTH_DEFAULT)
110 : source(source), target(target), length(length), max_speed(maxSpeed),
111 breakable(breakable), min_block_length(minBlockLength),
112 min_stop_block_length(minStopBlockLength) {};
113
129 Edge(size_t const source, size_t const target, double const length,
130 double const maxSpeed, std::optional<bool> const& breakable,
131 std::optional<double> const& min_block_length,
132 std::optional<double> const& min_stop_block_length)
133 : source(source), target(target), length(length), max_speed(maxSpeed),
138
139 // Operators
146 bool operator==(const Edge& other) const {
147 return (source == other.source && target == other.target &&
148 length == other.length && max_speed == other.max_speed &&
149 breakable == other.breakable &&
152 }
153};
154
163class Network {
164public:
172 struct VertexInput {
173 private:
174 friend class Network;
175 friend class Timetable;
176
177 std::variant<size_t, std::string_view, Vertex> m_data;
178
179 public:
180 // Implicit constructors: no explicit keyword on purpose
181 // NOLINTBEGIN(google-explicit-constructor)
182 VertexInput(size_t i) : m_data(i) {}
183 VertexInput(std::string_view v_name) : m_data(v_name) {}
184 VertexInput(Vertex vertex) : m_data(std::move(vertex)) {}
185 // NOLINTEND(google-explicit-constructor)
186
187 private:
197 size_t resolve(Network const* network) const;
198 };
199
207 struct EdgeInput {
208 private:
209 friend class Network;
210 friend class StationList;
211 friend class Route;
212
213 std::variant<size_t, std::pair<size_t, size_t>,
214 std::pair<std::string_view, std::string_view>, Edge,
215 std::string_view>
216 m_data;
217
218 public:
219 // Implicit constructors: no explicit keyword on purpose
220 // NOLINTBEGIN(google-explicit-constructor)
221 EdgeInput(size_t i) : m_data(i) {}
222 EdgeInput(size_t a, size_t b) : m_data(std::pair{a, b}) {}
223 EdgeInput(std::string_view a, std::string_view b)
224 : m_data(std::pair{a, b}) {}
225 template <typename T1, typename T2>
226 EdgeInput(const std::pair<T1, T2>& p) : EdgeInput(p.first, p.second) {}
227 EdgeInput(Edge edge) : m_data(edge) {}
228 EdgeInput(std::string_view e_name) : m_data(e_name) {}
229 // NOLINTEND(google-explicit-constructor)
230
231 private:
241 size_t resolve(Network const* network) const;
242 };
243
244private:
245 // the class ensures that m_network_name is a valid folder name on any
246 // operating system as enforced by throw_if_invalid_folder_name
247 std::string m_network_name{"UnnamedNetwork"};
248
249 std::vector<Vertex> m_vertices;
250 std::vector<Edge> m_edges;
251 std::vector<cda_rail::index_set>
252 m_successors; // for every edge, set of possible successor edges
253 std::unordered_map<std::string, size_t> m_vertex_name_to_index;
254
255 std::unordered_map<std::size_t, std::pair<size_t, double>>
256 m_new_edge_to_old_edge_after_transform; // needed if edges are
257 // discretized
258
259public:
260 // -----------------------------
261 // CONSTRUCTORS
262 // -----------------------------
263
272 explicit Network(std::string_view const networkName = "UnnamedNetwork")
273 : m_network_name(networkName) {
275 };
276
290 explicit Network(std::string_view network_name,
291 std::filesystem::path const& working_directory);
292
302 explicit Network(std::string_view const networkName,
303 std::string const& working_directory)
304 : Network(networkName, std::filesystem::path(working_directory)) {};
305
315 explicit Network(std::string_view const networkName,
316 char const* const workingDirectory)
317 : Network(networkName, std::filesystem::path(workingDirectory)) {};
318
319 // Rule of 0 suffices
320
321 // -----------------------------
322 // IMPORT / EXPORT
323 // -----------------------------
324
336 [[nodiscard]] static Network
337 import_network(std::string_view const networkName,
338 std::filesystem::path const& working_directory) {
339 return Network(networkName, working_directory);
340 };
341
353 [[nodiscard]] static Network
354 import_network(std::string_view const networkName,
355 std::string const& working_directory) {
356 return Network(networkName, working_directory);
357 };
358
369 [[nodiscard]] static Network
370 import_network(std::string_view const networkName,
371 char const* const workingDirectory) {
372 return Network(networkName, workingDirectory);
373 };
374
385 void export_network(std::filesystem::path const& working_directory) const;
386
394 void export_network(std::string const& working_directory) const {
395 export_network(std::filesystem::path(working_directory));
396 };
397
404 void export_network(char const* const workingDirectory) const {
405 export_network(std::filesystem::path(workingDirectory));
406 };
407
408 // -----------------------------
409 // GETTER
410 // -----------------------------
411
417 [[nodiscard]] bool has_vertex(size_t index) const {
418 return (index < m_vertices.size());
419 };
420
426 [[nodiscard]] bool has_vertex(std::string_view const name) const {
427 return m_vertex_name_to_index.contains(std::string{name});
428 };
429
430private:
441 [[nodiscard]] bool has_edge_helper(size_t source_id, size_t target_id) const;
442
443public:
449 [[nodiscard]] bool has_edge(size_t const index) const {
450 return (index < m_edges.size());
451 };
452
461 [[nodiscard]] bool has_edge(VertexInput const& source,
462 VertexInput const& target) const {
463 return has_edge_helper(source.resolve(this), target.resolve(this));
464 }
465
466private:
476 [[nodiscard]] bool is_valid_successor_helper(size_t e0, size_t e1) const;
477
478public:
493 [[nodiscard]] bool is_valid_successor(EdgeInput const& edge_in,
494 EdgeInput const& edge_out) const {
495 return is_valid_successor_helper(edge_in.resolve(this),
496 edge_out.resolve(this));
497 };
498
499private:
507 [[nodiscard]] bool is_adjustable_helper(size_t vertex_id) const;
508
509public:
522 [[nodiscard]] bool is_adjustable(VertexInput const& vertex) const {
523 return is_adjustable_helper(vertex.resolve(this));
524 };
525
540 [[nodiscard]] bool is_consistent_for_transformation() const;
541
556 [[nodiscard]] bool has_reversal_free_successors() const;
557
562 [[nodiscard]] size_t number_of_vertices() const { return m_vertices.size(); };
563
568 [[nodiscard]] size_t number_of_edges() const { return m_edges.size(); };
569
575 [[nodiscard]] std::string const& get_network_name() const {
576 return m_network_name;
577 }
578
584 [[nodiscard]] std::vector<Vertex> const& get_vertices() const {
585 return m_vertices;
586 };
587
594 [[nodiscard]] cda_rail::index_vector
596
602 [[nodiscard]] const std::vector<Edge>& get_edges() const { return m_edges; };
603
604private:
612 [[nodiscard]] std::pair<size_t, double>
613 get_old_edge_helper(size_t new_edge) const;
614
615public:
628 [[nodiscard]] std::pair<size_t, double>
629 get_old_edge(EdgeInput const& edge) const {
630 return get_old_edge_helper(edge.resolve(this));
631 };
632
646 [[nodiscard]] cda_rail::index_vector
647 get_old_path(const cda_rail::index_vector& path) const;
648
654 void reset_old_edges() { m_new_edge_to_old_edge_after_transform.clear(); }
655
663 [[nodiscard]] size_t get_vertex_index(std::string_view name) const;
664
675 [[nodiscard]] size_t resolve_vertex_index(VertexInput const& vertex) const {
676 return vertex.resolve(this);
677 }
678
679private:
687 [[nodiscard]] const Vertex& get_vertex_helper(size_t index) const;
688
689public:
697 [[nodiscard]] const Vertex& get_vertex(VertexInput const& vertex) const {
698 return get_vertex_helper(vertex.resolve(this));
699 }
700
701private:
712 [[nodiscard]] size_t get_edge_index_helper(size_t source_id,
713 size_t target_id) const;
714
715public:
726 [[nodiscard]] size_t get_edge_index(VertexInput const& source,
727 VertexInput const& target) const {
728 return get_edge_index_helper(source.resolve(this), target.resolve(this));
729 }
730
741 [[nodiscard]] size_t get_edge_index(EdgeInput const& edge_input) const {
742 return edge_input.resolve(this);
743 }
744
745private:
753 [[nodiscard]] const Edge& get_edge_helper(size_t index) const;
754
755public:
763 [[nodiscard]] const Edge& get_edge(EdgeInput const& edge) const {
764 return get_edge_helper(edge.resolve(this));
765 }
766
767private:
780 [[nodiscard]] std::string get_edge_name_helper(std::string_view v1,
781 std::string_view v2,
782 bool check_existence) const;
783
791 [[nodiscard]] std::string get_edge_name_helper(size_t v0, size_t v1,
792 bool check_existence) const {
793 return get_edge_name_helper(get_vertex(v0).name, get_vertex(v1).name,
794 check_existence);
795 }
796
797public:
806 [[nodiscard]] std::string get_edge_name(EdgeInput const& edge) const {
807 const auto& edge_object = get_edge(edge.resolve(this));
808 return get_edge_name_helper(get_vertex(edge_object.source).name,
809 get_vertex(edge_object.target).name, false);
810 }
811
812private:
820 [[nodiscard]] cda_rail::index_set out_edges_helper(size_t index) const;
821
829 [[nodiscard]] cda_rail::index_set in_edges_helper(size_t index) const;
830
838 [[nodiscard]] cda_rail::index_set
839 neighboring_edges_helper(size_t index) const;
840
841public:
849 [[nodiscard]] cda_rail::index_set out_edges(VertexInput const& vertex) const {
850 return out_edges_helper(vertex.resolve(this));
851 };
852
860 [[nodiscard]] cda_rail::index_set in_edges(VertexInput const& vertex) const {
861 return in_edges_helper(vertex.resolve(this));
862 };
863
872 [[nodiscard]] cda_rail::index_set
873 neighboring_edges(VertexInput const& vertex) const {
874 return neighboring_edges_helper(vertex.resolve(this));
875 };
876
877private:
889 [[nodiscard]] cda_rail::index_set get_predecessors_helper(size_t index) const;
890
898 [[nodiscard]] const cda_rail::index_set&
899 get_successors_helper(size_t index) const;
900
901public:
913 [[nodiscard]] cda_rail::index_set
914 get_predecessors(EdgeInput const& edge) const {
915 return get_predecessors_helper(edge.resolve(this));
916 }
917
925 [[nodiscard]] cda_rail::index_set const&
926 get_successors(EdgeInput const& edge) const {
927 return get_successors_helper(edge.resolve(this));
928 }
929
930private:
940 [[nodiscard]] cda_rail::index_set
941 edges_on_paths_between_helper(size_t source, size_t target) const;
942
943public:
958 [[nodiscard]] cda_rail::index_set
960 VertexInput const& target) const {
961 return edges_on_paths_between_helper(source.resolve(this),
962 target.resolve(this));
963 }
964
965private:
973 [[nodiscard]] cda_rail::index_set neighbors_helper(size_t v_index) const;
974
975public:
984 [[nodiscard]] cda_rail::index_set neighbors(VertexInput const& vertex) const {
985 return neighbors_helper(vertex.resolve(this));
986 };
987
988private:
998 [[nodiscard]] std::optional<size_t>
999 common_vertex_helper(size_t e_idx_1, size_t e_idx_2) const;
1000
1001public:
1011 [[nodiscard]] std::optional<size_t>
1012 common_vertex(EdgeInput const& edge1, EdgeInput const& edge2) const {
1013 return common_vertex_helper(edge1.resolve(this), edge2.resolve(this));
1014 };
1015
1016private:
1024 [[nodiscard]] std::optional<size_t>
1025 get_reverse_edge_index_helper(size_t edge_index) const;
1026
1027public:
1037 [[nodiscard]] std::optional<size_t>
1039 return get_reverse_edge_index_helper(edge.resolve(this));
1040 };
1041
1049 [[nodiscard]] std::optional<cda_rail::index_vector>
1050 get_reverse_path(cda_rail::index_vector const& edges) const;
1051
1061 [[nodiscard]] std::optional<size_t>
1062 get_optional_reverse_edge_index(std::optional<size_t> const edgeIndex) const {
1063 return edgeIndex.has_value()
1064 ? get_reverse_edge_index_helper(edgeIndex.value())
1065 : std::optional<size_t>();
1066 }
1067
1090 [[nodiscard]] std::vector<
1091 std::pair<std::optional<size_t>, std::optional<size_t>>>
1092 combine_reverse_edges(const cda_rail::index_vector& edges_to_consider,
1093 bool sort = false) const;
1094
1095private:
1106 [[nodiscard]] size_t get_track_index_helper(size_t edge_index) const;
1107
1108public:
1120 [[nodiscard]] size_t get_track_index(EdgeInput const& edge) const {
1121 return get_track_index_helper(edge.resolve(this));
1122 };
1123
1128 [[nodiscard]] cda_rail::index_vector breakable_edges() const;
1129
1140
1150 [[nodiscard]] std::vector<cda_rail::index_set> unbreakable_sections() const;
1151
1161 [[nodiscard]] std::vector<cda_rail::index_set> no_border_vss_sections() const;
1162
1163private:
1171 [[nodiscard]] cda_rail::index_set
1172 get_unbreakable_section_containing_edge_helper(size_t e) const;
1173
1181 [[nodiscard]] bool is_on_same_unbreakable_section_helper(size_t e1,
1182 size_t e2) const;
1183
1184public:
1196 [[nodiscard]] cda_rail::index_set
1198 return get_unbreakable_section_containing_edge_helper(edge.resolve(this));
1199 };
1200
1209 [[nodiscard]] bool
1211 EdgeInput const& edge2) const {
1212 return is_on_same_unbreakable_section_helper(edge1.resolve(this),
1213 edge2.resolve(this));
1214 };
1215
1223 [[nodiscard]] cda_rail::index_set
1224 vertices_used_by_edges(const cda_rail::index_set& edges_tmp) const;
1225
1236 [[nodiscard]] static std::vector<std::pair<size_t, size_t>>
1238 const std::vector<cda_rail::index_set>& ttd);
1239
1247 [[nodiscard]] cda_rail::index_set
1248 edge_set_complement(const cda_rail::index_set& edge_indices) const;
1249
1258 [[nodiscard]] cda_rail::index_set
1259 edge_set_complement(const cda_rail::index_set& edge_indices,
1260 const cda_rail::index_set& edges_to_consider) const;
1261
1262private:
1270 [[nodiscard]] double maximal_vertex_speed_helper(
1271 size_t vertex_id, const cda_rail::index_set& edges_to_consider) const;
1272
1273public:
1289 [[nodiscard]] double maximal_vertex_speed(
1290 VertexInput const& vertex,
1291 const cda_rail::index_set& edges_to_consider = {}) const {
1292 return maximal_vertex_speed_helper(vertex.resolve(this), edges_to_consider);
1293 };
1294
1295private:
1303 [[nodiscard]] double minimal_neighboring_edge_length_helper(
1304 size_t v, const cda_rail::index_set& edges_to_consider) const;
1305
1306public:
1319 VertexInput const& vertex,
1320 const cda_rail::index_set& edges_to_consider = {}) const {
1321 return minimal_neighboring_edge_length_helper(vertex.resolve(this),
1322 edges_to_consider);
1323 };
1324
1325private:
1337 [[nodiscard]] size_t
1338 max_vss_on_edge_helper(size_t index,
1339 const vss::SeparationFunction& sep_func) const;
1340
1341public:
1358 [[nodiscard]] size_t max_vss_on_edge(EdgeInput const& edge) const {
1359 return max_vss_on_edge(edge, vss::UNIFORM);
1360 };
1361
1376 [[nodiscard]] size_t
1378 const vss::SeparationFunction& sep_func) const {
1379 return max_vss_on_edge_helper(edge.resolve(this), sep_func);
1380 };
1381
1382 // -----------------------------
1383 // SETTER
1384 // -----------------------------
1385
1393 void set_network_name(std::string_view const networkName) {
1395 m_network_name = networkName;
1396 }
1397
1398private:
1408 void change_vertex_name_helper(size_t index, std::string_view new_name);
1409
1417 void change_vertex_type_helper(size_t index, VertexType new_type);
1418
1426 void change_vertex_headway_helper(size_t index, double new_headway);
1427
1428public:
1439 std::string_view const newName) {
1440 change_vertex_name_helper(vertex.resolve(this), newName);
1441 };
1442
1450 void change_vertex_type(VertexInput const& vertex, VertexType const newType) {
1451 change_vertex_type_helper(vertex.resolve(this), newType);
1452 };
1453
1462 double const newHeadway) {
1463 change_vertex_headway_helper(vertex.resolve(this), newHeadway);
1464 };
1465
1466private:
1474 void change_edge_length_helper(size_t index, double new_length);
1475
1483 void change_edge_max_speed_helper(size_t index, double new_max_speed);
1484
1492 void change_edge_min_block_length_helper(size_t index,
1493 double new_min_block_length);
1494
1502 void
1503 change_edge_min_stop_block_length_helper(size_t index,
1504 double new_min_stop_block_length);
1505
1513 void change_edge_breakable_helper(size_t index, bool value);
1514
1515public:
1523 void change_edge_length(EdgeInput const& edge, double new_length) {
1524 change_edge_length_helper(edge.resolve(this), new_length);
1525 };
1526
1534 void change_edge_max_speed(EdgeInput const& edge, double new_max_speed) {
1535 change_edge_max_speed_helper(edge.resolve(this), new_max_speed);
1536 }
1537
1546 double new_min_block_length) {
1547 change_edge_min_block_length_helper(edge.resolve(this),
1548 new_min_block_length);
1549 }
1550
1559 double new_min_stop_block_length) {
1560 change_edge_min_stop_block_length_helper(edge.resolve(this),
1561 new_min_stop_block_length);
1562 }
1563
1570 void set_edge_breakable(EdgeInput const& edge) {
1571 change_edge_breakable_status(edge, true);
1572 };
1573
1581 change_edge_breakable_status(edge, false);
1582 };
1583
1592 bool const breakable) {
1593 change_edge_breakable_helper(edge.resolve(this), breakable);
1594 };
1595
1603 size_t add_vertex(Vertex vertex);
1604
1614 size_t add_vertex(std::string_view name, VertexType type,
1615 double headway = Vertex::HEADWAY_DEFAULT) {
1616 return add_vertex(Vertex(name, type, headway));
1617 }
1618
1629 size_t add_vertex(std::string_view name, VertexType type,
1630 std::optional<double> const& headway) {
1631 return add_vertex(Vertex(name, type, headway));
1632 }
1633
1634private:
1650 size_t add_edge_helper(size_t source, size_t target, double length,
1651 double max_speed, std::optional<bool> const& breakable,
1652 std::optional<double> const& min_block_length,
1653 std::optional<double> const& min_stop_block_length);
1654
1655public:
1676 size_t add_edge(VertexInput const& source, VertexInput const& target,
1677 double const length, double const maxSpeed,
1678 std::optional<bool> const& breakable = {},
1679 std::optional<double> const& min_block_length = {},
1680 std::optional<double> const& min_stop_block_length = {}) {
1681 return add_edge_helper(source.resolve(this), target.resolve(this), length,
1682 maxSpeed, breakable, min_block_length,
1683 min_stop_block_length);
1684 }
1685
1708 std::pair<size_t, size_t> add_bidirectional_edge(
1709 VertexInput const& source, VertexInput const& target, double length,
1710 double max_speed, std::optional<bool> const& breakable = {},
1711 std::optional<double> const& min_block_length = {},
1712 std::optional<double> const& min_stop_block_length = {});
1713
1714private:
1728 void add_successor_helper(size_t edge_in, size_t edge_out);
1729
1730public:
1744 void add_successor(EdgeInput const& edge_in, EdgeInput const& edge_out) {
1745 // Pass 'this' to the inputs so they can resolve themselves
1746 add_successor_helper(edge_in.resolve(this), edge_out.resolve(this));
1747 }
1748
1749private:
1761 void add_successors_via_helper(size_t from, size_t via, size_t to);
1762
1771 void throw_if_not_exactly_the_neighbors(
1772 size_t vertex, cda_rail::index_vector const& vertices) const;
1773
1774public:
1784
1801 void add_turnout_successors(VertexInput const& turnout,
1802 VertexInput const& toe,
1803 VertexInput const& branch_1,
1804 VertexInput const& branch_2);
1805
1825 void add_crossing_successors(VertexInput const& crossing,
1826 VertexInput const& track_1_a,
1827 VertexInput const& track_1_b,
1828 VertexInput const& track_2_a,
1829 VertexInput const& track_2_b);
1830
1852 void add_double_slip_successors(VertexInput const& double_slip,
1853 VertexInput const& track_1_a,
1854 VertexInput const& track_1_b,
1855 VertexInput const& track_2_a,
1856 VertexInput const& track_2_b);
1857
1858 // ----------------------
1859 // Transformation Functions
1860 // ----------------------
1861
1876 std::pair<cda_rail::index_vector, cda_rail::index_vector>
1878 const vss::SeparationFunction& sep_func = vss::UNIFORM) {
1879 auto const edge_index = edge.resolve(this);
1880 return separate_edge_private_helper(
1881 edge_index, get_edge(edge_index).min_block_length, sep_func);
1882 };
1883
1893 std::pair<cda_rail::index_vector, cda_rail::index_vector>
1895 auto const edge_index = edge.resolve(this);
1896 return separate_edge_private_helper(
1897 edge_index, get_edge(edge_index).min_stop_block_length, vss::UNIFORM,
1898 true);
1899 };
1900
1909 std::vector<std::pair<size_t, cda_rail::index_vector>>
1910 separate_stop_edges(const cda_rail::index_set& stop_edges);
1911
1922 std::vector<std::pair<size_t, cda_rail::index_vector>>
1924
1941 std::vector<std::pair<size_t, cda_rail::index_vector>>
1942 separate_edges_at_vss(const std::vector<std::vector<double>>& vss_positions);
1943
1944 // ------------------------
1945 // Path Finding Algorithms
1946 // ------------------------
1947
1965 [[nodiscard]] std::vector<cda_rail::index_vector>
1967 size_t v, double desired_len, std::optional<size_t> exit_node = {},
1968 const cda_rail::index_set& edges_to_consider = {},
1969 bool return_successors_if_zero = false) const {
1970 return all_routes_of_given_length(v, std::nullopt, desired_len, false,
1971 exit_node, edges_to_consider,
1972 return_successors_if_zero);
1973 };
1974
1986 [[nodiscard]] std::vector<cda_rail::index_vector>
1988 size_t e, double desired_len, std::optional<size_t> exit_node = {},
1989 const cda_rail::index_set& edges_to_consider = {}) const {
1990 return all_routes_of_given_length(std::nullopt, e, desired_len, false,
1991 exit_node, edges_to_consider);
1992 };
1993
2004 [[nodiscard]] std::vector<cda_rail::index_vector>
2006 size_t v, double desired_len, std::optional<size_t> exit_node = {},
2007 const cda_rail::index_set& edges_to_consider = {}) const {
2008 return all_routes_of_given_length(v, std::nullopt, desired_len, true,
2009 exit_node, edges_to_consider);
2010 };
2011
2022 [[nodiscard]] std::vector<cda_rail::index_vector>
2024 size_t const e, double const desiredLen,
2025 std::optional<size_t> exit_node = {},
2026 const cda_rail::index_set& edges_to_consider = {}) const {
2027 return all_routes_of_given_length(std::nullopt, e, desiredLen, true,
2028 exit_node, edges_to_consider);
2029 }
2030
2031private:
2052 [[nodiscard]] std::vector<cda_rail::index_vector>
2053 all_paths_ending_at_ttd_helper(
2054 size_t e_0, const std::vector<cda_rail::index_set>& ttd_sections,
2055 std::optional<size_t> exit_node, bool skip_irrelevant_ttds,
2056 bool zero_length_if_next_edge_is_ttd) const;
2057
2058public:
2067 [[nodiscard]] static std::optional<size_t>
2068 get_ttd_of_edge(size_t e_0,
2069 const std::vector<cda_rail::index_set>& ttd_sections);
2070
2092 [[nodiscard]] std::vector<cda_rail::index_vector>
2094 const std::vector<cda_rail::index_set>& ttd_sections,
2095 std::optional<size_t> exit_node = {},
2096 bool skip_irrelevant_ttds = false,
2097 bool zero_length_if_next_edge_is_ttd = false) const {
2098 return all_paths_ending_at_ttd_helper(edge.resolve(this), ttd_sections,
2099 exit_node, skip_irrelevant_ttds,
2100 zero_length_if_next_edge_is_ttd);
2101 };
2102
2109 [[nodiscard]] double length_of_path(const cda_rail::index_vector& path) const;
2110
2121 [[nodiscard]] std::vector<std::vector<double>>
2123
2124private:
2146 [[nodiscard]] std::optional<double>
2147 shortest_path_helper(size_t source_edge_id, size_t target_id,
2148 bool target_is_edge = false,
2149 bool include_first_edge = false,
2150 bool use_minimal_time = false, double max_v = INF) const;
2151
2152public:
2170 [[nodiscard]] std::optional<double> shortest_path_from_edge_to_edge(
2171 EdgeInput const& source_edge, EdgeInput const& target_edge,
2172 bool include_first_edge = false, bool use_minimal_time = false,
2173 double max_v = INF) const {
2174 return shortest_path_helper(source_edge.resolve(this),
2175 target_edge.resolve(this), true,
2176 include_first_edge, use_minimal_time, max_v);
2177 };
2178
2198 [[nodiscard]] std::optional<double> shortest_path_from_edge_to_vertex(
2199 EdgeInput const& source_edge, VertexInput const& target_vertex,
2200 bool include_first_edge = false, bool use_minimal_time = false,
2201 double max_v = INF) const {
2202 return shortest_path_helper(source_edge.resolve(this),
2203 target_vertex.resolve(this), false,
2204 include_first_edge, use_minimal_time, max_v);
2205 };
2206
2207private:
2227 [[nodiscard]] std::pair<std::optional<double>, cda_rail::index_vector>
2228 shortest_path_using_edges_helper(size_t source_edge_id,
2229 size_t target_vertex_id,
2230 bool only_use_valid_successors = true,
2231 const cda_rail::index_set& edges_to_use = {},
2232 bool target_is_edge = false,
2233 bool include_first_edge = false,
2234 bool use_minimal_time = false,
2235 double max_v = INF) const {
2236 return shortest_path_between_sets_using_edges_helper(
2237 cda_rail::index_set{source_edge_id},
2238 cda_rail::index_set{target_vertex_id}, only_use_valid_successors,
2239 edges_to_use, target_is_edge, include_first_edge, use_minimal_time,
2240 max_v);
2241 };
2269 [[nodiscard]] std::pair<std::optional<double>, cda_rail::index_vector>
2270 shortest_path_between_sets_using_edges_helper(
2271 const cda_rail::index_set& source_edge_ids,
2272 const cda_rail::index_set& target_ids,
2273 bool only_use_valid_successors = true,
2274 const cda_rail::index_set& edges_to_use = {}, bool target_is_edge = false,
2275 bool include_first_edge = false, bool use_minimal_time = false,
2276 double max_v = INF) const;
2277
2291 [[nodiscard]] std::optional<double> shortest_path_length_between_sets_helper(
2292 const cda_rail::index_set& source_edge_ids,
2293 const cda_rail::index_set& target_ids, bool target_is_edge = false,
2294 bool include_first_edge = false, bool use_minimal_time = false,
2295 double max_v = INF) const {
2296 return shortest_path_between_sets_using_edges_helper(
2297 source_edge_ids, target_ids, true, {}, target_is_edge,
2298 include_first_edge, use_minimal_time, max_v)
2299 .first;
2300 };
2301
2326 [[nodiscard]] std::optional<size_t> dijkstra_on_edges_helper(
2327 const cda_rail::index_set& source_edge_ids,
2328 bool only_use_valid_successors, const cda_rail::index_set& edges_to_use,
2329 bool include_first_edge, bool use_minimal_time, double max_v,
2330 const cda_rail::index_set& target_edges, std::vector<double>& distances,
2331 cda_rail::index_vector& predecessors) const;
2332
2333public:
2355 [[nodiscard]] std::vector<double> shortest_path_lengths_to_all_vertices(
2356 const cda_rail::index_set& source_edge_ids,
2357 bool include_first_edge = false, bool use_minimal_time = false,
2358 double max_v = INF) const;
2359
2376 [[nodiscard]] std::pair<std::optional<double>, cda_rail::index_vector>
2378 EdgeInput const& target_edge,
2379 bool only_use_valid_successors = true,
2380 const cda_rail::index_set& edges_to_use = {},
2381 bool include_first_edge = false,
2382 bool use_minimal_time = false,
2383 double max_v = INF) const {
2384 return shortest_path_using_edges_helper(
2385 source_edge.resolve(this), target_edge.resolve(this),
2386 only_use_valid_successors, edges_to_use, true, include_first_edge,
2387 use_minimal_time, max_v);
2388 };
2389
2408 [[nodiscard]] std::pair<std::optional<double>, cda_rail::index_vector>
2410 VertexInput const& target_vertex,
2411 bool only_use_valid_successors = true,
2412 const cda_rail::index_set& edges_to_use = {},
2413 bool include_first_edge = false,
2414 bool use_minimal_time = false,
2415 double max_v = INF) const {
2416 return shortest_path_using_edges_helper(
2417 source_edge.resolve(this), target_vertex.resolve(this),
2418 only_use_valid_successors, edges_to_use, false, include_first_edge,
2419 use_minimal_time, max_v);
2420 };
2421
2438 [[nodiscard]] std::pair<std::optional<double>, cda_rail::index_vector>
2440 const cda_rail::index_set& target_edge_ids,
2441 bool only_use_valid_successors = true,
2442 const cda_rail::index_set& edges_to_use = {},
2443 bool include_first_edge = false,
2444 bool use_minimal_time = false,
2445 double max_v = INF) const {
2446 return shortest_path_between_sets_using_edges_helper(
2447 source_edge_ids, target_edge_ids, only_use_valid_successors,
2448 edges_to_use, true, include_first_edge, use_minimal_time, max_v);
2449 };
2450
2469 [[nodiscard]] std::pair<std::optional<double>, cda_rail::index_vector>
2471 const cda_rail::index_set& source_edge_ids,
2472 const cda_rail::index_set& target_vertex_ids,
2473 bool only_use_valid_successors = true,
2474 const cda_rail::index_set& edges_to_use = {},
2475 bool include_first_edge = false, bool use_minimal_time = false,
2476 double max_v = INF) const {
2477 return shortest_path_between_sets_using_edges_helper(
2478 source_edge_ids, target_vertex_ids, only_use_valid_successors,
2479 edges_to_use, false, include_first_edge, use_minimal_time, max_v);
2480 };
2481
2493 [[nodiscard]] std::optional<double> shortest_path_length_between_edge_sets(
2494 const cda_rail::index_set& source_edge_ids,
2495 const cda_rail::index_set& target_ids, bool include_first_edge = false,
2496 bool use_minimal_time = false, double max_v = INF) const {
2497 return shortest_path_length_between_sets_helper(source_edge_ids, target_ids,
2498 true, include_first_edge,
2499 use_minimal_time, max_v);
2500 };
2501
2513 [[nodiscard]] std::optional<double>
2515 const cda_rail::index_set& source_edge_ids,
2516 const cda_rail::index_set& target_vertex_ids,
2517 bool include_first_edge = false, bool use_minimal_time = false,
2518 double max_v = INF) const {
2519 return shortest_path_length_between_sets_helper(
2520 source_edge_ids, target_vertex_ids, false, include_first_edge,
2521 use_minimal_time, max_v);
2522 };
2523
2524 // -------------------------
2525 // Irrelevant TTD helper
2526 // -------------------------
2535 [[nodiscard]] std::unordered_set<size_t>
2536 get_border_vertices_of_ttd(const cda_rail::index_set& ttd_section) const;
2537
2538private:
2547 [[nodiscard]] bool
2548 has_ttd_path_helper(size_t source_vertex, size_t target_vertex,
2549 const cda_rail::index_set& ttd_section) const;
2550
2551public:
2561 [[nodiscard]] bool
2562 has_ttd_path(VertexInput const& source_vertex,
2563 VertexInput const& target_vertex,
2564 const cda_rail::index_set& ttd_section) const {
2565 return has_ttd_path_helper(source_vertex.resolve(this),
2566 target_vertex.resolve(this), ttd_section);
2567 }
2568
2569private:
2578 [[nodiscard]] bool has_ttd_path_not_using_border_vertex_helper(
2579 size_t border_vertex, const cda_rail::index_set& ttd_section) const;
2580
2581public:
2592 VertexInput const& border_vertex,
2593 const cda_rail::index_set& ttd_section) const {
2594 return has_ttd_path_not_using_border_vertex_helper(
2595 border_vertex.resolve(this), ttd_section);
2596 }
2597
2598private:
2599 // ---------------------
2600 // Private Helper
2601 // ---------------------
2602
2610 void read_graphml(const std::filesystem::path& p);
2611
2633 static void get_keys_inplace(
2634 tinyxml2::XMLElement* graphml_body, std::optional<std::string>& breakable,
2635 std::optional<std::string>& length, std::optional<std::string>& max_speed,
2636 std::optional<std::string>& min_block_length,
2637 std::optional<std::string>& min_stop_block_length,
2638 std::optional<std::string>& type, std::optional<std::string>& headway);
2639
2650 void add_vertices_from_graphml(const tinyxml2::XMLElement* graphml_node,
2651 const std::optional<std::string>& type,
2652 const std::optional<std::string>& headway);
2653
2663 static void extract_vertices_from_key_inplace(const std::string& key,
2664 std::string& source_name,
2665 std::string& target_name);
2666
2684 void add_edges_from_graphml(
2685 const tinyxml2::XMLElement* graphml_edge,
2686 const std::optional<std::string>& breakable,
2687 const std::optional<std::string>& length,
2688 const std::optional<std::string>& max_speed,
2689 const std::optional<std::string>& min_block_length,
2690 const std::optional<std::string>& min_stop_block_length);
2691
2697 void read_successors(const std::filesystem::path& p);
2698
2703 void export_graphml(const std::filesystem::path& p) const;
2704
2710 void export_successors_cpp(const std::filesystem::path& p) const;
2711
2718 [[nodiscard]] size_t other_vertex(size_t e, size_t v) const {
2719 return get_edge(e).source == v ? get_edge(e).target : get_edge(e).source;
2720 };
2721
2740 std::pair<cda_rail::index_vector, cda_rail::index_vector>
2741 separate_edge_private_helper(
2742 size_t edge_index, double min_length,
2743 const vss::SeparationFunction& sep_func = vss::UNIFORM,
2744 bool new_edge_breakable = false);
2745
2772 std::pair<cda_rail::index_vector, cda_rail::index_vector>
2773 separate_edge_at(size_t edge_index,
2774 const std::vector<double>& distances_from_source,
2775 bool new_edge_breakable = false);
2788 void update_new_old_edge(size_t new_edge, size_t old_edge, double position);
2789
2809 std::vector<std::pair<std::optional<size_t>, std::optional<size_t>>>
2810 sort_edge_pairs(
2811 std::vector<std::pair<std::optional<size_t>, std::optional<size_t>>>&
2812 edge_pairs) const;
2813
2828 void check_new_edge_requirements(size_t source, size_t target) const;
2829
2844 [[nodiscard]] static double delta_dist_helper(const Edge& successor_edge,
2845 double max_v,
2846 bool use_minimal_time);
2847
2868 void
2869 dfs_inplace(std::vector<cda_rail::index_set>& ret_val,
2870 std::unordered_set<size_t>& vertices_to_visit,
2871 const VertexType& section_type,
2872 const std::unordered_set<VertexType>& error_types = {}) const;
2900 [[nodiscard]] std::vector<cda_rail::index_vector> all_routes_of_given_length(
2901 std::optional<size_t> v_0, std::optional<size_t> e_0,
2902 double desired_length, bool reverse_direction,
2903 std::optional<size_t> exit_node = {},
2904 const cda_rail::index_set& edges_used_by_train = {},
2905 bool return_successors_if_zero = false) const;
2906
2931 [[nodiscard]] std::vector<cda_rail::index_vector>
2932 all_paths_ending_at_ttd_recursive_helper(
2933 size_t e_0, const std::vector<cda_rail::index_set>& ttd_sections,
2934 std::optional<size_t> exit_node, std::optional<size_t> safe_ttd,
2935 bool first_edge, bool skip_irrelevant_ttds) const;
2936
2937public:
2938 // exception helper
2948 EdgeInput const& edge_out) const {
2949 if (!is_valid_successor(edge_in, edge_out)) {
2952 get_edge_name(edge_out),
2953 " is not a valid successor of ",
2954 get_edge_name(edge_in),
2955 ".",
2956 }));
2957 }
2958 }
2959};
2960} // namespace cda_rail
2961
2962#endif // CDA_RAIL_DATASTRUCTURE_RAILWAYNETWORK_HPP
Directed multigraph representing a railway network.
Definition RailwayNetwork.hpp:163
cda_rail::index_set in_edges(VertexInput const &vertex) const
Returns the set of edge indices of all edges entering vertex.
Definition RailwayNetwork.hpp:860
static Network import_network(std::string_view const networkName, std::string const &working_directory)
Convenience overload of import_network accepting a std::string path.
Definition RailwayNetwork.hpp:354
void reset_old_edges()
Forgets from which edges earlier transformations derived the edges, so that get_old_edge and get_old_...
Definition RailwayNetwork.hpp:654
double maximal_vertex_speed(VertexInput const &vertex, const cda_rail::index_set &edges_to_consider={}) const
Returns the maximum speed achievable at vertex.
Definition RailwayNetwork.hpp:1289
std::optional< size_t > get_optional_reverse_edge_index(std::optional< size_t > const edgeIndex) const
Wraps get_reverse_edge_index to propagate an optional input.
Definition RailwayNetwork.hpp:1062
static std::vector< std::pair< size_t, size_t > > get_intersecting_ttd(const cda_rail::index_vector &edges, const std::vector< cda_rail::index_set > &ttd)
For each TTD section that intersects edges, returns the TTD index together with the position of the f...
Definition RailwayNetwork.cpp:605
size_t get_track_index(EdgeInput const &edge) const
Returns the canonical track index for edge.
Definition RailwayNetwork.hpp:1120
std::string const & get_network_name() const
Returns the network name.
Definition RailwayNetwork.hpp:575
size_t get_edge_index(EdgeInput const &edge_input) const
Resolves an edge descriptor to its numeric edge index.
Definition RailwayNetwork.hpp:741
Network(std::string_view const networkName, char const *const workingDirectory)
Convenience overload accepting a C-string path.
Definition RailwayNetwork.hpp:315
cda_rail::index_vector breakable_edges() const
Returns the indices of all breakable edges, sorted by index.
Definition RailwayNetwork.cpp:462
const Edge & get_edge(EdgeInput const &edge) const
Returns a const reference to the specified edge.
Definition RailwayNetwork.hpp:763
bool has_edge(size_t const index) const
Checks whether a numeric edge index is valid.
Definition RailwayNetwork.hpp:449
std::vector< cda_rail::index_vector > all_paths_ending_at_ttd(EdgeInput const &edge, const std::vector< cda_rail::index_set > &ttd_sections, std::optional< size_t > exit_node={}, bool skip_irrelevant_ttds=false, bool zero_length_if_next_edge_is_ttd=false) const
Returns all paths starting immediately after edge e_0 and ending upon entering a TTD section or reach...
Definition RailwayNetwork.hpp:2093
void change_vertex_type(VertexInput const &vertex, VertexType const newType)
Changes the type of a vertex.
Definition RailwayNetwork.hpp:1450
void change_edge_min_stop_block_length(EdgeInput const &edge, double new_min_stop_block_length)
Changes the minimum stop-block length of an edge.
Definition RailwayNetwork.hpp:1558
bool has_ttd_path_not_using_border_vertex(VertexInput const &border_vertex, const cda_rail::index_set &ttd_section) const
Checks if a TTD can be traversed without using a given border vertex.
Definition RailwayNetwork.hpp:2591
const std::vector< Edge > & get_edges() const
Returns a const reference to the underlying edge container.
Definition RailwayNetwork.hpp:602
bool has_edge(VertexInput const &source, VertexInput const &target) const
Checks whether a directed edge between two vertices exists.
Definition RailwayNetwork.hpp:461
std::optional< double > shortest_path_length_between_edge_sets(const cda_rail::index_set &source_edge_ids, const cda_rail::index_set &target_ids, bool include_first_edge=false, bool use_minimal_time=false, double max_v=INF) const
Returns the length of the shortest path from any edge in source_edge_ids to any edge in target_ids.
Definition RailwayNetwork.hpp:2493
cda_rail::index_vector get_old_path(const cda_rail::index_vector &path) const
Returns the path of the network before its transformations that corresponds to path.
Definition RailwayNetwork.cpp:198
std::pair< cda_rail::index_vector, cda_rail::index_vector > separate_edge(EdgeInput const &edge, const vss::SeparationFunction &sep_func=vss::UNIFORM)
Separates a breakable edge (and its reverse, if present) into multiple sub-edges according to sep_fun...
Definition RailwayNetwork.hpp:1877
void add_double_slip_successors(VertexInput const &double_slip, VertexInput const &track_1_a, VertexInput const &track_1_b, VertexInput const &track_2_a, VertexInput const &track_2_b)
Registers the successors of a double slip switch, i.e., a diamond crossing whose tracks are additiona...
Definition RailwayNetwork.cpp:913
static Network import_network(std::string_view const networkName, char const *const workingDirectory)
Convenience overload of import_network accepting a C-string path.
Definition RailwayNetwork.hpp:370
cda_rail::index_vector get_vertices_by_type(VertexType type) const
Returns the indices of all vertices of the specified type, ordered by vertex index.
Definition RailwayNetwork.cpp:176
void change_edge_max_speed(EdgeInput const &edge, double new_max_speed)
Changes the maximum speed of an edge.
Definition RailwayNetwork.hpp:1534
std::vector< cda_rail::index_vector > all_paths_of_length_ending_in_edge(size_t const e, double const desiredLen, std::optional< size_t > exit_node={}, const cda_rail::index_set &edges_to_consider={}) const
Returns all edge paths of length at least desiredLen ending at edge e.
Definition RailwayNetwork.hpp:2023
size_t number_of_edges() const
Returns the total number of edges in the network.
Definition RailwayNetwork.hpp:568
cda_rail::index_set get_predecessors(EdgeInput const &edge) const
Returns all predecessor edges of edge.
Definition RailwayNetwork.hpp:914
size_t add_edge(VertexInput const &source, VertexInput const &target, double const length, double const maxSpeed, std::optional< bool > const &breakable={}, std::optional< double > const &min_block_length={}, std::optional< double > const &min_stop_block_length={})
Adds a directed edge between two vertices.
Definition RailwayNetwork.hpp:1676
size_t max_vss_on_edge(EdgeInput const &edge, const vss::SeparationFunction &sep_func) const
Returns the maximum number of VSS borders that can be placed on edge if their positions are given by ...
Definition RailwayNetwork.hpp:1377
size_t add_vertex(std::string_view name, VertexType type, double headway=Vertex::HEADWAY_DEFAULT)
Constructs a vertex and adds it to the network.
Definition RailwayNetwork.hpp:1614
void export_network(std::string const &working_directory) const
Convenience overload of export_network accepting a std::string path.
Definition RailwayNetwork.hpp:394
void add_turnout_successors(VertexInput const &turnout, VertexInput const &toe, VertexInput const &branch_1, VertexInput const &branch_2)
Registers the successors of a simple turnout.
Definition RailwayNetwork.cpp:882
void change_edge_breakable_status(EdgeInput const &edge, bool const breakable)
Sets the breakable flag of an edge.
Definition RailwayNetwork.hpp:1591
std::pair< std::optional< double >, cda_rail::index_vector > shortest_path_between_edge_and_vertex_set(const cda_rail::index_set &source_edge_ids, const cda_rail::index_set &target_vertex_ids, bool only_use_valid_successors=true, const cda_rail::index_set &edges_to_use={}, bool include_first_edge=false, bool use_minimal_time=false, double max_v=INF) const
Returns the shortest path and distance from any edge in source_edge_ids to any vertex in target_verte...
Definition RailwayNetwork.hpp:2470
std::pair< size_t, double > get_old_edge(EdgeInput const &edge) const
Returns the pre-discretization edge and position for an edge that may have been created by a transfor...
Definition RailwayNetwork.hpp:629
const Vertex & get_vertex(VertexInput const &vertex) const
Returns a const reference to the specified vertex.
Definition RailwayNetwork.hpp:697
std::vector< std::pair< size_t, cda_rail::index_vector > > separate_edges_at_vss(const std::vector< std::vector< double > > &vss_positions)
Splits the breakable edges at the given positions, which become VSS border vertices.
Definition RailwayNetwork.cpp:976
std::vector< std::pair< size_t, cda_rail::index_vector > > separate_stop_edges(const cda_rail::index_set &stop_edges)
Separates all edges in stop_edges whose length is at least twice their min_stop_block_length.
Definition RailwayNetwork.cpp:927
std::optional< double > shortest_path_from_edge_to_vertex(EdgeInput const &source_edge, VertexInput const &target_vertex, bool include_first_edge=false, bool use_minimal_time=false, double max_v=INF) const
Returns the shortest distance from source_edge to target_vertex.
Definition RailwayNetwork.hpp:2198
void change_edge_length(EdgeInput const &edge, double new_length)
Changes the length of an edge.
Definition RailwayNetwork.hpp:1523
std::pair< std::optional< double >, cda_rail::index_vector > shortest_path_between_edge_sets(const cda_rail::index_set &source_edge_ids, const cda_rail::index_set &target_edge_ids, bool only_use_valid_successors=true, const cda_rail::index_set &edges_to_use={}, bool include_first_edge=false, bool use_minimal_time=false, double max_v=INF) const
Returns the shortest path and distance from any edge in source_edge_ids to any edge in target_edge_id...
Definition RailwayNetwork.hpp:2439
size_t get_vertex_index(std::string_view name) const
Returns the index of the vertex with the given name.
Definition RailwayNetwork.cpp:209
bool is_adjustable(VertexInput const &vertex) const
Checks whether a vertex is adjustable by certain algorithmic approaches.
Definition RailwayNetwork.hpp:522
std::vector< cda_rail::index_vector > all_paths_of_length_starting_in_edge(size_t e, double desired_len, std::optional< size_t > exit_node={}, const cda_rail::index_set &edges_to_consider={}) const
Returns all edge paths of length at least desired_len starting immediately after edge e (i....
Definition RailwayNetwork.hpp:1987
void set_edge_unbreakable(EdgeInput const &edge)
Marks an edge as unbreakable (no VSS borders may be placed on it).
Definition RailwayNetwork.hpp:1580
void set_network_name(std::string_view const networkName)
Sets the network name.
Definition RailwayNetwork.hpp:1393
bool has_vertex(std::string_view const name) const
Checks whether a vertex with the given name exists.
Definition RailwayNetwork.hpp:426
std::optional< size_t > get_reverse_edge_index(EdgeInput const &edge) const
Returns the index of the edge with reversed endpoints relative to edge.
Definition RailwayNetwork.hpp:1038
std::pair< std::optional< double >, cda_rail::index_vector > shortest_edge_to_edge_path(EdgeInput const &source_edge, EdgeInput const &target_edge, bool only_use_valid_successors=true, const cda_rail::index_set &edges_to_use={}, bool include_first_edge=false, bool use_minimal_time=false, double max_v=INF) const
Returns the shortest path and its length from source_edge to target_edge.
Definition RailwayNetwork.hpp:2377
std::optional< size_t > common_vertex(EdgeInput const &edge1, EdgeInput const &edge2) const
Returns the common endpoint of two edges, if one exists.
Definition RailwayNetwork.hpp:1012
cda_rail::index_set vertices_used_by_edges(const cda_rail::index_set &edges_tmp) const
Returns all vertex indices that are endpoints of at least one edge in edges_tmp.
Definition RailwayNetwork.cpp:595
std::unordered_set< size_t > get_border_vertices_of_ttd(const cda_rail::index_set &ttd_section) const
Returns the border vertices of a given TTD section.
Definition RailwayNetwork.cpp:1273
size_t resolve_vertex_index(VertexInput const &vertex) const
Resolves a vertex descriptor to its numeric vertex index.
Definition RailwayNetwork.hpp:675
static Network import_network(std::string_view const networkName, std::filesystem::path const &working_directory)
Loads a network from disk and returns it by value.
Definition RailwayNetwork.hpp:337
size_t add_vertex(Vertex vertex)
Adds a vertex to the network.
Definition RailwayNetwork.cpp:802
std::vector< cda_rail::index_vector > all_paths_of_length_starting_in_vertex(size_t v, double desired_len, std::optional< size_t > exit_node={}, const cda_rail::index_set &edges_to_consider={}, bool return_successors_if_zero=false) const
Returns all edge paths of length at least desired_len starting at vertex v.
Definition RailwayNetwork.hpp:1966
Network(std::string_view const networkName="UnnamedNetwork")
Default-constructs an empty network.
Definition RailwayNetwork.hpp:272
bool has_reversal_free_successors() const
Checks that no successor lets a train reverse its direction of travel.
Definition RailwayNetwork.cpp:136
std::vector< Vertex > const & get_vertices() const
Returns a const reference to the underlying vertex container.
Definition RailwayNetwork.hpp:584
bool is_on_same_unbreakable_section(EdgeInput const &edge1, EdgeInput const &edge2) const
Checks whether two edges belong to the same unbreakable section.
Definition RailwayNetwork.hpp:1210
void set_edge_breakable(EdgeInput const &edge)
Marks an edge as breakable (VSS borders may be placed on it).
Definition RailwayNetwork.hpp:1570
bool is_consistent_for_transformation() const
Checks network consistency required for edge-separation transformations.
Definition RailwayNetwork.cpp:98
bool has_vertex(size_t index) const
Checks whether a numeric vertex index is valid.
Definition RailwayNetwork.hpp:417
cda_rail::index_set edge_set_complement(const cda_rail::index_set &edge_indices) const
Returns the set of all network edge indices not in edge_indices.
Definition RailwayNetwork.cpp:621
void export_network(char const *const workingDirectory) const
Convenience overload of export_network accepting a C-string path.
Definition RailwayNetwork.hpp:404
std::optional< double > shortest_path_length_between_edge_and_vertex_set(const cda_rail::index_set &source_edge_ids, const cda_rail::index_set &target_vertex_ids, bool include_first_edge=false, bool use_minimal_time=false, double max_v=INF) const
Returns the length of the shortest path from any edge in source_edge_ids to any vertex in target_vert...
Definition RailwayNetwork.hpp:2514
cda_rail::index_set neighbors(VertexInput const &vertex) const
Returns the set of vertex indices adjacent to vertex via any incident edge.
Definition RailwayNetwork.hpp:984
std::pair< cda_rail::index_vector, cda_rail::index_vector > separate_stop_edge(EdgeInput const &edge)
Separates a breakable edge using min_stop_block_length with uniform separation.
Definition RailwayNetwork.hpp:1894
std::vector< double > shortest_path_lengths_to_all_vertices(const cda_rail::index_set &source_edge_ids, bool include_first_edge=false, bool use_minimal_time=false, double max_v=INF) const
Returns the shortest distance from any edge in source_edge_ids to every vertex of the network.
Definition RailwayNetwork.cpp:1239
std::pair< std::optional< double >, cda_rail::index_vector > shortest_edge_to_vertex_path(EdgeInput const &source_edge, VertexInput const &target_vertex, bool only_use_valid_successors=true, const cda_rail::index_set &edges_to_use={}, bool include_first_edge=false, bool use_minimal_time=false, double max_v=INF) const
Returns the shortest path and its length from source_edge to target_vertex.
Definition RailwayNetwork.hpp:2409
std::optional< double > shortest_path_from_edge_to_edge(EdgeInput const &source_edge, EdgeInput const &target_edge, bool include_first_edge=false, bool use_minimal_time=false, double max_v=INF) const
Returns the shortest distance from source_edge to target_edge.
Definition RailwayNetwork.hpp:2170
void change_vertex_name(VertexInput const &vertex, std::string_view const newName)
Renames a vertex.
Definition RailwayNetwork.hpp:1438
void export_network(std::filesystem::path const &working_directory) const
Exports the network to disk.
Definition RailwayNetwork.cpp:51
std::vector< cda_rail::index_vector > all_paths_of_length_ending_in_vertex(size_t v, double desired_len, std::optional< size_t > exit_node={}, const cda_rail::index_set &edges_to_consider={}) const
Returns all edge paths of length at least desired_len ending at vertex v (paths are listed from sourc...
Definition RailwayNetwork.hpp:2005
std::vector< std::pair< size_t, cda_rail::index_vector > > discretize(const vss::SeparationFunction &sep_func=vss::UNIFORM)
Discretizes all relevant breakable edges, inserting new NoBorderVSS vertices so that VSS borders are ...
Definition RailwayNetwork.cpp:957
void change_vertex_headway(VertexInput const &vertex, double const newHeadway)
Updates the additional headway imposed on a vertex.
Definition RailwayNetwork.hpp:1461
cda_rail::index_set out_edges(VertexInput const &vertex) const
Returns the set of edge indices of all edges leaving vertex.
Definition RailwayNetwork.hpp:849
std::optional< cda_rail::index_vector > get_reverse_path(cda_rail::index_vector const &edges) const
Get reverse path.
Definition RailwayNetwork.cpp:419
double minimal_neighboring_edge_length(VertexInput const &vertex, const cda_rail::index_set &edges_to_consider={}) const
Returns the length of the shortest edge incident to vertex.
Definition RailwayNetwork.hpp:1318
cda_rail::index_set get_unbreakable_section_containing_edge(EdgeInput const &edge) const
Returns the set of edge indices that form the unbreakable section containing edge.
Definition RailwayNetwork.hpp:1197
void add_successor(EdgeInput const &edge_in, EdgeInput const &edge_out)
Registers edge_out as a valid successor of edge_in.
Definition RailwayNetwork.hpp:1744
void throw_if_not_valid_successor(EdgeInput const &edge_in, EdgeInput const &edge_out) const
Checks that edge_out may be used directly after edge_in.
Definition RailwayNetwork.hpp:2947
size_t get_edge_index(VertexInput const &source, VertexInput const &target) const
Returns the index of the directed edge from source to target.
Definition RailwayNetwork.hpp:726
void add_crossing_successors(VertexInput const &crossing, VertexInput const &track_1_a, VertexInput const &track_1_b, VertexInput const &track_2_a, VertexInput const &track_2_b)
Registers the successors of a diamond crossing, where two tracks cross without being connected.
Definition RailwayNetwork.cpp:896
bool is_valid_successor(EdgeInput const &edge_in, EdgeInput const &edge_out) const
Checks whether edge_out is a valid successor of edge_in.
Definition RailwayNetwork.hpp:493
std::vector< cda_rail::index_set > no_border_vss_sections() const
Returns all sections bounded by NoBorderVSS vertices.
Definition RailwayNetwork.cpp:529
std::pair< size_t, size_t > add_bidirectional_edge(VertexInput const &source, VertexInput const &target, double length, double max_speed, std::optional< bool > const &breakable={}, std::optional< double > const &min_block_length={}, std::optional< double > const &min_stop_block_length={})
Adds a bidirectional edge between two vertices, i.e., it adds two edges, one in each direction.
Definition RailwayNetwork.cpp:823
std::vector< std::pair< std::optional< size_t >, std::optional< size_t > > > combine_reverse_edges(const cda_rail::index_vector &edges_to_consider, bool sort=false) const
Pairs each edge in edges_to_consider with its reverse edge, eliminating duplicate pairs.
Definition RailwayNetwork.cpp:436
void add_trivial_successors()
Lets trains pass straight through every vertex with exactly two neighbors.
Definition RailwayNetwork.cpp:872
Network(std::string_view const networkName, std::string const &working_directory)
Convenience overload accepting a std::string path.
Definition RailwayNetwork.hpp:302
cda_rail::index_vector relevant_breakable_edges() const
Returns the indices of breakable edges, counting each bidirectional track only once.
Definition RailwayNetwork.cpp:471
static std::optional< size_t > get_ttd_of_edge(size_t e_0, const std::vector< cda_rail::index_set > &ttd_sections)
Returns the index of the TTD section that contains a given edge.
Definition RailwayNetwork.cpp:1036
bool has_ttd_path(VertexInput const &source_vertex, VertexInput const &target_vertex, const cda_rail::index_set &ttd_section) const
Checks if a TTD can be traversed from a given source to target.
Definition RailwayNetwork.hpp:2562
std::string get_edge_name(EdgeInput const &edge) const
Returns the canonical name of an edge formatted as "<source_name>-<target_name>".
Definition RailwayNetwork.hpp:806
double length_of_path(const cda_rail::index_vector &path) const
Computes the total length of a path given as an ordered sequence of edge indices.
Definition RailwayNetwork.cpp:1047
size_t add_vertex(std::string_view name, VertexType type, std::optional< double > const &headway)
Constructs a vertex and adds it to the network.
Definition RailwayNetwork.hpp:1629
size_t number_of_vertices() const
Returns the total number of vertices in the network.
Definition RailwayNetwork.hpp:562
void change_edge_min_block_length(EdgeInput const &edge, double new_min_block_length)
Changes the minimum block length of an edge.
Definition RailwayNetwork.hpp:1545
cda_rail::index_set const & get_successors(EdgeInput const &edge) const
Returns the set of valid successor edges for edge.
Definition RailwayNetwork.hpp:926
size_t max_vss_on_edge(EdgeInput const &edge) const
Returns the maximum number of VSS borders that can be placed on edge.
Definition RailwayNetwork.hpp:1358
cda_rail::index_set edges_on_paths_between(VertexInput const &source, VertexInput const &target) const
Returns all edges that lie on some path from source to target.
Definition RailwayNetwork.hpp:959
std::vector< cda_rail::index_set > unbreakable_sections() const
Returns all unbreakable sections of the network.
Definition RailwayNetwork.cpp:486
cda_rail::index_set neighboring_edges(VertexInput const &vertex) const
Returns the set of edge indices of all edges incident to vertex (both in-edges and out-edges).
Definition RailwayNetwork.hpp:873
std::vector< std::vector< double > > all_edge_pairs_shortest_paths() const
Computes the shortest-path distance matrix between all ordered edge pairs using the Floyd–Warshall al...
Definition RailwayNetwork.cpp:1053
Thrown when input values do not satisfy expected constraints.
Definition CustomExceptions.hpp:97
A separation function together with the name it is known by.
Definition VSSModel.hpp:32
void throw_if_invalid_folder_name(std::string_view const folderName)
Validates that a folder name is portable across Linux, Windows, and macOS.
Definition CustomExceptions.hpp:407
constexpr SeparationFunction UNIFORM
The separation functions that are known by name.
Definition VSSModel.hpp:194
Definition CustomExceptions.hpp:15
constexpr double INF
Definition Definitions.hpp:25
std::unordered_set< size_t > index_set
Definition Definitions.hpp:22
std::vector< size_t > index_vector
Definition Definitions.hpp:21
VertexType
Which kind of block border a vertex is.
Definition Definitions.hpp:48
std::string concatenate_string_views(std::initializer_list< std::string_view > parts)
Concatenates multiple string views into a single string.
Definition StringHelper.cpp:11
Represents a directed edge in the railway network graph.
Definition RailwayNetwork.hpp:79
size_t target
Definition RailwayNetwork.hpp:85
double length
Definition RailwayNetwork.hpp:86
bool breakable
Definition RailwayNetwork.hpp:88
double min_stop_block_length
Definition RailwayNetwork.hpp:90
bool operator==(const Edge &other) const
Compares two edges for equality.
Definition RailwayNetwork.hpp:146
Edge(size_t const source, size_t const target, double const length, double const maxSpeed, std::optional< bool > const &breakable, std::optional< double > const &min_block_length, std::optional< double > const &min_stop_block_length)
Constructs an Edge with optional attribute values falling back to their defaults when not provided.
Definition RailwayNetwork.hpp:129
double min_block_length
Definition RailwayNetwork.hpp:89
static constexpr double MIN_BLOCK_LENGTH_DEFAULT
Definition RailwayNetwork.hpp:81
double max_speed
Definition RailwayNetwork.hpp:87
static constexpr bool BREAKABLE_DEFAULT
Definition RailwayNetwork.hpp:80
static constexpr double MIN_STOP_BLOCK_LENGTH_DEFAULT
Definition RailwayNetwork.hpp:82
size_t source
Definition RailwayNetwork.hpp:84
Edge(size_t const source, size_t const target, double const length, double const maxSpeed, bool const breakable=BREAKABLE_DEFAULT, double const minBlockLength=MIN_BLOCK_LENGTH_DEFAULT, double const minStopBlockLength=MIN_STOP_BLOCK_LENGTH_DEFAULT)
Constructs an Edge with explicit attribute values.
Definition RailwayNetwork.hpp:106
Unified edge descriptor accepted by all public API functions.
Definition RailwayNetwork.hpp:207
EdgeInput(std::string_view e_name)
Definition RailwayNetwork.hpp:228
EdgeInput(const std::pair< T1, T2 > &p)
Definition RailwayNetwork.hpp:226
friend class Route
Definition RailwayNetwork.hpp:211
friend class Network
Definition RailwayNetwork.hpp:209
EdgeInput(size_t a, size_t b)
Definition RailwayNetwork.hpp:222
EdgeInput(std::string_view a, std::string_view b)
Definition RailwayNetwork.hpp:223
EdgeInput(Edge edge)
Definition RailwayNetwork.hpp:227
friend class StationList
Definition RailwayNetwork.hpp:210
EdgeInput(size_t i)
Definition RailwayNetwork.hpp:221
Unified vertex descriptor accepted by all public API functions.
Definition RailwayNetwork.hpp:172
VertexInput(size_t i)
Definition RailwayNetwork.hpp:182
VertexInput(std::string_view v_name)
Definition RailwayNetwork.hpp:183
VertexInput(Vertex vertex)
Definition RailwayNetwork.hpp:184
friend class Network
Definition RailwayNetwork.hpp:174
friend class Timetable
Definition RailwayNetwork.hpp:175
Represents a vertex (node) in the railway network graph.
Definition RailwayNetwork.hpp:30
static constexpr double HEADWAY_DEFAULT
Definition RailwayNetwork.hpp:31
std::string name
Definition RailwayNetwork.hpp:33
VertexType type
Definition RailwayNetwork.hpp:34
double headway
Definition RailwayNetwork.hpp:35
bool operator==(Vertex const &other) const
Compares two vertices for equality.
Definition RailwayNetwork.hpp:67
Vertex(std::string_view const name, VertexType const type, std::optional< double > const &headway)
Constructs a Vertex with an optional headway value.
Definition RailwayNetwork.hpp:56
Vertex(std::string_view const name, VertexType const type, double const headway=HEADWAY_DEFAULT)
Constructs a Vertex with an explicit headway value.
Definition RailwayNetwork.hpp:45