Skip to content

Commit

Permalink
Write update times (#1789)
Browse files Browse the repository at this point in the history
* Save update times for optimization problems

* Create solver.utils.cpp

* Add individual update times

* [DEV] updateTime type, const func, remove constructor costStatistics

* [DEV] add tempsUpdate2

* [DEV] Remove useless include

* [DEV] change tempsUpdateName

* [DEV] add struct TIME_MEASURE

* [DEV] use double for update times

* [DEV] remove Yuni Clob

* [DEV] remove vector with single value

* [DEV] remove Yuni clob from opt time writer

* [DEV] replace type with long

* [DEV] code smells

* [DEV] remaned solver.utils into solver_utils

* [FIX] Compile

* [FIX] remove redundants public

* [DEV] Use std::round instead of recoded function

* [DEV] remove round function from header

* Refactor update time (#1820)

* Reformat

* Step 1 : de-duplicate

* Step 2 : use TIME_MEASURE inside SimplexResult

---------

Co-authored-by: Vincent Payet <[email protected]>
  • Loading branch information
flomnes and payetvin authored Dec 13, 2023
1 parent 2dc9169 commit e004c82
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 224 deletions.
36 changes: 21 additions & 15 deletions src/solver/optimisation/opt_appel_solveur_lineaire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TimeMeasurement
end_ = clock::now();
}

long long duration_ms() const
long duration_ms() const
{
return std::chrono::duration_cast<std::chrono::milliseconds>(end_ - start_).count();
}
Expand All @@ -93,7 +93,7 @@ class TimeMeasurement
struct SimplexResult
{
bool success = false;
long long solveTime = 0;
TIME_MEASURE timeMeasure;
mpsWriterFactory mps_writer_factory;
};

Expand All @@ -115,7 +115,7 @@ static SimplexResult OPT_TryToCallSimplex(
const int opt = optimizationNumber - 1;
assert(opt >= 0 && opt < 2);
OptimizationStatistics& optimizationStatistics = problemeHebdo->optimizationStatistics[opt];

TIME_MEASURE timeMeasure;
if (!PremierPassage)
{
ProbSpx = nullptr;
Expand Down Expand Up @@ -151,7 +151,7 @@ static SimplexResult OPT_TryToCallSimplex(
Probleme.Contexte = BRANCH_AND_BOUND_OU_CUT_NOEUD;
Probleme.BaseDeDepartFournie = UTILISER_LA_BASE_DU_PROBLEME_SPX;

TimeMeasurement measure;
TimeMeasurement updateMeasure;
if (options.useOrtools)
{
ORTOOLS_ModifierLeVecteurCouts(
Expand All @@ -175,8 +175,9 @@ static SimplexResult OPT_TryToCallSimplex(
ProblemeAResoudre->Sens.data(),
ProblemeAResoudre->NombreDeContraintes);
}
measure.tick();
optimizationStatistics.addUpdateTime(measure.duration_ms());
updateMeasure.tick();
timeMeasure.updateTime = updateMeasure.duration_ms();
optimizationStatistics.addUpdateTime(timeMeasure.updateTime);
}
}

Expand Down Expand Up @@ -256,8 +257,8 @@ static SimplexResult OPT_TryToCallSimplex(
}
}
measure.tick();
long long solveTime = measure.duration_ms();
optimizationStatistics.addSolveTime(solveTime);
timeMeasure.solveTime = measure.duration_ms();
optimizationStatistics.addSolveTime(timeMeasure.solveTime);

ProblemeAResoudre->ExistenceDUneSolution = Probleme.ExistenceDUneSolution;
if (ProblemeAResoudre->ExistenceDUneSolution != OUI_SPX && PremierPassage)
Expand All @@ -280,15 +281,17 @@ static SimplexResult OPT_TryToCallSimplex(
{
logs.info() << " solver: resetting";
}
return {.success=false, .solveTime=solveTime, .mps_writer_factory=mps_writer_factory};
return {.success=false, .timeMeasure=timeMeasure,
.mps_writer_factory=mps_writer_factory};
}

else
{
throw FatalError("Internal error: insufficient memory");
}
}
return {.success=true, .solveTime=solveTime, .mps_writer_factory=mps_writer_factory};
return {.success=true, .timeMeasure=timeMeasure,
.mps_writer_factory=mps_writer_factory};
}

