OR-Tools  9.2
basic_example.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// Testing correctness of the code snippets in the comments of math_opt.h.
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"
24
25namespace {
26
27// Model the problem:
28// max 2.0 * x + y
29// s.t. x + y <= 1.5
30// x in {0.0, 1.0}
31// y in [0.0, 2.5]
32//
33
34void SolveVersion1() {
35 using ::operations_research::math_opt::LinearConstraint;
36 using ::operations_research::math_opt::Model;
37 using ::operations_research::math_opt::SolveResult;
40 using ::operations_research::math_opt::Variable;
41
42 Model model("my_model");
43 const Variable x = model.AddBinaryVariable("x");
44 const Variable y = model.AddContinuousVariable(0.0, 2.5, "y");
45 const LinearConstraint c = model.AddLinearConstraint(
46 -std::numeric_limits<double>::infinity(), 1.5, "c");
47 model.set_coefficient(c, x, 1.0);
48 model.set_coefficient(c, y, 1.0);
49 model.set_objective_coefficient(x, 2.0);
50 model.set_objective_coefficient(y, 1.0);
51 model.set_maximize();
52 const SolveResult result = Solve(model, SolverType::kGscip).value();
53 for (const auto& warning : result.warnings) {
54 std::cerr << "Solver warning: " << warning << std::endl;
55 }
56 CHECK_EQ(result.termination.reason, TerminationReason::kOptimal)
57 << result.termination;
58 // The following code will print:
59 // objective value: 2.5
60 // value for variable x: 1
61 std::cout << "objective value: " << result.objective_value()
62 << "\nvalue for variable x: " << result.variable_values().at(x)
63 << std::endl;
64}
65
66void SolveVersion2() {
67 using ::operations_research::math_opt::LinearExpression;
68 using ::operations_research::math_opt::Model;
69 using ::operations_research::math_opt::SolveResult;
72 using ::operations_research::math_opt::Variable;
73
74 Model model("my_model");
75 const Variable x = model.AddBinaryVariable("x");
76 const Variable y = model.AddContinuousVariable(0.0, 2.5, "y");
77 // We can directly use linear combinations of variables ...
78 model.AddLinearConstraint(x + y <= 1.5, "c");
79 // ... or build them incrementally.
80 LinearExpression objective_expression;
81 objective_expression += 2 * x;
82 objective_expression += y;
83 model.Maximize(objective_expression);
84 const SolveResult result = Solve(model, SolverType::kGscip).value();
85 for (const auto& warning : result.warnings) {
86 std::cerr << "Solver warning: " << warning << std::endl;
87 }
88 CHECK_EQ(result.termination.reason, TerminationReason::kOptimal)
89 << result.termination;
90 // The following code will print:
91 // objective value: 2.5
92 // value for variable x: 1
93 std::cout << "objective value: " << result.objective_value()
94 << "\nvalue for variable x: " << result.variable_values().at(x)
95 << std::endl;
96}
97} // namespace
98
99int main(int argc, char** argv) {
101 absl::ParseCommandLine(argc, argv);
102 SolveVersion1();
103 SolveVersion2();
104 return 0;
105}
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:702
int main(int argc, char **argv)
GRBmodel * model
void InitGoogleLogging(const char *argv0)
absl::StatusOr< SolveResult > Solve(const Model &model, const SolverType solver_type, const SolveArguments &solve_args, const SolverInitArguments &init_args)
Definition: solve.cc:155