Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parameters_validation.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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/strings/str_cat.h"
20#include "ortools/glop/parameters.pb.h"
21
23
24#define TEST_FINITE_AND_NON_NEGATIVE(name) \
25 if (!std::isfinite(params.name())) { \
26 return absl::StrCat("parameter '", #name, "' is NaN or not finite"); \
27 } \
28 if (params.name() < 0) { \
29 return absl::StrCat("Parameters '", #name, "' must be non-negative"); \
30 }
31
32// We need an integer version of the test as std::isnan can fail to compile
33// on windows platforms when passed integer values.
34#define TEST_INTEGER_NON_NEGATIVE(name) \
35 if (params.name() < 0) { \
36 return absl::StrCat("Parameters '", #name, "' must be non-negative"); \
37 }
38
39#define TEST_NON_NEGATIVE(name) \
40 if (std::isnan(params.name())) { \
41 return absl::StrCat("parameter '", #name, "' is NaN"); \
42 } \
43 if (params.name() < 0) { \
44 return absl::StrCat("Parameters '", #name, "' must be non-negative"); \
45 }
46
47#define TEST_NOT_NAN(name) \
48 if (std::isnan(params.name())) { \
49 return absl::StrCat("parameter '", #name, "' is NaN"); \
50 }
51
52std::string ValidateParameters(const GlopParameters& params) {
53 TEST_FINITE_AND_NON_NEGATIVE(degenerate_ministep_factor);
54 TEST_FINITE_AND_NON_NEGATIVE(drop_tolerance);
55 TEST_FINITE_AND_NON_NEGATIVE(dual_feasibility_tolerance);
56 TEST_FINITE_AND_NON_NEGATIVE(dual_small_pivot_threshold);
57 TEST_FINITE_AND_NON_NEGATIVE(dualizer_threshold);
58 TEST_FINITE_AND_NON_NEGATIVE(harris_tolerance_ratio);
59 TEST_FINITE_AND_NON_NEGATIVE(lu_factorization_pivot_threshold);
60 TEST_FINITE_AND_NON_NEGATIVE(markowitz_singularity_threshold);
61 TEST_FINITE_AND_NON_NEGATIVE(max_number_of_reoptimizations);
62 TEST_FINITE_AND_NON_NEGATIVE(minimum_acceptable_pivot);
63 TEST_FINITE_AND_NON_NEGATIVE(preprocessor_zero_tolerance);
64 TEST_FINITE_AND_NON_NEGATIVE(primal_feasibility_tolerance);
65 TEST_FINITE_AND_NON_NEGATIVE(ratio_test_zero_threshold);
66 TEST_FINITE_AND_NON_NEGATIVE(recompute_edges_norm_threshold);
67 TEST_FINITE_AND_NON_NEGATIVE(recompute_reduced_costs_threshold);
68 TEST_FINITE_AND_NON_NEGATIVE(refactorization_threshold);
69 TEST_FINITE_AND_NON_NEGATIVE(relative_cost_perturbation);
70 TEST_FINITE_AND_NON_NEGATIVE(relative_max_cost_perturbation);
71 TEST_FINITE_AND_NON_NEGATIVE(small_pivot_threshold);
72 TEST_FINITE_AND_NON_NEGATIVE(solution_feasibility_tolerance);
73
74 TEST_NOT_NAN(objective_lower_limit);
75 TEST_NOT_NAN(objective_upper_limit);
76
77 TEST_NON_NEGATIVE(crossover_bound_snapping_distance);
78 TEST_NON_NEGATIVE(initial_condition_number_threshold);
79 TEST_NON_NEGATIVE(max_deterministic_time);
80 TEST_NON_NEGATIVE(max_time_in_seconds);
81
82 TEST_FINITE_AND_NON_NEGATIVE(max_valid_magnitude);
83 if (params.max_valid_magnitude() > 1e100) {
84 return "max_valid_magnitude must be <= 1e100";
85 }
86
87 TEST_FINITE_AND_NON_NEGATIVE(drop_magnitude);
88 if (params.drop_magnitude() < 1e-100) {
89 return "drop magnitude must be finite and >= 1e-100";
90 }
91
92 TEST_INTEGER_NON_NEGATIVE(basis_refactorization_period);
93 TEST_INTEGER_NON_NEGATIVE(devex_weights_reset_period);
94 TEST_INTEGER_NON_NEGATIVE(num_omp_threads);
95 TEST_INTEGER_NON_NEGATIVE(random_seed);
96
97 if (params.markowitz_zlatev_parameter() < 1) {
98 return "markowitz_zlatev_parameter must be >= 1";
99 }
100
101 return "";
102}
103
104#undef TEST_NOT_NAN
105#undef TEST_INTEGER_NON_NEGATIVE
106#undef TEST_NON_NEGATIVE
107#undef TEST_FINITE_AND_NON_NEGATIVE
108
109} // namespace operations_research::glop
#define TEST_FINITE_AND_NON_NEGATIVE(name)
#define TEST_INTEGER_NON_NEGATIVE(name)
#define TEST_NON_NEGATIVE(name)
#define TEST_NOT_NAN(name)
std::string ValidateParameters(const GlopParameters &params)