OR-Tools  9.3
solve_parameters_validator.cc
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
15
16#include <cmath>
17#include <string>
18#include <type_traits>
19
20#include "absl/status/status.h"
21#include "absl/status/statusor.h"
22#include "absl/strings/str_cat.h"
23#include "absl/time/time.h"
26#include "ortools/math_opt/parameters.pb.h"
28
29namespace operations_research {
30namespace math_opt {
31
32absl::Status ValidateSolveParameters(const SolveParametersProto& parameters) {
33 {
35 const absl::Duration time_limit,
37 _ << "invalid SolveParameters.time_limit");
38 if (time_limit < absl::ZeroDuration()) {
40 << "SolveParameters.time_limit = " << time_limit << " < 0";
41 }
42 }
43
44 if (parameters.has_threads()) {
45 if (parameters.threads() <= 0) {
46 return absl::InvalidArgumentError(absl::StrCat(
47 "SolveParameters.threads = ", parameters.threads(), " <= 0"));
48 }
49 }
50
51 if (parameters.has_relative_gap_tolerance()) {
52 if (parameters.relative_gap_tolerance() < 0) {
53 return absl::InvalidArgumentError(
54 absl::StrCat("SolveParameters.relative_gap_tolerance = ",
55 parameters.relative_gap_tolerance(), " < 0"));
56 }
57 }
58
59 if (parameters.has_absolute_gap_tolerance()) {
60 if (parameters.absolute_gap_tolerance() < 0) {
61 return absl::InvalidArgumentError(
62 absl::StrCat("SolveParameters.absolute_gap_tolerance = ",
63 parameters.absolute_gap_tolerance(), " < 0"));
64 }
65 }
66 if (parameters.has_node_limit() && parameters.node_limit() < 0) {
68 << "SolveParameters.node_limit = " << parameters.node_limit()
69 << " should be nonnegative.";
70 }
71
72 if (parameters.has_solution_limit() && parameters.solution_limit() <= 0) {
74 << "SolveParameters.solution_limit = " << parameters.solution_limit()
75 << " should be positive.";
76 }
77
78 if (std::isnan(parameters.cutoff_limit())) {
79 return absl::InvalidArgumentError("SolveParameters.cutoff_limit was NaN");
80 }
81 if (std::isnan(parameters.objective_limit())) {
82 return absl::InvalidArgumentError(
83 "SolveParameters.objective_limit was NaN");
84 }
85 if (std::isnan(parameters.best_bound_limit())) {
86 return absl::InvalidArgumentError(
87 "SolveParameters.best_bound_limit was NaN");
88 }
89
90 return absl::OkStatus();
91}
92
93} // namespace math_opt
94} // namespace operations_research
SatParameters parameters
ModelSharedTimeLimit * time_limit
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 OR_ASSIGN_OR_RETURN3(lhs, rexpr, error_expression)