OR-Tools  9.1
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::MathOpt;
37 using ::operations_research::math_opt::Objective;
38 using ::operations_research::math_opt::Result;
39 using ::operations_research::math_opt::SolveParametersProto;
40 using ::operations_research::math_opt::SolveResultProto;
41 using ::operations_research::math_opt::Variable;
42
43 MathOpt optimizer(operations_research::math_opt::SOLVER_TYPE_GSCIP,
44 "my_model");
45 const Variable x = optimizer.AddBinaryVariable("x");
46 const Variable y = optimizer.AddContinuousVariable(0.0, 2.5, "y");
47 const LinearConstraint c = optimizer.AddLinearConstraint(
48 -std::numeric_limits<double>::infinity(), 1.5, "c");
49 c.set_coefficient(x, 1.0);
50 c.set_coefficient(y, 1.0);
51 const Objective obj = optimizer.objective();
52 obj.set_linear_coefficient(x, 2.0);
53 obj.set_linear_coefficient(y, 1.0);
54 obj.set_maximize();
55 const Result result = optimizer.Solve(SolveParametersProto()).value();
56 for (const auto& warning : result.warnings) {
57 std::cerr << "Solver warning: " << warning << std::endl;
58 }
59 CHECK_EQ(result.termination_reason, SolveResultProto::OPTIMAL)
60 << result.termination_detail;
61 // The following code will print:
62 // objective value: 2.5
63 // value for variable x: 1
64 std::cout << "objective value: " << result.objective_value()
65 << "\nvalue for variable x: " << result.variable_values().at(x)
66 << std::endl;
67}
68
69void SolveVersion2() {
70 using ::operations_research::math_opt::LinearExpression;
71 using ::operations_research::math_opt::MathOpt;
72 using ::operations_research::math_opt::Result;
73 using ::operations_research::math_opt::SolveParametersProto;
74 using ::operations_research::math_opt::SolveResultProto;
75 using ::operations_research::math_opt::Variable;
76
77 MathOpt optimizer(operations_research::math_opt::SOLVER_TYPE_GSCIP,
78 "my_model");
79 const Variable x = optimizer.AddBinaryVariable("x");
80 const Variable y = optimizer.AddContinuousVariable(0.0, 2.5, "y");
81 // We can directly use linear combinations of variables ...
82 optimizer.AddLinearConstraint(x + y <= 1.5, "c");
83 // ... or build them incrementally.
84 LinearExpression objective_expression;
85 objective_expression += 2 * x;
86 objective_expression += y;
87 optimizer.objective().Maximize(objective_expression);
88 const Result result = optimizer.Solve(SolveParametersProto()).value();
89 for (const auto& warning : result.warnings) {
90 std::cerr << "Solver warning: " << warning << std::endl;
91 }
92 CHECK_EQ(result.termination_reason, SolveResultProto::OPTIMAL)
93 << result.termination_detail;
94 // The following code will print:
95 // objective value: 2.5
96 // value for variable x: 1
97 std::cout << "objective value: " << result.objective_value()
98 << "\nvalue for variable x: " << result.variable_values().at(x)
99 << std::endl;
100}
101} // namespace
102
103int main(int argc, char** argv) {
105 absl::ParseCommandLine(argc, argv);
106 SolveVersion1();
107 SolveVersion2();
108 return 0;
109}
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:698
int main(int argc, char **argv)
void InitGoogleLogging(const char *argv0)