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

Add adequacy patch optimization number to MPS filename to avoid overwrite #1187

Closed
wants to merge 5 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
1 change: 1 addition & 0 deletions src/solver/cmake/solver.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ set(RTESOLVER_OPT
optimisation/adequacy_patch_csr/count_constraints_variables.cpp

optimisation/opt_period_string_generator_base.h
optimisation/opt_period_string_generator_base.cpp

utils/ortools_utils.h
utils/ortools_utils.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ AdequacyPatchOptimization::AdequacyPatchOptimization(PROBLEME_HEBDO* problemeHeb
}
void AdequacyPatchOptimization::solve(uint weekInTheYear, int hourInTheYear)
{
problemeHebdo_->adqPatchParams->AdequacyFirstStep = true;
problemeHebdo_->adequacyPatchRuntimeData->LMR_FirstOptimization = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

ok.
Should conflict with #1189

OPT_OptimisationHebdomadaire(problemeHebdo_, thread_number_);
problemeHebdo_->adqPatchParams->AdequacyFirstStep = false;
problemeHebdo_->adequacyPatchRuntimeData->LMR_FirstOptimization = false;

for (int pays = 0; pays < problemeHebdo_->NombreDePays; ++pays)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ void setNTCbounds(double& Xmax,
Xmin = -(ValeursDeNTC->ValeurDeNTCExtremiteVersOrigine[Interco]);

// set for adq patch first step
if (problemeHebdo->adqPatchParams && problemeHebdo->adqPatchParams->AdequacyFirstStep)
if (problemeHebdo->adqPatchParams
&& problemeHebdo->adequacyPatchRuntimeData->LMR_FirstOptimization)
Comment on lines +139 to +140
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
if (problemeHebdo->adqPatchParams
&& problemeHebdo->adequacyPatchRuntimeData->LMR_FirstOptimization)
if (problemeHebdo->adequacyPatchRuntimeData
&& problemeHebdo->adequacyPatchRuntimeData->LMR_FirstOptimization)

{
ntcToZeroStatusForAdqPatch = getNTCtoZeroStatus(problemeHebdo, Interco);

Expand Down
32 changes: 23 additions & 9 deletions src/solver/optimisation/opt_appel_solveur_lineaire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "../simulation/sim_structure_probleme_economique.h"
#include "../simulation/sim_structure_probleme_adequation.h"
#include "../simulation/sim_extern_variables_globales.h"
#include "../simulation/adequacy_patch_runtime_data.h"

#include "opt_fonctions.h"

Expand Down Expand Up @@ -98,7 +99,7 @@ class TimeMeasurement
bool OPT_AppelDuSimplexe(PROBLEME_HEBDO* problemeHebdo,
int NumIntervalle,
const int optimizationNumber,
std::shared_ptr<OptPeriodStringGenerator> optPeriodStringGenerator)
OptPeriodStringGenerator& optPeriodStringGenerator)
{
PROBLEME_ANTARES_A_RESOUDRE* ProblemeAResoudre = problemeHebdo->ProblemeAResoudre;
Optimization::PROBLEME_SIMPLEXE_NOMME Probleme(ProblemeAResoudre->NomDesVariables,
Expand All @@ -107,8 +108,10 @@ bool OPT_AppelDuSimplexe(PROBLEME_HEBDO* problemeHebdo,
ProblemeAResoudre->StatutDesContraintes);
char PremierPassage = OUI_ANTARES;

PROBLEME_SPX* ProbSpx = (PROBLEME_SPX*)(ProblemeAResoudre->ProblemesSpx->ProblemeSpx[(int)NumIntervalle]);
MPSolver* solver = (MPSolver*)(ProblemeAResoudre->ProblemesSpx->ProblemeSpx[(int)NumIntervalle]);
PROBLEME_SPX* ProbSpx
= (PROBLEME_SPX*)(ProblemeAResoudre->ProblemesSpx->ProblemeSpx[(int)NumIntervalle]);
MPSolver* solver
= (MPSolver*)(ProblemeAResoudre->ProblemesSpx->ProblemeSpx[(int)NumIntervalle]);

auto study = Data::Study::Current::Get();
bool ortoolsUsed = study->parameters.ortoolsUsed;
Expand Down Expand Up @@ -234,8 +237,17 @@ bool OPT_AppelDuSimplexe(PROBLEME_HEBDO* problemeHebdo,
{
solver = ORTOOLS_ConvertIfNeeded(&Probleme, solver);
}
const std::string filename = createMPSfilename(optPeriodStringGenerator, optimizationNumber);
mpsWriterFactory mps_writer_factory(problemeHebdo->ExportMPS, problemeHebdo->exportMPSOnError, optimizationNumber, &Probleme, ortoolsUsed, solver);

auto adqPatchOptNumber
Copy link
Contributor

@guilpier-code guilpier-code Mar 1, 2023

Choose a reason for hiding this comment

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

Following the oral talk we just had, I think that the MPS filename should not be computed here, but at a higher in the call stack.
Here this name should be passed as an argument.
See this comment

= getAdqPatchOptmizationNumber(problemeHebdo->adequacyPatchRuntimeData.get());
const std::string filename
= createMPSfilename(optPeriodStringGenerator, optimizationNumber, adqPatchOptNumber);
mpsWriterFactory mps_writer_factory(problemeHebdo->ExportMPS,
problemeHebdo->exportMPSOnError,
optimizationNumber,
&Probleme,
ortoolsUsed,
solver);
auto mps_writer = mps_writer_factory.create();
mps_writer->runIfNeeded(study->resultWriter, filename);

Expand Down Expand Up @@ -305,7 +317,7 @@ bool OPT_AppelDuSimplexe(PROBLEME_HEBDO* problemeHebdo,
logs.info() << " Solver: Safe resolution succeeded";
}

double *pt;
double* pt;
double CoutOpt = 0.0;

for (int i = 0; i < ProblemeAResoudre->NombreDeVariables; i++)
Expand Down Expand Up @@ -373,12 +385,14 @@ bool OPT_AppelDuSimplexe(PROBLEME_HEBDO* problemeHebdo,

void OPT_EcrireResultatFonctionObjectiveAuFormatTXT(
double optimalSolutionCost,
std::shared_ptr<OptPeriodStringGenerator> optPeriodStringGenerator,
int optimizationNumber)
OptPeriodStringGenerator& optPeriodStringGenerator,
int optimizationNumber,
const std::optional<unsigned int> optAdqPatchNumber)
{
Yuni::Clob buffer;
auto study = Data::Study::Current::Get();
auto filename = createCriterionFilename(optPeriodStringGenerator, optimizationNumber);
auto filename
= createCriterionFilename(optPeriodStringGenerator, optimizationNumber, optAdqPatchNumber);
auto writer = study->resultWriter;

logs.info() << "Solver Criterion File: `" << filename << "'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ void OPT_ConstruireLaMatriceDesContraintesDuProblemeLineaire(PROBLEME_HEBDO* pro
}

const auto filename = getFilenameWithExtension(
"variables", "txt", problemeHebdo->year, problemeHebdo->weekInTheYear, 0);
"variables", "txt", problemeHebdo->year, problemeHebdo->weekInTheYear);
OPT_ExportVariables(study->resultWriter, varname, filename);
}

Expand Down
12 changes: 6 additions & 6 deletions src/solver/optimisation/opt_fonctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#ifndef __SOLVER_OPTIMISATION_FUNCTIONS_H__
#define __SOLVER_OPTIMISATION_FUNCTIONS_H__

#include <optional>

#include "../config.h"
#include "opt_structure_probleme_a_resoudre.h"
#include "../simulation/sim_structure_donnees.h"
Expand Down Expand Up @@ -65,10 +67,7 @@ bool OPT_PilotageOptimisationQuadratique(PROBLEME_HEBDO*);
**
** \return True si l'operation s'est bien deroulee, false si le probleme n'a pas de solution
*/
bool OPT_AppelDuSimplexe(PROBLEME_HEBDO*,
int,
const int,
std::shared_ptr<OptPeriodStringGenerator>);
bool OPT_AppelDuSimplexe(PROBLEME_HEBDO*, int, const int, OptPeriodStringGenerator&);
void OPT_LiberationProblemesSimplexe(PROBLEME_HEBDO*);
bool OPT_OptimisationLineaire(PROBLEME_HEBDO*, uint);
void OPT_SauvegarderLesPmaxThermiques(PROBLEME_HEBDO*);
Expand All @@ -95,8 +94,9 @@ void OPT_AugmenterLaTailleDeLaMatriceDesContraintes(PROBLEME_ANTARES_A_RESOUDRE*
void OPT_LiberationMemoireDuProblemeAOptimiser(PROBLEME_HEBDO*);

void OPT_EcrireResultatFonctionObjectiveAuFormatTXT(double,
std::shared_ptr<OptPeriodStringGenerator>,
int);
OptPeriodStringGenerator&,
int,
const std::optional<unsigned int>);

/*------------------------------*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void setBoundsForUnsuppliedEnergy(PROBLEME_HEBDO* problemeHebdo,

// adq patch: update ENS <= DENS in 2nd run
if (problemeHebdo->adqPatchParams
&& problemeHebdo->adqPatchParams->AdequacyFirstStep == false
&& !problemeHebdo->adequacyPatchRuntimeData->LMR_FirstOptimization
&& problemeHebdo->adequacyPatchRuntimeData->areaMode[Pays]
== Data::AdequacyPatch::physicalAreaInsideAdqPatch)
Xmax[var] = std::min(
Expand Down
11 changes: 8 additions & 3 deletions src/solver/optimisation/opt_optimisation_lineaire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

#include "../simulation/simulation.h"
#include "../simulation/adequacy_patch_runtime_data.h"

#include "opt_fonctions.h"

Expand Down Expand Up @@ -110,16 +111,20 @@ bool OPT_OptimisationLineaire(PROBLEME_HEBDO* problemeHebdo, uint numSpace)
problemeHebdo->year);

if (!OPT_AppelDuSimplexe(
problemeHebdo, NumeroDeLIntervalle, optimizationNumber, optPeriodStringGenerator))
problemeHebdo, NumeroDeLIntervalle, optimizationNumber, *optPeriodStringGenerator))
Copy link
Contributor

@guilpier-code guilpier-code Mar 1, 2023

Choose a reason for hiding this comment

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

Here we should have (something like) :

auto mpsFileNames = MPSfileNames(problemeHebdo->OptimisationAuPasHebdomadaire,
                                 NumeroDeLIntervalle,
                                 problemeHebdo->adequacyPatchRuntimeData
                                 problemeHebdo->weekInTheYear,
                                 problemeHebdo->year)

if (!OPT_AppelDuSimplexe(problemeHebdo, 
                         NumeroDeLIntervalle, 
                         optimizationNumber, 
                         mpsFileNames->mps()))
        return false;

if (problemeHebdo->ExportMPS ......)
{
    ...
    OPT_EcrireResultatFonctionObjectiveAuFormatTXT(optimalSolutionCost,
                                                   mpsFileNames->criterion());
}

This would allow :

  • extracting filenames creation from functions that print into these files
  • calling getAdqPatchOptmizationNumber(...) only once.

return false;

if (problemeHebdo->ExportMPS != Data::mpsExportStatus::NO_EXPORT
|| problemeHebdo->Expansion == OUI_ANTARES)
{
double optimalSolutionCost
= OPT_ObjectiveFunctionResult(problemeHebdo, NumeroDeLIntervalle, optimizationNumber);
OPT_EcrireResultatFonctionObjectiveAuFormatTXT(
optimalSolutionCost, optPeriodStringGenerator, optimizationNumber);
auto adqPatchOptNumber
= getAdqPatchOptmizationNumber(problemeHebdo->adequacyPatchRuntimeData.get());
OPT_EcrireResultatFonctionObjectiveAuFormatTXT(optimalSolutionCost,
*optPeriodStringGenerator,
optimizationNumber,
adqPatchOptNumber);
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/solver/optimisation/opt_period_string_generator_base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "opt_period_string_generator_base.h"
#include <sstream>

std::string OptPeriodStringGenerator::createOptimizationFilename(
const std::string& title,
unsigned int optNumber,
const std::optional<unsigned int> adqPatchOptNumber,
const std::string& extension) const
{
std::ostringstream outputFile;
outputFile << title.c_str() << "-";
outputFile << this->to_string();
outputFile << "--optim-nb-";

// Special case: Local matching rule (LMR) in adequacy patch performs 2 weekly optimizations
if (adqPatchOptNumber)
outputFile << std::to_string(*adqPatchOptNumber) << "-";

outputFile << std::to_string(optNumber);
outputFile << "." << extension.c_str();
return outputFile.str();
}
5 changes: 5 additions & 0 deletions src/solver/optimisation/opt_period_string_generator_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#pragma once

#include <string>
#include <optional>

// ---------------------------------------------
// Optimization period as string : base class
Expand All @@ -37,4 +38,8 @@ class OptPeriodStringGenerator
public:
virtual std::string to_string() const = 0;
virtual ~OptPeriodStringGenerator() = default;
std::string createOptimizationFilename(const std::string& title,
Copy link
Contributor

Choose a reason for hiding this comment

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

Base class OptPeriodStringGenerator is an abstraction for the optimization period, that is either :

  • <year>-<week>
  • <year>-<week>-<day>

inside the filename.
So why do we have a new createOptimizationFilename function that take many arguments about the whole file name ?
I guess OptPeriodStringGenerator should concern the optimization period only.

unsigned int optNumber,
const std::optional<unsigned int> adqPatchOptNumber,
const std::string& extension) const;
};
7 changes: 7 additions & 0 deletions src/solver/simulation/adequacy_patch_runtime_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ AdequacyPatchRuntimeData::AdequacyPatchRuntimeData(
from->thermal.unsuppliedEnergyCost, with->thermal.unsuppliedEnergyCost);
}
}

std::optional<unsigned int> getAdqPatchOptmizationNumber(AdequacyPatchRuntimeData* data)
{
if (!data)
return std::nullopt;
return data->LMR_FirstOptimization ? 1 : 2;
}
5 changes: 5 additions & 0 deletions src/solver/simulation/adequacy_patch_runtime_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <antares/study/study.h>
#include <vector>
#include <set>
#include <optional>

class AdequacyPatchRuntimeData
{
Expand All @@ -42,6 +43,8 @@ class AdequacyPatchRuntimeData
AdequacyPatchRuntimeData(const Antares::Data::AreaList& areas,
const std::vector<Antares::Data::AreaLink*>& links);

bool LMR_FirstOptimization;

std::vector<adqPatchParamsMode> areaMode;
std::vector<adqPatchParamsMode> originAreaMode;
std::vector<adqPatchParamsMode> extremityAreaMode;
Expand All @@ -50,3 +53,5 @@ class AdequacyPatchRuntimeData
bool wasCSRTriggeredAtAreaHour(int area, int hour) const;
void addCSRTriggeredAtAreaHour(int area, int hour);
};

std::optional<unsigned int> getAdqPatchOptmizationNumber(AdequacyPatchRuntimeData* data);
1 change: 0 additions & 1 deletion src/solver/simulation/sim_calcul_economique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ void SIM_InitialisationProblemeHebdo(Data::Study& study,
if (parameters.adqPatch.enabled)
{
problem.adqPatchParams = std::make_shared<AdequacyPatchParameters>();
// AdequacyFirstStep will be initialized during the economy solve
problem.adqPatchParams->SetNTCOutsideToInsideToZero
= parameters.adqPatch.localMatching.setToZeroOutsideInsideLinks;
problem.adqPatchParams->SetNTCOutsideToOutsideToZero
Expand Down
1 change: 0 additions & 1 deletion src/solver/simulation/sim_structure_probleme_economique.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ typedef struct

struct AdequacyPatchParameters
{
bool AdequacyFirstStep;
bool SetNTCOutsideToInsideToZero;
bool SetNTCOutsideToOutsideToZero;
bool IncludeHurdleCostCsr;
Expand Down
38 changes: 11 additions & 27 deletions src/solver/utils/filename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,31 @@ std::shared_ptr<OptPeriodStringGenerator> createOptPeriodAsString(bool isOptimiz
return std::make_shared<OptDailyStringGenerator>(day, week, year);
}

std::string createOptimizationFilename(
const std::string& title,
std::shared_ptr<OptPeriodStringGenerator> optPeriodStringGenerator,
unsigned int optNumber,
const std::string& extension)
std::string createCriterionFilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber,
const std::optional<unsigned int> adqPatchOptNumber)
{
std::ostringstream outputFile;
outputFile << title.c_str() << "-";
outputFile << optPeriodStringGenerator->to_string();
outputFile << "--optim-nb-" << std::to_string(optNumber);
outputFile << "." << extension.c_str();

return outputFile.str();
return optPeriodStringGenerator.createOptimizationFilename(
"criterion", optNumber, adqPatchOptNumber, "txt");
}

std::string createCriterionFilename(
std::shared_ptr<OptPeriodStringGenerator> optPeriodStringGenerator,
const unsigned int optNumber)
std::string createMPSfilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber,
const std::optional<unsigned int> adqPatchNumber)
{
return createOptimizationFilename("criterion", optPeriodStringGenerator, optNumber, "txt");
}

std::string createMPSfilename(std::shared_ptr<OptPeriodStringGenerator> optPeriodStringGenerator,
const unsigned int optNumber)
{
return createOptimizationFilename("problem", optPeriodStringGenerator, optNumber, "mps");
return optPeriodStringGenerator.createOptimizationFilename(
"problem", optNumber, adqPatchNumber, "mps");
}
// TODO[FOM] Remove this function
std::string getFilenameWithExtension(const YString& prefix,
const YString& extension,
uint year,
uint week,
uint optNumber)
uint week)
{
std::ostringstream outputFile;
outputFile << prefix.c_str() << "-" << std::to_string(year + 1) << "-"
<< std::to_string(week + 1);

if (optNumber)
outputFile << "--optim-nb-" << std::to_string(optNumber);

outputFile << "." << extension.c_str();
return outputFile.str();
}
17 changes: 10 additions & 7 deletions src/solver/utils/filename.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

#include <string>
#include <memory>
#include <optional>
#include "../optimisation/opt_period_string_generator_base.h"
#include <yuni/core/string.h> // TODO[FOM] Remove this include

// TODO[FOM] Remove this function
std::string getFilenameWithExtension(const YString& prefix,
const YString& extension,
uint year,
uint week,
uint optNumber);
uint week);

std::shared_ptr<OptPeriodStringGenerator> createOptPeriodAsString(bool isOptimizationWeekly,
unsigned int day,
unsigned int week,
unsigned int year);

std::string createCriterionFilename(
std::shared_ptr<OptPeriodStringGenerator> optPeriodStringGenerator,
const unsigned int optNumber);
std::string createMPSfilename(std::shared_ptr<OptPeriodStringGenerator> optPeriodStringGenerator,
const unsigned int optNumber);
std::string createCriterionFilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber,
const std::optional<unsigned int> adqPatchOptNumber
= std::nullopt);

std::string createMPSfilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber,
const std::optional<unsigned int> adqPatchOptNumber = std::nullopt);
7 changes: 4 additions & 3 deletions src/tests/src/solver/optimisation/adequacy_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ std::pair<double, double> setNTCboundsForOneTimeStep(AdequacyPatchMode originTyp

problem.adequacyPatchRuntimeData->originAreaMode[0] = originType;
problem.adequacyPatchRuntimeData->extremityAreaMode[0] = extremityType;
problem.adqPatchParams
= std::unique_ptr<AdequacyPatchParameters>(new AdequacyPatchParameters());
problem.adequacyPatchRuntimeData->LMR_FirstOptimization = true;
problem.adqPatchParams = std::make_shared<AdequacyPatchParameters>();

auto& adqPatchParams = problem.adqPatchParams;

adqPatchParams->AdequacyFirstStep = true;

adqPatchParams->SetNTCOutsideToOutsideToZero = SetNTCOutsideToOutsideToZero;
adqPatchParams->SetNTCOutsideToInsideToZero = SetNTCOutsideToInsideToZero;

Expand Down