OR-Tools  9.1
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/statusor.h"
21#include "ortools/math_opt/callback.pb.h"
24#include "ortools/math_opt/model.pb.h"
25#include "ortools/math_opt/model_parameters.pb.h"
26#include "ortools/math_opt/model_update.pb.h"
27#include "ortools/math_opt/parameters.pb.h"
28#include "ortools/math_opt/result.pb.h"
29
30namespace operations_research {
31namespace math_opt {
32
33// A solver for a given model and solver implementation.
34//
35// Use the New() function to build a new solver instance; then call Solve() to
36// solve the model. You can then update the model using Update() and resolve.
37//
38// Usage:
39// const ModelProto model = ...;
40// const auto solver = Solver::New(SOLVER_TYPE_GSCIP,
41// model,
42// SolverInitializerProto{});
43// CHECK_OK(solver.status());
44// const SolveParametersProto solve_params = ...;
45//
46// // First solve of the initial Model.
47// const auto first_solution = (*solver)->Solve(solve_params);
48// CHECK_OK(first_solution.status());
49// // Use the first_solution here.
50//
51// // Update the Model with a ModelUpdate.
52// const ModelUpdate update = ...;
53// CHECK_OK((*solver)->Update(update));
54// const auto second_solution = (*solver)->Solve(solve_params);
55// CHECK_OK(second_solution.status());
56// // Use the second_solution of the updated problem here.
57//
58class Solver {
59 public:
60 // Callback function type.
61 using Callback = std::function<CallbackResultProto(const CallbackDataProto&)>;
62
63 // Builds a solver of the given type with the provided model and
64 // initialization parameters.
65 static absl::StatusOr<std::unique_ptr<Solver>> New(
66 SolverType solver_type, const ModelProto& model,
67 const SolverInitializerProto& initializer);
68
69 Solver(const Solver&) = delete;
70 Solver& operator=(const Solver&) = delete;
71
72 // Solves the current model (included all updates).
73 absl::StatusOr<SolveResultProto> Solve(
74 const SolveParametersProto& parameters,
75 const ModelSolveParametersProto& model_parameters = {},
76 const CallbackRegistrationProto& callback_registration = {},
77 Callback user_cb = nullptr);
78
79 // Updates the model to solve and returns true, or returns false if this
80 // update is not supported by the underlying solver.
81 //
82 // A status error will be returned if the model_update is invalid or the
83 // underlying solver has an internal error.
84 absl::StatusOr<bool> Update(const ModelUpdateProto& model_update);
85
86 private:
87 Solver(std::unique_ptr<SolverInterface> underlying_solver,
88 ModelSummary model_summary);
89
90 const std::unique_ptr<SolverInterface> underlying_solver_;
91 ModelSummary model_summary_;
92};
93
94} // namespace math_opt
95} // namespace operations_research
96
97#endif // OR_TOOLS_MATH_OPT_CORE_SOLVER_H_
Solver & operator=(const Solver &)=delete
absl::StatusOr< SolveResultProto > Solve(const SolveParametersProto &parameters, const ModelSolveParametersProto &model_parameters={}, const CallbackRegistrationProto &callback_registration={}, Callback user_cb=nullptr)
Definition: solver.cc:113
std::function< CallbackResultProto(const CallbackDataProto &)> Callback
Definition: solver.h:61
static absl::StatusOr< std::unique_ptr< Solver > > New(SolverType solver_type, const ModelProto &model, const SolverInitializerProto &initializer)
Definition: solver.cc:101
absl::StatusOr< bool > Update(const ModelUpdateProto &model_update)
Definition: solver.cc:156
SatParameters parameters
GRBmodel * model
Collection of objects used to extend the Constraint Solver library.