1#ifndef CDA_RAIL_DATASTRUCTURE_RAILWAYNETWORK_HPP
2#define CDA_RAIL_DATASTRUCTURE_RAILWAYNETWORK_HPP
15#include <unordered_map>
16#include <unordered_set>
57 std::optional<double>
const&
headway)
130 double const maxSpeed, std::optional<bool>
const&
breakable,
177 std::variant<size_t, std::string_view, Vertex> m_data;
197 size_t resolve(
Network const* network)
const;
213 std::variant<size_t, std::pair<size_t, size_t>,
214 std::pair<std::string_view, std::string_view>,
Edge,
222 EdgeInput(
size_t a,
size_t b) : m_data(std::pair{a, b}) {}
224 : m_data(std::pair{a, b}) {}
225 template <
typename T1,
typename T2>
241 size_t resolve(
Network const* network)
const;
247 std::string m_network_name{
"UnnamedNetwork"};
249 std::vector<Vertex> m_vertices;
250 std::vector<Edge> m_edges;
251 std::vector<cda_rail::index_set>
253 std::unordered_map<std::string, size_t> m_vertex_name_to_index;
255 std::unordered_map<std::size_t, std::pair<size_t, double>>
256 m_new_edge_to_old_edge_after_transform;
272 explicit Network(std::string_view
const networkName =
"UnnamedNetwork")
273 : m_network_name(networkName) {
290 explicit Network(std::string_view network_name,
291 std::filesystem::path
const& working_directory);
302 explicit Network(std::string_view
const networkName,
303 std::string
const& working_directory)
304 :
Network(networkName, std::filesystem::path(working_directory)) {};
315 explicit Network(std::string_view
const networkName,
316 char const*
const workingDirectory)
317 :
Network(networkName, std::filesystem::path(workingDirectory)) {};
338 std::filesystem::path
const& working_directory) {
339 return Network(networkName, working_directory);
355 std::string
const& working_directory) {
356 return Network(networkName, working_directory);
371 char const*
const workingDirectory) {
372 return Network(networkName, workingDirectory);
385 void export_network(std::filesystem::path
const& working_directory)
const;
418 return (index < m_vertices.size());
426 [[nodiscard]]
bool has_vertex(std::string_view
const name)
const {
427 return m_vertex_name_to_index.contains(std::string{name});
441 [[nodiscard]]
bool has_edge_helper(
size_t source_id,
size_t target_id)
const;
449 [[nodiscard]]
bool has_edge(
size_t const index)
const {
450 return (index < m_edges.size());
463 return has_edge_helper(source.resolve(
this), target.resolve(
this));
476 [[nodiscard]]
bool is_valid_successor_helper(
size_t e0,
size_t e1)
const;
495 return is_valid_successor_helper(edge_in.resolve(
this),
496 edge_out.resolve(
this));
507 [[nodiscard]]
bool is_adjustable_helper(
size_t vertex_id)
const;
523 return is_adjustable_helper(vertex.resolve(
this));
576 return m_network_name;
602 [[nodiscard]]
const std::vector<Edge>&
get_edges()
const {
return m_edges; };
612 [[nodiscard]] std::pair<size_t, double>
613 get_old_edge_helper(
size_t new_edge)
const;
628 [[nodiscard]] std::pair<size_t, double>
630 return get_old_edge_helper(edge.resolve(
this));
676 return vertex.resolve(
this);
687 [[nodiscard]]
const Vertex& get_vertex_helper(
size_t index)
const;
698 return get_vertex_helper(vertex.resolve(
this));
712 [[nodiscard]]
size_t get_edge_index_helper(
size_t source_id,
713 size_t target_id)
const;
728 return get_edge_index_helper(source.resolve(
this), target.resolve(
this));
742 return edge_input.resolve(
this);
753 [[nodiscard]]
const Edge& get_edge_helper(
size_t index)
const;
764 return get_edge_helper(edge.resolve(
this));
780 [[nodiscard]] std::string get_edge_name_helper(std::string_view v1,
782 bool check_existence)
const;
791 [[nodiscard]] std::string get_edge_name_helper(
size_t v0,
size_t v1,
792 bool check_existence)
const {
807 const auto& edge_object =
get_edge(edge.resolve(
this));
839 neighboring_edges_helper(
size_t index)
const;
850 return out_edges_helper(vertex.resolve(
this));
861 return in_edges_helper(vertex.resolve(
this));
874 return neighboring_edges_helper(vertex.resolve(
this));
899 get_successors_helper(
size_t index)
const;
915 return get_predecessors_helper(edge.resolve(
this));
927 return get_successors_helper(edge.resolve(
this));
941 edges_on_paths_between_helper(
size_t source,
size_t target)
const;
961 return edges_on_paths_between_helper(source.resolve(
this),
962 target.resolve(
this));
985 return neighbors_helper(vertex.resolve(
this));
998 [[nodiscard]] std::optional<size_t>
999 common_vertex_helper(
size_t e_idx_1,
size_t e_idx_2)
const;
1011 [[nodiscard]] std::optional<size_t>
1013 return common_vertex_helper(edge1.resolve(
this), edge2.resolve(
this));
1024 [[nodiscard]] std::optional<size_t>
1025 get_reverse_edge_index_helper(
size_t edge_index)
const;
1037 [[nodiscard]] std::optional<size_t>
1039 return get_reverse_edge_index_helper(edge.resolve(
this));
1049 [[nodiscard]] std::optional<cda_rail::index_vector>
1061 [[nodiscard]] std::optional<size_t>
1063 return edgeIndex.has_value()
1064 ? get_reverse_edge_index_helper(edgeIndex.value())
1065 : std::optional<size_t>();
1090 [[nodiscard]] std::vector<
1091 std::pair<std::optional<size_t>, std::optional<size_t>>>
1093 bool sort =
false)
const;
1106 [[nodiscard]]
size_t get_track_index_helper(
size_t edge_index)
const;
1121 return get_track_index_helper(edge.resolve(
this));
1172 get_unbreakable_section_containing_edge_helper(
size_t e)
const;
1181 [[nodiscard]]
bool is_on_same_unbreakable_section_helper(
size_t e1,
1198 return get_unbreakable_section_containing_edge_helper(edge.resolve(
this));
1212 return is_on_same_unbreakable_section_helper(edge1.resolve(
this),
1213 edge2.resolve(
this));
1236 [[nodiscard]]
static std::vector<std::pair<size_t, size_t>>
1238 const std::vector<cda_rail::index_set>& ttd);
1270 [[nodiscard]]
double maximal_vertex_speed_helper(
1292 return maximal_vertex_speed_helper(vertex.resolve(
this), edges_to_consider);
1303 [[nodiscard]]
double minimal_neighboring_edge_length_helper(
1321 return minimal_neighboring_edge_length_helper(vertex.resolve(
this),
1337 [[nodiscard]]
size_t
1338 max_vss_on_edge_helper(
size_t index,
1339 const vss::SeparationFunction& sep_func)
const;
1376 [[nodiscard]]
size_t
1379 return max_vss_on_edge_helper(edge.resolve(
this), sep_func);
1395 m_network_name = networkName;
1408 void change_vertex_name_helper(
size_t index, std::string_view new_name);
1417 void change_vertex_type_helper(
size_t index,
VertexType new_type);
1426 void change_vertex_headway_helper(
size_t index,
double new_headway);
1439 std::string_view
const newName) {
1440 change_vertex_name_helper(vertex.resolve(
this), newName);
1451 change_vertex_type_helper(vertex.resolve(
this), newType);
1462 double const newHeadway) {
1463 change_vertex_headway_helper(vertex.resolve(
this), newHeadway);
1474 void change_edge_length_helper(
size_t index,
double new_length);
1483 void change_edge_max_speed_helper(
size_t index,
double new_max_speed);
1492 void change_edge_min_block_length_helper(
size_t index,
1493 double new_min_block_length);
1503 change_edge_min_stop_block_length_helper(
size_t index,
1504 double new_min_stop_block_length);
1513 void change_edge_breakable_helper(
size_t index,
bool value);
1524 change_edge_length_helper(edge.resolve(
this), new_length);
1535 change_edge_max_speed_helper(edge.resolve(
this), new_max_speed);
1546 double new_min_block_length) {
1547 change_edge_min_block_length_helper(edge.resolve(
this),
1548 new_min_block_length);
1559 double new_min_stop_block_length) {
1560 change_edge_min_stop_block_length_helper(edge.resolve(
this),
1561 new_min_stop_block_length);
1592 bool const breakable) {
1593 change_edge_breakable_helper(edge.resolve(
this), breakable);
1630 std::optional<double>
const& headway) {
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);
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);
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 = {});
1728 void add_successor_helper(
size_t edge_in,
size_t edge_out);
1746 add_successor_helper(edge_in.resolve(
this), edge_out.resolve(
this));
1761 void add_successors_via_helper(
size_t from,
size_t via,
size_t to);
1771 void throw_if_not_exactly_the_neighbors(
1802 VertexInput
const& toe,
1803 VertexInput
const& branch_1,
1804 VertexInput
const& branch_2);
1826 VertexInput
const& track_1_a,
1827 VertexInput
const& track_1_b,
1828 VertexInput
const& track_2_a,
1829 VertexInput
const& track_2_b);
1853 VertexInput
const& track_1_a,
1854 VertexInput
const& track_1_b,
1855 VertexInput
const& track_2_a,
1856 VertexInput
const& track_2_b);
1876 std::pair<cda_rail::index_vector, cda_rail::index_vector>
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);
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(
1909 std::vector<std::pair<size_t, cda_rail::index_vector>>
1922 std::vector<std::pair<size_t, cda_rail::index_vector>>
1941 std::vector<std::pair<size_t, cda_rail::index_vector>>
1965 [[nodiscard]] std::vector<cda_rail::index_vector>
1967 size_t v,
double desired_len, std::optional<size_t> exit_node = {},
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);
1986 [[nodiscard]] std::vector<cda_rail::index_vector>
1988 size_t e,
double desired_len, std::optional<size_t> exit_node = {},
1990 return all_routes_of_given_length(std::nullopt, e, desired_len,
false,
1991 exit_node, edges_to_consider);
2004 [[nodiscard]] std::vector<cda_rail::index_vector>
2006 size_t v,
double desired_len, std::optional<size_t> exit_node = {},
2008 return all_routes_of_given_length(v, std::nullopt, desired_len,
true,
2009 exit_node, edges_to_consider);
2022 [[nodiscard]] std::vector<cda_rail::index_vector>
2024 size_t const e,
double const desiredLen,
2025 std::optional<size_t> exit_node = {},
2027 return all_routes_of_given_length(std::nullopt, e, desiredLen,
true,
2028 exit_node, edges_to_consider);
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;
2067 [[nodiscard]]
static std::optional<size_t>
2069 const std::vector<cda_rail::index_set>& ttd_sections);
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);
2121 [[nodiscard]] std::vector<std::vector<double>>
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;
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);
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);
2228 shortest_path_using_edges_helper(
size_t source_edge_id,
2229 size_t target_vertex_id,
2230 bool only_use_valid_successors =
true,
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(
2239 edges_to_use, target_is_edge, include_first_edge, use_minimal_time,
2270 shortest_path_between_sets_using_edges_helper(
2273 bool only_use_valid_successors =
true,
2275 bool include_first_edge =
false,
bool use_minimal_time =
false,
2276 double max_v =
INF)
const;
2291 [[nodiscard]] std::optional<double> shortest_path_length_between_sets_helper(
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)
2326 [[nodiscard]] std::optional<size_t> dijkstra_on_edges_helper(
2329 bool include_first_edge,
bool use_minimal_time,
double max_v,
2357 bool include_first_edge =
false,
bool use_minimal_time =
false,
2358 double max_v =
INF)
const;
2379 bool only_use_valid_successors =
true,
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);
2411 bool only_use_valid_successors =
true,
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);
2441 bool only_use_valid_successors =
true,
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);
2473 bool only_use_valid_successors =
true,
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);
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);
2513 [[nodiscard]] std::optional<double>
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);
2535 [[nodiscard]] std::unordered_set<size_t>
2548 has_ttd_path_helper(
size_t source_vertex,
size_t target_vertex,
2565 return has_ttd_path_helper(source_vertex.resolve(
this),
2566 target_vertex.resolve(
this), ttd_section);
2578 [[nodiscard]]
bool has_ttd_path_not_using_border_vertex_helper(
2594 return has_ttd_path_not_using_border_vertex_helper(
2595 border_vertex.resolve(
this), ttd_section);
2610 void read_graphml(
const std::filesystem::path& p);
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);
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);
2663 static void extract_vertices_from_key_inplace(
const std::string& key,
2664 std::string& source_name,
2665 std::string& target_name);
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);
2697 void read_successors(
const std::filesystem::path& p);
2703 void export_graphml(
const std::filesystem::path& p)
const;
2710 void export_successors_cpp(
const std::filesystem::path& p)
const;
2718 [[nodiscard]]
size_t other_vertex(
size_t e,
size_t v)
const {
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);
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);
2809 std::vector<std::pair<std::optional<size_t>, std::optional<size_t>>>
2811 std::vector<std::pair<std::optional<size_t>, std::optional<size_t>>>&
2828 void check_new_edge_requirements(
size_t source,
size_t target)
const;
2844 [[nodiscard]]
static double delta_dist_helper(
const Edge& successor_edge,
2846 bool use_minimal_time);
2869 dfs_inplace(std::vector<cda_rail::index_set>& ret_val,
2870 std::unordered_set<size_t>& vertices_to_visit,
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 = {},
2905 bool return_successors_if_zero =
false)
const;
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;
2953 " is not a valid successor of ",
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
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
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