Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
binary_variable_bounds.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 changing the bounds on a binary variable in (g)Scip
15// can lead to unexpected behaviors: either incorrect bounds, or an internal
16// CHECK-fail in debug mode.
17#include <memory>
18#include <utility>
19
20#include "absl/log/check.h"
23
24int main(int argc, char* argv[]) {
25 InitGoogle("", &argc, &argv, true);
26
27 auto gscip_or = operations_research::GScip::Create("");
28 std::unique_ptr<operations_research::GScip> gscip = *std::move(gscip_or);
29
30 const auto x_or = gscip->AddVariable(
32 QCHECK_OK(x_or);
33 SCIP_VAR* const x = *x_or;
34
35 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
36
37 // First we set the bounds to [0, 0]. This works fine, no crash.
38 QCHECK_OK(gscip->SetLb(x, 0.0));
39 QCHECK_OK(gscip->SetUb(x, 0.0));
40 QCHECK_EQ(gscip->Lb(x), 0.0);
41 QCHECK_EQ(gscip->Ub(x), 0.0);
42 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
43
44 // Next we set the bounds to [1, 1]. Also fine without a crash.
45 QCHECK_OK(gscip->SetLb(x, 1.0));
46 QCHECK_OK(gscip->SetUb(x, 1.0));
47 QCHECK_EQ(gscip->Lb(x), 1.0);
48 QCHECK_EQ(gscip->Ub(x), 1.0);
49 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
50
51 // We change bounds back to [0, 1]. Also fine without a crash.
52 QCHECK_OK(gscip->SetLb(x, 0.0));
53 QCHECK_OK(gscip->SetUb(x, 1.0));
54 QCHECK_EQ(gscip->Lb(x), 0.0);
55 QCHECK_EQ(gscip->Ub(x), 1.0);
56 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
57
58 // Next we set the bounds to [0.25, 0.75]. This will not crash, but sets the
59 // bounds to unexpected (but mathematically equivalent) values.
60 QCHECK_OK(gscip->SetLb(x, 0.25));
61 QCHECK_OK(gscip->SetUb(x, 0.75));
62 QCHECK_EQ(gscip->Lb(x), 1.0);
63 QCHECK_EQ(gscip->Ub(x), 0.0);
64 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
65
66 // Setting the bounds to [0, 2] will CHECK-fail in debug model.
67 gscip->SetUb(x, 2.0).IgnoreError();
68
69 // Similarly, updating the lower bound to -1 will CHECK-fail in debug model.
70 gscip->SetLb(x, -1.0).IgnoreError();
71}
int main(int argc, char *argv[])
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