Skip to content
Draft
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
138 changes: 127 additions & 11 deletions src/solver/optimisation/opt_appel_solveur_lineaire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: MPL-2.0

#include <mutex>
#include <optional>
#include <string_view>

#include <antares/antares/fatal-error.h>
#include <antares/logs/logs.h>
Expand Down Expand Up @@ -55,6 +57,112 @@ static void logProblemSize(const MPSolver* mpSolver)
logs.info();
}

namespace
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes in this file are good, you integrate legacy variables in the simulation table.
Let’s discuss on this in dedicated pr

constexpr std::string_view kLegacyNameSeparator = "::";
constexpr std::string_view kLegacyHourTag = "hour";

struct LegacyVariableInfo
{
std::string output;
std::string component;
unsigned timeIndex;
};

std::optional<LegacyVariableInfo> parseLegacyVariableName(const std::string& name)
{
const auto firstSep = name.find(kLegacyNameSeparator);
const auto lastSep = name.rfind(kLegacyNameSeparator);
if (firstSep == std::string::npos || lastSep == std::string::npos || firstSep == lastSep)
{
return std::nullopt;
}

const std::string output = name.substr(0, firstSep);
const std::string timePart = name.substr(lastSep + kLegacyNameSeparator.size());
const std::string location = name.substr(firstSep + kLegacyNameSeparator.size(),
lastSep - firstSep - kLegacyNameSeparator.size());
if (output.empty() || timePart.empty())
{
return std::nullopt;
}

const auto lt = timePart.find('<');
const auto gt = timePart.rfind('>');
if (lt == std::string::npos || gt == std::string::npos || gt <= lt + 1)
{
return std::nullopt;
}

const std::string unit = timePart.substr(0, lt);
if (unit != kLegacyHourTag)
{
return std::nullopt;
}

const std::string value = timePart.substr(lt + 1, gt - lt - 1);
unsigned timeIndex = 0;
try
{
timeIndex = static_cast<unsigned>(std::stoul(value));
}
catch (const std::exception&)
{
return std::nullopt;
}

return LegacyVariableInfo{output, location, timeIndex};
}

void FillLegacySimulationTable(ISimulationTable& simulationTable,
const PROBLEME_ANTARES_A_RESOUDRE& problem,
const FillContext& fillContext,
unsigned currentBlock,
unsigned scenario)
{
const unsigned globalFirstTimeStep = fillContext.getGlobalFirstTimeStep();
const unsigned globalLastTimeStep = fillContext.getGlobalLastTimeStep();
const unsigned int block = currentBlock + 1;

for (int index = 0; index < problem.NombreDeVariables; ++index)
{
if (index < 0 || index >= static_cast<int>(problem.NomDesVariables.size())
|| index >= static_cast<int>(problem.X.size()))
{
continue;
}

const auto& name = problem.NomDesVariables[index];
if (name.empty())
{
continue;
}

const auto parsed = parseLegacyVariableName(name);
if (!parsed)
{
continue;
}

std::optional<unsigned> blockTimeIndex;
if (parsed->timeIndex >= globalFirstTimeStep && parsed->timeIndex <= globalLastTimeStep)
{
blockTimeIndex = parsed->timeIndex - globalFirstTimeStep + 1;
}

simulationTable.addEntry(
{.block = block,
.component = parsed->component,
.output = parsed->output,
.absolute_time_index = parsed->timeIndex + 1,
.block_time_index = blockTimeIndex,
.scenario_index = scenario,
.value = problem.X[static_cast<std::size_t>(index)],
.status = std::nullopt});
}
}
} // namespace

