OR-Tools  9.1
linear_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#include <string>
19#include <vector>
20
21#include "absl/flags/parse.h"
22#include "absl/flags/usage.h"
23#include "absl/status/statusor.h"
24#include "absl/strings/str_cat.h"
25#include "absl/strings/str_join.h"
26#include "absl/time/time.h"
29
30namespace {
31using ::operations_research::math_opt::LinearConstraint;
32using ::operations_research::math_opt::LinearExpression;
33using ::operations_research::math_opt::MathOpt;
34using ::operations_research::math_opt::Result;
35using ::operations_research::math_opt::SolveParametersProto;
36using ::operations_research::math_opt::SOLVER_TYPE_GLOP;
37using ::operations_research::math_opt::SolveResultProto;
38using ::operations_research::math_opt::SolveStatsProto;
40using ::operations_research::math_opt::Variable;
41
42constexpr double kInf = std::numeric_limits<double>::infinity();
43
44// Model and solve the problem:
45// max 10 * x0 + 6 * x1 + 4 * x2
46// s.t. 10 * x0 + 4 * x1 + 5 * x2 <= 600
47// 2 * x0 + 2 * x1 + 6 * x2 <= 300
48// x0 + x1 + x2 <= 100
49// x0 in [0, infinity)
50// x1 in [0, infinity)
51// x2 in [0, infinity)
52//
53void SolveSimpleLp() {
54 MathOpt optimizer(SOLVER_TYPE_GLOP, "Linear programming example");
55
56 // Variables
57 std::vector<Variable> x;
58 for (int j = 0; j < 3; j++) {
59 x.push_back(
60 optimizer.AddContinuousVariable(0.0, kInf, absl::StrCat("x", j)));
61 }
62
63 // Constraints
64 std::vector<LinearConstraint> constraints;
65 constraints.push_back(optimizer.AddLinearConstraint(
66 10 * x[0] + 4 * x[1] + 5 * x[2] <= 600, "c1"));
67 constraints.push_back(optimizer.AddLinearConstraint(
68 2 * x[0] + 2 * x[1] + 6 * x[2] <= 300, "c2"));
69 // sum(x[i]) <= 100
70 constraints.push_back(optimizer.AddLinearConstraint(Sum(x) <= 100, "c3"));
71
72 // Objective
73 optimizer.objective().Maximize(10 * x[0] + 6 * x[1] + 4 * x[2]);
74
75 std::cout << "Num variables: " << optimizer.num_variables() << std::endl;
76 std::cout << "Num constraints: " << optimizer.num_linear_constraints()
77 << std::endl;
78
79 const Result result = optimizer.Solve(SolveParametersProto()).value();
80
81 // Check for warnings.
82 for (const auto& warning : result.warnings) {
83 LOG(ERROR) << "Solver warning: " << warning << std::endl;
84 }
85 // Check that the problem has an optimal solution.
86 QCHECK_EQ(result.termination_reason, SolveResultProto::OPTIMAL)
87 << "Failed to find an optimal solution: " << result.termination_detail;
88
89 std::cout << "Problem solved in " << result.solve_time() << std::endl;
90 std::cout << "Objective value: " << result.objective_value() << std::endl;
91
92 std::cout << "Variable values: ["
93 << absl::StrJoin(result.variable_values().Values(x), ", ") << "]"
94 << std::endl;
95 std::cout << "Constraint duals: ["
96 << absl::StrJoin(result.dual_values().Values(constraints), ", ")
97 << "]" << std::endl;
98 std::cout << "Reduced costs: ["
99 << absl::StrJoin(result.reduced_costs().Values(x), ", ") << "]"
100 << std::endl;
101 const SolveStatsProto& stat = result.solve_stats;
102 std::cout << "Simplex iterations: " << stat.simplex_iterations() << std::endl;
103 std::cout << "Barrier iterations: " << stat.barrier_iterations() << std::endl;
104
105 // TODO(user): add basis statuses when they are included in Result
106}
107} // namespace
108
109int main(int argc, char** argv) {
111 absl::ParseCommandLine(argc, argv);
112 SolveSimpleLp();
113 return 0;
114}
#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)
LinearExpression Sum(const Iterable &items)