From 8fccd2ddd7c3a9b0de0c488cf8f327730fd3e415 Mon Sep 17 00:00:00 2001 From: Guillaume PIERRE Date: Fri, 29 Nov 2024 15:51:10 +0100 Subject: [PATCH] Component fillers (2.3) : adding unit tests on class LinearExpression --- .../optim-model-filler/LinearExpression.cpp | 2 +- .../optim-model-filler/LinearExpression.h | 2 +- .../solver/optim-model-filler/CMakeLists.txt | 1 + .../test_componentFiller.cpp | 1 - .../test_linearExpression.cpp | 115 ++++++++++++++++++ 5 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 src/tests/src/solver/optim-model-filler/test_linearExpression.cpp diff --git a/src/solver/optim-model-filler/LinearExpression.cpp b/src/solver/optim-model-filler/LinearExpression.cpp index fdf41cc5cf..7da0f072d8 100644 --- a/src/solver/optim-model-filler/LinearExpression.cpp +++ b/src/solver/optim-model-filler/LinearExpression.cpp @@ -38,7 +38,7 @@ static std::map add_maps(std::map left } else { - result[var_id] = coef; + result[var_id] = rhs_multiplier * coef; } } return result; diff --git a/src/solver/optim-model-filler/include/antares/solver/optim-model-filler/LinearExpression.h b/src/solver/optim-model-filler/include/antares/solver/optim-model-filler/LinearExpression.h index 56c6fa5a9f..0bdb4af14b 100644 --- a/src/solver/optim-model-filler/include/antares/solver/optim-model-filler/LinearExpression.h +++ b/src/solver/optim-model-filler/include/antares/solver/optim-model-filler/LinearExpression.h @@ -42,7 +42,7 @@ class LinearExpression return scalar_; } - const std::map& coefPerVar() const + std::map coefPerVar() const { return coef_per_var_; } diff --git a/src/tests/src/solver/optim-model-filler/CMakeLists.txt b/src/tests/src/solver/optim-model-filler/CMakeLists.txt index 8fac12447f..038b2fa107 100644 --- a/src/tests/src/solver/optim-model-filler/CMakeLists.txt +++ b/src/tests/src/solver/optim-model-filler/CMakeLists.txt @@ -5,6 +5,7 @@ target_sources(${EXECUTABLE_NAME} PRIVATE test_main.cpp test_componentFiller.cpp + test_linearExpression.cpp ) target_include_directories(${EXECUTABLE_NAME} PRIVATE diff --git a/src/tests/src/solver/optim-model-filler/test_componentFiller.cpp b/src/tests/src/solver/optim-model-filler/test_componentFiller.cpp index 18473a22e9..67d3f3a90f 100644 --- a/src/tests/src/solver/optim-model-filler/test_componentFiller.cpp +++ b/src/tests/src/solver/optim-model-filler/test_componentFiller.cpp @@ -34,7 +34,6 @@ #include "antares/solver/modeler/ortoolsImpl/linearProblem.h" #include "antares/solver/optim-model-filler/ComponentFiller.h" #include "antares/study/system-model/component.h" -#include "antares/study/system-model/library.h" #include "antares/study/system-model/parameter.h" using namespace Antares::Solver::Modeler::Api; diff --git a/src/tests/src/solver/optim-model-filler/test_linearExpression.cpp b/src/tests/src/solver/optim-model-filler/test_linearExpression.cpp new file mode 100644 index 0000000000..8cb4a906d7 --- /dev/null +++ b/src/tests/src/solver/optim-model-filler/test_linearExpression.cpp @@ -0,0 +1,115 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#define WIN32_LEAN_AND_MEAN + +#include + +#include "antares/solver/optim-model-filler/LinearExpression.h" + +#include "../../utils/unit_test_utils.h" + +using namespace Antares::Optimization; + +BOOST_AUTO_TEST_SUITE(_linear_expressions_) + +BOOST_AUTO_TEST_CASE(default_linear_expression) +{ + LinearExpression linearExpression; + + BOOST_CHECK_EQUAL(linearExpression.scalar(), 0.); + BOOST_CHECK(linearExpression.coefPerVar().empty()); +} + +BOOST_AUTO_TEST_CASE(linear_expression_explicit_construction) +{ + LinearExpression linearExpression(4., {{"some key", -5.}}); + + BOOST_CHECK_EQUAL(linearExpression.scalar(), 4.); + BOOST_CHECK_EQUAL(linearExpression.coefPerVar().size(), 1); + BOOST_CHECK_EQUAL(linearExpression.coefPerVar()["some key"], -5.); +} + +BOOST_AUTO_TEST_CASE(sum_two_linear_expressions) +{ + LinearExpression linearExpression1(4., {{"var1", -5.}, {"var2", 6.}}); + LinearExpression linearExpression2(-1., {{"var3", 20.}, {"var2", -4.}}); + + auto sum = linearExpression1 + linearExpression2; + + BOOST_CHECK_EQUAL(sum.scalar(), 3.); + BOOST_CHECK_EQUAL(sum.coefPerVar().size(), 3); + BOOST_CHECK_EQUAL(sum.coefPerVar()["var1"], -5.); + BOOST_CHECK_EQUAL(sum.coefPerVar()["var2"], 2.); + BOOST_CHECK_EQUAL(sum.coefPerVar()["var3"], 20.); +} + +BOOST_AUTO_TEST_CASE(subtract_two_linear_expressions) +{ + LinearExpression linearExpression1(4., {{"var1", -5.}, {"var2", 6.}}); + LinearExpression linearExpression2(-1., {{"var2", -4.}, {"var3", 20.}}); + + auto subtract = linearExpression1 - linearExpression2; + + BOOST_CHECK_EQUAL(subtract.scalar(), 5.); + BOOST_CHECK_EQUAL(subtract.coefPerVar().size(), 3); + BOOST_CHECK_EQUAL(subtract.coefPerVar()["var1"], -5.); + BOOST_CHECK_EQUAL(subtract.coefPerVar()["var2"], 10.); + BOOST_CHECK_EQUAL(subtract.coefPerVar()["var3"], -20.); +} + +BOOST_AUTO_TEST_CASE(multiply_linear_expression_by_scalar) +{ + LinearExpression linearExpression(4., {{"var1", -5.}, {"var2", 6.}}); + LinearExpression someScalar(-2., {}); + + auto product = linearExpression * someScalar; + + BOOST_CHECK_EQUAL(product.scalar(), -8.); + BOOST_CHECK_EQUAL(product.coefPerVar().size(), 2); + BOOST_CHECK_EQUAL(product.coefPerVar()["var1"], 10.); + BOOST_CHECK_EQUAL(product.coefPerVar()["var2"], -12.); +} + +BOOST_AUTO_TEST_CASE(multiply_scalar_by_linear_expression) +{ + LinearExpression linearExpression(4., {{"var1", -5.}, {"var2", 6.}}); + LinearExpression someScalar(-2., {}); + + auto product = someScalar * linearExpression; + + BOOST_CHECK_EQUAL(product.scalar(), -8.); + BOOST_CHECK_EQUAL(product.coefPerVar().size(), 2); + BOOST_CHECK_EQUAL(product.coefPerVar()["var1"], 10.); + BOOST_CHECK_EQUAL(product.coefPerVar()["var2"], -12.); +} + +BOOST_AUTO_TEST_CASE(multiply_two_linear_expressions_containing_variables__exception_raised) +{ + LinearExpression linearExpression1(4., {{"var1", -5.}, {"var2", 6.}}); + LinearExpression linearExpression2(-1., {{"var2", -4.}, {"var3", 20.}}); + + BOOST_CHECK_EXCEPTION(linearExpression1 * linearExpression2, + std::invalid_argument, + checkMessage("A linear expression can't have quadratic terms.")); +} + +BOOST_AUTO_TEST_SUITE_END()