OR-Tools  9.6
termination.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 #ifndef PDLP_TERMINATION_H_
15 #define PDLP_TERMINATION_H_
16 
17 #include <atomic>
18 #include <optional>
19 
20 #include "ortools/pdlp/solve_log.pb.h"
21 #include "ortools/pdlp/solvers.pb.h"
22 
23 namespace operations_research::pdlp {
24 
27  PointType type;
28 };
29 
30 // Information about the quadratic program's primal and dual bounds that are
31 // needed to evaluate relative convergence criteria.
37 };
38 
39 // Computes the effective optimality criteria for a `TerminationCriteria`.
40 TerminationCriteria::DetailedOptimalityCriteria EffectiveOptimalityCriteria(
41  const TerminationCriteria& termination_criteria);
42 
43 // Like the previous overload but takes a `SimpleOptimalityCriteria`. Useful in
44 // unit tests where no `TerminationCriteria` is naturally available.
45 TerminationCriteria::DetailedOptimalityCriteria EffectiveOptimalityCriteria(
46  const TerminationCriteria::SimpleOptimalityCriteria& simple_criteria);
47 
48 // Checks if any of the simple termination criteria are satisfied by `stats`,
49 // and returns a termination reason if so, and nullopt otherwise. The "simple"
50 // termination criteria are `time_sec_limit`, `iteration_limit`,
51 // `kkt_matrix_pass_limit`, and `interrupt_solve`. The corresponding fields of
52 // `stats` (`cumulative_time_sec`, `iteration_number`,
53 // `cumulative_kkt_matrix_passes`) are the only ones accessed. If returning a
54 // termination reason, the `PointType` will be set to `POINT_TYPE_NONE`.
55 std::optional<TerminationReasonAndPointType> CheckSimpleTerminationCriteria(
56  const TerminationCriteria& criteria, const IterationStats& stats,
57  const std::atomic<bool>* interrupt_solve = nullptr);
58 
59 // Checks if any iterate-based termination criteria (i.e., the criteria not
60 // checked by `CheckSimpleTerimationCriteria()`) are satisfied by the solution
61 // state described by `stats` (see definitions of termination criteria in
62 // solvers.proto). `bound_norms` provides the instance-dependent data required
63 // for the relative convergence criteria. Returns a termination reason and a
64 // point type if so (if multiple criteria are satisfied, the optimality and
65 // infeasibility conditions are checked first). If `force_numerical_termination`
66 // is true, returns `TERMINATION_REASON_NUMERICAL_ERROR` if no other criteria
67 // are satisfied. The return value is empty in any other case. If the output is
68 // not empty, the `PointType` indicates which entry prompted termination. If no
69 // entry prompted termination, e.g. `TERMINATION_REASON_NUMERICAL_ERROR` is
70 // returned, then the `PointType` is set to `POINT_TYPE_NONE`. NOTE: This
71 // function assumes that the solution used to compute the stats satisfies the
72 // primal and dual variable bounds; see
73 // https://developers.google.com/optimization/lp/pdlp_math#dual_variable_bounds.
74 std::optional<TerminationReasonAndPointType> CheckIterateTerminationCriteria(
75  const TerminationCriteria& criteria, const IterationStats& stats,
76  const QuadraticProgramBoundNorms& bound_norms,
77  bool force_numerical_termination = false);
78 
79 // Extracts the norms needed for the termination criteria from the full problem
80 // `stats`.
82  const QuadraticProgramStats& stats);
83 
84 // Returns `epsilon_absolute / epsilon_relative`, returning 1.0 if
85 // `epsilon_absolute` and `epsilon_relative` are equal (even if they are both
86 // 0.0 or infinity, which would normally yield NAN).
87 double EpsilonRatio(double epsilon_absolute, double epsilon_relative);
88 
89 // Metrics for tracking progress when relative convergence criteria are used.
90 // These depend on the `ConvergenceInformation`, the problem data, and the
91 // convergence tolerances.
93  // Relative versions of the residuals, defined as
94  // relative_residual = residual / (eps_ratio + norm),
95  // where
96  // eps_ratio = `eps_optimal_absolute / eps_optimal_relative`
97  // residual = one of the residuals (l{2,_inf}_{primal,dual}_residual)
98  // norm = the relative norm (l{2,_inf} norm of
99  // {constraint_bounds,primal_linear_objective} respectively).
100  // If `eps_optimal_relative == eps_optimal_absolute`, eps_ratio will be 1.0
101  // (even if `eps_optimal_relative` == 0.0 or inf). Otherwise, if
102  // `eps_optimal_relative == 0.0`, these will all be 0.0.
103  //
104  // If `eps_optimal_relative > 0.0`, the absolute and relative termination
105  // criteria translate to relative_residual <= `eps_optimal_relative`.
110 
111  // Relative optimality gap:
112  // (primal_objective - dual_objective) /
113  // (eps_ratio + |primal_objective| + |dual_objective|)
115 };
116 
118  const TerminationCriteria::DetailedOptimalityCriteria& optimality_criteria,
119  const ConvergenceInformation& stats,
120  const QuadraticProgramBoundNorms& bound_norms);
121 
122 bool ObjectiveGapMet(
123  const TerminationCriteria::DetailedOptimalityCriteria& optimality_criteria,
124  const ConvergenceInformation& stats);
125 
126 // Determines if the optimality criteria are met.
128  const TerminationCriteria::DetailedOptimalityCriteria& optimality_criteria,
129  const ConvergenceInformation& stats, OptimalityNorm optimality_norm,
130  const QuadraticProgramBoundNorms& bound_norms);
131 
132 } // namespace operations_research::pdlp
133 
134 #endif // PDLP_TERMINATION_H_
double EpsilonRatio(const double epsilon_absolute, const double epsilon_relative)
Definition: termination.cc:231
bool OptimalityCriteriaMet(const TerminationCriteria::DetailedOptimalityCriteria &optimality_criteria, const ConvergenceInformation &stats, const OptimalityNorm optimality_norm, const QuadraticProgramBoundNorms &bound_norms)
Definition: termination.cc:44
bool ObjectiveGapMet(const TerminationCriteria::DetailedOptimalityCriteria &optimality_criteria, const ConvergenceInformation &stats)
Definition: termination.cc:27
TerminationCriteria::DetailedOptimalityCriteria EffectiveOptimalityCriteria(const TerminationCriteria &termination_criteria)
Definition: termination.cc:127
std::optional< TerminationReasonAndPointType > CheckIterateTerminationCriteria(const TerminationCriteria &criteria, const IterationStats &stats, const QuadraticProgramBoundNorms &bound_norms, const bool force_numerical_termination)
Definition: termination.cc:187
RelativeConvergenceInformation ComputeRelativeResiduals(const TerminationCriteria::DetailedOptimalityCriteria &optimality_criteria, const ConvergenceInformation &stats, const QuadraticProgramBoundNorms &bound_norms)
Definition: termination.cc:240
std::optional< TerminationReasonAndPointType > CheckSimpleTerminationCriteria(const TerminationCriteria &criteria, const IterationStats &stats, const std::atomic< bool > *interrupt_solve)
Definition: termination.cc:162
QuadraticProgramBoundNorms BoundNormsFromProblemStats(const QuadraticProgramStats &stats)
Definition: termination.cc:222