Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parameters.cc
Go to the documentation of this file.
1// Copyright 2010-2025 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 <optional>
17#include <sstream>
18#include <string>
19#include <utility>
20
21#include "absl/log/check.h"
22#include "absl/status/status.h"
23#include "absl/status/statusor.h"
24#include "absl/strings/str_cat.h"
25#include "absl/strings/string_view.h"
26#include "absl/time/time.h"
27#include "absl/types/span.h"
38
39namespace operations_research {
40namespace math_opt {
41namespace {
42
43template <typename EnumType>
44bool ParseEnumFlag(const absl::string_view text, EnumType* const value,
45 std::string* const error) {
46 const std::optional<EnumType> enum_value = EnumFromString<EnumType>(text);
47 if (!enum_value.has_value()) {
48 *error = "unknown value for enumeration";
49 return false;
50 }
51
52 *value = *enum_value;
53 return true;
54}
55
56template <typename EnumType>
57std::string UnparseEnumFlag(const EnumType value) {
58 std::ostringstream oss;
59 oss << value;
60 return oss.str();
61}
62
63} // namespace
64
65std::optional<absl::string_view> Enum<SolverType>::ToOptString(
66 SolverType value) {
67 switch (value) {
69 return "gscip";
71 return "gurobi";
73 return "glop";
75 return "cp_sat";
77 return "pdlp";
79 return "glpk";
81 return "ecos";
83 return "scs";
85 return "highs";
87 return "santorini";
89 return "xpress";
90 }
91 return std::nullopt;
92}
93
94absl::Span<const SolverType> Enum<SolverType>::AllValues() {
95 static constexpr SolverType kSolverTypeValues[] = {
100 };
101 return absl::MakeConstSpan(kSolverTypeValues);
102}
103
104bool AbslParseFlag(const absl::string_view text, SolverType* const value,
105 std::string* const error) {
106 return ParseEnumFlag(text, value, error);
107}
108
109std::string AbslUnparseFlag(const SolverType value) {
110 return UnparseEnumFlag(value);
111}
112
113std::optional<absl::string_view> Enum<LPAlgorithm>::ToOptString(
114 LPAlgorithm value) {
115 switch (value) {
117 return "primal_simplex";
119 return "dual_simplex";
121 return "barrier";
123 return "first_order";
124 }
125 return std::nullopt;
126}
127
128absl::Span<const LPAlgorithm> Enum<LPAlgorithm>::AllValues() {
129 static constexpr LPAlgorithm kLPAlgorithmValues[] = {
134 };
135 return absl::MakeConstSpan(kLPAlgorithmValues);
136}
137
138bool AbslParseFlag(absl::string_view text, LPAlgorithm* const value,
139 std::string* const error) {
140 return ParseEnumFlag(text, value, error);
141}
142
143std::string AbslUnparseFlag(const LPAlgorithm value) {
144 return UnparseEnumFlag(value);
145}
146
147std::optional<absl::string_view> Enum<Emphasis>::ToOptString(Emphasis value) {
148 switch (value) {
149 case Emphasis::kOff:
150 return "off";
151 case Emphasis::kLow:
152 return "low";
154 return "medium";
155 case Emphasis::kHigh:
156 return "high";
158 return "very_high";
159 }
160 return std::nullopt;
161}
162
163absl::Span<const Emphasis> Enum<Emphasis>::AllValues() {
164 static constexpr Emphasis kEmphasisValues[] = {
167 };
168 return absl::MakeConstSpan(kEmphasisValues);
169}
170
171bool AbslParseFlag(absl::string_view text, Emphasis* const value,
172 std::string* const error) {
173 return ParseEnumFlag(text, value, error);
174}
175
176std::string AbslUnparseFlag(const Emphasis value) {
177 return UnparseEnumFlag(value);
178}
179
182 for (const auto& [key, val] : param_values) {
184 p.set_name(key);
185 p.set_value(val);
186 }
187 return result;
188}
189
191 const GurobiParametersProto& proto) {
192 GurobiParameters result;
193 for (const GurobiParametersProto::Parameter& p : proto.parameters()) {
194 result.param_values[p.name()] = p.value();
195 }
196 return result;
197}
198
207
216
219 for (const auto& [key, val] : param_values) {
221 p.set_name(key);
222 p.set_value(val);
223 }
224 return result;
225}
226
228 const XpressParametersProto& proto) {
229 XpressParameters result;
230 for (const XpressParametersProto::Parameter& p : proto.parameters()) {
231 result.param_values[p.name()] = p.value();
232 }
233 return result;
234}
235
239 if (time_limit < absl::InfiniteDuration()) {
241 result.mutable_time_limit()));
242 }
243 if (iteration_limit.has_value()) {
245 }
246 if (node_limit.has_value()) {
247 result.set_node_limit(*node_limit);
248 }
249 if (cutoff_limit.has_value()) {
251 }
252 if (objective_limit.has_value()) {
254 }
255 if (best_bound_limit.has_value()) {
257 }
258 if (solution_limit.has_value()) {
260 }
261 if (threads.has_value()) {
262 result.set_threads(*threads);
263 }
264 if (random_seed.has_value()) {
266 }
267 if (relative_gap_tolerance.has_value()) {
269 }
270 if (absolute_gap_tolerance.has_value()) {
272 }
273 if (solution_pool_size.has_value()) {
275 }
278 result.set_cuts(EnumToProto(cuts));
281 *result.mutable_gscip() = gscip;
282 *result.mutable_gurobi() = gurobi.Proto();
283 *result.mutable_glop() = glop;
284 *result.mutable_cp_sat() = cp_sat;
285 *result.mutable_pdlp() = pdlp;
286 *result.mutable_glpk() = glpk.Proto();
287 *result.mutable_highs() = highs;
288 *result.mutable_xpress() = xpress.Proto();
289 return result;
290}
291
292absl::StatusOr<SolveParameters> SolveParameters::FromProto(
293 const SolveParametersProto& proto) {
294 SolveParameters result;
295 result.enable_output = proto.enable_output();
296 if (proto.has_time_limit()) {
299 _ << "invalid time_limit");
300 } else {
301 result.time_limit = absl::InfiniteDuration();
302 }
303 if (proto.has_iteration_limit()) {
304 result.iteration_limit = proto.iteration_limit();
305 }
306 if (proto.has_node_limit()) {
307 result.node_limit = proto.node_limit();
308 }
309 if (proto.has_cutoff_limit()) {
310 result.cutoff_limit = proto.cutoff_limit();
311 }
312 if (proto.has_objective_limit()) {
313 result.objective_limit = proto.objective_limit();
314 }
315 if (proto.has_best_bound_limit()) {
316 result.best_bound_limit = proto.best_bound_limit();
317 }
318 if (proto.has_solution_limit()) {
319 result.solution_limit = proto.solution_limit();
320 }
321 if (proto.has_threads()) {
322 result.threads = proto.threads();
323 }
324 if (proto.has_random_seed()) {
325 result.random_seed = proto.random_seed();
326 }
327 if (proto.has_absolute_gap_tolerance()) {
329 }
330 if (proto.has_relative_gap_tolerance()) {
332 }
333 if (proto.has_solution_pool_size()) {
334 result.solution_pool_size = proto.solution_pool_size();
335 }
336 result.lp_algorithm = EnumFromProto(proto.lp_algorithm());
337 result.presolve = EnumFromProto(proto.presolve());
338 result.cuts = EnumFromProto(proto.cuts());
339 result.heuristics = EnumFromProto(proto.heuristics());
340 result.scaling = EnumFromProto(proto.scaling());
341 result.gscip = proto.gscip();
342 result.gurobi = GurobiParameters::FromProto(proto.gurobi());
343 result.glop = proto.glop();
344 result.cp_sat = proto.cp_sat();
345 result.pdlp = proto.pdlp();
346 result.glpk = GlpkParameters::FromProto(proto.glpk());
347 result.highs = proto.highs();
348 result.xpress = XpressParameters::FromProto(proto.xpress());
349 return result;
350}
351
352bool AbslParseFlag(absl::string_view text, SolveParameters* solve_parameters,
353 std::string* error) {
355 if (!ProtobufParseTextProtoForFlag(text, &proto, error)) {
356 return false;
357 }
358 absl::StatusOr<SolveParameters> params = SolveParameters::FromProto(proto);
359 if (!params.ok()) {
360 *error = absl::StrCat(
361 "SolveParametersProto was invalid and could not convert to "
362 "SolveParameters: ",
363 params.status().ToString());
364 return false;
365 }
366 *solve_parameters = *std::move(params);
367 return true;
368}
369
370std::string AbslUnparseFlag(SolveParameters solve_parameters) {
371 return ProtobufTextFormatPrintToStringForFlag(solve_parameters.Proto());
372}
373
374} // namespace math_opt
375} // namespace operations_research
::operations_research::math_opt::GurobiParametersProto_Parameter *PROTOBUF_NONNULL add_parameters()
Definition gurobi.pb.h:1452
GurobiParametersProto_Parameter Parameter
Definition gurobi.pb.h:684
const ::operations_research::math_opt::GurobiParametersProto_Parameter & parameters(int index) const
Definition gurobi.pb.h:1447
::operations_research::math_opt::GurobiParametersProto *PROTOBUF_NONNULL mutable_gurobi()
const ::operations_research::math_opt::HighsOptionsProto & highs() const
::operations_research::math_opt::EmphasisProto presolve() const
const ::operations_research::sat::SatParameters & cp_sat() const
const ::operations_research::math_opt::GlpkParametersProto & glpk() const
void set_presolve(::operations_research::math_opt::EmphasisProto value)
void set_lp_algorithm(::operations_research::math_opt::LPAlgorithmProto value)
::operations_research::math_opt::HighsOptionsProto *PROTOBUF_NONNULL mutable_highs()
::operations_research::glop::GlopParameters *PROTOBUF_NONNULL mutable_glop()
void set_scaling(::operations_research::math_opt::EmphasisProto value)
::operations_research::pdlp::PrimalDualHybridGradientParams *PROTOBUF_NONNULL mutable_pdlp()
::operations_research::sat::SatParameters *PROTOBUF_NONNULL mutable_cp_sat()
const ::operations_research::glop::GlopParameters & glop() const
::operations_research::math_opt::EmphasisProto scaling() const
::operations_research::math_opt::EmphasisProto heuristics() const
void set_cuts(::operations_research::math_opt::EmphasisProto value)
::operations_research::GScipParameters *PROTOBUF_NONNULL mutable_gscip()
const ::operations_research::math_opt::XpressParametersProto & xpress() const
::operations_research::math_opt::XpressParametersProto *PROTOBUF_NONNULL mutable_xpress()
::operations_research::math_opt::LPAlgorithmProto lp_algorithm() const
const ::operations_research::GScipParameters & gscip() const
const ::operations_research::pdlp::PrimalDualHybridGradientParams & pdlp() const
const ::operations_research::math_opt::GurobiParametersProto & gurobi() const
void set_heuristics(::operations_research::math_opt::EmphasisProto value)
const ::google::protobuf::Duration & time_limit() const
::google::protobuf::Duration *PROTOBUF_NONNULL mutable_time_limit()
::operations_research::math_opt::EmphasisProto cuts() const
::operations_research::math_opt::GlpkParametersProto *PROTOBUF_NONNULL mutable_glpk()
XpressParametersProto_Parameter Parameter
Definition xpress.pb.h:630
::operations_research::math_opt::XpressParametersProto_Parameter *PROTOBUF_NONNULL add_parameters()
Definition xpress.pb.h:906
const ::operations_research::math_opt::XpressParametersProto_Parameter & parameters(int index) const
Definition xpress.pb.h:901
bool AbslParseFlag(const absl::string_view text, SolverType *const value, std::string *const error)
std::optional< typename EnumProto< P >::Cpp > EnumFromProto(P proto_value)
Definition enums.h:281
std::optional< E > EnumFromString(absl::string_view str)
Definition enums.h:302
Enum< E >::Proto EnumToProto(std::optional< E > value)
Definition enums.h:270
std::string AbslUnparseFlag(const SolverType value)
OR-Tools root namespace.
std::string ProtobufTextFormatPrintToStringForFlag(const google::protobuf::Message &proto)
bool ProtobufParseTextProtoForFlag(absl::string_view text, ProtoType *message_out, std::string *error_out)
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 absl::Span< const E > AllValues()
static std::optional< absl::string_view > ToOptString(E value)
std::optional< bool > compute_unbound_rays_if_possible
Definition parameters.h:251
static GlpkParameters FromProto(const GlpkParametersProto &proto)
gtl::linked_hash_map< std::string, std::string > param_values
Definition parameters.h:221
static GurobiParameters FromProto(const GurobiParametersProto &proto)
std::optional< LPAlgorithm > lp_algorithm
Definition parameters.h:438
pdlp::PrimalDualHybridGradientParams pdlp
Definition parameters.h:464
static absl::StatusOr< SolveParameters > FromProto(const SolveParametersProto &proto)
static XpressParameters FromProto(const XpressParametersProto &proto)
gtl::linked_hash_map< std::string, std::string > param_values
Definition parameters.h:287
#define OR_ASSIGN_OR_RETURN3(lhs, rexpr, error_expression)