Skip to content

Commit

Permalink
ANT-2223 (#941)
Browse files Browse the repository at this point in the history
##
- [x] search unsp variables for all areas
- [x] compute LOLD 
- [ ] ~need more details on candidate capacity / area~
  • Loading branch information
a-zakir authored Oct 23, 2024
1 parent 68412f8 commit a977075
Show file tree
Hide file tree
Showing 30 changed files with 570 additions and 377 deletions.
4 changes: 1 addition & 3 deletions src/cpp/benders/benders_by_batch/BendersByBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ void BendersByBatch::InitializeProblems() {
}
}

// if (Rank() == rank_0) {
// SetSubproblemsVariablesIndex();
// }
BroadCastVariablesIndices();
init_problems_ = false;
}
void BendersByBatch::BroadcastSingleSubpbCostsUnderApprox() {
Expand Down
18 changes: 18 additions & 0 deletions src/cpp/benders/benders_core/BendersBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void BendersBase::CloseCsvFile() {
_csv_file.close();
}
}

void BendersBase::PrintCurrentIterationCsv() {
if (relevantIterationData_.last._valid) {
auto ite = _data.it - 1;
Expand Down Expand Up @@ -393,6 +394,7 @@ void BendersBase::GetSubproblemCut(SubProblemDataMap &subproblem_data_map) {
},
shouldParallelize());
}

