OR-Tools  8.2
legacy_scip_params.cc
Go to the documentation of this file.
1// Copyright 2010-2018 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
18#include "absl/status/status.h"
19#include "absl/strings/numbers.h"
20#include "absl/strings/str_format.h"
21#include "absl/strings/str_split.h"
24#include "scip/scip.h"
25#include "scip/scip_numerics.h"
26#include "scip/scip_param.h"
27#include "scip/struct_paramset.h"
28#include "scip/type_paramset.h"
29
30namespace operations_research {
31
33 const std::string& parameters, SCIP* scip) {
34 for (const auto parameter : absl::StrSplit(parameters, absl::ByAnyChar(",\n"),
35 absl::SkipWhitespace())) {
36 std::vector<std::string> key_value = absl::StrSplit(
37 parameter, absl::ByAnyChar("= "), absl::SkipWhitespace());
38 if (key_value.size() != 2) {
39 return absl::InvalidArgumentError(
40 absl::StrFormat("Cannot parse parameter '%s'. Expected format is "
41 "'parameter/name = value'",
42 parameter));
43 }
44
45 std::string name = key_value[0];
46 absl::RemoveExtraAsciiWhitespace(&name);
47 std::string value = key_value[1];
48 absl::RemoveExtraAsciiWhitespace(&value);
49 const double infinity = SCIPinfinity(scip);
50
51 SCIP_PARAM* param = SCIPgetParam(scip, name.c_str());
52 if (param == nullptr) {
53 return absl::InvalidArgumentError(
54 absl::StrFormat("Invalid parameter name '%s'", name));
55 }
56 switch (param->paramtype) {
57 case SCIP_PARAMTYPE_BOOL: {
58 bool parsed_value;
59 if (absl::SimpleAtob(value, &parsed_value)) {
61 SCIPsetBoolParam(scip, name.c_str(), parsed_value));
62 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
63 continue;
64 }
65 break;
66 }
67 case SCIP_PARAMTYPE_INT: {
68 int parsed_value;
69 if (absl::SimpleAtoi(value, &parsed_value)) {
71 SCIPsetIntParam(scip, name.c_str(), parsed_value));
72 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
73 continue;
74 }
75 break;
76 }
77 case SCIP_PARAMTYPE_LONGINT: {
78 int64_t parsed_value;
79 if (absl::SimpleAtoi(value, &parsed_value)) {
80 RETURN_IF_SCIP_ERROR(SCIPsetLongintParam(
81 scip, name.c_str(), static_cast<SCIP_Longint>(parsed_value)));
82 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
83 continue;
84 }
85 break;
86 }
87 case SCIP_PARAMTYPE_REAL: {
88 double parsed_value;
89 if (absl::SimpleAtod(value, &parsed_value)) {
90 if (parsed_value > infinity) parsed_value = infinity;
92 SCIPsetRealParam(scip, name.c_str(), parsed_value));
93 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
94 continue;
95 }
96 break;
97 }
98 case SCIP_PARAMTYPE_CHAR: {
99 if (value.size() == 1) {
100 RETURN_IF_SCIP_ERROR(SCIPsetCharParam(scip, name.c_str(), value[0]));
101 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
102 continue;
103 }
104 break;
105 }
106 case SCIP_PARAMTYPE_STRING: {
107 if (value.front() == '"' && value.back() == '"') {
108 value.erase(value.begin());
109 value.erase(value.end() - 1);
110 }
112 SCIPsetStringParam(scip, name.c_str(), value.c_str()));
113 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
114 continue;
115 }
116 }
117 return absl::InvalidArgumentError(
118 absl::StrFormat("Invalid parameter value '%s'", parameter));
119 }
120 return absl::OkStatus();
121}
122
123} // namespace operations_research
#define VLOG(verboselevel)
Definition: base/logging.h:978
SatParameters parameters
const std::string name
int64 value
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
absl::Status LegacyScipSetSolverSpecificParameters(const std::string &parameters, SCIP *scip)
#define RETURN_IF_SCIP_ERROR(x)