Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solve_impl.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_CPP_SOLVE_IMPL_H_
15#define ORTOOLS_MATH_OPT_CPP_SOLVE_IMPL_H_
16
17#include <memory>
18
19#include "absl/base/nullability.h"
20#include "absl/functional/any_invocable.h"
21#include "absl/status/statusor.h"
34
36
37// A factory of solver.
38//
39// The local_canceller is a local interrupter that exists in the scope of
40// SolveImpl(), ComputeInfeasibleSubsystemImpl() or IncrementalSolverImpl. It is
41// triggered:
42// * either when the user_canceller is triggered
43// * or when the BaseSolver::Callback returns an invalid CallbackResultProto; in
44// that case a new CallbackResultProto with its `terminate` set to true is
45// also returned instead.
46//
47// Solvers that don't support cancellation (i.e. in-process solving) should
48// ignore the local_canceller: this use case won't have a user_canceller and the
49// CallbackResultProto.terminate will terminate the solve as soon as possible if
50// the CallbackResultProto is invalid.
52 absl::AnyInvocable<absl::StatusOr<std::unique_ptr<BaseSolver>>(
53 SolverTypeProto solver_type, ModelProto model,
54 SolveInterrupter* local_canceller) const>;
55
56// Solves the input model.
57//
58// The `user_canceller` parameter is optional.
59absl::StatusOr<SolveResult> SolveImpl(
60 BaseSolverFactory solver_factory, const Model& model,
61 SolverType solver_type, const SolveArguments& solve_args,
62 const SolveInterrupter* absl_nullable user_canceller, bool remove_names);
63
64// ComputeInfeasibleSubsystems the input model in a subprocess.
65//
66// The `user_canceller` parameter is optional.
67absl::StatusOr<ComputeInfeasibleSubsystemResult> ComputeInfeasibleSubsystemImpl(
68 BaseSolverFactory solver_factory, const Model& model,
69 SolverType solver_type,
70 const ComputeInfeasibleSubsystemArguments& compute_args,
71 const SolveInterrupter* absl_nullable user_canceller, bool remove_names);
72
73// Incremental solve of a model.
74class IncrementalSolverImpl : public IncrementalSolver {
75 public:
76 // Creates a new incremental solve.
77 //
78 // The `user_canceller` parameter is optional.
79 static absl::StatusOr<std::unique_ptr<IncrementalSolverImpl>> New(
80 BaseSolverFactory solver_factory, Model* model, SolverType solver_type,
81 const SolveInterrupter* absl_nullable user_canceller, bool remove_names);
82
83 absl::StatusOr<UpdateResult> Update() override;
84
85 absl::StatusOr<SolveResult> SolveWithoutUpdate(
86 const SolveArguments& arguments) const override;
87
88 absl::StatusOr<ComputeInfeasibleSubsystemResult>
90 const ComputeInfeasibleSubsystemArguments& arguments) const override;
91
92 SolverType solver_type() const override { return solver_type_; }
93
94 private:
97 bool remove_names, std::shared_ptr<SolveInterrupter> local_canceller,
98 std::unique_ptr<const ScopedSolveInterrupterCallback> user_canceller_cb,
99 ModelStorageCPtr expected_storage,
100 std::unique_ptr<UpdateTracker> update_tracker,
101 std::unique_ptr<BaseSolver> solver);
102
103 const BaseSolverFactory solver_factory_;
104 const SolverType solver_type_;
105 const bool remove_names_;
106 // Here we use a shared_ptr so that we don't have to make sure that
107 // user_canceller_cb_, which points to local_canceller_ via a lambda-capture,
108 // can be destroyed after local_canceller_ without risk.
109 std::shared_ptr<SolveInterrupter> local_canceller_;
110 std::unique_ptr<const ScopedSolveInterrupterCallback> user_canceller_cb_;
111 const ModelStorageCPtr expected_storage_;
112 const std::unique_ptr<UpdateTracker> update_tracker_;
113 std::unique_ptr<BaseSolver> solver_;
114};
115
116} // namespace operations_research::math_opt::internal
117
118#endif // ORTOOLS_MATH_OPT_CPP_SOLVE_IMPL_H_
absl::StatusOr< SolveResult > SolveWithoutUpdate() const
absl::StatusOr< ComputeInfeasibleSubsystemResult > ComputeInfeasibleSubsystemWithoutUpdate() const
absl::StatusOr< UpdateResult > Update() override
static absl::StatusOr< std::unique_ptr< IncrementalSolverImpl > > New(BaseSolverFactory solver_factory, Model *model, SolverType solver_type, const SolveInterrupter *absl_nullable user_canceller, bool remove_names)
absl::StatusOr< ComputeInfeasibleSubsystemResult > ComputeInfeasibleSubsystemImpl(const BaseSolverFactory solver_factory, const Model &model, const SolverType solver_type, const ComputeInfeasibleSubsystemArguments &compute_args, const SolveInterrupter *absl_nullable const user_canceller, const bool remove_names)
absl::AnyInvocable< absl::StatusOr< std::unique_ptr< BaseSolver > >( SolverTypeProto solver_type, ModelProto model, SolveInterrupter *local_canceller) const > BaseSolverFactory
Definition solve_impl.h:51
absl::StatusOr< SolveResult > SolveImpl(const BaseSolverFactory solver_factory, const Model &model, const SolverType solver_type, const SolveArguments &solve_args, const SolveInterrupter *absl_nullable const user_canceller, const bool remove_names)
const ModelStorage *absl_nonnull ModelStorageCPtr