Skip to content

Commit

Permalink
Component fillers (2.3) : adding unit tests on class LinearExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
guilpier-code committed Nov 29, 2024
1 parent 42c54a6 commit 8fccd2d
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/solver/optim-model-filler/LinearExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static std::map<std::string, double> add_maps(std::map<std::string, double> left
}
else
{
result[var_id] = coef;
result[var_id] = rhs_multiplier * coef;
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class LinearExpression
return scalar_;
}

const std::map<std::string, double>& coefPerVar() const
std::map<std::string, double> coefPerVar() const
{
return coef_per_var_;
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/src/solver/optim-model-filler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target_sources(${EXECUTABLE_NAME}
PRIVATE
test_main.cpp
test_componentFiller.cpp
test_linearExpression.cpp
)
target_include_directories(${EXECUTABLE_NAME}
PRIVATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
115 changes: 115 additions & 0 deletions src/tests/src/solver/optim-model-filler/test_linearExpression.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://opensource.org/license/mpl-2-0/>.
*/

#define WIN32_LEAN_AND_MEAN

#include <boost/test/unit_test.hpp>

#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()

0 comments on commit 8fccd2d

Please sign in to comment.