Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
base_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_BASE_SOLVER_H_
15#define OR_TOOLS_MATH_OPT_CORE_BASE_SOLVER_H_
16
17#include <functional>
18#include <ostream>
19#include <string>
20#include <vector>
21
22#include "absl/status/statusor.h"
23#include "ortools/math_opt/callback.pb.h"
24#include "ortools/math_opt/infeasible_subsystem.pb.h"
25#include "ortools/math_opt/model.pb.h"
26#include "ortools/math_opt/model_parameters.pb.h"
27#include "ortools/math_opt/model_update.pb.h"
28#include "ortools/math_opt/parameters.pb.h"
29#include "ortools/math_opt/result.pb.h"
31
33
34// The API of solvers (in-process, sub-process and streaming RPC ones).
35//
36// Thread-safety: methods Solve() and Update() must not be called concurrently;
37// they should immediately return with an error status if this happens.
38//
39// TODO: b/350984134 - Rename `Solver` into `InProcessSolver` and then rename
40// `BaseSolver` into `Solver`.
42 public:
43 // Callback function for messages callback sent by the solver.
44 //
45 // Each message represents a single output line from the solver, and each
46 // message does not contain any '\n' character in it.
47 //
48 // Thread-safety: a callback may be called concurrently from multiple
49 // threads. The users is expected to use proper synchronization primitives to
50 // deal with that.
51 using MessageCallback = std::function<void(const std::vector<std::string>&)>;
52
53 // Callback function type for MIP/LP callbacks.
54 using Callback = std::function<CallbackResultProto(const CallbackDataProto&)>;
55
56 // Arguments used when calling Solve() to solve the problem.
57 struct SolveArgs {
58 SolveParametersProto parameters;
59 ModelSolveParametersProto model_parameters;
60
61 // An optional callback for messages emitted by the solver.
62 //
63 // When set it enables the solver messages and ignores the `enable_output`
64 // in solve parameters; messages are redirected to the callback and not
65 // printed on stdout/stderr/logs anymore.
67
68 CallbackRegistrationProto callback_registration;
69 Callback user_cb = nullptr;
70
71 // An optional interrupter that the solver can use to interrupt the solve
72 // early.
73 const SolveInterrupter* interrupter = nullptr;
74
75 friend std::ostream& operator<<(std::ostream& out, const SolveArgs& args);
76 };
77
78 // Arguments used when calling ComputeInfeasibleSubsystem().
80 SolveParametersProto parameters;
81
82 // An optional callback for messages emitted by the solver.
83 //
84 // When set it enables the solver messages and ignores the `enable_output`
85 // in solve parameters; messages are redirected to the callback and not
86 // printed on stdout/stderr/logs anymore.
88
89 // An optional interrupter that the solver can use to interrupt the solve
90 // early.
91 const SolveInterrupter* interrupter = nullptr;
92
93 friend std::ostream& operator<<(std::ostream& out,
95 };
96
97 BaseSolver() = default;
98 BaseSolver(const BaseSolver&) = delete;
99 BaseSolver& operator=(const BaseSolver&) = delete;
100 virtual ~BaseSolver() = default;
101
102 // Solves the current model (including all updates).
103 virtual absl::StatusOr<SolveResultProto> Solve(
104 const SolveArgs& arguments) = 0;
105
106 // Computes an infeasible subsystem of `model` (including all updates).
107 virtual absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
109 const ComputeInfeasibleSubsystemArgs& arguments) = 0;
110
111 // Updates the model to solve and returns true, or returns false if this
112 // update is not supported by the underlying solver.
113 //
114 // The model_update is passed by value. Non in-process implementations will
115 // move it in-place in the messages used to communicate with the other
116 // process. Thus if possible, the caller should std::move() this proto to this
117 // function.
118 //
119 // A status error will be returned if the model_update is invalid or the
120 // underlying solver has an internal error.
121 //
122 // When this function returns false, the BaseSolver object is in a failed
123 // state.
124 virtual absl::StatusOr<bool> Update(ModelUpdateProto model_update) = 0;
125};
126
127} // namespace operations_research::math_opt
128
129#endif // OR_TOOLS_MATH_OPT_CORE_BASE_SOLVER_H_
virtual absl::StatusOr< SolveResultProto > Solve(const SolveArgs &arguments)=0
Solves the current model (including all updates).
virtual absl::StatusOr< ComputeInfeasibleSubsystemResultProto > ComputeInfeasibleSubsystem(const ComputeInfeasibleSubsystemArgs &arguments)=0
Computes an infeasible subsystem of model (including all updates).
std::function< CallbackResultProto(const CallbackDataProto &)> Callback
Callback function type for MIP/LP callbacks.
Definition base_solver.h:54
virtual absl::StatusOr< bool > Update(ModelUpdateProto model_update)=0
std::function< void(const std::vector< std::string > &)> MessageCallback
Definition base_solver.h:51
BaseSolver & operator=(const BaseSolver &)=delete
BaseSolver(const BaseSolver &)=delete
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
Arguments used when calling ComputeInfeasibleSubsystem().
Definition base_solver.h:79
friend std::ostream & operator<<(std::ostream &out, const ComputeInfeasibleSubsystemArgs &args)
Arguments used when calling Solve() to solve the problem.
Definition base_solver.h:57
friend std::ostream & operator<<(std::ostream &out, const SolveArgs &args)