Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
sat_interface.cc
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#include <atomic>
15#include <cstdint>
16#include <string>
17#include <utility>
18#include <vector>
19
20#include "absl/base/attributes.h"
21#include "absl/status/status.h"
31
32namespace operations_research {
33
35 public:
36 explicit SatInterface(MPSolver* solver);
37 ~SatInterface() override;
38
39 // ----- Solve -----
40 MPSolver::ResultStatus Solve(const MPSolverParameters& param) override;
41 bool InterruptSolve() override;
42
43 // ----- Directly solve proto is supported ---
44 bool SupportsDirectlySolveProto(std::atomic<bool>* interrupt) const override {
45 return true;
46 }
48 std::atomic<bool>* interrupt) override {
49 return SatSolveProto(std::move(request), interrupt);
50 }
51
52 // ----- Model modifications and extraction -----
53 void Reset() override;
54 void SetOptimizationDirection(bool maximize) override;
55 void SetVariableBounds(int index, double lb, double ub) override;
56 void SetVariableInteger(int index, bool integer) override;
57 void SetConstraintBounds(int index, double lb, double ub) override;
58 void AddRowConstraint(MPConstraint* ct) override;
59 void AddVariable(MPVariable* var) override;
60 void SetCoefficient(MPConstraint* constraint, const MPVariable* variable,
61 double new_value, double old_value) override;
62 void ClearConstraint(MPConstraint* constraint) override;
63 void SetObjectiveCoefficient(const MPVariable* variable,
64 double coefficient) override;
65 void SetObjectiveOffset(double value) override;
66 void ClearObjective() override;
67
68 bool AddIndicatorConstraint(MPConstraint* const ct) override { return true; }
69
70 // ------ Query statistics on the solution and the solve ------
71 int64_t iterations() const override;
72 int64_t nodes() const override;
73 MPSolver::BasisStatus row_status(int constraint_index) const override;
74 MPSolver::BasisStatus column_status(int variable_index) const override;
75
76 // ----- Misc -----
77 bool IsContinuous() const override;
78 bool IsLP() const override;
79 bool IsMIP() const override;
80
81 std::string SolverVersion() const override;
82 void* underlying_solver() override;
83
84 void ExtractNewVariables() override;
85 void ExtractNewConstraints() override;
86 void ExtractObjective() override;
87
88 void SetParameters(const MPSolverParameters& param) override;
89 void SetRelativeMipGap(double value) override;
90 void SetPrimalTolerance(double value) override;
91 void SetDualTolerance(double value) override;
92 void SetPresolveMode(int value) override;
93 void SetScalingMode(int value) override;
94 void SetLpAlgorithm(int value) override;
96 const std::string& parameters) override;
97 absl::Status SetNumThreads(int num_threads) override;
98
99 private:
100 void NonIncrementalChange();
101
102 std::atomic<bool> interrupt_solve_;
103 sat::SatParameters parameters_;
104 int num_threads_ = 0;
105};
106
108 : MPSolverInterface(solver), interrupt_solve_(false) {}
109
111
113 interrupt_solve_ = false;
114
115 // Reset extraction as this interface is not incremental yet.
116 Reset();
117 ExtractModel();
118
119 SetParameters(param);
120 solver_->SetSolverSpecificParametersAsString(
121 solver_->solver_specific_parameter_string_);
122
123 // Time limit.
124 if (solver_->time_limit()) {
125 VLOG(1) << "Setting time limit = " << solver_->time_limit() << " ms.";
126 parameters_.set_max_time_in_seconds(
127 static_cast<double>(solver_->time_limit()) / 1000.0);
128 }
129
130 // Mark variables and constraints as extracted.
131 for (int i = 0; i < solver_->variables_.size(); ++i) {
133 }
134 for (int i = 0; i < solver_->constraints_.size(); ++i) {
136 }
137
138 MPModelRequest request;
139 solver_->ExportModelToProto(request.mutable_model());
142
143 const MPSolutionResponse response =
144 SatSolveProto(std::move(request), &interrupt_solve_);
145
146 // The solution must be marked as synchronized even when no solution exists.
148 result_status_ = static_cast<MPSolver::ResultStatus>(response.status());
149
150 if (response.status() == MPSOLVER_FEASIBLE ||
151 response.status() == MPSOLVER_OPTIMAL) {
152 const absl::Status result = solver_->LoadSolutionFromProto(response);
153 if (!result.ok()) {
154 LOG(ERROR) << "LoadSolutionFromProto failed: " << result;
155 }
156 }
157
158 return result_status_;
159}
160
162 interrupt_solve_ = true;
163 return true;
164}
165
167
169 NonIncrementalChange();
170}
171
172void SatInterface::SetVariableBounds(int index, double lb, double ub) {
173 NonIncrementalChange();
174}
175
176void SatInterface::SetVariableInteger(int index, bool integer) {
177 NonIncrementalChange();
178}
179
180void SatInterface::SetConstraintBounds(int index, double lb, double ub) {
181 NonIncrementalChange();
182}
183
185 NonIncrementalChange();
186}
187
189 NonIncrementalChange();
190}
191
193 const MPVariable* const variable,
194 double new_value, double old_value) {
195 NonIncrementalChange();
196}
197
199 NonIncrementalChange();
200}
201
203 double coefficient) {
204 NonIncrementalChange();
205}
206
207void SatInterface::SetObjectiveOffset(double value) { NonIncrementalChange(); }
208
209void SatInterface::ClearObjective() { NonIncrementalChange(); }
210
212 return 0; // FIXME
213}
214
215int64_t SatInterface::nodes() const { return 0; }
216
218 return MPSolver::BasisStatus::FREE; // FIXME
219}
220
222 return MPSolver::BasisStatus::FREE; // FIXME
223}
224
225bool SatInterface::IsContinuous() const { return false; }
226bool SatInterface::IsLP() const { return false; }
227bool SatInterface::IsMIP() const { return true; }
228
229std::string SatInterface::SolverVersion() const {
231}
232
233void* SatInterface::underlying_solver() { return nullptr; }
234
235void SatInterface::ExtractNewVariables() { NonIncrementalChange(); }
236
237void SatInterface::ExtractNewConstraints() { NonIncrementalChange(); }
238
239void SatInterface::ExtractObjective() { NonIncrementalChange(); }
240
242 parameters_.Clear();
243 parameters_.set_num_workers(num_threads_);
244 SetCommonParameters(param);
245}
246
247absl::Status SatInterface::SetNumThreads(int num_threads) {
248 num_threads_ = num_threads;
249 return absl::OkStatus();
250}
251
252// All these have no effect.
258
259// TODO(user): Implement me.
261
263 const std::string& parameters) {
264 return ProtobufTextFormatMergeFromString(parameters, &parameters_);
265}
266
267void SatInterface::NonIncrementalChange() {
268 // The current implementation is not incremental.
270}
271
272namespace {
273
274// See MpSolverInterfaceFactoryRepository for details.
275const void* const kRegisterSat ABSL_ATTRIBUTE_UNUSED = [] {
277 [](MPSolver* const solver) { return new SatInterface(solver); },
279 return nullptr;
280}();
281
282} // namespace
283
284} // namespace operations_research
void set_solver_specific_parameters(Arg_ &&arg, Args_... args)
::operations_research::MPModelProto *PROTOBUF_NONNULL mutable_model()
::operations_research::MPSolverResponseStatus status() const
static MPSolverInterfaceFactoryRepository * GetInstance()
void Register(MPSolverInterfaceFactory factory, MPSolver::OptimizationProblemType problem_type, std::function< bool()> is_runtime_ready={})
void set_variable_as_extracted(int var_index, bool extracted)
void set_constraint_as_extracted(int ct_index, bool extracted)
void SetCommonParameters(const MPSolverParameters &param)
The class for variables of a Mathematical Programming (MP) model.
void SetRelativeMipGap(double value) override
void AddVariable(MPVariable *var) override
void SetObjectiveOffset(double value) override
void SetCoefficient(MPConstraint *constraint, const MPVariable *variable, double new_value, double old_value) override
void SetVariableBounds(int index, double lb, double ub) override
bool SupportsDirectlySolveProto(std::atomic< bool > *interrupt) const override
absl::Status SetNumThreads(int num_threads) override
void SetObjectiveCoefficient(const MPVariable *variable, double coefficient) override
void AddRowConstraint(MPConstraint *ct) override
MPSolver::ResultStatus Solve(const MPSolverParameters &param) override
void SetScalingMode(int value) override
void SetVariableInteger(int index, bool integer) override
void ClearConstraint(MPConstraint *constraint) override
int64_t nodes() const override
bool IsContinuous() const override
void SetPresolveMode(int value) override
std::string SolverVersion() const override
void SetDualTolerance(double value) override
void SetOptimizationDirection(bool maximize) override
int64_t iterations() const override
bool SetSolverSpecificParametersAsString(const std::string &parameters) override
bool AddIndicatorConstraint(MPConstraint *const ct) override
MPSolutionResponse DirectlySolveProto(LazyMutableCopy< MPModelRequest > request, std::atomic< bool > *interrupt) override
void SetConstraintBounds(int index, double lb, double ub) override
MPSolver::BasisStatus column_status(int variable_index) const override
void SetPrimalTolerance(double value) override
MPSolver::BasisStatus row_status(int constraint_index) const override
void SetLpAlgorithm(int value) override
void SetParameters(const MPSolverParameters &param) override
std::string CpSatSolverVersion()
Returns a string that describes the version of the solver.
OR-Tools root namespace.
MPSolutionResponse SatSolveProto(LazyMutableCopy< MPModelRequest > request, std::atomic< bool > *interrupt_solve, std::function< void(const std::string &)> logging_callback, std::function< void(const MPSolution &)> solution_callback, std::function< void(const double)> best_bound_callback)
std::string EncodeParametersAsString(const P &parameters)
Definition proto_utils.h:71
bool ProtobufTextFormatMergeFromString(absl::string_view proto_text_string, ProtoType *proto)
Definition proto_utils.h:79