Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Nov 12, 2024
1 parent 6d586cb commit fe87565
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
31 changes: 18 additions & 13 deletions src/cpp/benders/benders_core/CriterionInputDataReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,15 @@ std::string CriterionInputData::PatternsPrefix() const {
return ret;
}

void CriterionInputData::SetStoppingThreshold(double stopping_threshold) {
stopping_threshold_ = stopping_threshold;
}

double CriterionInputData::StoppingThreshold() const {
return stopping_threshold_;
}
void CriterionInputData::SetCriterionCountThreshold(double count_threshold) {
count_threshold_ = count_threshold;
}
double CriterionInputData::CriterionCountThreshold() const {
return count_threshold_;
}

CriterionInputData CriterionInputFromYaml::Read(
OuterLoopCriterionInputData CriterionInputFromYaml::Read(
const std::filesystem::path &input_file) {
YAML::Node yaml_content;
try {
Expand All @@ -107,7 +101,16 @@ CriterionInputData CriterionInputFromYaml::Read(
LOGLOCATION);
}

return yaml_content.as<CriterionInputData>();
return yaml_content.as<OuterLoopCriterionInputData>();
}

double OuterLoopCriterionInputData::StoppingThreshold() const {
return stopping_threshold_;
}

void OuterLoopCriterionInputData::setStoppingThreshold(
double stoppingThreshold) {
stopping_threshold_ = stoppingThreshold;
}

namespace YAML {
Expand Down Expand Up @@ -144,8 +147,8 @@ struct convert<CriterionSingleInputData> {
}
};
template <>
struct convert<CriterionInputData> {
static Node encode(const CriterionInputData &rhs) { return {}; }
struct convert<OuterLoopCriterionInputData> {
static Node encode(const OuterLoopCriterionInputData &rhs) { return {}; }

static void DecodePatterns(const Node &patterns, CriterionInputData &rhs) {
if (!patterns.IsSequence()) {
Expand All @@ -162,8 +165,10 @@ struct convert<CriterionInputData> {
}
}

static bool decode(const Node &node, CriterionInputData &rhs) {
rhs.SetStoppingThreshold(node["stopping_threshold"].as<double>(1e-4));
static bool decode(const Node &node, OuterLoopCriterionInputData &rhs) {
rhs.setStoppingThreshold(node["stopping_threshold"].as<double>(1e-4));
Benders::Criterion::CriterionInputData sub_rhs;

rhs.SetCriterionCountThreshold(
node["criterion_count_threshold"].as<double>(1));

Expand All @@ -180,4 +185,4 @@ struct convert<CriterionInputData> {
return true;
}
};
} // namespace YAML
} // namespace YAML
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,39 @@ class CriterionInputData {
[[nodiscard]] std::vector<std::string> PatternBodies() const;
[[nodiscard]] std::string PatternsPrefix() const;

void SetStoppingThreshold(double stopping_threshold);
[[nodiscard]] double StoppingThreshold() const;
void SetCriterionCountThreshold(double count_threshold);
[[nodiscard]] double CriterionCountThreshold() const;
void AddSingleData(const CriterionSingleInputData &data);

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

/// @brief this class contains all data read from user input file
class OuterLoopCriterionInputData : public CriterionInputData {
public:
OuterLoopCriterionInputData() = default;
[[nodiscard]] double StoppingThreshold() const;
void setStoppingThreshold(double stoppingThreshold);

private:
double stopping_threshold_ = 1e-4;
};

/// @brief Abstract /***
class ICriterionInputDataReader {
public:
virtual CriterionInputData Read(const std::filesystem::path &input_file) = 0;
virtual OuterLoopCriterionInputData Read(
const std::filesystem::path &input_file) = 0;
virtual ~ICriterionInputDataReader() = default;
};

class CriterionInputFromYaml : public ICriterionInputDataReader {
public:
CriterionInputFromYaml() = default;
CriterionInputData Read(const std::filesystem::path &input_file) override;
OuterLoopCriterionInputData Read(
const std::filesystem::path &input_file) override;

private:
CriterionInputData criterion_input_data_;
Expand Down
31 changes: 23 additions & 8 deletions src/cpp/benders/factories/BendersFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ pBendersBase BendersMainFactory::PrepareForExecution(bool external_loop) {

benders_loggers_.AddLogger(logger_);
benders_loggers_.AddLogger(math_log_driver);
criterion_input_data_ = ProcessCriterionInput();
criterion_input_holder_ = ProcessCriterionInput();

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

Expand Down Expand Up @@ -102,11 +102,19 @@ 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->setCriterionComputationInputs(criterion_input_data_);
benders->setCriterionComputationInputs(
std::get<Benders::Criterion::CriterionInputData>(
criterion_input_holder_));

return benders;
}

bool BendersMainFactory::isCriterionListEmpty() const {
return std::visit(
[](auto&& the_variant) { return the_variant.CriterionsData().empty(); },
criterion_input_holder_);
}

std::shared_ptr<MathLoggerDriver> BendersMainFactory::BuildMathLogger(
bool benders_log_console) const {
const std::filesystem::path output_root(options_.OUTPUTROOT);
Expand All @@ -124,12 +132,15 @@ void BendersMainFactory::AddCriterionOutput(
std::shared_ptr<MathLoggerDriver> math_log_driver) {
const std::filesystem::path output_root(options_.OUTPUTROOT);

const auto& headers = criterion_input_data_.PatternBodies();
const auto& criterion_input_data =
std::get<Benders::Criterion::CriterionInputData>(criterion_input_holder_);

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_input_data_.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 @@ -173,7 +184,8 @@ void BendersMainFactory::EndMessage(const double execution_time) {
context_);
}

Benders::Criterion::CriterionInputData
std::variant<Benders::Criterion::CriterionInputData,
Benders::Criterion::OuterLoopCriterionInputData>
BendersMainFactory::ProcessCriterionInput() {
const auto fpath = std::filesystem::path(options_.INPUTROOT) /
options_.OUTER_LOOP_OPTION_FILE;
Expand Down Expand Up @@ -229,13 +241,16 @@ int BendersMainFactory::RunExternalLoop() {
auto benders = PrepareForExecution(true);
double tau = 0.5;

const auto& outer_loop_inputs =
std::get<Benders::Criterion::OuterLoopCriterionInputData>(
criterion_input_holder_);
std::shared_ptr<Outerloop::IMasterUpdate> master_updater =
std::make_shared<Outerloop::MasterUpdateBase>(
benders, tau, criterion_input_data_.StoppingThreshold());
benders, tau, outer_loop_inputs.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(outer_loop_inputs.CriterionsData(),
master_updater, cuts_manager, benders,
*pworld_);
ext_loop.Run();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#ifndef ANTARES_XPANSION_SRC_CPP_BENDERS_FACTORIES_INCLUDE_BENDERSFACTORY_H
#define ANTARES_XPANSION_SRC_CPP_BENDERS_FACTORIES_INCLUDE_BENDERSFACTORY_H
#include <variant>

#include "antares-xpansion/benders/benders_core/CriterionComputation.h"
#include "antares-xpansion/benders/benders_core/common.h"
#include "antares-xpansion/benders/benders_mpi/BendersMPI.h"
Expand All @@ -17,7 +19,9 @@ class BendersMainFactory {
SOLVER solver_ = SOLVER::BENDERS;
SimulationOptions options_;
BendersLoggerBase benders_loggers_;
Benders::Criterion::CriterionInputData criterion_input_data_;
std::variant<Benders::Criterion::CriterionInputData,
Benders::Criterion::OuterLoopCriterionInputData>
criterion_input_holder_;
Logger logger_ = nullptr;
Writer writer_ = nullptr;
BENDERSMETHOD method_ = BENDERSMETHOD::BENDERS;
Expand All @@ -30,7 +34,9 @@ class BendersMainFactory {
[[nodiscard]] std::shared_ptr<MathLoggerDriver> BuildMathLogger(
bool benders_log_console) const;
pBendersBase PrepareForExecution(bool external_loop);
[[nodiscard]] Benders::Criterion::CriterionInputData ProcessCriterionInput();
[[nodiscard]] std::variant<Benders::Criterion::CriterionInputData,
Benders::Criterion::OuterLoopCriterionInputData>
ProcessCriterionInput();

Benders::Criterion::CriterionInputData BuildPatternsUsingAreaFile();
std::set<std::string> ReadAreaFile();
Expand All @@ -49,5 +55,6 @@ class BendersMainFactory {
const SOLVER& solver);
int Run();
std::filesystem::path LogReportsName() const;
bool isCriterionListEmpty() const;
};
#endif // ANTARES_XPANSION_SRC_CPP_BENDERS_FACTORIES_INCLUDE_BENDERSFACTORY_H

0 comments on commit fe87565

Please sign in to comment.