Antares Simulator
Power System Simulator
TimeDependentLinearExpression.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 #include <span>
23 
24 #include <antares/expressions/visitors/VariadicNodeFunctionVisit.h>
25 
26 #include "LinearExpression.h"
27 
28 namespace Antares::Optimization
29 {
31 {
32 public:
33  explicit TimeDependentLinearExpression(std::size_t nbTimesteps);
34 
35  explicit TimeDependentLinearExpression(const std::span<const double>& values);
36 
37  // Constant expression
39  explicit TimeDependentLinearExpression(const std::vector<std::pair<int, double>>& coefs,
40  double constant);
41 
42  std::vector<double> constant() const;
43 
44  void mergeDuplicateCoefficients();
45 
46  std::size_t size() const;
47 
48  bool isConstant() const;
49 
50  using iterator = std::vector<LinearExpression>::iterator;
51  iterator begin();
52  iterator end();
53 
54  using const_iterator = std::vector<LinearExpression>::const_iterator;
55  const_iterator begin() const;
56  const_iterator end() const;
57 
58  LinearExpression& operator[](std::size_t idx);
59 
60  const LinearExpression& operator[](std::size_t idx) const;
61 
63 
65 
66  void rotate(int shift);
67 
68  TimeDependentLinearExpression& operator*=(double factor);
69 
71 
72  TimeDependentLinearExpression operator-() const;
73 
75 
76 private:
77  void expandTo(std::size_t nbTimesteps);
78 
79  std::vector<LinearExpression> v_;
80 };
81 
82 template<class Op>
83 TimeDependentLinearExpression applyOperation(const std::vector<TimeDependentLinearExpression>& lhs,
84  Op op)
85 {
86  const auto maxSize = Expressions::Visitors::getMaxSize(lhs);
87  std::vector<const LinearExpression*> row(lhs.size());
88  std::vector<double> constants(maxSize);
89 
90  for (std::size_t t = 0; t < maxSize; ++t)
91  {
92  for (int c = 0; c < lhs.size(); ++c)
93  {
94  const auto& e = lhs[c];
95  row[c] = &(e.size() < maxSize ? e[0] : e[t]);
96  }
97  constants[t] = applyOperation(row, op);
98  }
99 
100  return TimeDependentLinearExpression(constants);
101 }
102 
103 } // namespace Antares::Optimization
Definition: LinearExpression.h:30
Definition: TimeDependentLinearExpression.h:31