Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
integer_to_binary_variable.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
14// A simple example where SCIP's promotion of [0, 1] integer variables to binary
15// variables can lead to unexpected behaviors: either incorrect bounds, or an
16// internal CHECK-fail in debug mode. The same as `binary_variable_bounds.cc`,
17// except the variable is passed as a [0, 1] integer variable and immediately
18// promoted to a binary variable, thus inheriting all the odd behaviors.
19#include <memory>
20#include <utility>
21
22#include "absl/log/check.h"
25
26int main(int argc, char* argv[]) {
27 InitGoogle("", &argc, &argv, true);
28
29 auto gscip_or = operations_research::GScip::Create("");
30 std::unique_ptr<operations_research::GScip> gscip = *std::move(gscip_or);
31
32 const auto x_or = gscip->AddVariable(
34 QCHECK_OK(x_or);
35 SCIP_VAR* const x = *x_or;
36
37 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
38
39 // First we set the bounds to [0, 0]. This works fine, no crash.
40 QCHECK_OK(gscip->SetLb(x, 0.0));
41 QCHECK_OK(gscip->SetUb(x, 0.0));
42 QCHECK_EQ(gscip->Lb(x), 0.0);
43 QCHECK_EQ(gscip->Ub(x), 0.0);
44 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
45
46 // Next we set the bounds to [1, 1]. Also fine without a crash.
47 QCHECK_OK(gscip->SetLb(x, 1.0));
48 QCHECK_OK(gscip->SetUb(x, 1.0));
49 QCHECK_EQ(gscip->Lb(x), 1.0);
50 QCHECK_EQ(gscip->Ub(x), 1.0);
51 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
52
53 // We change bounds back to [0, 1]. Also fine without a crash.
54 QCHECK_OK(gscip->SetLb(x, 0.0));
55 QCHECK_OK(gscip->SetUb(x, 1.0));
56 QCHECK_EQ(gscip->Lb(x), 0.0);
57 QCHECK_EQ(gscip->Ub(x), 1.0);
58 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
59
60 // Next we set the bounds to [0.25, 0.75]. This will not crash, but sets the
61 // bounds to unexpected (but mathematically equivalent) values.
62 QCHECK_OK(gscip->SetLb(x, 0.25));
63 QCHECK_OK(gscip->SetUb(x, 0.75));
64 QCHECK_EQ(gscip->Lb(x), 1.0);
65 QCHECK_EQ(gscip->Ub(x), 0.0);
66 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
67
68 // Setting the bounds to [0, 2] will CHECK-fail in debug model.
69 gscip->SetUb(x, 2.0).IgnoreError();
70}
static absl::StatusOr< std::unique_ptr< GScip > > Create(const std::string &problem_name)
Definition gscip.cc:362
void InitGoogle(absl::string_view usage, int *argc, char ***argv, bool deprecated)
Definition init_google.h:49
int main(int argc, char *argv[])