Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
integer_to_continuous_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 we look at how SCIP handles incremental bound updates
15// going between variable types. SCIP can sometimes disregard bounds you pass it
16// when going from binary (or integer) to continuous, but this appears to only
17// happen when using the incremental bound updating API.
18#include <memory>
19#include <utility>
20
21#include "absl/log/check.h"
24
25int main(int argc, char* argv[]) {
26 InitGoogle("", &argc, &argv, true);
27
28 auto gscip_or = operations_research::GScip::Create("");
29 std::unique_ptr<operations_research::GScip> gscip = *std::move(gscip_or);
30
31 const auto x_or = gscip->AddVariable(
32 0.25, 1.25, 1.0, operations_research::GScipVarType::kBinary, "x");
33 QCHECK_OK(x_or);
34 SCIP_VAR* const x = *x_or;
35
36 // SCIP keeps the original, fractional bounds as-is.
37 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kBinary);
38 QCHECK_EQ(gscip->Lb(x), 0.25);
39 QCHECK_EQ(gscip->Ub(x), 1.25);
40
41 // We recover the original, fractional bounds after changing variable types.
42 QCHECK_OK(
44 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kContinuous);
45 QCHECK_EQ(gscip->Lb(x), 0.25);
46 QCHECK_EQ(gscip->Ub(x), 1.25);
47
48 // Without error, we change the vartype and bounds back to what they were
49 // originally (but now through the incremental API, not construction).
50 QCHECK_OK(gscip->SetVarType(x, operations_research::GScipVarType::kBinary));
51 QCHECK_OK(gscip->SetLb(x, 0.25));
52 QCHECK_OK(gscip->SetUb(x, 1.25));
53
54 // The fractional bounds are lost, replaced by rounded values.
55 QCHECK_OK(
57 QCHECK(gscip->VarType(x) == operations_research::GScipVarType::kContinuous);
58 QCHECK_EQ(gscip->Lb(x), 1.0);
59 QCHECK_EQ(gscip->Ub(x), 1.0);
60}
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[])