36 std::shared_ptr<MathLoggerDriver> mathLoggerDriver);
38 void set_solver_log_file(
const std::filesystem::path &log_file);
39 [[nodiscard]] std::filesystem::path solver_log_file()
const {
40 return solver_log_file_;
42 double execution_time()
const;
43 virtual std::string BendersName()
const = 0;
46 WorkerMasterDataVect AllCuts()
const;
49 LogData GetBestIterationData()
const;
51 int MasterRowIndex(
const std::string &row_name)
const;
52 void MasterChangeRhs(
int id_row,
double val)
const;
54 void MasterGetRhs(
double &rhs,
int id_row)
const;
55 const VariableMap &MasterVariables()
const {
return master_variable_map_; }
56 std::vector<double> MasterObjectiveFunctionCoeffs()
const;
57 void MasterRowsCoeffs(std::vector<int> &mstart, std::vector<int> &mclind,
58 std::vector<double> &dmatval,
int size,
59 std::vector<int> &nels,
int first,
int last)
const;
60 int MasterGetNElems()
const;
61 void MasterAddRows(std::vector<char>
const &qrtype_p,
62 std::vector<double>
const &rhs_p,
63 std::vector<double>
const &range_p,
64 std::vector<int>
const &mstart_p,
65 std::vector<int>
const &mclind_p,
66 std::vector<double>
const &dmatval_p,
67 const std::vector<std::string> &row_names = {})
const;
68 void MasterGetRowType(std::vector<char> &qrtype,
int first,
int last)
const;
69 void ResetMasterFromLastIteration();
71 bool MasterIsEmpty()
const;
72 void DoFreeProblems(
bool free_problems) { free_problems_ = free_problems; }
73 int MasterGetnrows()
const;
74 int MasterGetncols()
const;
76 void SetMasterObjectiveFunctionCoeffsToZeros()
const;
77 void SetMasterObjectiveFunction(
const double *coeffs,
int first,
79 virtual void InitializeProblems() = 0;
80 void SetMaxIteration(
int max_iteration) {
81 _options.MAX_ITERATIONS = max_iteration;
84 virtual void free() = 0;
86 int GetBendersRunNumber()
const {
return _data.criteria_current_iteration_data.benders_num_run; }
92 std::vector<double> GetOuterLoopCriterionAtBestBenders()
const;
94 void init_data(
double external_loop_lambda,
double external_loop_lambda_min,
double external_loop_lambda_max);
96 void SaveOuterLoopSolutionInOutputFile()
const;
97 void SaveCurrentOuterLoopIterationInOutputFile()
const;
98 void SetBilevelBestub(
double bilevel_best_ub);
99 void UpdateOuterLoopSolution();
101 bool isExceptionRaised()
const;
102 void UpdateOverallCosts();
105 std::shared_ptr<MathLoggerDriver> mathLoggerDriver_;
106 void setCriterionComputationInputs(
110 bool exception_raised_ =
false;
112 WorkerMasterDataVect workerMasterDataVect_;
115 VariableMap master_variable_map_;
116 CouplingMap coupling_map_;
119 bool init_data_ =
true;
120 bool init_problems_ =
true;
121 bool free_problems_ =
true;
123 std::vector<std::vector<double> > criteria_vector_for_each_iteration_;
124 bool is_bilevel_check_all_ =
false;
126 virtual void Run() = 0;
128 bool ShouldBendersStop();
129 bool is_initial_relaxation_requested()
const;
130 bool SwitchToIntegerMaster(
bool is_relaxed)
const;
132 virtual void ComputeXCut();
133 void ComputeInvestCost();
134 virtual void compute_ub();
137 virtual void post_run_actions()
const;
138 void BuildCutFull(
const SubProblemDataMap &subproblem_data_map);
139 virtual void DeactivateIntegrityConstraints()
const;
140 virtual void ActivateIntegrityConstraints()
const;
141 virtual void SetDataPreRelaxation();
142 virtual void ResetDataPostRelaxation();
144 std::string
const &subproblem_name)
const;
146 std::string
const &name)
const;
149 [[nodiscard]]
LogData bendersDataToLogData(
151 template <
typename T,
typename... Args>
152 void reset_master(Args &&...args) {
153 _master = std::make_shared<T>(std::forward<Args>(args)...);
154 master_is_empty_ =
false;
157 void free_subproblems();
158 void AddSubproblem(
const std::pair<std::string, VariableMap> &kvp);
159 [[nodiscard]]
virtual WorkerMasterPtr get_master()
const;
160 void MatchProblemToId();
161 void AddSubproblemName(
const std::string &name);
162 [[nodiscard]] std::string get_master_name()
const;
163 [[nodiscard]] std::string get_solver_name()
const;
164 [[nodiscard]]
int get_log_level()
const;
165 [[nodiscard]]
bool is_trace()
const;
166 [[nodiscard]] Point get_x_cut()
const;
167 void set_x_cut(
const Point &x0);
168 [[nodiscard]] Point get_x_out()
const;
169 void set_x_out(
const Point &x0);
170 [[nodiscard]]
double get_timer_master()
const;
171 void set_timer_master(
const double &timer_master);
172 [[nodiscard]]
double GetSubproblemsWalltime()
const;
173 void SetSubproblemsWalltime(
const double &duration);
174 [[nodiscard]]
double GetSubproblemsCpuTime()
const;
175 void SetSubproblemsCpuTime(
const double &duration);
176 [[nodiscard]]
double GetSubproblemsCumulativeCpuTime()
const;
177 void SetSubproblemsCumulativeCpuTime(
const double &duration);
178 [[nodiscard]]
double GetSubproblemCost()
const;
179 void SetSubproblemCost(
const double &subproblem_cost);
180 bool IsResumeMode()
const;
181 std::filesystem::path LastIterationFile()
const {
182 return std::filesystem::path(_options.LAST_ITERATION_JSON_FILE);
184 void UpdateMaxNumberIterationResumeMode(
const unsigned nb_iteration_done);
185 void SaveCurrentIterationInOutputFile()
const;
186 void SaveSolutionInOutputFile()
const;
187 void PrintCurrentIterationCsv();
190 void ChecksResumeMode();
191 virtual void SaveCurrentBendersData();
192 void ClearCurrentIterationCutTrace();
193 virtual void EndWritingInOutputFile()
const;
194 [[nodiscard]]
int GetNumIterationsBeforeRestart()
const {
195 return iterations_before_resume;
197 double GetBendersTime()
const;
198 virtual void write_basis()
const;
200 SubproblemsMapPtr GetSubProblemMap()
const {
return subproblem_map; }
201 StrVector GetSubProblemNames()
const {
return subproblems; }
202 double AbsoluteGap()
const {
return _options.ABSOLUTE_GAP; }
203 double RelativeGap()
const {
return _options.RELATIVE_GAP; }
204 double RelaxedGap()
const {
return _options.RELAXED_GAP; }
205 DblVector GetAlpha_i()
const {
return _data.single_subpb_costs_under_approx; }
206 void SetAlpha_i(
const DblVector &single_subpb_costs_under_approx) {
207 _data.single_subpb_costs_under_approx = single_subpb_costs_under_approx;
209 int ProblemToId(
const std::string &problem_name)
const {
210 return _problem_to_id.at(problem_name);
214 int GetNumOfSubProblemsSolvedBeforeResume() {
215 return cumulative_number_of_subproblem_resolved_before_resume;
219 void ResetSimplexIterationsBounds();
221 SubproblemsMapPtr subproblem_map;
224 virtual void SolveSubproblem(SubProblemDataMap &subproblem_data_map,
226 const std::string &name,
227 const std::shared_ptr<SubproblemWorker> &worker);
245 void print_master_and_cut(std::ostream &file,
int ite,
248 Point
const &xopt)
const;
249 void check_status(
const SubProblemDataMap &subproblem_data_map)
const;
250 [[nodiscard]]
LogData build_log_data_from_data()
const;
253 [[nodiscard]] std::string status_from_criterion()
const;
254 void compute_cut_aggregate(
const SubProblemDataMap &subproblem_data_map);
255 void compute_cut(
const SubProblemDataMap &subproblem_data_map);
256 [[nodiscard]] std::map<std::string, int> get_master_variable_map(
257 const std::map<std::string, std::map<std::string, int>> &input_map)
const;
258 [[nodiscard]]
virtual bool shouldParallelize()
const = 0;
262 bool master_is_empty_ =
true;
264 unsigned int _totalNbProblems = 0;
265 std::filesystem::path solver_log_file_ =
"";
266 WorkerMasterPtr _master;
267 VariableMap _problem_to_id;
268 StrVector subproblems;
269 std::ofstream _csv_file;
270 std::filesystem::path _csv_file_path;
272 int iterations_before_resume = 0;
273 int cumulative_number_of_subproblem_resolved_before_resume = 0;