OR-Tools  8.1
sat_interface.cc
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 
14 #include <atomic>
15 #include <string>
16 #include <vector>
17 
18 #include "absl/status/status.h"
19 #include "absl/status/statusor.h"
20 #include "ortools/base/hash.h"
22 #include "ortools/base/logging.h"
29 #include "ortools/sat/lp_utils.h"
30 #include "ortools/sat/model.h"
32 
33 namespace operations_research {
34 
35 #if defined(PROTOBUF_INTERNAL_IMPL)
36 using google::protobuf::Message;
37 #else
38 using google::protobuf::Message;
39 #endif
40 
42  public:
43  explicit SatInterface(MPSolver* const solver);
44  ~SatInterface() override;
45 
46  // ----- Solve -----
47  MPSolver::ResultStatus Solve(const MPSolverParameters& param) override;
48  bool InterruptSolve() override;
49 
50  // ----- Model modifications and extraction -----
51  void Reset() override;
52  void SetOptimizationDirection(bool maximize) override;
53  void SetVariableBounds(int index, double lb, double ub) override;
54  void SetVariableInteger(int index, bool integer) override;
55  void SetConstraintBounds(int index, double lb, double ub) override;
56  void AddRowConstraint(MPConstraint* const ct) override;
57  void AddVariable(MPVariable* const var) override;
58  void SetCoefficient(MPConstraint* const constraint,
59  const MPVariable* const variable, double new_value,
60  double old_value) override;
61  void ClearConstraint(MPConstraint* const constraint) override;
62  void SetObjectiveCoefficient(const MPVariable* const variable,
63  double coefficient) override;
64  void SetObjectiveOffset(double value) override;
65  void ClearObjective() override;
66 
67  bool AddIndicatorConstraint(MPConstraint* const ct) override { return true; }
68 
69  // ------ Query statistics on the solution and the solve ------
70  int64 iterations() const override;
71  int64 nodes() const override;
72  MPSolver::BasisStatus row_status(int constraint_index) const override;
73  MPSolver::BasisStatus column_status(int variable_index) const override;
74 
75  // ----- Misc -----
76  bool IsContinuous() const override;
77  bool IsLP() const override;
78  bool IsMIP() const override;
79 
80  std::string SolverVersion() const override;
81  void* underlying_solver() override;
82 
83  void ExtractNewVariables() override;
84  void ExtractNewConstraints() override;
85  void ExtractObjective() override;
86 
87  void SetParameters(const MPSolverParameters& param) override;
88  void SetRelativeMipGap(double value) override;
89  void SetPrimalTolerance(double value) override;
90  void SetDualTolerance(double value) override;
91  void SetPresolveMode(int value) override;
92  void SetScalingMode(int value) override;
93  void SetLpAlgorithm(int value) override;
95  const std::string& parameters) override;
96  absl::Status SetNumThreads(int num_threads) override;
97 
98  private:
99  void NonIncrementalChange();
100 
101  std::atomic<bool> interrupt_solve_;
102  sat::SatParameters parameters_;
103  int num_threads_ = 8;
104 };
105 
107  : MPSolverInterface(solver), interrupt_solve_(false) {}
108 
110 
112  interrupt_solve_ = false;
113 
114  // Reset extraction as this interface is not incremental yet.
115  Reset();
116  ExtractModel();
117 
118  SetParameters(param);
120  solver_->solver_specific_parameter_string_);
121 
122  // Time limit.
123  if (solver_->time_limit()) {
124  VLOG(1) << "Setting time limit = " << solver_->time_limit() << " ms.";
125  parameters_.set_max_time_in_seconds(
126  static_cast<double>(solver_->time_limit()) / 1000.0);
127  }
128 
129  // Mark variables and constraints as extracted.
130  for (int i = 0; i < solver_->variables_.size(); ++i) {
131  set_variable_as_extracted(i, true);
132  }
133  for (int i = 0; i < solver_->constraints_.size(); ++i) {
135  }
136 
137  MPModelRequest request;
138  solver_->ExportModelToProto(request.mutable_model());
139  // If sat::SatParameters is compiled with proto-lite (go/mobile-cpp-protos),
140  // then serialize as non-human readable string. This is because proto-lite
141  // does not support reflection mechanism, which is a prerequisite for method
142  // like `ShortDebugString`.
144  request.set_solver_specific_parameters(parameters_.SerializeAsString());
145  } else {
146  request.set_solver_specific_parameters(parameters_.ShortDebugString());
147  }
148  request.set_enable_internal_solver_output(!quiet_);
149  const absl::StatusOr<MPSolutionResponse> status_or =
150  SatSolveProto(std::move(request), &interrupt_solve_);
151 
152  if (!status_or.ok()) return MPSolver::ABNORMAL;
153  const MPSolutionResponse& response = status_or.value();
154 
155  // The solution must be marked as synchronized even when no solution exists.
157  switch (response.status()) {
158  case MPSOLVER_OPTIMAL:
160  break;
161  case MPSOLVER_FEASIBLE:
163  break;
164  case MPSOLVER_INFEASIBLE:
166  break;
169  break;
170  default:
172  break;
173  }
174 
175  // TODO(user): Just use LoadSolutionFromProto(), but fix that function first
176  // to load everything and not just the solution.
177  if (response.status() == MPSOLVER_FEASIBLE ||
178  response.status() == MPSOLVER_OPTIMAL) {
179  objective_value_ = response.objective_value();
180  best_objective_bound_ = response.best_objective_bound();
181  const size_t num_vars = solver_->variables_.size();
182  for (int var_id = 0; var_id < num_vars; ++var_id) {
183  MPVariable* const var = solver_->variables_[var_id];
184  var->set_solution_value(response.variable_value(var_id));
185  }
186  }
187 
188  return result_status_;
189 }
190 
192  interrupt_solve_ = true;
193  return true;
194 }
195 
197 
199  NonIncrementalChange();
200 }
201 
202 void SatInterface::SetVariableBounds(int index, double lb, double ub) {
203  NonIncrementalChange();
204 }
205 
206 void SatInterface::SetVariableInteger(int index, bool integer) {
207  NonIncrementalChange();
208 }
209 
210 void SatInterface::SetConstraintBounds(int index, double lb, double ub) {
211  NonIncrementalChange();
212 }
213 
215  NonIncrementalChange();
216 }
217 
219  NonIncrementalChange();
220 }
221 
223  const MPVariable* const variable,
224  double new_value, double old_value) {
225  NonIncrementalChange();
226 }
227 
229  NonIncrementalChange();
230 }
231 
233  double coefficient) {
234  NonIncrementalChange();
235 }
236 
237 void SatInterface::SetObjectiveOffset(double value) { NonIncrementalChange(); }
238 
239 void SatInterface::ClearObjective() { NonIncrementalChange(); }
240 
242  return 0; // FIXME
243 }
244 
245 int64 SatInterface::nodes() const { return 0; }
246 
247 MPSolver::BasisStatus SatInterface::row_status(int constraint_index) const {
248  return MPSolver::BasisStatus::FREE; // FIXME
249 }
250 
252  return MPSolver::BasisStatus::FREE; // FIXME
253 }
254 
255 bool SatInterface::IsContinuous() const { return false; }
256 bool SatInterface::IsLP() const { return false; }
257 bool SatInterface::IsMIP() const { return true; }
258 
259 std::string SatInterface::SolverVersion() const {
260  return "SAT Based MIP Solver";
261 }
262 
263 void* SatInterface::underlying_solver() { return nullptr; }
264 
265 void SatInterface::ExtractNewVariables() { NonIncrementalChange(); }
266 
267 void SatInterface::ExtractNewConstraints() { NonIncrementalChange(); }
268 
269 void SatInterface::ExtractObjective() { NonIncrementalChange(); }
270 
272  // By default, we use 8 threads as it allows to try a good set of orthogonal
273  // parameters. This can be overridden by the user.
274  parameters_.set_num_search_workers(num_threads_);
275  parameters_.set_log_search_progress(!quiet_);
276  SetCommonParameters(param);
277 }
278 
279 absl::Status SatInterface::SetNumThreads(int num_threads) {
280  num_threads_ = num_threads;
281  return absl::OkStatus();
282 }
283 
284 // All these have no effect.
290 
291 // TODO(user): Implement me.
293 
295  const std::string& parameters) {
296  return ProtobufTextFormatMergeFromString(parameters, &parameters_);
297 }
298 
299 void SatInterface::NonIncrementalChange() {
300  // The current implementation is not incremental.
302 }
303 
304 // Register Sat in the global linear solver factory.
306  return new SatInterface(solver);
307 }
308 
309 } // namespace operations_research
var
IntVar * var
Definition: expr_array.cc:1858
response
SharedResponseManager * response
Definition: cp_model_solver.cc:2085
integral_types.h
VLOG
#define VLOG(verboselevel)
Definition: base/logging.h:978
cp_model.pb.h
operations_research::SatInterface::SetNumThreads
absl::Status SetNumThreads(int num_threads) override
Definition: sat_interface.cc:279
time_limit.h
operations_research::SatInterface::IsMIP
bool IsMIP() const override
Definition: sat_interface.cc:257
operations_research::SatInterface::AddIndicatorConstraint
bool AddIndicatorConstraint(MPConstraint *const ct) override
Definition: sat_interface.cc:67
operations_research::SatInterface::AddVariable
void AddVariable(MPVariable *const var) override
Definition: sat_interface.cc:218
operations_research::SatInterface::SetPresolveMode
void SetPresolveMode(int value) override
Definition: sat_interface.cc:292
linear_solver.pb.h
operations_research::MPSolver::OPTIMAL
@ OPTIMAL
optimal.
Definition: linear_solver.h:427
proto_utils.h
logging.h
operations_research::MPSolverInterface::quiet_
bool quiet_
Definition: linear_solver.h:1733
operations_research::MPSolver
This mathematical programming (MP) solver class is the main class though which users build and solve ...
Definition: linear_solver.h:177
operations_research::SatInterface::SetSolverSpecificParametersAsString
bool SetSolverSpecificParametersAsString(const std::string &parameters) override
Definition: sat_interface.cc:294
operations_research::SatInterface::SetVariableBounds
void SetVariableBounds(int index, double lb, double ub) override
Definition: sat_interface.cc:202
value
int64 value
Definition: demon_profiler.cc:43
operations_research::SatInterface::nodes
int64 nodes() const override
Definition: sat_interface.cc:245
model.h
operations_research::SatInterface::SetOptimizationDirection
void SetOptimizationDirection(bool maximize) override
Definition: sat_interface.cc:198
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::MPSolver::MODEL_INVALID
@ MODEL_INVALID
the model is trivially invalid (NaN coefficients, etc).
Definition: linear_solver.h:437
operations_research::MPSolverInterface
Definition: linear_solver.h:1514
operations_research::SatInterface::ClearConstraint
void ClearConstraint(MPConstraint *const constraint) override
Definition: sat_interface.cc:228
operations_research::SatInterface::SetCoefficient
void SetCoefficient(MPConstraint *const constraint, const MPVariable *const variable, double new_value, double old_value) override
Definition: sat_interface.cc:222
operations_research::MPSolver::ABNORMAL
@ ABNORMAL
abnormal, i.e., error of some kind.
Definition: linear_solver.h:435
int64
int64_t int64
Definition: integral_types.h:34
sat_proto_solver.h
index
int index
Definition: pack.cc:508
operations_research::MPSolverParameters
This class stores parameter settings for LP and MIP solvers.
Definition: linear_solver.h:1358
operations_research::MPConstraint
The class for constraints of a Mathematical Programming (MP) model.
Definition: linear_solver.h:1175
operations_research::SatInterface::InterruptSolve
bool InterruptSolve() override
Definition: sat_interface.cc:191
operations_research::SatInterface::SolverVersion
std::string SolverVersion() const override
Definition: sat_interface.cc:259
operations_research::BuildSatInterface
MPSolverInterface * BuildSatInterface(MPSolver *const solver)
Definition: sat_interface.cc:305
operations_research::MPSolverInterface::objective_value_
double objective_value_
Definition: linear_solver.h:1727
operations_research::MPSOLVER_FEASIBLE
@ MPSOLVER_FEASIBLE
Definition: linear_solver.pb.h:230
operations_research::SatInterface::SetScalingMode
void SetScalingMode(int value) override
Definition: sat_interface.cc:287
operations_research::SatInterface::SetRelativeMipGap
void SetRelativeMipGap(double value) override
Definition: sat_interface.cc:289
operations_research::MPSolverInterface::ResetExtractionInformation
void ResetExtractionInformation()
Definition: linear_solver.cc:1665
operations_research::MPSolverInterface::solver_
MPSolver *const solver_
Definition: linear_solver.h:1712
operations_research::SatInterface::IsContinuous
bool IsContinuous() const override
Definition: sat_interface.cc:255
operations_research::MPSolver::NOT_SOLVED
@ NOT_SOLVED
not been solved yet.
Definition: linear_solver.h:439
operations_research::MPSolver::SetSolverSpecificParametersAsString
bool SetSolverSpecificParametersAsString(const std::string &parameters)
Advanced usage: pass solver specific parameters in text format.
Definition: linear_solver.cc:346
operations_research::MPSolverInterface::SOLUTION_SYNCHRONIZED
@ SOLUTION_SYNCHRONIZED
Definition: linear_solver.h:1526
operations_research::SatInterface::SetPrimalTolerance
void SetPrimalTolerance(double value) override
Definition: sat_interface.cc:285
operations_research::SatInterface::SetConstraintBounds
void SetConstraintBounds(int index, double lb, double ub) override
Definition: sat_interface.cc:210
operations_research::MPSolver::time_limit
int64 time_limit() const
Definition: linear_solver.h:777
operations_research::SatInterface::SetDualTolerance
void SetDualTolerance(double value) override
Definition: sat_interface.cc:286
operations_research::MPSolver::BasisStatus
BasisStatus
Advanced usage: possible basis status values for a variable and the slack variable of a linear constr...
Definition: linear_solver.h:640
operations_research::SatInterface::AddRowConstraint
void AddRowConstraint(MPConstraint *const ct) override
Definition: sat_interface.cc:214
operations_research::SatInterface::Solve
MPSolver::ResultStatus Solve(const MPSolverParameters &param) override
Definition: sat_interface.cc:111
ct
const Constraint * ct
Definition: demon_profiler.cc:42
operations_research::SatInterface::ExtractNewConstraints
void ExtractNewConstraints() override
Definition: sat_interface.cc:267
operations_research::MPSolverInterface::sync_status_
SynchronizationStatus sync_status_
Definition: linear_solver.h:1714
operations_research::SatInterface
Definition: sat_interface.cc:41
operations_research::SatInterface::SetObjectiveOffset
void SetObjectiveOffset(double value) override
Definition: sat_interface.cc:237
operations_research::MPSolver::ResultStatus
ResultStatus
The status of solving the problem.
Definition: linear_solver.h:425
operations_research::SatInterface::Reset
void Reset() override
Definition: sat_interface.cc:196
operations_research::SatInterface::~SatInterface
~SatInterface() override
Definition: sat_interface.cc:109
operations_research::SatInterface::IsLP
bool IsLP() const override
Definition: sat_interface.cc:256
operations_research::MPSolverInterface::ExtractModel
void ExtractModel()
Definition: linear_solver.cc:1637
operations_research::SatInterface::ExtractNewVariables
void ExtractNewVariables() override
Definition: sat_interface.cc:265
operations_research::MPSOLVER_MODEL_INVALID
@ MPSOLVER_MODEL_INVALID
Definition: linear_solver.pb.h:237
operations_research::MPSolverInterface::set_variable_as_extracted
void set_variable_as_extracted(int var_index, bool extracted)
Definition: linear_solver.h:1655
coefficient
int64 coefficient
Definition: routing_search.cc:970
operations_research::SatInterface::iterations
int64 iterations() const override
Definition: sat_interface.cc:241
operations_research::SatInterface::underlying_solver
void * underlying_solver() override
Definition: sat_interface.cc:263
operations_research::SatInterface::ClearObjective
void ClearObjective() override
Definition: sat_interface.cc:239
operations_research::MPSOLVER_INFEASIBLE
@ MPSOLVER_INFEASIBLE
Definition: linear_solver.pb.h:231
operations_research::MPVariable
The class for variables of a Mathematical Programming (MP) model.
Definition: linear_solver.h:1050
hash.h
operations_research::MPSolver::ExportModelToProto
void ExportModelToProto(MPModelProto *output_model) const
Exports model to protocol buffer.
Definition: linear_solver.cc:902
linear_solver.h
A C++ wrapper that provides a simple and unified interface to several linear programming and mixed in...
lp_utils.h
operations_research::MPSOLVER_OPTIMAL
@ MPSOLVER_OPTIMAL
Definition: linear_solver.pb.h:229
operations_research::MPSolverInterface::SetCommonParameters
void SetCommonParameters(const MPSolverParameters &param)
Definition: linear_solver.cc:1733
operations_research::SatInterface::SetVariableInteger
void SetVariableInteger(int index, bool integer) override
Definition: sat_interface.cc:206
operations_research::MPSolver::INFEASIBLE
@ INFEASIBLE
proven infeasible.
Definition: linear_solver.h:431
cp_model_solver.h
operations_research::ProtobufTextFormatMergeFromString
bool ProtobufTextFormatMergeFromString(const std::string &proto_text_string, ProtoType *proto)
Definition: port/proto_utils.h:75
operations_research::SatInterface::SetLpAlgorithm
void SetLpAlgorithm(int value) override
Definition: sat_interface.cc:288
operations_research::MPSolverInterface::MUST_RELOAD
@ MUST_RELOAD
Definition: linear_solver.h:1519
operations_research::MPSolverInterface::result_status_
MPSolver::ResultStatus result_status_
Definition: linear_solver.h:1717
operations_research::MPSolverInterface::set_constraint_as_extracted
void set_constraint_as_extracted(int ct_index, bool extracted)
Definition: linear_solver.h:1661
operations_research::SatInterface::SetObjectiveCoefficient
void SetObjectiveCoefficient(const MPVariable *const variable, double coefficient) override
Definition: sat_interface.cc:232
parameters
SatParameters parameters
Definition: cp_model_fz_solver.cc:107
operations_research::SatInterface::SetParameters
void SetParameters(const MPSolverParameters &param) override
Definition: sat_interface.cc:271
operations_research::SatInterface::row_status
MPSolver::BasisStatus row_status(int constraint_index) const override
Definition: sat_interface.cc:247
operations_research::SatInterface::SatInterface
SatInterface(MPSolver *const solver)
Definition: sat_interface.cc:106
operations_research::MPSolver::FEASIBLE
@ FEASIBLE
feasible, or stopped by limit.
Definition: linear_solver.h:429
operations_research::SatInterface::column_status
MPSolver::BasisStatus column_status(int variable_index) const override
Definition: sat_interface.cc:251
operations_research::SatInterface::ExtractObjective
void ExtractObjective() override
Definition: sat_interface.cc:269
operations_research::MPSolverInterface::best_objective_bound_
double best_objective_bound_
Definition: linear_solver.h:1730
operations_research::SatSolveProto
absl::StatusOr< MPSolutionResponse > SatSolveProto(MPModelRequest request, std::atomic< bool > *interrupt_solve)
Definition: sat_proto_solver.cc:59