Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
legacy_scip_params.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 <string>
18#include <vector>
19
20#include "absl/status/status.h"
21#include "absl/strings/numbers.h"
22#include "absl/strings/str_format.h"
23#include "absl/strings/str_split.h"
24#include "absl/strings/string_view.h"
27#include "scip/scip.h"
28#include "scip/scip_numerics.h"
29#include "scip/scip_param.h"
30#include "scip/struct_paramset.h"
31#include "scip/type_paramset.h"
32
33namespace operations_research {
34
36 SCIP* scip) {
37 for (const auto parameter : absl::StrSplit(parameters, absl::ByAnyChar(",\n"),
38 absl::SkipWhitespace())) {
39 std::vector<std::string> key_value = absl::StrSplit(
40 parameter, absl::ByAnyChar("= "), absl::SkipWhitespace());
41 if (key_value.size() != 2) {
42 return absl::InvalidArgumentError(
43 absl::StrFormat("Cannot parse parameter '%s'. Expected format is "
44 "'parameter/name = value'",
45 parameter));
46 }
47
48 std::string name = key_value[0];
49 absl::RemoveExtraAsciiWhitespace(&name);
50 std::string value = key_value[1];
51 absl::RemoveExtraAsciiWhitespace(&value);
52 const double infinity = SCIPinfinity(scip);
53
54 SCIP_PARAM* param = SCIPgetParam(scip, name.c_str());
55 if (param == nullptr) {
56 return absl::InvalidArgumentError(
57 absl::StrFormat("Invalid parameter name '%s'", name));
58 }
59 switch (param->paramtype) {
60 case SCIP_PARAMTYPE_BOOL: {
61 bool parsed_value;
62 if (absl::SimpleAtob(value, &parsed_value)) {
64 SCIPsetBoolParam(scip, name.c_str(), parsed_value));
65 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
66 continue;
67 }
68 break;
69 }
70 case SCIP_PARAMTYPE_INT: {
71 int parsed_value;
72 if (absl::SimpleAtoi(value, &parsed_value)) {
74 SCIPsetIntParam(scip, name.c_str(), parsed_value));
75 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
76 continue;
77 }
78 break;
79 }
80 case SCIP_PARAMTYPE_LONGINT: {
81 int64_t parsed_value;
82 if (absl::SimpleAtoi(value, &parsed_value)) {
83 RETURN_IF_SCIP_ERROR(SCIPsetLongintParam(
84 scip, name.c_str(), static_cast<SCIP_Longint>(parsed_value)));
85 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
86 continue;
87 }
88 break;
89 }
90 case SCIP_PARAMTYPE_REAL: {
91 double parsed_value;
92 if (absl::SimpleAtod(value, &parsed_value)) {
93 if (parsed_value > infinity) parsed_value = infinity;
95 SCIPsetRealParam(scip, name.c_str(), parsed_value));
96 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
97 continue;
98 }
99 break;
100 }
101 case SCIP_PARAMTYPE_CHAR: {
102 if (value.size() == 1) {
103 RETURN_IF_SCIP_ERROR(SCIPsetCharParam(scip, name.c_str(), value[0]));
104 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
105 continue;
106 }
107 break;
108 }
109 case SCIP_PARAMTYPE_STRING: {
110 if (value.front() == '"' && value.back() == '"') {
111 value.erase(value.begin());
112 value.erase(value.end() - 1);
113 }
115 SCIPsetStringParam(scip, name.c_str(), value.c_str()));
116 VLOG(2) << absl::StrFormat("Set parameter %s to %s", name, value);
117 continue;
118 }
119 }
120 return absl::InvalidArgumentError(
121 absl::StrFormat("Invalid parameter value '%s'", parameter));
122 }
123 return absl::OkStatus();
124}
125
126} // namespace operations_research
SatParameters parameters
const std::string name
A name for logging purposes.
int64_t value
In SWIG mode, we don't want anything besides these top-level includes.
absl::Status LegacyScipSetSolverSpecificParameters(absl::string_view parameters, SCIP *scip)
#define RETURN_IF_SCIP_ERROR(x)