OR-Tools  9.0
cp_sat_solver.h
Go to the documentation of this file.
1 // Copyright 2010-2021 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_MATH_OPT_SOLVERS_CP_SAT_SOLVER_H_
15 #define OR_TOOLS_MATH_OPT_SOLVERS_CP_SAT_SOLVER_H_
16 
17 #include <stdint.h>
18 
19 #include <cstdint>
20 #include <memory>
21 #include <vector>
22 
23 #include "absl/status/status.h"
24 #include "absl/status/statusor.h"
26 #include "ortools/math_opt/callback.pb.h"
27 #include "ortools/math_opt/model.pb.h"
28 #include "ortools/math_opt/model_parameters.pb.h"
29 #include "ortools/math_opt/model_update.pb.h"
30 #include "ortools/math_opt/parameters.pb.h"
31 #include "ortools/math_opt/result.pb.h"
32 #include "ortools/math_opt/solution.pb.h"
34 
35 namespace operations_research {
36 namespace math_opt {
37 
38 class CpSatSolver : public SolverInterface {
39  public:
40  static absl::StatusOr<std::unique_ptr<SolverInterface>> New(
41  const ModelProto& model, const SolverInitializerProto& initializer);
42 
43  absl::StatusOr<SolveResultProto> Solve(
44  const SolveParametersProto& parameters,
45  const ModelSolveParametersProto& model_parameters,
46  const CallbackRegistrationProto& callback_registration,
47  Callback cb) override;
48  absl::Status Update(const ModelUpdateProto& model_update) override;
49  bool CanUpdate(const ModelUpdateProto& model_update) override;
50 
51  private:
52  CpSatSolver(MPModelProto cp_sat_model, std::vector<int64_t> variable_ids);
53 
54  // Extract the solution from CP-SAT's response.
55  //
56  // This function assumes it exists, i.e. that the input `response.status` is
57  // feasible or optimal.
58  PrimalSolutionProto ExtractSolution(
59  const MPSolutionResponse& response,
60  const ModelSolveParametersProto& model_parameters) const;
61 
62  const MPModelProto cp_sat_model_;
63 
64  // For the i-th variable in `cp_sat_model_`, `variable_ids_[i]` contains the
65  // corresponding id in the input `Model`.
66  const std::vector<int64_t> variable_ids_;
67 };
68 
69 } // namespace math_opt
70 } // namespace operations_research
71 
72 #endif // OR_TOOLS_MATH_OPT_SOLVERS_CP_SAT_SOLVER_H_
bool CanUpdate(const ModelUpdateProto &model_update) override
absl::Status Update(const ModelUpdateProto &model_update) override
absl::StatusOr< SolveResultProto > Solve(const SolveParametersProto &parameters, const ModelSolveParametersProto &model_parameters, const CallbackRegistrationProto &callback_registration, Callback cb) override
static absl::StatusOr< std::unique_ptr< SolverInterface > > New(const ModelProto &model, const SolverInitializerProto &initializer)
std::function< absl::StatusOr< CallbackResultProto >(const CallbackDataProto &)> Callback
SatParameters parameters
SharedResponseManager * response
absl::Span< const int64_t > variable_ids
GRBmodel * model
Collection of objects used to extend the Constraint Solver library.