Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Feb 13, 2024
1 parent 5a31454 commit e057cd9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
12 changes: 11 additions & 1 deletion src/cpp/benders/benders_core/BendersBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,4 +910,14 @@ int BendersBase::MasterGetnrows() const { return _master->Getnrows(); }

WorkerMasterData BendersBase::BestIterationWorkerMaster() const {
return relevantIterationData_.best;
}
}

void BendersBase::ResetData() {
for (auto logger : std::vector<std::shared_ptr<ILoggerBenders>>{
mathLoggerDriver_, _logger}) {
logger->display_message("Reset data ...");
}
_data.best_ub = 1e+20;
_data.best_it = 0;
_data.stopping_criterion = StoppingCriterion::empty;
}
10 changes: 10 additions & 0 deletions src/cpp/benders/benders_core/OuterLoop.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "OuterLoop.h"

#include "LoggerUtils.h"
OuterLoop::OuterLoop(std::shared_ptr<IOuterLoopCriterion> criterion,
std::shared_ptr<IMasterUpdate> master_updater,
std::shared_ptr<ICutsManager> cuts_manager,
Expand Down Expand Up @@ -34,8 +35,17 @@ void OuterLoop::Run() {
// auto cuts = cuts_manager_->Load();
auto criterion =
criterion_->IsCriterionSatisfied(benders_->BestIterationWorkerMaster());
if (criterion == CRITERION::GREATER) {
std::ostringstream err_msg;
err_msg << PrefixMessage(LogUtils::LOGLEVEL::FATAL, "External Loop")
<< "Criterion cannot be satisfied for your study:\n"
<< criterion_->StateAsString();
throw CriterionCouldNotBeSatisfied(err_msg.str(), LOGLOCATION);
}
while (criterion != CRITERION::EQUAL) {
benders_->ResetData();
benders_->launch();

// de-comment for general case
// cuts_manager_->Save(benders_->CutsPerIteration());
// auto cuts = cuts_manager_->Load();
Expand Down
42 changes: 18 additions & 24 deletions src/cpp/benders/benders_core/OuterloopCriterion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,22 @@ OuterloopCriterionLOL::OuterloopCriterionLOL(double threshold, double epsilon)

CRITERION OuterloopCriterionLOL::IsCriterionSatisfied(
const WorkerMasterData& worker_master_data) {
double sum_loss = ProcessSum(worker_master_data);
CRITERION ret = (sum_loss <= threshold_ + epsilon_)
? (sum_loss >= threshold_ - epsilon_) ? CRITERION::EQUAL
: CRITERION::LESSER
ProcessSum(worker_master_data);
CRITERION ret = (sum_loss_ <= threshold_ + epsilon_)
? (sum_loss_ >= threshold_ - epsilon_) ? CRITERION::EQUAL
: CRITERION::LESSER
: CRITERION::GREATER;
if (sum_loss <= threshold_ + epsilon_) {
if (sum_loss >= threshold_ - epsilon_) {
return CRITERION::EQUAL;
}
return CRITERION::LESSER;
} else {
std::ostringstream err_msg;
err_msg << PrefixMessage(LogUtils::LOGLEVEL::FATAL, "External Loop")
<< "Criterion cannot be satisfied for your study:\n"
<< "Sum loss = " << sum_loss << "\n"
<< "threshold: " << threshold_ << "\n"
<< "epsilon: " << epsilon_ << "\n";
throw CriterionCouldNotBeSatisfied(err_msg.str(), LOGLOCATION);
}
// CRITERION ret = (sum_loss <= threshold_ - epsilon_) ? CRITERION::LESSER
// : (sum_loss >= threshold_ + epsilon_) ?

// CRITERION ret = (sum_loss_ <= threshold_ - epsilon_) ? CRITERION::LESSER
// : (sum_loss_ >= threshold_ + epsilon_) ?
// CRITERION::GREATER
// : CRITERION::EQUAL;
return ret;
}

double OuterloopCriterionLOL::ProcessSum(
void OuterloopCriterionLOL::ProcessSum(
const WorkerMasterData& worker_master_data) {
double sum_loss = 0;
sum_loss_ = 0;
for (const auto& [sub_problem_name, sub_problem_data] :
worker_master_data._cut_trace) {
for (auto i(0); i < sub_problem_data.variables.names.size(); ++i) {
Expand All @@ -45,10 +32,17 @@ double OuterloopCriterionLOL::ProcessSum(
if (std::regex_search(var_name, rgx_) &&
solution > UNSUPPLIED_ENERGY_MAX) {
// 1h of unsupplied energy
sum_loss += 1;
sum_loss_ += 1;
}
}
}
}

std::string OuterloopCriterionLOL::StateAsString() const {
std::ostringstream msg;
msg << "Sum loss = " << sum_loss_ << "\n"
<< "threshold: " << threshold_ << "\n"
<< "epsilon: " << epsilon_ << "\n";

return sum_loss;
return msg.str();
}
1 change: 1 addition & 0 deletions src/cpp/benders/benders_core/include/BendersBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class BendersBase {
_options.MAX_ITERATIONS = max_iteration;
}
BendersBaseOptions Options() const { return _options; }
void ResetData();

protected:
CurrentIterationData _data;
Expand Down
5 changes: 4 additions & 1 deletion src/cpp/benders/benders_core/include/OuterLoopCriterion.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ class IOuterLoopCriterion {
public:
virtual CRITERION IsCriterionSatisfied(
const WorkerMasterData& worker_master_data) = 0;
virtual std::string StateAsString() const = 0;
};

class OuterloopCriterionLOL : public IOuterLoopCriterion {
public:
explicit OuterloopCriterionLOL(double threshold, double epsilon);
CRITERION IsCriterionSatisfied(
const WorkerMasterData& milp_solution) override;
std::string StateAsString() const override;

private:
double ProcessSum(const WorkerMasterData& worker_master_data);
void ProcessSum(const WorkerMasterData& worker_master_data);
const std::string positive_unsupplied_vars_prefix_ =
"^PositiveUnsuppliedEnergy::";
const std::regex rgx_ = std::regex(positive_unsupplied_vars_prefix_);
const int UNSUPPLIED_ENERGY_MAX = 1;
double threshold_ = 1e6;
double epsilon_ = 1e-4;
double sum_loss_ = 0.0;
};
2 changes: 1 addition & 1 deletion src/cpp/benders/factories/BendersFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int RunExternalLoop_(char** argv, const std::filesystem::path& options_file,
SimulationOptions options(options_file);
auto benders = PrepareForExecution(benders_loggers, options, argv[0], env,
world);
double threshold = 5684;
double threshold = 29409;
double epsilon = 1e-2;
double lambda_min = 0;
double lambda = 16;
Expand Down

0 comments on commit e057cd9

Please sign in to comment.