diff --git a/docs/user-guide/get-started/adequacy-criterion.md b/docs/user-guide/get-started/adequacy-criterion.md index 9f92c2520..6f0902447 100644 --- a/docs/user-guide/get-started/adequacy-criterion.md +++ b/docs/user-guide/get-started/adequacy-criterion.md @@ -47,5 +47,5 @@ Several log files are written: - `reportbenders.txt` gives information on the progress of the algorithm with an operational perspective, - `benders_solver.log` contains more detailed information on all data of interest to follow the progress of the algorithm (`lambda_min`, `lambda_max`, detailed solving times, ...). -- The file `criterions.txt` under `lp/` dir stores adequacy criterions for all valid patterns (area+criterion) +- The file `LOLD.txt` under `lp/` dir stores adequacy criteria for all valid patterns (area+criterion) - The file `PositiveUnsuppliedEnergy.txt` under `lp/` dir stores the amount of unsupplied energy for all valid patterns (area+criterion) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index 0680aeb06..574f2cb51 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -153,7 +153,7 @@ void BendersByBatch::SeparationLoop() { SolveBatches(); if (Rank() == rank_0) { - outer_loop_criterion_.push_back(_data.outer_loop_current_iteration_data.outer_loop_criterion); + criteria_vector_for_each_iteration_.push_back(_data.criteria_current_iteration_data.criteria); // TODO // UpdateOuterLoopMaxCriterionArea(); UpdateTrace(); diff --git a/src/cpp/benders/benders_core/BendersBase.cpp b/src/cpp/benders/benders_core/BendersBase.cpp index 4a3067d12..5df28bfb1 100644 --- a/src/cpp/benders/benders_core/BendersBase.cpp +++ b/src/cpp/benders/benders_core/BendersBase.cpp @@ -42,7 +42,7 @@ void BendersBase::init_data() { _data.iteration_time = 0; _data.timer_master = 0; _data.subproblems_walltime = 0; - outer_loop_criterion_.clear(); + criteria_vector_for_each_iteration_.clear(); } void BendersBase::OpenCsvFile() { @@ -168,10 +168,10 @@ void BendersBase::update_best_ub() { _data.best_ub = _data.ub; _data.best_it = _data.it; FillWorkerMasterData(relevantIterationData_.best); - _data.outer_loop_current_iteration_data.max_criterion_best_it = - _data.outer_loop_current_iteration_data.max_criterion; - _data.outer_loop_current_iteration_data.max_criterion_area_best_it = - _data.outer_loop_current_iteration_data.max_criterion_area; + _data.criteria_current_iteration_data.max_criterion_best_it = + _data.criteria_current_iteration_data.max_criterion; + _data.criteria_current_iteration_data.max_criterion_area_best_it = + _data.criteria_current_iteration_data.max_criterion_area; relevantIterationData_.best._cut_trace = relevantIterationData_.last._cut_trace; best_iteration_data = bendersDataToLogData(_data); } @@ -534,7 +534,7 @@ void BendersBase::SaveCurrentOuterLoopIterationInOutputFile() const { if (LastWorkerMasterData._valid) { _writer->write_iteration( iteration(LastWorkerMasterData), - _data.outer_loop_current_iteration_data.benders_num_run); + _data.criteria_current_iteration_data.benders_num_run); _writer->dump(); } } @@ -594,7 +594,7 @@ Output::SolutionData BendersBase::solution() const { void BendersBase::UpdateOuterLoopSolution() { outer_loop_solution_data_ = BendersSolution(); outer_loop_solution_data_.best_it = - _data.outer_loop_current_iteration_data.benders_num_run; + _data.criteria_current_iteration_data.benders_num_run; } Output::SolutionData BendersBase::GetOuterLoopSolution() const { @@ -995,13 +995,13 @@ WorkerMasterData BendersBase::BestIterationWorkerMaster() const { CurrentIterationData BendersBase::GetCurrentIterationData() const { return _data; } -OuterLoopCurrentIterationData BendersBase::GetOuterLoopData() const { - return _data.outer_loop_current_iteration_data; +CriteriaCurrentIterationData BendersBase::GetOuterLoopData() const { + return _data.criteria_current_iteration_data; } std::vector BendersBase::GetOuterLoopCriterionAtBestBenders() const { - return ((outer_loop_criterion_.empty()) + return ((criteria_vector_for_each_iteration_.empty()) ? std::vector() - : outer_loop_criterion_[_data.best_it - 1]); + : criteria_vector_for_each_iteration_[_data.best_it - 1]); } void BendersBase::init_data(double external_loop_lambda, @@ -1009,19 +1009,19 @@ void BendersBase::init_data(double external_loop_lambda, double external_loop_lambda_max) { benders_timer.restart(); auto benders_num_run = - _data.outer_loop_current_iteration_data.benders_num_run; + _data.criteria_current_iteration_data.benders_num_run; auto outer_loop_bilevel_best_ub = - _data.outer_loop_current_iteration_data.outer_loop_bilevel_best_ub; + _data.criteria_current_iteration_data.outer_loop_bilevel_best_ub; init_data(); - _data.outer_loop_current_iteration_data.outer_loop_criterion.clear(); - _data.outer_loop_current_iteration_data.benders_num_run = benders_num_run; - _data.outer_loop_current_iteration_data.outer_loop_bilevel_best_ub = + _data.criteria_current_iteration_data.criteria.clear(); + _data.criteria_current_iteration_data.benders_num_run = benders_num_run; + _data.criteria_current_iteration_data.outer_loop_bilevel_best_ub = outer_loop_bilevel_best_ub; - _data.outer_loop_current_iteration_data.external_loop_lambda = + _data.criteria_current_iteration_data.lambda = external_loop_lambda; - _data.outer_loop_current_iteration_data.external_loop_lambda_min = + _data.criteria_current_iteration_data.lambda_min = external_loop_lambda_min; - _data.outer_loop_current_iteration_data.external_loop_lambda_max = + _data.criteria_current_iteration_data.lambda_max = external_loop_lambda_max; } @@ -1044,7 +1044,7 @@ void BendersBase::UpdateOverallCosts() { } void BendersBase::SetBilevelBestub(double bilevel_best_ub) { - _data.outer_loop_current_iteration_data.outer_loop_bilevel_best_ub = + _data.criteria_current_iteration_data.outer_loop_bilevel_best_ub = bilevel_best_ub; } diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 143c9885f..b60355cc8 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -216,29 +216,29 @@ void PrintExternalLoopData(LogDestination& log_destination, const HEADERSTYPE& type, const BENDERSMETHOD& method) { log_destination.InsertDelimiter(); - log_destination << data.outer_loop_current_iteration_data.benders_num_run; + log_destination << data.criteria_current_iteration_data.benders_num_run; log_destination.InsertDelimiter(); log_destination << std::scientific << std::setprecision(10) - << data.outer_loop_current_iteration_data.max_criterion; + << data.criteria_current_iteration_data.max_criterion; log_destination.InsertDelimiter(); - log_destination << data.outer_loop_current_iteration_data.max_criterion_area; + log_destination << data.criteria_current_iteration_data.max_criterion_area; log_destination.InsertDelimiter(); log_destination << std::scientific << std::setprecision(10) - << data.outer_loop_current_iteration_data.outer_loop_bilevel_best_ub; + << data.criteria_current_iteration_data.outer_loop_bilevel_best_ub; log_destination.InsertDelimiter(); log_destination << std::scientific << std::setprecision(10) - << data.outer_loop_current_iteration_data.external_loop_lambda; + << data.criteria_current_iteration_data.lambda; log_destination.InsertDelimiter(); log_destination << std::scientific << std::setprecision(10) - << data.outer_loop_current_iteration_data.external_loop_lambda_min; + << data.criteria_current_iteration_data.lambda_min; log_destination.InsertDelimiter(); log_destination << std::scientific << std::setprecision(10) - << data.outer_loop_current_iteration_data.external_loop_lambda_max; + << data.criteria_current_iteration_data.lambda_max; PrintBendersData(log_destination, data, type, method); } void MathLoggerBaseExternalLoop::Print(const CurrentIterationData& data) { diff --git a/src/cpp/benders/benders_core/CriterionComputation.cpp b/src/cpp/benders/benders_core/CriterionComputation.cpp index 2cd6177b8..0033da69e 100644 --- a/src/cpp/benders/benders_core/CriterionComputation.cpp +++ b/src/cpp/benders/benders_core/CriterionComputation.cpp @@ -2,46 +2,46 @@ namespace Benders::Criterion { -void CriterionComputation::ComputeOuterLoopCriterion( +void CriterionComputation::ComputeCriterion( double subproblem_weight, const std::vector &sub_problem_solution, - std::vector &outerLoopCriterions, - std::vector &outerLoopPatternsValues) { - auto outer_loop_input_size = var_indices_.size(); // num of patterns - outerLoopCriterions.resize(outer_loop_input_size, 0.); - outerLoopPatternsValues.resize(outer_loop_input_size, 0.); - - double criterion_count_threshold = - outer_loop_input_data_.CriterionCountThreshold(); - - for (int pattern_index(0); pattern_index < outer_loop_input_size; - ++pattern_index) { - auto pattern_variables_indices = var_indices_[pattern_index]; - for (auto variables_index : pattern_variables_indices) { - const auto &solution = sub_problem_solution[variables_index]; - outerLoopPatternsValues[pattern_index] += solution; - if (solution > criterion_count_threshold) - // 1h of no supplied energy - outerLoopCriterions[pattern_index] += subproblem_weight; + std::vector &criteria, + std::vector &patterns_values) { + auto criteria_input_size = var_indices_.size(); // num of patterns + criteria.resize(criteria_input_size, 0.); + patterns_values.resize(criteria_input_size, 0.); + + double criterion_count_threshold = + criterion_input_data_.CriterionCountThreshold(); + + for (int pattern_index(0); pattern_index < criteria_input_size; + ++pattern_index) { + auto pattern_variables_indices = var_indices_[pattern_index]; + for (auto variables_index: pattern_variables_indices) { + const auto &solution = sub_problem_solution[variables_index]; + patterns_values[pattern_index] += solution; + if (solution > criterion_count_threshold) + // 1h of no supplied energy + criteria[pattern_index] += subproblem_weight; + } } - } } void CriterionComputation::SearchVariables( const std::vector &variables) { Benders::Criterion::VariablesGroup variablesGroup( - variables, outer_loop_input_data_.CriterionsData()); + variables, criterion_input_data_.Criteria()); var_indices_ = variablesGroup.Indices(); } -const CriterionInputData &CriterionComputation::getOuterLoopInputData() const { - return outer_loop_input_data_; +const CriterionInputData &CriterionComputation::getCriterionInputData() const { + return criterion_input_data_; } std::vector> &CriterionComputation::getVarIndices() { return var_indices_; } CriterionComputation::CriterionComputation( - const CriterionInputData &outer_loop_input_data) - : outer_loop_input_data_(outer_loop_input_data) {} - + const CriterionInputData &criterion_input_data) + : criterion_input_data_(criterion_input_data) { +} } // namespace Benders::Criterion \ No newline at end of file diff --git a/src/cpp/benders/benders_core/CriterionInputDataReader.cpp b/src/cpp/benders/benders_core/CriterionInputDataReader.cpp index 246fb515b..17cc1a029 100644 --- a/src/cpp/benders/benders_core/CriterionInputDataReader.cpp +++ b/src/cpp/benders/benders_core/CriterionInputDataReader.cpp @@ -51,7 +51,7 @@ void CriterionInputData::AddSingleData(const CriterionSingleInputData &data) { } const std::vector & -CriterionInputData::CriterionsData() const { +CriterionInputData::Criteria() const { return criterion_vector_; } @@ -127,9 +127,9 @@ class convert { err_msg << PrefixMessage(LogUtils::LOGLEVEL::FATAL, "Outer Loop") << "Error could not read 'area' field in outer loop input file" << "\n"; - throw CriterionCouldNotReadAreaField(err_msg.str(), LOGLOCATION); + throw CouldNotReadAreaField(err_msg.str(), LOGLOCATION); } - auto criterion = pattern["criterion"]; + const auto criterion = pattern["criterion"]; if (criterion.IsNull()) { std::ostringstream err_msg; @@ -137,7 +137,7 @@ class convert { << PrefixMessage(LogUtils::LOGLEVEL::FATAL, "Outer Loop") << "Error could not read 'criterion' field in outer loop input file" << "\n"; - throw CriterionCouldNotReadCriterionField(err_msg.str(), LOGLOCATION); + throw CouldNotReadCriterionField(err_msg.str(), LOGLOCATION); } rhs.SetCriterion(criterion.as()); diff --git a/src/cpp/benders/benders_core/VariablesGroup.cpp b/src/cpp/benders/benders_core/VariablesGroup.cpp index aed8e7200..c359dbf3c 100644 --- a/src/cpp/benders/benders_core/VariablesGroup.cpp +++ b/src/cpp/benders/benders_core/VariablesGroup.cpp @@ -14,8 +14,8 @@ using namespace Benders::Criterion; VariablesGroup::VariablesGroup( const std::vector& all_variables, - const std::vector& outer_loop_single_input_data) - : all_variables_(all_variables), outer_loop_single_input_data_(outer_loop_single_input_data) { + const std::vector& criterion_single_input_data) + : all_variables_(all_variables), criterion_single_input_data_(criterion_single_input_data) { Search(); } @@ -24,11 +24,11 @@ std::vector> VariablesGroup::Indices() const { } void VariablesGroup::Search() { - indices_.assign(outer_loop_single_input_data_.size(), {}); + indices_.assign(criterion_single_input_data_.size(), {}); int var_index(0); for (const auto& variable : all_variables_) { int pattern_index(0); - for (const auto& single_input_data : outer_loop_single_input_data_) { + for (const auto& single_input_data : criterion_single_input_data_) { if (std::regex_search(variable, single_input_data.Pattern().MakeRegex())) { indices_[pattern_index].push_back(var_index); } diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersBase.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersBase.h index 86a56936e..3f9be98ae 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersBase.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersBase.h @@ -82,9 +82,13 @@ class BendersBase { } BendersBaseOptions Options() const { return _options; } virtual void free() = 0; - int GetBendersRunNumber() const { return _data.outer_loop_current_iteration_data.benders_num_run; } + + int GetBendersRunNumber() const { return _data.criteria_current_iteration_data.benders_num_run; } + CurrentIterationData GetCurrentIterationData() const; - OuterLoopCurrentIterationData GetOuterLoopData() const; + + CriteriaCurrentIterationData GetOuterLoopData() const; + std::vector GetOuterLoopCriterionAtBestBenders() const; virtual void init_data(); void init_data(double external_loop_lambda, double external_loop_lambda_min, double external_loop_lambda_max); @@ -116,7 +120,7 @@ class BendersBase { bool init_problems_ = true; bool free_problems_ = true; - std::vector> outer_loop_criterion_; + std::vector > criteria_vector_for_each_iteration_; bool is_bilevel_check_all_ = false; virtual void Run() = 0; diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersMathLogger.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersMathLogger.h index c1a62c16f..a48a90fb8 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersMathLogger.h @@ -134,7 +134,7 @@ struct MathLoggerExternalLoopSpecific : public MathLogger { explicit MathLoggerExternalLoopSpecific( const std::filesystem::path& file_path, const std::vector& headers, - T OuterLoopCurrentIterationData::*ptr) + T CriteriaCurrentIterationData::*ptr) : MathLogger(file_path), ptr_(ptr) { headers_.push_back("Outer loop"); headers_.push_back("Ite"); @@ -150,7 +150,7 @@ struct MathLoggerExternalLoopSpecific : public MathLogger { private: std::vector headers_; - T OuterLoopCurrentIterationData::*ptr_; + T CriteriaCurrentIterationData::*ptr_; }; class MathLoggerImplementation : public MathLoggerBehaviour { @@ -193,7 +193,7 @@ class MathLoggerDriver : public ILoggerXpansion { template void add_logger(const std::filesystem::path& file_path, const std::vector& headers, - T OuterLoopCurrentIterationData::*t); + T CriteriaCurrentIterationData::*t); void Print(const CurrentIterationData& data); virtual void PrintIterationSeparatorBegin() override; virtual void PrintIterationSeparatorEnd() override; @@ -206,7 +206,7 @@ class MathLoggerDriver : public ILoggerXpansion { template void MathLoggerDriver::add_logger(const std::filesystem::path& file_path, const std::vector& headers, - T OuterLoopCurrentIterationData::*t) { + T CriteriaCurrentIterationData::*t) { auto impl = std::make_shared>(file_path, headers, t); add_logger(std::make_shared(impl)); @@ -220,11 +220,11 @@ template void MathLoggerExternalLoopSpecific::Print( const CurrentIterationData& data) { LogsDestination().InsertDelimiter(); - LogsDestination() << data.outer_loop_current_iteration_data.benders_num_run; + LogsDestination() << data.criteria_current_iteration_data.benders_num_run; LogsDestination().InsertDelimiter(); LogsDestination() << data.it; LogsDestination().InsertDelimiter(); - for (const auto& t : data.outer_loop_current_iteration_data.*ptr_) { + for (const auto& t : data.criteria_current_iteration_data.*ptr_) { LogsDestination() << std::scientific << std::setprecision(10) << t; LogsDestination().InsertDelimiter(); } diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersStructsDatas.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersStructsDatas.h index d11659950..c193252ac 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersStructsDatas.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/BendersStructsDatas.h @@ -5,16 +5,16 @@ #include "Worker.h" #include "common.h" -struct OuterLoopCurrentIterationData{ +struct CriteriaCurrentIterationData { int benders_num_run = 0; - std::vector outer_loop_criterion = {}; - std::vector outer_loop_patterns_values = {}; + std::vector criteria = {}; + std::vector patterns_values = {}; double max_criterion = 0.; double max_criterion_best_it = 0.; double outer_loop_bilevel_best_ub = +1e20; - double external_loop_lambda = 0.; - double external_loop_lambda_min = 0.; - double external_loop_lambda_max = 0.; + double lambda = 0.; + double lambda_min = 0.; + double lambda_max = 0.; std::string max_criterion_area = "N/A"; std::string max_criterion_area_best_it = "N/A"; }; @@ -53,7 +53,7 @@ struct CurrentIterationData { int min_simplexiter; int max_simplexiter; // ugly - OuterLoopCurrentIterationData outer_loop_current_iteration_data; + CriteriaCurrentIterationData criteria_current_iteration_data; }; // /*! \struct to store benders cuts data diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionComputation.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionComputation.h index a8d841462..6e4155b10 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionComputation.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionComputation.h @@ -15,13 +15,13 @@ class CriterionComputation { * @brief Constructs a CriterionComputation object. * * This constructor initializes the CriterionComputation instance with the - * provided outer loop input data. + * provided criterion input data. * - * @param outer_loop_input_data The input data to be used for criterion + * @param criterion_input_data The input data to be used for criterion * computation. */ explicit CriterionComputation( - const CriterionInputData &outer_loop_input_data); + const CriterionInputData &criterion_input_data); /** * @brief Searches for relevant variables based on the provided variable @@ -36,25 +36,25 @@ class CriterionComputation { void SearchVariables(const std::vector &variables); /** - * @brief Computes the outer loop criterion based on subproblem solutions. + * @brief Computes the criteria based on subproblem solutions. * - * This method calculates the outer loop criteria and pattern values + * This method calculates the criterion criteria and pattern values * based on the provided subproblem weight and solution. It updates the - * outerLoopCriterions and outerLoopPatternsValues vectors accordingly. + * criteria and patterns values vectors accordingly. * * @param subproblem_weight The weight of the subproblem affecting the * criteria. * @param sub_problem_solution A vector containing the solutions of the * subproblem. - * @param outerLoopCriterions A reference to a vector where the computed + * @param criteria A reference to a vector where the computed * criteria will be stored. - * @param outerLoopPatternsValues A reference to a vector where the computed + * @param patterns_values A reference to a vector where the computed * pattern values will be stored. */ - void ComputeOuterLoopCriterion( + void ComputeCriterion( double subproblem_weight, const std::vector &sub_problem_solution, - std::vector &outerLoopCriterions, - std::vector &outerLoopPatternsValues); + std::vector &criteria, + std::vector &patterns_values); /** * @brief Retrieves the variable indices. @@ -67,17 +67,17 @@ class CriterionComputation { std::vector> &getVarIndices(); /** - * @brief Retrieves the outer loop input data. + * @brief Retrieves the criterion input data. * - * This method returns a constant reference to the outer loop input data + * This method returns a constant reference to the criterion input data * associated with this CriterionComputation instance. * - * @return A constant reference to the OuterLoopInputData object. + * @return A constant reference to the CriterionInputData object. */ - const CriterionInputData &getOuterLoopInputData() const; + const CriterionInputData &getCriterionInputData() const; - private: +private: std::vector> var_indices_ = {}; - CriterionInputData outer_loop_input_data_; + CriterionInputData criterion_input_data_; }; } // namespace Benders::Criterion \ No newline at end of file diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h index 11accb803..df576689a 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h @@ -30,14 +30,14 @@ class CriterionInputPatternsShouldBeArray using LogUtils::XpansionError::XpansionError; }; -class CriterionCouldNotReadAreaField - : public LogUtils::XpansionError { - using LogUtils::XpansionError::XpansionError; +class CouldNotReadAreaField + : public LogUtils::XpansionError { + using LogUtils::XpansionError::XpansionError; }; -class CriterionCouldNotReadCriterionField - : public LogUtils::XpansionError { - using LogUtils::XpansionError::XpansionError; +class CouldNotReadCriterionField + : public LogUtils::XpansionError { + using LogUtils::XpansionError::XpansionError; }; /// @brief lovely class @@ -82,8 +82,9 @@ class CriterionInputData { public: CriterionInputData() = default; - [[nodiscard]] const std::vector &CriterionsData() - const; + [[nodiscard]] const std::vector &Criteria() + const; + [[nodiscard]] std::vector PatternBodies() const; [[nodiscard]] std::string PatternsPrefix() const; diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/SubproblemCut.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/SubproblemCut.h index ddcf43a87..5a0adb214 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/SubproblemCut.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/SubproblemCut.h @@ -9,9 +9,9 @@ namespace PlainData { struct SubProblemData { double subproblem_cost; Point var_name_and_subgradient; - std::vector outer_loop_criterions; + std::vector criteria; // no-supplied energy - std::vector outer_loop_patterns_values; + std::vector patterns_values; double single_subpb_costs_under_approx; double subproblem_timer; int simplex_iter; @@ -21,7 +21,7 @@ struct SubProblemData { void serialize(Archive &ar, const unsigned int version) { ar & subproblem_cost; ar & var_name_and_subgradient; - ar & outer_loop_criterions; + ar & criteria; ar & single_subpb_costs_under_approx; ar & subproblem_timer; ar & simplex_iter; diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/VariablesGroup.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/VariablesGroup.h index afd458b07..69a3f5444 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/VariablesGroup.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/VariablesGroup.h @@ -9,13 +9,14 @@ class VariablesGroup { public: explicit VariablesGroup(const std::vector& all_variables, const std::vector& - outer_loop_single_input_data); + criterion_single_input_data); + [[nodiscard]] std::vector> Indices() const; private: void Search(); const std::vector& all_variables_; - const std::vector& outer_loop_single_input_data_; + const std::vector &criterion_single_input_data_; std::vector> indices_; }; } // namespace Benders::Criterion \ No newline at end of file diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index 02056fe41..550bf1beb 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -159,10 +159,10 @@ void BendersMpi::GatherCuts(const SubProblemDataMap &subproblem_data_map, // only rank_0 receive non-emtpy gathered_subproblem_map master_build_cuts(gathered_subproblem_map); - ComputeSubproblemsContributionToOuterLoopCriterion(subproblem_data_map); + ComputeSubproblemsContributionToCriteria(subproblem_data_map); if (_world.rank() == rank_0) { - outer_loop_criterion_.push_back( - _data.outer_loop_current_iteration_data.outer_loop_criterion); + criteria_vector_for_each_iteration_.push_back( + _data.criteria_current_iteration_data.criteria); UpdateMaxCriterionArea(); } } @@ -176,54 +176,54 @@ void BendersMpi::SolveSubproblem( std::vector solution; worker->get_solution(solution); - criterion_computation_.ComputeOuterLoopCriterion( - SubproblemWeight(_data.nsubproblem, name), solution, - subproblem_data.outer_loop_criterions, - subproblem_data.outer_loop_patterns_values); + criterion_computation_.ComputeCriterion( + SubproblemWeight(_data.nsubproblem, name), solution, + subproblem_data.criteria, + subproblem_data.patterns_values); } void BendersMpi::UpdateMaxCriterionArea() { - auto criterions_begin = - _data.outer_loop_current_iteration_data.outer_loop_criterion.cbegin(); - auto criterions_end = - _data.outer_loop_current_iteration_data.outer_loop_criterion.cend(); - auto max_criterion_it = std::max_element(criterions_begin, criterions_end); - if (max_criterion_it != criterions_end) { - _data.outer_loop_current_iteration_data.max_criterion = *max_criterion_it; + auto criteria_begin = + _data.criteria_current_iteration_data.criteria.cbegin(); + auto criteria_end = + _data.criteria_current_iteration_data.criteria.cend(); + auto max_criterion_it = std::max_element(criteria_begin, criteria_end); + if (max_criterion_it != criteria_end) { + _data.criteria_current_iteration_data.max_criterion = *max_criterion_it; auto max_criterion_index = - std::distance(criterions_begin, max_criterion_it); - _data.outer_loop_current_iteration_data.max_criterion_area = - criterion_computation_.getOuterLoopInputData() - .CriterionsData()[max_criterion_index] - .Pattern() - .GetBody(); + std::distance(criteria_begin, max_criterion_it); + _data.criteria_current_iteration_data.max_criterion_area = + criterion_computation_.getCriterionInputData() + .Criteria()[max_criterion_index] + .Pattern() + .GetBody(); } } -void BendersMpi::ComputeSubproblemsContributionToOuterLoopCriterion( - const SubProblemDataMap &subproblem_data_map) { +void BendersMpi::ComputeSubproblemsContributionToCriteria( + const SubProblemDataMap &subproblem_data_map) { const auto vars_size = criterion_computation_.getVarIndices().size(); - std::vector outer_loop_criterion_per_sub_problem_per_pattern( - vars_size, {}); - _data.outer_loop_current_iteration_data.outer_loop_criterion.resize(vars_size, - 0.); - std::vector outer_loop_patterns_values_per_sub_problem_per_pattern( - vars_size, {}); - _data.outer_loop_current_iteration_data.outer_loop_patterns_values.resize( - vars_size, 0.); + std::vector criteria_per_sub_problem_per_pattern( + vars_size, {}); + _data.criteria_current_iteration_data.criteria.resize(vars_size, + 0.); + std::vector patterns_values_per_sub_problem_per_pattern( + vars_size, {}); + _data.criteria_current_iteration_data.patterns_values.resize( + vars_size, 0.); for (const auto &[subproblem_name, subproblem_data] : subproblem_data_map) { - AddVectors(outer_loop_criterion_per_sub_problem_per_pattern, - subproblem_data.outer_loop_criterions); - AddVectors(outer_loop_patterns_values_per_sub_problem_per_pattern, - subproblem_data.outer_loop_patterns_values); + AddVectors(criteria_per_sub_problem_per_pattern, + subproblem_data.criteria); + AddVectors(patterns_values_per_sub_problem_per_pattern, + subproblem_data.patterns_values); } - Reduce(outer_loop_criterion_per_sub_problem_per_pattern, - _data.outer_loop_current_iteration_data.outer_loop_criterion, + Reduce(criteria_per_sub_problem_per_pattern, + _data.criteria_current_iteration_data.criteria, std::plus(), rank_0); - Reduce(outer_loop_patterns_values_per_sub_problem_per_pattern, - _data.outer_loop_current_iteration_data.outer_loop_patterns_values, + Reduce(patterns_values_per_sub_problem_per_pattern, + _data.criteria_current_iteration_data.patterns_values, std::plus(), rank_0); } diff --git a/src/cpp/benders/benders_mpi/BendersMpiOuterLoop.cpp b/src/cpp/benders/benders_mpi/BendersMpiOuterLoop.cpp index e532f45b2..95c07b24a 100644 --- a/src/cpp/benders/benders_mpi/BendersMpiOuterLoop.cpp +++ b/src/cpp/benders/benders_mpi/BendersMpiOuterLoop.cpp @@ -11,7 +11,7 @@ BendersMpiOuterLoop::BendersMpiOuterLoop( void BendersMpiOuterLoop::launch() { - ++_data.outer_loop_current_iteration_data.benders_num_run; + ++_data.criteria_current_iteration_data.benders_num_run; BendersMpi::launch(); } } // namespace Outerloop \ No newline at end of file diff --git a/src/cpp/benders/benders_mpi/OuterLoopBenders.cpp b/src/cpp/benders/benders_mpi/OuterLoopBenders.cpp index 53a30d5e3..f918e2903 100644 --- a/src/cpp/benders/benders_mpi/OuterLoopBenders.cpp +++ b/src/cpp/benders/benders_mpi/OuterLoopBenders.cpp @@ -115,7 +115,7 @@ void OuterLoopBenders::OuterLoopBilevelChecks() { const auto& x_cut = benders_->GetCurrentIterationData().x_cut; const auto& external_loop_lambda = benders_->GetCurrentIterationData() - .outer_loop_current_iteration_data.external_loop_lambda; + .criteria_current_iteration_data.lambda; if (outer_loop_biLevel_.Update_bilevel_data_if_feasible( x_cut, benders_->GetOuterLoopCriterionAtBestBenders() /*/!\ must be at best it*/ diff --git a/src/cpp/benders/benders_mpi/include/antares-xpansion/benders/benders_mpi/BendersMPI.h b/src/cpp/benders/benders_mpi/include/antares-xpansion/benders/benders_mpi/BendersMPI.h index 05da35767..b76183b8f 100644 --- a/src/cpp/benders/benders_mpi/include/antares-xpansion/benders/benders_mpi/BendersMPI.h +++ b/src/cpp/benders/benders_mpi/include/antares-xpansion/benders/benders_mpi/BendersMPI.h @@ -90,7 +90,7 @@ class BendersMpi : public BendersBase { virtual void GatherCuts(const SubProblemDataMap &subproblem_data_map, const Timer &walltime); void BroadCastVariablesIndices(); - virtual void ComputeSubproblemsContributionToOuterLoopCriterion( + virtual void ComputeSubproblemsContributionToCriteria( const SubProblemDataMap &subproblem_data_map); void SolveSubproblem( SubProblemDataMap &subproblem_data_map, diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index 828c99d82..17f00b5c8 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -125,8 +125,8 @@ void BendersMainFactory::SetupLoggerAndOutputWriter( bool BendersMainFactory::isCriterionListEmpty() const { return std::visit( - [](auto&& the_variant) { return the_variant.CriterionsData().empty(); }, - criterion_input_holder_); + [](auto &&the_variant) { return the_variant.Criteria().empty(); }, + criterion_input_holder_); } std::shared_ptr BendersMainFactory::BuildMathLogger( @@ -150,14 +150,14 @@ void BendersMainFactory::AddCriterionOutputs() { criterion_input_holder_); math_log_driver_->add_logger( output_root / LOLD_FILE, headers, - &OuterLoopCurrentIterationData::outer_loop_criterion); + &CriteriaCurrentIterationData::criteria); positive_unsupplied_file_ = std::visit( [](auto&& the_variant) { return the_variant.PatternsPrefix() + ".txt"; }, criterion_input_holder_); math_log_driver_->add_logger( output_root / positive_unsupplied_file_, headers, - &OuterLoopCurrentIterationData::outer_loop_patterns_values); + &CriteriaCurrentIterationData::patterns_values); } int BendersMainFactory::RunBenders() { @@ -271,7 +271,7 @@ int BendersMainFactory::RunExternalLoop() { std::shared_ptr cuts_manager = std::make_shared(); - Outerloop::OuterLoopBenders ext_loop(outer_loop_inputs.CriterionsData(), + Outerloop::OuterLoopBenders ext_loop(outer_loop_inputs.Criteria(), master_updater, cuts_manager, benders_, *pworld_); StartMessage(); diff --git a/tests/cpp/outer_loop/outer_loop_test.cpp b/tests/cpp/outer_loop/outer_loop_test.cpp index 71ebd4f34..b203a9350 100644 --- a/tests/cpp/outer_loop/outer_loop_test.cpp +++ b/tests/cpp/outer_loop/outer_loop_test.cpp @@ -124,7 +124,7 @@ TEST_P(MasterUpdateBaseTest, ConstraintIsAddedBendersMPI) { auto master_updater = std::make_shared( benders, 0.5, outer_loop_input_data.StoppingThreshold()); auto cut_manager = std::make_shared(); - Outerloop::OuterLoopBenders out_loop(outer_loop_input_data.CriterionsData(), + Outerloop::OuterLoopBenders out_loop(outer_loop_input_data.Criteria(), master_updater, cut_manager, benders, *pworld); out_loop.OuterLoopCheckFeasibility(); @@ -286,7 +286,7 @@ criterion_count_threshold: 1e-1 ASSERT_EQ(data.StoppingThreshold(), 1e-4); ASSERT_EQ(data.CriterionCountThreshold(), 1e-1); - auto patterns = data.CriterionsData(); + auto patterns = data.Criteria(); ASSERT_EQ(patterns.size(), 4); auto pattern1 = patterns[0]; ASSERT_EQ(pattern1.Criterion(), 185.0); @@ -380,24 +380,28 @@ TEST_F(OuterLoopBiLevelTest, BiLevelBestUbInitialization) { ASSERT_EQ(outerLoopBiLevel.BilevelBestub(), 1e20); } -TEST_F(OuterLoopBiLevelTest, Unfeasibility_With_High_Criterions) { +TEST_F(OuterLoopBiLevelTest, Unfeasibility_With_High_Criteria) { Outerloop::OuterLoopBiLevel outerLoopBiLevel(data); - const std::vector criterions = {data[0].Criterion() + 12, - data[1].Criterion() + 2024}; + const std::vector criteria = { + data[0].Criterion() + 12, + data[1].Criterion() + 2024 + }; const auto lambda = 1205; ASSERT_FALSE(outerLoopBiLevel.Update_bilevel_data_if_feasible( - {}, criterions, 0., 0., lambda)); + {}, criteria, 0., 0., lambda)); ASSERT_EQ(outerLoopBiLevel.LambdaMin(), lambda); } TEST_F(OuterLoopBiLevelTest, UnfeasibilityWithInfiniteOverAllCosts) { Outerloop::OuterLoopBiLevel outerLoopBiLevel(data); - const std::vector criterions = {data[0].Criterion() - 12, - data[1].Criterion() - 24}; + const std::vector criteria = { + data[0].Criterion() - 12, + data[1].Criterion() - 24 + }; const auto lambda = 1205; const auto overall_cost = 1e20; ASSERT_FALSE(outerLoopBiLevel.Update_bilevel_data_if_feasible( - {}, criterions, overall_cost, 0., lambda)); + {}, criteria, overall_cost, 0., lambda)); ASSERT_EQ(outerLoopBiLevel.LambdaMin(), lambda); } @@ -411,12 +415,14 @@ TEST_F(OuterLoopBiLevelTest, FeasibleScenario) { const auto cand_coeff = 2.0; outerLoopBiLevel.Init({cand_coeff}, max_x, {{cand, 0}}); ASSERT_EQ(outerLoopBiLevel.LambdaMax(), cand_coeff * max_x.at(cand)); - const std::vector criterions = {data[0].Criterion() - 12, - data[1].Criterion() - 24}; + const std::vector criteria = { + data[0].Criterion() - 12, + data[1].Criterion() - 24 + }; const Point x = {{cand, max_x.at(cand) / 2}}; ASSERT_TRUE(outerLoopBiLevel.Update_bilevel_data_if_feasible( - x, criterions, overall_cost, invest_cost_at_x, lambda)); + x, criteria, overall_cost, invest_cost_at_x, lambda)); ASSERT_EQ(outerLoopBiLevel.LambdaMin(), 0.); ASSERT_EQ(outerLoopBiLevel.LambdaMax(), invest_cost_at_x); ASSERT_EQ(outerLoopBiLevel.BilevelBestub(), overall_cost); @@ -436,12 +442,14 @@ TEST_F(OuterLoopBiLevelTest, // higher than lambda_max initial value const auto invest_cost_at_x = initial_lambda_max * 30; ASSERT_EQ(outerLoopBiLevel.LambdaMax(), initial_lambda_max); - const std::vector criterions = {data[0].Criterion() - 12, - data[1].Criterion() - 24}; + const std::vector criteria = { + data[0].Criterion() - 12, + data[1].Criterion() - 24 + }; const Point x = {{cand, max_x.at(cand) / 2}}; ASSERT_TRUE(outerLoopBiLevel.Update_bilevel_data_if_feasible( - x, criterions, overall_cost, invest_cost_at_x, lambda)); + x, criteria, overall_cost, invest_cost_at_x, lambda)); ASSERT_EQ(outerLoopBiLevel.LambdaMin(), 0.); ASSERT_EQ(outerLoopBiLevel.LambdaMax(), initial_lambda_max); ASSERT_EQ(outerLoopBiLevel.BilevelBestub(), overall_cost);