Antares Simulator
Power System Simulator
OptimEntityContainer.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 
22 #pragma once
23 #include <span>
24 #include <vector>
25 
26 #include <antares/optimisation/linear-problem-api/mipConstraint.h>
27 #include <antares/optimisation/linear-problem-api/mipVariable.h>
28 #include <antares/study/system-model/component.h>
29 #include <antares/study/system-model/variabilityType.h>
30 #include "antares/optimisation/linear-problem-api/linearProblem.h"
31 
32 #include "EvaluationContext.h"
33 #include "scenarioGroupRepo.h"
34 
35 namespace Antares::Optimisation
36 {
38 {
39  std::vector<unsigned> modelVariableGlobalIndices;
40  std::vector<unsigned> modelConstraintsGlobalIndices;
41  std::vector<VariabilityType> modelConstraintsVariability;
42  EvaluationContext evaluationContext;
43 };
44 
46 {
47 public:
50  const ScenarioGroupRepository* scenarioGroupRepository);
51 
52  [[nodiscard]] unsigned int getVariableStartColumn(
54  unsigned int index) const
55  {
56  const auto& optimComponent = optimComponents_.at(component.Index());
57  return variableStartColumn_.at(optimComponent.modelVariableGlobalIndices.at(index));
58  }
59 
60  [[nodiscard]] const EvaluationContext& getEvaluationContext(
61  const Antares::ModelerStudy::SystemModel::Component& component) const
62  {
63  const auto& optimComponent = optimComponents_.at(component.Index());
64  return optimComponent.evaluationContext;
65  }
66 
67  [[nodiscard]] std::pair<unsigned int, VariabilityType> getConstraintData(
69  unsigned int index) const
70  {
71  const auto& optimComponent = optimComponents_.at(component.Index());
72  return {constraintStartLine_.at(optimComponent.modelConstraintsGlobalIndices.at(index)),
73  optimComponent.modelConstraintsVariability.at(index)};
74  }
75 
77  {
78  return linearProblem_;
79  }
80 
81  void addStartColumn()
82  {
83  variableStartColumn_.push_back(linearProblem_.variableCount());
84  }
85 
86  [[nodiscard]] const std::vector<std::unique_ptr<LinearProblemApi::IMipVariable>>& getVariables()
87  const
88  {
89  return linearProblem_.getVariables();
90  }
91 
92  [[nodiscard]] std::span<const std::unique_ptr<LinearProblemApi::IMipVariable>>
93  getComponentVariable(const Antares::ModelerStudy::SystemModel::Component& component,
94  unsigned int index,
95  std::size_t nbTimeSteps) const
96  {
97  const auto& variables = linearProblem_.getVariables();
98  unsigned int startColumn = getVariableStartColumn(component, index);
99  return {variables.data() + startColumn, nbTimeSteps};
100  }
101 
102  [[nodiscard]] std::pair<std::span<const std::unique_ptr<LinearProblemApi::IMipConstraint>>,
104  getComponentConstraint(const Antares::ModelerStudy::SystemModel::Component& component,
105  unsigned int index,
106  std::size_t nbTimeSteps) const
107  {
108  const auto& constraints = linearProblem_.getConstraints();
109  const auto [startLine, timeIndex] = getConstraintData(component, index);
110  return {{constraints.data() + startLine, nbTimeSteps}, timeIndex};
111  }
112 
113  [[nodiscard]] const std::vector<std::unique_ptr<LinearProblemApi::IMipConstraint>>&
114  getConstraints() const
115  {
116  return linearProblem_.getConstraints();
117  }
118 
119  [[nodiscard]] OptimComponent& getOptimComponent(size_t index)
120  {
121  return optimComponents_.at(index);
122  }
123 
124  void addFromSystemComponents(
125  const std::vector<Antares::ModelerStudy::SystemModel::Component>& component,
126  Modeler::Config::Location targetLocation = Modeler::Config::Location::SUBPROBLEMS);
127  void registerConstraint(const ModelerStudy::SystemModel::Component& component,
128  const VariabilityType& variability);
129 
130  unsigned constraintGLobalIndex() const
131  {
132  return static_cast<unsigned int>(constraintStartLine_.size());
133  }
134 
135 private:
136  std::vector<unsigned int> variableStartColumn_;
137  std::vector<OptimComponent> optimComponents_;
138  std::vector<unsigned int> constraintStartLine_;
139  LinearProblemApi::ILinearProblem& linearProblem_;
141  const ScenarioGroupRepository* scenarioGroupRepository_;
142 
143  void addStartLine()
144  {
145  constraintStartLine_.push_back(linearProblem_.constraintCount());
146  }
147 };
148 } // namespace Antares::Optimisation
Represents the context for evaluating expressions.
Definition: EvaluationContext.h:25
Interface for linear problem data. Provides a method to retrieve data for a specific dataset,...
Definition: ILinearProblemData.h:114
Definition: OptimEntityContainer.h:46
Definition: scenarioGroupRepo.h:12
Definition: VariableNode.h:9
VariabilityType
Represents the time and scenario variation of a value.
Definition: variabilityType.h:29
Definition: OptimEntityContainer.h:38