static void fillModelerComponents(
std::vector<std::unique_ptr<LinearProblemFiller>>& fillersCollection,
Solver::ModelerData* modelerData,
Expand Down Expand Up @@ -225,7 +333,7 @@ static SimplexResult OPT_TryToCallSimplex(const SingleOptimOptions& options,
throw FatalError("Internal error: insufficient memory");
}

if (simulationTable && modelerData)
if (simulationTable)
{
unsigned currentBlock = problemeHebdo->OptimisationAuPasHebdomadaire
? problemeHebdo->weekInTheYear
Expand All @@ -234,16 +342,24 @@ static SimplexResult OPT_TryToCallSimplex(const SingleOptimOptions& options,
? TimeConversionMode::WeeklyBlocks
: TimeConversionMode::DailyBlocks;
measure.reset();
FillSimulationTable(*simulationTable,
ortoolsProblem,
::getObjectiveValue(solver.get()),

*modelerData,
optimEntityContainer,
fillCtx,
currentBlock,
timeConversionMode,
true);
if (modelerData)
{
FillSimulationTable(*simulationTable,
ortoolsProblem,
::getObjectiveValue(solver.get()),
*modelerData,
optimEntityContainer,
fillCtx,
currentBlock,
timeConversionMode,
true);
}

FillLegacySimulationTable(*simulationTable,
*ProblemeAResoudre,
fillCtx,
currentBlock,
fillCtx.getYear());

measure.tick();
timeMeasure.simulationTableFillTime = measure.duration_ms();
Expand Down
13 changes: 9 additions & 4 deletions src/solver/simulation/adequacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ std::string Adequacy::getSimulationTableHeader() const
return "";
}

void Adequacy::writeSimulationTable(uint numSpace)
{
if (!simulationTables_.empty() && numSpace < simulationTables_.size())
{
simulationTables_[numSpace].write();
}
}

// valGen maybe_unused to match simulationBegin() declaration in economy.cpp
bool Adequacy::simulationBegin()
{
Expand Down Expand Up @@ -209,10 +217,7 @@ bool Adequacy::year(Progression::Task& progression,
resultWriter,
simulationObserver_.get(),
currentSimTable);
if (currentSimTable)
{
currentSimTable->write();
}
writeSimulationTable(numSpace);

RemixHydroForAllAreas(study.areas,
currentProblem,
Expand Down
13 changes: 8 additions & 5 deletions src/solver/simulation/economy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ std::string Economy::getSimulationTableHeader() const
return "";
}

void Economy::writeSimulationTable(uint numSpace)
{
if (!simulationTables_.empty() && numSpace < simulationTables_.size())
{
simulationTables_[numSpace].write();
}
}

bool Economy::simulationBegin()
{
if (!preproOnly)
Expand Down Expand Up @@ -150,14 +158,9 @@ bool Economy::year(Progression::Task& progression,
hourInTheYear,
randomForYear.pThermalNoisesByArea,
state.year);
auto* currentSimTable = simulationTables_.empty() ? nullptr : &simulationTables_[numSpace];
try
{
weeklyOptProblems_[numSpace].solve();
if (currentSimTable)
{
currentSimTable->write();
}
// Runs all the post processes in the list of post-process commands
optRuntimeData opt_runtime_data(state.year, w, hourInTheYear);
postProcessesList_[numSpace]->runAll(opt_runtime_data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Adequacy

OptimisationsSimulationTable& getSimulationTable(uint numSpace);

void writeSimulationTable(uint numSpace);

private:
bool simplexIsRequired(uint hourInTheYear,
uint numSpace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Economy
void initializeState(Variable::State& state, uint numSpace);
OptimisationsSimulationTable& getSimulationTable(uint numSpace);
std::string getSimulationTableHeader() const;
void writeSimulationTable(uint numSpace);

private:
uint pNbWeeks;
Expand Down
23 changes: 13 additions & 10 deletions src/solver/simulation/include/antares/solver/simulation/solver.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "antares/solver/simulation/timeseries-numbers.h"
#include "antares/solver/ts-generator/generator.h"
#include "antares/solver/variable/print.h"
#include "antares/solver/variable/surveyresults/surveyresults.h"

namespace Antares::Solver::Simulation
{
Expand Down Expand Up @@ -157,15 +158,6 @@ public:
optWriter,
pDurationCollector,
scratchmap);
if (!study.parameters.noOutput)
{
auto& simTable = simulation_->getSimulationTable(numSpace);

auto buffers = simTable.moveBuffers();

simulation_->storeYearBuffers(y, std::move(buffers.first), std::move(buffers.second));
}

// Log failing weeks
logFailedWeek(y, study, failedWeekList);

Expand Down Expand Up @@ -193,6 +185,15 @@ public:
};
}

if (!study.parameters.noOutput)
{
auto& simTable = simulation_->getSimulationTable(numSpace);

auto buffers = simTable.moveBuffers();

simulation_->storeYearBuffers(y, std::move(buffers.first), std::move(buffers.second));
}

// 10 - Synthesis results
// Computing the summary : adding the contribution of MC years
// previously computed in parallel
Expand Down Expand Up @@ -401,7 +402,9 @@ void ISimulation<ImplementationType>::writeResults(bool synthesis, uint year, ui
ImplementationType::variables.exportSurveyResults(synthesis,
newPath,
numSpace,
pResultWriter);
pResultWriter,
nullptr);
ImplementationType::writeSimulationTable(numSpace);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ class List: public NextT
void exportSurveyResults(bool global,
const Yuni::String& output,
unsigned int numSpace,
IResultWriter& writer);
IResultWriter& writer,
const SurveyResults::LegacySimulationTableOptions* legacyOptions = nullptr);

/*!
** \brief Ask to all variables to fullfil the digest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ template<class NextT>
void List<NextT>::exportSurveyResults(bool global,
const Yuni::String& output,
unsigned int numSpace,
IResultWriter& writer)
IResultWriter& writer,
const SurveyResults::LegacySimulationTableOptions* legacyOptions)
{
using namespace Antares;

Expand All @@ -294,6 +295,10 @@ void List<NextT>::exportSurveyResults(bool global,

// Year by year ?
survey.yearByYearResults = !global;
if (!global && legacyOptions && legacyOptions->simulationTable)
{
survey.configureLegacySimulationTable(*legacyOptions);
}

if (global)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class IntermediateValues final
Type year;

template<uint Size, class VCardT, class A>
void internalExportAnnualValues(SurveyResults& report, const A& array, bool annual) const;
void internalExportAnnualValues(SurveyResults& report, const A& array, bool annual, int precision) const;

void computeDailyAveragesForCurrentYear();
void computeWeeklyAveragesForCurrentYear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ inline void IntermediateValues::buildAnnualSurveyReport(SurveyResults& report,
switch (precision)
{
case Category::hourly:
internalExportAnnualValues<HOURS_PER_YEAR, VCardT>(report, hour, false);
internalExportAnnualValues<HOURS_PER_YEAR, VCardT>(report, hour, false, precision);
break;
case Category::daily:
internalExportAnnualValues<DAYS_PER_YEAR, VCardT>(report, day, false);
internalExportAnnualValues<DAYS_PER_YEAR, VCardT>(report, day, false, precision);
break;
case Category::weekly:
internalExportAnnualValues<WEEKS_PER_YEAR, VCardT>(report, week, false);
internalExportAnnualValues<WEEKS_PER_YEAR, VCardT>(report, week, false, precision);
break;
case Category::monthly:
internalExportAnnualValues<MONTHS_PER_YEAR, VCardT>(report, month, false);
internalExportAnnualValues<MONTHS_PER_YEAR, VCardT>(report, month, false, precision);
break;
case Category::annual:
internalExportAnnualValues<1, VCardT>(report, &year, true);
internalExportAnnualValues<1, VCardT>(report, &year, true, precision);
break;
}
}
Expand All @@ -63,7 +63,8 @@ inline void IntermediateValues::buildAnnualSurveyReport(SurveyResults& report,
template<unsigned int Size, class VCardT, class A>
void IntermediateValues::internalExportAnnualValues(SurveyResults& report,
const A& array,
bool annual) const
bool annual,
int precision) const
{
using namespace Yuni;
assert(report.data.columnIndex < report.maxVariables && "Column index out of bounds");
Expand All @@ -88,6 +89,9 @@ void IntermediateValues::internalExportAnnualValues(SurveyResults& report,
target = year;
}

const double* valuesToExport = annual ? &year : &array[0];
report.exportLegacySimulationTableValues(valuesToExport, Size, precision, annual);

// Next column index
++report.data.columnIndex;
}
Expand Down
Loading
Loading