-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The goal of this PR is to introduce and unit test class **LinearProblemBuilder**. This class is an orchestration class, and therefore makes other classes collaborate. What was done : - Remove **LinearProblemBuilder::solve()** : this method was planned in the specifications, but it extends the builder responsibility scope. Once modified by the builder, the problem can be solved by calling its own **solve()** method. - Add methods **numVariables()** and **numConstraints()** to base class **ILinearProblem** for unit tests reason. - **CMakeLists.txt** file for modeler API was split into 2 **\*.cmake** files - **Trial for a dependency simplification** : In the original / specified architecture, **LinearProblemBuilder** collaborates with classes : - **LinearProblemData** - **ILinearProblem** (interface for LP) - **LinearProblemFiller** : this class also depends on previous classes Here, we allowed ourselves to reduce dependencies of this builder class (is this simplification relevant ?) : class **LinearProblemBuilder** currently only depends on **LinearProblemFiller**, while fillers keeps its planned dependencies on **LinearProblemData** and **ILinearProblem**. Indeed, any filler is in charge to modify problem (using LP data), the but the builder only manipulates fillers. - **Mocking fillers** : fillers are the entities responsible for creating variables and constraints in the linear problem. To do that, they need the LP itself as well as the LP data (see fillers constructor). We created an abstract base filler. All concrete filler inherit from it. Class **LinearProblemBuilder** is constructed from a vector of fillers. - Several unit tests were created for class **LinearProblemBuilder** --------- Co-authored-by: Abdoulbari Zaher <[email protected]>
- Loading branch information
1 parent
8ce6107
commit f20ffc8
Showing
15 changed files
with
298 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <algorithm> | ||
#include <memory> | ||
|
||
#include <antares/solver/modeler/api/linearProblemBuilder.h> | ||
|
||
namespace Antares::Solver::Modeler::Api | ||
{ | ||
|
||
LinearProblemBuilder::LinearProblemBuilder(const std::vector<LinearProblemFiller*>& fillers): | ||
fillers_(fillers) | ||
{ | ||
} | ||
|
||
void LinearProblemBuilder::build(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
std::ranges::for_each(fillers_, [&](const auto& filler) { filler->addVariables(pb, data); }); | ||
std::ranges::for_each(fillers_, [&](const auto& filler) { filler->addConstraints(pb, data); }); | ||
std::ranges::for_each(fillers_, [&](const auto& filler) { filler->addObjective(pb, data); }); | ||
} | ||
|
||
} // namespace Antares::Solver::Modeler::Api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/tests/src/solver/modeler/api/mock-fillers/OneConstraintFiller.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include "antares/solver/modeler/api/linearProblemFiller.h" | ||
|
||
namespace Antares::Solver::Modeler::Api | ||
{ | ||
|
||
class OneConstraintFiller: public LinearProblemFiller | ||
{ | ||
public: | ||
explicit OneConstraintFiller() = default; | ||
void addVariables(ILinearProblem& pb, LinearProblemData& data) override; | ||
void addConstraints(ILinearProblem& pb, LinearProblemData& data) override; | ||
void addObjective(ILinearProblem& pb, LinearProblemData& data) override; | ||
}; | ||
|
||
void OneConstraintFiller::addVariables(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
} | ||
|
||
void OneConstraintFiller::addConstraints(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
pb.addConstraint(1, 2, "constraint-by-OneConstraintFiller"); | ||
} | ||
|
||
void OneConstraintFiller::addObjective(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
} | ||
|
||
} // namespace Antares::Solver::Modeler::Api |
35 changes: 35 additions & 0 deletions
35
src/tests/src/solver/modeler/api/mock-fillers/OneVarFiller.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include "antares/solver/modeler/api/linearProblemFiller.h" | ||
|
||
namespace Antares::Solver::Modeler::Api | ||
{ | ||
|
||
class OneVarFiller: public LinearProblemFiller | ||
{ | ||
public: | ||
explicit OneVarFiller() = default; | ||
void addVariables(ILinearProblem& pb, LinearProblemData& data) override; | ||
void addConstraints(ILinearProblem& pb, LinearProblemData& data) override; | ||
void addObjective(ILinearProblem& pb, LinearProblemData& data) override; | ||
|
||
private: | ||
std::string added_var_name_ = "var-by-OneVarFiller"; | ||
}; | ||
|
||
void OneVarFiller::addVariables(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
pb.addNumVariable(0, 1, added_var_name_); | ||
} | ||
|
||
void OneVarFiller::addConstraints(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
} | ||
|
||
void OneVarFiller::addObjective(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
auto* var = pb.getVariable(added_var_name_); | ||
pb.setObjectiveCoefficient(var, 1); | ||
} | ||
|
||
} // namespace Antares::Solver::Modeler::Api |
33 changes: 33 additions & 0 deletions
33
src/tests/src/solver/modeler/api/mock-fillers/TwoVarsTwoConstraintsFiller.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "antares/solver/modeler/api/linearProblemFiller.h" | ||
|
||
namespace Antares::Solver::Modeler::Api | ||
{ | ||
|
||
class TwoVarsTwoConstraintsFiller: public LinearProblemFiller | ||
{ | ||
public: | ||
explicit TwoVarsTwoConstraintsFiller() = default; | ||
void addVariables(ILinearProblem& pb, LinearProblemData& data) override; | ||
void addConstraints(ILinearProblem& pb, LinearProblemData& data) override; | ||
void addObjective(ILinearProblem& pb, LinearProblemData& data) override; | ||
}; | ||
|
||
void TwoVarsTwoConstraintsFiller::addVariables(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
pb.addNumVariable(0, 1, "var-1-by-TwoVarsTwoConstraintsFiller"); | ||
pb.addNumVariable(0, 3, "var-2-by-TwoVarsTwoConstraintsFiller"); | ||
} | ||
|
||
void TwoVarsTwoConstraintsFiller::addConstraints(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
pb.addConstraint(1, 2, "constr-1-by-TwoVarsTwoConstraintsFiller"); | ||
pb.addConstraint(1, 3, "constr-2-by-TwoVarsTwoConstraintsFiller"); | ||
} | ||
|
||
void TwoVarsTwoConstraintsFiller::addObjective(ILinearProblem& pb, LinearProblemData& data) | ||
{ | ||
} | ||
|
||
} // namespace Antares::Solver::Modeler::Api |
115 changes: 115 additions & 0 deletions
115
src/tests/src/solver/modeler/api/testModelerLPbuilder.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/modeler/api/linearProblemBuilder.h> | ||
#include <antares/solver/modeler/ortoolsImpl/linearProblem.h> | ||
|
||
#include "mock-fillers/OneConstraintFiller.h" | ||
#include "mock-fillers/OneVarFiller.h" | ||
#include "mock-fillers/TwoVarsTwoConstraintsFiller.h" | ||
|
||
using namespace Antares::Solver::Modeler::Api; | ||
using namespace Antares::Solver::Modeler::OrtoolsImpl; | ||
|
||
struct Fixture | ||
{ | ||
Fixture() | ||
{ | ||
pb = std::make_unique<OrtoolsLinearProblem>(false, "sirius"); | ||
} | ||
|
||
std::vector<LinearProblemFiller*> fillers; | ||
LinearProblemData LP_Data; | ||
std::unique_ptr<ILinearProblem> pb; | ||
}; | ||
|
||
BOOST_AUTO_TEST_SUITE(tests_on_linear_problem_builder) | ||
|
||
BOOST_FIXTURE_TEST_CASE(no_filler_given_to_builder___nothing_built, Fixture) | ||
{ | ||
LinearProblemBuilder lpBuilder(fillers); | ||
lpBuilder.build(*pb, LP_Data); | ||
|
||
BOOST_CHECK_EQUAL(pb->variableCount(), 0); | ||
BOOST_CHECK_EQUAL(pb->constraintCount(), 0); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(one_var_filler___the_var_is_built, Fixture) | ||
{ | ||
auto oneVarFiller = std::make_unique<OneVarFiller>(); | ||
fillers = {oneVarFiller.get()}; | ||
|
||
LinearProblemBuilder lpBuilder(fillers); | ||
lpBuilder.build(*pb, LP_Data); | ||
|
||
BOOST_CHECK_EQUAL(pb->variableCount(), 1); | ||
BOOST_CHECK_EQUAL(pb->constraintCount(), 0); | ||
auto* var = pb->getVariable("var-by-OneVarFiller"); | ||
BOOST_CHECK(var); | ||
BOOST_CHECK_EQUAL(pb->getObjectiveCoefficient(var), 1); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(one_constraint_filler___the_constraint_is_built, Fixture) | ||
{ | ||
auto oneConstrFiller = std::make_unique<OneConstraintFiller>(); | ||
fillers = {oneConstrFiller.get()}; | ||
|
||
LinearProblemBuilder lpBuilder(fillers); | ||
lpBuilder.build(*pb, LP_Data); | ||
|
||
BOOST_CHECK_EQUAL(pb->variableCount(), 0); | ||
BOOST_CHECK_EQUAL(pb->constraintCount(), 1); | ||
BOOST_CHECK(pb->getConstraint("constraint-by-OneConstraintFiller")); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(two_fillers_given_to_builder___all_is_built, Fixture) | ||
{ | ||
auto oneVarFiller = std::make_unique<OneVarFiller>(); | ||
auto oneConstrFiller = std::make_unique<OneConstraintFiller>(); | ||
|
||
fillers = {oneVarFiller.get(), oneConstrFiller.get()}; | ||
|
||
LinearProblemBuilder lpBuilder(fillers); | ||
lpBuilder.build(*pb, LP_Data); | ||
|
||
BOOST_CHECK_EQUAL(pb->constraintCount(), 1); | ||
BOOST_CHECK(pb->getConstraint("constraint-by-OneConstraintFiller")); | ||
BOOST_CHECK_EQUAL(pb->variableCount(), 1); | ||
} | ||
|
||
BOOST_FIXTURE_TEST_CASE(three_fillers_given_to_builder___3_vars_3_constr_are_built, Fixture) | ||
{ | ||
auto oneVarFiller = std::make_unique<OneVarFiller>(); | ||
auto oneConstrFiller = std::make_unique<OneConstraintFiller>(); | ||
auto twoVarsTwoConstrFiller = std::make_unique<TwoVarsTwoConstraintsFiller>(); | ||
fillers = {oneVarFiller.get(), oneConstrFiller.get(), twoVarsTwoConstrFiller.get()}; | ||
|
||
LinearProblemBuilder lpBuilder(fillers); | ||
lpBuilder.build(*pb, LP_Data); | ||
|
||
BOOST_CHECK_EQUAL(pb->variableCount(), 3); | ||
BOOST_CHECK_EQUAL(pb->constraintCount(), 3); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.