forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvers_proto_validation.cc
222 lines (211 loc) · 8.84 KB
/
solvers_proto_validation.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2010-2022 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/pdlp/solvers_proto_validation.h"
#include <cmath>
#include "absl/status/status.h"
#include "ortools/base/status_macros.h"
#include "ortools/pdlp/solvers.pb.h"
namespace operations_research::pdlp {
using ::absl::InvalidArgumentError;
using ::absl::OkStatus;
absl::Status ValidateTerminationCriteria(const TerminationCriteria& criteria) {
if (criteria.optimality_norm() != OPTIMALITY_NORM_L_INF &&
criteria.optimality_norm() != OPTIMALITY_NORM_L2) {
return InvalidArgumentError("invalid value for optimality_norm");
}
if (std::isnan(criteria.eps_optimal_absolute())) {
return InvalidArgumentError("eps_optimal_absolute is NAN");
}
if (criteria.eps_optimal_absolute() < 0) {
return InvalidArgumentError("eps_optimal_absolute must be non-negative");
}
if (std::isnan(criteria.eps_optimal_relative())) {
return InvalidArgumentError("eps_optimal_relative is NAN");
}
if (criteria.eps_optimal_relative() < 0) {
return InvalidArgumentError("eps_optimal_relative must be non-negative");
}
if (std::isnan(criteria.eps_primal_infeasible())) {
return InvalidArgumentError("eps_primal_infeasible is NAN");
}
if (criteria.eps_primal_infeasible() < 0) {
return InvalidArgumentError("eps_primal_infeasible must be non-negative");
}
if (std::isnan(criteria.eps_dual_infeasible())) {
return InvalidArgumentError("eps_dual_infeasible is NAN");
}
if (criteria.eps_dual_infeasible() < 0) {
return InvalidArgumentError("eps_dual_infeasible must be non-negative");
}
if (std::isnan(criteria.time_sec_limit())) {
return InvalidArgumentError("time_sec_limit is NAN");
}
if (criteria.time_sec_limit() < 0) {
return InvalidArgumentError("time_sec_limit must be non-negative");
}
if (criteria.iteration_limit() < 0) {
return InvalidArgumentError("iteration_limit must be non-negative");
}
if (std::isnan(criteria.kkt_matrix_pass_limit())) {
return InvalidArgumentError("kkt_matrix_pass_limit is NAN");
}
if (criteria.kkt_matrix_pass_limit() < 0) {
return InvalidArgumentError("kkt_matrix_pass_limit must be non-negative");
}
return OkStatus();
}
absl::Status ValidateAdaptiveLinesearchParams(
const AdaptiveLinesearchParams& params) {
if (std::isnan(params.step_size_reduction_exponent())) {
return InvalidArgumentError("step_size_reduction_exponent is NAN");
}
if (params.step_size_reduction_exponent() <= 0) {
return InvalidArgumentError(
"step_size_reduction_exponent must be positive");
}
if (std::isnan(params.step_size_growth_exponent())) {
return InvalidArgumentError("step_size_growth_exponent is NAN");
}
if (params.step_size_growth_exponent() <= 0) {
return InvalidArgumentError("step_size_growth_exponent must be positive");
}
return OkStatus();
}
absl::Status ValidateMalitskyPockParams(const MalitskyPockParams& params) {
if (std::isnan(params.step_size_downscaling_factor())) {
return InvalidArgumentError("step_size_downscaling_factor is NAN");
}
if (params.step_size_downscaling_factor() <= 0 ||
params.step_size_downscaling_factor() >= 1) {
return InvalidArgumentError(
"step_size_downscaling_factor must be between 0 and 1 exclusive");
}
if (std::isnan(params.linesearch_contraction_factor())) {
return InvalidArgumentError("linesearch_contraction_factor is NAN");
}
if (params.linesearch_contraction_factor() <= 0 ||
params.linesearch_contraction_factor() >= 1) {
return InvalidArgumentError(
"linesearch_contraction_factor must be between 0 and 1 exclusive");
}
if (std::isnan(params.step_size_interpolation())) {
return InvalidArgumentError("step_size_interpolation is NAN");
}
if (params.step_size_interpolation() < 0) {
return InvalidArgumentError("step_size_interpolation must be non-negative");
}
return OkStatus();
}
absl::Status ValidatePrimalDualHybridGradientParams(
const PrimalDualHybridGradientParams& params) {
RETURN_IF_ERROR(ValidateTerminationCriteria(params.termination_criteria()))
<< "termination_criteria invalid";
if (params.num_threads() <= 0) {
return InvalidArgumentError("num_threads must be positive");
}
if (params.verbosity_level() < 0) {
return InvalidArgumentError("verbosity_level must be non-negative");
}
if (params.major_iteration_frequency() <= 0) {
return InvalidArgumentError("major_iteration_frequency must be positive");
}
if (params.termination_check_frequency() <= 0) {
return InvalidArgumentError("termination_check_frequency must be positive");
}
if (params.restart_strategy() !=
PrimalDualHybridGradientParams::NO_RESTARTS &&
params.restart_strategy() !=
PrimalDualHybridGradientParams::EVERY_MAJOR_ITERATION &&
params.restart_strategy() !=
PrimalDualHybridGradientParams::ADAPTIVE_HEURISTIC &&
params.restart_strategy() !=
PrimalDualHybridGradientParams::ADAPTIVE_DISTANCE_BASED) {
return InvalidArgumentError("invalid restart_strategy");
}
if (std::isnan(params.primal_weight_update_smoothing())) {
return InvalidArgumentError("primal_weight_update_smoothing is NAN");
}
if (params.primal_weight_update_smoothing() < 0 ||
params.primal_weight_update_smoothing() > 1) {
return InvalidArgumentError(
"primal_weight_update_smoothing must be between 0 and 1 inclusive");
}
if (std::isnan(params.initial_primal_weight())) {
return InvalidArgumentError("initial_primal_weight is NAN");
}
if (params.has_initial_primal_weight() &&
params.initial_primal_weight() <= 0) {
return InvalidArgumentError(
"initial_primal_weight must be positive if specified");
}
if (params.l_inf_ruiz_iterations() < 0) {
return InvalidArgumentError("l_inf_ruiz_iterations must be non-negative");
}
if (params.l_inf_ruiz_iterations() > 100) {
return InvalidArgumentError("l_inf_ruiz_iterations must be at most 100");
}
if (std::isnan(params.sufficient_reduction_for_restart())) {
return InvalidArgumentError("sufficient_reduction_for_restart is NAN");
}
if (params.sufficient_reduction_for_restart() <= 0 ||
params.sufficient_reduction_for_restart() >= 1) {
return InvalidArgumentError(
"sufficient_reduction_for_restart must be between 0 and 1 exclusive");
}
if (std::isnan(params.necessary_reduction_for_restart())) {
return InvalidArgumentError("necessary_reduction_for_restart is NAN");
}
if (params.necessary_reduction_for_restart() <
params.sufficient_reduction_for_restart() ||
params.necessary_reduction_for_restart() >= 1) {
return InvalidArgumentError(
"necessary_reduction_for_restart must be in the interval "
"[sufficient_reduction_for_restart, 1)");
}
if (params.linesearch_rule() !=
PrimalDualHybridGradientParams::ADAPTIVE_LINESEARCH_RULE &&
params.linesearch_rule() !=
PrimalDualHybridGradientParams::MALITSKY_POCK_LINESEARCH_RULE &&
params.linesearch_rule() !=
PrimalDualHybridGradientParams::CONSTANT_STEP_SIZE_RULE) {
return InvalidArgumentError("invalid linesearch_rule");
}
RETURN_IF_ERROR(
ValidateAdaptiveLinesearchParams(params.adaptive_linesearch_parameters()))
<< "adaptive_linesearch_parameters invalid";
RETURN_IF_ERROR(ValidateMalitskyPockParams(params.malitsky_pock_parameters()))
<< "malitsky_pock_parameters invalid";
if (std::isnan(params.initial_step_size_scaling())) {
return InvalidArgumentError("initial_step_size_scaling is NAN");
}
if (params.initial_step_size_scaling() <= 0.0) {
return InvalidArgumentError("initial_step_size_scaling must be positive");
}
if (std::isnan(params.infinite_constraint_bound_threshold())) {
return InvalidArgumentError("infinite_constraint_bound_threshold is NAN");
}
if (params.infinite_constraint_bound_threshold() < 0.0) {
return InvalidArgumentError(
"infinite_constraint_bound_threshold must be non-negative");
}
if (std::isnan(params.diagonal_qp_trust_region_solver_tolerance())) {
return InvalidArgumentError(
"diagonal_qp_trust_region_solver_tolerance is NAN");
}
if (params.diagonal_qp_trust_region_solver_tolerance() < 0.0) {
return InvalidArgumentError(
"diagonal_qp_trust_region_solver_tolerance must be non-negative");
}
return OkStatus();
}
} // namespace operations_research::pdlp