MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
GeneralSimulator.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_SIMULATOR_GENERALSIMULATOR_HPP
2#define CDA_RAIL_SIMULATOR_GENERALSIMULATOR_HPP
3
5#include "Definitions.hpp"
8
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12#include <map>
13#include <memory>
14#include <optional>
15#include <string>
16#include <unordered_set>
17#include <vector>
18
19namespace cda_rail::simulator {
20
28enum class SimulatorType : std::uint8_t { Greedy = 0, EventBased = 1 };
30constexpr std::string simulator_type_to_string(SimulatorType const type) {
31 switch (type) {
33 return "Greedy";
35 return "EventBased";
36 default:
37 throw cda_rail::exceptions::ConsistencyException("Unknown simulator type");
38 }
39}
40
45struct PosVel {
46 double pos;
47 double vel;
48};
49
52 bool success; // true if simulation was successful, false if it was infeasible
53 std::vector<double> exit_times; // exit times of each train (or route end time
54 // if route was only partially simulated)
55 std::vector<std::vector<double>>
56 stop_times; // For every train, a vector of times at which scheduled stops
57 // were reached
58 std::vector<double> vertex_headways; // for every vertex, earliest time at
59 // which next train can enter
60 std::vector<std::map<double, PosVel>>
61 train_trajectories; // For every train, a map of time to position and
62 // velocity at that time
63};
64
75 std::vector<cda_rail::index_vector> train_edges;
76 std::vector<cda_rail::index_vector> ttd_orders;
77 std::vector<cda_rail::index_vector> vertex_orders;
78 std::vector<std::vector<double>> stop_positions;
79
86 bool operator==(const SimulatorState& other) const;
87
94 bool operator>(const SimulatorState& other) const;
95};
96
105private:
106 std::shared_ptr<
108 m_instance;
109 std::vector<cda_rail::index_set> m_ttd_sections;
110
111 std::vector<cda_rail::index_vector> m_train_edges;
112 std::vector<cda_rail::index_vector> m_ttd_orders;
113 std::vector<cda_rail::index_vector> m_vertex_orders;
114 std::vector<std::vector<double>> m_stop_positions;
115
116 // ----------------------
117 // Constructor
118 // ----------------------
119protected:
127 std::shared_ptr<const cda_rail::instances::
128 GeneralProblemInstanceWithScheduleAndRoutes>
129 instance,
130 std::vector<cda_rail::index_set> ttd_sections);
142 std::shared_ptr<const cda_rail::instances::
143 GeneralProblemInstanceWithScheduleAndRoutes>
144 instance,
145 std::vector<cda_rail::index_set> ttd_sections,
146 std::vector<cda_rail::index_vector> train_edges,
147 std::vector<cda_rail::index_vector> ttd_orders,
148 std::vector<cda_rail::index_vector> vertex_orders,
149 std::vector<std::vector<double>> stop_positions);
150
151public:
160 std::vector<cda_rail::index_set> ttd_sections);
173 std::vector<cda_rail::index_set> ttd_sections,
174 std::vector<cda_rail::index_vector> train_edges,
175 std::vector<cda_rail::index_vector> ttd_orders,
176 std::vector<cda_rail::index_vector> vertex_orders,
177 std::vector<std::vector<double>> stop_positions);
178
185 virtual ~GeneralSimulator() = default;
186
187 // Rule of 5 (due to virtual deconstructor)
196
197 // ---------------------
198 // SIMPLE GETTER
203 [[nodiscard]] virtual instances::
204 GeneralProblemInstanceWithScheduleAndRoutes const*
205 get_instance() const {
206 return m_instance.get();
207 }
208
215 [[nodiscard]] const std::vector<cda_rail::index_set>&
217 return m_ttd_sections;
218 }
219
225 [[nodiscard]] const std::vector<cda_rail::index_vector>&
227 return m_train_edges;
228 }
229
235 [[nodiscard]] const cda_rail::index_vector&
236 get_train_edges_of_tr(size_t train_id) const;
237
242 [[nodiscard]] const std::vector<cda_rail::index_vector>&
244 return m_ttd_orders;
245 }
246
252 [[nodiscard]] const cda_rail::index_vector&
253 get_ttd_orders_of_ttd(size_t ttd_index) const;
261 [[nodiscard]] std::optional<size_t>
262 get_ttd(Network::EdgeInput const& edge) const;
263
270 [[nodiscard]] const std::vector<cda_rail::index_vector>&
272 return m_vertex_orders;
273 }
274
280 [[nodiscard]] const cda_rail::index_vector&
282
289 [[nodiscard]] const std::vector<std::vector<double>>&
291 return m_stop_positions;
292 }
293
299 [[nodiscard]] const std::vector<double>&
300 get_stop_positions_of_tr(size_t train_id) const;
301
302 // -------------------
303 // HELPFUL VALUES
304 // -------------------
311 [[nodiscard]] double train_edge_length(size_t tr) const;
320 [[nodiscard]] double get_edge_position(size_t train_id, size_t edge_id) const;
328 [[nodiscard]] size_t get_edge_at_position(size_t train_id,
329 double position) const;
330
338 [[nodiscard]] bool
340 const cda_rail::index_vector& edges) const {
342 tr, edges, get_stop_positions_of_tr(tr).size());
343 }
344
351 [[nodiscard]] bool is_current_pos_valid_stop_position(size_t tr) const {
362 }
363
365 [[nodiscard]] SimulatorState get_simulator_state() const {
366 return {
367 .train_edges = m_train_edges,
368 .ttd_orders = m_ttd_orders,
369 .vertex_orders = m_vertex_orders,
370 .stop_positions = m_stop_positions,
371 };
372 }
373
374 // -------------------
375 // EDITING FUNCTIONS
376 // -------------------
377
380
382 void set_train_edges(std::vector<cda_rail::index_vector> tr_edges);
384 void set_train_edges_of_tr(size_t train_id, cda_rail::index_vector edges);
386 void append_train_edge_to_tr(size_t train_id,
387 cda_rail::Network::EdgeInput const& edge);
388
390 void set_ttd_orders(std::vector<cda_rail::index_vector> orders);
392 void set_ttd_orders_of_ttd(size_t ttd_index, cda_rail::index_vector orders);
393
395 void set_vertex_orders(std::vector<cda_rail::index_vector> orders);
399
401 void set_stop_positions(std::vector<std::vector<double>> positions);
403 void set_stop_positions_of_tr(size_t train_id, std::vector<double> positions);
405 void append_stop_position_to_tr(size_t train_id, double position);
407 void append_stop_edge_to_tr(size_t train_id, size_t edge);
409 void append_current_stop_position_of_tr(size_t train_id);
410
411 // ------------------
412 // STATE HELPER
413 // ------------------
414
420 [[nodiscard]] bool is_final_state() const;
421
422 // ------------------
423 // SIMULATION
424 // ------------------
425
426 // virtual function
446 [[nodiscard]] virtual SimulatorResults
447 simulate(double dt, bool late_entry_possible,
448 bool limit_speed_by_leaving_edges, bool save_trajectories,
449 bool block_vertices_after_disappearing) const = 0;
450
455 [[nodiscard]] SimulatorResults
456 simulate(double dt = 6.0, bool late_entry_possible = false,
457 bool limit_speed_by_leaving_edges = true,
458 bool save_trajectories =
459 false) const { // default values for virtual function
460 return simulate(dt, late_entry_possible, limit_speed_by_leaving_edges,
461 save_trajectories, true);
462 };
463
484 [[nodiscard]] virtual std::optional<SimulatorState>
485 first_come_first_served_state(double dt, bool late_entry_possible,
486 bool limit_speed_by_leaving_edges) const;
487
488protected:
489 // --------------------
490 // PROTECTED HELPER
491 // --------------------
492
500 [[nodiscard]] double tr_braking_distance(size_t tr, double v) const;
501
508 [[nodiscard]] std::vector<double> edge_milestones(size_t tr) const;
509
517 [[nodiscard]] bool is_on_route(size_t tr, size_t edge_id) const;
518
524 [[nodiscard]] std::vector<std::unordered_set<size_t>> tr_on_edges() const;
525
526private:
534 [[nodiscard]] static cda_rail::index_set
535 trains_on_path_helper(cda_rail::index_vector const& edges,
536 std::vector<cda_rail::index_vector> const& tr_edges);
537
538public:
546 [[nodiscard]] cda_rail::index_set
548 bool also_reverse_path = false) const {
549 return trains_on_path(edges, get_train_edges(), also_reverse_path);
550 }
551
559 [[nodiscard]] cda_rail::index_set
561 std::vector<cda_rail::index_vector> const& tr_edges,
562 bool also_reverse_path = false) const {
563 return trains_on_path(edges, tr_edges, get_instance()->get_const_network(),
564 also_reverse_path);
565 }
566
575 [[nodiscard]] static cda_rail::index_set
577 std::vector<cda_rail::index_vector> const& tr_edges,
578 Network const& network, bool also_reverse_path = false);
579
580private:
581 // -----------------------
582 // EXCEPTION HELPER
583 // -----------------------
585 void check_ttd_sections(
586 std::vector<cda_rail::index_set> const& ttd_sections) const;
587
589 void check_train_edges_for_tr(size_t tr,
590 cda_rail::index_vector const& edges) const;
592 void check_train_edges(
593 std::vector<cda_rail::index_vector> const& train_edges) const;
594
596 void check_if_all_tr_valid(cda_rail::index_vector const& train_ids) const;
598 static void check_if_all_tr_unique(cda_rail::index_vector const& train_ids);
600 void
601 check_ttd_orders(std::vector<cda_rail::index_vector> const& ttd_orders) const;
603 void check_vertex_orders(
604 std::vector<cda_rail::index_vector> const& vertex_orders) const;
605
607 void check_stop_positions_for_tr(size_t tr,
608 std::vector<double> const& positions) const;
610 void check_stop_positions(
611 std::vector<std::vector<double>> const& stop_positions) const;
612};
613} // namespace cda_rail::simulator
614
615template <> struct std::hash<cda_rail::simulator::SimulatorState> {
622 size_t
623 operator()(const cda_rail::simulator::SimulatorState& state) const noexcept;
624}; // namespace std
625
626#endif // CDA_RAIL_SIMULATOR_GENERALSIMULATOR_HPP
Directed multigraph representing a railway network.
Definition RailwayNetwork.hpp:163
Thrown when a consistency check fails.
Definition CustomExceptions.hpp:80
An instance consisting of a network, a timetable, and routes.
Definition GeneralProblemInstance.hpp:191
bool is_route_end_valid_stop_pos(size_t tr, const cda_rail::index_vector &edges, size_t next_stop_id) const
Checks whether a route end is a valid stopping position.
Definition GeneralProblemInstance.cpp:63
SimulatorResults simulate(double dt=6.0, bool late_entry_possible=false, bool limit_speed_by_leaving_edges=true, bool save_trajectories=false) const
Simulates with default values, in particular a time step of 6 seconds and blocking the last vertex of...
Definition GeneralSimulator.hpp:456
void set_vertex_orders_of_vertex(cda_rail::Network::VertexInput const &vertex, cda_rail::index_vector orders)
Replaces the train order stored for one vertex.
Definition GeneralSimulator.cpp:248
void set_simulator_state(SimulatorState state)
Replaces the full simulator state.
Definition GeneralSimulator.cpp:191
double train_edge_length(size_t tr) const
Returns the length of the current edge sequence of a train.
Definition GeneralSimulator.cpp:144
SimulatorState get_simulator_state() const
The full simulator state.
Definition GeneralSimulator.hpp:365
GeneralSimulator(std::shared_ptr< const cda_rail::instances::GeneralProblemInstanceWithScheduleAndRoutes > instance, std::vector< cda_rail::index_set > ttd_sections)
Constructs a simulator from shared instance data.
Definition GeneralSimulator.cpp:42
GeneralSimulator(GeneralSimulator &&)=default
Move constructor.
const std::vector< cda_rail::index_vector > & get_ttd_orders() const
Retrieves the ordering vectors for all TTD sections.
Definition GeneralSimulator.hpp:243
const std::vector< cda_rail::index_vector > & get_vertex_orders() const
Accesses the train ordering for each vertex.
Definition GeneralSimulator.hpp:271
bool is_route_end_valid_stop_pos(size_t tr, const cda_rail::index_vector &edges) const
Checks whether a route end can serve as the next stop position.
Definition GeneralSimulator.hpp:339
size_t get_edge_at_position(size_t train_id, double position) const
Returns the edge that contains a route position.
Definition GeneralSimulator.cpp:169
void append_current_stop_position_of_tr(size_t train_id)
Appends the current route-end position as a stop for a train.
Definition GeneralSimulator.cpp:327
GeneralSimulator(GeneralSimulator const &)=default
Copy constructor.
const cda_rail::index_vector & get_vertex_orders_of_vertex(Network::VertexInput const &vertex) const
Returns the train order stored for a vertex.
Definition GeneralSimulator.cpp:131
void set_vertex_orders(std::vector< cda_rail::index_vector > orders)
Replaces all vertex train orders.
Definition GeneralSimulator.cpp:242
void append_stop_edge_to_tr(size_t train_id, size_t edge)
Appends a stop edge reference to a train.
Definition GeneralSimulator.cpp:299
bool is_current_pos_valid_stop_position(size_t tr) const
Checks whether the train's current last edge is a valid stop position.
Definition GeneralSimulator.hpp:351
void set_ttd_orders(std::vector< cda_rail::index_vector > orders)
Replaces all TTD train orders.
Definition GeneralSimulator.cpp:225
void set_train_edges_of_tr(size_t train_id, cda_rail::index_vector edges)
Replaces the edge sequence of one train.
Definition GeneralSimulator.cpp:205
virtual SimulatorResults simulate(double dt, bool late_entry_possible, bool limit_speed_by_leaving_edges, bool save_trajectories, bool block_vertices_after_disappearing) const =0
Simulates train movements given the current routing, ordering, and stopping configuration.
virtual std::optional< SimulatorState > first_come_first_served_state(double dt, bool late_entry_possible, bool limit_speed_by_leaving_edges) const
Decides every order that the current state leaves open first come, first served.
Definition GeneralSimulator.cpp:360
const cda_rail::index_vector & get_train_edges_of_tr(size_t train_id) const
Returns the current edge sequence for one train.
Definition GeneralSimulator.cpp:102
const std::vector< double > & get_stop_positions_of_tr(size_t train_id) const
Returns the stop positions of one train.
Definition GeneralSimulator.cpp:138
virtual instances::GeneralProblemInstanceWithScheduleAndRoutes const * get_instance() const
Provides access to the problem instance for this simulator.
Definition GeneralSimulator.hpp:205
const std::vector< cda_rail::index_set > & get_ttd_sections() const
Retrieves the TTD sections considered by the simulator.
Definition GeneralSimulator.hpp:216
GeneralSimulator & operator=(GeneralSimulator const &)=default
Copy assignment operator.
std::optional< size_t > get_ttd(Network::EdgeInput const &edge) const
Returns the TTD section containing an edge, if any.
Definition GeneralSimulator.cpp:118
void set_stop_positions_of_tr(size_t train_id, std::vector< double > positions)
Replaces the stop positions of one train.
Definition GeneralSimulator.cpp:263
cda_rail::index_set trains_on_path(cda_rail::index_vector const &edges, std::vector< cda_rail::index_vector > const &tr_edges, bool also_reverse_path=false) const
Returns the trains from provided routes that contain a path.
Definition GeneralSimulator.hpp:560
bool is_on_route(size_t tr, size_t edge_id) const
Checks whether an edge is on a train's current route.
Definition GeneralSimulator.cpp:398
GeneralSimulator & operator=(GeneralSimulator &&)=default
Move assignment operator.
const std::vector< std::vector< double > > & get_stop_positions() const
Accesses all trains' scheduled stop positions.
Definition GeneralSimulator.hpp:290
std::vector< double > edge_milestones(size_t tr) const
Returns cumulative edge milestones for a train route.
Definition GeneralSimulator.cpp:381
double tr_braking_distance(size_t tr, double v) const
Computes braking distance for a train at a given speed.
Definition GeneralSimulator.cpp:368
void append_stop_position_to_tr(size_t train_id, double position)
Appends one stop position to a train.
Definition GeneralSimulator.cpp:269
void set_train_edges(std::vector< cda_rail::index_vector > tr_edges)
Replaces all train edge sequences.
Definition GeneralSimulator.cpp:199
void set_ttd_orders_of_ttd(size_t ttd_index, cda_rail::index_vector orders)
Replaces the order stored for one TTD section.
Definition GeneralSimulator.cpp:231
const cda_rail::index_vector & get_ttd_orders_of_ttd(size_t ttd_index) const
Returns the train order for one TTD section.
Definition GeneralSimulator.cpp:109
const std::vector< cda_rail::index_vector > & get_train_edges() const
Accesses the edge sequences for all trains.
Definition GeneralSimulator.hpp:226
cda_rail::index_set trains_on_path(cda_rail::index_vector const &edges, bool also_reverse_path=false) const
Returns the trains whose current routes contain a path.
Definition GeneralSimulator.hpp:547
std::vector< std::unordered_set< size_t > > tr_on_edges() const
Returns, for each edge, the trains currently using it.
Definition GeneralSimulator.cpp:410
void append_train_edge_to_tr(size_t train_id, cda_rail::Network::EdgeInput const &edge)
Appends one edge to a train edge sequence.
Definition GeneralSimulator.cpp:211
double get_edge_position(size_t train_id, size_t edge_id) const
Returns the route position at which an edge ends, i.e., the position of its target vertex.
Definition GeneralSimulator.cpp:151
void set_stop_positions(std::vector< std::vector< double > > positions)
Replaces all stop-position vectors.
Definition GeneralSimulator.cpp:257
virtual ~GeneralSimulator()=default
Virtual destructor.
bool is_final_state() const
Checks whether the current simulator state is terminal.
Definition GeneralSimulator.cpp:340
Definition GeneralPerformanceOptimizationInstance.hpp:41
Definition EventSimulator.hpp:25
constexpr std::string simulator_type_to_string(SimulatorType const type)
The name of type, as it is reported and parsed.
Definition GeneralSimulator.hpp:30
SimulatorType
Which simulator obtains the trajectories of a state.
Definition GeneralSimulator.hpp:28
@ EventBased
Definition GeneralSimulator.hpp:28
@ Greedy
Definition GeneralSimulator.hpp:28
Definition CustomExceptions.hpp:15
std::unordered_set< size_t > index_set
Definition Definitions.hpp:22
std::vector< size_t > index_vector
Definition Definitions.hpp:21
Unified edge descriptor accepted by all public API functions.
Definition RailwayNetwork.hpp:207
Unified vertex descriptor accepted by all public API functions.
Definition RailwayNetwork.hpp:172
Position of a train's front along its route in m together with its velocity in m/s.
Definition GeneralSimulator.hpp:45
double vel
Definition GeneralSimulator.hpp:47
double pos
Definition GeneralSimulator.hpp:46
What a simulation run produced.
Definition GeneralSimulator.hpp:51
std::vector< std::map< double, PosVel > > train_trajectories
Definition GeneralSimulator.hpp:61
std::vector< double > exit_times
Definition GeneralSimulator.hpp:53
std::vector< std::vector< double > > stop_times
Definition GeneralSimulator.hpp:56
bool success
Definition GeneralSimulator.hpp:52
std::vector< double > vertex_headways
Definition GeneralSimulator.hpp:58
Everything a simulation run needs beyond the instance itself.
Definition GeneralSimulator.hpp:74
bool operator==(const SimulatorState &other) const
Compares two simulator states for equality.
Definition GeneralSimulator.cpp:23
std::vector< cda_rail::index_vector > ttd_orders
Definition GeneralSimulator.hpp:76
std::vector< std::vector< double > > stop_positions
Definition GeneralSimulator.hpp:78
bool operator>(const SimulatorState &other) const
Provides a strict ordering between simulator states.
Definition GeneralSimulator.cpp:30
std::vector< cda_rail::index_vector > train_edges
Definition GeneralSimulator.hpp:75
std::vector< cda_rail::index_vector > vertex_orders
Definition GeneralSimulator.hpp:77
size_t operator()(const cda_rail::simulator::SimulatorState &state) const noexcept
Computes a hash value for a simulator state.
Definition GeneralSimulator.cpp:611