Google OR-Tools v9.12
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 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 // Fico XPRESS solver (third party).
114 //
115 // Supports LP, MIP, and nonconvex integer quadratic problems.
116 // A fast option, but has special licensing.
117 kXpress = SOLVER_TYPE_XPRESS
118};
119
120MATH_OPT_DEFINE_ENUM(SolverType, SOLVER_TYPE_UNSPECIFIED);
121
122// Parses a flag of type SolverType.
123//
124// The expected values are the one returned by EnumToString().
125bool AbslParseFlag(absl::string_view text, SolverType* value,
126 std::string* error);
127
128// Unparses a flag of type SolverType.
129//
130// The returned values are the same as EnumToString().
131std::string AbslUnparseFlag(SolverType value);
132
133// Selects an algorithm for solving linear programs.
134enum class LPAlgorithm {
135 // The (primal) simplex method. Typically can provide primal and dual
136 // solutions, primal/dual rays on primal/dual unbounded problems, and a basis.
137 kPrimalSimplex = LP_ALGORITHM_PRIMAL_SIMPLEX,
138
139 // The dual simplex method. Typically can provide primal and dual
140 // solutions, primal/dual rays on primal/dual unbounded problems, and a basis.
141 kDualSimplex = LP_ALGORITHM_DUAL_SIMPLEX,
142
143 // The barrier method, also commonly called an interior point method (IPM).
144 // Can typically give both primal and dual solutions. Some implementations can
145 // also produce rays on unbounded/infeasible problems. A basis is not given
146 // unless the underlying solver does "crossover" and finishes with simplex.
147 kBarrier = LP_ALGORITHM_BARRIER,
148
149 // An algorithm based around a first-order method. These will typically
150 // produce both primal and dual solutions, and potentially also certificates
151 // of primal and/or dual infeasibility. First-order methods typically will
152 // provide solutions with lower accuracy, so users should take care to set
153 // solution quality parameters (e.g., tolerances) and to validate solutions.
154 kFirstOrder = LP_ALGORITHM_FIRST_ORDER,
155};
156
157MATH_OPT_DEFINE_ENUM(LPAlgorithm, LP_ALGORITHM_UNSPECIFIED);
158
159// Parses a flag of type LPAlgorithm.
160//
161// The expected values are the one returned by EnumToString().
162bool AbslParseFlag(absl::string_view text, LPAlgorithm* value,
163 std::string* error);
164
165// Unparses a flag of type LPAlgorithm.
166//
167// The returned values are the same as EnumToString().
168std::string AbslUnparseFlag(LPAlgorithm value);
169
170// Effort level applied to an optional task while solving (see SolveParameters
171// for use).
172//
173// Typically used as a std::optional<Emphasis>. It's used to configure a solver
174// feature as follows:
175// * If a solver doesn't support the feature, only nullopt will always be
176// valid, any other setting will give an invalid argument error (some solvers
177// may also accept kOff).
178// * If the solver supports the feature:
179// - When unset, the underlying default is used.
180// - When the feature cannot be turned off, kOff will return an error.
181// - If the feature is enabled by default, the solver default is typically
182// mapped to kMedium.
183// - If the feature is supported, kLow, kMedium, kHigh, and kVeryHigh will
184// never give an error, and will map onto their best match.
185enum class Emphasis {
186 kOff = EMPHASIS_OFF,
187 kLow = EMPHASIS_LOW,
188 kMedium = EMPHASIS_MEDIUM,
189 kHigh = EMPHASIS_HIGH,
190 kVeryHigh = EMPHASIS_VERY_HIGH
191};
192
193MATH_OPT_DEFINE_ENUM(Emphasis, EMPHASIS_UNSPECIFIED);
194
195// Parses a flag of type Emphasis.
196//
197// The expected values are the one returned by EnumToString().
198bool AbslParseFlag(absl::string_view text, Emphasis* value, std::string* error);
199
200// Unparses a flag of type Emphasis.
201//
202// The returned values are the same as EnumToString().
203std::string AbslUnparseFlag(Emphasis value);
204
205// Gurobi specific parameters for solving. See
206// https://www.gurobi.com/documentation/9.1/refman/parameters.html
207// for a list of possible parameters.
208//
209// Example use:
210// GurobiParameters gurobi;
211// gurobi.param_values["BarIterLimit"] = "10";
212//
213// With Gurobi, the order that parameters are applied can have an impact in rare
214// situations. Parameters are applied in the following order:
215// * LogToConsole is set from SolveParameters.enable_output.
216// * Any common parameters not overwritten by GurobiParameters.
217// * param_values in iteration order (insertion order).
218// We set LogToConsole first because setting other parameters can generate
219// output.
221 // Parameter name-value pairs to set in insertion order.
223
224 GurobiParametersProto Proto() const;
225 static GurobiParameters FromProto(const GurobiParametersProto& proto);
226
227 bool empty() const { return param_values.empty(); }
228};
229
230// GLPK specific parameters for solving.
231//
232// Fields are optional to enable capturing user intention; if the user
233// explicitly sets a value, then no generic solve parameters will overwrite this
234// parameter. User specified solver specific parameters have priority over
235// generic parameters.
237 // Compute the primal or dual unbound ray when the variable (structural or
238 // auxiliary) causing the unboundness is identified (see glp_get_unbnd_ray()).
239 //
240 // The unset value is equivalent to false.
241 //
242 // Rays are only available when solving linear programs, they are not
243 // available for MIPs. On top of that they are only available when using a
244 // simplex algorithm with the presolve disabled.
245 //
246 // A primal ray can only be built if the chosen LP algorithm is
247 // LPAlgorithm::kPrimalSimplex. Same for a dual ray and
248 // LPAlgorithm::kDualSimplex.
249 //
250 // The computation involves the basis factorization to be available which may
251 // lead to extra computations/errors.
252 std::optional<bool> compute_unbound_rays_if_possible = std::nullopt;
253
254 GlpkParametersProto Proto() const;
255 static GlpkParameters FromProto(const GlpkParametersProto& proto);
256};
257
258// Parameters to control a single solve.
259//
260// Contains both parameters common to all solvers, e.g. time_limit, and
261// parameters for a specific solver, e.g. gscip. If a value is set in both
262// common and solver specific fields, the solver specific setting is used.
263//
264// The common parameters that are optional and unset indicate that the solver
265// default is used.
266//
267// Solver specific parameters for solvers other than the one in use are ignored.
268//
269// Parameters that depends on the model (e.g. branching priority is set for
270// each variable) are passed in ModelSolveParametersProto.
272 // Enables printing the solver implementation traces. These traces are sent
273 // to the standard output stream.
274 //
275 // Note that if the user registers a message callback, then this parameter
276 // value is ignored and no traces are printed.
277 bool enable_output = false;
278
279 // Maximum time a solver should spend on the problem.
280 //
281 // This value is not a hard limit, solve time may slightly exceed this value.
282 // Always passed to the underlying solver, the solver default is not used.
283 absl::Duration time_limit = absl::InfiniteDuration();
284
285 // Limit on the iterations of the underlying algorithm (e.g. simplex pivots).
286 // The specific behavior is dependent on the solver and algorithm used, but
287 // often can give a deterministic solve limit (further configuration may be
288 // needed, e.g. one thread).
289 //
290 // Typically supported by LP, QP, and MIP solvers, but for MIP solvers see
291 // also node_limit.
292 std::optional<int64_t> iteration_limit;
293
294 // Limit on the number of subproblems solved in enumerative search (e.g.
295 // branch and bound). For many solvers this can be used to deterministically
296 // limit computation (further configuration may be needed, e.g. one thread).
297 //
298 // Typically for MIP solvers, see also iteration_limit.
299 std::optional<int64_t> node_limit;
300
301 // The solver stops early if it can prove there are no primal solutions at
302 // least as good as cutoff.
303 //
304 // On an early stop, the solver returns termination reason kNoSolutionFound
305 // and with limit kCutoff and is not required to give any extra solution
306 // information. Has no effect on the return value if there is no early stop.
307 //
308 // It is recommended that you use a tolerance if you want solutions with
309 // objective exactly equal to cutoff to be returned.
310 //
311 // See the user guide for more details and a comparison with best_bound_limit.
312 std::optional<double> cutoff_limit;
313
314 // The solver stops early as soon as it finds a solution at least this good,
315 // with termination reason kFeasible and limit kObjective.
316 std::optional<double> objective_limit;
317
318 // The solver stops early as soon as it proves the best bound is at least this
319 // good, with termination reason kFeasible or kNoSolutionFound and limit
320 // kObjective.
321 //
322 // See the user guide for a comparison with cutoff_limit.
323 std::optional<double> best_bound_limit;
324
325 // The solver stops early after finding this many feasible solutions, with
326 // termination reason kFeasible and limit kSolution. Must be greater than
327 // zero if set. It is often used to get the solver to stop on the first
328 // feasible solution found. Note that there is no guarantee on the objective
329 // value for any of the returned solutions.
330 //
331 // Solvers will typically not return more solutions than the solution limit,
332 // but this is not enforced by MathOpt, see also b/214041169.
333 //
334 // Currently supported for Gurobi and SCIP, and for CP-SAT only with value 1.
335 std::optional<int32_t> solution_limit;
336
337 // If unset, use the solver default. If set, it must be >= 1.
338 std::optional<int32_t> threads;
339
340 // Seed for the pseudo-random number generator in the underlying
341 // solver. Note that all solvers use pseudo-random numbers to select things
342 // such as perturbation in the LP algorithm, for tie-break-up rules, and for
343 // heuristic fixings. Varying this can have a noticeable impact on solver
344 // behavior.
345 //
346 // Although all solvers have a concept of seeds, note that valid values
347 // depend on the actual solver.
348 // - Gurobi: [0:GRB_MAXINT] (which as of Gurobi 9.0 is 2x10^9).
349 // - GSCIP: [0:2147483647] (which is MAX_INT or kint32max or 2^31-1).
350 // - GLOP: [0:2147483647] (same as above)
351 // In all cases, the solver will receive a value equal to:
352 // MAX(0, MIN(MAX_VALID_VALUE_FOR_SOLVER, random_seed)).
353 std::optional<int32_t> random_seed;
354
355 // An absolute optimality tolerance (primarily) for MIP solvers.
356 //
357 // The absolute GAP is the absolute value of the difference between:
358 // * the objective value of the best feasible solution found,
359 // * the dual bound produced by the search.
360 // The solver can stop once the absolute GAP is at most absolute_gap_tolerance
361 // (when set), and return TerminationReason::kOptimal.
362 //
363 // Must be >= 0 if set.
364 //
365 // See also relative_gap_tolerance.
366 std::optional<double> absolute_gap_tolerance;
367
368 // A relative optimality tolerance (primarily) for MIP solvers.
369 //
370 // The relative GAP is a normalized version of the absolute GAP (defined on
371 // absolute_gap_tolerance), where the normalization is solver-dependent, e.g.
372 // the absolute GAP divided by the objective value of the best feasible
373 // solution found.
374 //
375 // The solver can stop once the relative GAP is at most relative_gap_tolerance
376 // (when set), and return TerminationReason::kOptimal.
377 //
378 // Must be >= 0 if set.
379 //
380 // See also absolute_gap_tolerance.
381 std::optional<double> relative_gap_tolerance;
382
383 // Maintain up to `solution_pool_size` solutions while searching. The solution
384 // pool generally has two functions:
385 // (1) For solvers that can return more than one solution, this limits how
386 // many solutions will be returned.
387 // (2) Some solvers may run heuristics using solutions from the solution
388 // pool, so changing this value may affect the algorithm's path.
389 // To force the solver to fill the solution pool, e.g. with the n best
390 // solutions, requires further, solver specific configuration.
391 std::optional<int32_t> solution_pool_size;
392
393 // The algorithm for solving a linear program. If nullopt, use the solver
394 // default algorithm.
395 //
396 // For problems that are not linear programs but where linear programming is
397 // a subroutine, solvers may use this value. E.g. MIP solvers will typically
398 // use this for the root LP solve only (and use dual simplex otherwise).
399 std::optional<LPAlgorithm> lp_algorithm;
400
401 // Effort on simplifying the problem before starting the main algorithm, or
402 // the solver default effort level if unset.
403 std::optional<Emphasis> presolve;
404
405 // Effort on getting a stronger LP relaxation (MIP only) or the solver default
406 // effort level if unset.
407 //
408 // NOTE: disabling cuts may prevent callbacks from having a chance to add cuts
409 // at MIP_NODE, this behavior is solver specific.
410 std::optional<Emphasis> cuts;
411
412 // Effort in finding feasible solutions beyond those encountered in the
413 // complete search procedure (MIP only), or the solver default effort level if
414 // unset.
415 std::optional<Emphasis> heuristics;
416
417 // Effort in rescaling the problem to improve numerical stability, or the
418 // solver default effort level if unset.
419 std::optional<Emphasis> scaling;
420
421 GScipParameters gscip;
423 glop::GlopParameters glop;
424 sat::SatParameters cp_sat;
425 pdlp::PrimalDualHybridGradientParams pdlp;
426
428 HighsOptionsProto highs;
429
430 SolveParametersProto Proto() const;
431 static absl::StatusOr<SolveParameters> FromProto(
432 const SolveParametersProto& proto);
433};
434
435bool AbslParseFlag(absl::string_view text, SolveParameters* solve_parameters,
436 std::string* error);
437
438std::string AbslUnparseFlag(SolveParameters solve_parameters);
439
440} // namespace math_opt
441} // namespace operations_research
442
443#endif // OR_TOOLS_MATH_OPT_CPP_PARAMETERS_H_
#define MATH_OPT_DEFINE_ENUM(CppEnum, proto_unspecified_value)
Definition enums.h:325
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
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:183
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:185
LPAlgorithm
Selects an algorithm for solving linear programs.
Definition parameters.h:134
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:183
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:183
In SWIG mode, we don't want anything besides these top-level includes.
std::optional< bool > compute_unbound_rays_if_possible
Definition parameters.h:252
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:222
static GurobiParameters FromProto(const GurobiParametersProto &proto)
std::optional< LPAlgorithm > lp_algorithm
Definition parameters.h:399
pdlp::PrimalDualHybridGradientParams pdlp
Definition parameters.h:425
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:338