Google OR-Tools v9.14
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"
37
38namespace operations_research {
39namespace math_opt {
40namespace {
41
42template <typename EnumType>
43bool ParseEnumFlag(const absl::string_view text, EnumType* const value,
44 std::string* const error) {
45 const std::optional<EnumType> enum_value = EnumFromString<EnumType>(text);
46 if (!enum_value.has_value()) {
47 *error = "unknown value for enumeration";
48 return false;
49 }
50
51 *value = *enum_value;
52 return true;
53}
54
55template <typename EnumType>
56std::string UnparseEnumFlag(const EnumType value) {
57 std::ostringstream oss;
58 oss << value;
59 return oss.str();
60}
61
62} // namespace
63
64std::optional<absl::string_view> Enum<SolverType>::ToOptString(
65 SolverType value) {
66 switch (value) {
68 return "gscip";
70 return "gurobi";
72 return "glop";
74 return "cp_sat";
76 return "pdlp";
78 return "glpk";
80 return "ecos";
82 return "scs";
84 return "highs";
86 return "santorini";
88 return "xpress";
89 }
90 return std::nullopt;
91}
92
93absl::Span<const SolverType> Enum<SolverType>::AllValues() {
94 static constexpr SolverType kSolverTypeValues[] = {
99 };
100 return absl::MakeConstSpan(kSolverTypeValues);
101}
102
103bool AbslParseFlag(const absl::string_view text, SolverType* const value,
104 std::string* const error) {
105 return ParseEnumFlag(text, value, error);
106}
107
108std::string AbslUnparseFlag(const SolverType value) {
109 return UnparseEnumFlag(value);
110}
111
112std::optional<absl::string_view> Enum<LPAlgorithm>::ToOptString(
113 LPAlgorithm value) {
114 switch (value) {
116 return "primal_simplex";
118 return "dual_simplex";
120 return "barrier";
122 return "first_order";
123 }
124 return std::nullopt;
125}
126
127absl::Span<const LPAlgorithm> Enum<LPAlgorithm>::AllValues() {
128 static constexpr LPAlgorithm kLPAlgorithmValues[] = {
133 };
134 return absl::MakeConstSpan(kLPAlgorithmValues);
135}
136
137bool AbslParseFlag(absl::string_view text, LPAlgorithm* const value,
138 std::string* const error) {
139 return ParseEnumFlag(text, value, error);
140}
141
142std::string AbslUnparseFlag(const LPAlgorithm value) {
143 return UnparseEnumFlag(value);
144}
145
146std::optional<absl::string_view> Enum<Emphasis>::ToOptString(Emphasis value) {
147 switch (value) {
148 case Emphasis::kOff:
149 return "off";
150 case Emphasis::kLow:
151 return "low";
153 return "medium";
154 case Emphasis::kHigh:
155 return "high";
157 return "very_high";
158 }
159 return std::nullopt;
160}
161
162absl::Span<const Emphasis> Enum<Emphasis>::AllValues() {
163 static constexpr Emphasis kEmphasisValues[] = {
166 };
167 return absl::MakeConstSpan(kEmphasisValues);
168}
169
170bool AbslParseFlag(absl::string_view text, Emphasis* const value,
171 std::string* const error) {
172 return ParseEnumFlag(text, value, error);
173}
174
175std::string AbslUnparseFlag(const Emphasis value) {
176 return UnparseEnumFlag(value);
177}
178
181 for (const auto& [key, val] : param_values) {
183 p.set_name(key);
184 p.set_value(val);
185 }
186 return result;
187}
188
190 const GurobiParametersProto& proto) {
191 GurobiParameters result;
192 for (const GurobiParametersProto::Parameter& p : proto.parameters()) {
193 result.param_values[p.name()] = p.value();
194 }
195 return result;
196}
197
206
215
219 if (time_limit < absl::InfiniteDuration()) {
221 result.mutable_time_limit()));
222 }
223 if (iteration_limit.has_value()) {
225 }
226 if (node_limit.has_value()) {
227 result.set_node_limit(*node_limit);
228 }
229 if (cutoff_limit.has_value()) {
231 }
232 if (objective_limit.has_value()) {
234 }
235 if (best_bound_limit.has_value()) {
237 }
238 if (solution_limit.has_value()) {
240 }
241 if (threads.has_value()) {
242 result.set_threads(*threads);
243 }
244 if (random_seed.has_value()) {
246 }
247 if (relative_gap_tolerance.has_value()) {
249 }
250 if (absolute_gap_tolerance.has_value()) {
252 }
253 if (solution_pool_size.has_value()) {
255 }
258 result.set_cuts(EnumToProto(cuts));
261 *result.mutable_gscip() = gscip;
262 *result.mutable_gurobi() = gurobi.Proto();
263 *result.mutable_glop() = glop;
264 *result.mutable_cp_sat() = cp_sat;
265 *result.mutable_pdlp() = pdlp;
266 *result.mutable_glpk() = glpk.Proto();
267 *result.mutable_highs() = highs;
268 return result;
269}
270
271absl::StatusOr<SolveParameters> SolveParameters::FromProto(
272 const SolveParametersProto& proto) {
273 SolveParameters result;
274 result.enable_output = proto.enable_output();
275 if (proto.has_time_limit()) {
278 _ << "invalid time_limit");
279 } else {
280 result.time_limit = absl::InfiniteDuration();
281 }
282 if (proto.has_iteration_limit()) {
283 result.iteration_limit = proto.iteration_limit();
284 }
285 if (proto.has_node_limit()) {
286 result.node_limit = proto.node_limit();
287 }
288 if (proto.has_cutoff_limit()) {
289 result.cutoff_limit = proto.cutoff_limit();
290 }
291 if (proto.has_objective_limit()) {
292 result.objective_limit = proto.objective_limit();
293 }
294 if (proto.has_best_bound_limit()) {
295 result.best_bound_limit = proto.best_bound_limit();
296 }
297 if (proto.has_solution_limit()) {
298 result.solution_limit = proto.solution_limit();
299 }
300 if (proto.has_threads()) {
301 result.threads = proto.threads();
302 }
303 if (proto.has_random_seed()) {
304 result.random_seed = proto.random_seed();
305 }
306 if (proto.has_absolute_gap_tolerance()) {
308 }
309 if (proto.has_relative_gap_tolerance()) {
311 }
312 if (proto.has_solution_pool_size()) {
313 result.solution_pool_size = proto.solution_pool_size();
314 }
315 result.lp_algorithm = EnumFromProto(proto.lp_algorithm());
316 result.presolve = EnumFromProto(proto.presolve());
317 result.cuts = EnumFromProto(proto.cuts());
318 result.heuristics = EnumFromProto(proto.heuristics());
319 result.scaling = EnumFromProto(proto.scaling());
320 result.gscip = proto.gscip();
321 result.gurobi = GurobiParameters::FromProto(proto.gurobi());
322 result.glop = proto.glop();
323 result.cp_sat = proto.cp_sat();
324 result.pdlp = proto.pdlp();
325 result.glpk = GlpkParameters::FromProto(proto.glpk());
326 result.highs = proto.highs();
327 return result;
328}
329
330bool AbslParseFlag(absl::string_view text, SolveParameters* solve_parameters,
331 std::string* error) {
333 if (!ProtobufParseTextProtoForFlag(text, &proto, error)) {
334 return false;
335 }
336 absl::StatusOr<SolveParameters> params = SolveParameters::FromProto(proto);
337 if (!params.ok()) {
338 *error = absl::StrCat(
339 "SolveParametersProto was invalid and could not convert to "
340 "SolveParameters: ",
341 params.status().ToString());
342 return false;
343 }
344 *solve_parameters = *std::move(params);
345 return true;
346}
347
348std::string AbslUnparseFlag(SolveParameters solve_parameters) {
349 return ProtobufTextFormatPrintToStringForFlag(solve_parameters.Proto());
350}
351
352} // namespace math_opt
353} // namespace operations_research
bool has_compute_unbound_rays_if_possible() const
optional bool compute_unbound_rays_if_possible = 1;
Definition glpk.pb.h:289
::operations_research::math_opt::GurobiParametersProto_Parameter *PROTOBUF_NONNULL add_parameters()
Definition gurobi.pb.h:1450
GurobiParametersProto_Parameter Parameter
nested types -------------------------------------------------—
Definition gurobi.pb.h:687
const ::operations_research::math_opt::GurobiParametersProto_Parameter & parameters(int index) const
Definition gurobi.pb.h:1445
::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
bool has_cutoff_limit() const
optional double cutoff_limit = 20;
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)
bool has_objective_limit() const
optional double objective_limit = 21;
bool has_random_seed() const
optional int32 random_seed = 5;
bool has_solution_limit() const
optional int32 solution_limit = 23;
::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()
bool has_absolute_gap_tolerance() const
optional double absolute_gap_tolerance = 18;
bool has_node_limit() const
optional int64 node_limit = 24;
::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
bool has_best_bound_limit() const
optional double best_bound_limit = 22;
void set_heuristics(::operations_research::math_opt::EmphasisProto value)
bool has_relative_gap_tolerance() const
optional double relative_gap_tolerance = 17;
bool has_threads() const
optional int32 threads = 4;
const ::google::protobuf::Duration & time_limit() const
bool has_solution_pool_size() const
optional int32 solution_pool_size = 25;
bool has_time_limit() const
.google.protobuf.Duration time_limit = 1;
bool has_iteration_limit() const
optional int64 iteration_limit = 2;
::google::protobuf::Duration *PROTOBUF_NONNULL mutable_time_limit()
::operations_research::math_opt::EmphasisProto cuts() const
::operations_research::math_opt::GlpkParametersProto *PROTOBUF_NONNULL mutable_glpk()
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
bool AbslParseFlag(const absl::string_view text, SolverType *const value, std::string *const error)
SolverType
The solvers supported by MathOpt.
Definition parameters.h:41
Emphasis
never give an error, and will map onto their best match.
Definition parameters.h:184
LPAlgorithm
Selects an algorithm for solving linear programs.
Definition parameters.h:133
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)
In SWIG mode, we don't want anything besides these top-level includes.
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()
Returns all possible values of the enum.
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
Parameter name-value pairs to set in insertion order.
Definition parameters.h:221
static GurobiParameters FromProto(const GurobiParametersProto &proto)
std::optional< LPAlgorithm > lp_algorithm
Definition parameters.h:398
pdlp::PrimalDualHybridGradientParams pdlp
Definition parameters.h:424
static absl::StatusOr< SolveParameters > FromProto(const SolveParametersProto &proto)
std::optional< int32_t > threads
If unset, use the solver default. If set, it must be >= 1.
Definition parameters.h:337
#define OR_ASSIGN_OR_RETURN3(lhs, rexpr, error_expression)