Antares Simulator
Power System Simulator
ConstraintBuilder.h
1 /*
2 ** Copyright 2007-2025, 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/solver/simulation/sim_structure_probleme_economique.h"
24 
25 #include "../opt_fonctions.h"
26 #include "../opt_rename_problem.h"
27 #include "../variables/VariableManagement.h"
28 
29 // TODO God struct should be decomposed
31 {
32 public:
33  std::vector<double>& Pi;
34  std::vector<int>& Colonne;
35  int& nombreDeContraintes;
36  int& nombreDeTermesDansLaMatriceDeContrainte;
37  std::vector<int>& IndicesDebutDeLigne;
38  std::vector<double>& CoefficientsDeLaMatriceDesContraintes;
39  std::vector<int>& IndicesColonnes;
40  std::vector<int>& NombreDeTermesDesLignes;
41  std::string& Sens;
42  int& IncrementDAllocationMatriceDesContraintes;
43  std::vector<CORRESPONDANCES_DES_VARIABLES>& CorrespondanceVarNativesVarOptim;
44  const int32_t& NombreDePasDeTempsPourUneOptimisation;
45  std::vector<int>& NumeroDeVariableStockFinal;
46  std::vector<std::vector<int>>& NumeroDeVariableDeTrancheDeStock;
47  std::vector<std::string>& NomDesContraintes;
48  const std::vector<const char*>& NomsDesPays;
49  const uint32_t& weekInTheYear;
50  const uint32_t& NombreDePasDeTemps;
51 };
52 
65 class ConstraintBuilder final
66 {
67 public:
68  ConstraintBuilder() = delete;
69 
71  data(data),
72  variableManager_(data.CorrespondanceVarNativesVarOptim,
73  data.NumeroDeVariableStockFinal,
74  data.NumeroDeVariableDeTrancheDeStock,
75  data.NombreDePasDeTempsPourUneOptimisation)
76  {
77  }
78 
79  ConstraintBuilder& updateHourWithinWeek(unsigned hour)
80  {
81  hourInWeek_ = hour;
82  return *this;
83  }
84 
93  ConstraintBuilder& DispatchableProduction(unsigned int index,
94  double coeff,
95  int offset = 0,
96  int delta = 0);
97 
98  ConstraintBuilder& NumberOfDispatchableUnits(unsigned int index, double coeff);
99 
100  ConstraintBuilder& NumberStoppingDispatchableUnits(unsigned int index, double coeff);
101 
102  ConstraintBuilder& NumberStartingDispatchableUnits(unsigned int index, double coeff);
103 
104  ConstraintBuilder& NumberBreakingDownDispatchableUnits(unsigned int index, double coeff);
105 
106  ConstraintBuilder& NTCDirect(unsigned int index, double coeff, int offset = 0, int delta = 0);
107 
108  ConstraintBuilder& IntercoDirectCost(unsigned int index, double coeff);
109 
110  ConstraintBuilder& IntercoIndirectCost(unsigned int index, double coeff);
111 
112  ConstraintBuilder& ShortTermStorageInjection(unsigned int index,
113  double coeff,
114  int offset = 0,
115  int delta = 0);
116 
117  ConstraintBuilder& ShortTermStorageWithdrawal(unsigned int index,
118  double coeff,
119  int offset = 0,
120  int delta = 0);
121 
122  ConstraintBuilder& ShortTermStorageLevel(unsigned int index,
123  double coeff,
124  int offset = 0,
125  int delta = 0);
126 
127  ConstraintBuilder& ShortTermStorageOverflow(unsigned int index,
128  double coeff,
129  int offset = 0,
130  int delta = 0);
131 
132  ConstraintBuilder& ShortTermCostVariationInjection(unsigned int index,
133  double coeff,
134  int offset = 0,
135  int delta = 0);
136  ConstraintBuilder& ShortTermCostVariationWithdrawal(unsigned int index,
137  double coeff,
138  int offset = 0,
139  int delta = 0);
140 
141  ConstraintBuilder& HydProd(unsigned int index, double coeff);
142 
143  ConstraintBuilder& HydProdDown(unsigned int index, double coeff);
144 
145  ConstraintBuilder& HydProdUp(unsigned int index, double coeff);
146 
147  ConstraintBuilder& Pumping(unsigned int index, double coeff);
148 
149  ConstraintBuilder& HydroLevel(unsigned int index, double coeff);
150 
151  ConstraintBuilder& Overflow(unsigned int index, double coeff);
152 
153  ConstraintBuilder& FinalStorage(unsigned int index, double coeff);
154 
155  ConstraintBuilder& PositiveUnsuppliedEnergy(unsigned int index, double coeff);
156 
157  ConstraintBuilder& NegativeUnsuppliedEnergy(unsigned int index, double coeff);
158 
159  ConstraintBuilder& LayerStorage(unsigned area, unsigned layer, double coeff);
160 
162 
163  class ConstraintBuilderInvalidOperator final: public std::runtime_error
164  {
165  public:
166  using std::runtime_error::runtime_error;
167  };
168 
175  {
176  if (op == '<' || op == '=' || op == '>')
177  {
178  operator_ = op;
179  }
180  else
181  {
182  throw ConstraintBuilderInvalidOperator(std::string("Invalid operator: ") + op);
183  }
184 
185  return *this;
186  }
187 
194  {
195  operator_ = '=';
196  return *this;
197  }
198 
205  {
206  operator_ = '<';
207  return *this;
208  }
209 
216  {
217  operator_ = '>';
218  return *this;
219  }
220 
225  void build();
226 
227  int NumberOfVariables() const
228  {
229  return nombreDeTermes_;
230  }
231 
232  ConstraintBuilderData& data;
233 
234 private:
235  void OPT_ChargerLaContrainteDansLaMatriceDesContraintes();
236 
237  unsigned int hourInWeek_ = 0;
238 
239  char operator_ = '=';
240  int nombreDeTermes_ = 0;
241 
242  void AddVariable(int index, double coeff);
243 
250  VariableManagement::VariableManager variableManager_;
251 };
252 
255 {
256 public:
257  ConstraintFactory() = delete;
258 
259  explicit ConstraintFactory(ConstraintBuilder& builder):
260  builder(builder)
261  {
262  }
263 
264  ConstraintBuilder& builder;
265 };
266 
267 // Helper functions
268 inline void ExportPaliers(const PALIERS_THERMIQUES& PaliersThermiquesDuPays,
269  ConstraintBuilder& newConstraintBuilder)
270 {
271  for (int index = 0; index < PaliersThermiquesDuPays.NombreDePaliersThermiques; index++)
272  {
273  const int palier = PaliersThermiquesDuPays
274  .NumeroDuPalierDansLEnsembleDesPaliersThermiques[index];
275  newConstraintBuilder.DispatchableProduction(palier, -1.0);
276  }
277 }
278 
280 {
281 public:
282  const char& TypeDeContrainteCouplante;
283  const int& NombreDInterconnexionsDansLaContrainteCouplante;
284  const std::vector<int>& NumeroDeLInterconnexion;
285  const std::vector<double>& PoidsDeLInterconnexion;
286  const std::vector<int>& OffsetTemporelSurLInterco;
287  const int& NombreDePaliersDispatchDansLaContrainteCouplante;
288  const std::vector<int>& PaysDuPalierDispatch;
289  const std::vector<int>& NumeroDuPalierDispatch;
290  const std::vector<double>& PoidsDuPalierDispatch;
291  const std::vector<int>& OffsetTemporelSurLePalierDispatch;
292  const char& SensDeLaContrainteCouplante;
293  const char* const& NomDeLaContrainteCouplante;
294  const std::vector<PALIERS_THERMIQUES>& PaliersThermiquesDuPays;
295 };
296 
298 {
299  const std::vector<PALIERS_THERMIQUES>& PaliersThermiquesDuPays;
300  bool Simulation;
301 };
302 
304 {
305  std::vector<CORRESPONDANCES_DES_CONTRAINTES>& CorrespondanceCntNativesCntOptim;
306 
307  const std::vector<::AREA_INPUT>& ShortTermStorage;
308 };
309 
311 {
312  CORRESPONDANCES_DES_CONTRAINTES_HEBDOMADAIRES& CorrespondanceCntNativesCntOptimHebdomadaires;
313 };
Definition: ConstraintBuilder.h:280
Definition: ConstraintBuilder.h:31
Definition: ConstraintBuilder.h:164
Definition: ConstraintBuilder.h:66
ConstraintBuilder & lessThan()
set the sign of the constraint to '<', building a constraint less than rhs
Definition: ConstraintBuilder.h:204
ConstraintBuilder & greaterThan()
set the sign of the constraint to '>', building a constraint greather than rhs
Definition: ConstraintBuilder.h:215
void build()
add the constraint in the matrix
Definition: ConstraintBuilder.cpp:24
ConstraintBuilder & equalTo()
set the sign of the constraint to '=', building a constraint equal to rhs
Definition: ConstraintBuilder.h:193
ConstraintBuilder & SetOperator(char op)
set the operator of the constraint (sign)
Definition: ConstraintBuilder.h:174
Definition: ConstraintBuilder.h:255
Definition: ShortTermStorageLevel.h:26
Definition: VariableManagement.h:12
Definition: sim_structure_probleme_economique.h:114
Definition: sim_structure_probleme_economique.h:268
Definition: ConstraintBuilder.h:311
Definition: ConstraintBuilder.h:304
Definition: ConstraintBuilder.h:298