MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
GenPOMovingBlockAStarSolver.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_SOLVER_ASTAR_BASED_GENPOMOVINGBLOCKASTARSOLVER_HPP
2#define CDA_RAIL_SOLVER_ASTAR_BASED_GENPOMOVINGBLOCKASTARSOLVER_HPP
3
5#include "Definitions.hpp"
10
11// NOLINTNEXTLINE(misc-include-cleaner)
12#include "gtest/gtest_prod.h"
13#include <cstddef>
14#include <cstdint>
15#include <filesystem>
16#include <optional>
17#include <queue>
18#include <string>
19#include <string_view>
20#include <vector>
21
22// If TEST_FRIENDS has value true, the corresponding test is friended to test
23// complex private functions.
24// This is not good practice. However, after consideration, it was decided that
25// - it is not reasonable to make the functions public
26// - they have a complexity that should be tested
27// - by only testing the overall solution, there is too much code tested at once
28#ifndef TEST_FRIENDS
29#define TEST_FRIENDS false
30#endif
31#if TEST_FRIENDS
32class GenPOMovingBlockAStarSolver_NextTrains_Test;
33class GenPOMovingBlockAStarSolver_PathExtensions_Test;
34class GenPOMovingBlockAStarSolver_PathExtensionsCloseToEnd_Test;
35class GenPOMovingBlockAStarSolver_InferInsertionBounds_Test;
36class GenPOMovingBlockAStarSolver_ExtendStateWithPathExtension_Test;
37class GenPOMovingBlockAStarSolver_ExtendTrainOrderEntry_Test;
38class GenPOMovingBlockAStarSolver_ExtendTrainOrderExit_Test;
39class GenPOMovingBlockAStarSolver_ExtendTrainOrderNothingToChange_Test;
40class GenPOMovingBlockAStarSolver_NextStates_Test;
41class GenPOMovingBlockAStarSolver_NextStatesTTD_Test;
42#endif
43
45// Every so many iterations the current state of the search is logged.
46#define DEBUG_LOGGING_RATE 1000
47
57enum class NextStateStrategy : std::uint8_t {
61};
62
63constexpr std::string
65 switch (strategy) {
67 return "SingleEdge";
69 return "NextTTD";
71 return "NextRelevantTTD";
72 default:
74 "Unknown next-state strategy");
75 }
76}
77
86 double dt = 6.0; // DB simulation default is 6 seconds
88 bool late_entry_possible = false;
90};
91
113} // namespace cda_rail::solver::astar_based
114
136 : public GeneralSolver<
137 instances::GeneralPerformanceOptimizationInstance,
138 instances::SolGeneralPerformanceOptimizationInstance> {
139private:
140#if TEST_FRIENDS
141 FRIEND_TEST(::GenPOMovingBlockAStarSolver, NextTrains);
142 FRIEND_TEST(::GenPOMovingBlockAStarSolver, PathExtensions);
143 FRIEND_TEST(::GenPOMovingBlockAStarSolver, PathExtensionsCloseToEnd);
144 FRIEND_TEST(::GenPOMovingBlockAStarSolver, InferInsertionBounds);
145 FRIEND_TEST(::GenPOMovingBlockAStarSolver, ExtendStateWithPathExtension);
146 FRIEND_TEST(::GenPOMovingBlockAStarSolver, ExtendTrainOrderEntry);
147 FRIEND_TEST(::GenPOMovingBlockAStarSolver, ExtendTrainOrderExit);
148 FRIEND_TEST(::GenPOMovingBlockAStarSolver, ExtendTrainOrderNothingToChange);
149 FRIEND_TEST(::GenPOMovingBlockAStarSolver, NextStates);
150 FRIEND_TEST(::GenPOMovingBlockAStarSolver, NextStatesTTD);
151#endif
152
153public:
154 // ------------------------
155 // CONSTRUCTOR
159
169
176 GenPOMovingBlockAStarSolver(std::string_view const instanceName,
177 std::string_view const instanceSubdirectory,
178 std::filesystem::path const& working_directory)
179 : GeneralSolver(instanceName, instanceSubdirectory, working_directory) {}
180
181 ~GenPOMovingBlockAStarSolver() override = default;
182
183 // Rule of 5 (due to virtual deconstructor)
194
195 // ----------------------------
196 // SOLVE
197 // ----------------------------
198
209 solve(int time_limit, bool debug_input, bool overwrite_severity) override {
210 return solve({}, {}, {}, time_limit, debug_input, overwrite_severity);
211 };
212
225 solve(const ModelDetailMBAStar& model_detail_input,
226 const SolverStrategyMBAStar& solver_strategy_input,
227 const GeneralSolutionSettings& solution_settings_input,
228 int time_limit = -1, bool debug_input = false,
229 bool overwrite_severity = true);
230
231private:
232 // ---------------------------
233 // STATE TRANSITION HELPER
234 // ---------------------------
235
236 // Relevant Trains
244 [[nodiscard]] static cda_rail::index_set get_all_trains_for_state_transition(
245 simulator::SimulatorState const& simulator_state,
260 [[nodiscard]] static cda_rail::index_set get_next_time_aware_train(
261 simulator::SimulatorState const& simulator_state,
262 simulator::SimulatorResults const& simulator_result,
276 [[nodiscard]] static cda_rail::index_set
277 get_relevant_trains_for_state_transition(
278 simulator::SimulatorState const& simulator_state,
279 simulator::SimulatorResults const& simulator_result,
281 SolverStrategyMBAStar const& solver_strategy);
282
283 // Path Extensions
291 [[nodiscard]] static std::vector<cda_rail::index_vector> get_entry_paths(
292 size_t tr,
294 struct PathExtensionData {
296 size_t stop_possible_from_idx_onward{0};
297
298 bool operator==(const PathExtensionData& other) const {
299 return path == other.path && stop_possible_from_idx_onward ==
300 other.stop_possible_from_idx_onward;
301 }
302 };
313 [[nodiscard]] static std::vector<PathExtensionData> get_path_extensions(
314 size_t tr, simulator::SimulatorState const& simulator_state,
315 NextStateStrategy next_state_strategy,
316 instances::GeneralPerformanceOptimizationInstance const* instance,
317 std::vector<cda_rail::index_set> const& ttd_sections);
318
319 // Train Order
320 struct IndexBound {
321 size_t lb{};
322 size_t ub{};
323
324 bool operator==(const IndexBound& other) const {
325 return lb == other.lb && ub == other.ub;
326 }
327 };
338 [[nodiscard]] static IndexBound infer_order_insertion_bounds(
339 size_t tr, cda_rail::index_vector const& prev_order,
340 cda_rail::index_vector const& next_order,
341 cda_rail::index_set const& tr_sharing_path, bool insert_at_end);
352 [[nodiscard]] static IndexBound infer_order_entry_order_bounds(
353 size_t tr, cda_rail::index_vector const& entry_order,
354 bool late_entry_possible, bool insert_at_end,
355 instances::GeneralPerformanceOptimizationInstance const* instance);
356
357 // State Extension
367 [[nodiscard]] static std::vector<simulator::SimulatorState>
368 extend_state_by_path_extension(
369 size_t tr, simulator::SimulatorState state,
370 PathExtensionData const& path_extension_data,
371 instances::GeneralPerformanceOptimizationInstance const* instance);
383 [[nodiscard]] static std::vector<simulator::SimulatorState>
384 extend_train_orders_of_state(
385 size_t tr, simulator::SimulatorState state,
386 const ModelDetailMBAStar& model_detail_input,
387 const SolverStrategyMBAStar& solver_strategy_input,
388 std::vector<cda_rail::index_set> const& ttd_sections,
389 instances::GeneralPerformanceOptimizationInstance const* instance);
404 [[nodiscard]] static std::vector<simulator::SimulatorState>
405 extend_train_orders_of_state_recursive_helper(
406 size_t tr, simulator::SimulatorState state,
407 const SolverStrategyMBAStar& solver_strategy_input,
408 std::vector<cda_rail::index_set> const& ttd_sections,
409 instances::GeneralPerformanceOptimizationInstance const* instance,
410 cda_rail::index_vector const& prev_order, size_t first_edge_index,
411 std::optional<size_t> const& safe_ttd);
412
429 [[nodiscard]] static std::vector<simulator::SimulatorState>
430 next_states(const simulator::SimulatorState& simulator_state,
431 const simulator::SimulatorResults& simulator_results,
432 const ModelDetailMBAStar& model_detail_input,
433 const SolverStrategyMBAStar& solver_strategy_input,
434 instances::GeneralPerformanceOptimizationInstance const* instance,
435 std::vector<cda_rail::index_set> const& ttd_sections);
436
437 // ----------------------
438 // DATA STRUCTURE
439 // ----------------------
440
441 struct StateObjectivePair {
442 double simulated_objective_val{};
443 double heuristic_val{};
444 double state_value{};
445 double unweighted_state_value{};
446 bool is_final_state{};
447 simulator::SimulatorState state{};
448 simulator::SimulatorResults results{};
449 };
450
452 struct CompareByObjective {
464 bool operator()(const StateObjectivePair& a,
465 const StateObjectivePair& b) const {
466 return (a.state_value > b.state_value) ||
467 (a.state_value == b.state_value && !a.is_final_state &&
468 b.is_final_state) ||
469 (a.state_value == b.state_value && !a.is_final_state &&
470 !b.is_final_state && b.state > a.state);
471 }
472 };
473
474 using MinPriorityQueue =
475 std::priority_queue<StateObjectivePair, std::vector<StateObjectivePair>,
476 CompareByObjective>;
477};
478} // namespace cda_rail::solver::astar_based
479
480#endif // CDA_RAIL_SOLVER_ASTAR_BASED_GENPOMOVINGBLOCKASTARSOLVER_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
S solve()
Solves the problem instance using default configuration.
Definition GeneralSolver.hpp:314
GenPOMovingBlockAStarSolver & operator=(const GenPOMovingBlockAStarSolver &)=default
Copy assignment operator.
S solve()
Solves the problem instance using default configuration.
Definition GeneralSolver.hpp:314
GenPOMovingBlockAStarSolver(GenPOMovingBlockAStarSolver &&)=default
Move constructor.
GenPOMovingBlockAStarSolver(const instances::GeneralPerformanceOptimizationInstance &instance)
Constructs the solver from a performance optimization instance.
Definition GenPOMovingBlockAStarSolver.hpp:166
GenPOMovingBlockAStarSolver & operator=(GenPOMovingBlockAStarSolver &&)=default
Move assignment operator.
GenPOMovingBlockAStarSolver()=default
Constructs a default solver instance.
GenPOMovingBlockAStarSolver(const GenPOMovingBlockAStarSolver &)=default
Copy constructor.
GenPOMovingBlockAStarSolver(std::string_view const instanceName, std::string_view const instanceSubdirectory, std::filesystem::path const &working_directory)
Constructs a solver instance from file-based configuration.
Definition GenPOMovingBlockAStarSolver.hpp:176
instances::SolGeneralPerformanceOptimizationInstance solve(int time_limit, bool debug_input, bool overwrite_severity) override
Solves the problem with default model and strategy configurations.
Definition GenPOMovingBlockAStarSolver.hpp:209
RemainingTimeHeuristicType
Which lower bound on the remaining time the A* search uses.
Definition GreedyHeuristic.hpp:20
@ Simple
Definition GreedyHeuristic.hpp:20
SimulatorType
Which simulator obtains the trajectories of a state.
Definition GeneralSimulator.hpp:28
@ Greedy
Definition GeneralSimulator.hpp:28
Definition CompletionHeuristic.hpp:10
constexpr std::string next_state_strategy_to_string(NextStateStrategy strategy)
The name of strategy, as it is reported and parsed.
Definition GenPOMovingBlockAStarSolver.hpp:64
NextStateStrategy
How far the trains are moved by one state transition.
Definition GenPOMovingBlockAStarSolver.hpp:57
@ NextRelevantTTD
Definition GenPOMovingBlockAStarSolver.hpp:60
@ SingleEdge
Definition GenPOMovingBlockAStarSolver.hpp:58
@ NextTTD
Definition GenPOMovingBlockAStarSolver.hpp:59
std::unordered_set< size_t > index_set
Definition Definitions.hpp:22
std::vector< size_t > index_vector
Definition Definitions.hpp:21
bool operator==(const FixedSizeVector< T > &lhs, const std::vector< T > &rhs)
Equality comparison between a FixedSizeVector and a std::vector.
Definition FixedSizeVector.hpp:276
What a simulation run produced.
Definition GeneralSimulator.hpp:51
Everything a simulation run needs beyond the instance itself.
Definition GeneralSimulator.hpp:74
Where a solver writes its solution to, see GeneralSolver::export_general_solution.
Definition GeneralSolver.hpp:61
What the simulation underlying the A* search does.
Definition GenPOMovingBlockAStarSolver.hpp:85
bool limit_speed_by_leaving_edges
Definition GenPOMovingBlockAStarSolver.hpp:89
double dt
Definition GenPOMovingBlockAStarSolver.hpp:86
bool late_entry_possible
Definition GenPOMovingBlockAStarSolver.hpp:88
simulator::SimulatorType simulator_type
Definition GenPOMovingBlockAStarSolver.hpp:87
How the A* search itself explores the states.
Definition GenPOMovingBlockAStarSolver.hpp:104
bool time_aware_state_transitions
Definition GenPOMovingBlockAStarSolver.hpp:109
NextStateStrategy next_state_strategy
Definition GenPOMovingBlockAStarSolver.hpp:107
double a_star_weight
Definition GenPOMovingBlockAStarSolver.hpp:110
size_t completion_interval
Definition GenPOMovingBlockAStarSolver.hpp:111
simulator::RemainingTimeHeuristicType remaining_time_heuristic_type
Definition GenPOMovingBlockAStarSolver.hpp:105
bool consider_earliest_exit
Definition GenPOMovingBlockAStarSolver.hpp:108