Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solve.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// IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h"
15// IWYU pragma: friend "ortools/math_opt/cpp/.*"
16
17// Functions and classes used to solve a Model.
18//
19// The main entry point is the Solve() function.
20//
21// For users that need incremental solving, there is the IncrementalSolver
22// class.
23
24#ifndef OR_TOOLS_MATH_OPT_CPP_SOLVE_H_
25#define OR_TOOLS_MATH_OPT_CPP_SOLVE_H_
26
27#include <functional>
28#include <memory>
29
30#include "absl/status/statusor.h"
33#include "ortools/math_opt/cpp/incremental_solver.h" // IWYU pragma: export
35#include "ortools/math_opt/cpp/parameters.h" // IWYU pragma: export
36#include "ortools/math_opt/cpp/solve_arguments.h" // IWYU pragma: export
37#include "ortools/math_opt/cpp/solve_result.h" // IWYU pragma: export
38#include "ortools/math_opt/cpp/solver_init_arguments.h" // IWYU pragma: export
39#include "ortools/math_opt/cpp/update_result.h" // IWYU pragma: export
40#include "ortools/math_opt/parameters.pb.h" // IWYU pragma: export
41
42namespace operations_research {
43namespace math_opt {
44
45// Solves the input model.
46//
47// A Status error will be returned if the inputs are invalid or there is an
48// unexpected failure in an underlying solver or for some internal math_opt
49// errors. Otherwise, check SolveResult::termination.reason to see if an optimal
50// solution was found.
51//
52// Memory model: the returned SolveResult owns its own memory (for solutions,
53// solve stats, etc.), EXCEPT for a pointer back to the model. As a result:
54// * Keep the model alive to access SolveResult,
55// * Avoid unnecessarily copying SolveResult,
56// * The result is generally accessible after mutating the model, but some care
57// is needed if variables or linear constraints are added or deleted.
58//
59// Thread-safety: this method is safe to call concurrently on the same Model.
60//
61// Some solvers may add more restrictions regarding threading. Please see
62// SolverType::kXxx documentation for details.
63absl::StatusOr<SolveResult> Solve(const Model& model, SolverType solver_type,
64 const SolveArguments& solve_args = {},
65 const SolverInitArguments& init_args = {});
66
67// The type of a standard function with the same signature as Solve() above.
68//
69// If you want mock the Solve() for testing, you can take a SolveFunction as
70// an argument, e.g.
71// absl::Status DoMySolve(SolveFunction solve_function=Solve) {
72// Model model;
73// // fill in model...
74// SolveArguments args;
75// SolveInitArguments init_args;
76// ASSIGN_OR_RETURN(
77// const SolveResult result,
78// solve_function(model, SolverType::kGscip, args, init_args));
79// // process result...
80// return absl::OkStatus();
81// }
83 std::function<absl::StatusOr<operations_research::math_opt::SolveResult>(
88
89// Computes an infeasible subsystem of the input model.
90//
91// A Status error will be returned if the inputs are invalid or there is an
92// unexpected failure in an underlying solver or for some internal math_opt
93// errors. Otherwise, check ComputeInfeasibleSubsystemResult::feasibility to see
94// if an infeasible subsystem was found.
95//
96// Memory model: the returned ComputeInfeasibleSubsystemResult owns its own
97// memory (for subsystems, solve stats, etc.), EXCEPT for a pointer back to the
98// model. As a result:
99// * Keep the model alive to access ComputeInfeasibleSubsystemResult,
100// * Avoid unnecessarily copying ComputeInfeasibleSubsystemResult,
101// * The result is generally accessible after mutating the model, but some care
102// is needed if variables or linear constraints are added or deleted.
103//
104// Thread-safety: this method is safe to call concurrently on the same Model.
105absl::StatusOr<ComputeInfeasibleSubsystemResult> ComputeInfeasibleSubsystem(
106 const Model& model, SolverType solver_type,
107 const ComputeInfeasibleSubsystemArguments& compute_args = {},
108 const SolverInitArguments& init_args = {});
109
110// Creates a new incremental solve for the given model. It may returns an
111// error if the parameters are invalid (for example if the selected solver is
112// not linked in the binary).
113//
114// The returned IncrementalSolver keeps a copy of `arguments`. Thus the
115// content of arguments.non_streamable (for example pointers to solver
116// specific struct) must be valid until the destruction of the
117// IncrementalSolver. It also registers on the Model to keep track of updates
118// (see class documentation for details).
119absl::StatusOr<std::unique_ptr<IncrementalSolver>> NewIncrementalSolver(
120 Model* model, SolverType solver_type, SolverInitArguments arguments = {});
121
122} // namespace math_opt
123} // namespace operations_research
124
125#endif // OR_TOOLS_MATH_OPT_CPP_SOLVE_H_
GRBmodel * model
SolverType
The solvers supported by MathOpt.
Definition parameters.h:42
absl::StatusOr< SolveResult > Solve(const Model &model, const SolverType solver_type, const SolveArguments &solve_args, const SolverInitArguments &init_args)
Definition solve.cc:62
absl::StatusOr< std::unique_ptr< IncrementalSolver > > NewIncrementalSolver(Model *model, SolverType solver_type, SolverInitArguments arguments)
Definition solve.cc:82
absl::StatusOr< ComputeInfeasibleSubsystemResult > ComputeInfeasibleSubsystem(const Model &model, const SolverType solver_type, const ComputeInfeasibleSubsystemArguments &compute_args, const SolverInitArguments &init_args)
Definition solve.cc:72
std::function< absl::StatusOr< operations_research::math_opt::SolveResult >( const operations_research::math_opt::Model &, operations_research::math_opt::SolverType, const operations_research::math_opt::SolveArguments &, const operations_research::math_opt::SolverInitArguments &)> SolveFunction
Definition solve.h:82
In SWIG mode, we don't want anything besides these top-level includes.
Arguments passed to ComputeInfeasibleSubsystem() to control the solver.