Google OR-Tools v9.12
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-2025 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 <algorithm>
18#include <atomic>
19#include <cstdint>
20#include <functional>
21#include <memory>
22#include <optional>
23#include <string>
24#include <utility>
25#include <vector>
26
27#include "absl/container/btree_map.h"
28#include "absl/container/fixed_array.h"
29#include "absl/strings/str_cat.h"
30#include "absl/strings/str_join.h"
31#include "ortools/linear_solver/linear_solver.pb.h"
34
35namespace operations_research {
36namespace mb {
37
38#if !defined(SWIG)
39// Base implementation of linear expressions.
40
42class FlatExpr;
43class ExprVisitor;
44class LinearExpr;
47class Variable;
48
49// A linear expression that containing variables and constants.
50class LinearExpr : public std::enable_shared_from_this<LinearExpr> {
51 public:
52 virtual ~LinearExpr() = default;
53 virtual void Visit(ExprVisitor& /*lin*/, double /*c*/) = 0;
54 virtual std::string ToString() const = 0;
55 virtual std::string DebugString() const = 0;
56
57 static std::shared_ptr<LinearExpr> Term(std::shared_ptr<LinearExpr> expr,
58 double coeff);
59 static std::shared_ptr<LinearExpr> Affine(std::shared_ptr<LinearExpr> expr,
60 double coeff, double constant);
61 static std::shared_ptr<LinearExpr> AffineCst(double value, double coeff,
62 double constant);
63 static std::shared_ptr<LinearExpr> Constant(double value);
64
65 std::shared_ptr<LinearExpr> Add(std::shared_ptr<LinearExpr> expr);
66 std::shared_ptr<LinearExpr> AddFloat(double cst);
67 std::shared_ptr<LinearExpr> Sub(std::shared_ptr<LinearExpr> expr);
68 std::shared_ptr<LinearExpr> SubFloat(double cst);
69 std::shared_ptr<LinearExpr> RSubFloat(double cst);
70 std::shared_ptr<LinearExpr> MulFloat(double cst);
71 std::shared_ptr<LinearExpr> Neg();
72
73 std::shared_ptr<BoundedLinearExpression> Eq(std::shared_ptr<LinearExpr> rhs);
74 std::shared_ptr<BoundedLinearExpression> EqCst(double rhs);
75 std::shared_ptr<BoundedLinearExpression> Ge(std::shared_ptr<LinearExpr> rhs);
76 std::shared_ptr<BoundedLinearExpression> GeCst(double rhs);
77 std::shared_ptr<BoundedLinearExpression> Le(std::shared_ptr<LinearExpr> rhs);
78 std::shared_ptr<BoundedLinearExpression> LeCst(double rhs);
79};
80
81// Compare the indices of variables.
83 bool operator()(std::shared_ptr<Variable> lhs,
84 std::shared_ptr<Variable> rhs) const;
85};
86
87// A visitor class to parse a floating point linear expression.
89 public:
90 virtual ~ExprVisitor() = default;
91 void AddToProcess(std::shared_ptr<LinearExpr> expr, double coeff);
92 void AddConstant(double constant);
93 virtual void AddVarCoeff(std::shared_ptr<Variable> var, double coeff) = 0;
94 void Clear();
95
96 protected:
97 std::vector<std::pair<std::shared_ptr<LinearExpr>, double>> to_process_;
98 double offset_ = 0;
99};
100
102 public:
103 ~ExprFlattener() override = default;
104 void AddVarCoeff(std::shared_ptr<Variable> var, double coeff) override;
105 double Flatten(std::vector<std::shared_ptr<Variable>>* vars,
106 std::vector<double>* coeffs);
107
108 private:
109 absl::btree_map<std::shared_ptr<Variable>, double, VariableComparator>
110 canonical_terms_;
111};
112
114 public:
115 explicit ExprEvaluator(ModelSolverHelper* helper) : helper_(helper) {}
116 ~ExprEvaluator() override = default;
117 void AddVarCoeff(std::shared_ptr<Variable> var, double coeff) override;
118 double Evaluate();
119
120 private:
121 ModelSolverHelper* helper_;
122};
123
124// A flat linear expression sum(vars[i] * coeffs[i]) + offset
125class FlatExpr : public LinearExpr {
126 public:
127 explicit FlatExpr(std::shared_ptr<LinearExpr> expr);
128 // Flatten pos - neg.
129 FlatExpr(std::shared_ptr<LinearExpr> pos, std::shared_ptr<LinearExpr> neg);
130 FlatExpr(const std::vector<std::shared_ptr<Variable>>&,
131 const std::vector<double>&, double);
132 explicit FlatExpr(double offset);
133 const std::vector<std::shared_ptr<Variable>>& vars() const { return vars_; }
134 std::vector<int> VarIndices() const;
135 const std::vector<double>& coeffs() const { return coeffs_; }
136 double offset() const { return offset_; }
137
138 void Visit(ExprVisitor& lin, double c) override;
139 std::string ToString() const override;
140 std::string DebugString() const override;
141
142 private:
143 std::vector<std::shared_ptr<Variable>> vars_;
144 std::vector<double> coeffs_;
145 double offset_;
146};
147
148// A class to hold a sum of linear expressions, and optional integer and
149// double offsets.
150class SumArray : public LinearExpr {
151 public:
152 explicit SumArray(std::vector<std::shared_ptr<LinearExpr>> exprs,
153 double offset)
154 : exprs_(std::move(exprs)), offset_(offset) {}
155 ~SumArray() override = default;
156
157 void Visit(ExprVisitor& lin, double c) override {
158 for (int i = 0; i < exprs_.size(); ++i) {
159 lin.AddToProcess(exprs_[i], c);
160 }
161 if (offset_ != 0.0) {
162 lin.AddConstant(offset_ * c);
163 }
164 }
165
166 std::string ToString() const override {
167 if (exprs_.empty()) {
168 if (offset_ != 0.0) {
169 return absl::StrCat(offset_);
170 }
171 }
172 std::string s = "(";
173 for (int i = 0; i < exprs_.size(); ++i) {
174 if (i > 0) {
175 absl::StrAppend(&s, " + ");
176 }
177 absl::StrAppend(&s, exprs_[i]->ToString());
178 }
179 if (offset_ != 0.0) {
180 if (offset_ > 0.0) {
181 absl::StrAppend(&s, " + ", offset_);
182 } else {
183 absl::StrAppend(&s, " - ", -offset_);
184 }
185 }
186 absl::StrAppend(&s, ")");
187 return s;
188 }
189
190 std::string DebugString() const override {
191 std::string s = absl::StrCat(
192 "SumArray(",
193 absl::StrJoin(exprs_, ", ",
194 [](std::string* out, std::shared_ptr<LinearExpr> expr) {
195 absl::StrAppend(out, expr->DebugString());
196 }));
197 if (offset_ != 0.0) {
198 absl::StrAppend(&s, ", offset=", offset_);
199 }
200 absl::StrAppend(&s, ")");
201 return s;
202 }
203
204 void AddInPlace(std::shared_ptr<LinearExpr> expr) { exprs_.push_back(expr); }
205 void AddFloatInPlace(double cst) { offset_ += cst; }
206 int num_exprs() const { return exprs_.size(); }
207 double offset() const { return offset_; }
208
209 private:
210 std::vector<std::shared_ptr<LinearExpr>> exprs_;
211 double offset_;
212};
213
214// A class to hold a weighted sum of floating point linear expressions.
216 public:
217 WeightedSumArray(const std::vector<std::shared_ptr<LinearExpr>>& exprs,
218 const std::vector<double>& coeffs, double offset);
219 ~WeightedSumArray() override = default;
220
221 void Visit(ExprVisitor& lin, double c) override;
222 std::string ToString() const override;
223 std::string DebugString() const override;
224
225 private:
226 const absl::FixedArray<std::shared_ptr<LinearExpr>, 2> exprs_;
227 const absl::FixedArray<double, 2> coeffs_;
228 double offset_;
229};
230
231// A class to hold linear_expr * a = b.
232class AffineExpr : public LinearExpr {
233 public:
234 AffineExpr(std::shared_ptr<LinearExpr> expr, double coeff, double offset);
235 ~AffineExpr() override = default;
236
237 void Visit(ExprVisitor& lin, double c) override;
238
239 std::string ToString() const override;
240 std::string DebugString() const override;
241
242 std::shared_ptr<LinearExpr> expression() const { return expr_; }
243 double coefficient() const { return coeff_; }
244 double offset() const { return offset_; }
245
246 std::shared_ptr<LinearExpr> AddFloat(double cst);
247 std::shared_ptr<LinearExpr> SubFloat(double cst);
248 std::shared_ptr<LinearExpr> RSubFloat(double cst);
249 std::shared_ptr<LinearExpr> MulFloat(double cst);
250 std::shared_ptr<LinearExpr> Neg();
251
252 private:
253 std::shared_ptr<LinearExpr> expr_;
254 double coeff_;
255 double offset_;
256};
257
258// A class to hold a fixed value.
259class FixedValue : public LinearExpr {
260 public:
261 explicit FixedValue(double value) : value_(value) {}
262 ~FixedValue() override = default;
263
264 void Visit(ExprVisitor& lin, double c) override;
265
266 std::string ToString() const override;
267 std::string DebugString() const override;
268
269 private:
270 double value_;
271};
272
273// A class to hold a variable index.
274class Variable : public LinearExpr {
275 public:
277 Variable(ModelBuilderHelper* helper, double lb, double ub, bool is_integral);
278 Variable(ModelBuilderHelper* helper, double lb, double ub, bool is_integral,
279 const std::string& name);
280 Variable(ModelBuilderHelper* helper, int64_t lb, int64_t ub,
281 bool is_integral);
282 Variable(ModelBuilderHelper* helper, int64_t lb, int64_t ub, bool is_integral,
283 const std::string& name);
284 ~Variable() override {}
285
286 ModelBuilderHelper* helper() const { return helper_; }
287 int index() const { return index_; }
288 std::string name() const;
289 void SetName(const std::string& name);
290 double lower_bounds() const;
291 void SetLowerBound(double lb);
292 double upper_bound() const;
293 void SetUpperBound(double ub);
294 bool is_integral() const;
295 void SetIsIntegral(bool is_integral);
296 double objective_coefficient() const;
297 void SetObjectiveCoefficient(double coeff);
298
299 void Visit(ExprVisitor& lin, double c) override {
300 std::shared_ptr<Variable> var =
301 std::static_pointer_cast<Variable>(shared_from_this());
302 lin.AddVarCoeff(var, c);
303 }
304
305 std::string ToString() const override;
306
307 std::string DebugString() const override;
308
309 bool operator<(const Variable& other) const { return index_ < other.index_; }
310
311 protected:
314};
315
316template <typename H>
317H AbslHashValue(H h, std::shared_ptr<Variable> i) {
318 return H::combine(std::move(h), i->index());
319}
320
321// A class to hold a linear expression with bounds.
323 public:
324 BoundedLinearExpression(std::shared_ptr<LinearExpr> expr, double lower_bound,
325 double upper_bound);
326 BoundedLinearExpression(std::shared_ptr<LinearExpr> pos,
327 std::shared_ptr<LinearExpr> neg, double lower_bound,
328 double upper_bound);
329 BoundedLinearExpression(std::shared_ptr<LinearExpr> expr, int64_t lower_bound,
330 int64_t upper_bound);
331 BoundedLinearExpression(std::shared_ptr<LinearExpr> pos,
332 std::shared_ptr<LinearExpr> neg, int64_t lower_bound,
333 int64_t upper_bound);
334
336
337 double lower_bound() const;
338 double upper_bound() const;
339 const std::vector<std::shared_ptr<Variable>>& vars() const;
340 const std::vector<double>& coeffs() const;
341 std::string ToString() const;
342 std::string DebugString() const;
343 bool CastToBool(bool* result) const;
344
345 private:
346 std::vector<std::shared_ptr<Variable>> vars_;
347 std::vector<double> coeffs_;
348 double lower_bound_;
349 double upper_bound_;
350};
351
352#endif // !defined(SWIG)
353
354// The arguments of the functions defined below must follow these rules
355// to be wrapped by SWIG correctly:
356// 1) Their types must include the full operations_research::
357// namespace.
358// 2) Their names must correspond to the ones declared in the .i
359// file (see the java/ and csharp/ subdirectories).
360
361// Helper for importing/exporting models and model protobufs.
362//
363// Wrapping global function is brittle with SWIG. It is much easier to
364// wrap static class methods.
365//
366// Note: all these methods rely on c++ code that uses absl::Status or
367// absl::StatusOr. Unfortunately, these are inconsistently wrapped in non C++
368// languages. As a consequence, we need to provide an API that does not involve
369// absl::Status or absl::StatusOr.
371 public:
372 void OverwriteModel(const ModelBuilderHelper& other_helper);
374 options = MPModelExportOptions());
376 options = MPModelExportOptions());
377 bool WriteToMpsFile(const std::string& filename,
380 bool ReadModelFromProtoFile(const std::string& filename);
381 bool WriteModelToProtoFile(const std::string& filename);
382
383 bool ImportFromMpsString(const std::string& mps_string);
384 bool ImportFromMpsFile(const std::string& mps_file);
385#if defined(USE_LP_PARSER)
386 bool ImportFromLpString(const std::string& lp_string);
387 bool ImportFromLpFile(const std::string& lp_file);
388#endif // defined(USE_LP_PARSER)
389
390 const MPModelProto& model() const;
391 MPModelProto* mutable_model();
392
393 // Direct low level model building API.
394 int AddVar();
395 void SetVarLowerBound(int var_index, double lb);
396 void SetVarUpperBound(int var_index, double ub);
397 void SetVarIntegrality(int var_index, bool is_integer);
398 void SetVarObjectiveCoefficient(int var_index, double coeff);
399 void SetVarName(int var_index, const std::string& name);
400
401 double VarLowerBound(int var_index) const;
402 double VarUpperBound(int var_index) const;
403 bool VarIsIntegral(int var_index) const;
404 double VarObjectiveCoefficient(int var_index) const;
405 std::string VarName(int var_index) const;
406
407 double ConstraintLowerBound(int ct_index) const;
408 double ConstraintUpperBound(int ct_index) const;
410 std::string ConstraintName(int ct_index) const;
411 std::vector<double> ConstraintCoefficients(int ct_index) const;
412 std::vector<int> ConstraintVarIndices(int ct_index) const;
413 void AddConstraintTerm(int ct_index, int var_index, double coeff);
414 void ClearConstraintTerms(int ct_index);
415 void SafeAddConstraintTerm(int ct_index, int var_index, double coeff);
416 void SetConstraintCoefficient(int ct_index, int var_index, double coeff);
417 void SetConstraintLowerBound(int ct_index, double lb);
418 void SetConstraintName(int ct_index, const std::string& name);
419 void SetConstraintUpperBound(int ct_index, double ub);
420
421 bool EnforcedIndicatorValue(int ct_index) const;
422 bool IsEnforcedConstraint(int ct_index) const;
423 double EnforcedConstraintLowerBound(int ct_index) const;
424 double EnforcedConstraintUpperBound(int ct_index) const;
426 int EnforcedIndicatorVariableIndex(int ct_index) const;
427 std::string EnforcedConstraintName(int ct_index) const;
428 std::vector<double> EnforcedConstraintCoefficients(int ct_index) const;
429 std::vector<int> EnforcedConstraintVarIndices(int ct_index) const;
430 void AddEnforcedConstraintTerm(int ct_index, int var_index, double coeff);
431 void ClearEnforcedConstraintTerms(int ct_index);
432 void SafeAddEnforcedConstraintTerm(int ct_index, int var_index, double coeff);
433 void SetEnforcedConstraintCoefficient(int ct_index, int var_index,
434 double coeff);
435 void SetEnforcedConstraintLowerBound(int ct_index, double lb);
436 void SetEnforcedConstraintName(int ct_index, const std::string& name);
437 void SetEnforcedConstraintUpperBound(int ct_index, double ub);
438 void SetEnforcedIndicatorValue(int ct_index, bool positive);
439 void SetEnforcedIndicatorVariableIndex(int ct_index, int var_index);
440
441 int num_constraints() const;
442 int num_variables() const;
443
444 std::string name() const;
445 void SetName(const std::string& name);
446
447 void ClearObjective();
448 bool maximize() const;
449 void SetMaximize(bool maximize);
450 double ObjectiveOffset() const;
451 void SetObjectiveOffset(double offset);
452
453 void ClearHints();
454 void AddHint(int var_index, double var_value);
455
456 private:
457 MPModelProto model_;
458};
459
460// Simple director class for C#.
462 public:
463 virtual ~MbLogCallback() {}
464 virtual void NewMessage(const std::string& message) = 0;
465};
466
482
483// Class used to solve a request. This class is not meant to be exposed to the
484// public. Its responsibility is to bridge the MPModelProto in the non-C++
485// languages with the C++ Solve method.
486//
487// It contains 2 helper objects: a logger, and an atomic bool to interrupt
488// search.
490 public:
491 explicit ModelSolverHelper(const std::string& solver_name);
492 bool SolverIsSupported() const;
493 void Solve(const ModelBuilderHelper& model);
494
495 // Only used by the CVXPY interface. Does not store the response internally.
496 std::optional<MPSolutionResponse> SolveRequest(const MPModelRequest& request);
497
498 // Returns true if the interrupt signal was correctly sent, that is if the
499 // underlying solver supports it.
500 bool InterruptSolve();
501
502 void SetLogCallback(std::function<void(const std::string&)> log_callback);
504 void ClearLogCallback();
505
506 bool has_response() const;
507 bool has_solution() const;
508 const MPSolutionResponse& response() const;
509 SolveStatus status() const;
510
511 // If not defined, or no solution, they will silently return 0.
512 double objective_value() const;
513 double best_objective_bound() const;
514 double variable_value(int var_index) const;
515#if !defined(SWIG)
516 double expression_value(std::shared_ptr<LinearExpr> expr) const;
517#endif // !defined(SWIG)
518 double reduced_cost(int var_index) const;
519 double dual_value(int ct_index) const;
520 double activity(int ct_index);
521
522 std::string status_string() const;
523 double wall_time() const;
524 double user_time() const;
525
526 // Solve parameters.
527 void SetTimeLimitInSeconds(double limit);
529 const std::string& solver_specific_parameters);
530 void EnableOutput(bool enabled);
531
532 // TODO(user): set parameters.
533
534 private:
535 SolveInterrupter interrupter_;
536 std::atomic<bool> interrupt_solve_ = false;
537 std::function<void(const std::string&)> log_callback_;
538 std::optional<MPSolutionResponse> response_;
539 std::optional<MPModelRequest::SolverType> solver_type_;
540 std::optional<double> time_limit_in_second_;
541 std::string solver_specific_parameters_;
542 std::optional<const MPModelProto*> model_of_last_solve_;
543 std::vector<double> activities_;
544 bool solver_output_ = false;
545 mutable ExprEvaluator evaluator_;
546};
547
548} // namespace mb
549} // namespace operations_research
550
551#endif // OR_TOOLS_LINEAR_SOLVER_WRAPPERS_MODEL_BUILDER_HELPER_H_
AffineExpr(std::shared_ptr< LinearExpr > expr, double coeff, double offset)
std::string DebugString() const override
std::shared_ptr< LinearExpr > Neg()
std::shared_ptr< LinearExpr > AddFloat(double cst)
std::shared_ptr< LinearExpr > RSubFloat(double cst)
void Visit(ExprVisitor &lin, double c) override
std::shared_ptr< LinearExpr > MulFloat(double cst)
std::shared_ptr< LinearExpr > SubFloat(double cst)
std::shared_ptr< LinearExpr > expression() const
A class to hold a linear expression with bounds.
BoundedLinearExpression(std::shared_ptr< LinearExpr > expr, double lower_bound, double upper_bound)
const std::vector< std::shared_ptr< Variable > > & vars() const
void AddVarCoeff(std::shared_ptr< Variable > var, double coeff) override
double Flatten(std::vector< std::shared_ptr< Variable > > *vars, std::vector< double > *coeffs)
void AddVarCoeff(std::shared_ptr< Variable > var, double coeff) override
A visitor class to parse a floating point linear expression.
std::vector< std::pair< std::shared_ptr< LinearExpr >, double > > to_process_
void AddToProcess(std::shared_ptr< LinearExpr > expr, double coeff)
Expression visitors.
virtual void AddVarCoeff(std::shared_ptr< Variable > var, double coeff)=0
void Visit(ExprVisitor &lin, double c) override
std::string DebugString() const override
A flat linear expression sum(vars[i] * coeffs[i]) + offset.
std::string ToString() const override
void Visit(ExprVisitor &lin, double c) override
const std::vector< std::shared_ptr< Variable > > & vars() const
FlatExpr(std::shared_ptr< LinearExpr > expr)
const std::vector< double > & coeffs() const
std::vector< int > VarIndices() const
std::string DebugString() const override
A linear expression that containing variables and constants.
virtual std::string ToString() const =0
std::shared_ptr< LinearExpr > Add(std::shared_ptr< LinearExpr > expr)
std::shared_ptr< BoundedLinearExpression > Le(std::shared_ptr< LinearExpr > rhs)
virtual std::string DebugString() const =0
std::shared_ptr< LinearExpr > Sub(std::shared_ptr< LinearExpr > expr)
std::shared_ptr< BoundedLinearExpression > EqCst(double rhs)
std::shared_ptr< BoundedLinearExpression > LeCst(double rhs)
static std::shared_ptr< LinearExpr > Affine(std::shared_ptr< LinearExpr > expr, double coeff, double constant)
static std::shared_ptr< LinearExpr > Constant(double value)
static std::shared_ptr< LinearExpr > AffineCst(double value, double coeff, double constant)
std::shared_ptr< BoundedLinearExpression > Ge(std::shared_ptr< LinearExpr > rhs)
std::shared_ptr< LinearExpr > Neg()
std::shared_ptr< LinearExpr > RSubFloat(double cst)
std::shared_ptr< LinearExpr > AddFloat(double cst)
virtual void Visit(ExprVisitor &, double)=0
std::shared_ptr< LinearExpr > MulFloat(double cst)
static std::shared_ptr< LinearExpr > Term(std::shared_ptr< LinearExpr > expr, double coeff)
Expressions.
std::shared_ptr< BoundedLinearExpression > GeCst(double rhs)
std::shared_ptr< BoundedLinearExpression > Eq(std::shared_ptr< LinearExpr > rhs)
std::shared_ptr< LinearExpr > SubFloat(double cst)
virtual void NewMessage(const std::string &message)=0
void SetEnforcedConstraintLowerBound(int ct_index, double lb)
std::vector< int > ConstraintVarIndices(int ct_index) const
std::vector< double > EnforcedConstraintCoefficients(int ct_index) const
void SetVarIntegrality(int var_index, bool is_integer)
void SetEnforcedIndicatorValue(int ct_index, bool positive)
void SafeAddEnforcedConstraintTerm(int ct_index, int var_index, double coeff)
void SetEnforcedConstraintName(int ct_index, const std::string &name)
void SetVarName(int var_index, const std::string &name)
void AddConstraintTerm(int ct_index, int var_index, double coeff)
bool WriteToMpsFile(const std::string &filename, const operations_research::MPModelExportOptions &options=MPModelExportOptions())
bool ReadModelFromProtoFile(const std::string &filename)
std::string ExportToMpsString(const operations_research::MPModelExportOptions &options=MPModelExportOptions())
void SetEnforcedConstraintCoefficient(int ct_index, int var_index, double coeff)
std::string EnforcedConstraintName(int ct_index) const
void AddEnforcedConstraintTerm(int ct_index, int var_index, double coeff)
void OverwriteModel(const ModelBuilderHelper &other_helper)
ModelBuilderHelper.
bool WriteModelToProtoFile(const std::string &filename)
void SetEnforcedIndicatorVariableIndex(int ct_index, int var_index)
void SetConstraintLowerBound(int ct_index, double lb)
bool ImportFromMpsFile(const std::string &mps_file)
void SetEnforcedConstraintUpperBound(int ct_index, double ub)
void AddHint(int var_index, double var_value)
int AddVar()
Direct low level model building API.
void SetVarObjectiveCoefficient(int var_index, double coeff)
void SetConstraintName(int ct_index, const std::string &name)
bool ImportFromMpsString(const std::string &mps_string)
void SetConstraintUpperBound(int ct_index, double ub)
std::vector< double > ConstraintCoefficients(int ct_index) const
void SafeAddConstraintTerm(int ct_index, int var_index, double coeff)
void SetConstraintCoefficient(int ct_index, int var_index, double coeff)
std::string ExportToLpString(const operations_research::MPModelExportOptions &options=MPModelExportOptions())
std::vector< int > EnforcedConstraintVarIndices(int ct_index) const
void Solve(const ModelBuilderHelper &model)
void SetSolverSpecificParameters(const std::string &solver_specific_parameters)
ModelSolverHelper(const std::string &solver_name)
std::optional< MPSolutionResponse > SolveRequest(const MPModelRequest &request)
Only used by the CVXPY interface. Does not store the response internally.
double objective_value() const
If not defined, or no solution, they will silently return 0.
const MPSolutionResponse & response() const
void SetLogCallbackFromDirectorClass(MbLogCallback *log_callback)
double expression_value(std::shared_ptr< LinearExpr > expr) const
void SetTimeLimitInSeconds(double limit)
Solve parameters.
void SetLogCallback(std::function< void(const std::string &)> log_callback)
void Visit(ExprVisitor &lin, double c) override
std::string ToString() const override
void AddInPlace(std::shared_ptr< LinearExpr > expr)
std::string DebugString() const override
SumArray(std::vector< std::shared_ptr< LinearExpr > > exprs, double offset)
A class to hold a variable index.
bool operator<(const Variable &other) const
Variable(ModelBuilderHelper *helper, int index)
void Visit(ExprVisitor &lin, double c) override
void SetName(const std::string &name)
ModelBuilderHelper * helper() const
std::string DebugString() const override
std::string ToString() const override
WeightedSumArray(const std::vector< std::shared_ptr< LinearExpr > > &exprs, const std::vector< double > &coeffs, double offset)
void Visit(ExprVisitor &lin, double c) override
H AbslHashValue(H h, std::shared_ptr< Variable > i)
In SWIG mode, we don't want anything besides these top-level includes.
STL namespace.
bool operator()(std::shared_ptr< Variable > lhs, std::shared_ptr< Variable > rhs) const