OR-Tools  9.3
parameters.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 <cstdint>
17#include <optional>
18#include <string>
19#include <type_traits>
20
21#include "absl/status/status.h"
22#include "absl/status/statusor.h"
23#include "absl/strings/string_view.h"
24#include "absl/time/time.h"
25#include "absl/types/span.h"
30#include "ortools/math_opt/parameters.pb.h"
31#include "ortools/math_opt/solvers/gurobi.pb.h"
32
33namespace operations_research {
34namespace math_opt {
35
36std::optional<absl::string_view> Enum<SolverType>::ToOptString(
38 switch (value) {
40 return "gscip";
42 return "gurobi";
44 return "glop";
46 return "cp_sat";
48 return "glpk";
49 }
50 return std::nullopt;
51}
52
53absl::Span<const SolverType> Enum<SolverType>::AllValues() {
54 static constexpr SolverType kSolverTypeValues[] = {
57 };
58 return absl::MakeConstSpan(kSolverTypeValues);
59}
60
61bool AbslParseFlag(const absl::string_view text, SolverType* const value,
62 std::string* const error) {
63 const std::optional enum_value = EnumFromString<SolverType>(text);
64 if (!enum_value.has_value()) {
65 *error = "unknown value for enumeration";
66 return false;
67 }
68
69 *value = *enum_value;
70 return true;
71}
72
73std::string AbslUnparseFlag(const SolverType value) {
74 std::ostringstream oss;
75 oss << value;
76 return oss.str();
77}
78
79std::optional<absl::string_view> Enum<LPAlgorithm>::ToOptString(
81 switch (value) {
83 return "primal_simplex";
85 return "dual_simplex";
87 return "barrier";
88 }
89 return std::nullopt;
90}
91
92absl::Span<const LPAlgorithm> Enum<LPAlgorithm>::AllValues() {
93 static constexpr LPAlgorithm kLPAlgorithmValues[] = {
97 };
98 return absl::MakeConstSpan(kLPAlgorithmValues);
99}
100
101std::optional<absl::string_view> Enum<Emphasis>::ToOptString(Emphasis value) {
102 switch (value) {
103 case Emphasis::kOff:
104 return "off";
105 case Emphasis::kLow:
106 return "low";
108 return "medium";
109 case Emphasis::kHigh:
110 return "high";
112 return "very_high";
113 }
114 return std::nullopt;
115}
116
117absl::Span<const Emphasis> Enum<Emphasis>::AllValues() {
118 static constexpr Emphasis kEmphasisValues[] = {
121 };
122 return absl::MakeConstSpan(kEmphasisValues);
123}
124
125GurobiParametersProto GurobiParameters::Proto() const {
126 GurobiParametersProto result;
127 for (const auto& [key, val] : param_values) {
128 GurobiParametersProto::Parameter& p = *result.add_parameters();
129 p.set_name(key);
130 p.set_value(val);
131 }
132 return result;
133}
134
136 const GurobiParametersProto& proto) {
137 GurobiParameters result;
138 for (const GurobiParametersProto::Parameter& p : proto.parameters()) {
139 result.param_values[p.name()] = p.value();
140 }
141 return result;
142}
143
144SolveParametersProto SolveParameters::Proto() const {
145 SolveParametersProto result;
146 result.set_enable_output(enable_output);
147 if (time_limit < absl::InfiniteDuration()) {
149 result.mutable_time_limit()));
150 }
151 if (iteration_limit.has_value()) {
152 result.set_iteration_limit(*iteration_limit);
153 }
154 if (node_limit.has_value()) {
155 result.set_node_limit(*node_limit);
156 }
157 if (cutoff_limit.has_value()) {
158 result.set_cutoff_limit(*cutoff_limit);
159 }
160 if (objective_limit.has_value()) {
161 result.set_objective_limit(*objective_limit);
162 }
163 if (best_bound_limit.has_value()) {
164 result.set_best_bound_limit(*best_bound_limit);
165 }
166 if (solution_limit.has_value()) {
167 result.set_solution_limit(*solution_limit);
168 }
169 if (threads.has_value()) {
170 result.set_threads(*threads);
171 }
172 if (random_seed.has_value()) {
173 result.set_random_seed(*random_seed);
174 }
175 if (relative_gap_tolerance.has_value()) {
176 result.set_relative_gap_tolerance(*relative_gap_tolerance);
177 }
178 if (absolute_gap_tolerance.has_value()) {
179 result.set_absolute_gap_tolerance(*absolute_gap_tolerance);
180 }
181 result.set_lp_algorithm(EnumToProto(lp_algorithm));
182 result.set_presolve(EnumToProto(presolve));
183 result.set_cuts(EnumToProto(cuts));
184 result.set_heuristics(EnumToProto(heuristics));
185 result.set_scaling(EnumToProto(scaling));
186 *result.mutable_gscip() = gscip;
187 *result.mutable_gurobi() = gurobi.Proto();
188 *result.mutable_glop() = glop;
189 *result.mutable_cp_sat() = cp_sat;
190 return result;
191}
192
193absl::StatusOr<SolveParameters> SolveParameters::FromProto(
194 const SolveParametersProto& proto) {
195 SolveParameters result;
196 result.enable_output = proto.enable_output();
197 if (proto.has_time_limit()) {
200 } else {
201 result.time_limit = absl::InfiniteDuration();
202 }
203 if (proto.has_iteration_limit()) {
204 result.iteration_limit = proto.iteration_limit();
205 }
206 if (proto.has_node_limit()) {
207 result.node_limit = proto.node_limit();
208 }
209 if (proto.has_cutoff_limit()) {
210 result.cutoff_limit = proto.cutoff_limit();
211 }
212 if (proto.has_objective_limit()) {
213 result.objective_limit = proto.objective_limit();
214 }
215 if (proto.has_best_bound_limit()) {
216 result.best_bound_limit = proto.best_bound_limit();
217 }
218 if (proto.has_solution_limit()) {
219 result.solution_limit = proto.solution_limit();
220 }
221 if (proto.has_threads()) {
222 result.threads = proto.threads();
223 }
224 if (proto.has_random_seed()) {
225 result.random_seed = proto.random_seed();
226 }
227 if (proto.has_absolute_gap_tolerance()) {
228 result.absolute_gap_tolerance = proto.absolute_gap_tolerance();
229 }
230 if (proto.has_relative_gap_tolerance()) {
231 result.relative_gap_tolerance = proto.relative_gap_tolerance();
232 }
233 result.lp_algorithm = EnumFromProto(proto.lp_algorithm());
234 result.presolve = EnumFromProto(proto.presolve());
235 result.cuts = EnumFromProto(proto.cuts());
236 result.heuristics = EnumFromProto(proto.heuristics());
237 result.scaling = EnumFromProto(proto.scaling());
238 result.gscip = proto.gscip();
239 result.gurobi = GurobiParameters::FromProto(proto.gurobi());
240 result.glop = proto.glop();
241 result.cp_sat = proto.cp_sat();
242 return result;
243}
244
245} // namespace math_opt
246} // namespace operations_research
#define CHECK_OK(x)
Definition: base/logging.h:44
#define ASSIGN_OR_RETURN(lhs, rexpr)
CpModelProto proto
int64_t value
bool AbslParseFlag(const absl::string_view text, SolverType *const value, std::string *const error)
Definition: parameters.cc:61
std::string AbslUnparseFlag(const SolverType value)
Definition: parameters.cc:73
std::optional< typename EnumProto< P >::Cpp > EnumFromProto(const P proto_value)
Definition: enums.h:275
Enum< E >::Proto EnumToProto(const std::optional< E > value)
Definition: enums.h:264
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
inline ::absl::StatusOr< google::protobuf::Duration > EncodeGoogleApiProto(absl::Duration d)
Definition: protoutil.h:27
static std::optional< absl::string_view > ToOptString(E value)
Definition: callback.cc:75
static absl::Span< const E > AllValues()
Definition: callback.cc:94
static GurobiParameters FromProto(const GurobiParametersProto &proto)
Definition: parameters.cc:135
gtl::linked_hash_map< std::string, std::string > param_values
Definition: parameters.h:154
std::optional< double > absolute_gap_tolerance
Definition: parameters.h:271
std::optional< double > relative_gap_tolerance
Definition: parameters.h:286
std::optional< LPAlgorithm > lp_algorithm
Definition: parameters.h:294
static absl::StatusOr< SolveParameters > FromProto(const SolveParametersProto &proto)
Definition: parameters.cc:193