Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
bindingConstraintsMarginalCost.h
1/*
2** Copyright 2007-2024, RTE (https://www.rte-france.com)
3** See AUTHORS.txt
4** SPDX-License-Identifier: MPL-2.0
5** This file is part of Antares-Simulator,
6** Adequacy and Performance assessment for interconnected energy networks.
7**
8** Antares_Simulator is free software: you can redistribute it and/or modify
9** it under the terms of the Mozilla Public Licence 2.0 as published by
10** the Mozilla Foundation, either version 2 of the License, or
11** (at your option) any later version.
12**
13** Antares_Simulator is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16** Mozilla Public Licence 2.0 for more details.
17**
18** You should have received a copy of the Mozilla Public Licence 2.0
19** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20*/
21#pragma once
22
23#include "antares/study/binding_constraint/BindingConstraint.h"
24
25#include "../../variable.h"
26
27namespace Antares
28{
29namespace Solver
30{
31namespace Variable
32{
33namespace Economy
34{
36{
38 static std::string Caption()
39 {
40 return "BC. MARG. COST";
41 }
42
44 static std::string Unit()
45 {
46 return "Euro";
47 }
48
50 static std::string Description()
51 {
52 return "Marginal cost for binding constraints";
53 }
54
56 typedef Results<R::AllYears::Average< // The average values thoughout all years
57 R::AllYears::StdDeviation< // The standard deviation values throughout all years
58 R::AllYears::Min< // The minimum values thoughout all years
59 R::AllYears::Max< // The maximum values thoughout all years
60 >>>>>
62
64 static constexpr uint8_t categoryDataLevel = Category::DataLevel::bindingConstraint;
66 static constexpr uint8_t categoryFileLevel = ResultsType::categoryFile
67 & (Category::FileLevel::bc);
69 static constexpr uint8_t precision = Category::all;
71 static constexpr uint8_t nodeDepthForGUI = +0;
73 static constexpr uint8_t decimal = 2;
75 static constexpr int columnCount = 1;
77 static constexpr uint8_t hasIntermediateValues = 1;
79 static constexpr uint8_t isPossiblyNonApplicable = 1;
80
82 typedef std::vector<IntermediateValues> IntermediateValuesType;
83
84}; // class VCard
85
86/*
87 Marginal cost associated to binding constraints :
88 Suppose that the BC is hourly,
89 - if binding constraint is not saturated (rhs is not reached) for a given hour, the value is 0;
90 - if binding constraint is saturated (rhs is reached), the value is the total benefit (�/MW) for
91 the system that would result in increasing the BC's rhs of 1 MW.
92*/
93template<class NextT = Container::EndOfList>
95 : public Variable::IVariable<BindingConstMarginCost<NextT>, NextT, VCardBindingConstMarginCost>
96{
97public:
99 typedef NextT NextType;
104
107
109
110 enum
111 {
113 count = 1 + NextT::count,
114 };
115
116 template<int CDataLevel, int CFile>
118 {
119 enum
120 {
121 count = ((VCardType::categoryDataLevel & CDataLevel
123 ? (NextType::template Statistics<CDataLevel, CFile>::count
125 : NextType::template Statistics<CDataLevel, CFile>::count),
126 };
127 };
128
129public:
130 BindingConstMarginCost() = default;
131
132 void simulationBegin()
133 {
134 NextType::simulationBegin();
135 }
136
137 void simulationEnd()
138 {
139 NextType::simulationEnd();
140 }
141
142 void initializeFromStudy(Data::Study& study)
143 {
144 pNbYearsParallel = study.maxNbYearsInParallel;
145
146 // Statistics thoughout all years
147 InitializeResultsFromStudy(AncestorType::pResults, study);
148
149 // Intermediate values
150 pValuesForTheCurrentYear.resize(pNbYearsParallel);
151 for (unsigned int numSpace = 0; numSpace < pNbYearsParallel; numSpace++)
152 {
153 pValuesForTheCurrentYear[numSpace].initializeFromStudy(study);
154 }
155
156 NextType::initializeFromStudy(study);
157 }
158
159 template<class R>
160 static void InitializeResultsFromStudy(R& results, Data::Study& study)
161 {
162 VariableAccessorType::InitializeAndReset(results, study);
163 }
164
165 void setAssociatedBindConstraint(std::shared_ptr<Data::BindingConstraint> bc_ptr)
166 {
167 associatedBC_ = bc_ptr;
168 }
169
170 void setBindConstraintsCount(uint bcCount)
171 {
172 nbCount_ = bcCount;
173 }
174
175 size_t getMaxNumberColumns() const
176 {
177 return nbCount_ * ResultsType::count;
178 }
179
180 void yearBegin(unsigned int year, unsigned int numSpace)
181 {
182 // Reset the values for the current year
183 pValuesForTheCurrentYear[numSpace].reset();
184
185 // Next variable
186 NextType::yearBegin(year, numSpace);
187 }
188
189 void yearEnd(unsigned int year, unsigned int numSpace)
190 {
191 if (isInitialized())
192 {
193 // Compute statistics for the current year depending on
194 // the BC type (hourly, daily, weekly)
195 using namespace Data;
196 switch (associatedBC_->type())
197 {
198 case BindingConstraint::typeHourly:
199 pValuesForTheCurrentYear[numSpace].computeAveragesForCurrentYearFromHourlyResults();
200 break;
201 case BindingConstraint::typeDaily:
202 pValuesForTheCurrentYear[numSpace].computeAveragesForCurrentYearFromDailyResults();
203 break;
204 case BindingConstraint::typeWeekly:
205 pValuesForTheCurrentYear[numSpace].computeAveragesForCurrentYearFromWeeklyResults();
206 break;
207 case BindingConstraint::typeUnknown:
208 case BindingConstraint::typeMax:
209 break;
210 }
211 }
212
213 // Next variable
214 NextType::yearEnd(year, numSpace);
215 }
216
217 void computeSummary(std::map<unsigned int, unsigned int>& numSpaceToYear,
218 unsigned int nbYearsForCurrentSummary)
219 {
220 for (unsigned int numSpace = 0; numSpace < nbYearsForCurrentSummary; ++numSpace)
221 {
222 // Merge all those values with the global results
223 AncestorType::pResults.merge(numSpaceToYear[numSpace] /*year*/,
224 pValuesForTheCurrentYear[numSpace]);
225 }
226
227 // Next variable
228 NextType::computeSummary(numSpaceToYear, nbYearsForCurrentSummary);
229 }
230
231 void weekBegin(State& state)
232 {
233 if (isInitialized())
234 {
235 auto numSpace = state.numSpace;
236 // For daily binding constraints, getting daily marginal price
237 using namespace Data;
238 switch (associatedBC_->type())
239 {
240 case BindingConstraint::typeHourly:
241 case BindingConstraint::typeUnknown:
242 case BindingConstraint::typeMax:
243 break;
244
245 case BindingConstraint::typeDaily:
246 {
247 int dayInTheYear = state.weekInTheYear * 7;
248 for (int dayInTheWeek = 0; dayInTheWeek < 7; dayInTheWeek++)
249 {
250 pValuesForTheCurrentYear[numSpace].day[dayInTheYear]
251 -= state.problemeHebdo
252 ->ResultatsContraintesCouplantes[associatedBC_][dayInTheWeek];
253
254 dayInTheYear++;
255 }
256 break;
257 }
258
259 // For weekly binding constraints, getting weekly marginal price
260 case BindingConstraint::typeWeekly:
261 {
262 uint weekInTheYear = state.weekInTheYear;
263 double weeklyValue = -state.problemeHebdo
264 ->ResultatsContraintesCouplantes[associatedBC_][0];
265
266 pValuesForTheCurrentYear[numSpace].week[weekInTheYear] = weeklyValue;
267
268 int dayInTheYear = state.weekInTheYear * 7;
269 for (int dayInTheWeek = 0; dayInTheWeek < 7; dayInTheWeek++)
270 {
271 pValuesForTheCurrentYear[numSpace].day[dayInTheYear] = weeklyValue;
272 dayInTheYear++;
273 }
274 break;
275 }
276 }
277 }
278 NextType::weekBegin(state);
279 }
280
281 void hourBegin(unsigned int hourInTheYear)
282 {
283 // Next variable
284 NextType::hourBegin(hourInTheYear);
285 }
286
287 void hourForEachArea(State& state, unsigned int numSpace)
288 {
289 NextType::hourForEachArea(state, numSpace);
290 }
291
292 void weekForEachArea(State& state, unsigned int numSpace)
293 {
294 NextType::weekForEachArea(state, numSpace);
295 }
296
297 template<class VCardToFindT>
298 static void retrieveResultsForArea(typename Storage<VCardToFindT>::ResultsType** result,
299 const Data::Area* area)
300 {
301 // Next variable
302 NextType::template retrieveResultsForArea<VCardToFindT>(result, area);
303 }
304
305 void buildDigest(SurveyResults& results, int digestLevel, int dataLevel) const
306 {
307 NextType::buildDigest(results, digestLevel, dataLevel);
308 }
309
310 template<class V>
311 static void simulationEndSpatialAggregates(V& allVars)
312 {
313 NextType::template simulationEndSpatialAggregates<V>(allVars);
314 }
315
316 template<class V>
317 static void computeSpatialAggregatesSummary(
318 V& allVars,
319 std::map<unsigned int, unsigned int>& numSpaceToYear,
320 unsigned int nbYearsForCurrentSummary)
321 {
322 NextType::template computeSpatialAggregatesSummary<V>(allVars,
323 numSpaceToYear,
324 nbYearsForCurrentSummary);
325 }
326
327 void beforeYearByYearExport(uint year, uint numSpace)
328 {
329 NextType::beforeYearByYearExport(year, numSpace);
330 }
331
332 template<class SearchVCardT, class O>
333 static void computeSpatialAggregateWith(O& out, const Data::Area* area, uint numSpace)
334 {
335 NextType::template computeSpatialAggregateWith<SearchVCardT, O>(out, area, numSpace);
336 }
337
338 template<class VCardToFindT>
339 static void retrieveResultsForLink(typename Storage<VCardToFindT>::ResultsType** result,
340 const Data::AreaLink* link)
341 {
342 NextType::template retrieveResultsForLink<VCardToFindT>(result, link);
343 }
344
345 template<class VCardToFindT>
346 static void retrieveResultsForThermalCluster(
347 typename Storage<VCardToFindT>::ResultsType** result,
348 const Data::ThermalCluster* cluster)
349 {
350 NextType::template retrieveResultsForThermalCluster<VCardToFindT>(result, cluster);
351 }
352
353 void hourEnd(State& state, unsigned int hourInTheYear)
354 {
355 if (isInitialized())
356 {
357 auto numSpace = state.numSpace;
358 if (associatedBC_->type() == Data::BindingConstraint::typeHourly)
359 {
360 pValuesForTheCurrentYear[numSpace][hourInTheYear]
361 -= state.problemeHebdo
362 ->ResultatsContraintesCouplantes[associatedBC_][state.hourInTheWeek];
363 }
364 }
365
366 NextType::hourEnd(state, hourInTheYear);
367 }
368
369 Antares::Memory::Stored<double>::ConstReturnType retrieveRawHourlyValuesForCurrentYear(
370 unsigned int,
371 unsigned int numSpace) const
372 {
373 return pValuesForTheCurrentYear[numSpace].hour;
374 }
375
376 void localBuildAnnualSurveyReport(
377 SurveyResults& results,
378 int fileLevel,
379 int precision /* printed results : hourly, daily, weekly, ...*/,
380 unsigned int numSpace) const
381 {
382 if (!(precision & associatedBC_->yearByYearFilter()))
383 {
384 return;
385 }
386
387 // Initializing external pointer on current variable non applicable status
388 results.isCurrentVarNA[0] = isCurrentOutputNonApplicable(precision);
389
390 if (AncestorType::isPrinted[0])
391 {
392 // Write the data for the current year
393 results.variableCaption = getBindConstraintCaption();
394 results.variableUnit = VCardType::Unit();
395 pValuesForTheCurrentYear[numSpace]
396 .template buildAnnualSurveyReport<VCardType>(results, fileLevel, precision);
397 }
398 }
399
400 void buildSurveyReport(SurveyResults& results,
401 int dataLevel,
402 int fileLevel,
403 int precision) const
404 {
405 // Building syntheses results
406 // ------------------------------
407 if (precision & associatedBC_->yearByYearFilter())
408 {
409 // And only if we match the current data level _and_ precision level
410 if ((dataLevel & VCardType::categoryDataLevel)
411 && (fileLevel & VCardType::categoryFileLevel) && (precision & VCardType::precision))
412 {
413 results.isPrinted = AncestorType::isPrinted;
414 results.isCurrentVarNA[0] = isCurrentOutputNonApplicable(precision);
415 results.variableCaption = getBindConstraintCaption();
416
417 VariableAccessorType::template BuildSurveyReport<VCardType>(results,
419 dataLevel,
420 fileLevel,
421 precision,
422 false);
423 }
424 }
425 NextType::buildSurveyReport(results, dataLevel, fileLevel, precision);
426 }
427
428private:
429 // Private methods
430 // ---------------
431 std::string getBindConstraintCaption() const
432 {
433 std::string mathOperator(
434 Antares::Data::BindingConstraint::MathOperatorToCString(associatedBC_->operatorType()));
435 return std::string() + associatedBC_->name().c_str() + " (" + mathOperator + ")";
436 }
437
438 bool isInitialized()
439 {
440 return associatedBC_ != nullptr;
441 }
442
443 bool isCurrentOutputNonApplicable(int precision) const
444 {
445 using namespace Antares::Data;
446 // The current marginal prices to print becomes non applicable if they have a precision
447 // (hour, day, week, ...) smaller than the associated binding constraint granularity.
448 // Ex : if the BC is daily and we try to print hourly associated marginal prices,
449 // then these prices are set to N/A
450 switch (associatedBC_->type())
451 {
452 case BindingConstraint::typeUnknown:
453 case BindingConstraint::typeMax:
454 return true;
455 default:
456 const auto precision_bc = 1 << (associatedBC_->type() - 1);
457 return precision < precision_bc;
458 }
459 }
460
461 // Private data mambers
462 // ----------------------
464 typename VCardType::IntermediateValuesType pValuesForTheCurrentYear;
465 unsigned int pNbYearsParallel = 0;
466 std::shared_ptr<Data::BindingConstraint> associatedBC_ = nullptr;
467 uint nbCount_ = 0; // Number of inequality BCs
468
469}; // class BindingConstMarginCost
470
471} // namespace Economy
472} // namespace Variable
473} // namespace Solver
474} // namespace Antares
Definition for a single area.
Definition area.h:52
static const char * MathOperatorToCString(Operator o)
Convert a binding constraint operator into a short C-String (symbol)
Definition BindingConstraint.cpp:133
@ typeHourly
The binding constraint has hourly values.
Definition BindingConstraint.h:59
Definition study.h:61
A single thermal cluster.
Definition cluster.h:78
Definition bindingConstraintsMarginalCost.h:96
Variable::IVariable< BindingConstMarginCost< NextT >, NextT, VCardType > AncestorType
Ancestor.
Definition bindingConstraintsMarginalCost.h:103
VCardBindingConstMarginCost VCardType
VCard.
Definition bindingConstraintsMarginalCost.h:101
VCardType::ResultsType ResultsType
List of expected results.
Definition bindingConstraintsMarginalCost.h:106
@ count
How many items have we got.
Definition bindingConstraintsMarginalCost.h:113
NextT NextType
Type of the next static variable.
Definition bindingConstraintsMarginalCost.h:99
Interface for any variable.
Definition variable.h:51
StoredResultType pResults
All the results about this variable.
Definition variable.h:327
Temporary buffer for allocating results for a single year.
Definition intermediate.h:46
Definition results.h:48
@ count
The count if item in the list.
Definition results.h:56
Definition cbuilder.h:120
Definition variable.h:25
Definition bindingConstraintsMarginalCost.h:118
Definition bindingConstraintsMarginalCost.h:36
static constexpr uint8_t categoryFileLevel
File level (provided by the type of the results)
Definition bindingConstraintsMarginalCost.h:66
static std::string Unit()
Unit.
Definition bindingConstraintsMarginalCost.h:44
static constexpr uint8_t hasIntermediateValues
Intermediate values.
Definition bindingConstraintsMarginalCost.h:77
static constexpr uint8_t categoryDataLevel
Data Level.
Definition bindingConstraintsMarginalCost.h:64
static constexpr uint8_t nodeDepthForGUI
Indentation (GUI)
Definition bindingConstraintsMarginalCost.h:71
static constexpr uint8_t decimal
Decimal precision.
Definition bindingConstraintsMarginalCost.h:73
static constexpr int columnCount
Number of columns used by the variable.
Definition bindingConstraintsMarginalCost.h:75
static constexpr uint8_t precision
Precision (views)
Definition bindingConstraintsMarginalCost.h:69
Results< R::AllYears::Average< R::AllYears::StdDeviation< R::AllYears::Min< R::AllYears::Max< > > > > > ResultsType
The expecte results.
Definition bindingConstraintsMarginalCost.h:61
static constexpr uint8_t isPossiblyNonApplicable
Can this variable be non applicable (0 : no, 1 : yes)
Definition bindingConstraintsMarginalCost.h:79
static std::string Description()
The short description of the variable.
Definition bindingConstraintsMarginalCost.h:50
static std::string Caption()
Caption.
Definition bindingConstraintsMarginalCost.h:38
VariableAccessor< typenameVCardT::ResultsType, VCardT::columnCount >::Type ResultsType
The true type used for the results.
Definition info.h:764