forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
lp_test.cc
232 lines (199 loc) · 8.56 KB
/
lp_test.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2010-2024 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.
// Linear programming example that shows how to use the API.
#include "ortools/base/init_google.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 SolveAndPrint(MPSolver& solver, std::vector<MPVariable*> variables,
std::vector<MPConstraint*> constraints, bool is_continuous) {
LOG(INFO) << "Number of variables = " << solver.NumVariables();
LOG(INFO) << "Number of constraints = " << solver.NumConstraints();
const MPSolver::ResultStatus result_status = solver.Solve();
// Check that the problem has an optimal solution.
if (result_status != MPSolver::OPTIMAL) {
LOG(FATAL) << "The problem does not have an optimal solution!";
}
LOG(INFO) << "Solution:";
for (const auto& i : variables) {
LOG(INFO) << i->name() << " = " << i->solution_value();
}
LOG(INFO) << "Optimal objective value = " << solver.Objective().Value();
LOG(INFO) << "";
LOG(INFO) << "Advanced usage:";
LOG(INFO) << "Problem solved in " << solver.wall_time() << " milliseconds";
if (solver.ProblemType() != MPSolver::BOP_INTEGER_PROGRAMMING)
LOG(INFO) << "Problem solved in " << solver.iterations() << " iterations";
if (is_continuous) {
for (const auto& i : variables) {
LOG(INFO) << i->name() << ": reduced cost " << i->reduced_cost();
}
const std::vector<double> activities = solver.ComputeConstraintActivities();
for (const auto& i : constraints) {
LOG(INFO) << i->name() << ": dual value = " << i->dual_value()
<< " activity = " << activities[i->index()];
}
}
}
void RunLinearProgrammingExample(
MPSolver::OptimizationProblemType optimization_problem_type) {
MPSolver solver("LinearProgrammingExample", optimization_problem_type);
const double infinity = solver.infinity();
// x and y are continuous non-negative variables.
MPVariable* const x = solver.MakeNumVar(0.0, infinity, "x");
MPVariable* const y = solver.MakeNumVar(0.0, infinity, "y");
// Objectif function: Maximize 3x + 4y.
MPObjective* const objective = solver.MutableObjective();
objective->SetCoefficient(x, 3);
objective->SetCoefficient(y, 4);
objective->SetMaximization();
// x + 2y <= 14.
MPConstraint* const c0 = solver.MakeRowConstraint(-infinity, 14.0, "c0");
c0->SetCoefficient(x, 1);
c0->SetCoefficient(y, 2);
// 3x - y >= 0.
MPConstraint* const c1 = solver.MakeRowConstraint(0.0, infinity, "c1");
c1->SetCoefficient(x, 3);
c1->SetCoefficient(y, -1);
// x - y <= 2.
MPConstraint* const c2 = solver.MakeRowConstraint(-infinity, 2.0, "c2");
c2->SetCoefficient(x, 1);
c2->SetCoefficient(y, -1);
SolveAndPrint(solver, {x, y}, {c0, c1, c2}, true);
}
void RunMixedIntegerProgrammingExample(
MPSolver::OptimizationProblemType optimization_problem_type) {
MPSolver solver("MixedIntegerProgrammingExample", optimization_problem_type);
const double infinity = solver.infinity();
// x and y are integers non-negative variables.
MPVariable* const x = solver.MakeIntVar(0.0, infinity, "x");
MPVariable* const y = solver.MakeIntVar(0.0, infinity, "y");
// Objective function: Maximize x + 10 * y.
MPObjective* const objective = solver.MutableObjective();
objective->SetCoefficient(x, 1);
objective->SetCoefficient(y, 10);
objective->SetMaximization();
// x + 7 * y <= 17.5
MPConstraint* const c0 = solver.MakeRowConstraint(-infinity, 17.5, "c0");
c0->SetCoefficient(x, 1);
c0->SetCoefficient(y, 7);
// x <= 3.5
MPConstraint* const c1 = solver.MakeRowConstraint(-infinity, 3.5, "c1");
c1->SetCoefficient(x, 1);
c1->SetCoefficient(y, 0);
SolveAndPrint(solver, {x, y}, {c0, c1}, false);
}
void RunBooleanProgrammingExample(
MPSolver::OptimizationProblemType optimization_problem_type) {
MPSolver solver("BooleanProgrammingExample", optimization_problem_type);
const double infinity = solver.infinity();
// x and y are boolean variables.
MPVariable* const x = solver.MakeBoolVar("x");
MPVariable* const y = solver.MakeBoolVar("y");
// Objective function: Minimize 2 * x + y.
MPObjective* const objective = solver.MutableObjective();
objective->SetCoefficient(x, 2);
objective->SetCoefficient(y, 1);
objective->SetMinimization();
// 1 <= x + 2 * y <= 3.
MPConstraint* const c0 = solver.MakeRowConstraint(1, 3, "c0");
c0->SetCoefficient(x, 1);
c0->SetCoefficient(y, 2);
SolveAndPrint(solver, {x, y}, {c0}, false);
}
void MutableObjectiveCrash() {
LOG(INFO) << "MutableObjectiveCrash";
// Create the linear solver with the GLOP backend.
std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("GLOP"));
// Create the variables x and y.
MPVariable* const x = solver->MakeNumVar(0.0, 1, "x");
MPVariable* const y = solver->MakeNumVar(0.0, 2, "y");
LOG(INFO) << "Number of variables = " << solver->NumVariables();
// Create a linear constraint, 0 <= x + y <= 2.
MPConstraint* const ct = solver->MakeRowConstraint(0.0, 2.0, "ct");
ct->SetCoefficient(x, 1);
ct->SetCoefficient(y, 1);
LOG(INFO) << "Number of constraints = " << solver->NumConstraints();
// Create the objective function, 3 * x + y.
MPObjective* const objective = solver->MutableObjective();
objective->SetCoefficient(x, 3);
objective->SetCoefficient(y, 1);
objective->SetMaximization();
solver->Solve();
LOG(INFO) << "Solution:" << std::endl;
LOG(INFO) << "Objective value = " << objective->Value();
LOG(INFO) << "x = " << x->solution_value();
LOG(INFO) << "y = " << y->solution_value();
}
void RunAllExamples() {
// Linear programming problems
#if defined(USE_CLP)
LOG(INFO) << "---- Linear programming example with CLP ----";
RunLinearProgrammingExample(MPSolver::CLP_LINEAR_PROGRAMMING);
#endif // USE_CLP
#if defined(USE_GLPK)
LOG(INFO) << "---- Linear programming example with GLPK ----";
RunLinearProgrammingExample(MPSolver::GLPK_LINEAR_PROGRAMMING);
#endif // USE_GLPK
#if defined(USE_GLOP)
LOG(INFO) << "---- Linear programming example with GLOP ----";
RunLinearProgrammingExample(MPSolver::GLOP_LINEAR_PROGRAMMING);
#endif // USE_GLOP
#if defined(USE_PDLP)
LOG(INFO) << "---- Linear programming example with PDLP ----";
RunLinearProgrammingExample(MPSolver::PDLP_LINEAR_PROGRAMMING);
#endif // USE_PDLP
#if defined(USE_GUROBI)
LOG(INFO) << "---- Linear programming example with Gurobi ----";
RunLinearProgrammingExample(MPSolver::GUROBI_LINEAR_PROGRAMMING);
#endif // USE_GUROBI
#if defined(USE_CPLEX)
LOG(INFO) << "---- Linear programming example with CPLEX ----";
RunLinearProgrammingExample(MPSolver::CPLEX_LINEAR_PROGRAMMING);
#endif // USE_CPLEX
// Integer programming problems
#if defined(USE_SCIP)
LOG(INFO) << "---- Mixed Integer programming example with SCIP ----";
RunMixedIntegerProgrammingExample(MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
#endif // USE_SCIP
#if defined(USE_GLPK)
LOG(INFO) << "---- Mixed Integer programming example with GLPK ----";
RunMixedIntegerProgrammingExample(MPSolver::GLPK_MIXED_INTEGER_PROGRAMMING);
#endif // USE_GLPK
#if defined(USE_CBC)
LOG(INFO) << "---- Mixed Integer programming example with CBC ----";
RunMixedIntegerProgrammingExample(MPSolver::CBC_MIXED_INTEGER_PROGRAMMING);
#endif // USE_CBC
#if defined(USE_GUROBI)
LOG(INFO) << "---- Mixed Integer programming example with GUROBI ----";
RunMixedIntegerProgrammingExample(MPSolver::GUROBI_MIXED_INTEGER_PROGRAMMING);
#endif // USE_GUROBI
#if defined(USE_CPLEX)
LOG(INFO) << "---- Mixed Integer programming example with CPLEX ----";
RunMixedIntegerProgrammingExample(MPSolver::CPLEX_MIXED_INTEGER_PROGRAMMING);
#endif // USE_CPLEX
// Boolean integer programming problems
#if defined(USE_BOP)
LOG(INFO) << "---- Boolean Integer programming example with BOP ----";
RunBooleanProgrammingExample(MPSolver::BOP_INTEGER_PROGRAMMING);
#endif // USE_BOP
MutableObjectiveCrash();
}
} // namespace operations_research
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
absl::SetFlag(&FLAGS_stderrthreshold, 0);
operations_research::RunAllExamples();
return 0;
}