OR-Tools  9.1
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 linear 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::MathOpt;
28using ::operations_research::math_opt::Result;
29using ::operations_research::math_opt::SolveParametersProto;
30using ::operations_research::math_opt::SOLVER_TYPE_GSCIP;
31using ::operations_research::math_opt::SolveResultProto;
32using ::operations_research::math_opt::SolveStatsProto;
33using ::operations_research::math_opt::Variable;
35
36constexpr double kInf = std::numeric_limits<double>::infinity();
37
38// Model and solve the problem:
39// max x + 10 * y
40// s.t. x + 7 * y <= 17.5
41// x <= 3.5
42// x in {0.0, 1.0, 2.0, ...,
43// y in {0.0, 1.0, 2.0, ...,
44//
45void SolveSimpleMIP() {
46 MathOpt optimizer(SOLVER_TYPE_GSCIP, "Integer programming example");
47
48 // Variables
49 const Variable x = optimizer.AddIntegerVariable(0.0, kInf, "x");
50 const Variable y = optimizer.AddIntegerVariable(0.0, kInf, "y");
51
52 // Constraints
53 optimizer.AddLinearConstraint(x + 7 * y <= 17.5, "c1");
54 optimizer.AddLinearConstraint(x <= 3.5, "c2");
55
56 // Objective
57 optimizer.objective().Maximize(x + 10 * y);
58
59 std::cout << "Num variables: " << optimizer.num_variables() << std::endl;
60 std::cout << "Num constraints: " << optimizer.num_linear_constraints()
61 << std::endl;
62
63 const Result result = optimizer.Solve(SolveParametersProto()).value();
64
65 // Check for warnings.
66 for (const auto& warning : result.warnings) {
67 LOG(ERROR) << "Solver warning: " << warning << std::endl;
68 }
69 // Check that the problem has an optimal solution.
70 QCHECK_EQ(result.termination_reason, SolveResultProto::OPTIMAL)
71 << "Failed to find an optimal solution: " << result.termination_detail;
72
73 std::cout << "Problem solved in " << result.solve_time() << std::endl;
74 std::cout << "Objective value: " << result.objective_value() << std::endl;
75
76 const double x_val = result.variable_values().at(x);
77 const double y_val = result.variable_values().at(y);
78
79 std::cout << "Variable values: [x=" << x_val << ", y=" << y_val << "]"
80 << std::endl;
81 const SolveStatsProto& stat = result.solve_stats;
82 std::cout << "Simplex iterations: " << stat.simplex_iterations() << std::endl;
83 std::cout << "Barrier iterations: " << stat.barrier_iterations() << std::endl;
84 std::cout << "Branch and bound nodes: " << stat.node_count() << std::endl;
85}
86} // namespace
87
88int main(int argc, char** argv) {
90 absl::ParseCommandLine(argc, argv);
91 SolveSimpleMIP();
92 return 0;
93}
#define LOG(severity)
Definition: base/logging.h:416
#define QCHECK_EQ
Definition: base/logging.h:40
int main(int argc, char **argv)
const int ERROR
Definition: log_severity.h:32
void InitGoogleLogging(const char *argv0)