bool OPT_AppelDuSimplexe(const OptimizationOptions& options,
Expand All @@ -309,7 +312,7 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options,

bool PremierPassage = true;

struct SimplexResult simplexResult =
SimplexResult simplexResult =
OPT_TryToCallSimplex(options, problemeHebdo, Probleme, NumIntervalle, optimizationNumber,
optPeriodStringGenerator, PremierPassage, writer);

Expand All @@ -320,8 +323,6 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options,
optPeriodStringGenerator, PremierPassage, writer);
}

long long solveTime = simplexResult.solveTime;

if (ProblemeAResoudre->ExistenceDUneSolution == OUI_SPX)
{
if (!PremierPassage)
Expand All @@ -345,15 +346,20 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options,
*pt = ProblemeAResoudre->CoutsReduits[i];
}

{
const int opt = optimizationNumber - 1;
assert(opt >= 0 && opt < 2);
problemeHebdo->timeMeasure[opt] = simplexResult.timeMeasure;
}

// TODO remove this if..else
if (optimizationNumber == PREMIERE_OPTIMISATION)
{
problemeHebdo->coutOptimalSolution1[NumIntervalle] = CoutOpt;
problemeHebdo->tempsResolution1[NumIntervalle] = solveTime;
}
else
{
problemeHebdo->coutOptimalSolution2[NumIntervalle] = CoutOpt;
problemeHebdo->tempsResolution2[NumIntervalle] = solveTime;
}
for (int Cnt = 0; Cnt < ProblemeAResoudre->NombreDeContraintes; Cnt++)
{
Expand Down
3 changes: 2 additions & 1 deletion src/solver/simulation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ set(SRC_SIMULATION


# Solver
solver.utils.h
solver_utils.h
solver_utils.cpp
solver.h
solver.hxx
solver.data.h
Expand Down
4 changes: 1 addition & 3 deletions src/solver/simulation/adequacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,7 @@ bool Adequacy::year(Progression::Task& progression,

pProblemesHebdo[numSpace].firstWeekOfSimulation = false;

optWriter.addTime(w,
pProblemesHebdo[numSpace].tempsResolution1[0],
pProblemesHebdo[numSpace].tempsResolution2[0]);
optWriter.addTime(w, pProblemesHebdo[numSpace].timeMeasure);

++progression;
}
Expand Down
6 changes: 5 additions & 1 deletion src/solver/simulation/common-eco-adq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,14 @@ void finalizeOptimizationStatistics(PROBLEME_HEBDO& problem,
{
auto& firstOptStat = problem.optimizationStatistics[0];
state.averageOptimizationTime1 = firstOptStat.getAverageSolveTime();
firstOptStat.reset();

auto& secondOptStat = problem.optimizationStatistics[1];
state.averageOptimizationTime2 = secondOptStat.getAverageSolveTime();

state.averageUpdateTime
= firstOptStat.getAverageUpdateTime() + secondOptStat.getAverageUpdateTime();

firstOptStat.reset();
secondOptStat.reset();
}

Expand Down
4 changes: 1 addition & 3 deletions src/solver/simulation/economy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ bool Economy::year(Progression::Task& progression,
state.optimalSolutionCost1 += pProblemesHebdo[numSpace].coutOptimalSolution1[opt];
state.optimalSolutionCost2 += pProblemesHebdo[numSpace].coutOptimalSolution2[opt];
}
optWriter.addTime(w,
pProblemesHebdo[numSpace].tempsResolution1[0],
pProblemesHebdo[numSpace].tempsResolution2[0]);
optWriter.addTime(w, pProblemesHebdo[numSpace].timeMeasure);
}
catch (Data::AssertionError& ex)
{
Expand Down
21 changes: 13 additions & 8 deletions src/solver/simulation/opt_time_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,34 @@
** SPDX-License-Identifier: licenceRef-GPL3_WITH_RTE-Exceptions
*/
#include "opt_time_writer.h"
#include <filesystem>

OptimizationStatisticsWriter::OptimizationStatisticsWriter(
Antares::Solver::IResultWriter& writer,
uint year) :
pWriter(writer)
pYear(year), pWriter(writer)
{
printHeader();
#define SEP Yuni::IO::Separator
pFilename << "debug" << SEP << "solver" << SEP << "weeksSolveTimes_" << year << ".txt";
#undef SEP
}

void OptimizationStatisticsWriter::printHeader()
{
pBuffer << "# Week Optimization_1_ms Optimization_2_ms\n";
pBuffer << "# Week Optimization_1_ms Optimization_2_ms Update_ms1 Update_ms2\n";
}

void OptimizationStatisticsWriter::addTime(uint week, double opt_1_ms, double opt_2_ms)
void OptimizationStatisticsWriter::addTime(uint week, const TIME_MEASURES& timeMeasure)
{
pBuffer << week << " " << opt_1_ms << " " << opt_2_ms << "\n";
pBuffer << week
<< " " << timeMeasure[0].solveTime
<< " " << timeMeasure[1].solveTime
<< " " << timeMeasure[0].updateTime
<< " " << timeMeasure[1].updateTime <<"\n";
}

void OptimizationStatisticsWriter::finalize()
{
pWriter.addEntryFromBuffer(pFilename.c_str(), pBuffer);
using path = std::filesystem::path;
const path filename = path("optimization") / "week-by-week" / ("year_" + std::to_string(pYear) + ".txt");
std::string s = pBuffer.str();
pWriter.addEntryFromBuffer(filename.string(), s);
}
9 changes: 5 additions & 4 deletions src/solver/simulation/opt_time_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@
** SPDX-License-Identifier: licenceRef-GPL3_WITH_RTE-Exceptions
*/
#pragma once
#include <yuni/core/string.h>
#include <sstream>
#include <antares/writer/i_writer.h>

#include "sim_structure_probleme_economique.h"
#include "simulation.h"

class OptimizationStatisticsWriter
{
public:
void addTime(uint week, const TIME_MEASURES& timeMeasure);
OptimizationStatisticsWriter(Antares::Solver::IResultWriter& writer, uint year);
void addTime(uint week, double opt_1_ms, double opt_2_ms);
void finalize();

private:
void printHeader();
Yuni::Clob pBuffer;
Yuni::String pFilename;
std::ostringstream pBuffer;
uint pYear;
Antares::Solver::IResultWriter& pWriter;
};
4 changes: 0 additions & 4 deletions src/solver/simulation/sim_alloc_probleme_hebdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ void SIM_AllocationProblemeDonneesGenerales(PROBLEME_HEBDO& problem,

problem.coutOptimalSolution1.assign(7, 0.);
problem.coutOptimalSolution2.assign(7, 0.);

problem.tempsResolution1.assign(7, 0.);
problem.tempsResolution2.assign(7, 0.);
}

void SIM_AllocationProblemePasDeTemps(PROBLEME_HEBDO& problem,
Expand Down Expand Up @@ -523,4 +520,3 @@ void SIM_AllocateAreas(PROBLEME_HEBDO& problem,
}
}
}

2 changes: 0 additions & 2 deletions src/solver/simulation/sim_calcul_economique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,6 @@ void SIM_RenseignementProblemeHebdo(const Study& study,
{
problem.coutOptimalSolution1[opt] = 0.;
problem.coutOptimalSolution2[opt] = 0.;
problem.tempsResolution1[opt] = 0.;
problem.tempsResolution2[opt] = 0.;
}

for (uint k = 0; k < studyruntime.interconnectionsCount(); ++k)
Expand Down
10 changes: 8 additions & 2 deletions src/solver/simulation/sim_structure_probleme_economique.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,16 @@ struct COUTS_DE_TRANSPORT

std::vector<double> CoutDeTransportOrigineVersExtremiteRef;
std::vector<double> CoutDeTransportExtremiteVersOrigineRef;
};

struct TIME_MEASURE
{
long solveTime = 0;
long updateTime = 0;
};

using TIME_MEASURES = std::array<TIME_MEASURE, 2>;

struct VARIABLES_DUALES_INTERCONNEXIONS
{
std::vector<double> VariableDualeParInterconnexion;
Expand Down Expand Up @@ -597,8 +604,7 @@ struct PROBLEME_HEBDO
std::vector<double> coutOptimalSolution1;
std::vector<double> coutOptimalSolution2;

std::vector<double> tempsResolution1;
std::vector<double> tempsResolution2;
TIME_MEASURES timeMeasure;

/* Unused for now, will be used in future revisions */
#if 0
Expand Down
5 changes: 2 additions & 3 deletions src/solver/simulation/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@

#include <antares/study/study.h>
#include <antares/logs/logs.h>
#include <antares/study/fwd.h> // PowerFluctuations
#include <antares/benchmarking/DurationCollector.h>

#include <yuni/core/string.h>
#include <yuni/job/queue/service.h>
#include "../variable/state.h"
#include "antares/solver/misc/options.h"
#include "solver.data.h"
#include "solver.utils.h"
#include "solver_utils.h"
#include "../hydro/management/management.h"

#include <antares/writer/writer_factory.h>
Expand Down Expand Up @@ -164,7 +163,7 @@ class ISimulation : public Impl
bool pFirstSetParallelWithAPerformedYearWasRun;

//! Statistics about annual (system and solution) costs
annualCostsStatistics pAnnualCostsStatistics;
annualCostsStatistics pAnnualStatistics;

// Collecting durations inside the simulation
Benchmarking::IDurationCollector& pDurationCollector;
Expand Down
20 changes: 10 additions & 10 deletions src/solver/simulation/solver.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -894,13 +894,13 @@ void ISimulation<Impl>::computeAnnualCostsStatistics(
{
// Get space number associated to the performed year
uint numSpace = set_it->performedYearToSpace[y];
pAnnualCostsStatistics.systemCost.addCost(state[numSpace].annualSystemCost);
pAnnualCostsStatistics.criterionCost1.addCost(state[numSpace].optimalSolutionCost1);
pAnnualCostsStatistics.criterionCost2.addCost(state[numSpace].optimalSolutionCost2);
pAnnualCostsStatistics.optimizationTime1.addCost(
state[numSpace].averageOptimizationTime1);
pAnnualCostsStatistics.optimizationTime2.addCost(
state[numSpace].averageOptimizationTime2);
const Variable::State& s = state[numSpace];
pAnnualStatistics.systemCost.addCost(s.annualSystemCost);
pAnnualStatistics.criterionCost1.addCost(s.optimalSolutionCost1);
pAnnualStatistics.criterionCost2.addCost(s.optimalSolutionCost2);
pAnnualStatistics.optimizationTime1.addCost(s.averageOptimizationTime1);
pAnnualStatistics.optimizationTime2.addCost(s.averageOptimizationTime2);
pAnnualStatistics.updateTime.addCost(s.averageUpdateTime);
}
}
}
Expand Down Expand Up @@ -940,7 +940,7 @@ void ISimulation<Impl>::loopThroughYears(uint firstYear,
uint maxNbYearsPerformedInAset
= buildSetsOfParallelYears(firstYear, endYear, setsOfParallelYears);
// Related to annual costs statistics (printed in output into separate files)
pAnnualCostsStatistics.setNbPerformedYears(pNbYearsReallyPerformed);
pAnnualStatistics.setNbPerformedYears(pNbYearsReallyPerformed);

// Container for random numbers of parallel years (to be executed or not)
randomNumbers randomForParallelYears(maxNbYearsPerformedInAset,
Expand Down Expand Up @@ -1047,8 +1047,8 @@ void ISimulation<Impl>::loopThroughYears(uint firstYear,
} // End loop over sets of parallel years

// Writing annual costs statistics
pAnnualCostsStatistics.endStandardDeviations();
pAnnualCostsStatistics.writeToOutput(pResultWriter);
pAnnualStatistics.endStandardDeviations();
pAnnualStatistics.writeToOutput(pResultWriter);
}

} // namespace Antares::Solver::Simulation
Expand Down
Loading

0 comments on commit e004c82

Please sign in to comment.