OR-Tools  9.3
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/status/statusor.h"
22#include "absl/strings/str_cat.h"
23#include "absl/strings/str_join.h"
24#include "absl/time/time.h"
30
31namespace {
32
33namespace math_opt = ::operations_research::math_opt;
34
35constexpr double kInf = std::numeric_limits<double>::infinity();
36
37// Model and solve the problem:
38// max 10 * x0 + 6 * x1 + 4 * x2
39// s.t. 10 * x0 + 4 * x1 + 5 * x2 <= 600
40// 2 * x0 + 2 * x1 + 6 * x2 <= 300
41// x0 + x1 + x2 <= 100
42// x0 in [0, infinity)
43// x1 in [0, infinity)
44// x2 in [0, infinity)
45//
46absl::Status Main() {
47 math_opt::Model model("Linear programming example");
48
49 // Variables
50 std::vector<math_opt::Variable> x;
51 for (int j = 0; j < 3; j++) {
52 x.push_back(model.AddContinuousVariable(0.0, kInf, absl::StrCat("x", j)));
53 }
54
55 // Constraints
56 std::vector<math_opt::LinearConstraint> constraints;
57 constraints.push_back(
58 model.AddLinearConstraint(10 * x[0] + 4 * x[1] + 5 * x[2] <= 600, "c1"));
59 constraints.push_back(
60 model.AddLinearConstraint(2 * x[0] + 2 * x[1] + 6 * x[2] <= 300, "c2"));
61 // sum(x[i]) <= 100
62 constraints.push_back(model.AddLinearConstraint(Sum(x) <= 100, "c3"));
63
64 // Objective
65 model.Maximize(10 * x[0] + 6 * x[1] + 4 * x[2]);
66
68 Solve(model, math_opt::SolverType::kGlop));
69 if (result.termination.reason != math_opt::TerminationReason::kOptimal) {
71 << "model failed to solve to optimality" << result.termination;
72 }
73
74 std::cout << "Problem solved in " << result.solve_time() << std::endl;
75 std::cout << "Objective value: " << result.objective_value() << std::endl;
76
77 std::cout << "Variable values: ["
78 << absl::StrJoin(result.variable_values().Values(x), ", ") << "]"
79 << std::endl;
80
81 return absl::OkStatus();
82}
83} // namespace
84
85int main(int argc, char** argv) {
86 InitGoogle(argv[0], &argc, &argv, true);
87 const absl::Status status = Main();
88 if (!status.ok()) {
89 LOG(QFATAL) << status;
90 }
91 return 0;
92}
#define LOG(severity)
Definition: base/logging.h:420
#define ASSIGN_OR_RETURN(lhs, rexpr)
absl::Status status
Definition: g_gurobi.cc:35
GRBmodel * model
void InitGoogle(const char *usage, int *argc, char ***argv, bool deprecated)
Definition: init_google.h:32
int main(int argc, char **argv)
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:94
StatusBuilder InternalErrorBuilder()
const VariableMap< double > & variable_values() const