Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Feb 26, 2024
1 parent 2cf2edc commit 4ea538a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_core/SimulationOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ BendersBaseOptions SimulationOptions::get_benders_options() const {
}

ExternalLoopOptions SimulationOptions::GetExternalLoopOptions() const {
return {EXT_LOOP_LOSS_OF_LOAD_THRESOLD, EXT_LOOP_EPSILON,
return {EXT_LOOP_LOSS_OF_LOAD_THRESHOLD, EXT_LOOP_EPSILON,
EXT_LOOP_MAX_UNSUP_ENERGY};
}
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_core/include/SimulationOptions.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ BENDERS_OPTIONS_MACRO(LAST_MASTER_BASIS, std::string, "master_last_basis",
BENDERS_OPTIONS_MACRO(BATCH_SIZE, size_t, 0, asUInt())

// EXTERNAL Loop Loss of Load thresold
BENDERS_OPTIONS_MACRO(EXT_LOOP_LOSS_OF_LOAD_THRESOLD, double, 1.0, asDouble())
BENDERS_OPTIONS_MACRO(EXT_LOOP_LOSS_OF_LOAD_THRESHOLD, double, 1.0, asDouble())

// EXTERNAL Loop epsilon
BENDERS_OPTIONS_MACRO(EXT_LOOP_EPSILON, double, 1e-1, asDouble())
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_core/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ struct BendersBaseOptions : public BaseOptions {
};

struct ExternalLoopOptions {
double EXT_LOOP_LOSS_OF_LOAD_THRESOLD = 1.0;
double EXT_LOOP_LOSS_OF_LOAD_THRESHOLD = 1.0;
double EXT_LOOP_EPSILON = 1e-1;
double EXT_LOOP_MAX_UNSUP_ENERGY = 1e-1;
};
Expand Down
6 changes: 3 additions & 3 deletions src/cpp/benders/external_loop/OuterloopCriterion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ CRITERION OuterloopCriterionLossOfLoad::IsCriterionSatisfied(
ProcessSum(worker_master_data);

if (sum_loss_ <=
options_.EXT_LOOP_LOSS_OF_LOAD_THRESOLD + options_.EXT_LOOP_EPSILON) {
options_.EXT_LOOP_LOSS_OF_LOAD_THRESHOLD + options_.EXT_LOOP_EPSILON) {
if (sum_loss_ >=
options_.EXT_LOOP_LOSS_OF_LOAD_THRESOLD - options_.EXT_LOOP_EPSILON) {
options_.EXT_LOOP_LOSS_OF_LOAD_THRESHOLD - options_.EXT_LOOP_EPSILON) {
return CRITERION::IS_MET;
}
return CRITERION::LOW;
Expand Down Expand Up @@ -43,7 +43,7 @@ void OuterloopCriterionLossOfLoad::ProcessSum(
std::string OuterloopCriterionLossOfLoad::StateAsString() const {
std::ostringstream msg;
msg << "Sum loss = " << sum_loss_ << "\n"
<< "threshold: " << options_.EXT_LOOP_LOSS_OF_LOAD_THRESOLD << "\n"
<< "threshold: " << options_.EXT_LOOP_LOSS_OF_LOAD_THRESHOLD << "\n"
<< "epsilon: " << options_.EXT_LOOP_EPSILON << "\n";

return msg.str();
Expand Down
1 change: 1 addition & 0 deletions tests/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ add_subdirectory(restart_benders)
add_subdirectory(zip_mps)
add_subdirectory(benders)
add_subdirectory(full_run)
add_subdirectory(ext_loop)
20 changes: 20 additions & 0 deletions tests/cpp/ext_loop/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
add_executable(ext_loop_test
ext_loop_test.cpp)

target_link_libraries(ext_loop_test
PRIVATE
external_loop
benders_by_batch_core
benders_core
output_core
logger_lib
GTest::Main
tests_utils)

# target_include_directories(ext_loop_test
# PRIVATE
# ${CMAKE_CURRENT_SOURCE_DIR}/../TestDoubles/
# )

add_test(NAME unit_ext_loop COMMAND ext_loop_test WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
set_property(TEST unit_ext_loop PROPERTY LABELS unit)
50 changes: 50 additions & 0 deletions tests/cpp/ext_loop/ext_loop_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

#include "OuterLoopCriterion.h"
#include "gtest/gtest.h"

class OuterLoopCriterionTest : public ::testing::Test {};

TEST_F(OuterLoopCriterionTest, IsCriterionHigh) {
double threshold = 1.4;
double epsilon = 1e-1;
double max_unsup_energy = 0.1;
const ExternalLoopOptions options = {threshold, epsilon, max_unsup_energy};
PlainData::Variables variables = {
{"PositiveUnsuppliedEnergy::1", "PositiveUnsuppliedEnergy::2", "var3"},
{0.2, 0.3, 68}};

PlainData::SubProblemData subProblemData;
subProblemData.variables = variables;
SubProblemDataMap cut_trace = {
std::make_pair(std::string("P1"), subProblemData)};

WorkerMasterData worker_master_data;
worker_master_data._cut_trace = cut_trace;

OuterloopCriterionLossOfLoad criterion(options);

EXPECT_EQ(criterion.IsCriterionSatisfied(worker_master_data),
CRITERION::HIGH);
}

TEST_F(OuterLoopCriterionTest, IsCriterionLow) {
double threshold = 4;
double epsilon = 1e-1;
double max_unsup_energy = 0.1;
const ExternalLoopOptions options = {threshold, epsilon, max_unsup_energy};
PlainData::Variables variables = {
{"PositiveUnsuppliedEnergy::1", "PositiveUnsuppliedEnergy::2", "var3"},
{0.2, 0.3, 68}};

PlainData::SubProblemData subProblemData;
subProblemData.variables = variables;
SubProblemDataMap cut_trace = {
std::make_pair(std::string("P1"), subProblemData)};

WorkerMasterData worker_master_data;
worker_master_data._cut_trace = cut_trace;

OuterloopCriterionLossOfLoad criterion(options);

EXPECT_EQ(criterion.IsCriterionSatisfied(worker_master_data), CRITERION::LOW);
}

0 comments on commit 4ea538a

Please sign in to comment.