Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Nov 25, 2024
1 parent 099bc57 commit 694347b
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 170 deletions.
2 changes: 1 addition & 1 deletion docs/user-guide/get-started/adequacy-criterion.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_by_batch/BendersByBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
40 changes: 20 additions & 20 deletions src/cpp/benders/benders_core/BendersBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -995,33 +995,33 @@ 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<double> BendersBase::GetOuterLoopCriterionAtBestBenders() const {
return ((outer_loop_criterion_.empty())
return ((criteria_vector_for_each_iteration_.empty())
? std::vector<double>()
: outer_loop_criterion_[_data.best_it - 1]);
: criteria_vector_for_each_iteration_[_data.best_it - 1]);
}

void BendersBase::init_data(double external_loop_lambda,
double external_loop_lambda_min,
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;
}

Expand All @@ -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;
}

Expand Down
14 changes: 7 additions & 7 deletions src/cpp/benders/benders_core/BendersMathLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
52 changes: 26 additions & 26 deletions src/cpp/benders/benders_core/CriterionComputation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@

namespace Benders::Criterion {

void CriterionComputation::ComputeOuterLoopCriterion(
void CriterionComputation::ComputeCriterion(
double subproblem_weight, const std::vector<double> &sub_problem_solution,
std::vector<double> &outerLoopCriterions,
std::vector<double> &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<double> &criteria,
std::vector<double> &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<std::string> &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<std::vector<int>> &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
8 changes: 4 additions & 4 deletions src/cpp/benders/benders_core/CriterionInputDataReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void CriterionInputData::AddSingleData(const CriterionSingleInputData &data) {
}

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

Expand Down Expand Up @@ -127,17 +127,17 @@ class convert<CriterionSingleInputData> {
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;
err_msg
<< 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<double>());
Expand Down
8 changes: 4 additions & 4 deletions src/cpp/benders/benders_core/VariablesGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ using namespace Benders::Criterion;

VariablesGroup::VariablesGroup(
const std::vector<std::string>& all_variables,
const std::vector<CriterionSingleInputData>& outer_loop_single_input_data)
: all_variables_(all_variables), outer_loop_single_input_data_(outer_loop_single_input_data) {
const std::vector<CriterionSingleInputData>& criterion_single_input_data)
: all_variables_(all_variables), criterion_single_input_data_(criterion_single_input_data) {
Search();
}

Expand All @@ -24,11 +24,11 @@ std::vector<std::vector<int>> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> GetOuterLoopCriterionAtBestBenders() const;
virtual void init_data();
void init_data(double external_loop_lambda, double external_loop_lambda_min, double external_loop_lambda_max);
Expand Down Expand Up @@ -116,7 +120,7 @@ class BendersBase {
bool init_problems_ = true;
bool free_problems_ = true;

std::vector<std::vector<double>> outer_loop_criterion_;
std::vector<std::vector<double> > criteria_vector_for_each_iteration_;
bool is_bilevel_check_all_ = false;

virtual void Run() = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ struct MathLoggerExternalLoopSpecific : public MathLogger {
explicit MathLoggerExternalLoopSpecific(
const std::filesystem::path& file_path,
const std::vector<std::string>& headers,
T OuterLoopCurrentIterationData::*ptr)
T CriteriaCurrentIterationData::*ptr)
: MathLogger(file_path), ptr_(ptr) {
headers_.push_back("Outer loop");
headers_.push_back("Ite");
Expand All @@ -150,7 +150,7 @@ struct MathLoggerExternalLoopSpecific : public MathLogger {

private:
std::vector<std::string> headers_;
T OuterLoopCurrentIterationData::*ptr_;
T CriteriaCurrentIterationData::*ptr_;
};

class MathLoggerImplementation : public MathLoggerBehaviour {
Expand Down Expand Up @@ -193,7 +193,7 @@ class MathLoggerDriver : public ILoggerXpansion {
template <class T>
void add_logger(const std::filesystem::path& file_path,
const std::vector<std::string>& headers,
T OuterLoopCurrentIterationData::*t);
T CriteriaCurrentIterationData::*t);
void Print(const CurrentIterationData& data);
virtual void PrintIterationSeparatorBegin() override;
virtual void PrintIterationSeparatorEnd() override;
Expand All @@ -206,7 +206,7 @@ class MathLoggerDriver : public ILoggerXpansion {
template <class T>
void MathLoggerDriver::add_logger(const std::filesystem::path& file_path,
const std::vector<std::string>& headers,
T OuterLoopCurrentIterationData::*t) {
T CriteriaCurrentIterationData::*t) {
auto impl = std::make_shared<MathLoggerExternalLoopSpecific<T>>(file_path,
headers, t);
add_logger(std::make_shared<MathLoggerImplementation>(impl));
Expand All @@ -220,11 +220,11 @@ template <class T>
void MathLoggerExternalLoopSpecific<T>::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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
#include "Worker.h"
#include "common.h"

struct OuterLoopCurrentIterationData{
struct CriteriaCurrentIterationData {
int benders_num_run = 0;
std::vector<double> outer_loop_criterion = {};
std::vector<double> outer_loop_patterns_values = {};
std::vector<double> criteria = {};
std::vector<double> 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";
};
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 694347b

Please sign in to comment.