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