Skip to content

Commit

Permalink
move criterion computation (#829)
Browse files Browse the repository at this point in the history
close #813
  • Loading branch information
a-zakir authored Jul 2, 2024
1 parent 99d1ffa commit e9e1570
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/cpp/benders/benders_by_batch/BendersByBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ void BendersByBatch::GetSubproblemCut(
worker->solve(subproblem_data.lpstatus, Options().OUTPUTROOT,
Options().LAST_MASTER_MPS + MPS_SUFFIX, _writer);
// worker->get_solution(subproblem_data.solution);
// TODO not supported yet
// if (Options().EXTERNAL_LOOP_OPTIONS.DO_OUTER_LOOP) {
// std::vector<double> solution;
// worker->get_solution(solution);
// subproblem_data.outer_loop_criterions =
// ComputeOuterLoopCriterion(name, solution);
// }
worker->get_value(subproblem_data.subproblem_cost); // solution phi(x,s)
worker->get_subgradient(
subproblem_data.var_name_and_subgradient); // dual pi_s
Expand Down
15 changes: 8 additions & 7 deletions src/cpp/benders/benders_core/BendersBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,12 @@ void BendersBase::GetSubproblemCut(SubProblemDataMap &subproblem_data_map) {
worker->solve(subproblem_data.lpstatus, _options.OUTPUTROOT,
_options.LAST_MASTER_MPS + MPS_SUFFIX, _writer);
worker->get_value(subproblem_data.subproblem_cost);
worker->get_solution(subproblem_data.solution);
if (_options.EXTERNAL_LOOP_OPTIONS.DO_OUTER_LOOP) {
std::vector<double> solution;
worker->get_solution(solution);
subproblem_data.outer_loop_criterions =
ComputeOuterLoopCriterion(name, solution);
}
worker->get_subgradient(subproblem_data.var_name_and_subgradient);
worker->get_splex_num_of_ite_last(subproblem_data.simplex_iter);
subproblem_data.subproblem_timer = subproblem_timer.elapsed();
Expand Down Expand Up @@ -427,9 +432,7 @@ void BendersBase::compute_cut(const SubProblemDataMap &subproblem_data_map) {
subproblem_data.var_name_and_subgradient,
_data.x_cut, subproblem_data.subproblem_cost);
relevantIterationData_.last._cut_trace[subproblem_name] = subproblem_data;
// ComputeOuterLoopCriterion(subproblem_name, subproblem_data);
}
// outer_loop_criterion_.push_back(current_outer_loop_criterion_);
}

void compute_cut_val(const Point &var_name_subgradient, const Point &x_cut,
Expand Down Expand Up @@ -1022,8 +1025,7 @@ std::vector<double> BendersBase::GetOuterLoopCriterionAtBestBenders() const {

std::vector<double> BendersBase::ComputeOuterLoopCriterion(
const std::string &subproblem_name,
const PlainData::SubProblemData &sub_problem_data) {

const std::vector<double> &sub_problem_solution) {
auto outer_loop_input_size = var_indices_.size(); // num of patterns
std::vector<double> outer_loop_criterion_per_sub_problem(outer_loop_input_size,
{});
Expand All @@ -1037,8 +1039,7 @@ std::vector<double> BendersBase::ComputeOuterLoopCriterion(
++pattern_index) {
auto pattern_variables_indices = var_indices_[pattern_index];
for (auto variables_index : pattern_variables_indices) {

if (auto solution = sub_problem_data.solution[variables_index];
if (auto solution = sub_problem_solution[variables_index];
solution > criterion_count_threshold)
// 1h of no supplied energy
outer_loop_criterion_per_sub_problem[pattern_index] +=
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_core/include/BendersBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class BendersBase {
// outer loop criterion per pattern
std::vector<double> ComputeOuterLoopCriterion(
const std::string &subproblem_name,
const PlainData::SubProblemData &sub_problem_data);
const std::vector<double> &sub_problem_solution);

void UpdateOuterLoopMaxCriterionArea();
void UpdateOuterLoopSolution();
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/benders/benders_core/include/SubproblemCut.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PlainData {
struct SubProblemData {
double subproblem_cost;
Point var_name_and_subgradient;
std::vector<double> solution;
std::vector<double> outer_loop_criterions;
double single_subpb_costs_under_approx;
double subproblem_timer;
int simplex_iter;
Expand All @@ -19,7 +19,7 @@ struct SubProblemData {
void serialize(Archive &ar, const unsigned int version) {
ar & subproblem_cost;
ar & var_name_and_subgradient;
ar & solution;
ar & outer_loop_criterions;
ar & single_subpb_costs_under_approx;
ar & subproblem_timer;
ar & simplex_iter;
Expand Down
5 changes: 3 additions & 2 deletions src/cpp/benders/benders_mpi/BendersMPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,16 @@ BendersMpi::ComputeSubproblemsContributionToOuterLoopCriterion(
var_indices_.size(), {});
std::vector<double> outer_loop_criterion_sub_problems_map_result(
var_indices_.size(), {});

for (const auto &[subproblem_name, subproblem_data] : subproblem_data_map) {
AddVectors<double>(
outer_loop_criterion_per_sub_problem_per_pattern,
ComputeOuterLoopCriterion(subproblem_name, subproblem_data));
subproblem_data.outer_loop_criterions);
}

Reduce(outer_loop_criterion_per_sub_problem_per_pattern,
outer_loop_criterion_sub_problems_map_result, std::plus<double>(),
rank_0);
// outer_loop_criterion_sub_problems_map_result/=nbyears;

return outer_loop_criterion_sub_problems_map_result;
}
Expand Down

0 comments on commit e9e1570

Please sign in to comment.