From 744318eddd8f3919a95368384757cf719e8ad8f1 Mon Sep 17 00:00:00 2001 From: Jason Marechal Date: Tue, 25 Jun 2024 10:05:58 +0200 Subject: [PATCH 1/3] Resize coef and colIndex before sending them by API --- .../HebdoProblemToLpsTranslator.cpp | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp b/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp index 1109e9c780..4e35248ba4 100644 --- a/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp +++ b/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp @@ -21,6 +21,7 @@ */ #include "antares/solver/optimisation/HebdoProblemToLpsTranslator.h" + #include "antares/solver/utils/filename.h" namespace Antares::Solver @@ -31,7 +32,8 @@ namespace /** * @brief Copies elements from one container to another. * - * This function takes two containers as arguments. It copies elements from the first container to the second one. + * This function takes two containers as arguments. It copies elements from the first container to + * the second one. * * @param in The container from which to copy elements. * @param out The container to which to copy elements. @@ -41,14 +43,16 @@ void copy(const T& in, U& out) { std::ranges::copy(in, std::back_inserter(out)); } -} +} // namespace WeeklyDataFromAntares HebdoProblemToLpsTranslator::translate( const PROBLEME_ANTARES_A_RESOUDRE* problem, std::string_view name) const { if (problem == nullptr) + { return {}; + } auto ret = WeeklyDataFromAntares(); copy(problem->CoutLineaire, ret.LinearCost); @@ -64,23 +68,33 @@ WeeklyDataFromAntares HebdoProblemToLpsTranslator::translate( return ret; } -ConstantDataFromAntares HebdoProblemToLpsTranslator::commonProblemData(const PROBLEME_ANTARES_A_RESOUDRE* problem) const { +ConstantDataFromAntares HebdoProblemToLpsTranslator::commonProblemData( + const PROBLEME_ANTARES_A_RESOUDRE* problem) const +{ if (problem == nullptr) + { return ConstantDataFromAntares(); + } - if (problem->NombreDeVariables <= 0) { + if (problem->NombreDeVariables <= 0) + { throw WeeklyProblemTranslationException("VariablesCount must be strictly positive"); } - if (problem->NombreDeContraintes <= 0) { + if (problem->NombreDeContraintes <= 0) + { throw WeeklyProblemTranslationException("ConstraintesCount must be strictly positive"); } - if (problem->NombreDeContraintes > problem->IndicesDebutDeLigne.size()) { - throw WeeklyProblemTranslationException("ConstraintesCount exceed IndicesDebutDeLigne size"); + if (problem->NombreDeContraintes > problem->IndicesDebutDeLigne.size()) + { + throw WeeklyProblemTranslationException( + "ConstraintesCount exceed IndicesDebutDeLigne size"); } - if (problem->NombreDeContraintes > problem->NombreDeTermesDesLignes.size()) { - throw WeeklyProblemTranslationException("ConstraintesCount exceed NombreDeTermesDesLignes size"); + if (problem->NombreDeContraintes > problem->NombreDeTermesDesLignes.size()) + { + throw WeeklyProblemTranslationException( + "ConstraintesCount exceed NombreDeTermesDesLignes size"); } ConstantDataFromAntares ret; @@ -88,22 +102,22 @@ ConstantDataFromAntares HebdoProblemToLpsTranslator::commonProblemData(const PRO ret.VariablesCount = problem->NombreDeVariables; ret.ConstraintesCount = problem->NombreDeContraintes; - ret.CoeffCount = problem->IndicesDebutDeLigne[problem->NombreDeContraintes - 1] + - problem->NombreDeTermesDesLignes[problem->NombreDeContraintes - 1]; + ret.CoeffCount = problem->IndicesDebutDeLigne[problem->NombreDeContraintes - 1] + + problem->NombreDeTermesDesLignes[problem->NombreDeContraintes - 1]; copy(problem->TypeDeVariable, ret.VariablesType); - copy(problem->CoefficientsDeLaMatriceDesContraintes, - ret.ConstraintsMatrixCoeff); + copy(problem->CoefficientsDeLaMatriceDesContraintes, ret.ConstraintsMatrixCoeff); + ret.ConstraintsMatrixCoeff.resize(ret.CoeffCount); copy(problem->IndicesColonnes, ret.ColumnIndexes); - + ret.ColumnIndexes.resize(ret.CoeffCount); copy(problem->IndicesDebutDeLigne, ret.Mdeb); return ret; } WeeklyProblemTranslationException::WeeklyProblemTranslationException( - const std::string& string) noexcept -: std::runtime_error{string} + const std::string& string) noexcept: + std::runtime_error{string} { } -} // namespace Antares::Solver \ No newline at end of file +} // namespace Antares::Solver From 48990eaf57c0d77beb05d3216d765fe838fcf29d Mon Sep 17 00:00:00 2001 From: Jason Marechal Date: Wed, 26 Jun 2024 16:45:08 +0200 Subject: [PATCH 2/3] Add free function in anonymous namespace --- .../opt_optimisation_lineaire.cpp | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/solver/optimisation/opt_optimisation_lineaire.cpp b/src/solver/optimisation/opt_optimisation_lineaire.cpp index d3635ff326..caf250cb2f 100644 --- a/src/solver/optimisation/opt_optimisation_lineaire.cpp +++ b/src/solver/optimisation/opt_optimisation_lineaire.cpp @@ -76,15 +76,14 @@ void notifyProblemHebdo(const PROBLEME_HEBDO* problemeHebdo, createMPSfilename(*optPeriodStringGenerator, optimizationNumber)); } -} +} // namespace bool runWeeklyOptimization(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, const AdqPatchParams& adqPatchParams, Solver::IResultWriter& writer, int optimizationNumber, - Solver::Simulation::ISimulationObserver& simulationObserver - ) + Solver::Simulation::ISimulationObserver& simulationObserver) { const int NombreDePasDeTempsPourUneOptimisation = problemeHebdo ->NombreDePasDeTempsPourUneOptimisation; @@ -121,7 +120,10 @@ bool runWeeklyOptimization(const OptimizationOptions& options, problemeHebdo->weekInTheYear, problemeHebdo->year); - notifyProblemHebdo(problemeHebdo, optimizationNumber, simulationObserver, optPeriodStringGenerator.get()); + notifyProblemHebdo(problemeHebdo, + optimizationNumber, + simulationObserver, + optPeriodStringGenerator.get()); if (!OPT_AppelDuSimplexe(options, problemeHebdo, @@ -158,14 +160,42 @@ void runThermalHeuristic(PROBLEME_HEBDO* problemeHebdo) OPT_CalculerLesPminThermiquesEnFonctionDeMUTetMDT(problemeHebdo); } } + + +void resizeProbleme(PROBLEME_ANTARES_A_RESOUDRE* ProblemeAResoudre, + unsigned nombreDeVariables, + unsigned nombreDeContraintes) +{ + ProblemeAResoudre->CoutQuadratique.resize(nombreDeVariables); + ProblemeAResoudre->CoutLineaire.resize(nombreDeVariables); + ProblemeAResoudre->TypeDeVariable.resize(nombreDeVariables); + ProblemeAResoudre->Xmin.resize(nombreDeVariables); + ProblemeAResoudre->Xmax.resize(nombreDeVariables); + ProblemeAResoudre->X.resize(nombreDeVariables); + ProblemeAResoudre->AdresseOuPlacerLaValeurDesVariablesOptimisees.resize(nombreDeVariables); + ProblemeAResoudre->AdresseOuPlacerLaValeurDesCoutsReduits.resize(nombreDeVariables); + ProblemeAResoudre->PositionDeLaVariable.resize(nombreDeVariables); + ProblemeAResoudre->Pi.resize(nombreDeVariables); + ProblemeAResoudre->Colonne.resize(nombreDeVariables); + ProblemeAResoudre->NomDesVariables.resize(nombreDeVariables); + ProblemeAResoudre->VariablesEntieres.resize(nombreDeVariables); + + ProblemeAResoudre->Sens.resize(nombreDeContraintes); + ProblemeAResoudre->IndicesDebutDeLigne.resize(nombreDeContraintes); + ProblemeAResoudre->NombreDeTermesDesLignes.resize(nombreDeContraintes); + ProblemeAResoudre->SecondMembre.resize(nombreDeContraintes); + ProblemeAResoudre->AdresseOuPlacerLaValeurDesCoutsMarginaux.resize(nombreDeContraintes); + ProblemeAResoudre->CoutsMarginauxDesContraintes.resize(nombreDeContraintes); + ProblemeAResoudre->ComplementDeLaBase.resize(nombreDeContraintes); + ProblemeAResoudre->NomDesContraintes.resize(nombreDeContraintes); +} } // namespace bool OPT_OptimisationLineaire(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, const AdqPatchParams& adqPatchParams, Solver::IResultWriter& writer, - Solver::Simulation::ISimulationObserver& simulationObserver - ) + Solver::Simulation::ISimulationObserver& simulationObserver) { if (!problemeHebdo->OptimisationAuPasHebdomadaire) { @@ -189,6 +219,9 @@ bool OPT_OptimisationLineaire(const OptimizationOptions& options, ConstraintBuilder builder(builder_data); LinearProblemMatrix linearProblemMatrix(problemeHebdo, builder); linearProblemMatrix.Run(); + resizeProbleme(problemeHebdo->ProblemeAResoudre.get(), + problemeHebdo->ProblemeAResoudre->NombreDeVariables, + problemeHebdo->ProblemeAResoudre->NombreDeContraintes); if (problemeHebdo->ExportStructure && problemeHebdo->firstWeekOfSimulation) { OPT_ExportStructures(problemeHebdo, writer); From 5357f22a08fa8ded4aa30faa87af35723177f21e Mon Sep 17 00:00:00 2001 From: Jason Marechal Date: Thu, 27 Jun 2024 12:03:15 +0200 Subject: [PATCH 3/3] Add missing value in mdeb --- src/solver/optimisation/HebdoProblemToLpsTranslator.cpp | 1 + .../src/solver/optimisation/translator/test_translator.cpp | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp b/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp index 4e35248ba4..d4c6e4b6b0 100644 --- a/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp +++ b/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp @@ -112,6 +112,7 @@ ConstantDataFromAntares HebdoProblemToLpsTranslator::commonProblemData( copy(problem->IndicesColonnes, ret.ColumnIndexes); ret.ColumnIndexes.resize(ret.CoeffCount); copy(problem->IndicesDebutDeLigne, ret.Mdeb); + ret.Mdeb.push_back(ret.CoeffCount); return ret; } diff --git a/src/tests/src/solver/optimisation/translator/test_translator.cpp b/src/tests/src/solver/optimisation/translator/test_translator.cpp index 6fdc32c8fd..2430c74cb0 100644 --- a/src/tests/src/solver/optimisation/translator/test_translator.cpp +++ b/src/tests/src/solver/optimisation/translator/test_translator.cpp @@ -130,7 +130,9 @@ BOOST_AUTO_TEST_CASE(common_data_properly_copied) BOOST_CHECK(std::ranges::equal(ret.VariablesType, problemHebdo.TypeDeVariable)); BOOST_CHECK(ret.ConstraintsMatrixCoeff == problemHebdo.CoefficientsDeLaMatriceDesContraintes); BOOST_CHECK(std::ranges::equal(ret.ColumnIndexes, problemHebdo.IndicesColonnes)); - BOOST_CHECK(std::ranges::equal(ret.Mdeb, problemHebdo.IndicesDebutDeLigne)); + auto expectedMdeb = problemHebdo.IndicesDebutDeLigne; + expectedMdeb.push_back(problemHebdo.CoefficientsDeLaMatriceDesContraintes.size()); + BOOST_CHECK(std::ranges::equal(ret.Mdeb, expectedMdeb)); } // throw exception if NombreDeVariables is 0