OR-Tools  9.3
solve_result.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_SOLVE_RESULT_H_
15#define OR_TOOLS_MATH_OPT_CPP_SOLVE_RESULT_H_
16
17#include <optional>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "absl/time/time.h"
23#include "absl/types/span.h"
25#include "ortools/gscip/gscip.pb.h"
27#include "ortools/math_opt/cpp/enums.h" // IWYU pragma: export
29#include "ortools/math_opt/cpp/solution.h" // IWYU pragma: export
31#include "ortools/math_opt/result.pb.h" // IWYU pragma: export
32
33namespace operations_research {
34namespace math_opt {
35
36// Problem feasibility status as claimed by the solver (solver is not required
37// to return a certificate for the claim).
39 // Solver does not claim a status.
40 kUndetermined = FEASIBILITY_STATUS_UNDETERMINED,
41
42 // Solver claims the problem is feasible.
43 kFeasible = FEASIBILITY_STATUS_FEASIBLE,
44
45 // Solver claims the problem is infeasible.
46 kInfeasible = FEASIBILITY_STATUS_INFEASIBLE,
47};
48
49MATH_OPT_DEFINE_ENUM(FeasibilityStatus, FEASIBILITY_STATUS_UNSPECIFIED);
50
51// Feasibility status of the primal problem and its dual (or the dual of a
52// continuous relaxation) as claimed by the solver. The solver is not required
53// to return a certificate for the claim (e.g. the solver may claim primal
54// feasibility without returning a primal feasible solutuion). This combined
55// status gives a comprehensive description of a solver's claims about
56// feasibility and unboundedness of the solved problem. For instance,
57// * a feasible status for primal and dual problems indicates the primal is
58// feasible and bounded and likely has an optimal solution (guaranteed for
59// problems without non-linear constraints).
60// * a primal feasible and a dual infeasible status indicates the primal
61// problem is unbounded (i.e. has arbitrarily good solutions).
62// Note that a dual infeasible status by itself (i.e. accompanied by an
63// undetermined primal status) does not imply the primal problem is unbounded as
64// we could have both problems be infeasible. Also, while a primal and dual
65// feasible status may imply the existence of an optimal solution, it does not
66// guarantee the solver has actually found such optimal solution.
68 // Status for the primal problem.
70
71 // Status for the dual problem (or for the dual of a continuous relaxation).
73
74 // If true, the solver claims the primal or dual problem is infeasible, but
75 // it does not know which (or if both are infeasible). Can be true only when
76 // primal_problem_status = dual_problem_status = kUndetermined. This extra
77 // information is often needed when preprocessing determines there is no
78 // optimal solution to the problem (but can't determine if it is due to
79 // infeasibility, unboundedness, or both).
81
83 const ProblemStatusProto& problem_status_proto);
84
85 ProblemStatusProto ToProto() const;
86 std::string ToString() const;
87};
88
89std::ostream& operator<<(std::ostream& ostr, const ProblemStatus& status);
90
91struct SolveStats {
92 // Elapsed wall clock time as measured by math_opt, roughly the time inside
93 // Solver::Solve(). Note: this does not include work done building the model.
94 absl::Duration solve_time = absl::ZeroDuration();
95
96 // TODO(b/195295177): Update to add clearer contracts once PDLP's bounds
97 // contract is clarified.
98
99 // Solver claims the optimal value is equal or better (smaller for
100 // minimization and larger for maximization) than best_primal_bound:
101 // * best_primal_bound is trivial (+inf for minimization and -inf
102 // maximization) when the solver does not claim to have such bound. This
103 // may happen for some solvers (e.g., PDLP, typically continuous solvers)
104 // even when returning optimal (solver could terminate with slightly
105 // infeasible primal solutions).
106 // * best_primal_bound can be closer to the optimal value than the objective
107 // of the best primal feasible solution. In particular, best_primal_bound
108 // may be non-trivial even when no primal feasible solutions are returned.
109 // * best_dual_bound is always better (smaller for minimization and larger
110 // for maximization) than best_primal_bound.
111
112 double best_primal_bound = 0.0;
113
114 // Solver claims the optimal value is equal or worse (larger for
115 // minimization and smaller for maximization) than best_dual_bound:
116 // * best_dual_bound is always better (smaller for minimization and larger
117 // for maximization) than best_primal_bound.
118 // * best_dual_bound is trivial (-inf for minimization and +inf
119 // maximization) when the solver does not claim to have such bound.
120 // Similarly to best_primal_bound, this may happen for some solvers even
121 // when returning optimal. MIP solvers will typically report a bound even
122 // if it is imprecise.
123 // * for continuous problems best_dual_bound can be closer to the optimal
124 // value than the objective of the best dual feasible solution. For MIP
125 // one of the first non-trivial values for best_dual_bound is often the
126 // optimal value of the LP relaxation of the MIP.
127 double best_dual_bound = 0.0;
128
129 // Feasibility statuses for primal and dual problems.
131
133
135
137
138 int node_count = 0;
139
140 // Will CHECK fail on invalid input, if problem_status is invalid.
141 static SolveStats FromProto(const SolveStatsProto& solve_stats_proto);
142
143 SolveStatsProto ToProto() const;
144 std::string ToString() const;
145};
146
147std::ostream& operator<<(std::ostream& ostr, const SolveStats& stats);
148
149// The reason a call to Solve() terminates.
151 // A provably optimal solution (up to numerical tolerances) has been found.
152 kOptimal = TERMINATION_REASON_OPTIMAL,
153
154 // The primal problem has no feasible solutions.
155 kInfeasible = TERMINATION_REASON_INFEASIBLE,
156
157 // The primal problem is feasible and arbitrarily good solutions can be
158 // found along a primal ray.
159 kUnbounded = TERMINATION_REASON_UNBOUNDED,
160
161 // The primal problem is either infeasible or unbounded. More details on the
162 // problem status may be available in solve_stats.problem_status. Note that
163 // Gurobi's unbounded status may be mapped here as explained in
164 // go/mathopt-solver-specific#gurobi-inf-or-unb.
165 kInfeasibleOrUnbounded = TERMINATION_REASON_INFEASIBLE_OR_UNBOUNDED,
166
167 // The problem was solved to one of the criteria above (Optimal, Infeasible,
168 // Unbounded, or InfeasibleOrUnbounded), but one or more tolerances was not
169 // met. Some primal/dual solutions/rays be present, but either they will be
170 // slightly infeasible, or (if the problem was nearly optimal) their may be
171 // a gap between the best solution objective and best objective bound.
172 //
173 // Users can still query primal/dual solutions/rays and solution stats, but
174 // they are responsible for dealing with the numerical imprecision.
175 kImprecise = TERMINATION_REASON_IMPRECISE,
176
177 // The optimizer reached some kind of limit and a primal feasible solution
178 // is returned. See SolveResultProto.limit_detail for detailed description of
179 // the kind of limit that was reached.
180 kFeasible = TERMINATION_REASON_FEASIBLE,
181
182 // The optimizer reached some kind of limit and it did not find a primal
183 // feasible solution. See SolveResultProto.limit_detail for detailed
184 // description of the kind of limit that was reached.
185 kNoSolutionFound = TERMINATION_REASON_NO_SOLUTION_FOUND,
186
187 // The algorithm stopped because it encountered unrecoverable numerical
188 // error. No solution information is available.
189 kNumericalError = TERMINATION_REASON_NUMERICAL_ERROR,
190
191 // The algorithm stopped because of an error not covered by one of the
192 // statuses defined above. No solution information is available.
193 kOtherError = TERMINATION_REASON_OTHER_ERROR
194};
195
196MATH_OPT_DEFINE_ENUM(TerminationReason, TERMINATION_REASON_UNSPECIFIED);
197
198// When a Solve() stops early with TerminationReason kFeasible or
199// kNoSolutionFound, the specific limit that was hit.
200enum class Limit {
201 // Used if the underlying solver cannot determine which limit was reached, or
202 // as a null value when we terminated not from a limit (e.g. kOptimal).
203 kUndetermined = LIMIT_UNDETERMINED,
204
205 // An iterative algorithm stopped after conducting the maximum number of
206 // iterations (e.g. simplex or barrier iterations).
207 kIteration = LIMIT_ITERATION,
208
209 // The algorithm stopped after a user-specified computation time.
210 kTime = LIMIT_TIME,
211
212 // A branch-and-bound algorithm stopped because it explored a maximum number
213 // of nodes in the branch-and-bound tree.
214 kNode = LIMIT_NODE,
215
216 // The algorithm stopped because it found the required number of solutions.
217 // This is often used in MIPs to get the solver to return the first feasible
218 // solution it encounters.
219 kSolution = LIMIT_SOLUTION,
220
221 // The algorithm stopped because it ran out of memory.
222 kMemory = LIMIT_MEMORY,
223
224 // The solver was run with a cutoff (e.g. SolveParameters.cutoff_limit was
225 // set) on the objective, indicating that the user did not want any solution
226 // worse than the cutoff, and the solver concluded there were no solutions at
227 // least as good as the cutoff. Typically no further solution information is
228 // provided.
229 kCutoff = LIMIT_CUTOFF,
230
231 // The algorithm stopped because it found a solution better than a minimum
232 // limit set by the user.
233 kObjective = LIMIT_OBJECTIVE,
234
235 // The algorithm stopped because the norm of an iterate became too large.
236 kNorm = LIMIT_NORM,
237
238 // The algorithm stopped because of an interrupt signal or a user interrupt
239 // request.
240 kInterrupted = LIMIT_INTERRUPTED,
241
242 // The algorithm stopped because it was unable to continue making progress
243 // towards the solution.
244 kSlowProgress = LIMIT_SLOW_PROGRESS,
245
246 // The algorithm stopped due to a limit not covered by one of the above. Note
247 // that kUndetermined is used when the reason cannot be determined, and kOther
248 // is used when the reason is known but does not fit into any of the above
249 // alternatives.
250 kOther = LIMIT_OTHER
251};
252
253MATH_OPT_DEFINE_ENUM(Limit, LIMIT_UNSPECIFIED);
254
255// All information regarding why a call to Solve() terminated.
257 // When the reason is kFeasible or kNoSolutionFound, please use the static
258 // functions Feasible and NoSolutionFound.
259 explicit Termination(TerminationReason reason, std::string detail = {});
260
262
263 // Is set iff reason is kFeasible or kNoSolutionFound.
264 std::optional<Limit> limit;
265
266 // Additional typically solver specific information about termination.
267 // Not all solvers can always determine the limit which caused termination,
268 // Limit::kUndetermined is used when the cause cannot be determined.
269 std::string detail;
270
271 // Returns true if a limit was reached (i.e. if reason is kFeasible or
272 // kNoSolutionFound, and limit is not empty).
273 bool limit_reached() const;
274
275 // Will CHECK fail on invalid input, if reason is unspecified, if limit is
276 // set when reason is not TERMINATION_REASON_FEASIBLE or
277 // TERMINATION_REASON_NO_SOLUTION_FOUND, or if limit is unspecified when
278 // reason is TERMINATION_REASON_FEASIBLE or
279 // TERMINATION_REASON_NO_SOLUTION_FOUND (see solution_validator.h).
280 static Termination FromProto(const TerminationProto& termination_proto);
281
282 // Sets the reason to kFeasible
283 static Termination Feasible(Limit limit, std::string detail = {});
284
285 // Sets the reason to kNoSolutionFound
286 static Termination NoSolutionFound(Limit limit, std::string detail = {});
287
288 TerminationProto ToProto() const;
289 std::string ToString() const;
290};
291
292std::ostream& operator<<(std::ostream& ostr, const Termination& termination);
293
294// The result of solving an optimization problem with Solve().
297 : termination(std::move(termination)) {}
298
299 // The reason the solver stopped.
301
302 // Statistics on the solve process, e.g. running time, iterations.
304
305 // Basic solutions use, as of Nov 2021:
306 // * All convex optimization solvers (LP, convex QP) return only one
307 // solution as a primal dual pair.
308 // * Only MI(Q)P solvers return more than one solution. MIP solvers do not
309 // return any dual information, or primal infeasible solutions. Solutions
310 // are returned in order of best primal objective first. Gurobi solves
311 // nonconvex QP (integer or continuous) as MIQP.
312
313 // The general contract for the order of solutions that future solvers should
314 // implement is to order by:
315 // 1. The solutions with a primal feasible solution, ordered by best primal
316 // objective first.
317 // 2. The solutions with a dual feasible solution, ordered by best dual
318 // objective (unknown dual objective is worst)
319 // 3. All remaining solutions can be returned in any order.
320 std::vector<Solution> solutions;
321
322 // Directions of unbounded primal improvement, or equivalently, dual
323 // infeasibility certificates. Typically provided for TerminationReasons
324 // kUnbounded and kInfeasibleOrUnbounded.
325 std::vector<PrimalRay> primal_rays;
326
327 // Directions of unbounded dual improvement, or equivalently, primal
328 // infeasibility certificates. Typically provided for TerminationReason
329 // kInfeasible.
330 std::vector<DualRay> dual_rays;
331
332 // Solver specific output from Gscip. Only populated if Gscip is used.
334
336 const SolveResultProto& solve_result_proto);
337
338 absl::Duration solve_time() const { return solve_stats.solve_time; }
339
340 // Indicates if at least one primal feasible solution is available.
341 //
342 // When termination.reason is TerminationReason::kOptimal, this is guaranteed
343 // to be true and need not be checked.
344 bool has_primal_feasible_solution() const;
345
346 // The objective value of the best primal feasible solution. Will CHECK fail
347 // if there are no primal feasible solutions.
348 double objective_value() const;
349
350 // A bound on the best possible objective value.
351 double best_objective_bound() const;
352
353 // The variable values from the best primal feasible solution. Will CHECK fail
354 // if there are no primal feasible solutions.
356
357 // Returns true only if the problem has been shown to be feasible and bounded.
358 bool bounded() const;
359
360 // Indicates if at least one primal ray is available.
361 //
362 // This is NOT guaranteed to be true when termination.reason is
363 // TerminationReason::kUnbounded or TerminationReason::kInfeasibleOrUnbounded.
364 bool has_ray() const { return !primal_rays.empty(); }
365
366 // The variable values from the first primal ray. Will CHECK fail if there
367 // are no primal rays.
369
370 // Indicates if the best primal solution has an associated dual feasible
371 // solution.
372 //
373 // This is NOT guaranteed to be true when termination.reason is
374 // TerminationReason::kOptimal. It also may be true even when the best primal
375 // solution is not feasible.
376 bool has_dual_feasible_solution() const;
377
378 // The dual values from the best dual solution. Will CHECK fail if there
379 // are no dual solutions.
381
382 // The reduced from the best dual solution. Will CHECK fail if there
383 // are no dual solutions.
384 const VariableMap<double>& reduced_costs() const;
385
386 // Indicates if at least one dual ray is available.
387 //
388 // This is NOT guaranteed to be true when termination.reason is
389 // TerminationReason::kInfeasible.
390 bool has_dual_ray() const { return !dual_rays.empty(); }
391
392 // The dual values from the first dual ray. Will CHECK fail if there
393 // are no dual rays.
395
396 // The reduced from the first dual ray. Will CHECK fail if there
397 // are no dual rays.
399
400 // Indicates if at least one basis is available.
401 bool has_basis() const;
402
403 // The constraint basis status for the first primal/dual pair.
405
406 // The variable basis status for the first primal/dual pair.
408};
409
410} // namespace math_opt
411} // namespace operations_research
412
413#endif // OR_TOOLS_MATH_OPT_CPP_SOLVE_RESULT_H_
absl::Status status
Definition: g_gurobi.cc:35
GRBmodel * model
std::ostream & operator<<(std::ostream &out, const E value)
Definition: enums.h:231
MATH_OPT_DEFINE_ENUM(CallbackEvent, CALLBACK_EVENT_UNSPECIFIED)
Collection of objects used to extend the Constraint Solver library.
STL namespace.
static ProblemStatus FromProto(const ProblemStatusProto &problem_status_proto)
const VariableMap< double > & ray_variable_values() const
const LinearConstraintMap< double > & dual_values() const
const VariableMap< BasisStatus > & variable_status() const
const LinearConstraintMap< double > & ray_dual_values() const
static SolveResult FromProto(const ModelStorage *model, const SolveResultProto &solve_result_proto)
const VariableMap< double > & ray_reduced_costs() const
const LinearConstraintMap< BasisStatus > & constraint_status() const
const VariableMap< double > & variable_values() const
const VariableMap< double > & reduced_costs() const
static SolveStats FromProto(const SolveStatsProto &solve_stats_proto)
Termination(TerminationReason reason, std::string detail={})
static Termination FromProto(const TerminationProto &termination_proto)
static Termination Feasible(Limit limit, std::string detail={})
static Termination NoSolutionFound(Limit limit, std::string detail={})