Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
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_PARAMETERS_H_
18#define OR_TOOLS_MATH_OPT_CPP_PARAMETERS_H_
19
20#include <cstdint>
21#include <optional>
22#include <string>
23
24#include "absl/status/statusor.h"
25#include "absl/strings/string_view.h"
26#include "absl/time/time.h"
27#include "absl/types/span.h"
29#include "ortools/glop/parameters.pb.h" // IWYU pragma: export
30#include "ortools/gscip/gscip.pb.h" // IWYU pragma: export
31#include "ortools/math_opt/cpp/enums.h" // IWYU pragma: export
32#include "ortools/math_opt/parameters.pb.h"
33#include "ortools/math_opt/solvers/gurobi.pb.h" // IWYU pragma: export
34#include "ortools/math_opt/solvers/highs.pb.h" // IWYU pragma: export
35#include "ortools/pdlp/solvers.pb.h" // IWYU pragma: export
36#include "ortools/sat/sat_parameters.pb.h" // IWYU pragma: export
37
38namespace operations_research {
39namespace math_opt {
40
41// The solvers supported by MathOpt.
42enum class SolverType {
43 // Solving Constraint Integer Programs (SCIP) solver (third party).
44 //
45 // Supports LP, MIP, and nonconvex integer quadratic problems. No dual data
46 // for LPs is returned though. Prefer GLOP for LPs.
47 kGscip = SOLVER_TYPE_GSCIP,
48
49 // Gurobi solver (third party).
50 //
51 // Supports LP, MIP, and nonconvex integer quadratic problems. Generally the
52 // fastest option, but has special licensing.
53 kGurobi = SOLVER_TYPE_GUROBI,
54
55 // Google's Glop solver.
56 //
57 // Supports LP with primal and dual simplex methods.
58 kGlop = SOLVER_TYPE_GLOP,
59
60 // Google's CP-SAT solver.
61 //
62 // Supports problems where all variables are integer and bounded (or implied
63 // to be after presolve). Experimental support to rescale and discretize
64 // problems with continuous variables.
65 kCpSat = SOLVER_TYPE_CP_SAT,
66
67 // Google's PDLP solver.
68 //
69 // Supports LP and convex diagonal quadratic objectives. Uses first order
70 // methods rather than simplex. Can solve very large problems.
71 kPdlp = SOLVER_TYPE_PDLP,
72
73 // GNU Linear Programming Kit (GLPK) (third party).
74 //
75 // Supports MIP and LP.
76 //
77 // Thread-safety: GLPK use thread-local storage for memory allocations. As a
78 // consequence when using IncrementalSolver, the user must make sure that
79 // instances are destroyed on the same thread as they are created or GLPK will
80 // crash. It seems OK to call IncrementalSolver::Solve() from another thread
81 // than the one used to create the Solver but it is not documented by GLPK and
82 // should be avoided. Of course these limitations do not apply to the Solve()
83 // function that recreates a new GLPK problem in the calling thread and
84 // destroys before returning.
85 //
86 // When solving a LP with the presolver, a solution (and the unbound rays) are
87 // only returned if an optimal solution has been found. Else nothing is
88 // returned. See glpk-5.0/doc/glpk.pdf page #40 available from glpk-5.0.tar.gz
89 // for details.
90 kGlpk = SOLVER_TYPE_GLPK,
91
92 // The Embedded Conic Solver (ECOS).
93 //
94 // Supports LP and SOCP problems. Uses interior point methods (barrier).
95 kEcos = SOLVER_TYPE_ECOS,
96
97 // The Splitting Conic Solver (SCS) (third party).
98 //
99 // Supports LP and SOCP problems. Uses a first-order method.
100 kScs = SOLVER_TYPE_SCS,
101
102 // The HiGHS Solver (third party).
103 //
104 // Supports LP and MIP problems (convex QPs are unimplemented).
105 kHighs = SOLVER_TYPE_HIGHS,
106
107 // MathOpt's reference implementation of a MIP solver.
108 //
109 // Slow/not recommended for production. Not an LP solver (no dual information
110 // returned).
111 kSantorini = SOLVER_TYPE_SANTORINI,
112};
113
114MATH_OPT_DEFINE_ENUM(SolverType, SOLVER_TYPE_UNSPECIFIED);
115
116// Parses a flag of type SolverType.
117//
118// The expected values are the one returned by EnumToString().
119bool AbslParseFlag(absl::string_view text, SolverType* value,
120 std::string* error);
121
122// Unparses a flag of type SolverType.
123//
124// The returned values are the same as EnumToString().
125std::string AbslUnparseFlag(SolverType value);
126
127// Selects an algorithm for solving linear programs.
128enum class LPAlgorithm {
129 // The (primal) simplex method. Typically can provide primal and dual
130 // solutions, primal/dual rays on primal/dual unbounded problems, and a basis.
131 kPrimalSimplex = LP_ALGORITHM_PRIMAL_SIMPLEX,
132
133 // The dual simplex method. Typically can provide primal and dual
134 // solutions, primal/dual rays on primal/dual unbounded problems, and a basis.
135 kDualSimplex = LP_ALGORITHM_DUAL_SIMPLEX,
136
137 // The barrier method, also commonly called an interior point method (IPM).
138 // Can typically give both primal and dual solutions. Some implementations can
139 // also produce rays on unbounded/infeasible problems. A basis is not given
140 // unless the underlying solver does "crossover" and finishes with simplex.
141 kBarrier = LP_ALGORITHM_BARRIER,
142
143 // An algorithm based around a first-order method. These will typically
144 // produce both primal and dual solutions, and potentially also certificates
145 // of primal and/or dual infeasibility. First-order methods typically will
146 // provide solutions with lower accuracy, so users should take care to set
147 // solution quality parameters (e.g., tolerances) and to validate solutions.
148 kFirstOrder = LP_ALGORITHM_FIRST_ORDER,
149};
150
151MATH_OPT_DEFINE_ENUM(LPAlgorithm, LP_ALGORITHM_UNSPECIFIED);
152
153// Parses a flag of type LPAlgorithm.
154//
155// The expected values are the one returned by EnumToString().
156bool AbslParseFlag(absl::string_view text, LPAlgorithm* value,
157 std::string* error);
158
159// Unparses a flag of type LPAlgorithm.
160//
161// The returned values are the same as EnumToString().
163
164// Effort level applied to an optional task while solving (see SolveParameters
165// for use).
166//
167// Typically used as a std::optional<Emphasis>. It's used to configure a solver
168// feature as follows:
169// * If a solver doesn't support the feature, only nullopt will always be
170// valid, any other setting will give an invalid argument error (some solvers
171// may also accept kOff).
172// * If the solver supports the feature:
173// - When unset, the underlying default is used.
174// - When the feature cannot be turned off, kOff will return an error.
175// - If the feature is enabled by default, the solver default is typically
176// mapped to kMedium.
177// - If the feature is supported, kLow, kMedium, kHigh, and kVeryHigh will
178// never give an error, and will map onto their best match.
179enum class Emphasis {
180 kOff = EMPHASIS_OFF,
181 kLow = EMPHASIS_LOW,
182 kMedium = EMPHASIS_MEDIUM,
183 kHigh = EMPHASIS_HIGH,
184 kVeryHigh = EMPHASIS_VERY_HIGH
185};
186
187MATH_OPT_DEFINE_ENUM(Emphasis, EMPHASIS_UNSPECIFIED);
188
189// Parses a flag of type Emphasis.
190//
191// The expected values are the one returned by EnumToString().
192bool AbslParseFlag(absl::string_view text, Emphasis* value, std::string* error);
193
194// Unparses a flag of type Emphasis.
195//
196// The returned values are the same as EnumToString().
197std::string AbslUnparseFlag(Emphasis value);
198
199// Gurobi specific parameters for solving. See
200// https://www.gurobi.com/documentation/9.1/refman/parameters.html
201// for a list of possible parameters.
202//
203// Example use:
204// GurobiParameters gurobi;
205// gurobi.param_values["BarIterLimit"] = "10";
206//
207// With Gurobi, the order that parameters are applied can have an impact in rare
208// situations. Parameters are applied in the following order:
209// * LogToConsole is set from SolveParameters.enable_output.
210// * Any common parameters not overwritten by GurobiParameters.
211// * param_values in iteration order (insertion order).
212// We set LogToConsole first because setting other parameters can generate
213// output.
215 // Parameter name-value pairs to set in insertion order.
217
218 GurobiParametersProto Proto() const;
219 static GurobiParameters FromProto(const GurobiParametersProto& proto);
220
221 bool empty() const { return param_values.empty(); }
222};
223
224// GLPK specific parameters for solving.
225//
226// Fields are optional to enable capturing user intention; if the user
227// explicitly sets a value, then no generic solve parameters will overwrite this
228// parameter. User specified solver specific parameters have priority over
229// generic parameters.
231 // Compute the primal or dual unbound ray when the variable (structural or
232 // auxiliary) causing the unboundness is identified (see glp_get_unbnd_ray()).
233 //
234 // The unset value is equivalent to false.
235 //
236 // Rays are only available when solving linear programs, they are not
237 // available for MIPs. On top of that they are only available when using a
238 // simplex algorithm with the presolve disabled.
239 //
240 // A primal ray can only be built if the chosen LP algorithm is
241 // LPAlgorithm::kPrimalSimplex. Same for a dual ray and
242 // LPAlgorithm::kDualSimplex.
243 //
244 // The computation involves the basis factorization to be available which may
245 // lead to extra computations/errors.
246 std::optional<bool> compute_unbound_rays_if_possible = std::nullopt;
247
248 GlpkParametersProto Proto() const;
249 static GlpkParameters FromProto(const GlpkParametersProto& proto);
250};
251
252// Parameters to control a single solve.
253//
254// Contains both parameters common to all solvers, e.g. time_limit, and
255// parameters for a specific solver, e.g. gscip. If a value is set in both
256// common and solver specific fields, the solver specific setting is used.
257//
258// The common parameters that are optional and unset indicate that the solver
259// default is used.
260//
261// Solver specific parameters for solvers other than the one in use are ignored.
262//
263// Parameters that depends on the model (e.g. branching priority is set for
264// each variable) are passed in ModelSolveParametersProto.
266 // Enables printing the solver implementation traces. These traces are sent
267 // to the standard output stream.
268 //
269 // Note that if the user registers a message callback, then this parameter
270 // value is ignored and no traces are printed.
271 bool enable_output = false;
272
273 // Maximum time a solver should spend on the problem.
274 //
275 // This value is not a hard limit, solve time may slightly exceed this value.
276 // Always passed to the underlying solver, the solver default is not used.
277 absl::Duration time_limit = absl::InfiniteDuration();
278
279 // Limit on the iterations of the underlying algorithm (e.g. simplex pivots).
280 // The specific behavior is dependent on the solver and algorithm used, but
281 // often can give a deterministic solve limit (further configuration may be
282 // needed, e.g. one thread).
283 //
284 // Typically supported by LP, QP, and MIP solvers, but for MIP solvers see
285 // also node_limit.
286 std::optional<int64_t> iteration_limit;
287
288 // Limit on the number of subproblems solved in enumerative search (e.g.
289 // branch and bound). For many solvers this can be used to deterministically
290 // limit computation (further configuration may be needed, e.g. one thread).
291 //
292 // Typically for MIP solvers, see also iteration_limit.
293 std::optional<int64_t> node_limit;
294
295 // The solver stops early if it can prove there are no primal solutions at
296 // least as good as cutoff.
297 //
298 // On an early stop, the solver returns termination reason kNoSolutionFound
299 // and with limit kCutoff and is not required to give any extra solution
300 // information. Has no effect on the return value if there is no early stop.
301 //
302 // It is recommended that you use a tolerance if you want solutions with
303 // objective exactly equal to cutoff to be returned.
304 //
305 // See the user guide for more details and a comparison with best_bound_limit.
306 std::optional<double> cutoff_limit;
307
308 // The solver stops early as soon as it finds a solution at least this good,
309 // with termination reason kFeasible and limit kObjective.
310 std::optional<double> objective_limit;
311
312 // The solver stops early as soon as it proves the best bound is at least this
313 // good, with termination reason kFeasible or kNoSolutionFound and limit
314 // kObjective.
315 //
316 // See the user guide for a comparison with cutoff_limit.
317 std::optional<double> best_bound_limit;
318
319 // The solver stops early after finding this many feasible solutions, with
320 // termination reason kFeasible and limit kSolution. Must be greater than
321 // zero if set. It is often used to get the solver to stop on the first
322 // feasible solution found. Note that there is no guarantee on the objective
323 // value for any of the returned solutions.
324 //
325 // Solvers will typically not return more solutions than the solution limit,
326 // but this is not enforced by MathOpt, see also b/214041169.
327 //
328 // Currently supported for Gurobi and SCIP, and for CP-SAT only with value 1.
329 std::optional<int32_t> solution_limit;
330
331 // If unset, use the solver default. If set, it must be >= 1.
332 std::optional<int32_t> threads;
333
334 // Seed for the pseudo-random number generator in the underlying
335 // solver. Note that all solvers use pseudo-random numbers to select things
336 // such as perturbation in the LP algorithm, for tie-break-up rules, and for
337 // heuristic fixings. Varying this can have a noticeable impact on solver
338 // behavior.
339 //
340 // Although all solvers have a concept of seeds, note that valid values
341 // depend on the actual solver.
342 // - Gurobi: [0:GRB_MAXINT] (which as of Gurobi 9.0 is 2x10^9).
343 // - GSCIP: [0:2147483647] (which is MAX_INT or kint32max or 2^31-1).
344 // - GLOP: [0:2147483647] (same as above)
345 // In all cases, the solver will receive a value equal to:
346 // MAX(0, MIN(MAX_VALID_VALUE_FOR_SOLVER, random_seed)).
347 std::optional<int32_t> random_seed;
348
349 // An absolute optimality tolerance (primarily) for MIP solvers.
350 //
351 // The absolute GAP is the absolute value of the difference between:
352 // * the objective value of the best feasible solution found,
353 // * the dual bound produced by the search.
354 // The solver can stop once the absolute GAP is at most absolute_gap_tolerance
355 // (when set), and return TerminationReason::kOptimal.
356 //
357 // Must be >= 0 if set.
358 //
359 // See also relative_gap_tolerance.
360 std::optional<double> absolute_gap_tolerance;
361
362 // A relative optimality tolerance (primarily) for MIP solvers.
363 //
364 // The relative GAP is a normalized version of the absolute GAP (defined on
365 // absolute_gap_tolerance), where the normalization is solver-dependent, e.g.
366 // the absolute GAP divided by the objective value of the best feasible
367 // solution found.
368 //
369 // The solver can stop once the relative GAP is at most relative_gap_tolerance
370 // (when set), and return TerminationReason::kOptimal.
371 //
372 // Must be >= 0 if set.
373 //
374 // See also absolute_gap_tolerance.
375 std::optional<double> relative_gap_tolerance;
376
377 // Maintain up to `solution_pool_size` solutions while searching. The solution
378 // pool generally has two functions:
379 // (1) For solvers that can return more than one solution, this limits how
380 // many solutions will be returned.
381 // (2) Some solvers may run heuristics using solutions from the solution
382 // pool, so changing this value may affect the algorithm's path.
383 // To force the solver to fill the solution pool, e.g. with the n best
384 // solutions, requires further, solver specific configuration.
385 std::optional<int32_t> solution_pool_size;
386
387 // The algorithm for solving a linear program. If nullopt, use the solver
388 // default algorithm.
389 //
390 // For problems that are not linear programs but where linear programming is
391 // a subroutine, solvers may use this value. E.g. MIP solvers will typically
392 // use this for the root LP solve only (and use dual simplex otherwise).
393 std::optional<LPAlgorithm> lp_algorithm;
394
395 // Effort on simplifying the problem before starting the main algorithm, or
396 // the solver default effort level if unset.
397 std::optional<Emphasis> presolve;
398
399 // Effort on getting a stronger LP relaxation (MIP only) or the solver default
400 // effort level if unset.
401 //
402 // NOTE: disabling cuts may prevent callbacks from having a chance to add cuts
403 // at MIP_NODE, this behavior is solver specific.
404 std::optional<Emphasis> cuts;
405
406 // Effort in finding feasible solutions beyond those encountered in the
407 // complete search procedure (MIP only), or the solver default effort level if
408 // unset.
409 std::optional<Emphasis> heuristics;
410
411 // Effort in rescaling the problem to improve numerical stability, or the
412 // solver default effort level if unset.
413 std::optional<Emphasis> scaling;
414
415 GScipParameters gscip;
417 glop::GlopParameters glop;
418 sat::SatParameters cp_sat;
419 pdlp::PrimalDualHybridGradientParams pdlp;
420
422 HighsOptionsProto highs;
423
424 SolveParametersProto Proto() const;
425 static absl::StatusOr<SolveParameters> FromProto(
426 const SolveParametersProto& proto);
427};
428
429bool AbslParseFlag(absl::string_view text, SolveParameters* solve_parameters,
430 std::string* error);
431
432std::string AbslUnparseFlag(SolveParameters solve_parameters);
433
434} // namespace math_opt
435} // namespace operations_research
436
437#endif // OR_TOOLS_MATH_OPT_CPP_PARAMETERS_H_
CpModelProto proto
The output proto.
int64_t value
#define MATH_OPT_DEFINE_ENUM(CppEnum, proto_unspecified_value)
Definition enums.h:325
When the underlying default is used When the feature cannot be turned kOff will return an error If the feature is enabled by the solver default is typically If the feature is kLow
Definition parameters.h:177
bool AbslParseFlag(const absl::string_view text, SolverType *const value, std::string *const error)
SolverType
The solvers supported by MathOpt.
Definition parameters.h:42
Emphasis
never give an error, and will map onto their best match.
Definition parameters.h:179
LPAlgorithm
Selects an algorithm for solving linear programs.
Definition parameters.h:128
When the underlying default is used When the feature cannot be turned kOff will return an error If the feature is enabled by the solver default is typically If the feature is kMedium
Definition parameters.h:177
std::string AbslUnparseFlag(const SolverType value)
When the underlying default is used When the feature cannot be turned kOff will return an error If the feature is enabled by the solver default is typically If the feature is kHigh
Definition parameters.h:177
In SWIG mode, we don't want anything besides these top-level includes.
std::optional< bool > compute_unbound_rays_if_possible
Definition parameters.h:246
static GlpkParameters FromProto(const GlpkParametersProto &proto)
gtl::linked_hash_map< std::string, std::string > param_values
Parameter name-value pairs to set in insertion order.
Definition parameters.h:216
static GurobiParameters FromProto(const GurobiParametersProto &proto)
std::optional< LPAlgorithm > lp_algorithm
Definition parameters.h:393
pdlp::PrimalDualHybridGradientParams pdlp
Definition parameters.h:419
static absl::StatusOr< SolveParameters > FromProto(const SolveParametersProto &proto)
std::optional< int32_t > threads
If unset, use the solver default. If set, it must be >= 1.
Definition parameters.h:332