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