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