Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
model_solve_parameters.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#ifndef OR_TOOLS_MATH_OPT_CPP_MODEL_SOLVE_PARAMETERS_H_
18#define OR_TOOLS_MATH_OPT_CPP_MODEL_SOLVE_PARAMETERS_H_
19
20#include <sys/types.h>
21
22#include <cstdint>
23#include <initializer_list>
24#include <optional>
25#include <vector>
26
27#include "absl/container/flat_hash_set.h"
28#include "absl/status/status.h"
29#include "absl/status/statusor.h"
31#include "ortools/math_opt/cpp/map_filter.h" // IWYU pragma: export
35#include "ortools/math_opt/model_parameters.pb.h"
37
38namespace operations_research {
39namespace math_opt {
40
41// Parameters to control a single solve that are specific to the input
42// model (see SolveParametersProto for model independent parameters).
44 // Returns the parameters that empty DualSolution and DualRay, only keep the
45 // values of all variables in PrimalSolution and PrimalRay.
46 //
47 // This is a shortcut method that is equivalent to setting the dual filters
48 // with MakeSkipAllFilter().
50
51 // Returns the parameters that empty DualSolution and DualRay, only keep the
52 // values of the specified variables in PrimalSolution and PrimalRay.
53 //
54 // The input Collection must be usable in a for-range loop with Variable
55 // values. This will be typically a std::vector<Variable> or and
56 // std::initializer_list<Variable> (see the other overload).
57 //
58 // This is a shortcut method that is equivalent to setting the dual filters
59 // with MakeSkipAllFilter() and the variable_values_filter with
60 // MakeKeepKeysFilter(variables).
61 //
62 // Example:
63 // std::vector<Variable> decision_vars = ...;
64 // const auto params =
65 // ModelSolveParameters::OnlySomePrimalVariables(decision_vars);
66 template <typename Collection>
68 const Collection& variables);
69
70 // Returns the parameters that empty DualSolution and DualRay, only keeping
71 // the values of the specified variables in PrimalSolution and PrimalRay.
72 //
73 // See the other overload's documentation for details. This overload is needed
74 // since C++ can't guess the type when using an initializer list expression.
75 //
76 // Example:
77 // const Variable a = ...;
78 // const Variable b = ...;
79 // const auto params =
80 // ModelSolveParameters::OnlySomePrimalVariables({a, b});
82 std::initializer_list<Variable> variables);
83
84 // The filter that is applied to variable_values of both PrimalSolution and
85 // PrimalRay.
87
88 // The filter that is applied to dual_values of DualSolution and DualRay.
90
91 // The filter that is applied to quadratic_dual_values of DualSolution and
92 // DualRay.
94
95 // The filter that is applied to reduced_costs of DualSolution and DualRay.
97
98 // Optional initial basis for warm starting simplex LP solvers. If set, it is
99 // expected to be valid.
100 std::optional<Basis> initial_basis;
101
102 // A suggested starting solution for the solver.
103 //
104 // MIP solvers generally only want primal information (`variable_values`),
105 // while LP solvers want both primal and dual information (`dual_values`).
106 //
107 // Many MIP solvers can work with: (1) partial solutions that do not specify
108 // all variables or (2) infeasible solutions. In these cases, solvers
109 // typically solve a sub-MIP to complete/correct the hint.
110 //
111 // How the hint is used by the solver, if at all, is highly dependent on the
112 // solver, the problem type, and the algorithm used. The most reliable way to
113 // ensure your hint has an effect is to read the underlying solvers logs with
114 // and without the hint.
115 //
116 // Simplex-based LP solvers typically prefer an initial basis to a solution
117 // hint (they need to crossover to convert the hint to a basic feasible
118 // solution otherwise).
120 // Values should be finite and not NaN (otherwise, `Solve()` will return a
121 // `Status` error).
123
124 // Values should be finite and not NaN (otherwise, `Solve()` will return a
125 // `Status` error).
127
128 // Returns a failure if the referenced variables and constraints don't
129 // belong to the input expected_storage (which must not be nullptr).
130 absl::Status CheckModelStorage(const ModelStorage* expected_storage) const;
131
132 // Returns the proto equivalent of this object.
133 //
134 // The caller should use CheckModelStorage() as this function does not check
135 // internal consistency of the referenced variables and constraints.
136 SolutionHintProto Proto() const;
137
138 // Returns the hint corresponding to this proto and the given model.
139 //
140 // This can be useful when loading a hint from another format, e.g. with
141 // MPModelProtoSolutionHintToMathOptHint().
142 static absl::StatusOr<SolutionHint> FromProto(
143 const Model& model, const SolutionHintProto& hint_proto);
144 };
145
146 // Optional solution hints. If the underlying solver only accepts a single
147 // hint, the first hint is used.
148 std::vector<SolutionHint> solution_hints;
149
150 // Optional branching priorities. Variables with higher values will be
151 // branched on first. Variables for which priorities are not set get the
152 // solver's default priority (usually zero).
154
155 // Parameters for an individual objective in a multi-objective model.
157 // Optional objective degradation absolute tolerance. For a hierarchical
158 // multi-objective solver, each objective fⁱ is processed in priority order:
159 // the solver determines the optimal objective value Γⁱ, if it exists,
160 // subject to all constraints in the model and the additional constraints
161 // that fᵏ(x) = Γᵏ (within tolerances) for each k < i. If set, a solution is
162 // considered to be "within tolerances" for this objective fᵏ if
163 // |fᵏ(x) - Γᵏ| ≤ `objective_degradation_absolute_tolerance`.
164 //
165 // See also `objective_degradation_relative_tolerance`; if both parameters
166 // are set for a given objective, the solver need only satisfy one to be
167 // considered "within tolerances".
168 //
169 // If set, must be nonnegative.
171
172 // Optional objective degradation relative tolerance. For a hierarchical
173 // multi-objective solver, each objective fⁱ is processed in priority order:
174 // the solver determines the optimal objective value Γⁱ, if it exists,
175 // subject to all constraints in the model and the additional constraints
176 // that fᵏ(x) = Γᵏ (within tolerances) for each k < i. If set, a solution is
177 // considered to be "within tolerances" for this objective fᵏ if
178 // |fᵏ(x) - Γᵏ| ≤ `objective_degradation_relative_tolerance` * |Γᵏ|.
179 //
180 // See also `objective_degradation_absolute_tolerance`; if both parameters
181 // are set for a given objective, the solver need only satisfy one to be
182 // considered "within tolerances".
183 //
184 // If set, must be nonnegative.
186
187 // Returns the proto equivalent of this object.
188 ObjectiveParametersProto Proto() const;
189
190 static ObjectiveParameters FromProto(const ObjectiveParametersProto& proto);
191 };
192 // Parameters for individual objectives in a multi-objective model.
194
195 // Optional lazy constraint annotations. Included linear constraints will be
196 // marked as "lazy" with supporting solvers, meaning that they will only be
197 // added to the working model as-needed as the solver runs.
198 //
199 // Note that this an algorithmic hint that does not affect the model's
200 // feasible region; solvers not supporting these annotations will simply
201 // ignore it.
202 absl::flat_hash_set<LinearConstraint> lazy_linear_constraints;
203
204 // Returns a failure if the referenced variables and constraints do not belong
205 // to the input expected_storage (which must not be nullptr).
206 absl::Status CheckModelStorage(const ModelStorage* expected_storage) const;
207
208 // Returns the proto equivalent of this object.
209 //
210 // The caller should use CheckModelStorage() as this function does not check
211 // internal consistency of the referenced variables and constraints.
212 ModelSolveParametersProto Proto() const;
213
214 // Returns the ModelSolveParameters corresponding to this proto and the given
215 // model.
216 static absl::StatusOr<ModelSolveParameters> FromProto(
217 const Model& model, const ModelSolveParametersProto& proto);
218};
219
221// Inline functions implementations.
223
224template <typename Collection>
226 const Collection& variables) {
228 parameters.variable_values_filter = MakeKeepKeysFilter(variables);
229 return parameters;
230}
231
232} // namespace math_opt
233} // namespace operations_research
234
235#endif // OR_TOOLS_MATH_OPT_CPP_MODEL_SOLVE_PARAMETERS_H_
SatParameters parameters
CpModelProto proto
The output proto.
GRBmodel * model
absl::flat_hash_map< Variable, V > VariableMap
absl::flat_hash_map< LinearConstraint, V > LinearConstraintMap
absl::flat_hash_map< Objective, V > ObjectiveMap
Definition objective.h:123
MapFilter< ValueType > MakeKeepKeysFilter(const Collection &keys)
Definition map_filter.h:167
In SWIG mode, we don't want anything besides these top-level includes.
Parameters for an individual objective in a multi-objective model.
static ObjectiveParameters FromProto(const ObjectiveParametersProto &proto)
ObjectiveParametersProto Proto() const
Returns the proto equivalent of this object.
static absl::StatusOr< SolutionHint > FromProto(const Model &model, const SolutionHintProto &hint_proto)
absl::Status CheckModelStorage(const ModelStorage *expected_storage) const
absl::Status CheckModelStorage(const ModelStorage *expected_storage) const
MapFilter< QuadraticConstraint > quadratic_dual_values_filter
MapFilter< LinearConstraint > dual_values_filter
The filter that is applied to dual_values of DualSolution and DualRay.
ObjectiveMap< ObjectiveParameters > objective_parameters
Parameters for individual objectives in a multi-objective model.
static ModelSolveParameters OnlySomePrimalVariables(const Collection &variables)
MapFilter< Variable > reduced_costs_filter
The filter that is applied to reduced_costs of DualSolution and DualRay.
static absl::StatusOr< ModelSolveParameters > FromProto(const Model &model, const ModelSolveParametersProto &proto)
absl::flat_hash_set< LinearConstraint > lazy_linear_constraints