OR-Tools  9.6
model_builder_helper.h
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_LINEAR_SOLVER_WRAPPERS_MODEL_BUILDER_HELPER_H_
15 #define OR_TOOLS_LINEAR_SOLVER_WRAPPERS_MODEL_BUILDER_HELPER_H_
16 
17 #include <atomic>
18 #include <functional>
19 #include <limits>
20 #include <optional>
21 #include <string>
22 #include <vector>
23 
24 #include "ortools/linear_solver/linear_solver.pb.h"
26 #include "ortools/util/logging.h"
27 
28 namespace operations_research {
29 
30 // The arguments of the functions defined below must follow these rules
31 // to be wrapped by SWIG correctly:
32 // 1) Their types must include the full operations_research::
33 // namespace.
34 // 2) Their names must correspond to the ones declared in the .i
35 // file (see the java/ and csharp/ subdirectories).
36 
37 // Helper for importing/exporting models and model protobufs.
38 //
39 // Wrapping global function is brittle with SWIG. It is much easier to
40 // wrap static class methods.
41 //
42 // Note: all these methods rely on c++ code that uses absl::Status or
43 // absl::StatusOr. Unfortunately, these are inconsistently wrapped in non C++
44 // languages. As a consequence, we need to provide an API that does not involve
45 // absl::Status or absl::StatusOr.
47  public:
49  options = MPModelExportOptions());
51  options = MPModelExportOptions());
52  bool WriteModelToFile(const std::string& filename);
53 
54  bool ImportFromMpsString(const std::string& mps_string);
55  bool ImportFromMpsFile(const std::string& mps_file);
56 #if defined(USE_LP_PARSER)
57  bool ImportFromLpString(const std::string& lp_string);
58  bool ImportFromLpFile(const std::string& lp_file);
59 #endif // defined(USE_LP_PARSER)
60 
61  const MPModelProto& model() const;
62  MPModelProto* mutable_model();
63 
64  // Direct low level model building API.
65  int AddVar();
66  void SetVarLowerBound(int var_index, double lb);
67  void SetVarUpperBound(int var_index, double ub);
68  void SetVarIntegrality(int var_index, bool is_integer);
69  void SetVarObjectiveCoefficient(int var_index, double coeff);
70  void SetVarName(int var_index, const std::string& name);
71 
72  int AddLinearConstraint();
73  void SetConstraintLowerBound(int ct_index, double lb);
74  void SetConstraintUpperBound(int ct_index, double ub);
75  void AddConstraintTerm(int ct_index, int var_index, double coeff);
76  void SetConstraintName(int ct_index, const std::string& name);
77 
78  int num_variables() const;
79  double VarLowerBound(int var_index) const;
80  double VarUpperBound(int var_index) const;
81  bool VarIsIntegral(int var_index) const;
82  double VarObjectiveCoefficient(int var_index) const;
83  std::string VarName(int var_index) const;
84 
85  int num_constraints() const;
86  double ConstraintLowerBound(int ct_index) const;
87  double ConstraintUpperBound(int ct_index) const;
88  std::string ConstraintName(int ct_index) const;
89  std::vector<int> ConstraintVarIndices(int ct_index) const;
90  std::vector<double> ConstraintCoefficients(int ct_index) const;
91 
92  std::string name() const;
93  void SetName(const std::string& name);
94 
95  void ClearObjective();
96  bool maximize() const;
97  void SetMaximize(bool maximize);
98  double ObjectiveOffset() const;
99  void SetObjectiveOffset(double offset);
100 
101  private:
102  MPModelProto model_;
103 };
104 
105 // Simple director class for C#.
106 class LogCallback {
107  public:
108  virtual ~LogCallback() {}
109  virtual void NewMessage(const std::string& message) = 0;
110 };
111 
126 };
127 
128 // Class used to solve a request. This class is not meant to be exposed to the
129 // public. Its responsibility is to bridge the MPModelProto in the non-C++
130 // languages with the C++ Solve method.
131 //
132 // It contains 2 helper objects: a logger, and an atomic bool to interrupt
133 // search.
135  public:
136  explicit ModelSolverHelper(const std::string& solver_name);
137  bool SolverIsSupported() const;
138  void Solve(const ModelBuilderHelper& model);
139 
140  // Only used by the CVXPY interface. Does not store the response internally.
141  // interrupt_solve_ is passed to the solve method.
142  std::optional<MPSolutionResponse> SolveRequest(const MPModelRequest& request);
143 
144  // Returns true if the interrupt signal was correctly sent, that is if the
145  // underlying solver supports it.
146  bool InterruptSolve();
147 
148  void SetLogCallback(std::function<void(const std::string&)> log_callback);
149  void SetLogCallbackFromDirectorClass(LogCallback* log_callback);
150  void ClearLogCallback();
151 
152  bool has_response() const;
153  bool has_solution() const;
154  const MPSolutionResponse& response() const;
155  SolveStatus status() const;
156 
157  // If not defined, or no solution, they will silently return 0.
158  double objective_value() const;
159  double best_objective_bound() const;
160  double variable_value(int var_index) const;
161  double reduced_cost(int var_index) const;
162  double dual_value(int ct_index) const;
163  double activity(int ct_index);
164 
165  std::string status_string() const;
166  double wall_time() const;
167  double user_time() const;
168 
169  // Solve parameters.
170  void SetTimeLimitInSeconds(double limit);
172  const std::string& solver_specific_parameters);
173  void EnableOutput(bool enabled);
174 
175  // TODO(user): set parameters.
176 
177  private:
178  std::atomic<bool> interrupt_solve_ = false;
179  std::function<void(const std::string&)> log_callback_;
180  std::optional<MPSolutionResponse> response_;
181  std::optional<MPModelRequest::SolverType> solver_type_;
182  std::optional<double> time_limit_in_second_;
183  std::string solver_specific_parameters_;
184  std::optional<const MPModelProto*> model_of_last_solve_;
185  std::vector<double> activities_;
186  bool solver_output_ = false;
187 };
188 
189 } // namespace operations_research
190 
191 #endif // OR_TOOLS_LINEAR_SOLVER_WRAPPERS_MODEL_BUILDER_HELPER_H_
virtual void NewMessage(const std::string &message)=0
bool WriteModelToFile(const std::string &filename)
void SetVarUpperBound(int var_index, double ub)
void SetConstraintUpperBound(int ct_index, double ub)
double VarObjectiveCoefficient(int var_index) const
void SetVarObjectiveCoefficient(int var_index, double coeff)
void AddConstraintTerm(int ct_index, int var_index, double coeff)
double ConstraintUpperBound(int ct_index) const
std::string VarName(int var_index) const
std::string ExportToMpsString(const operations_research::MPModelExportOptions &options=MPModelExportOptions())
std::string ConstraintName(int ct_index) const
void SetVarName(int var_index, const std::string &name)
void SetConstraintName(int ct_index, const std::string &name)
std::vector< double > ConstraintCoefficients(int ct_index) const
bool ImportFromMpsFile(const std::string &mps_file)
std::vector< int > ConstraintVarIndices(int ct_index) const
double ConstraintLowerBound(int ct_index) const
std::string ExportToLpString(const operations_research::MPModelExportOptions &options=MPModelExportOptions())
void SetConstraintLowerBound(int ct_index, double lb)
void SetVarLowerBound(int var_index, double lb)
void SetVarIntegrality(int var_index, bool is_integer)
bool ImportFromMpsString(const std::string &mps_string)
double variable_value(int var_index) const
void SetSolverSpecificParameters(const std::string &solver_specific_parameters)
void SetLogCallbackFromDirectorClass(LogCallback *log_callback)
void SetLogCallback(std::function< void(const std::string &)> log_callback)
void Solve(const ModelBuilderHelper &model)
const MPSolutionResponse & response() const
std::optional< MPSolutionResponse > SolveRequest(const MPModelRequest &request)
ModelSolverHelper(const std::string &solver_name)
GRBmodel * model
Collection of objects used to extend the Constraint Solver library.
std::string message
Definition: trace.cc:399