1#ifndef CDA_RAIL_SOLVER_GENERALSOLVER_HPP
2#define CDA_RAIL_SOLVER_GENERALSOLVER_HPP
6#include "nlohmann/json.hpp"
7#include "nlohmann/json_fwd.hpp"
11#include "gtest/gtest_prod.h"
22#include <unordered_map>
32#define TEST_FRIENDS false
35class GenPOMovingBlockAStarSolver_SolverDataExport_Test;
77 nlohmann::json
const& solver_data) {
80 export_directory.string());
82 std::ofstream data_file(export_directory /
"solver_data.json");
83 if (!data_file.is_open()) {
85 "Could not open solver_data.json for writing");
87 data_file << solver_data <<
'\n';
108 FRIEND_TEST(::GenPOMovingBlockAStarSolver, SolverDataExport);
112 std::is_base_of_v<cda_rail::instances::GeneralProblemInstance, T>,
113 "T must be a child of GeneralProblemInstance");
115 std::is_base_of_v<cda_rail::instances::SolGeneralProblemInstance, S>,
116 "S must be a child of SolGeneralProblemInstance<T>");
120 decltype(std::chrono::high_resolution_clock::now())
m_start;
127 template <
typename... Args>
129 template <
typename Arg>
145 m_start = std::chrono::high_resolution_clock::now();
164 template <
typename... Args>
167 std::constructible_from<T, Args...>)
189 [[nodiscard]] nlohmann::json
192 data[
"model_creation_time"] =
194 data[
"model_solving_time"] =
198 for (
auto const& [key, value] : further_data.
bool_data) {
201 for (
auto const& [key, value] : further_data.
integer_data) {
204 for (
auto const& [key, value] : further_data.
double_data) {
207 for (
auto const& [key, value] : further_data.
string_data) {
242 std::optional<std::filesystem::path>
251 if constexpr (
requires(S& sol,
double dt) { sol.evaluate(dt); }) {
253 solution.has_solution()) {
258 }
catch (
const std::exception& e) {
259 PLOGW <<
"Exporting the solution without evaluation, since it "
267 const bool export_instance =
277 PLOGI <<
"Saving solution to " << export_path.string();
323 [[nodiscard]] S
solve(
int time_limit,
bool debug_input) {
324 return solve(time_limit, debug_input,
true);
336 [[nodiscard]]
virtual S
solve(
int time_limit,
bool debug_input,
337 bool overwrite_severity) = 0;
Thrown when export fails.
Definition CustomExceptions.hpp:63
nlohmann::json m_solver_data
Definition GeneralSolver.hpp:125
nlohmann::json solver_data(FurtherData const &further_data) const
Solver data, i.e., timing and further information (e.g. iterations, nodes, etc.), as written to solve...
Definition GeneralSolver.hpp:190
void solve_init_general(bool debug_input, bool overwrite_severity)
Initializes logging and records solver start time.
Definition GeneralSolver.hpp:142
virtual ~GeneralSolver()=default
Virtual destructor.
S solve()
Solves the problem instance using default configuration.
Definition GeneralSolver.hpp:314
decltype(std::chrono::high_resolution_clock::now()) m_model_created
Definition GeneralSolver.hpp:121
GeneralSolver(Args &&... args)
Constructs a GeneralSolver with arguments for the problem instance constructor.
Definition GeneralSolver.hpp:165
int64_t m_create_time
Definition GeneralSolver.hpp:123
decltype(std::chrono::high_resolution_clock::now()) m_model_solved
Definition GeneralSolver.hpp:122
int64_t m_solve_time
Definition GeneralSolver.hpp:124
virtual S solve(int time_limit, bool debug_input, bool overwrite_severity)=0
Solves the problem instance, which every solver implements.
std::optional< std::filesystem::path > export_general_solution(S &solution, const GeneralSolutionSettings &solution_settings, FurtherData const &further_data={})
Exports the solution together with the solver data if requested.
Definition GeneralSolver.hpp:243
GeneralSolver(T instance)
Constructs a solver from a problem instance.
Definition GeneralSolver.hpp:157
const nlohmann::json & get_solver_data() const
The solver data of the last solve, as written to solver_data.json if the solution is exported,...
Definition GeneralSolver.hpp:294
T m_instance
Definition GeneralSolver.hpp:119
const T & get_instance() const
Accesses the problem instance stored in this solver.
Definition GeneralSolver.hpp:302
decltype(std::chrono::high_resolution_clock::now()) m_start
Definition GeneralSolver.hpp:120
S solve(int time_limit, bool debug_input)
Solves the problem instance.
Definition GeneralSolver.hpp:323
void export_solver_data(std::filesystem::path const &export_directory, FurtherData const &further_data={})
Exports solver data, see solver_data, into solver_data.json.
Definition GeneralSolver.hpp:220
GeneralSolver()=default
Constructs a solver with a default-initialized problem instance.
T & editable_instance()
Provides mutable access to the problem instance.
Definition GeneralSolver.hpp:307
Definition CompletionHeuristic.hpp:10
void write_solver_data(std::filesystem::path const &export_directory, nlohmann::json const &solver_data)
Writes solver data, see GeneralSolver::get_solver_data, to solver_data.json in export_directory.
Definition GeneralSolver.hpp:76
GeneralExportOption
What a solver writes to disk once it is done.
Definition GeneralSolver.hpp:46
@ NoExport
Definition GeneralSolver.hpp:47
@ ExportSolution
Definition GeneralSolver.hpp:48
@ ExportSolutionWithInstance
Definition GeneralSolver.hpp:49
bool is_directory_and_create(const std::filesystem::path &p)
Ensures that a filesystem path exists as a directory.
Definition GeneralHelper.cpp:99
double get_time_difference_in_seconds(std::chrono::time_point< Clock, Duration1 > const &start, std::chrono::time_point< Clock, Duration2 > const &end)
Definition GeneralHelper.hpp:136
void initialize_plog(bool debug_input, bool overwrite_severity=false)
Initializes the global plog console logger and adjusts its severity.
Definition GeneralHelper.cpp:89
std::string concatenate_string_views(std::initializer_list< std::string_view > parts)
Concatenates multiple string views into a single string.
Definition StringHelper.cpp:11
Where a solver writes its solution to, see GeneralSolver::export_general_solution.
Definition GeneralSolver.hpp:61
GeneralExportOption export_option
Definition GeneralSolver.hpp:62
std::string working_directory
Definition GeneralSolver.hpp:63
std::optional< double > evaluation_time_step
Definition GeneralSolver.hpp:66
std::optional< std::string > parameter_identifier
Definition GeneralSolver.hpp:65
std::string solution_subdirectory
Definition GeneralSolver.hpp:64
Solver-specific values to be written to solver_data.json.
Definition GeneralSolver.hpp:177
std::unordered_map< std::string, int > integer_data
Definition GeneralSolver.hpp:179
std::unordered_map< std::string, std::string > string_data
Definition GeneralSolver.hpp:181
std::unordered_map< std::string, bool > bool_data
Definition GeneralSolver.hpp:178
std::unordered_map< std::string, double > double_data
Definition GeneralSolver.hpp:180
Definition GeneralSolver.hpp:128