void BendersBase::SolveSubproblem(
SubProblemDataMap &subproblem_data_map,
PlainData::SubProblemData &subproblem_data, const std::string &name,
Expand All @@ -408,6 +410,17 @@ void BendersBase::SolveSubproblem(
subproblem_data.subproblem_timer = subproblem_timer.elapsed();
}

// Search for variables in sub problems that satisfy patterns
// var_indices is a vector(for each patterns p) of vector (var indices related
// to p)
void BendersBase::SetSubproblemsVariablesIndices() {
if (!subproblem_map.empty()) {
auto subproblem = subproblem_map.begin();

criterions_computation_->SearchVariables(
subproblem->second->_solver->get_col_names());
}
}
/*!
* \brief Add cut to Master Problem and store the cut in a set
*
Expand Down Expand Up @@ -1037,3 +1050,8 @@ void BendersBase::SetBilevelBestub(double bilevel_best_ub) {
_data.outer_loop_current_iteration_data.outer_loop_bilevel_best_ub =
bilevel_best_ub;
}
void BendersBase::setCriterionsComputation(
std::shared_ptr<Benders::Criterion::CriterionComputation>
criterionsComputation) {
criterions_computation_ = criterionsComputation;
}
8 changes: 4 additions & 4 deletions src/cpp/benders/benders_core/BendersMathLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,15 @@ MathLoggerImplementation::MathLoggerImplementation(
implementation_ =
std::make_shared<MathLoggerBase>(file_path, width, type);
break;
case BENDERSMETHOD::BENDERS_EXTERNAL_LOOP:
case BENDERSMETHOD::BENDERS_OUTERLOOP:
implementation_ =
std::make_shared<MathLoggerBaseExternalLoop>(file_path, width, type);
break;
case BENDERSMETHOD::BENDERS_BY_BATCH:
implementation_ =
std::make_shared<MathLoggerBendersByBatch>(file_path, width, type);
break;
case BENDERSMETHOD::BENDERS_BY_BATCH_EXTERNAL_LOOP:
case BENDERSMETHOD::BENDERS_BY_BATCH_OUTERLOOP:
implementation_ = std::make_shared<MathLoggerBendersByBatchExternalLoop>(
file_path, width, type);
break;
Expand All @@ -305,14 +305,14 @@ MathLoggerImplementation::MathLoggerImplementation(const BENDERSMETHOD& method,
case BENDERSMETHOD::BENDERS:
implementation_ = std::make_shared<MathLoggerBase>(width, type);
break;
case BENDERSMETHOD::BENDERS_EXTERNAL_LOOP:
case BENDERSMETHOD::BENDERS_OUTERLOOP:
implementation_ =
std::make_shared<MathLoggerBaseExternalLoop>(width, type);
break;
case BENDERSMETHOD::BENDERS_BY_BATCH:
implementation_ = std::make_shared<MathLoggerBendersByBatch>(width, type);
break;
case BENDERSMETHOD::BENDERS_BY_BATCH_EXTERNAL_LOOP:
case BENDERSMETHOD::BENDERS_BY_BATCH_OUTERLOOP:
implementation_ =
std::make_shared<MathLoggerBendersByBatchExternalLoop>(width, type);
break;
Expand Down
6 changes: 6 additions & 0 deletions src/cpp/benders/benders_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ target_sources(benders_core PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Worker.cpp
${CMAKE_CURRENT_SOURCE_DIR}/WorkerMaster.cpp
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp
${CMAKE_CURRENT_SOURCE_DIR}/CriterionComputation.cpp
${CMAKE_CURRENT_SOURCE_DIR}/OuterLoopInputDataReader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/VariablesGroup.cpp
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/BendersBase.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/BendersMathLogger.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/BendersStructsDatas.h
Expand All @@ -37,6 +40,9 @@ target_sources(benders_core PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/Worker.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/WorkerMaster.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/common.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/CriterionComputation.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/OuterLoopInputDataReader.h
${CMAKE_CURRENT_SOURCE_DIR}/include/antares-xpansion/benders/benders_core/VariablesGroup.h
)

add_library(antaresXpansion::benders_core ALIAS benders_core)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "antares-xpansion/benders/outer_loop/CriterionComputation.h"
#include "antares-xpansion/benders/benders_core/CriterionComputation.h"

namespace Outerloop {
namespace Benders::Criterion {

void CriterionComputation::ComputeOuterLoopCriterion(
double subproblem_weight, const std::vector<double> &sub_problem_solution,
Expand All @@ -26,11 +26,9 @@ void CriterionComputation::ComputeOuterLoopCriterion(
}
}



void CriterionComputation::SearchVariables(
const std::vector<std::string> &variables) {
Outerloop::VariablesGroup variablesGroup(
Benders::Criterion::VariablesGroup variablesGroup(
variables, outer_loop_input_data_.OuterLoopData());
var_indices_ = variablesGroup.Indices();
}
Expand All @@ -46,4 +44,4 @@ CriterionComputation::CriterionComputation(
const OuterLoopInputData &outer_loop_input_data)
: outer_loop_input_data_(outer_loop_input_data) {}

} // namespace Outerloop
} // namespace Benders::Criterion
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "antares-xpansion/benders/outer_loop/OuterLoopInputDataReader.h"
#include "include/antares-xpansion/benders/benders_core/OuterLoopInputDataReader.h"

#include <utility>

#include "antares-xpansion/xpansion_interfaces/StringManip.h"

using namespace Outerloop;
using namespace Benders::Criterion;

/**
* prefix could be := PositiveUnsuppliedEnergy:: or something else necessarily
Expand Down Expand Up @@ -144,7 +144,7 @@ struct convert<OuterLoopSingleInputData> {
}

rhs.SetCriterion(criterion.as<double>());
rhs.ResetPattern("PositiveUnsuppliedEnergy::", body.as<std::string>());
rhs.ResetPattern(PositiveUnsuppliedEnergy, body.as<std::string>());
return true;
}
};
Expand Down
5 changes: 2 additions & 3 deletions src/cpp/benders/benders_core/SubproblemWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
SubproblemWorker::SubproblemWorker(
VariableMap const &variable_map, const std::filesystem::path &path_to_mps,
double const &slave_weight, const std::string &solver_name,
const int log_level, SolverLogManager&solver_log_manager,
Logger logger)
: Worker(logger) {
const int log_level, SolverLogManager &solver_log_manager, Logger logger)
: Worker(std::move(logger)) {
init(variable_map, path_to_mps, solver_name, log_level, solver_log_manager);

int mps_ncols(_solver->get_ncols());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
#include "antares-xpansion/benders/outer_loop/VariablesGroup.h"
using namespace Outerloop;
#include "include/antares-xpansion/benders/benders_core/VariablesGroup.h"

#include <regex>

using namespace Benders::Criterion;

/**
* @file VariablesGroup.cpp
* @brief Implementation of the VariablesGroup class.
*
* This file contains the implementation of the VariablesGroup class,
* which is responsible for grouping variables based on provided input patterns.
*/

VariablesGroup::VariablesGroup(
const std::vector<std::string>& all_variables,
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_core/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ CouplingMap build_input(const std::filesystem::path &structure_path) {
CouplingMap coupling_map;
std::ifstream summary(structure_path, std::ios::in);
if (!summary) {
std::cout << "Cannot open file summary " << structure_path << std::endl;
std::cout << "Cannot open structure file " << structure_path << std::endl;
return coupling_map;
}
std::string line;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

#include "BendersMathLogger.h"
#include "BendersStructsDatas.h"
#include "antares-xpansion/xpansion_interfaces/ILogger.h"
#include "antares-xpansion/xpansion_interfaces/OutputWriter.h"
#include "CriterionComputation.h"
#include "SimulationOptions.h"
#include "SubproblemCut.h"
#include "SubproblemWorker.h"
#include "antares-xpansion/helpers/Timer.h"
#include "Worker.h"
#include "WorkerMaster.h"
#include "antares-xpansion/helpers/Timer.h"
#include "antares-xpansion/xpansion_interfaces/ILogger.h"
#include "antares-xpansion/xpansion_interfaces/OutputWriter.h"
#include "common.h"
/**
* std execution policies don't share a base type so we can't just select
Expand Down Expand Up @@ -93,15 +94,18 @@ class BendersBase {
void SetBilevelBestub(double bilevel_best_ub);
void UpdateOuterLoopSolution();

protected:
bool exception_raised_ = false;

public:
bool isExceptionRaised() const;
[[nodiscard]] std::filesystem::path OuterloopOptionsFile() const;
void UpdateOverallCosts();
Logger _logger;
Writer _writer;
std::shared_ptr<MathLoggerDriver> mathLoggerDriver_;
void setCriterionsComputation(
std::shared_ptr<Benders::Criterion::CriterionComputation>
criterionsComputation);

protected:
bool exception_raised_ = false;
CurrentIterationData _data;
WorkerMasterDataVect workerMasterDataVect_;
// BendersCuts best_iteration_cuts_;
Expand Down Expand Up @@ -152,14 +156,6 @@ class BendersBase {
void AddSubproblem(const std::pair<std::string, VariableMap> &kvp);
[[nodiscard]] virtual WorkerMasterPtr get_master() const;
void MatchProblemToId();
/**
* for the nth variable name, Subproblems shares the same prefix , only the
suffix is different
* ex variable at index = 0 is named in:
* subproblems-1-1 --> NTCDirect::link<area1$$area2>::hour<0>
* subproblems-3-5 --> NTCDirect::link<area1$$area2>::hour<672>
*/
void AddSubproblemName(const std::string &name);
[[nodiscard]] std::string get_master_name() const;
[[nodiscard]] std::string get_solver_name() const;
Expand Down Expand Up @@ -227,6 +223,22 @@ class BendersBase {
PlainData::SubProblemData &subproblem_data,
const std::string &name,
const std::shared_ptr<SubproblemWorker> &worker);
// TODO to be rethink
std::shared_ptr<Benders::Criterion::CriterionComputation>
criterions_computation_;
/**
* for the nth variable name, Subproblems shares the same prefix , only the
suffix is different
* ex variable at index = 0 is named in:
* subproblems-1-1 --> NTCDirect::link<area1$$area2>::hour<0>
* subproblems-3-5 -->
NTCDirect::link<area1$$area2>::hour<672>
*/
// Search for variables in sub problems that satisfy patterns
// var_indices is a vector(for each patterns p) of vector (var indices related
// to p)
void SetSubproblemsVariablesIndices();

private:
void print_master_and_cut(std::ostream &file, int ite,
Expand All @@ -246,8 +258,6 @@ class BendersBase {
Output::Iteration iteration(const WorkerMasterData &masterDataPtr_l) const;
LogData FinalLogData() const;
void FillWorkerMasterData(WorkerMasterData &workerMasterData);

private:
bool master_is_empty_ = true;
BendersBaseOptions _options;
unsigned int _totalNbProblems = 0;
Expand All @@ -263,9 +273,5 @@ class BendersBase {
Timer benders_timer;
Output::SolutionData outer_loop_solution_data_;

public:
Logger _logger;
Writer _writer;
std::shared_ptr<MathLoggerDriver> mathLoggerDriver_;
};
using pBendersBase = std::shared_ptr<BendersBase>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once

#include "OuterLoopInputDataReader.h"
#include "VariablesGroup.h"
namespace Benders::Criterion {

class CriterionComputation {
public:
/**
* @brief Constructs a CriterionComputation object.
*
* This constructor initializes the CriterionComputation instance with the
* provided outer loop input data.
*
* @param outer_loop_input_data The input data to be used for criterion
* computation.
*/
explicit CriterionComputation(
const OuterLoopInputData &outer_loop_input_data);

/**
* @brief Searches for relevant variables based on the provided variable
* names.
*
* This method initializes a VariablesGroup with the provided variable names
* and retrieves the indices of these variables for later computation.
*
* @param variables A vector of strings representing the variable names to
* search for.
*/
void SearchVariables(const std::vector<std::string> &variables);

/**
* @brief Computes the outer loop criterion based on subproblem solutions.
*
* This method calculates the outer loop criteria and pattern values
* based on the provided subproblem weight and solution. It updates the
* outerLoopCriterions and outerLoopPatternsValues 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
* criteria will be stored.
* @param outerLoopPatternsValues A reference to a vector where the computed
* pattern values will be stored.
*/
void ComputeOuterLoopCriterion(
double subproblem_weight, const std::vector<double> &sub_problem_solution,
std::vector<double> &outerLoopCriterions,
std::vector<double> &outerLoopPatternsValues);

/**
* @brief Retrieves the variable indices.
*
* This method returns a reference to the vector containing the indices of
* the variables associated with this CriterionComputation instance.
*
* @return A reference to the vector of variable indices.
*/
std::vector<std::vector<int>> &getVarIndices();

/**
* @brief Retrieves the outer loop input data.
*
* This method returns a constant reference to the outer loop input data
* associated with this CriterionComputation instance.
*
* @return A constant reference to the OuterLoopInputData object.
*/
const OuterLoopInputData &getOuterLoopInputData() const;

private:
std::vector<std::vector<int>> var_indices_;
const OuterLoopInputData outer_loop_input_data_;
};
} // namespace Benders::Criterion
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "antares-xpansion/helpers/LoggerUtils.h"
#include "yaml-cpp/yaml.h"

namespace Outerloop {
namespace Benders::Criterion {
static constexpr const char *const PositiveUnsuppliedEnergy =
"PositiveUnsuppliedEnergy::";

class OuterLoopInputFileError
: public LogUtils::XpansionError<std::runtime_error> {
Expand Down Expand Up @@ -113,4 +115,4 @@ class OuterLoopInputFromYaml : public IOuterLoopInputDataReader {
OuterLoopInputData outerLoopInputData_;
};

} // namespace Outerloop
} // namespace Benders::Criterion
Loading

0 comments on commit a977075

Please sign in to comment.