OR-Tools  9.3
parameters.h
Go to the documentation of this file.
1// Copyright 2010-2021 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_PARAMETERS_H_
15#define OR_TOOLS_MATH_OPT_CPP_PARAMETERS_H_
16
17#include <cstdint>
18#include <optional>
19#include <string>
20
21#include "absl/status/statusor.h"
22#include "absl/strings/string_view.h"
23#include "absl/time/time.h"
24#include "absl/types/span.h"
26#include "ortools/glop/parameters.pb.h" // IWYU pragma: export
27#include "ortools/gscip/gscip.pb.h" // IWYU pragma: export
28#include "ortools/math_opt/cpp/enums.h" // IWYU pragma: export
29#include "ortools/math_opt/parameters.pb.h"
30#include "ortools/math_opt/solvers/gurobi.pb.h" // IWYU pragma: export
31#include "ortools/sat/sat_parameters.pb.h" // IWYU pragma: export
32
33namespace operations_research {
34namespace math_opt {
35
36// The solvers wrapped by MathOpt.
37enum class SolverType {
38 // Solving Constraint Integer Programs (SCIP) solver.
39 //
40 // It supports both MIPs and LPs. No dual data for LPs is returned though. To
41 // solve LPs, kGlop should be preferred.
42 kGscip = SOLVER_TYPE_GSCIP,
43
44 // Gurobi solver.
45 //
46 // It supports both MIPs and LPs.
47 kGurobi = SOLVER_TYPE_GUROBI,
48
49 // Google's Glop linear solver.
50 //
51 // It only solves LPs.
52 kGlop = SOLVER_TYPE_GLOP,
53
54 // Google's CP-SAT solver.
55 //
56 // It supports solving IPs and can scale MIPs to solve them as IPs.
57 kCpSat = SOLVER_TYPE_CP_SAT,
58
59 // GNU Linear Programming Kit (GLPK).
60 //
61 // It supports both MIPs and LPs.
62 //
63 // Thread-safety: GLPK use thread-local storage for memory allocations. As a
64 // consequence when using IncrementalSolver, the user must make sure that
65 // instances are destroyed on the same thread as they are created or GLPK will
66 // crash. It seems OK to call IncrementalSolver::Solve() from another thread
67 // than the one used to create the Solver but it is not documented by GLPK and
68 // should be avoided. Of course these limitations do not apply to the Solve()
69 // function that recreates a new GLPK problem in the calling thread and
70 // destroys before returning.
71 //
72 // When solving a LP with the presolver, a solution (and the unbound rays) are
73 // only returned if an optimal solution has been found. Else nothing is
74 // returned. See glpk-5.0/doc/glpk.pdf page #40 available from glpk-5.0.tar.gz
75 // for details.
76 kGlpk = SOLVER_TYPE_GLPK,
77
78};
79
80MATH_OPT_DEFINE_ENUM(SolverType, SOLVER_TYPE_UNSPECIFIED);
81
82// Parses a flag of type SolverType.
83//
84// The expected values are the one returned by EnumToString().
85bool AbslParseFlag(absl::string_view text, SolverType* value,
86 std::string* error);
87
88// Unparses a flag of type SolverType.
89//
90// The returned values are the same as EnumToString().
92
93// Selects an algorithm for solving linear programs.
94enum class LPAlgorithm {
95 // The (primal) simplex method. Typically can provide primal and dual
96 // solutions, primal/dual rays on primal/dual unbounded problems, and a basis.
97 kPrimalSimplex = LP_ALGORITHM_PRIMAL_SIMPLEX,
98
99 // The dual simplex method. Typically can provide primal and dual
100 // solutions, primal/dual rays on primal/dual unbounded problems, and a basis.
101 kDualSimplex = LP_ALGORITHM_DUAL_SIMPLEX,
102
103 // The barrier method, also commonly called an interior point method (IPM).
104 // Can typically give both primal and dual solutions. Some implementations can
105 // also produce rays on unbounded/infeasible problems. A basis is not given
106 // unless the underlying solver does "crossover" and finishes with simplex.
107 kBarrier = LP_ALGORITHM_BARRIER
108};
109
110MATH_OPT_DEFINE_ENUM(LPAlgorithm, LP_ALGORITHM_UNSPECIFIED);
111
112// Effort level applied to an optional task while solving (see SolveParameters
113// for use).
114//
115// Typically used as a std::optional<Emphasis>. It used to configure a solver
116// feature as follows:
117// * If a solver doesn't support the feature, only nullopt will always be
118// valid, any other setting will give an invalid argument error (some solvers
119// may also accept kOff).
120// * If the solver supports the feature:
121// - When unset, the underlying default is used.
122// - When the feature cannot be turned off, kOff will return an error.
123// - If the feature is enabled by default, the solver default is typically
124// mapped to kMedium.
125// - If the feature is supported, kLow, kMedium, kHigh, and kVeryHigh will
126// never give an error, and will map onto their best match.
127enum class Emphasis {
128 kOff = EMPHASIS_OFF,
129 kLow = EMPHASIS_LOW,
130 kMedium = EMPHASIS_MEDIUM,
131 kHigh = EMPHASIS_HIGH,
132 kVeryHigh = EMPHASIS_VERY_HIGH
133};
134
135MATH_OPT_DEFINE_ENUM(Emphasis, EMPHASIS_UNSPECIFIED);
136
137// Gurobi specific parameters for solving. See
138// https://www.gurobi.com/documentation/9.1/refman/parameters.html
139// for a list of possible parameters.
140//
141// Example use:
142// GurobiParameters gurobi;
143// gurobi.param_values["BarIterLimit"] = "10";
144//
145// With Gurobi, the order that parameters are applied can have an impact in rare
146// situations. Parameters are applied in the following order:
147// * LogToConsole is set from SolveParameters.enable_output.
148// * Any common parameters not overwritten by GurobiParameters.
149// * param_values in iteration order (insertion order).
150// We set LogToConsole first because setting other parameters can generate
151// output.
153 // Parameter name-value pairs to set in insertion order.
155
156 GurobiParametersProto Proto() const;
157 static GurobiParameters FromProto(const GurobiParametersProto& proto);
158
159 bool empty() const { return param_values.empty(); }
160};
161
162// Parameters to control a single solve.
163//
164// Contains both parameters common to all solvers e.g. time_limit, and
165// parameters for a specific solver, e.g. gscip. If a value is set in both
166// common and solver specific field, the solver specific setting is used.
167//
168// The common parameters that are optional and unset indicate that the solver
169// default is used.
170//
171// Solver specific parameters for solvers other than the one in use are ignored.
172//
173// Parameters that depends on the model (e.g. branching priority is set for
174// each variable) are passed in ModelSolveParametersProto.
176 // Enables printing the solver implementation traces. These traces are sent
177 // to the standard output stream.
178 //
179 // Note that if the solver supports message callback and the user registers a
180 // callback for it, then this parameter value is ignored and no traces are
181 // printed.
182 bool enable_output = false;
183
184 // Maximum time a solver should spend on the problem.
185 //
186 // This value is not a hard limit, solve time may slightly exceed this value.
187 // Always passed to the underlying solver, the solver default is not used.
188 absl::Duration time_limit = absl::InfiniteDuration();
189
190 // Limit on the iterations of the underlying algorithm (e.g. simplex pivots).
191 // The specific behavior is dependent on the solver and algorithm used, but
192 // often can give a deterministic solve limit (further configuration may be
193 // needed, e.g. one thread).
194 //
195 // Typically supported by LP, QP, and MIP solvers, but for MIP solvers see
196 // also node_limit.
197 std::optional<int64_t> iteration_limit;
198
199 // Limit on the number of subproblems solved in enumerative search (e.g.
200 // branch and bound). For many solvers this can be used to deterministically
201 // limit computation (further configuration may be needed, e.g. one thread).
202 //
203 // Typically for MIP solvers, see also iteration_limit.
204 std::optional<int64_t> node_limit;
205
206 // The solver stops early if it can prove there are no primal solutions at
207 // least as good as cutoff.
208 //
209 // On an early stop, the solver returns termination reason kNoSolutionFound
210 // and with limit kCutoff and is not required to give any extra solution
211 // information. Has no effect on the return value if there is no early stop.
212 //
213 // It is recommended that you use a tolerance if you want solutions with
214 // objective exactly equal to cutoff to be returned.
215 //
216 // See the user guide for more details and a comparison with best_bound_limit.
217 std::optional<double> cutoff_limit;
218
219 // The solver stops early as soon as it finds a solution at least this good,
220 // with termination reason kFeasible and limit kObjective.
221 std::optional<double> objective_limit;
222
223 // The solver stops early as soon as it proves the best bound is at least this
224 // good, with termination reason kFeasible or kNoSolutionFound and limit
225 // kObjective.
226 //
227 // See the user guide for a comparison with cutoff_limit.
228 std::optional<double> best_bound_limit;
229
230 // The solver stops early after finding this many feasible solutions, with
231 // termination reason kFeasible and limit kSolution. Must be greater than
232 // zero if set. It is often used get the solver to stop on the first feasible
233 // solution found. Note that there is no guarantee on the objective value for
234 // any of the returned solutions.
235 //
236 // Solvers will typically not return more solutions than the solution limit,
237 // but this is not enforced by MathOpt, see also b/214041169.
238 //
239 // Currently supported for Gurobi and SCIP, and for CP-SAT only with value 1.
240 std::optional<int32_t> solution_limit;
241
242 // If unset, use the solver default. If set, it must be >= 1.
243 std::optional<int32_t> threads;
244
245 // Seed for the pseudo-random number generator in the underlying
246 // solver. Note that all solvers use pseudo-random numbers to select things
247 // such as perturbation in the LP algorithm, for tie-break-up rules, and for
248 // heuristic fixings. Varying this can have a noticeable impact on solver
249 // behavior.
250 //
251 // Although all solvers have a concept of seeds, note that valid values
252 // depend on the actual solver.
253 // - Gurobi: [0:GRB_MAXINT] (which as of Gurobi 9.0 is 2x10^9).
254 // - GSCIP: [0:2147483647] (which is MAX_INT or kint32max or 2^31-1).
255 // - GLOP: [0:2147483647] (same as above)
256 // In all cases, the solver will receive a value equal to:
257 // MAX(0, MIN(MAX_VALID_VALUE_FOR_SOLVER, random_seed)).
258 std::optional<int32_t> random_seed;
259
260 // An absolute optimality tolerance (primarily) for MIP solvers.
261 //
262 // The absolute GAP is the absolute value of the difference between:
263 // * the objective value of the best feasible solution found,
264 // * the dual bound produced by the search.
265 // The solver can stop once the absolute GAP is at most absolute_gap_tolerance
266 // (when set), and return TerminationReason::kOptimal.
267 //
268 // Must be >= 0 if set.
269 //
270 // See also relative_gap_tolerance.
271 std::optional<double> absolute_gap_tolerance;
272
273 // A relative optimality tolerance (primarily) for MIP solvers.
274 //
275 // The relative GAP is a normalized version of the absolute GAP (defined on
276 // absolute_gap_tolerance), where the normalization is solver-dependent, e.g.
277 // the absolute GAP divided by the objective value of the best feasible
278 // solution found.
279 //
280 // The solver can stop once the relative GAP is at most relative_gap_tolerance
281 // (when set), and return TerminationReason::kOptimal.
282 //
283 // Must be >= 0 if set.
284 //
285 // See also absolute_gap_tolerance.
286 std::optional<double> relative_gap_tolerance;
287
288 // The algorithm for solving a linear program. If nullopt, use the solver
289 // default algorithm.
290 //
291 // For problems that are not linear programs but where linear programming is
292 // a subroutine, solvers may use this value. E.g. MIP solvers will typically
293 // use this for the root LP solve only (and use dual simplex otherwise).
294 std::optional<LPAlgorithm> lp_algorithm;
295
296 // Effort on simplifying the problem before starting the main algorithm, or
297 // the solver default effort level if unset.
298 std::optional<Emphasis> presolve;
299
300 // Effort on getting a stronger LP relaxation (MIP only) or the solver default
301 // effort level if unset.
302 //
303 // NOTE: disabling cuts may prevent callbacks from having a chance to add cuts
304 // at MIP_NODE, this behavior is solver specific.
305 std::optional<Emphasis> cuts;
306
307 // Effort in finding feasible solutions beyond those encountered in the
308 // complete search procedure (MIP only), or the solver default effort level if
309 // unset.
310 std::optional<Emphasis> heuristics;
311
312 // Effort in rescaling the problem to improve numerical stability, or the
313 // solver default effort level if unset.
314 std::optional<Emphasis> scaling;
315
316 GScipParameters gscip;
318 glop::GlopParameters glop;
319 sat::SatParameters cp_sat;
320
321 SolveParametersProto Proto() const;
322 static absl::StatusOr<SolveParameters> FromProto(
323 const SolveParametersProto& proto);
324};
325
326} // namespace math_opt
327} // namespace operations_research
328
329#endif // OR_TOOLS_MATH_OPT_CPP_PARAMETERS_H_
CpModelProto proto
int64_t value
MATH_OPT_DEFINE_ENUM(CallbackEvent, CALLBACK_EVENT_UNSPECIFIED)
bool AbslParseFlag(const absl::string_view text, SolverType *const value, std::string *const error)
Definition: parameters.cc:61
std::string AbslUnparseFlag(const SolverType value)
Definition: parameters.cc:73
Collection of objects used to extend the Constraint Solver library.
static GurobiParameters FromProto(const GurobiParametersProto &proto)
Definition: parameters.cc:135
gtl::linked_hash_map< std::string, std::string > param_values
Definition: parameters.h:154
std::optional< double > absolute_gap_tolerance
Definition: parameters.h:271
std::optional< double > relative_gap_tolerance
Definition: parameters.h:286
std::optional< LPAlgorithm > lp_algorithm
Definition: parameters.h:294
static absl::StatusOr< SolveParameters > FromProto(const SolveParametersProto &proto)
Definition: parameters.cc:193