Skip to content

Commit

Permalink
Write raw optimization results [ANT-2302] (#2565)
Browse files Browse the repository at this point in the history
Allow the user to export solutions through new boolean option
`include-export-solutions` (default=`false`).

---------

Co-authored-by: sylvmara <symarandon@gmail.com>
  • Loading branch information
flomnes and sylvmara authored Jan 9, 2025
1 parent 4cb902d commit ad2140f
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/user-guide/solver/static-modeler/04-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,18 @@ _**This section is under construction**_

> _**Note:**_ You can find more information on this parameter [here](../03-appendix.md#details-on-the-include-exportmps-parameter).
---
#### include-export-solutions
- **Expected value:** `true` or `false`
- **Required:** no
- **Default value:** `false`
- **Usage:** set to `true` to activate writing the raw optimization results, that is
- For each variable, optimal values (saved in output/output-name/optimal-values-y-w--optim-nb-z.txt) and reduced costs (saved in output/output-name/reduced-costs-y-w--optim-nb-z.txt)
- each constraint, the marginal cost is saved in output/output-name/marinal-costs-y-w--optim-nb-z.txt
where y is the year number (starting from 1), w is the week number (starting from 1) and z is the optimization index (1 or 2).

This is an advanced option intended to help developers and advanced users better understand their simulation results.

---
#### include-split-exported-mps
[//]: # (TODO: document this parameter, seems to belong to another category)
Expand Down
1 change: 1 addition & 0 deletions src/libs/antares/study/include/antares/study/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ class Parameters final
//! Enum to define unfeasible problem behavior \see UnfeasibleProblemBehavior
UnfeasibleProblemBehavior unfeasibleProblemBehavior;

bool exportSolutions;
} include;

struct Compatibility
Expand Down
12 changes: 12 additions & 0 deletions src/libs/antares/study/parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ void Parameters::reset()

include.exportMPS = mpsExportStatus::NO_EXPORT;
include.exportStructure = false;
include.exportSolutions = false;
namedProblems = false;

include.unfeasibleProblemBehavior = UnfeasibleProblemBehavior::ERROR_MPS;
Expand Down Expand Up @@ -721,6 +722,11 @@ static bool SGDIntLoadFamily_Optimization(Parameters& d,
return true;
}

if (key == "include-export-solutions")
{
return value.to<bool>(d.include.exportSolutions);
}

if (key == "include-exportstructure")
{
return value.to<bool>(d.include.exportStructure);
Expand Down Expand Up @@ -1769,6 +1775,10 @@ void Parameters::prepareForSimulation(const StudyLoadOptions& options)
{
logs.info() << " :: ignoring hurdle costs";
}
if (!include.exportSolutions)
{
logs.info() << " :: ignoring solution export";
}

logs.info() << " :: solver " << options.optOptions.ortoolsSolver
<< " is used for problem resolution";
Expand Down Expand Up @@ -1888,7 +1898,9 @@ void Parameters::saveToINI(IniFile& ini) const
section->add("include-primaryreserve", include.reserve.primary);

section->add("include-exportmps", mpsExportStatusToString(include.exportMPS));

section->add("include-exportstructure", include.exportStructure);
section->add("include-export-solutions", include.exportSolutions);

// Unfeasible problem behavior
section->add("include-unfeasible-problem-behavior",
Expand Down
39 changes: 39 additions & 0 deletions src/solver/optimisation/opt_optimisation_lineaire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ void OPT_EcrireResultatFonctionObjectiveAuFormatTXT(
writer.addEntryFromBuffer(filename, buffer);
}

void OPT_WriteSolution(const PROBLEME_ANTARES_A_RESOUDRE& pb,
const OptPeriodStringGenerator& optPeriodStringGenerator,
int optimizationNumber,
Solver::IResultWriter& writer)
{
Yuni::Clob buffer;
auto filename = createSolutionFilename(optPeriodStringGenerator, optimizationNumber);
for (int var = 0; var < pb.NombreDeVariables; var++)
{
buffer.appendFormat("%s\t%11.10e\n", pb.NomDesVariables[var].c_str(), pb.X[var]);
}
writer.addEntryFromBuffer(filename, buffer);
buffer.clear();

filename = createMarginalCostFilename(optPeriodStringGenerator, optimizationNumber);
for (unsigned int cont = 0; cont < pb.NombreDeContraintes; ++cont)
{
buffer.appendFormat("%s\t%11.10e\n",
pb.NomDesContraintes[cont].c_str(),
pb.CoutsMarginauxDesContraintes[cont]);
}
writer.addEntryFromBuffer(filename, buffer);
buffer.clear();

filename = createReducedCostFilename(optPeriodStringGenerator, optimizationNumber);
for (unsigned int var = 0; var < pb.NombreDeVariables; ++var)
{
buffer.appendFormat("%s\t%11.10e\n", pb.NomDesVariables[var].c_str(), pb.CoutsReduits[var]);
}
writer.addEntryFromBuffer(filename, buffer);
}

namespace
{
void notifyProblemHebdo(const PROBLEME_HEBDO* problemeHebdo,
Expand Down Expand Up @@ -141,6 +173,13 @@ bool runWeeklyOptimization(const OptimizationOptions& options,
optimizationNumber,
writer);
}
if (problemeHebdo->exportSolutions)
{
OPT_WriteSolution(*problemeHebdo->ProblemeAResoudre,
*optPeriodStringGenerator,
optimizationNumber,
writer);
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ struct PROBLEME_HEBDO
bool exportMPSOnError = false;
bool ExportStructure = false;
bool NamedProblems = false;
bool exportSolutions = false;

uint32_t HeureDansLAnnee = 0;
bool LeProblemeADejaEteInstancie = false;
Expand Down
1 change: 1 addition & 0 deletions src/solver/simulation/sim_calcul_economique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ void SIM_InitialisationProblemeHebdo(Data::Study& study,
problem.NombreDeContraintesCouplantes = activeConstraints.size();

problem.ExportMPS = study.parameters.include.exportMPS;
problem.exportSolutions = study.parameters.include.exportSolutions;
problem.ExportStructure = study.parameters.include.exportStructure;
problem.NamedProblems = study.parameters.namedProblems;
problem.exportMPSOnError = Data::exportMPS(parameters.include.unfeasibleProblemBehavior);
Expand Down
18 changes: 18 additions & 0 deletions src/solver/utils/filename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ std::string createMPSfilename(const OptPeriodStringGenerator& optPeriodStringGen
{
return createOptimizationFilename("problem", optPeriodStringGenerator, optNumber, "mps");
}

std::string createSolutionFilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber)
{
return createOptimizationFilename("optimal-values", optPeriodStringGenerator, optNumber, "txt");
}

std::string createMarginalCostFilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber)
{
return createOptimizationFilename("marginal-costs", optPeriodStringGenerator, optNumber, "txt");
}

std::string createReducedCostFilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber)
{
return createOptimizationFilename("reduced-costs", optPeriodStringGenerator, optNumber, "txt");
}
10 changes: 10 additions & 0 deletions src/solver/utils/include/antares/solver/utils/filename.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,15 @@ std::shared_ptr<OptPeriodStringGenerator> createOptPeriodAsString(bool isOptimiz

std::string createCriterionFilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber);

std::string createMPSfilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber);

std::string createSolutionFilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber);

std::string createMarginalCostFilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber);

std::string createReducedCostFilename(const OptPeriodStringGenerator& optPeriodStringGenerator,
const unsigned int optNumber);

0 comments on commit ad2140f

Please sign in to comment.