OR-Tools  9.3
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_CORE_SOLVER_H_
15#define OR_TOOLS_MATH_OPT_CORE_SOLVER_H_
16
17#include <functional>
18#include <memory>
19
20#include "absl/status/status.h"
21#include "absl/status/statusor.h"
22#include "absl/synchronization/mutex.h"
23#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
33namespace operations_research {
34namespace math_opt {
35
36// A solver for a given model and solver implementation.
37//
38// Use the New() function to build a new solver instance; then call Solve() to
39// solve the model. You can then update the model using Update() and resolve.
40//
41// Thread-safety: methods Solve() and Update() must not be called concurrently;
42// they will immediately return with an error status if this happens. Some
43// solvers may add more restriction regarding threading. Please see
44// SOLVER_TYPE_XXX documentation for details.
45//
46// Usage:
47// const ModelProto model = ...;
48// const auto solver = Solver::New(SOLVER_TYPE_GSCIP,
49// model,
50// /*arguments=*/{});
51// CHECK_OK(solver.status());
52// Solver::SolveArgs solve_arguments;
53// ...
54//
55// // First solve of the initial Model.
56// const auto first_solution = (*solver)->Solve(solve_arguments);
57// CHECK_OK(first_solution.status());
58// // Use the first_solution here.
59//
60// // Update the Model with a ModelUpdate.
61// const ModelUpdate update = ...;
62// CHECK_OK((*solver)->Update(update));
63// const auto second_solution = (*solver)->Solve(solve_arguments);
64// CHECK_OK(second_solution.status());
65// // Use the second_solution of the updated problem here.
66//
67class Solver {
68 public:
70
71 // Callback function for messages callback sent by the solver.
72 //
73 // Each message represents a single output line from the solver, and each
74 // message does not contain any '\n' character in it.
75 //
76 // Thread-safety: a callback may be called concurrently from multiple
77 // threads. The users is expected to use proper synchronization primitives to
78 // deal with that.
80
81 // Callback function type for MIP/LP callbacks.
82 using Callback = std::function<CallbackResultProto(const CallbackDataProto&)>;
83
84 // Arguments used when calling Solve() to solve the problem.
85 struct SolveArgs {
86 SolveParametersProto parameters;
87 ModelSolveParametersProto model_parameters;
88
89 // An optional callback for messages emitted by the solver.
90 //
91 // When set it enables the solver messages and ignores the `enable_output`
92 // in solve parameters; messages are redirected to the callback and not
93 // printed on stdout/stderr/logs anymore.
95
96 CallbackRegistrationProto callback_registration;
97 Callback user_cb = nullptr;
98
99 // An optional interrupter that the solver can use to interrupt the solve
100 // early.
102 };
103
104 // A shortcut for calling Solver::New() and then Solver::Solve().
105 static absl::StatusOr<SolveResultProto> NonIncrementalSolve(
106 const ModelProto& model, SolverTypeProto solver_type,
107 const InitArgs& init_args, const SolveArgs& solve_args);
108
109 // Builds a solver of the given type with the provided model and
110 // initialization parameters.
111 static absl::StatusOr<std::unique_ptr<Solver>> New(
112 SolverTypeProto solver_type, const ModelProto& model,
113 const InitArgs& arguments);
114
115 Solver(const Solver&) = delete;
116 Solver& operator=(const Solver&) = delete;
117
118 ~Solver();
119
120 // Solves the current model (included all updates).
121 absl::StatusOr<SolveResultProto> Solve(const SolveArgs& arguments);
122
123 // Updates the model to solve and returns true, or returns false if this
124 // update is not supported by the underlying solver.
125 //
126 // A status error will be returned if the model_update is invalid or the
127 // underlying solver has an internal error.
128 absl::StatusOr<bool> Update(const ModelUpdateProto& model_update);
129
130 private:
131 Solver(std::unique_ptr<SolverInterface> underlying_solver,
132 ModelSummary model_summary);
133
134 // Mutex used to ensure that Solve() and Update() are not called concurrently.
135 absl::Mutex mutex_;
136
137 const std::unique_ptr<SolverInterface> underlying_solver_;
138 ModelSummary model_summary_;
139};
140
141namespace internal {
142
143// Validates that the input streamable and non_streamable init arguments are
144// either not set or are the one of solver_type.
145absl::Status ValidateInitArgs(const Solver::InitArgs& init_args,
146 SolverTypeProto solver_type);
147
148} // namespace internal
149} // namespace math_opt
150} // namespace operations_research
151
152#endif // OR_TOOLS_MATH_OPT_CORE_SOLVER_H_
static absl::StatusOr< SolveResultProto > NonIncrementalSolve(const ModelProto &model, SolverTypeProto solver_type, const InitArgs &init_args, const SolveArgs &solve_args)
Definition: solver.cc:161
absl::StatusOr< SolveResultProto > Solve(const SolveArgs &arguments)
Definition: solver.cc:192
Solver & operator=(const Solver &)=delete
SolverInterface::MessageCallback MessageCallback
Definition: solver.h:79
std::function< CallbackResultProto(const CallbackDataProto &)> Callback
Definition: solver.h:82
static absl::StatusOr< std::unique_ptr< Solver > > New(SolverTypeProto solver_type, const ModelProto &model, const InitArgs &arguments)
Definition: solver.cc:179
absl::StatusOr< bool > Update(const ModelUpdateProto &model_update)
Definition: solver.cc:237
std::function< void(const std::vector< std::string > &)> MessageCallback
GRBmodel * model
absl::Status ValidateInitArgs(const Solver::InitArgs &init_args, const SolverTypeProto solver_type)
Definition: solver.cc:251
Collection of objects used to extend the Constraint Solver library.
CallbackRegistrationProto callback_registration
Definition: solver.h:96
ModelSolveParametersProto model_parameters
Definition: solver.h:87