OR-Tools  8.2
linear_solver.h
Go to the documentation of this file.
1 // Copyright 2010-2018 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 
134 #ifndef OR_TOOLS_LINEAR_SOLVER_LINEAR_SOLVER_H_
135 #define OR_TOOLS_LINEAR_SOLVER_LINEAR_SOLVER_H_
136 
137 #include <functional>
138 #include <limits>
139 #include <map>
140 #include <memory>
141 #include <string>
142 #include <utility>
143 #include <vector>
144 
145 #include "absl/container/flat_hash_map.h"
146 #include "absl/flags/parse.h"
147 #include "absl/flags/usage.h"
148 #include "absl/status/status.h"
149 #include "absl/strings/match.h"
150 #include "absl/strings/str_format.h"
151 #include "absl/types/optional.h"
153 #include "ortools/base/logging.h"
154 #include "ortools/base/macros.h"
155 #include "ortools/base/timer.h"
157 #include "ortools/linear_solver/linear_solver.pb.h"
160 
161 ABSL_DECLARE_FLAG(bool, linear_solver_enable_verbose_output);
162 
163 namespace operations_research {
164 
165 constexpr double kDefaultPrimalTolerance = 1e-07;
166 
167 class MPConstraint;
168 class MPObjective;
169 class MPSolverInterface;
170 class MPSolverParameters;
171 class MPVariable;
172 
173 // There is a homonymous version taking a MPSolver::OptimizationProblemType.
174 bool SolverTypeIsMip(MPModelRequest::SolverType solver_type);
175 
180 class MPSolver {
181  public:
189  // Linear programming problems.
190  // ----------------------------
193  GLOP_LINEAR_PROGRAMMING = 2, // Recommended default value. Made in Google.
194 
195  // Integer programming problems.
196  // -----------------------------
197  SCIP_MIXED_INTEGER_PROGRAMMING = 3, // Recommended default value.
200 
201  // Commercial software (need license).
208 
209  // Boolean optimization problem (requires only integer variables and works
210  // best with only Boolean variables).
212 
213  // SAT based solver (requires only integer and Boolean variables).
214  // If you pass it mixed integer problems, it will scale coefficients to
215  // integer values, and solver continuous variables as integral variables.
217 
218  // Dedicated knapsack solvers.
220  };
221 
223  MPSolver(const std::string& name, OptimizationProblemType problem_type);
224  virtual ~MPSolver();
225 
254  static MPSolver* CreateSolver(const std::string& solver_id);
255 
261 
267  static bool ParseSolverType(absl::string_view solver_id,
269 
275  const std::string& solver_id);
276 
277  bool IsMIP() const;
278 
280  const std::string& Name() const {
281  return name_; // Set at construction.
282  }
283 
286  return problem_type_; // Set at construction.
287  }
288 
294  void Clear();
295 
297  int NumVariables() const { return variables_.size(); }
298 
303  const std::vector<MPVariable*>& variables() const { return variables_; }
304 
310  MPVariable* LookupVariableOrNull(const std::string& var_name) const;
311 
319  MPVariable* MakeVar(double lb, double ub, bool integer,
320  const std::string& name);
321 
323  MPVariable* MakeNumVar(double lb, double ub, const std::string& name);
324 
326  MPVariable* MakeIntVar(double lb, double ub, const std::string& name);
327 
329  MPVariable* MakeBoolVar(const std::string& name);
330 
345  void MakeVarArray(int nb, double lb, double ub, bool integer,
346  const std::string& name_prefix,
347  std::vector<MPVariable*>* vars);
348 
350  void MakeNumVarArray(int nb, double lb, double ub, const std::string& name,
351  std::vector<MPVariable*>* vars);
352 
354  void MakeIntVarArray(int nb, double lb, double ub, const std::string& name,
355  std::vector<MPVariable*>* vars);
356 
358  void MakeBoolVarArray(int nb, const std::string& name,
359  std::vector<MPVariable*>* vars);
360 
362  int NumConstraints() const { return constraints_.size(); }
363 
369  const std::vector<MPConstraint*>& constraints() const { return constraints_; }
370 
379  const std::string& constraint_name) const;
380 
389  MPConstraint* MakeRowConstraint(double lb, double ub);
390 
393 
395  MPConstraint* MakeRowConstraint(double lb, double ub,
396  const std::string& name);
397 
399  MPConstraint* MakeRowConstraint(const std::string& name);
400 
406 
409  const std::string& name);
410 
417  const MPObjective& Objective() const { return *objective_; }
418 
420  MPObjective* MutableObjective() { return objective_.get(); }
421 
442  NOT_SOLVED = 6
443  };
444 
447 
449  ResultStatus Solve(const MPSolverParameters& param);
450 
455  void Write(const std::string& file_name);
456 
463  std::vector<double> ComputeConstraintActivities() const;
464 
483  bool VerifySolution(double tolerance, bool log_errors) const;
484 
493  void Reset();
494 
502  bool InterruptSolve();
503 
511  MPSolverResponseStatus LoadModelFromProto(const MPModelProto& input_model,
512  std::string* error_message);
520  MPSolverResponseStatus LoadModelFromProtoWithUniqueNamesOrDie(
521  const MPModelProto& input_model, std::string* error_message);
522 
524  void FillSolutionResponseProto(MPSolutionResponse* response) const;
525 
541  static void SolveWithProto(const MPModelRequest& model_request,
542  MPSolutionResponse* response);
543 
545  void ExportModelToProto(MPModelProto* output_model) const;
546 
578  absl::Status LoadSolutionFromProto(
579  const MPSolutionResponse& response,
580  double tolerance = kDefaultPrimalTolerance);
581 
586  absl::Status ClampSolutionWithinBounds();
587 
594  bool ExportModelAsLpFormat(bool obfuscate, std::string* model_str) const;
595  bool ExportModelAsMpsFormat(bool fixed_format, bool obfuscate,
596  std::string* model_str) const;
597 
608  absl::Status SetNumThreads(int num_threads);
609 
611  int GetNumThreads() const { return num_threads_; }
612 
619  bool SetSolverSpecificParametersAsString(const std::string& parameters);
621  return solver_specific_parameter_string_;
622  }
623 
637  void SetHint(std::vector<std::pair<const MPVariable*, double> > hint);
638 
643  enum BasisStatus {
644  FREE = 0,
648  BASIC
649  };
650 
662  void SetStartingLpBasis(
663  const std::vector<MPSolver::BasisStatus>& variable_statuses,
664  const std::vector<MPSolver::BasisStatus>& constraint_statuses);
665 
671  static double infinity() { return std::numeric_limits<double>::infinity(); }
672 
681  bool OutputIsEnabled() const;
682 
684  void EnableOutput();
685 
687  void SuppressOutput();
688 
689  absl::Duration TimeLimit() const { return time_limit_; }
690  void SetTimeLimit(absl::Duration time_limit) {
691  DCHECK_GE(time_limit, absl::ZeroDuration());
692  time_limit_ = time_limit;
693  }
694 
695  absl::Duration DurationSinceConstruction() const {
696  return absl::Now() - construction_time_;
697  }
698 
700  int64 iterations() const;
701 
707  int64 nodes() const;
708 
710  std::string SolverVersion() const;
711 
725  void* underlying_solver();
726 
750  double ComputeExactConditionNumber() const;
751 
766  ABSL_MUST_USE_RESULT bool NextSolution();
767 
768  // Does not take ownership of "mp_callback".
769  //
770  // As of 2019-10-22, only SCIP and Gurobi support Callbacks.
771  // SCIP does not support suggesting a heuristic solution in the callback.
772  //
773  // See go/mpsolver-callbacks for additional documentation.
774  void SetCallback(MPCallback* mp_callback);
775  bool SupportsCallbacks() const;
776 
777  // DEPRECATED: Use TimeLimit() and SetTimeLimit(absl::Duration) instead.
778  // NOTE: These deprecated functions used the convention time_limit = 0 to mean
779  // "no limit", which now corresponds to time_limit_ = InfiniteDuration().
780  int64 time_limit() const {
781  return time_limit_ == absl::InfiniteDuration()
782  ? 0
783  : absl::ToInt64Milliseconds(time_limit_);
784  }
785  void set_time_limit(int64 time_limit_milliseconds) {
786  SetTimeLimit(time_limit_milliseconds == 0
787  ? absl::InfiniteDuration()
788  : absl::Milliseconds(time_limit_milliseconds));
789  }
790  double time_limit_in_secs() const {
791  return static_cast<double>(time_limit()) / 1000.0;
792  }
793 
794  // DEPRECATED: Use DurationSinceConstruction() instead.
795  int64 wall_time() const {
796  return absl::ToInt64Milliseconds(DurationSinceConstruction());
797  }
798 
799  // Supports search and loading Gurobi shared library.
800  static bool LoadGurobiSharedLibrary();
801  static void SetGurobiLibraryPath(const std::string& full_library_path);
802 
803  friend class GLPKInterface;
804  friend class CLPInterface;
805  friend class CBCInterface;
806  friend class SCIPInterface;
807  friend class GurobiInterface;
808  friend class CplexInterface;
809  friend class XpressInterface;
810  friend class SLMInterface;
811  friend class MPSolverInterface;
812  friend class GLOPInterface;
813  friend class BopInterface;
814  friend class SatInterface;
815  friend class KnapsackInterface;
816 
817  // Debugging: verify that the given MPVariable* belongs to this solver.
818  bool OwnsVariable(const MPVariable* var) const;
819 
820  private:
821  // Computes the size of the constraint with the largest number of
822  // coefficients with index in [min_constraint_index,
823  // max_constraint_index)
824  int ComputeMaxConstraintSize(int min_constraint_index,
825  int max_constraint_index) const;
826 
827  // Returns true if the model has constraints with lower bound > upper bound.
828  bool HasInfeasibleConstraints() const;
829 
830  // Returns true if the model has at least 1 integer variable.
831  bool HasIntegerVariables() const;
832 
833  // Generates the map from variable names to their indices.
834  void GenerateVariableNameIndex() const;
835 
836  // Generates the map from constraint names to their indices.
837  void GenerateConstraintNameIndex() const;
838 
839  // Checks licenses for commercial solver, and checks shared library loading
840  // for or-tools.
841  static bool GurobiIsCorrectlyInstalled();
842 
843  // The name of the linear programming problem.
844  const std::string name_;
845 
846  // The type of the linear programming problem.
847  const OptimizationProblemType problem_type_;
848 
849  // The solver interface.
850  std::unique_ptr<MPSolverInterface> interface_;
851 
852  // The vector of variables in the problem.
853  std::vector<MPVariable*> variables_;
854  // A map from a variable's name to its index in variables_.
855  mutable absl::optional<absl::flat_hash_map<std::string, int> >
856  variable_name_to_index_;
857  // Whether variables have been extracted to the underlying interface.
858  std::vector<bool> variable_is_extracted_;
859 
860  // The vector of constraints in the problem.
861  std::vector<MPConstraint*> constraints_;
862  // A map from a constraint's name to its index in constraints_.
863  mutable absl::optional<absl::flat_hash_map<std::string, int> >
864  constraint_name_to_index_;
865  // Whether constraints have been extracted to the underlying interface.
866  std::vector<bool> constraint_is_extracted_;
867 
868  // The linear objective function.
869  std::unique_ptr<MPObjective> objective_;
870 
871  // Initial values for all or some of the problem variables that can be
872  // exploited as a starting hint by a solver.
873  //
874  // Note(user): as of 05/05/2015, we can't use >> because of some SWIG errors.
875  //
876  // TODO(user): replace by two vectors, a std::vector<bool> to indicate if a
877  // hint is provided and a std::vector<double> for the hint value.
878  std::vector<std::pair<const MPVariable*, double> > solution_hint_;
879 
880  absl::Duration time_limit_ = absl::InfiniteDuration(); // Default = No limit.
881 
882  const absl::Time construction_time_;
883 
884  // Permanent storage for the number of threads.
885  int num_threads_ = 1;
886 
887  // Permanent storage for SetSolverSpecificParametersAsString().
888  std::string solver_specific_parameter_string_;
889 
890  MPSolverResponseStatus LoadModelFromProtoInternal(
891  const MPModelProto& input_model, bool clear_names,
892  bool check_model_validity, std::string* error_message);
893 
895 };
896 
898  return SolverTypeIsMip(static_cast<MPModelRequest::SolverType>(solver_type));
899 }
900 
901 const absl::string_view ToString(
902  MPSolver::OptimizationProblemType optimization_problem_type);
903 
904 inline std::ostream& operator<<(
905  std::ostream& os,
906  MPSolver::OptimizationProblemType optimization_problem_type) {
907  return os << ToString(optimization_problem_type);
908 }
909 
910 inline std::ostream& operator<<(std::ostream& os,
911  MPSolver::ResultStatus status) {
912  return os << ProtoEnumToString<MPSolverResponseStatus>(
913  static_cast<MPSolverResponseStatus>(status));
914 }
915 
916 bool AbslParseFlag(absl::string_view text,
918  std::string* error);
919 
920 inline std::string AbslUnparseFlag(
921  MPSolver::OptimizationProblemType solver_type) {
922  return std::string(ToString(solver_type));
923 }
924 
926 class MPObjective {
927  public:
932  void Clear();
933 
940  void SetCoefficient(const MPVariable* const var, double coeff);
941 
947  double GetCoefficient(const MPVariable* const var) const;
948 
954  const absl::flat_hash_map<const MPVariable*, double>& terms() const {
955  return coefficients_;
956  }
957 
959  void SetOffset(double value);
960 
962  double offset() const { return offset_; }
963 
968  void OptimizeLinearExpr(const LinearExpr& linear_expr, bool is_maximization);
969 
971  void MaximizeLinearExpr(const LinearExpr& linear_expr) {
972  OptimizeLinearExpr(linear_expr, true);
973  }
975  void MinimizeLinearExpr(const LinearExpr& linear_expr) {
976  OptimizeLinearExpr(linear_expr, false);
977  }
978 
980  void AddLinearExpr(const LinearExpr& linear_expr);
981 
983  void SetOptimizationDirection(bool maximize);
984 
987 
990 
992  bool maximization() const;
993 
995  bool minimization() const;
996 
1008  double Value() const;
1009 
1016  double BestBound() const;
1017 
1018  private:
1019  friend class MPSolver;
1020  friend class MPSolverInterface;
1021  friend class CBCInterface;
1022  friend class CLPInterface;
1023  friend class GLPKInterface;
1024  friend class SCIPInterface;
1025  friend class SLMInterface;
1026  friend class GurobiInterface;
1027  friend class CplexInterface;
1028  friend class XpressInterface;
1029  friend class GLOPInterface;
1030  friend class BopInterface;
1031  friend class SatInterface;
1032  friend class KnapsackInterface;
1033 
1034  // Constructor. An objective points to a single MPSolverInterface
1035  // that is specified in the constructor. An objective cannot belong
1036  // to several models.
1037  // At construction, an MPObjective has no terms (which is equivalent
1038  // on having a coefficient of 0 for all variables), and an offset of 0.
1039  explicit MPObjective(MPSolverInterface* const interface_in)
1040  : interface_(interface_in), coefficients_(1), offset_(0.0) {}
1041 
1042  MPSolverInterface* const interface_;
1043 
1044  // Mapping var -> coefficient.
1045  absl::flat_hash_map<const MPVariable*, double> coefficients_;
1046  // Constant term.
1047  double offset_;
1048 
1050 };
1051 
1053 class MPVariable {
1054  public:
1056  const std::string& name() const { return name_; }
1057 
1059  void SetInteger(bool integer);
1060 
1062  bool integer() const { return integer_; }
1063 
1071  double solution_value() const;
1072 
1074  int index() const { return index_; }
1075 
1077  double lb() const { return lb_; }
1078 
1080  double ub() const { return ub_; }
1081 
1083  void SetLB(double lb) { SetBounds(lb, ub_); }
1084 
1086  void SetUB(double ub) { SetBounds(lb_, ub); }
1087 
1089  void SetBounds(double lb, double ub);
1090 
1097  double unrounded_solution_value() const;
1098 
1103  double reduced_cost() const;
1104 
1112 
1123  int branching_priority() const { return branching_priority_; }
1124  void SetBranchingPriority(int priority);
1125 
1126  protected:
1127  friend class MPSolver;
1128  friend class MPSolverInterface;
1129  friend class CBCInterface;
1130  friend class CLPInterface;
1131  friend class GLPKInterface;
1132  friend class SCIPInterface;
1133  friend class SLMInterface;
1134  friend class GurobiInterface;
1135  friend class CplexInterface;
1136  friend class XpressInterface;
1137  friend class GLOPInterface;
1139  friend class BopInterface;
1140  friend class SatInterface;
1141  friend class KnapsackInterface;
1142 
1143  // Constructor. A variable points to a single MPSolverInterface that
1144  // is specified in the constructor. A variable cannot belong to
1145  // several models.
1146  MPVariable(int index, double lb, double ub, bool integer,
1147  const std::string& name, MPSolverInterface* const interface_in)
1148  : index_(index),
1149  lb_(lb),
1150  ub_(ub),
1151  integer_(integer),
1152  name_(name.empty() ? absl::StrFormat("auto_v_%09d", index) : name),
1153  solution_value_(0.0),
1154  reduced_cost_(0.0),
1155  interface_(interface_in) {}
1156 
1157  void set_solution_value(double value) { solution_value_ = value; }
1158  void set_reduced_cost(double reduced_cost) { reduced_cost_ = reduced_cost; }
1159 
1160  private:
1161  const int index_;
1162  double lb_;
1163  double ub_;
1164  bool integer_;
1165  const std::string name_;
1166  double solution_value_;
1167  double reduced_cost_;
1168  int branching_priority_ = 0;
1169  MPSolverInterface* const interface_;
1171 };
1172 
1179  public:
1181  const std::string& name() const { return name_; }
1182 
1184  void Clear();
1185 
1192  void SetCoefficient(const MPVariable* const var, double coeff);
1193 
1198  double GetCoefficient(const MPVariable* const var) const;
1199 
1205  const absl::flat_hash_map<const MPVariable*, double>& terms() const {
1206  return coefficients_;
1207  }
1208 
1210  double lb() const { return lb_; }
1211 
1213  double ub() const { return ub_; }
1214 
1216  void SetLB(double lb) { SetBounds(lb, ub_); }
1217 
1219  void SetUB(double ub) { SetBounds(lb_, ub); }
1220 
1222  void SetBounds(double lb, double ub);
1223 
1225  bool is_lazy() const { return is_lazy_; }
1226 
1240  void set_is_lazy(bool laziness) { is_lazy_ = laziness; }
1241 
1242  const MPVariable* indicator_variable() const { return indicator_variable_; }
1243  bool indicator_value() const { return indicator_value_; }
1244 
1246  int index() const { return index_; }
1247 
1252  double dual_value() const;
1253 
1267 
1268  protected:
1269  friend class MPSolver;
1270  friend class MPSolverInterface;
1271  friend class CBCInterface;
1272  friend class CLPInterface;
1273  friend class GLPKInterface;
1274  friend class SCIPInterface;
1275  friend class SLMInterface;
1276  friend class GurobiInterface;
1277  friend class CplexInterface;
1278  friend class XpressInterface;
1279  friend class GLOPInterface;
1280  friend class BopInterface;
1281  friend class SatInterface;
1282  friend class KnapsackInterface;
1283 
1284  // Constructor. A constraint points to a single MPSolverInterface
1285  // that is specified in the constructor. A constraint cannot belong
1286  // to several models.
1287  MPConstraint(int index, double lb, double ub, const std::string& name,
1288  MPSolverInterface* const interface_in)
1289  : coefficients_(1),
1290  index_(index),
1291  lb_(lb),
1292  ub_(ub),
1293  name_(name.empty() ? absl::StrFormat("auto_c_%09d", index) : name),
1294  is_lazy_(false),
1295  indicator_variable_(nullptr),
1296  dual_value_(0.0),
1297  interface_(interface_in) {}
1298 
1299  void set_dual_value(double dual_value) { dual_value_ = dual_value; }
1300 
1301  private:
1302  // Returns true if the constraint contains variables that have not
1303  // been extracted yet.
1304  bool ContainsNewVariables();
1305 
1306  // Mapping var -> coefficient.
1307  absl::flat_hash_map<const MPVariable*, double> coefficients_;
1308 
1309  const int index_; // See index().
1310 
1311  // The lower bound for the linear constraint.
1312  double lb_;
1313 
1314  // The upper bound for the linear constraint.
1315  double ub_;
1316 
1317  // Name.
1318  const std::string name_;
1319 
1320  // True if the constraint is "lazy", i.e. the constraint is added to the
1321  // underlying Linear Programming solver only if it is violated.
1322  // By default this parameter is 'false'.
1323  bool is_lazy_;
1324 
1325  // If given, this constraint is only active if `indicator_variable_`'s value
1326  // is equal to `indicator_value_`.
1327  const MPVariable* indicator_variable_;
1328  bool indicator_value_;
1329 
1330  double dual_value_;
1331  MPSolverInterface* const interface_;
1333 };
1334 
1362  public:
1367 
1376  DUAL_TOLERANCE = 2
1377  };
1378 
1382  PRESOLVE = 1000,
1388  SCALING = 1003
1389  };
1390 
1396  PRESOLVE_ON = 1
1397  };
1398 
1402  DUAL = 10,
1404  PRIMAL = 11,
1406  BARRIER = 12
1407  };
1408 
1413 
1418  INCREMENTALITY_ON = 1
1419  };
1420 
1426  SCALING_ON = 1
1427  };
1428 
1429  // Placeholder value to indicate that a parameter is set to
1430  // the default value defined in the wrapper.
1431  static const double kDefaultDoubleParamValue;
1432  static const int kDefaultIntegerParamValue;
1433 
1434  // Placeholder value to indicate that a parameter is unknown.
1435  static const double kUnknownDoubleParamValue;
1436  static const int kUnknownIntegerParamValue;
1437 
1438  // Default values for parameters. Only parameters that define the
1439  // properties of the solution returned need to have a default value
1440  // (that is the same for all solvers). You can also define a default
1441  // value for performance parameters when you are confident it is a
1442  // good choice (example: always turn presolve on).
1443  static const double kDefaultRelativeMipGap;
1444  static const double kDefaultPrimalTolerance;
1445  static const double kDefaultDualTolerance;
1448 
1451 
1454 
1457 
1464 
1471 
1473  void Reset();
1474 
1476  double GetDoubleParam(MPSolverParameters::DoubleParam param) const;
1477 
1480 
1481  private:
1482  // Parameter value for each parameter.
1483  // @see DoubleParam
1484  // @see IntegerParam
1485  double relative_mip_gap_value_;
1486  double primal_tolerance_value_;
1487  double dual_tolerance_value_;
1488  int presolve_value_;
1489  int scaling_value_;
1490  int lp_algorithm_value_;
1491  int incrementality_value_;
1492 
1493  // Boolean value indicating whether each parameter is set to the
1494  // solver's default value. Only parameters for which the wrapper
1495  // does not define a default value need such an indicator.
1496  bool lp_algorithm_is_default_;
1497 
1498  DISALLOW_COPY_AND_ASSIGN(MPSolverParameters);
1499 };
1500 
1501 // Whether the given MPSolverResponseStatus (of a solve) would yield an RPC
1502 // error when happening on the linear solver stubby server, see
1503 // ./linear_solver_service.proto.
1504 // Note that RPC errors forbid to carry a response to the client, who can only
1505 // see the RPC error itself (error code + error message).
1506 bool MPSolverResponseStatusIsRpcError(MPSolverResponseStatus status);
1507 
1508 // This class wraps the actual mathematical programming solvers. Each
1509 // solver (GLOP, CLP, CBC, GLPK, SCIP) has its own interface class that
1510 // derives from this abstract class. This class is never directly
1511 // accessed by the user.
1512 // @see glop_interface.cc
1513 // @see cbc_interface.cc
1514 // @see clp_interface.cc
1515 // @see glpk_interface.cc
1516 // @see scip_interface.cc
1518  public:
1520  // The underlying solver (CLP, GLPK, ...) and MPSolver are not in
1521  // sync for the model nor for the solution.
1523  // The underlying solver and MPSolver are in sync for the model
1524  // but not for the solution: the model has changed since the
1525  // solution was computed last.
1527  // The underlying solver and MPSolver are in sync for the model and
1528  // the solution.
1530  };
1531 
1532  // When the underlying solver does not provide the number of simplex
1533  // iterations.
1534  static constexpr int64 kUnknownNumberOfIterations = -1;
1535  // When the underlying solver does not provide the number of
1536  // branch-and-bound nodes.
1537  static constexpr int64 kUnknownNumberOfNodes = -1;
1538 
1539  // Constructor. The user will access the MPSolverInterface through the
1540  // MPSolver passed as argument.
1541  explicit MPSolverInterface(MPSolver* const solver);
1542  virtual ~MPSolverInterface();
1543 
1544  // ----- Solve -----
1545  // Solves problem with specified parameter values. Returns true if the
1546  // solution is optimal.
1548 
1549  // Directly solves a MPModelRequest, bypassing the MPSolver data structures
1550  // entirely. Returns {} (eg. absl::nullopt) if the feature is not supported by
1551  // the underlying solver.
1552  virtual absl::optional<MPSolutionResponse> DirectlySolveProto(
1553  const MPModelRequest& request) {
1554  return absl::nullopt;
1555  }
1556 
1557  // Writes the model using the solver internal write function. Currently only
1558  // available for GurobiInterface.
1559  virtual void Write(const std::string& filename);
1560 
1561  // ----- Model modifications and extraction -----
1562  // Resets extracted model.
1563  virtual void Reset() = 0;
1564 
1565  // Sets the optimization direction (min/max).
1566  virtual void SetOptimizationDirection(bool maximize) = 0;
1567 
1568  // Modifies bounds of an extracted variable.
1569  virtual void SetVariableBounds(int index, double lb, double ub) = 0;
1570 
1571  // Modifies integrality of an extracted variable.
1572  virtual void SetVariableInteger(int index, bool integer) = 0;
1573 
1574  // Modify bounds of an extracted variable.
1575  virtual void SetConstraintBounds(int index, double lb, double ub) = 0;
1576 
1577  // Adds a linear constraint.
1578  virtual void AddRowConstraint(MPConstraint* const ct) = 0;
1579 
1580  // Adds an indicator constraint. Returns true if the feature is supported by
1581  // the underlying solver.
1582  virtual bool AddIndicatorConstraint(MPConstraint* const ct) {
1583  LOG(ERROR) << "Solver doesn't support indicator constraints.";
1584  return false;
1585  }
1586 
1587  // Add a variable.
1588  virtual void AddVariable(MPVariable* const var) = 0;
1589 
1590  // Changes a coefficient in a constraint.
1591  virtual void SetCoefficient(MPConstraint* const constraint,
1592  const MPVariable* const variable,
1593  double new_value, double old_value) = 0;
1594 
1595  // Clears a constraint from all its terms.
1596  virtual void ClearConstraint(MPConstraint* const constraint) = 0;
1597 
1598  // Changes a coefficient in the linear objective.
1599  virtual void SetObjectiveCoefficient(const MPVariable* const variable,
1600  double coefficient) = 0;
1601 
1602  // Changes the constant term in the linear objective.
1603  virtual void SetObjectiveOffset(double value) = 0;
1604 
1605  // Clears the objective from all its terms.
1606  virtual void ClearObjective() = 0;
1607 
1608  virtual void BranchingPriorityChangedForVariable(int var_index) {}
1609  // ------ Query statistics on the solution and the solve ------
1610  // Returns the number of simplex iterations. The problem must be discrete,
1611  // otherwise it crashes, or returns kUnknownNumberOfIterations in NDEBUG mode.
1612  virtual int64 iterations() const = 0;
1613  // Returns the number of branch-and-bound nodes. The problem must be discrete,
1614  // otherwise it crashes, or returns kUnknownNumberOfNodes in NDEBUG mode.
1615  virtual int64 nodes() const = 0;
1616  // Returns the best objective bound. The problem must be discrete, otherwise
1617  // it crashes, or returns trivial bound (+/- inf) in NDEBUG mode.
1618  double best_objective_bound() const;
1619  // Returns the objective value of the best solution found so far.
1620  double objective_value() const;
1621 
1622  // Returns the basis status of a row.
1623  virtual MPSolver::BasisStatus row_status(int constraint_index) const = 0;
1624  // Returns the basis status of a constraint.
1625  virtual MPSolver::BasisStatus column_status(int variable_index) const = 0;
1626 
1627  // Checks whether the solution is synchronized with the model, i.e. whether
1628  // the model has changed since the solution was computed last.
1629  // If it isn't, it crashes in NDEBUG, and returns false othwerwise.
1630  bool CheckSolutionIsSynchronized() const;
1631  // Checks whether a feasible solution exists. The behavior is similar to
1632  // CheckSolutionIsSynchronized() above.
1633  virtual bool CheckSolutionExists() const;
1634  // Handy shortcut to do both checks above (it is often used).
1637  }
1638 
1639  // ----- Misc -----
1640  // Queries problem type. For simplicity, the distinction between
1641  // continuous and discrete is based on the declaration of the user
1642  // when the solver is created (example: GLPK_LINEAR_PROGRAMMING
1643  // vs. GLPK_MIXED_INTEGER_PROGRAMMING), not on the actual content of
1644  // the model.
1645  // Returns true if the problem is continuous.
1646  virtual bool IsContinuous() const = 0;
1647  // Returns true if the problem is continuous and linear.
1648  virtual bool IsLP() const = 0;
1649  // Returns true if the problem is discrete and linear.
1650  virtual bool IsMIP() const = 0;
1651 
1652  // Returns the index of the last variable extracted.
1654 
1655  bool variable_is_extracted(int var_index) const {
1656  return solver_->variable_is_extracted_[var_index];
1657  }
1658  void set_variable_as_extracted(int var_index, bool extracted) {
1659  solver_->variable_is_extracted_[var_index] = extracted;
1660  }
1661  bool constraint_is_extracted(int ct_index) const {
1662  return solver_->constraint_is_extracted_[ct_index];
1663  }
1664  void set_constraint_as_extracted(int ct_index, bool extracted) {
1665  solver_->constraint_is_extracted_[ct_index] = extracted;
1666  }
1667 
1668  // Returns the boolean indicating the verbosity of the solver output.
1669  bool quiet() const { return quiet_; }
1670  // Sets the boolean indicating the verbosity of the solver output.
1671  void set_quiet(bool quiet_value) { quiet_ = quiet_value; }
1672 
1673  // Returns the result status of the last solve.
1676  return result_status_;
1677  }
1678 
1679  // Returns a string describing the underlying solver and its version.
1680  virtual std::string SolverVersion() const = 0;
1681 
1682  // Returns the underlying solver.
1683  virtual void* underlying_solver() = 0;
1684 
1685  // Computes exact condition number. Only available for continuous
1686  // problems and only implemented in GLPK.
1687  virtual double ComputeExactConditionNumber() const;
1688 
1689  // See MPSolver::SetStartingLpBasis().
1690  virtual void SetStartingLpBasis(
1691  const std::vector<MPSolver::BasisStatus>& variable_statuses,
1692  const std::vector<MPSolver::BasisStatus>& constraint_statuses) {
1693  LOG(FATAL) << "Not supported by this solver.";
1694  }
1695 
1696  virtual bool InterruptSolve() { return false; }
1697 
1698  // See MPSolver::NextSolution() for contract.
1699  virtual bool NextSolution() { return false; }
1700 
1701  // See MPSolver::SetCallback() for details.
1702  virtual void SetCallback(MPCallback* mp_callback) {
1703  LOG(FATAL) << "Callbacks not supported for this solver.";
1704  }
1705 
1706  virtual bool SupportsCallbacks() const { return false; }
1707 
1708  friend class MPSolver;
1709 
1710  // To access the maximize_ bool and the MPSolver.
1711  friend class MPConstraint;
1712  friend class MPObjective;
1713 
1714  protected:
1716  // Indicates whether the model and the solution are synchronized.
1718  // Indicates whether the solve has reached optimality,
1719  // infeasibility, a limit, etc.
1721  // Optimization direction.
1723 
1724  // Index in MPSolver::variables_ of last constraint extracted.
1726  // Index in MPSolver::constraints_ of last variable extracted.
1728 
1729  // The value of the objective function.
1731 
1732  // The value of the best objective bound. Used only for MIP solvers.
1734 
1735  // Boolean indicator for the verbosity of the solver output.
1736  bool quiet_;
1737 
1738  // Index of dummy variable created for empty constraints or the
1739  // objective offset.
1740  static const int kDummyVariableIndex;
1741 
1742  // Extracts model stored in MPSolver.
1743  void ExtractModel();
1744  // Extracts the variables that have not been extracted yet.
1745  virtual void ExtractNewVariables() = 0;
1746  // Extracts the constraints that have not been extracted yet.
1747  virtual void ExtractNewConstraints() = 0;
1748  // Extracts the objective.
1749  virtual void ExtractObjective() = 0;
1750  // Resets the extraction information.
1752  // Change synchronization status from SOLUTION_SYNCHRONIZED to
1753  // MODEL_SYNCHRONIZED. To be used for model changes.
1755 
1756  // Sets parameters common to LP and MIP in the underlying solver.
1757  void SetCommonParameters(const MPSolverParameters& param);
1758  // Sets MIP specific parameters in the underlying solver.
1759  void SetMIPParameters(const MPSolverParameters& param);
1760  // Sets all parameters in the underlying solver.
1761  virtual void SetParameters(const MPSolverParameters& param) = 0;
1762  // Sets an unsupported double parameter.
1764  // Sets an unsupported integer parameter.
1765  virtual void SetUnsupportedIntegerParam(
1767  // Sets a supported double parameter to an unsupported value.
1769  double value);
1770  // Sets a supported integer parameter to an unsupported value.
1771  virtual void SetIntegerParamToUnsupportedValue(
1773  // Sets each parameter in the underlying solver.
1774  virtual void SetRelativeMipGap(double value) = 0;
1775  virtual void SetPrimalTolerance(double value) = 0;
1776  virtual void SetDualTolerance(double value) = 0;
1777  virtual void SetPresolveMode(int value) = 0;
1778 
1779  // Sets the number of threads to be used by the solver.
1780  virtual absl::Status SetNumThreads(int num_threads);
1781 
1782  // Pass solver specific parameters in text format. The format is
1783  // solver-specific and is the same as the corresponding solver configuration
1784  // file format. Returns true if the operation was successful.
1785  //
1786  // Default implementation returns true if the input is empty. It returns false
1787  // and logs a WARNING if the input is not empty.
1789  const std::string& parameters);
1790 
1791  // Sets the scaling mode.
1792  virtual void SetScalingMode(int value) = 0;
1793  virtual void SetLpAlgorithm(int value) = 0;
1794 };
1795 
1796 } // namespace operations_research
1797 
1798 #endif // OR_TOOLS_LINEAR_SOLVER_LINEAR_SOLVER_H_
#define DCHECK_GE(val1, val2)
Definition: base/logging.h:889
#define LOG(severity)
Definition: base/logging.h:420
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
Definition: linear_expr.h:114
An expression of the form:
Definition: linear_expr.h:192
The class for constraints of a Mathematical Programming (MP) model.
void SetBounds(double lb, double ub)
Sets both the lower and upper bounds.
void SetCoefficient(const MPVariable *const var, double coeff)
Sets the coefficient of the variable on the constraint.
double GetCoefficient(const MPVariable *const var) const
Gets the coefficient of a given variable on the constraint (which is 0 if the variable does not appea...
void SetUB(double ub)
Sets the upper bound.
double ub() const
Returns the upper bound.
const MPVariable * indicator_variable() const
const absl::flat_hash_map< const MPVariable *, double > & terms() const
Returns a map from variables to their coefficients in the constraint.
MPConstraint(int index, double lb, double ub, const std::string &name, MPSolverInterface *const interface_in)
void Clear()
Clears all variables and coefficients. Does not clear the bounds.
bool is_lazy() const
Advanced usage: returns true if the constraint is "lazy" (see below).
void set_is_lazy(bool laziness)
Advanced usage: sets the constraint "laziness".
int index() const
Returns the index of the constraint in the MPSolver::constraints_.
double lb() const
Returns the lower bound.
void set_dual_value(double dual_value)
const std::string & name() const
Returns the name of the constraint.
void SetLB(double lb)
Sets the lower bound.
MPSolver::BasisStatus basis_status() const
Advanced usage: returns the basis status of the constraint.
double dual_value() const
Advanced usage: returns the dual value of the constraint in the current solution (only available for ...
A class to express a linear objective.
void SetMaximization()
Sets the optimization direction to maximize.
void SetCoefficient(const MPVariable *const var, double coeff)
Sets the coefficient of the variable in the objective.
double GetCoefficient(const MPVariable *const var) const
Gets the coefficient of a given variable in the objective.
void SetOffset(double value)
Sets the constant term in the objective.
bool maximization() const
Is the optimization direction set to maximize?
void OptimizeLinearExpr(const LinearExpr &linear_expr, bool is_maximization)
Resets the current objective to take the value of linear_expr, and sets the objective direction to ma...
void AddLinearExpr(const LinearExpr &linear_expr)
Adds linear_expr to the current objective, does not change the direction.
const absl::flat_hash_map< const MPVariable *, double > & terms() const
Returns a map from variables to their coefficients in the objective.
void MinimizeLinearExpr(const LinearExpr &linear_expr)
Resets the current objective to minimize linear_expr.
double Value() const
Returns the objective value of the best solution found so far.
double offset() const
Gets the constant term in the objective.
double BestBound() const
Returns the best objective bound.
bool minimization() const
Is the optimization direction set to minimize?
void Clear()
Clears the offset, all variables and coefficients, and the optimization direction.
void SetMinimization()
Sets the optimization direction to minimize.
void MaximizeLinearExpr(const LinearExpr &linear_expr)
Resets the current objective to maximize linear_expr.
void SetOptimizationDirection(bool maximize)
Sets the optimization direction (maximize: true or minimize: false).
This mathematical programming (MP) solver class is the main class though which users build and solve ...
void FillSolutionResponseProto(MPSolutionResponse *response) const
Encodes the current solution in a solution response protocol buffer.
int NumConstraints() const
Returns the number of constraints.
static OptimizationProblemType ParseSolverTypeOrDie(const std::string &solver_id)
Parses the name of the solver and returns the correct optimization type or dies.
const std::string & Name() const
Returns the name of the model set at construction.
void MakeBoolVarArray(int nb, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of boolean variables.
MPObjective * MutableObjective()
Returns the mutable objective object.
bool VerifySolution(double tolerance, bool log_errors) const
Advanced usage: Verifies the correctness of the solution.
const std::vector< MPVariable * > & variables() const
Returns the array of variables handled by the MPSolver.
absl::Duration DurationSinceConstruction() const
void Reset()
Advanced usage: resets extracted model to solve from scratch.
MPVariable * LookupVariableOrNull(const std::string &var_name) const
Looks up a variable by name, and returns nullptr if it does not exist.
void SetStartingLpBasis(const std::vector< MPSolver::BasisStatus > &variable_statuses, const std::vector< MPSolver::BasisStatus > &constraint_statuses)
Advanced usage: Incrementality.
static bool SupportsProblemType(OptimizationProblemType problem_type)
Whether the given problem type is supported (this will depend on the targets that you linked).
static MPSolver * CreateSolver(const std::string &solver_id)
Recommended factory method to create a MPSolver instance, especially in non C++ languages.
MPVariable * MakeBoolVar(const std::string &name)
Creates a boolean variable.
void SetHint(std::vector< std::pair< const MPVariable *, double > > hint)
Sets a hint for solution.
double ComputeExactConditionNumber() const
Advanced usage: computes the exact condition number of the current scaled basis: L1norm(B) * L1norm(i...
const MPObjective & Objective() const
Returns the objective object.
int64 nodes() const
Returns the number of branch-and-bound nodes evaluated during the solve.
ResultStatus
The status of solving the problem.
@ FEASIBLE
feasible, or stopped by limit.
@ NOT_SOLVED
not been solved yet.
@ INFEASIBLE
proven infeasible.
@ UNBOUNDED
proven unbounded.
@ ABNORMAL
abnormal, i.e., error of some kind.
@ MODEL_INVALID
the model is trivially invalid (NaN coefficients, etc).
static void SolveWithProto(const MPModelRequest &model_request, MPSolutionResponse *response)
Solves the model encoded by a MPModelRequest protocol buffer and fills the solution encoded as a MPSo...
const std::vector< MPConstraint * > & constraints() const
Returns the array of constraints handled by the MPSolver.
void MakeNumVarArray(int nb, double lb, double ub, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of continuous variables.
void MakeVarArray(int nb, double lb, double ub, bool integer, const std::string &name_prefix, std::vector< MPVariable * > *vars)
Creates an array of variables.
void * underlying_solver()
Advanced usage: returns the underlying solver.
OptimizationProblemType
The type of problems (LP or MIP) that will be solved and the underlying solver (GLOP,...
bool SetSolverSpecificParametersAsString(const std::string &parameters)
Advanced usage: pass solver specific parameters in text format.
static void SetGurobiLibraryPath(const std::string &full_library_path)
absl::Status SetNumThreads(int num_threads)
Sets the number of threads to use by the underlying solver.
std::string SolverVersion() const
Returns a string describing the underlying solver and its version.
void ExportModelToProto(MPModelProto *output_model) const
Exports model to protocol buffer.
int GetNumThreads() const
Returns the number of threads to be used during solve.
int64 iterations() const
Returns the number of simplex iterations.
void MakeIntVarArray(int nb, double lb, double ub, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of integer variables.
std::vector< double > ComputeConstraintActivities() const
Advanced usage: compute the "activities" of all constraints, which are the sums of their linear terms...
static double infinity()
Infinity.
std::string GetSolverSpecificParametersAsString() const
static bool ParseSolverType(absl::string_view solver_id, OptimizationProblemType *type)
Parses the name of the solver.
int NumVariables() const
Returns the number of variables.
absl::Status ClampSolutionWithinBounds()
Resets values of out of bound variables to the corresponding bound and returns an error if any of the...
bool OwnsVariable(const MPVariable *var) const
void Clear()
Clears the objective (including the optimization direction), all variables and constraints.
bool ExportModelAsLpFormat(bool obfuscate, std::string *model_str) const
Shortcuts to the homonymous MPModelProtoExporter methods, via exporting to a MPModelProto with Export...
void Write(const std::string &file_name)
Writes the model using the solver internal write function.
MPConstraint * MakeRowConstraint()
Creates a constraint with -infinity and +infinity bounds.
void SetCallback(MPCallback *mp_callback)
MPSolverResponseStatus LoadModelFromProto(const MPModelProto &input_model, std::string *error_message)
Loads model from protocol buffer.
bool OutputIsEnabled() const
Controls (or queries) the amount of output produced by the underlying solver.
bool ExportModelAsMpsFormat(bool fixed_format, bool obfuscate, std::string *model_str) const
ABSL_MUST_USE_RESULT bool NextSolution()
Some solvers (MIP only, not LP) can produce multiple solutions to the problem.
MPVariable * MakeVar(double lb, double ub, bool integer, const std::string &name)
Creates a variable with the given bounds, integrality requirement and name.
MPConstraint * LookupConstraintOrNull(const std::string &constraint_name) const
Looks up a constraint by name, and returns nullptr if it does not exist.
MPVariable * MakeNumVar(double lb, double ub, const std::string &name)
Creates a continuous variable.
bool InterruptSolve()
Interrupts the Solve() execution to terminate processing if possible.
void set_time_limit(int64 time_limit_milliseconds)
MPVariable * MakeIntVar(double lb, double ub, const std::string &name)
Creates an integer variable.
MPSolver(const std::string &name, OptimizationProblemType problem_type)
Create a solver with the given name and underlying solver backend.
ResultStatus Solve()
Solves the problem using the default parameter values.
void EnableOutput()
Enables solver logging.
void SuppressOutput()
Suppresses solver logging.
absl::Status LoadSolutionFromProto(const MPSolutionResponse &response, double tolerance=kDefaultPrimalTolerance)
Load a solution encoded in a protocol buffer onto this solver for easy access via the MPSolver interf...
MPSolverResponseStatus LoadModelFromProtoWithUniqueNamesOrDie(const MPModelProto &input_model, std::string *error_message)
Loads model from protocol buffer.
virtual OptimizationProblemType ProblemType() const
Returns the optimization problem type set at construction.
absl::Duration TimeLimit() const
BasisStatus
Advanced usage: possible basis status values for a variable and the slack variable of a linear constr...
void SetTimeLimit(absl::Duration time_limit)
virtual void SetLpAlgorithm(int value)=0
static constexpr int64 kUnknownNumberOfNodes
virtual void SetIntegerParamToUnsupportedValue(MPSolverParameters::IntegerParam param, int value)
static constexpr int64 kUnknownNumberOfIterations
void SetUnsupportedDoubleParam(MPSolverParameters::DoubleParam param)
void set_constraint_as_extracted(int ct_index, bool extracted)
virtual bool AddIndicatorConstraint(MPConstraint *const ct)
virtual void AddVariable(MPVariable *const var)=0
void SetMIPParameters(const MPSolverParameters &param)
virtual bool IsContinuous() const =0
virtual double ComputeExactConditionNumber() const
virtual void Write(const std::string &filename)
MPSolverInterface(MPSolver *const solver)
bool constraint_is_extracted(int ct_index) const
virtual void SetVariableBounds(int index, double lb, double ub)=0
virtual void SetPrimalTolerance(double value)=0
virtual void BranchingPriorityChangedForVariable(int var_index)
virtual void SetParameters(const MPSolverParameters &param)=0
virtual void SetRelativeMipGap(double value)=0
virtual void SetOptimizationDirection(bool maximize)=0
virtual bool SetSolverSpecificParametersAsString(const std::string &parameters)
virtual MPSolver::BasisStatus column_status(int variable_index) const =0
virtual void SetScalingMode(int value)=0
virtual MPSolver::BasisStatus row_status(int constraint_index) const =0
virtual std::string SolverVersion() const =0
virtual absl::Status SetNumThreads(int num_threads)
virtual void ClearConstraint(MPConstraint *const constraint)=0
virtual int64 nodes() const =0
virtual void SetObjectiveOffset(double value)=0
virtual absl::optional< MPSolutionResponse > DirectlySolveProto(const MPModelRequest &request)
virtual void SetStartingLpBasis(const std::vector< MPSolver::BasisStatus > &variable_statuses, const std::vector< MPSolver::BasisStatus > &constraint_statuses)
virtual int64 iterations() const =0
virtual void SetVariableInteger(int index, bool integer)=0
virtual void SetCallback(MPCallback *mp_callback)
bool variable_is_extracted(int var_index) const
virtual void SetDualTolerance(double value)=0
virtual void SetPresolveMode(int value)=0
virtual MPSolver::ResultStatus Solve(const MPSolverParameters &param)=0
MPSolver::ResultStatus result_status() const
virtual void SetUnsupportedIntegerParam(MPSolverParameters::IntegerParam param)
virtual void SetCoefficient(MPConstraint *const constraint, const MPVariable *const variable, double new_value, double old_value)=0
virtual void SetObjectiveCoefficient(const MPVariable *const variable, double coefficient)=0
void SetDoubleParamToUnsupportedValue(MPSolverParameters::DoubleParam param, double value)
void set_variable_as_extracted(int var_index, bool extracted)
virtual void SetConstraintBounds(int index, double lb, double ub)=0
void SetCommonParameters(const MPSolverParameters &param)
virtual void AddRowConstraint(MPConstraint *const ct)=0
This class stores parameter settings for LP and MIP solvers.
void ResetIntegerParam(MPSolverParameters::IntegerParam param)
Sets an integer parameter to its default value (default value defined in MPSolverParameters if it exi...
void SetDoubleParam(MPSolverParameters::DoubleParam param, double value)
Sets a double parameter to a specific value.
IncrementalityValues
Advanced usage: Incrementality options.
@ INCREMENTALITY_OFF
Start solve from scratch.
@ INCREMENTALITY_ON
Reuse results from previous solve as much as the underlying solver allows.
ScalingValues
Advanced usage: Scaling options.
static const IncrementalityValues kDefaultIncrementality
void Reset()
Sets all parameters to their default value.
DoubleParam
Enumeration of parameters that take continuous values.
@ DUAL_TOLERANCE
Advanced usage: tolerance for dual feasibility of basic solutions.
@ PRIMAL_TOLERANCE
Advanced usage: tolerance for primal feasibility of basic solutions.
@ RELATIVE_MIP_GAP
Limit for relative MIP gap.
static const PresolveValues kDefaultPresolve
double GetDoubleParam(MPSolverParameters::DoubleParam param) const
Returns the value of a double parameter.
IntegerParam
Enumeration of parameters that take integer or categorical values.
@ LP_ALGORITHM
Algorithm to solve linear programs.
@ SCALING
Advanced usage: enable or disable matrix scaling.
@ PRESOLVE
Advanced usage: presolve mode.
@ INCREMENTALITY
Advanced usage: incrementality from one solve to the next.
PresolveValues
For each categorical parameter, enumeration of possible values.
void SetIntegerParam(MPSolverParameters::IntegerParam param, int value)
Sets a integer parameter to a specific value.
int GetIntegerParam(MPSolverParameters::IntegerParam param) const
Returns the value of an integer parameter.
MPSolverParameters()
The constructor sets all parameters to their default value.
void ResetDoubleParam(MPSolverParameters::DoubleParam param)
Sets a double parameter to its default value (default value defined in MPSolverParameters if it exist...
The class for variables of a Mathematical Programming (MP) model.
void SetBounds(double lb, double ub)
Sets both the lower and upper bounds.
double unrounded_solution_value() const
Advanced usage: unrounded solution value.
int branching_priority() const
Advanced usage: Certain MIP solvers (e.g.
void set_solution_value(double value)
void SetBranchingPriority(int priority)
void SetUB(double ub)
Sets the upper bound.
double ub() const
Returns the upper bound.
double reduced_cost() const
Advanced usage: returns the reduced cost of the variable in the current solution (only available for ...
void SetInteger(bool integer)
Sets the integrality requirement of the variable.
MPVariable(int index, double lb, double ub, bool integer, const std::string &name, MPSolverInterface *const interface_in)
void set_reduced_cost(double reduced_cost)
bool integer() const
Returns the integrality requirement of the variable.
int index() const
Returns the index of the variable in the MPSolver::variables_.
double lb() const
Returns the lower bound.
const std::string & name() const
Returns the name of the variable.
void SetLB(double lb)
Sets the lower bound.
double solution_value() const
Returns the value of the variable in the current solution.
MPSolver::BasisStatus basis_status() const
Advanced usage: returns the basis status of the variable in the current solution (only available for ...
SatParameters parameters
SharedResponseManager * response
const std::string name
const Constraint * ct
int64 value
IntVar * var
Definition: expr_array.cc:1858
int64_t int64
const int64 offset_
Definition: interval.cc:2076
This file allows you to write natural code (like a mathematical equation) to model optimization probl...
MPSolver::OptimizationProblemType problem_type
ABSL_DECLARE_FLAG(bool, linear_solver_enable_verbose_output)
const int ERROR
Definition: log_severity.h:32
const int FATAL
Definition: log_severity.h:32
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:29
Definition: cleanup.h:22
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
constexpr double kDefaultPrimalTolerance
const absl::string_view ToString(MPSolver::OptimizationProblemType optimization_problem_type)
bool SolverTypeIsMip(MPModelRequest::SolverType solver_type)
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
bool AbslParseFlag(const absl::string_view text, MPSolver::OptimizationProblemType *solver_type, std::string *error)
std::string AbslUnparseFlag(MPSolver::OptimizationProblemType solver_type)
bool MPSolverResponseStatusIsRpcError(MPSolverResponseStatus status)
int index
Definition: pack.cc:508
int64 coefficient
IntVar *const objective_
Definition: search.cc:2951