forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_solver_protocol_buffers.cc
107 lines (95 loc) · 3.97 KB
/
linear_solver_protocol_buffers.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include "absl/flags/parse.h"
#include "absl/flags/usage.h"
#include "ortools/base/logging.h"
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/linear_solver/linear_solver.pb.h"
namespace operations_research {
void BuildLinearProgrammingMaxExample(MPSolver::OptimizationProblemType type) {
const double kObjCoef[] = {10.0, 6.0, 4.0};
const std::string kVarName[] = {"x1", "x2", "x3"};
const int numVars = 3;
const int kNumConstraints = 3;
const std::string kConstraintName[] = {"c1", "c2", "c3"};
const double kConstraintCoef1[] = {1.0, 1.0, 1.0};
const double kConstraintCoef2[] = {10.0, 4.0, 5.0};
const double kConstraintCoef3[] = {2.0, 2.0, 6.0};
const double* kConstraintCoef[] = {kConstraintCoef1, kConstraintCoef2,
kConstraintCoef3};
const double kConstraintUb[] = {100.0, 600.0, 300.0};
const double infinity = MPSolver::infinity();
MPModelProto model_proto;
model_proto.set_name("Max_Example");
// Create variables and objective function
for (int j = 0; j < numVars; ++j) {
MPVariableProto* x = model_proto.add_variable();
x->set_name(kVarName[j]); // Could be skipped (optional).
x->set_lower_bound(0.0);
x->set_upper_bound(infinity); // Could be skipped (default value).
x->set_is_integer(false); // Could be skipped (default value).
x->set_objective_coefficient(kObjCoef[j]);
}
model_proto.set_maximize(true);
// Create constraints
for (int i = 0; i < kNumConstraints; ++i) {
MPConstraintProto* constraint_proto = model_proto.add_constraint();
constraint_proto->set_name(kConstraintName[i]); // Could be skipped.
constraint_proto->set_lower_bound(-infinity); // Could be skipped.
constraint_proto->set_upper_bound(kConstraintUb[i]);
for (int j = 0; j < numVars; ++j) {
// These two lines may be skipped when the coefficient is zero.
constraint_proto->add_var_index(j);
constraint_proto->add_coefficient(kConstraintCoef[i][j]);
}
}
MPModelRequest model_request;
*model_request.mutable_model() = model_proto;
#if defined(USE_GLOP)
if (type == MPSolver::GLOP_LINEAR_PROGRAMMING) {
model_request.set_solver_type(MPModelRequest::GLOP_LINEAR_PROGRAMMING);
}
#endif // USE_GLOP
#if defined(USE_CLP)
if (type == MPSolver::CLP_LINEAR_PROGRAMMING) {
model_request.set_solver_type(MPModelRequest::CLP_LINEAR_PROGRAMMING);
}
#endif // USE_CLP
MPSolutionResponse solution_response;
MPSolver::SolveWithProto(model_request, &solution_response);
// The problem has an optimal solution.
CHECK_EQ(MPSOLVER_OPTIMAL, solution_response.status());
LOG(INFO) << "objective = " << solution_response.objective_value();
for (int j = 0; j < numVars; ++j) {
LOG(INFO) << model_proto.variable(j).name() << " = "
<< solution_response.variable_value(j);
}
}
void RunAllExamples() {
#if defined(USE_GLOP)
LOG(INFO) << "----- Running Max Example with GLOP -----";
BuildLinearProgrammingMaxExample(MPSolver::GLOP_LINEAR_PROGRAMMING);
#endif // USE_GLOP
#if defined(USE_CLP)
LOG(INFO) << "----- Running Max Example with Coin LP -----";
BuildLinearProgrammingMaxExample(MPSolver::CLP_LINEAR_PROGRAMMING);
#endif // USE_CLP
}
} // namespace operations_research
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
absl::ParseCommandLine(argc, argv);
operations_research::RunAllExamples();
return EXIT_SUCCESS;
}