Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cpp_example.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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// Demonstrates how to call the MathOpt C API defined in solver.h from C++.
15//
16// At a high level, the example:
17// * builds a ModelProto in C++,
18// * serializes the model to binary,
19// * calls MathOptSolve() from the C-API on the model binary, which outputs a
20// SolveResultProto in binary,
21// * parses a C++ SolveResultProto from the binary,
22// * prints some key parts of the SolveResultProto.
23//
24// Actual C++ users should use MathOpt's various C++ APIs. This is just a
25// demonstration of how the C API is intended to be used (from any language that
26// an interoperate with C).
27
28#include <cstddef>
29#include <cstdlib>
30#include <iostream>
31#include <string>
32
33#include "absl/status/status.h"
36#include "ortools/math_opt/model.pb.h"
37#include "ortools/math_opt/parameters.pb.h"
38#include "ortools/math_opt/result.pb.h"
39#include "ortools/math_opt/sparse_containers.pb.h"
40
41// This example solves the optimization problem:
42// max x
43// x in [0, 1]
44// and then prints out the termination reason and objective value.
45int main(int argc, char** argv) {
46 InitGoogle(argv[0], &argc, &argv, true);
47
48 // Create a serialized ModelProto for the problem.
49 operations_research::math_opt::ModelProto model;
50 model.mutable_variables()->add_ids(0);
51 model.mutable_variables()->add_lower_bounds(0.0);
52 model.mutable_variables()->add_upper_bounds(1.0);
53 model.mutable_variables()->add_names("x");
54 model.mutable_variables()->add_integers(false);
55 model.mutable_objective()->set_maximize(true);
56 model.mutable_objective()->mutable_linear_coefficients()->add_ids(0);
57 model.mutable_objective()->mutable_linear_coefficients()->add_values(1.0);
58 const std::string model_str = model.SerializeAsString();
59 const void* model_bin = model_str.data();
60 const size_t model_bin_size = model_str.size();
61
62 // Pick a solver.
63 const int solver_type =
64 static_cast<int>(operations_research::math_opt::SOLVER_TYPE_GLOP);
65
66 // Set up the output arguments for MathOptSolve()
67 void* result_bin = nullptr;
68 size_t result_bin_size = 0;
69 char* status_msg = nullptr;
70
71 // Call the C API to do solve the model and populate the output arguments.
72 const int status_code = MathOptSolve(model_bin, model_bin_size, solver_type,
73 /*interrupter=*/nullptr, &result_bin,
74 &result_bin_size, &status_msg);
75
76 // If MathOptSolve() failed, print the error and abort.
77 if (status_code != 0) {
78 std::cerr << absl::Status(static_cast<absl::StatusCode>(status_code),
79 status_msg)
80 << std::endl;
81 // If you handle the error instead of crashing, be sure to free status_msg.
82 std::abort();
83 }
84
85 // Recover the SolveResultProto from the output arguments (stored as a
86 // serialized proto).
87 operations_research::math_opt::SolveResultProto result;
88 if (!result.ParseFromArray(result_bin, static_cast<int>(result_bin_size))) {
89 std::cout << "failed to parse SolveResultProto" << std::endl;
90 std::abort();
91 }
92
93 // Print out the desired output.
94 std::cout << "Termination is optimal: "
95 << (result.termination().reason() ==
96 operations_research::math_opt::TERMINATION_REASON_OPTIMAL)
97 << std::endl;
98 std::cout << "Objective value: "
99 << result.termination().objective_bounds().primal_bound()
100 << std::endl;
101
102 // Clean up any memory allocated by MathOptSolve(). Note that invoking these
103 // functions on nullptr is safe.
104 MathOptFree(result_bin);
105 MathOptFree(status_msg);
106 return 0;
107}
void MathOptFree(void *ptr)
Definition solver.cc:147
int MathOptSolve(const void *model, const size_t model_size, const int solver_type, MathOptInterrupter *const interrupter, void **solve_result, size_t *solve_result_size, char **status_msg)
Definition solver.cc:112
int main(int argc, char **argv)
GRBmodel * model
void InitGoogle(const char *usage, int *argc, char ***argv, bool deprecated)
Definition init_google.h:34