diff --git a/src/solver/modeler/api/include/antares/solver/modeler/api/linearProblemBuilder.h b/src/solver/modeler/api/include/antares/solver/modeler/api/linearProblemBuilder.h index c5b4419113..d5d7fd43e1 100644 --- a/src/solver/modeler/api/include/antares/solver/modeler/api/linearProblemBuilder.h +++ b/src/solver/modeler/api/include/antares/solver/modeler/api/linearProblemBuilder.h @@ -32,7 +32,7 @@ class LinearProblemBuilder { public: explicit LinearProblemBuilder(const std::vector& fillers); - void build(ILinearProblem& pb, LinearProblemData& data); + void build(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx); private: const std::vector& fillers_; diff --git a/src/solver/modeler/api/include/antares/solver/modeler/api/linearProblemFiller.h b/src/solver/modeler/api/include/antares/solver/modeler/api/linearProblemFiller.h index aad1fa3ce6..fb5bd230ae 100644 --- a/src/solver/modeler/api/include/antares/solver/modeler/api/linearProblemFiller.h +++ b/src/solver/modeler/api/include/antares/solver/modeler/api/linearProblemFiller.h @@ -29,12 +29,37 @@ namespace Antares::Solver::Modeler::Api { +struct FillContext +{ + FillContext(unsigned first, unsigned last): + firstTimeStep(first), + lastTimeStep(last) + { + } + + unsigned getFirstTimeStep() const + { + return firstTimeStep; + } + + unsigned getLastTimeStep() const + { + return lastTimeStep; + } + + std::vector scenariosSelected; + +private: + unsigned firstTimeStep; + unsigned lastTimeStep; +}; + class LinearProblemFiller { public: - virtual void addVariables(ILinearProblem& pb, LinearProblemData& data) = 0; - virtual void addConstraints(ILinearProblem& pb, LinearProblemData& data) = 0; - virtual void addObjective(ILinearProblem& pb, LinearProblemData& data) = 0; + virtual void addVariables(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) = 0; + virtual void addConstraints(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) = 0; + virtual void addObjective(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) = 0; virtual ~LinearProblemFiller() = default; }; diff --git a/src/solver/modeler/api/linearProblemBuilder.cpp b/src/solver/modeler/api/linearProblemBuilder.cpp index f9d6103152..5363647c3f 100644 --- a/src/solver/modeler/api/linearProblemBuilder.cpp +++ b/src/solver/modeler/api/linearProblemBuilder.cpp @@ -1,3 +1,24 @@ +/* + * 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 . + */ + #include #include @@ -11,11 +32,14 @@ LinearProblemBuilder::LinearProblemBuilder(const std::vectoraddVariables(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); }); + std::ranges::for_each(fillers_, + [&](const auto& filler) { filler->addVariables(pb, data, ctx); }); + std::ranges::for_each(fillers_, + [&](const auto& filler) { filler->addConstraints(pb, data, ctx); }); + std::ranges::for_each(fillers_, + [&](const auto& filler) { filler->addObjective(pb, data, ctx); }); } } // namespace Antares::Solver::Modeler::Api diff --git a/src/tests/src/solver/modeler/api/mock-fillers/FillerContext.h b/src/tests/src/solver/modeler/api/mock-fillers/FillerContext.h new file mode 100644 index 0000000000..f24c931e88 --- /dev/null +++ b/src/tests/src/solver/modeler/api/mock-fillers/FillerContext.h @@ -0,0 +1,48 @@ +#pragma once + +#include "antares/solver/modeler/api/linearProblemFiller.h" + +namespace Antares::Solver::Modeler::Api +{ + +class VarFillerContext: public LinearProblemFiller +{ +public: + explicit VarFillerContext() = default; + void addVariables(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; + void addConstraints(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; + void addObjective(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; + + std::array, 5> timeseries = { + {{1, 3, 5}, {2, 4, 6}, {7, 9, 11}, {8, 10, 12}, {13, 15, 17}}}; +}; + +void VarFillerContext::addVariables(ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) +{ + for (unsigned timestep = ctx.getFirstTimeStep(); timestep < ctx.getLastTimeStep(); timestep++) + { + for (unsigned scenario: ctx.scenariosSelected) + { + pb.addNumVariable(timeseries[timestep][scenario], + timeseries[timestep][scenario], + "variable-ts" + std::to_string(timestep) + "-sc" + + std::to_string(scenario)); + } + } +} + +void VarFillerContext::addConstraints([[maybe_unused]] ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) +{ +} + +void VarFillerContext::addObjective([[maybe_unused]] ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) +{ +} + +} // namespace Antares::Solver::Modeler::Api diff --git a/src/tests/src/solver/modeler/api/mock-fillers/OneConstraintFiller.h b/src/tests/src/solver/modeler/api/mock-fillers/OneConstraintFiller.h index cb1f9db3e9..5ec2662b2a 100644 --- a/src/tests/src/solver/modeler/api/mock-fillers/OneConstraintFiller.h +++ b/src/tests/src/solver/modeler/api/mock-fillers/OneConstraintFiller.h @@ -9,21 +9,27 @@ 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 addVariables(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; + void addConstraints(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; + void addObjective(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; }; -void OneConstraintFiller::addVariables(ILinearProblem& pb, LinearProblemData& data) +void OneConstraintFiller::addVariables([[maybe_unused]] ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) { } -void OneConstraintFiller::addConstraints(ILinearProblem& pb, LinearProblemData& data) +void OneConstraintFiller::addConstraints(ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) { pb.addConstraint(1, 2, "constraint-by-OneConstraintFiller"); } -void OneConstraintFiller::addObjective(ILinearProblem& pb, LinearProblemData& data) +void OneConstraintFiller::addObjective([[maybe_unused]] ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) { } diff --git a/src/tests/src/solver/modeler/api/mock-fillers/OneVarFiller.h b/src/tests/src/solver/modeler/api/mock-fillers/OneVarFiller.h index 5d2ba08b55..a828056b63 100644 --- a/src/tests/src/solver/modeler/api/mock-fillers/OneVarFiller.h +++ b/src/tests/src/solver/modeler/api/mock-fillers/OneVarFiller.h @@ -9,24 +9,30 @@ 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; + void addVariables(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; + void addConstraints(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; + void addObjective(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; private: std::string added_var_name_ = "var-by-OneVarFiller"; }; -void OneVarFiller::addVariables(ILinearProblem& pb, LinearProblemData& data) +void OneVarFiller::addVariables(ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) { pb.addNumVariable(0, 1, added_var_name_); } -void OneVarFiller::addConstraints(ILinearProblem& pb, LinearProblemData& data) +void OneVarFiller::addConstraints([[maybe_unused]] ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) { } -void OneVarFiller::addObjective(ILinearProblem& pb, LinearProblemData& data) +void OneVarFiller::addObjective(ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) { auto* var = pb.getVariable(added_var_name_); pb.setObjectiveCoefficient(var, 1); diff --git a/src/tests/src/solver/modeler/api/mock-fillers/TwoVarsTwoConstraintsFiller.h b/src/tests/src/solver/modeler/api/mock-fillers/TwoVarsTwoConstraintsFiller.h index e8671644df..832ba3954f 100644 --- a/src/tests/src/solver/modeler/api/mock-fillers/TwoVarsTwoConstraintsFiller.h +++ b/src/tests/src/solver/modeler/api/mock-fillers/TwoVarsTwoConstraintsFiller.h @@ -9,24 +9,30 @@ 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 addVariables(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; + void addConstraints(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; + void addObjective(ILinearProblem& pb, LinearProblemData& data, FillContext& ctx) override; }; -void TwoVarsTwoConstraintsFiller::addVariables(ILinearProblem& pb, LinearProblemData& data) +void TwoVarsTwoConstraintsFiller::addVariables(ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) { pb.addNumVariable(0, 1, "var-1-by-TwoVarsTwoConstraintsFiller"); pb.addNumVariable(0, 3, "var-2-by-TwoVarsTwoConstraintsFiller"); } -void TwoVarsTwoConstraintsFiller::addConstraints(ILinearProblem& pb, LinearProblemData& data) +void TwoVarsTwoConstraintsFiller::addConstraints(ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) { pb.addConstraint(1, 2, "constr-1-by-TwoVarsTwoConstraintsFiller"); pb.addConstraint(1, 3, "constr-2-by-TwoVarsTwoConstraintsFiller"); } -void TwoVarsTwoConstraintsFiller::addObjective(ILinearProblem& pb, LinearProblemData& data) +void TwoVarsTwoConstraintsFiller::addObjective([[maybe_unused]] ILinearProblem& pb, + [[maybe_unused]] LinearProblemData& data, + [[maybe_unused]] FillContext& ctx) { } diff --git a/src/tests/src/solver/modeler/api/testModelerLPbuilder.cpp b/src/tests/src/solver/modeler/api/testModelerLPbuilder.cpp index ccdccf251a..6a423e2b37 100644 --- a/src/tests/src/solver/modeler/api/testModelerLPbuilder.cpp +++ b/src/tests/src/solver/modeler/api/testModelerLPbuilder.cpp @@ -25,6 +25,7 @@ #include #include +#include "mock-fillers/FillerContext.h" #include "mock-fillers/OneConstraintFiller.h" #include "mock-fillers/OneVarFiller.h" #include "mock-fillers/TwoVarsTwoConstraintsFiller.h" @@ -41,6 +42,7 @@ struct Fixture std::vector fillers; LinearProblemData LP_Data; + FillContext ctx = {0, 0}; // dummy value for other tests than context std::unique_ptr pb; }; @@ -49,7 +51,7 @@ 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); + lpBuilder.build(*pb, LP_Data, ctx); BOOST_CHECK_EQUAL(pb->variableCount(), 0); BOOST_CHECK_EQUAL(pb->constraintCount(), 0); @@ -61,7 +63,7 @@ BOOST_FIXTURE_TEST_CASE(one_var_filler___the_var_is_built, Fixture) fillers = {oneVarFiller.get()}; LinearProblemBuilder lpBuilder(fillers); - lpBuilder.build(*pb, LP_Data); + lpBuilder.build(*pb, LP_Data, ctx); BOOST_CHECK_EQUAL(pb->variableCount(), 1); BOOST_CHECK_EQUAL(pb->constraintCount(), 0); @@ -76,7 +78,7 @@ BOOST_FIXTURE_TEST_CASE(one_constraint_filler___the_constraint_is_built, Fixture fillers = {oneConstrFiller.get()}; LinearProblemBuilder lpBuilder(fillers); - lpBuilder.build(*pb, LP_Data); + lpBuilder.build(*pb, LP_Data, ctx); BOOST_CHECK_EQUAL(pb->variableCount(), 0); BOOST_CHECK_EQUAL(pb->constraintCount(), 1); @@ -91,7 +93,7 @@ BOOST_FIXTURE_TEST_CASE(two_fillers_given_to_builder___all_is_built, Fixture) fillers = {oneVarFiller.get(), oneConstrFiller.get()}; LinearProblemBuilder lpBuilder(fillers); - lpBuilder.build(*pb, LP_Data); + lpBuilder.build(*pb, LP_Data, ctx); BOOST_CHECK_EQUAL(pb->constraintCount(), 1); BOOST_CHECK(pb->getConstraint("constraint-by-OneConstraintFiller")); @@ -106,10 +108,32 @@ BOOST_FIXTURE_TEST_CASE(three_fillers_given_to_builder___3_vars_3_constr_are_bui fillers = {oneVarFiller.get(), oneConstrFiller.get(), twoVarsTwoConstrFiller.get()}; LinearProblemBuilder lpBuilder(fillers); - lpBuilder.build(*pb, LP_Data); + lpBuilder.build(*pb, LP_Data, ctx); BOOST_CHECK_EQUAL(pb->variableCount(), 3); BOOST_CHECK_EQUAL(pb->constraintCount(), 3); } +BOOST_FIXTURE_TEST_CASE(FillerWithContext, Fixture) +{ + auto varFiller = std::make_unique(); + fillers = {varFiller.get()}; + + ctx = FillContext(0, 5); + + ctx.scenariosSelected.push_back(0); + ctx.scenariosSelected.push_back(2); + + LinearProblemBuilder lpBuilder(fillers); + lpBuilder.build(*pb, LP_Data, ctx); + + BOOST_CHECK_EQUAL(pb->variableCount(), 10); // 5 timestep * 2 scenario + + auto var1 = pb->getVariable("variable-ts0-sc0"); + BOOST_CHECK_EQUAL(var1->getLb(), varFiller->timeseries[0][0]); + + auto var2 = pb->getVariable("variable-ts3-sc2"); + BOOST_CHECK_EQUAL(var2->getLb(), varFiller->timeseries[3][2]); +} + BOOST_AUTO_TEST_SUITE_END()