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