OR-Tools  9.2
integer_programming.cc
Go to the documentation of this file.
1// Copyright 2010-2021 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// Simple integer programming example
15
16#include <iostream>
17#include <limits>
18
19#include "absl/flags/parse.h"
20#include "absl/flags/usage.h"
21#include "absl/status/statusor.h"
22#include "absl/time/time.h"
25
26namespace {
27using ::operations_research::math_opt::Model;
28using ::operations_research::math_opt::SolveResult;
31using ::operations_research::math_opt::Variable;
33
34constexpr double kInf = std::numeric_limits<double>::infinity();
35
36// Model and solve the problem:
37// max x + 10 * y
38// s.t. x + 7 * y <= 17.5
39// x <= 3.5
40// x in {0.0, 1.0, 2.0, ...,
41// y in {0.0, 1.0, 2.0, ...,
42//
43void SolveSimpleMIP() {
44 Model model("Integer programming example");
45
46 // Variables
47 const Variable x = model.AddIntegerVariable(0.0, kInf, "x");
48 const Variable y = model.AddIntegerVariable(0.0, kInf, "y");
49
50 // Constraints
51 model.AddLinearConstraint(x + 7 * y <= 17.5, "c1");
52 model.AddLinearConstraint(x <= 3.5, "c2");
53
54 // Objective
55 model.Maximize(x + 10 * y);
56
57 std::cout << "Num variables: " << model.num_variables() << std::endl;
58 std::cout << "Num constraints: " << model.num_linear_constraints()
59 << std::endl;
60
61 const SolveResult result = Solve(model, SolverType::kGscip).value();
62
63 // Check for warnings.
64 for (const auto& warning : result.warnings) {
65 LOG(ERROR) << "Solver warning: " << warning << std::endl;
66 }
67 // Check that the problem has an optimal solution.
68 QCHECK_EQ(result.termination.reason, TerminationReason::kOptimal)
69 << "Failed to find an optimal solution: " << result.termination;
70
71 std::cout << "Problem solved in " << result.solve_time() << std::endl;
72 std::cout << "Objective value: " << result.objective_value() << std::endl;
73
74 const double x_val = result.variable_values().at(x);
75 const double y_val = result.variable_values().at(y);
76
77 std::cout << "Variable values: [x=" << x_val << ", y=" << y_val << "]"
78 << std::endl;
79}
80} // namespace
81
82int main(int argc, char** argv) {
84 absl::ParseCommandLine(argc, argv);
85 SolveSimpleMIP();
86 return 0;
87}
#define LOG(severity)
Definition: base/logging.h:420
#define QCHECK_EQ
Definition: base/logging.h:40
GRBmodel * model
int main(int argc, char **argv)
const int ERROR
Definition: log_severity.h:32
void InitGoogleLogging(const char *argv0)
absl::StatusOr< SolveResult > Solve(const Model &model, const SolverType solver_type, const SolveArguments &solve_args, const SolverInitArguments &init_args)
Definition: solve.cc:155