Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly size ConstraintsMatrixCoeff and ColumnIndexes #2208

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions src/solver/optimisation/HebdoProblemToLpsTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#include "antares/solver/optimisation/HebdoProblemToLpsTranslator.h"

#include "antares/solver/utils/filename.h"

namespace Antares::Solver
Expand All @@ -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.
Expand All @@ -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);
Expand All @@ -64,46 +68,57 @@ 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;

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);
ret.Mdeb.push_back(ret.CoeffCount);
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
} // namespace Antares::Solver
45 changes: 39 additions & 6 deletions src/solver/optimisation/opt_optimisation_lineaire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Comment on lines +178 to +179
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All is good except for this. These are temporary variables used in ConstraintBuilder::OPT_ChargerLaContrainteDansLaMatriceDesContraintes. You don't need to resize them

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)
{
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading