OR-Tools  9.2
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::Model;
34using ::operations_research::math_opt::SolveResult;
38using ::operations_research::math_opt::Variable;
39
40constexpr double kInf = std::numeric_limits<double>::infinity();
41
42// Model and solve the problem:
43// max 10 * x0 + 6 * x1 + 4 * x2
44// s.t. 10 * x0 + 4 * x1 + 5 * x2 <= 600
45// 2 * x0 + 2 * x1 + 6 * x2 <= 300
46// x0 + x1 + x2 <= 100
47// x0 in [0, infinity)
48// x1 in [0, infinity)
49// x2 in [0, infinity)
50//
51void SolveSimpleLp() {
52 Model model("Linear programming example");
53
54 // Variables
55 std::vector<Variable> x;
56 for (int j = 0; j < 3; j++) {
57 x.push_back(model.AddContinuousVariable(0.0, kInf, absl::StrCat("x", j)));
58 }
59
60 // Constraints
61 std::vector<LinearConstraint> constraints;
62 constraints.push_back(
63 model.AddLinearConstraint(10 * x[0] + 4 * x[1] + 5 * x[2] <= 600, "c1"));
64 constraints.push_back(
65 model.AddLinearConstraint(2 * x[0] + 2 * x[1] + 6 * x[2] <= 300, "c2"));
66 // sum(x[i]) <= 100
67 constraints.push_back(model.AddLinearConstraint(Sum(x) <= 100, "c3"));
68
69 // Objective
70 model.Maximize(10 * x[0] + 6 * x[1] + 4 * x[2]);
71
72 std::cout << "Num variables: " << model.num_variables() << std::endl;
73 std::cout << "Num constraints: " << model.num_linear_constraints()
74 << std::endl;
75
76 const SolveResult result = Solve(model, SolverType::kGlop).value();
77
78 // Check for warnings.
79 for (const auto& warning : result.warnings) {
80 LOG(ERROR) << "Solver warning: " << warning << std::endl;
81 }
82 // Check that the problem has an optimal solution.
83 QCHECK_EQ(result.termination.reason, TerminationReason::kOptimal)
84 << "Failed to find an optimal solution: " << result.termination;
85
86 std::cout << "Problem solved in " << result.solve_time() << std::endl;
87 std::cout << "Objective value: " << result.objective_value() << std::endl;
88
89 std::cout << "Variable values: ["
90 << absl::StrJoin(result.variable_values().Values(x), ", ") << "]"
91 << std::endl;
92 std::cout << "Constraint duals: ["
93 << absl::StrJoin(result.dual_values().Values(constraints), ", ")
94 << "]" << std::endl;
95 std::cout << "Reduced costs: ["
96 << absl::StrJoin(result.reduced_costs().Values(x), ", ") << "]"
97 << std::endl;
98
99 // TODO(user): add basis statuses when they are included in SolveResult
100}
101} // namespace
102
103int main(int argc, char** argv) {
105 absl::ParseCommandLine(argc, argv);
106 SolveSimpleLp();
107 return 0;
108}
#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)
LinearExpression Sum(const Iterable &items)
absl::StatusOr< SolveResult > Solve(const Model &model, const SolverType solver_type, const SolveArguments &solve_args, const SolverInitArguments &init_args)
Definition: solve.cc:155