Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
model_builder_helper.h
Go to the documentation of this file.
1// Copyright 2010-2024 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"
28
29namespace operations_research {
30
31// The arguments of the functions defined below must follow these rules
32// to be wrapped by SWIG correctly:
33// 1) Their types must include the full operations_research::
34// namespace.
35// 2) Their names must correspond to the ones declared in the .i
36// file (see the java/ and csharp/ subdirectories).
37
38// Helper for importing/exporting models and model protobufs.
39//
40// Wrapping global function is brittle with SWIG. It is much easier to
41// wrap static class methods.
42//
43// Note: all these methods rely on c++ code that uses absl::Status or
44// absl::StatusOr. Unfortunately, these are inconsistently wrapped in non C++
45// languages. As a consequence, we need to provide an API that does not involve
46// absl::Status or absl::StatusOr.
48 public:
49 void OverwriteModel(const ModelBuilderHelper& other_helper);
51 options = MPModelExportOptions());
53 options = MPModelExportOptions());
54 bool WriteToMpsFile(const std::string& filename,
57 bool ReadModelFromProtoFile(const std::string& filename);
58 bool WriteModelToProtoFile(const std::string& filename);
59
60 bool ImportFromMpsString(const std::string& mps_string);
61 bool ImportFromMpsFile(const std::string& mps_file);
62#if defined(USE_LP_PARSER)
63 bool ImportFromLpString(const std::string& lp_string);
64 bool ImportFromLpFile(const std::string& lp_file);
65#endif // defined(USE_LP_PARSER)
66
67 const MPModelProto& model() const;
68 MPModelProto* mutable_model();
69
70 // Direct low level model building API.
71 int AddVar();
72 void SetVarLowerBound(int var_index, double lb);
73 void SetVarUpperBound(int var_index, double ub);
74 void SetVarIntegrality(int var_index, bool is_integer);
75 void SetVarObjectiveCoefficient(int var_index, double coeff);
76 void SetVarName(int var_index, const std::string& name);
77
78 double VarLowerBound(int var_index) const;
79 double VarUpperBound(int var_index) const;
80 bool VarIsIntegral(int var_index) const;
81 double VarObjectiveCoefficient(int var_index) const;
82 std::string VarName(int var_index) const;
83
84 double ConstraintLowerBound(int ct_index) const;
85 double ConstraintUpperBound(int ct_index) const;
87 std::string ConstraintName(int ct_index) const;
88 std::vector<double> ConstraintCoefficients(int ct_index) const;
89 std::vector<int> ConstraintVarIndices(int ct_index) const;
90 void AddConstraintTerm(int ct_index, int var_index, double coeff);
92 void SafeAddConstraintTerm(int ct_index, int var_index, double coeff);
93 void SetConstraintCoefficient(int ct_index, int var_index, double coeff);
94 void SetConstraintLowerBound(int ct_index, double lb);
95 void SetConstraintName(int ct_index, const std::string& name);
96 void SetConstraintUpperBound(int ct_index, double ub);
97
98 bool EnforcedIndicatorValue(int ct_index) const;
99 bool IsEnforcedConstraint(int ct_index) const;
100 double EnforcedConstraintLowerBound(int ct_index) const;
101 double EnforcedConstraintUpperBound(int ct_index) const;
104 std::string EnforcedConstraintName(int ct_index) const;
105 std::vector<double> EnforcedConstraintCoefficients(int ct_index) const;
106 std::vector<int> EnforcedConstraintVarIndices(int ct_index) const;
107 void AddEnforcedConstraintTerm(int ct_index, int var_index, double coeff);
109 void SafeAddEnforcedConstraintTerm(int ct_index, int var_index, double coeff);
111 double coeff);
112 void SetEnforcedConstraintLowerBound(int ct_index, double lb);
113 void SetEnforcedConstraintName(int ct_index, const std::string& name);
114 void SetEnforcedConstraintUpperBound(int ct_index, double ub);
115 void SetEnforcedIndicatorValue(int ct_index, bool positive);
117
118 int num_constraints() const;
119 int num_variables() const;
120
121 std::string name() const;
122 void SetName(const std::string& name);
123
124 void ClearObjective();
125 bool maximize() const;
126 void SetMaximize(bool maximize);
127 double ObjectiveOffset() const;
128 void SetObjectiveOffset(double offset);
129
130 void ClearHints();
131 void AddHint(int var_index, double var_value);
132
133 private:
134 MPModelProto model_;
135};
136
137// Simple director class for C#.
139 public:
140 virtual ~MbLogCallback() {}
141 virtual void NewMessage(const std::string& message) = 0;
142};
143
159
160// Class used to solve a request. This class is not meant to be exposed to the
161// public. Its responsibility is to bridge the MPModelProto in the non-C++
162// languages with the C++ Solve method.
163//
164// It contains 2 helper objects: a logger, and an atomic bool to interrupt
165// search.
167 public:
168 explicit ModelSolverHelper(const std::string& solver_name);
169 bool SolverIsSupported() const;
170 void Solve(const ModelBuilderHelper& model);
171
172 // Only used by the CVXPY interface. Does not store the response internally.
173 std::optional<MPSolutionResponse> SolveRequest(const MPModelRequest& request);
174
175 // Returns true if the interrupt signal was correctly sent, that is if the
176 // underlying solver supports it.
177 bool InterruptSolve();
178
179 void SetLogCallback(std::function<void(const std::string&)> log_callback);
181 void ClearLogCallback();
182
183 bool has_response() const;
184 bool has_solution() const;
185 const MPSolutionResponse& response() const;
186 SolveStatus status() const;
187
188 // If not defined, or no solution, they will silently return 0.
189 double objective_value() const;
190 double best_objective_bound() const;
191 double variable_value(int var_index) const;
192 double reduced_cost(int var_index) const;
193 double dual_value(int ct_index) const;
194 double activity(int ct_index);
195
196 std::string status_string() const;
197 double wall_time() const;
198 double user_time() const;
199
200 // Solve parameters.
201 void SetTimeLimitInSeconds(double limit);
203 const std::string& solver_specific_parameters);
204 void EnableOutput(bool enabled);
205
206 // TODO(user): set parameters.
207
208 private:
209 SolveInterrupter interrupter_;
210 std::atomic<bool> interrupt_solve_ = false;
211 std::function<void(const std::string&)> log_callback_;
212 std::optional<MPSolutionResponse> response_;
213 std::optional<MPModelRequest::SolverType> solver_type_;
214 std::optional<double> time_limit_in_second_;
215 std::string solver_specific_parameters_;
216 std::optional<const MPModelProto*> model_of_last_solve_;
217 std::vector<double> activities_;
218 bool solver_output_ = false;
219};
220
221} // namespace operations_research
222
223#endif // OR_TOOLS_LINEAR_SOLVER_WRAPPERS_MODEL_BUILDER_HELPER_H_
Simple director class for C#.
virtual void NewMessage(const std::string &message)=0
void SetEnforcedConstraintLowerBound(int ct_index, double lb)
std::string ExportToLpString(const operations_research::MPModelExportOptions &options=MPModelExportOptions())
bool ReadModelFromProtoFile(const std::string &filename)
void SetConstraintCoefficient(int ct_index, int var_index, double coeff)
void SafeAddEnforcedConstraintTerm(int ct_index, int var_index, double coeff)
void SetConstraintUpperBound(int ct_index, double ub)
void SetConstraintName(int ct_index, const std::string &name)
void SafeAddConstraintTerm(int ct_index, int var_index, double coeff)
void AddHint(int var_index, double var_value)
std::string VarName(int var_index) const
void SetVarObjectiveCoefficient(int var_index, double coeff)
void SetVarName(int var_index, const std::string &name)
int AddVar()
Direct low level model building API.
bool ImportFromMpsFile(const std::string &mps_file)
void SetEnforcedConstraintUpperBound(int ct_index, double ub)
void SetEnforcedConstraintName(int ct_index, const std::string &name)
double VarObjectiveCoefficient(int var_index) const
double EnforcedConstraintLowerBound(int ct_index) const
void AddConstraintTerm(int ct_index, int var_index, double coeff)
void SetEnforcedIndicatorVariableIndex(int ct_index, int var_index)
std::vector< double > ConstraintCoefficients(int ct_index) const
void SetConstraintLowerBound(int ct_index, double lb)
void SetEnforcedConstraintCoefficient(int ct_index, int var_index, double coeff)
bool ImportFromMpsString(const std::string &mps_string)
void AddEnforcedConstraintTerm(int ct_index, int var_index, double coeff)
bool WriteToMpsFile(const std::string &filename, const operations_research::MPModelExportOptions &options=MPModelExportOptions())
std::string ConstraintName(int ct_index) const
std::vector< double > EnforcedConstraintCoefficients(int ct_index) const
void SetEnforcedIndicatorValue(int ct_index, bool positive)
void SetVarIntegrality(int var_index, bool is_integer)
std::string EnforcedConstraintName(int ct_index) const
std::vector< int > EnforcedConstraintVarIndices(int ct_index) const
void SetVarUpperBound(int var_index, double ub)
std::string ExportToMpsString(const operations_research::MPModelExportOptions &options=MPModelExportOptions())
double EnforcedConstraintUpperBound(int ct_index) const
void SetVarLowerBound(int var_index, double lb)
bool WriteModelToProtoFile(const std::string &filename)
std::vector< int > ConstraintVarIndices(int ct_index) const
void OverwriteModel(const ModelBuilderHelper &other_helper)
void Solve(const ModelBuilderHelper &model)
void SetSolverSpecificParameters(const std::string &solver_specific_parameters)
double objective_value() const
If not defined, or no solution, they will silently return 0.
std::optional< MPSolutionResponse > SolveRequest(const MPModelRequest &request)
Only used by the CVXPY interface. Does not store the response internally.
void SetLogCallback(std::function< void(const std::string &)> log_callback)
const MPSolutionResponse & response() const
void SetLogCallbackFromDirectorClass(MbLogCallback *log_callback)
ModelSolverHelper(const std::string &solver_name)
void SetTimeLimitInSeconds(double limit)
Solve parameters.
GRBmodel * model
int ct_index
In SWIG mode, we don't want anything besides these top-level includes.
int var_index
Definition search.cc:3268
std::string message
Definition trace.cc:397