Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solver.h
Go to the documentation of this file.
1// Copyright 2010-2024 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 <memory>
18
19#include "absl/status/status.h"
20#include "absl/status/statusor.h"
21#include "ortools/math_opt/callback.pb.h"
26#include "ortools/math_opt/infeasible_subsystem.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
34
35// A solver for a given model and solver implementation.
36//
37// Use the New() function to build a new solver instance; then call Solve() to
38// solve the model. You can then update the model using Update() and resolve.
39//
40// Thread-safety: methods Solve() and Update() must not be called concurrently;
41// they will immediately return with an error status if this happens. Some
42// solvers may add more restriction regarding threading. Please see
43// SOLVER_TYPE_XXX documentation for details.
44//
45// Usage:
46// const ModelProto model = ...;
47// const auto solver = Solver::New(SOLVER_TYPE_GSCIP,
48// model,
49// /*arguments=*/{});
50// CHECK_OK(solver.status());
51// Solver::SolveArgs solve_arguments;
52// ...
53//
54// // First solve of the initial Model.
55// const auto first_solution = (*solver)->Solve(solve_arguments);
56// CHECK_OK(first_solution.status());
57// // Use the first_solution here.
58//
59// // Update the Model with a ModelUpdate.
60// const ModelUpdate update = ...;
61// CHECK_OK((*solver)->Update(update));
62// const auto second_solution = (*solver)->Solve(solve_arguments);
63// CHECK_OK(second_solution.status());
64// // Use the second_solution of the updated problem here.
65//
66class Solver : public BaseSolver {
67 public:
69
70 // A shortcut for calling Solver::New() and then Solver::Solve().
71 static absl::StatusOr<SolveResultProto> NonIncrementalSolve(
72 const ModelProto& model, SolverTypeProto solver_type,
73 const InitArgs& init_args, const SolveArgs& solve_args);
74
75 // A shortcut for calling Solver::New() and then
76 // Solver()::ComputeInfeasibleSubsystem()
77 static absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
79 const ModelProto& model, SolverTypeProto solver_type,
80 const InitArgs& init_args,
81 const ComputeInfeasibleSubsystemArgs& compute_infeasible_subsystem_args);
82
83 // Builds a solver of the given type with the provided model and
84 // initialization parameters.
85 static absl::StatusOr<std::unique_ptr<Solver>> New(
86 SolverTypeProto solver_type, const ModelProto& model,
87 const InitArgs& arguments);
88
89 ~Solver() override;
90
91 absl::StatusOr<SolveResultProto> Solve(const SolveArgs& arguments) override;
92
93 // See BaseSolver::Update.
94 //
95 // When this function returns false, the Solver object is in a failed
96 // state. In that case the underlying SolverInterface implementation has been
97 // destroyed (this enables the caller to instantiate a new Solver without
98 // destroying the previous one first even if they use Gurobi with a single-use
99 // license).
100 absl::StatusOr<bool> Update(ModelUpdateProto model_update) override;
101
102 absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
104 const ComputeInfeasibleSubsystemArgs& arguments) override;
105
106 private:
107 Solver(std::unique_ptr<SolverInterface> underlying_solver,
108 ModelSummary model_summary);
109
110 // Tracker used to ensure that Solve() and Update() are not called
111 // concurrently.
112 ConcurrentCallsGuard::Tracker concurrent_calls_tracker_;
113
114 // Can be nullptr only if fatal_failure_occurred_ is true (but the contrary is
115 // not true). This happens when Update() returns false.
116 std::unique_ptr<SolverInterface> underlying_solver_;
117
118 ModelSummary model_summary_;
119
120 // Set to true if a previous call to Solve() or Update() returned a failing
121 // status (or if Update() returned false).
122 //
123 // This is guarded by concurrent_calls_tracker_.
124 bool fatal_failure_occurred_ = false;
125};
126
127namespace internal {
128
129// Validates that the input streamable and non_streamable init arguments are
130// either not set or are the one of solver_type.
131absl::Status ValidateInitArgs(const Solver::InitArgs& init_args,
132 SolverTypeProto solver_type);
133
134} // namespace internal
135} // namespace operations_research::math_opt
136
137#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)
A shortcut for calling Solver::New() and then Solver::Solve().
Definition solver.cc:70
absl::StatusOr< bool > Update(ModelUpdateProto model_update) override
Definition solver.cc:158
absl::StatusOr< ComputeInfeasibleSubsystemResultProto > ComputeInfeasibleSubsystem(const ComputeInfeasibleSubsystemArgs &arguments) override
Computes an infeasible subsystem of model (including all updates).
Definition solver.cc:188
absl::StatusOr< SolveResultProto > Solve(const SolveArgs &arguments) override
Solves the current model (including all updates).
Definition solver.cc:101
static absl::StatusOr< ComputeInfeasibleSubsystemResultProto > NonIncrementalComputeInfeasibleSubsystem(const ModelProto &model, SolverTypeProto solver_type, const InitArgs &init_args, const ComputeInfeasibleSubsystemArgs &compute_infeasible_subsystem_args)
Definition solver.cc:220
static absl::StatusOr< std::unique_ptr< Solver > > New(SolverTypeProto solver_type, const ModelProto &model, const InitArgs &arguments)
Definition solver.cc:88
GRBmodel * model
absl::Status ValidateInitArgs(const Solver::InitArgs &init_args, const SolverTypeProto solver_type)
Definition solver.cc:231
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
Arguments used when calling ComputeInfeasibleSubsystem().
Definition base_solver.h:79
Arguments used when calling Solve() to solve the problem.
Definition base_solver.h:57