MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
VSSGenTimetableSolver.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_SOLVER_MIP_BASED_VSSGENTIMETABLESOLVER_HPP
2#define CDA_RAIL_SOLVER_MIP_BASED_VSSGENTIMETABLESOLVER_HPP
3
5#include "Definitions.hpp"
7#include "VSSModel.hpp"
9#include "gurobi_c++.h"
12#include "unordered_map"
13
14#include <cstddef>
15#include <cstdint>
16#include <optional>
17#include <string>
18#include <utility>
19#include <vector>
20
22using std::size_t;
23
30enum class UpdateStrategyVSSGen : std::uint8_t { Fixed = 0, Relative = 1 };
31
33constexpr std::string update_strategy_to_string(UpdateStrategyVSSGen strategy) {
34 switch (strategy) {
36 return "Fixed";
38 return "Relative";
39 default:
40 throw cda_rail::exceptions::ConsistencyException("Unknown update strategy");
41 }
42}
43
45constexpr std::string
47 switch (strategy) {
49 return "Optimal";
51 return "TradeOff";
53 return "Feasible";
54 default:
56 "Unknown optimality strategy");
57 }
58}
59
76
79 double delta_t = 15;
80 bool fix_routes = true;
81 bool train_dynamics = true;
82 bool braking_curves = true;
83};
84
95 double delta_t = 15;
96 bool train_dynamics = true;
97 bool braking_curves = true;
98 bool fix_stop_positions = true;
103};
104
115 // NOLINTNEXTLINE(readability-redundant-member-init)
117 bool use_pwl{false};
120};
121
132 : public GeneralMIPSolver<
133 instances::GeneralPerformanceOptimizationInstance,
134 instances::SolVSSGeneralPerformanceOptimizationInstance> {
136
137private:
138 // Instance variables
139 double dt{-1};
140 size_t num_t{0};
141 size_t num_tr{0};
142 size_t num_edges{0};
143 size_t num_vertices{0};
144 size_t num_breakable_sections{0};
145 std::vector<cda_rail::index_vector> unbreakable_sections;
146 std::vector<cda_rail::index_vector> no_border_vss_sections;
147 std::vector<std::pair<size_t, size_t>> train_interval;
148 std::vector<std::pair<std::optional<size_t>, std::optional<size_t>>>
149 breakable_edges_pairs;
150 cda_rail::index_vector no_border_vss_vertices;
151 cda_rail::index_vector relevant_edges;
152 cda_rail::index_vector breakable_edges;
153 bool fix_routes{false};
154 vss::Model vss_model{
155 vss::ModelType::Continuous}; // This line is correct and intended.
156 // vss_model is of type vss::Model
157 bool include_train_dynamics{false};
158 bool include_braking_curves{false};
159 bool use_pwl{false};
160 bool use_schedule_cuts{false};
161 bool convexify_braking_distance{false};
162 bool iterative_vss{false};
164 UpdateStrategyVSSGen iterative_update_strategy{UpdateStrategyVSSGen::Fixed};
165 double iterative_initial_value{1.0};
166 double iterative_update_value{2.0};
167 bool iterative_include_cuts{true};
168 bool iterative_include_cuts_tmp{true};
169 bool postprocess{false};
170 cda_rail::index_vector max_vss_per_edge_in_iteration{};
171 // Network::max_vss_on_edge of every edge, which is computed only once, since
172 // it is needed throughout the model creation and not cheap
173 cda_rail::index_vector max_vss_per_edge{};
174 std::unordered_map<size_t, size_t> breakable_edge_indices{};
175 std::vector<std::pair<cda_rail::index_vector, cda_rail::index_vector>>
176 fwd_bwd_sections{};
177
178 // Variable functions
180 void create_variables();
182 void create_general_variables();
184 void create_fixed_routes_variables();
186 void create_free_routes_variables();
188 void create_discretized_variables();
190 void create_non_discretized_variables();
192 void create_brakelen_variables();
194 void create_only_stop_at_vss_variables();
196 void create_non_discretized_only_stop_at_vss_variables();
197
198 // Constraint functions
200 void create_constraints();
202 void create_general_constraints();
204 void create_fixed_routes_constraints();
206 void create_free_routes_constraints();
212 void create_discretized_constraints();
214 void create_non_discretized_constraints();
216 void create_acceleration_constraints();
218 void create_brakelen_constraints();
219
220 // Helper functions for constraints
222 void create_general_boundary_constraints();
223
228 void create_general_schedule_constraints();
230 void create_unbreakable_sections_constraints();
232 void create_general_speed_constraints();
240 void create_reverse_occupation_constraints();
241
243 void create_fixed_routes_position_constraints();
245 void create_boundary_fixed_routes_constraints();
247 void create_fixed_routes_occupation_constraints();
249 void create_fixed_route_schedule_constraints();
251 void create_fixed_routes_impossibility_cuts();
253 void create_fixed_routes_no_overlap_entry_exit_constraints();
254
259 void create_non_discretized_general_constraints();
261 void create_non_discretized_position_constraints();
263 void create_non_discretized_free_route_constraints();
265 void create_non_discretized_fixed_route_constraints();
267 void create_non_discretized_fraction_constraints();
269 void create_non_discretized_alt_fraction_constraints();
271 void create_non_discretized_general_only_stop_at_vss_constraints();
273 void create_non_discretized_free_routes_only_stop_at_vss_constraints();
275 void create_non_discretized_fixed_routes_only_stop_at_vss_constraints();
276
278 void create_free_routes_position_constraints();
280 void create_free_routes_overlap_constraints();
282 void create_boundary_free_routes_constraints();
284 void create_free_routes_occupation_constraints();
286 void create_free_routes_impossibility_cuts();
288 void create_free_routes_no_overlap_entry_exit_constraints();
289
290 // Objective
292 void set_objective();
293
294 // Helper functions
296 void set_timeout(int time_limit);
298 [[nodiscard]] std::optional<
299 instances::SolVSSGeneralPerformanceOptimizationInstance>
300 optimize(const std::optional<
301 instances::GeneralPerformanceOptimizationInstance>& old_instance,
302 int time_limit);
315 void export_lp_model_if_applicable(
316 const instances::SolVSSGeneralPerformanceOptimizationInstance& sol_object,
317 const SolutionSettingsVSSGen& solution_settings);
325 [[nodiscard]] FurtherData
326 get_further_data(const SolutionSettingsVSSGen& solution_settings,
327 int time_limit) const;
336 [[nodiscard]] cda_rail::index_vector
337 unbreakable_section_indices(size_t train_index) const;
339 void calculate_fwd_bwd_sections();
341 void calculate_fwd_bwd_sections_discretized();
343 void calculate_fwd_bwd_sections_non_discretized();
345 [[nodiscard]] double get_max_brakelen(const size_t& tr) const;
346
351 [[nodiscard]] std::pair<std::vector<cda_rail::index_vector>,
352 std::vector<cda_rail::index_vector>>
353 common_entry_exit_vertices() const;
354
359 struct TemporaryImpossibilityStruct {
360 bool to_use{false};
361 size_t t_before{0};
362 size_t t_after{0};
363 double v_before{0};
364 double v_after{0};
365 cda_rail::index_vector edges_before;
366 cda_rail::index_vector edges_after;
367 };
379 [[nodiscard]] TemporaryImpossibilityStruct
380 get_temporary_impossibility_struct(const size_t& tr, const size_t& t) const;
381
393 [[nodiscard]] std::pair<size_t, size_t>
394 stop_time_indices(const ScheduledStop& stop) const;
395
396 [[nodiscard]] double
397 max_distance_travelled(const size_t& tr, const size_t& time_steps,
398 const double& v0, const double& a_max,
399 const bool& braking_distance) const;
400
401 [[nodiscard]] instances::SolVSSGeneralPerformanceOptimizationInstance
402 extract_solution(
403 bool postprocess, bool full_model,
404 const std::optional<instances::GeneralPerformanceOptimizationInstance>&
405 old_instance) const;
406
418 int extract_discretized_vss_positions(
419 instances::SolVSSGeneralPerformanceOptimizationInstance& sol_obj) const;
420
432 [[nodiscard]] cda_rail::index_vector extract_model_route(size_t tr) const;
433
443 bool update_vss(size_t relevant_edge_index, double obj_ub,
444 GRBLinExpr& cut_expr);
446 void update_max_vss_on_edge(size_t relevant_edge_index, size_t new_max_vss,
447 GRBLinExpr& cut_expr);
454 [[nodiscard]] std::optional<instances::GeneralPerformanceOptimizationInstance>
455 initialize_variables(const ModelDetailVSSGen& model_detail,
456 const ModelSettingsVSSGen& model_settings,
457 const SolverStrategyVSSGen& solver_strategy,
458 const SolutionSettingsVSSGen& solution_settings,
459 int time_limit, bool debug_input,
460 bool overwrite_severity);
461
462protected:
463 void cleanup() override;
464
466 void solve_init_vss_gen_timetable(bool debug_input, bool overwrite_severity) {
467 this->solve_init_general_mip(debug_input, overwrite_severity);
468 };
469
470public:
471 ~VSSGenTimetableSolver() override = default;
472 // Constructors
476 template <typename... Args>
477 explicit VSSGenTimetableSolver(Args&&... args)
478 requires(!IsSingleInstanceArgument<Args...>::value)
479 : GeneralMIPSolver(std::forward<Args>(args)...) {}
480
481 // Methods
496 solve(const ModelDetailVSSGen& model_detail,
497 const ModelSettingsVSSGen& model_settings = {},
498 const SolverStrategyVSSGen& solver_strategy = {},
499 const SolutionSettingsVSSGen& solution_settings = {},
500 int time_limit = -1, bool debug_input = false,
501 bool overwrite_severity = true);
502
504 [[nodiscard]] instances::SolVSSGeneralPerformanceOptimizationInstance
505 solve(int time_limit, bool debug_input, bool overwrite_severity) override {
506 return solve({}, {}, {}, {}, time_limit, debug_input, overwrite_severity);
507 }
508};
509
518 : public VSSGenTimetableSolver {
519private:
521 bool m_fix_orders_on_edges{true};
522 bool m_fix_stop_positions{true};
523 bool m_fix_exact_positions{true};
524 bool m_fix_exact_velocities{true};
525 bool m_hint_approximate_positions{true};
526
527 // Additional functions
529 void include_additional_information();
531 void fix_oder_on_edges();
533 void fix_stop_positions_constraints();
535 void fix_exact_positions_and_velocities_constraints();
537 void hint_approximate_positions_constraints();
538
539protected:
540 void cleanup() override;
541
542public:
544 // Constructors
547 moving_block_solution)
548 : VSSGenTimetableSolver(*moving_block_solution.get_instance()),
549 m_moving_block_solution(moving_block_solution) {
550 // All information extracted from the moving block solution (train orders,
551 // positions, velocities, ...) refers to the routes of that solution, which
552 // may differ from the routes specified in the instance. Since this solver
553 // always uses fixed routes, the instance routes are replaced by the ones
554 // of the moving block solution.
555 m_instance.set_routes(m_moving_block_solution.get_const_solution_routes());
556 };
557
558 // Methods
569 solve(const ModelDetailMBInformation& model_detail_mb_information,
570 const ModelSettingsVSSGen& model_settings = {},
571 const SolverStrategyVSSGen& solver_strategy = {},
572 const SolutionSettingsVSSGen& solution_settings = {},
573 int time_limit = -1, bool debug_input = false,
574 bool overwrite_severity = true);
575
577 [[nodiscard]] instances::SolVSSGeneralPerformanceOptimizationInstance
578 solve(int time_limit, bool debug_input, bool overwrite_severity) override {
579 return solve({}, {}, {}, {}, time_limit, debug_input, overwrite_severity);
580 }
581};
582
583} // namespace cda_rail::solver::mip_based
584
585#endif // CDA_RAIL_SOLVER_MIP_BASED_VSSGENTIMETABLESOLVER_HPP
Thrown when a consistency check fails.
Definition CustomExceptions.hpp:80
An instance in which the trains are routed as efficiently as possible.
Definition GeneralPerformanceOptimizationInstance.hpp:95
The trajectories a solver found for a performance optimization instance.
Definition GeneralPerformanceOptimizationInstance.hpp:641
The same solution, together with the VSS borders it needs.
Definition GeneralPerformanceOptimizationInstance.hpp:1064
S solve()
Solves the problem instance using default configuration.
Definition GeneralSolver.hpp:314
const instances::GeneralPerformanceOptimizationInstance & get_instance() const
Definition GeneralSolver.hpp:302
void cleanup() override
Releases the model and the environment after solving.
Definition VSSGenTimetableSolver_MovingBlockInformation.cpp:377
S solve()
Solves the problem instance using default configuration.
Definition GeneralSolver.hpp:314
VSSGenTimetableSolverWithMovingBlockInformation(const instances::SolGeneralPerformanceOptimizationInstance &moving_block_solution)
Definition VSSGenTimetableSolver.hpp:545
instances::SolVSSGeneralPerformanceOptimizationInstance solve(int time_limit, bool debug_input, bool overwrite_severity) override
Definition VSSGenTimetableSolver.hpp:578
friend class VSSGenTimetableSolverWithMovingBlockInformation
Definition VSSGenTimetableSolver.hpp:135
instances::SolVSSGeneralPerformanceOptimizationInstance solve(int time_limit, bool debug_input, bool overwrite_severity) override
Definition VSSGenTimetableSolver.hpp:505
S solve()
Solves the problem instance using default configuration.
Definition GeneralSolver.hpp:314
void solve_init_vss_gen_timetable(bool debug_input, bool overwrite_severity)
Initializes the logging and the Gurobi model.
Definition VSSGenTimetableSolver.hpp:466
void cleanup() override
Releases the model and the environment after solving.
Definition VSSGenTimetableSolver_helper.cpp:571
VSSGenTimetableSolver(Args &&... args)
Definition VSSGenTimetableSolver.hpp:477
VSSGenTimetableSolver(const instances::GeneralPerformanceOptimizationInstance &instance)
Definition VSSGenTimetableSolver.hpp:473
How a VSS generation solver models the VSS borders.
Definition VSSModel.hpp:205
Definition GeneralMIPSolver.hpp:17
constexpr std::string update_strategy_to_string(UpdateStrategyVSSGen strategy)
The name of strategy, as it is reported and parsed.
Definition VSSGenTimetableSolver.hpp:33
UpdateStrategyVSSGen
How the iterative approach increases the number of VSS per edge.
Definition VSSGenTimetableSolver.hpp:30
@ Relative
Definition VSSGenTimetableSolver.hpp:30
@ Fixed
Definition VSSGenTimetableSolver.hpp:30
constexpr std::string optimality_strategy_to_string(OptimalityStrategy strategy)
The name of strategy, as it is reported and parsed.
Definition VSSGenTimetableSolver.hpp:46
@ Continuous
Definition VSSModel.hpp:93
std::vector< size_t > index_vector
Definition Definitions.hpp:21
OptimalityStrategy
How hard a solver tries to prove optimality.
Definition Definitions.hpp:87
@ Feasible
Definition Definitions.hpp:90
@ TradeOff
Definition Definitions.hpp:89
@ Optimal
Definition Definitions.hpp:88
double braking_distance(double v, double d)
Helpers for 1D train motion under constant-acceleration bounds.
Definition EOMHelper.cpp:182
The same, plus how much of a moving block solution is used.
Definition VSSGenTimetableSolver.hpp:94
bool hint_approximate_positions
Definition VSSGenTimetableSolver.hpp:101
bool fix_order_on_edges
Definition VSSGenTimetableSolver.hpp:102
bool fix_exact_positions
Definition VSSGenTimetableSolver.hpp:99
bool fix_stop_positions
Definition VSSGenTimetableSolver.hpp:98
double delta_t
Definition VSSGenTimetableSolver.hpp:95
bool braking_curves
Definition VSSGenTimetableSolver.hpp:97
bool fix_exact_velocities
Definition VSSGenTimetableSolver.hpp:100
bool train_dynamics
Definition VSSGenTimetableSolver.hpp:96
How accurately the VSS generation model describes the operation.
Definition VSSGenTimetableSolver.hpp:78
bool train_dynamics
Definition VSSGenTimetableSolver.hpp:81
bool braking_curves
Definition VSSGenTimetableSolver.hpp:82
double delta_t
Definition VSSGenTimetableSolver.hpp:79
bool fix_routes
Definition VSSGenTimetableSolver.hpp:80
How the VSS borders and the braking distances are modelled.
Definition VSSGenTimetableSolver.hpp:114
bool use_pwl
Definition VSSGenTimetableSolver.hpp:117
bool convexify_braking_distance
Definition VSSGenTimetableSolver.hpp:119
bool use_schedule_cuts
Definition VSSGenTimetableSolver.hpp:118
vss::Model model_type
Definition VSSGenTimetableSolver.hpp:116
How the VSS generation model is solved.
Definition VSSGenTimetableSolver.hpp:67
bool include_cuts
Definition VSSGenTimetableSolver.hpp:74
cda_rail::OptimalityStrategy optimality_strategy
Definition VSSGenTimetableSolver.hpp:69
bool iterative_approach
Definition VSSGenTimetableSolver.hpp:68
double initial_value
Definition VSSGenTimetableSolver.hpp:72
UpdateStrategyVSSGen update_strategy
Definition VSSGenTimetableSolver.hpp:71
double update_value
Definition VSSGenTimetableSolver.hpp:73