OR-Tools  9.0
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 "ortools/base/logging.h"
22 #include "absl/status/statusor.h"
23 #include "absl/time/time.h"
25 
26 namespace {
27 using ::operations_research::math_opt::MathOpt;
28 using ::operations_research::math_opt::Result;
29 using ::operations_research::math_opt::SolveParametersProto;
30 using ::operations_research::math_opt::SOLVER_TYPE_GSCIP;
31 using ::operations_research::math_opt::SolveResultProto;
32 using ::operations_research::math_opt::SolveStatsProto;
33 using ::operations_research::math_opt::Variable;
35 
36 constexpr 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 //
45 void 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 
88 int main(int argc, char** argv) {
90 absl::ParseCommandLine(argc, argv);
91  SolveSimpleMIP();
92  return 0;
93 }
#define LOG(severity)
Definition: base/logging.h:423
#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)