Antares Simulator
Power System Simulator
linearProblem.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 
24 #include <antares/optimisation/linear-problem-api/linearProblem.h>
25 #include <antares/optimisation/linear-problem-mpsolver-impl/mipConstraint.h>
26 #include <antares/optimisation/linear-problem-mpsolver-impl/mipSolution.h>
27 
28 namespace operations_research
29 {
30 class MPSolver;
31 class MPSolverParameters;
32 class MPObjective;
33 } // namespace operations_research
34 
35 namespace Antares::Optimisation::LinearProblemMpsolverImpl
36 {
37 
39 {
40 public:
41  OrtoolsLinearProblem(bool isMip, const std::string& solverName);
42  ~OrtoolsLinearProblem() override = default;
43 
45  double ub,
46  const std::string& name) override;
47 
49  double ub,
50  const std::string& name) override;
51 
53  double ub,
54  bool integer,
55  const std::string& name) override;
56  [[nodiscard]] const std::vector<std::unique_ptr<LinearProblemApi::IMipVariable>>& getVariables()
57  const override;
58  [[nodiscard]] LinearProblemApi::IMipVariable* getVariable(std::size_t index) const override;
59  [[nodiscard]] LinearProblemApi::IMipVariable* lookupVariable(
60  const std::string& name) const override;
61 
62  [[nodiscard]] int variableCount() const override;
63 
65  double ub,
66  const std::string& name) override;
67  [[nodiscard]] const std::vector<std::unique_ptr<LinearProblemApi::IMipConstraint>>&
68  getConstraints() const override;
69  [[nodiscard]] LinearProblemApi::IMipConstraint* getConstraint(std::size_t index) const override;
70  [[nodiscard]] LinearProblemApi::IMipConstraint* lookupConstraint(
71  const std::string& name) const override;
72  [[nodiscard]] int constraintCount() const override;
73 
74  void setObjectiveCoefficient(LinearProblemApi::IMipVariable* var, double coefficient) override;
75  double getObjectiveCoefficient(const LinearProblemApi::IMipVariable* var) const override;
76 
87  void setObjectiveOffset(double objectiveOffset) override;
88 
98  [[nodiscard]] double getObjectiveOffset() const override;
99 
100  void setMinimization() override;
101  void setMaximization() override;
102 
103  [[nodiscard]] bool isMinimization() const override;
104  [[nodiscard]] bool isMaximization() const override;
105 
106  OrtoolsMipSolution* solve(bool verboseSolver) override;
107 
113  OrtoolsMipSolution* solution(bool verboseSolver);
114  double objectiveValue() const override;
115 
116  [[nodiscard]] double infinity() const override;
117  [[nodiscard]] bool isLP() const override;
118 
119  friend void Write(const OrtoolsLinearProblem& problem, const std::filesystem::path& path);
120 
121 protected:
122  [[nodiscard]] operations_research::MPSolver* MpSolver() const;
123 
124 private:
125  operations_research::MPSolver* mpSolver_{nullptr};
126  operations_research::MPObjective* objective_{nullptr};
127  operations_research::MPSolverParameters params_;
128 
129  std::vector<std::unique_ptr<LinearProblemApi::IMipVariable>> variables_;
130  std::vector<std::unique_ptr<LinearProblemApi::IMipConstraint>> constraints_;
131 
132  std::unique_ptr<OrtoolsMipSolution> solution_;
133  bool isLP_{true};
134 };
135 
136 } // namespace Antares::Optimisation::LinearProblemMpsolverImpl
LinearProblemApi::IMipVariable * addNumVariable(double lb, double ub, const std::string &name) override
Create a continuous variable.
Definition: linearProblem.cpp:80
OrtoolsMipSolution * solve(bool verboseSolver) override
Solve the problem, returns a IMipSolution.
Definition: linearProblem.cpp:221
void setObjectiveOffset(double objectiveOffset) override
Sets the constant offset for the objective function.
Definition: linearProblem.cpp:186
void setMinimization() override
Sets the optimization direction to minimize.
Definition: linearProblem.cpp:196
void setObjectiveCoefficient(LinearProblemApi::IMipVariable *var, double coefficient) override
Set the objective coefficient for a given variable.
Definition: linearProblem.cpp:174
void setMaximization() override
Sets the optimization direction to maximize.
Definition: linearProblem.cpp:201
LinearProblemApi::IMipConstraint * addConstraint(double lb, double ub, const std::string &name) override
Add a bounded constraint to the problem.
Definition: linearProblem.cpp:130
LinearProblemApi::IMipVariable * addVariable(double lb, double ub, bool integer, const std::string &name) override
Create a continuous or integer variable.
Definition: linearProblem.cpp:52
LinearProblemApi::IMipVariable * addIntVariable(double lb, double ub, const std::string &name) override
Create a integer variable.
Definition: linearProblem.cpp:87
double getObjectiveOffset() const override
Returns the current objective offset value.
Definition: linearProblem.cpp:191
OrtoolsMipSolution * solution(bool verboseSolver)
Definition: linearProblem.cpp:234