OR-Tools  9.6
solve_parameters_validator.cc
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 
15 
16 #include <cmath>
17 #include <string>
18 
19 #include "absl/status/status.h"
20 #include "absl/status/statusor.h"
21 #include "absl/strings/str_cat.h"
22 #include "absl/strings/string_view.h"
23 #include "absl/time/time.h"
24 #include "ortools/base/protoutil.h"
26 #include "ortools/math_opt/parameters.pb.h"
28 
29 namespace operations_research {
30 namespace math_opt {
31 namespace {
32 
33 // Returns an error if the input value is not one of the possible values of the
34 // enum. The parameter_name is the name of the SolveParametersProto field
35 // holding the value.
36 absl::Status ValidateEmphasisProtoParameter(
37  const EmphasisProto value, const absl::string_view field_name) {
38  if (!EmphasisProto_IsValid(value)) {
40  << "Unknown enum value for SolverParameters." << field_name << " = "
41  << value;
42  }
43  return absl::OkStatus();
44 }
45 
46 } // namespace
47 
48 absl::Status ValidateSolveParameters(const SolveParametersProto& parameters) {
49  {
51  const absl::Duration time_limit,
53  _ << "invalid SolveParameters.time_limit");
54  if (time_limit < absl::ZeroDuration()) {
56  << "SolveParameters.time_limit = " << time_limit << " < 0";
57  }
58  }
59 
60  if (parameters.has_threads()) {
61  if (parameters.threads() <= 0) {
62  return absl::InvalidArgumentError(absl::StrCat(
63  "SolveParameters.threads = ", parameters.threads(), " <= 0"));
64  }
65  }
66 
67  if (parameters.has_relative_gap_tolerance()) {
68  if (parameters.relative_gap_tolerance() < 0) {
69  return absl::InvalidArgumentError(
70  absl::StrCat("SolveParameters.relative_gap_tolerance = ",
71  parameters.relative_gap_tolerance(), " < 0"));
72  }
73  }
74 
75  if (parameters.has_absolute_gap_tolerance()) {
76  if (parameters.absolute_gap_tolerance() < 0) {
77  return absl::InvalidArgumentError(
78  absl::StrCat("SolveParameters.absolute_gap_tolerance = ",
79  parameters.absolute_gap_tolerance(), " < 0"));
80  }
81  }
82  if (parameters.has_node_limit() && parameters.node_limit() < 0) {
84  << "SolveParameters.node_limit = " << parameters.node_limit()
85  << " should be nonnegative.";
86  }
87 
88  if (parameters.has_solution_limit() && parameters.solution_limit() <= 0) {
90  << "SolveParameters.solution_limit = " << parameters.solution_limit()
91  << " should be positive.";
92  }
93 
94  if (!std::isfinite(parameters.cutoff_limit())) {
96  << "SolveParameters.cutoff_limit should be finite (and not NaN) but "
97  "was: "
98  << parameters.cutoff_limit();
99  }
100  if (std::isnan(parameters.objective_limit())) {
101  return absl::InvalidArgumentError(
102  "SolveParameters.objective_limit was NaN");
103  }
104  if (std::isnan(parameters.best_bound_limit())) {
105  return absl::InvalidArgumentError(
106  "SolveParameters.best_bound_limit was NaN");
107  }
108  if (parameters.has_solution_pool_size() &&
109  parameters.solution_pool_size() <= 0) {
111  << "SolveParameters.solution_pool_size must be positive if set, but "
112  "was set to: "
113  << parameters.solution_pool_size();
114  }
115  if (!LPAlgorithmProto_IsValid(parameters.lp_algorithm())) {
117  << "Unknown enum value for SolverParameters.lp_algorithm = "
118  << parameters.lp_algorithm();
119  }
120 #define VALIDATE_EMPHASIS(property) \
121  RETURN_IF_ERROR( \
122  ValidateEmphasisProtoParameter(parameters.property(), #property))
123  VALIDATE_EMPHASIS(presolve);
124  VALIDATE_EMPHASIS(cuts);
125  VALIDATE_EMPHASIS(heuristics);
126  VALIDATE_EMPHASIS(scaling);
127 #undef VALIDATE_EMPHASIS
128  return absl::OkStatus();
129 }
130 
131 } // namespace math_opt
132 } // namespace operations_research
SatParameters parameters
ModelSharedTimeLimit * time_limit
int64_t value
absl::Status ValidateSolveParameters(const SolveParametersProto &parameters)
Collection of objects used to extend the Constraint Solver library.
inline ::absl::StatusOr< absl::Duration > DecodeGoogleApiProto(const google::protobuf::Duration &proto)
Definition: protoutil.h:42
StatusBuilder InvalidArgumentErrorBuilder()
#define VALIDATE_EMPHASIS(property)
#define OR_ASSIGN_OR_RETURN3(lhs, rexpr, error_expression)