OR-Tools  9.6
integer_programming_mo.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 integer programming example
15 
16 #include <iostream>
17 #include <limits>
18 
19 #include "absl/status/statusor.h"
20 #include "absl/time/time.h"
22 #include "ortools/base/logging.h"
26 
27 namespace {
28 
29 namespace math_opt = ::operations_research::math_opt;
30 
31 constexpr double kInf = std::numeric_limits<double>::infinity();
32 
33 // Model and solve the problem:
34 // max x + 10 * y
35 // s.t. x + 7 * y <= 17.5
36 // x <= 3.5
37 // x in {0.0, 1.0, 2.0, ...,
38 // y in {0.0, 1.0, 2.0, ...,
39 //
40 absl::Status Main() {
41  math_opt::Model model("Integer programming example");
42 
43  // Variables
44  const math_opt::Variable x = model.AddIntegerVariable(0.0, kInf, "x");
45  const math_opt::Variable y = model.AddIntegerVariable(0.0, kInf, "y");
46 
47  // Constraints
48  model.AddLinearConstraint(x + 7 * y <= 17.5, "c1");
49  model.AddLinearConstraint(x <= 3.5, "c2");
50 
51  // Objective
52  model.Maximize(x + 10 * y);
53 
55  Solve(model, math_opt::SolverType::kGscip));
56 
57  switch (result.termination.reason) {
58  case math_opt::TerminationReason::kOptimal:
59  case math_opt::TerminationReason::kFeasible:
60  std::cout << "Problem solved in " << result.solve_time() << std::endl;
61  std::cout << "Objective value: " << result.objective_value() << std::endl;
62  std::cout << "Variable values: [x=" << result.variable_values().at(x)
63  << ", y=" << result.variable_values().at(y) << "]" << std::endl;
64  return absl::OkStatus();
65  default:
67  << "model failed to solve: " << result.termination;
68  }
69 }
70 } // namespace
71 
72 int main(int argc, char** argv) {
73  InitGoogle(argv[0], &argc, &argv, true);
74  const absl::Status status = Main();
75  if (!status.ok()) {
76  LOG(QFATAL) << status;
77  }
78  return 0;
79 }
#define ASSIGN_OR_RETURN(lhs, rexpr)
absl::Status status
Definition: g_gurobi.cc:41
GRBmodel * model
void InitGoogle(const char *usage, int *argc, char ***argv, bool deprecated)
Definition: init_google.h:34
int main(int argc, char **argv)
absl::StatusOr< SolveResult > Solve(const Model &model, const SolverType solver_type, const SolveArguments &solve_args, const SolverInitArguments &init_args)
StatusBuilder InternalErrorBuilder()
const VariableMap< double > & variable_values() const