MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
GeneralMIPSolver.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_SOLVER_MIP_BASED_GENERALMIPSOLVER_HPP
2#define CDA_RAIL_SOLVER_MIP_BASED_GENERALMIPSOLVER_HPP
3
4#include "MultiArray.hpp"
5#include "gurobi_c++.h"
6#include "gurobi_c.h"
9
10#include <optional>
11#include <plog/Log.h>
12#include <string>
13#include <type_traits>
14#include <unordered_map>
15#include <vector>
16
18
29 bool export_lp_model = false;
30 std::string model_name = "model";
31};
32
40
50
51// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast,readability-inconsistent-ifelse-braces,cppcoreguidelines-pro-bounds-array-to-pointer-decay)
52
59class MessageCallback : public GRBCallback {
60public:
61 explicit MessageCallback() = default;
62
63protected:
65 void callback() override {
66 if (where == GRB_CB_MESSAGE) {
67 std::string msg = getStringInfo(GRB_CB_MSG_STRING);
68 if (!msg.empty() && msg.back() == '\n') {
69 msg.pop_back(); // Remove the last character (newline)
70 }
71 PLOGI << msg;
72 }
73 }
74};
75
76// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast,readability-inconsistent-ifelse-braces,cppcoreguidelines-pro-bounds-array-to-pointer-decay)
77
89template <typename T, typename S>
90class GeneralMIPSolver : public GeneralSolver<T, S> {
91 static_assert(
92 std::is_base_of_v<cda_rail::instances::GeneralProblemInstance, T>,
93 "T must be a child of GeneralProblemInstance");
94 static_assert(
95 std::is_base_of_v<cda_rail::instances::SolGeneralProblemInstance, S>,
96 "S must be a child of SolGeneralProblemInstance<T>");
97
98protected:
99 std::vector<GRBTempConstr> m_lazy_constraints;
100
101 // Gurobi variables
102 std::optional<GRBEnv> m_env;
103 std::optional<GRBModel> m_model;
104 std::unordered_map<std::string, MultiArray<GRBVar>> m_vars;
106
108 virtual void cleanup() {
110 m_lazy_constraints.clear();
111 if (m_model.has_value()) {
112 m_model->reset(1);
113 }
114 m_vars.clear();
115 m_model.reset();
116 m_env.reset();
117 };
118
120 void solve_init_general_mip(bool debug_input, bool overwrite_severity) {
121 static auto message_callback = MessageCallback();
122 this->solve_init_general_mip(debug_input, overwrite_severity,
123 &message_callback);
124 };
125
134 void solve_init_general_mip(bool debug_input, bool overwrite_severity,
135 GRBCallback* cb) {
136 this->solve_init_general(debug_input, overwrite_severity);
137
138 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,readability-inconsistent-ifelse-braces)
139 PLOGD << "Create Gurobi environment and model";
140 this->m_env.emplace(true);
141 // this->m_env->set(GRB_StringParam_LogFile, "test_log.log");
142 this->m_env->start();
143 this->m_model.emplace(m_env.value());
144
145 this->m_model->setCallback(cb);
146 this->m_model->set(GRB_IntParam_LogToConsole, 0);
147 };
148
150 GeneralMIPSolver() = default;
152 explicit GeneralMIPSolver(const T& instance)
153 : GeneralSolver<T, S>(instance) {};
154
155 template <typename... Args>
156 explicit GeneralMIPSolver(Args&&... args)
157 requires(
159 : GeneralSolver<T, S>(std::forward<Args>(args)...) {}
160};
161} // namespace cda_rail::solver::mip_based
162
163#endif // CDA_RAIL_SOLVER_MIP_BASED_GENERALMIPSOLVER_HPP
void solve_init_general(bool debug_input, bool overwrite_severity)
Initializes logging and records solver start time.
Definition GeneralSolver.hpp:142
GeneralSolver()=default
Constructs a solver with a default-initialized problem instance.
GeneralMIPSolver()=default
Constructs a solver with a default-initialized problem instance.
void solve_init_general_mip(bool debug_input, bool overwrite_severity, GRBCallback *cb)
Initializes the logging, the Gurobi environment, and the model.
Definition GeneralMIPSolver.hpp:134
std::optional< GRBModel > m_model
Definition GeneralMIPSolver.hpp:103
std::optional< GRBEnv > m_env
Definition GeneralMIPSolver.hpp:102
std::vector< GRBTempConstr > m_lazy_constraints
Definition GeneralMIPSolver.hpp:99
void solve_init_general_mip(bool debug_input, bool overwrite_severity)
Initializes the model with the default message callback.
Definition GeneralMIPSolver.hpp:120
std::unordered_map< std::string, MultiArray< GRBVar > > m_vars
Definition GeneralMIPSolver.hpp:104
GeneralMIPSolver(const T &instance)
Constructs a solver from a problem instance.
Definition GeneralMIPSolver.hpp:152
GRBLinExpr m_objective_expr
Definition GeneralMIPSolver.hpp:105
GeneralMIPSolver(Args &&... args)
Constructs the problem instance from the given arguments.
Definition GeneralMIPSolver.hpp:156
virtual void cleanup()
Releases the model and the environment after solving.
Definition GeneralMIPSolver.hpp:108
Routes the output of Gurobi into the logging of this tool.
Definition GeneralMIPSolver.hpp:59
void callback() override
Logs one message of Gurobi, without its trailing newline.
Definition GeneralMIPSolver.hpp:65
Definition GeneralMIPSolver.hpp:17
SolutionSettingsMIP SolutionSettingsMovingBlock
Export settings of the moving block MIP solver.
Definition GeneralMIPSolver.hpp:39
Where a solver writes its solution to, see GeneralSolver::export_general_solution.
Definition GeneralSolver.hpp:61
Export settings shared by all MIP based solvers.
Definition GeneralMIPSolver.hpp:28
bool export_lp_model
Definition GeneralMIPSolver.hpp:29
std::string model_name
Definition GeneralMIPSolver.hpp:30
Export settings of the VSS generation MIP solver.
Definition GeneralMIPSolver.hpp:47
bool postprocess
Definition GeneralMIPSolver.hpp:48