Google OR-Tools v9.15
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-2025 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 ORTOOLS_MATH_OPT_CORE_BASE_SOLVER_H_
15#define ORTOOLS_MATH_OPT_CORE_BASE_SOLVER_H_
16
17#include <functional>
18#include <ostream>
19#include <string>
20#include <vector>
21
22#include "absl/base/nullability.h"
23#include "absl/status/statusor.h"
32
34
35// The API of solvers (in-process, sub-process and streaming RPC ones).
36//
37// Thread-safety: methods Solve(), ComputeInfeasibleSubsystem() and Update()
38// must not be called concurrently; they should immediately return with an error
39// status if this happens.
40//
41// TODO: b/350984134 - Rename `Solver` into `InProcessSolver` and then rename
42// `BaseSolver` into `Solver`.
44 public:
45 // Callback function for messages callback sent by the solver.
46 //
47 // Each message represents a single output line from the solver, and each
48 // message does not contain any '\n' character in it.
49 //
50 // Thread-safety: a callback may be called concurrently from multiple
51 // threads. The users is expected to use proper synchronization primitives to
52 // deal with that.
53 using MessageCallback = std::function<void(const std::vector<std::string>&)>;
54
55 // Callback function type for MIP/LP callbacks.
56 using Callback = std::function<CallbackResultProto(const CallbackDataProto&)>;
57
58 // Arguments used when calling Solve() to solve the problem.
59 struct SolveArgs {
62
63 // An optional callback for messages emitted by the solver.
64 //
65 // When set it enables the solver messages and ignores the `enable_output`
66 // in solve parameters; messages are redirected to the callback and not
67 // printed on stdout/stderr/logs anymore.
69
70 // Registration parameter controlling calls to user_cb.
72
73 // An optional MIP/LP callback. Only called for events registered in
74 // callback_registration.
75 //
76 // Solve() returns an error if called without a user_cb but with some
77 // non-empty callback_registration.request_registration.
78 Callback user_cb = nullptr;
79
80 // An optional interrupter that the solver can use to interrupt the solve
81 // early.
82 const SolveInterrupter* absl_nullable interrupter = nullptr;
83
84 friend std::ostream& operator<<(std::ostream& out, const SolveArgs& args);
85 };
86
87 // Arguments used when calling ComputeInfeasibleSubsystem().
90
91 // An optional callback for messages emitted by the solver.
92 //
93 // When set it enables the solver messages and ignores the `enable_output`
94 // in solve parameters; messages are redirected to the callback and not
95 // printed on stdout/stderr/logs anymore.
97
98 // An optional interrupter that the solver can use to interrupt the solve
99 // early.
100 const SolveInterrupter* absl_nullable interrupter = nullptr;
101
102 friend std::ostream& operator<<(std::ostream& out,
104 };
105
106 BaseSolver() = default;
107 BaseSolver(const BaseSolver&) = delete;
108 BaseSolver& operator=(const BaseSolver&) = delete;
109 virtual ~BaseSolver() = default;
110
111 // Solves the current model (including all updates).
112 virtual absl::StatusOr<SolveResultProto> Solve(
113 const SolveArgs& arguments) = 0;
114
115 // Computes an infeasible subsystem of `model` (including all updates).
116 virtual absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
118 const ComputeInfeasibleSubsystemArgs& arguments) = 0;
119
120 // Updates the model to solve and returns true, or returns false if this
121 // update is not supported by the underlying solver.
122 //
123 // The model_update is passed by value. Non in-process implementations will
124 // move it in-place in the messages used to communicate with the other
125 // process. Thus if possible, the caller should std::move() this proto to this
126 // function.
127 //
128 // A status error will be returned if the model_update is invalid or the
129 // underlying solver has an internal error.
130 //
131 // When this function returns false, the BaseSolver object is in a failed
132 // state.
133 virtual absl::StatusOr<bool> Update(ModelUpdateProto model_update) = 0;
134};
135
136} // namespace operations_research::math_opt
137
138#endif // ORTOOLS_MATH_OPT_CORE_BASE_SOLVER_H_
virtual absl::StatusOr< SolveResultProto > Solve(const SolveArgs &arguments)=0
virtual absl::StatusOr< ComputeInfeasibleSubsystemResultProto > ComputeInfeasibleSubsystem(const ComputeInfeasibleSubsystemArgs &arguments)=0
std::function< CallbackResultProto(const CallbackDataProto &)> Callback
Definition base_solver.h:56
virtual absl::StatusOr< bool > Update(ModelUpdateProto model_update)=0
std::function< void(const std::vector< std::string > &)> MessageCallback
Definition base_solver.h:53
BaseSolver & operator=(const BaseSolver &)=delete
BaseSolver(const BaseSolver &)=delete
friend std::ostream & operator<<(std::ostream &out, const ComputeInfeasibleSubsystemArgs &args)
const SolveInterrupter *absl_nullable interrupter
Definition base_solver.h:82
friend std::ostream & operator<<(std::ostream &out, const SolveArgs &args)