Skip to content

Commit

Permalink
simplifying CriterionComputation object usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Nov 12, 2024
1 parent ee8880e commit 6d586cb
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 52 deletions.
16 changes: 6 additions & 10 deletions src/cpp/benders/benders_core/BendersBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ BendersBase::BendersBase(const BendersBaseOptions &options, Logger logger,
mathLoggerDriver_(std::move(mathLoggerDriver)) {
}

std::filesystem::path BendersBase::OuterloopOptionsFile() const {
return std::filesystem::path(_options.INPUTROOT) /
_options.EXTERNAL_LOOP_OPTIONS.OUTER_LOOP_OPTION_FILE;
}

/*!
* \brief Initialize set of data used in the loop
*/
Expand Down Expand Up @@ -417,7 +412,7 @@ void BendersBase::SetSubproblemsVariablesIndices() {
if (!subproblem_map.empty()) {
auto subproblem = subproblem_map.begin();

criterions_computation_->SearchVariables(
criterion_computation_.SearchVariables(
subproblem->second->_solver->get_col_names());
}
}
Expand Down Expand Up @@ -1050,8 +1045,9 @@ 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;

void BendersBase::setCriterionComputationInputs(
const Benders::Criterion::CriterionInputData &criterion_input_data) {
criterion_computation_ =
Benders::Criterion::CriterionComputation(criterion_input_data);
}
11 changes: 6 additions & 5 deletions src/cpp/benders/benders_core/CriterionInputDataReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,27 @@ void CriterionSingleInputData::ResetPattern(const std::string &prefix,
}

void CriterionInputData::AddSingleData(const CriterionSingleInputData &data) {
criterions_.push_back(data);
criterion_vector_.push_back(data);
}

const std::vector<CriterionSingleInputData> &
CriterionInputData::CriterionsData() const {
return criterions_;
return criterion_vector_;
}

std::vector<std::string> CriterionInputData::PatternBodies() const {
std::vector<std::string> ret;
for (const auto &data : criterions_) {
for (const auto &data : criterion_vector_) {
ret.push_back(data.Pattern().GetBody());
}
return ret;
}

std::string CriterionInputData::PatternsPrefix() const {
std::string ret("");
if (!criterions_.empty()) {
ret = StringManip::split(criterions_[0].Pattern().GetPrefix(), "::")[0];
if (!criterion_vector_.empty()) {
ret =
StringManip::split(criterion_vector_[0].Pattern().GetPrefix(), "::")[0];
}
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,12 @@ class BendersBase {
void UpdateOuterLoopSolution();

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);
void setCriterionComputationInputs(
const Benders::Criterion::CriterionInputData &criterion_input_data);

protected:
bool exception_raised_ = false;
Expand Down Expand Up @@ -223,9 +221,8 @@ 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_;

Benders::Criterion::CriterionComputation criterion_computation_;
/**
* for the nth variable name, Subproblems shares the same prefix , only the
suffix is different
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace Benders::Criterion {

class CriterionComputation {
public:
/**
* @brief default constructor
*/
explicit CriterionComputation() = default;

/**
* @brief Constructs a CriterionComputation object.
*
Expand Down Expand Up @@ -72,7 +77,7 @@ class CriterionComputation {
const CriterionInputData &getOuterLoopInputData() const;

private:
std::vector<std::vector<int>> var_indices_;
const CriterionInputData outer_loop_input_data_;
std::vector<std::vector<int>> var_indices_ = {};
CriterionInputData outer_loop_input_data_;
};
} // namespace Benders::Criterion
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CriterionInputData {

private:
double stopping_threshold_ = 1e-4;
std::vector<CriterionSingleInputData> criterions_;
std::vector<CriterionSingleInputData> criterion_vector_;
double count_threshold_ = 1;
};

Expand Down
8 changes: 4 additions & 4 deletions src/cpp/benders/benders_mpi/BendersMPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void BendersMpi::BroadCastVariablesIndices() {
if (_world.rank() == rank_0) {
SetSubproblemsVariablesIndices();
}
BroadCast(criterions_computation_->getVarIndices(), rank_0);
BroadCast(criterion_computation_.getVarIndices(), rank_0);
}

void BendersMpi::BuildMasterProblem() {
Expand Down Expand Up @@ -176,7 +176,7 @@ void BendersMpi::SolveSubproblem(

std::vector<double> solution;
worker->get_solution(solution);
criterions_computation_->ComputeOuterLoopCriterion(
criterion_computation_.ComputeOuterLoopCriterion(
SubproblemWeight(_data.nsubproblem, name), solution,
subproblem_data.outer_loop_criterions,
subproblem_data.outer_loop_patterns_values);
Expand All @@ -193,7 +193,7 @@ void BendersMpi::UpdateMaxCriterionArea() {
auto max_criterion_index =
std::distance(criterions_begin, max_criterion_it);
_data.outer_loop_current_iteration_data.max_criterion_area =
criterions_computation_->getOuterLoopInputData()
criterion_computation_.getOuterLoopInputData()
.CriterionsData()[max_criterion_index]
.Pattern()
.GetBody();
Expand All @@ -202,7 +202,7 @@ void BendersMpi::UpdateMaxCriterionArea() {

void BendersMpi::ComputeSubproblemsContributionToOuterLoopCriterion(
const SubProblemDataMap &subproblem_data_map) {
const auto vars_size = criterions_computation_->getVarIndices().size();
const auto vars_size = criterion_computation_.getVarIndices().size();
std::vector<double> outer_loop_criterion_per_sub_problem_per_pattern(
vars_size, {});
_data.outer_loop_current_iteration_data.outer_loop_criterion.resize(vars_size,
Expand Down
21 changes: 7 additions & 14 deletions src/cpp/benders/factories/BendersFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ pBendersBase BendersMainFactory::PrepareForExecution(bool external_loop) {

benders_loggers_.AddLogger(logger_);
benders_loggers_.AddLogger(math_log_driver);
auto outer_loop_input_data = ProcessCriterionInput();
criterion_computation_ =
std::make_shared<Benders::Criterion::CriterionComputation>(
outer_loop_input_data);
criterion_input_data_ = ProcessCriterionInput();

if (pworld_->rank() == 0 && !outer_loop_input_data.CriterionsData().empty()) {
if (pworld_->rank() == 0 && !criterion_input_data_.CriterionsData().empty()) {
AddCriterionOutput(math_log_driver);
}

Expand Down Expand Up @@ -105,7 +102,7 @@ pBendersBase BendersMainFactory::PrepareForExecution(bool external_loop) {
writer_->write_log_level(options_.LOG_LEVEL);
writer_->write_master_name(options_.MASTER_NAME);
writer_->write_solver_name(options_.SOLVER_NAME);
benders->setCriterionsComputation(criterion_computation_);
benders->setCriterionComputationInputs(criterion_input_data_);

return benders;
}
Expand All @@ -127,14 +124,12 @@ void BendersMainFactory::AddCriterionOutput(
std::shared_ptr<MathLoggerDriver> math_log_driver) {
const std::filesystem::path output_root(options_.OUTPUTROOT);

const auto& headers =
criterion_computation_->getOuterLoopInputData().PatternBodies();
const auto& headers = criterion_input_data_.PatternBodies();
math_log_driver->add_logger(
output_root / LOLD_FILE, headers,
&OuterLoopCurrentIterationData::outer_loop_criterion);

positive_unsupplied_file_ =
criterion_computation_->getOuterLoopInputData().PatternsPrefix() + ".txt";
positive_unsupplied_file_ = criterion_input_data_.PatternsPrefix() + ".txt";
math_log_driver->add_logger(
output_root / positive_unsupplied_file_, headers,
&OuterLoopCurrentIterationData::outer_loop_patterns_values);
Expand Down Expand Up @@ -234,15 +229,13 @@ int BendersMainFactory::RunExternalLoop() {
auto benders = PrepareForExecution(true);
double tau = 0.5;

const auto& criterion_input_data =
criterion_computation_->getOuterLoopInputData();
std::shared_ptr<Outerloop::IMasterUpdate> master_updater =
std::make_shared<Outerloop::MasterUpdateBase>(
benders, tau, criterion_input_data.StoppingThreshold());
benders, tau, criterion_input_data_.StoppingThreshold());
std::shared_ptr<Outerloop::ICutsManager> cuts_manager =
std::make_shared<Outerloop::CutsManagerRunTime>();

Outerloop::OuterLoopBenders ext_loop(criterion_input_data.CriterionsData(),
Outerloop::OuterLoopBenders ext_loop(criterion_input_data_.CriterionsData(),
master_updater, cuts_manager, benders,
*pworld_);
ext_loop.Run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class BendersMainFactory {
SOLVER solver_ = SOLVER::BENDERS;
SimulationOptions options_;
BendersLoggerBase benders_loggers_;
std::shared_ptr<Benders::Criterion::CriterionComputation>
criterion_computation_ = nullptr;
Benders::Criterion::CriterionInputData criterion_input_data_;
Logger logger_ = nullptr;
Writer writer_ = nullptr;
BENDERSMETHOD method_ = BENDERSMETHOD::BENDERS;
Expand Down
11 changes: 4 additions & 7 deletions tests/cpp/outer_loop/outer_loop_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,14 @@ TEST_P(MasterUpdateBaseTest, ConstraintIsAddedBendersMPI) {
auto outer_loop_input_data =
Benders::Criterion::CriterionInputFromYaml().Read(
std::filesystem::path(bendersoptions.INPUTROOT) / OUTER_OPTIONS_FILE);
auto criterion_computation =
std::make_shared<Benders::Criterion::CriterionComputation>(
outer_loop_input_data);
benders->setCriterionsComputation(criterion_computation);

benders->setCriterionComputationInputs(outer_loop_input_data);

auto master_updater = std::make_shared<MasterUpdateBase>(
benders, 0.5, outer_loop_input_data.StoppingThreshold());
auto cut_manager = std::make_shared<Outerloop::CutsManagerRunTime>();
Outerloop::OuterLoopBenders out_loop(
criterion_computation->getOuterLoopInputData().CriterionsData(),
master_updater, cut_manager, benders, *pworld);
Outerloop::OuterLoopBenders out_loop(outer_loop_input_data.CriterionsData(),
master_updater, cut_manager, benders, *pworld);
out_loop.OuterLoopCheckFeasibility();

auto num_constraints_master_before = benders->MasterGetnrows();
Expand Down

0 comments on commit 6d586cb

Please sign in to comment.