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