forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
quadratic_program.cc
355 lines (332 loc) · 13.9 KB
/
quadratic_program.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// 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.
#include "ortools/pdlp/quadratic_program.h"
#include <algorithm>
#include <cstdint>
#include <limits>
#include <optional>
#include <string>
#include <tuple>
#include <vector>
#include "Eigen/Core"
#include "Eigen/SparseCore"
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "ortools/base/status_macros.h"
#include "ortools/linear_solver/linear_solver.pb.h"
namespace operations_research::pdlp {
using ::Eigen::VectorXd;
absl::Status ValidateQuadraticProgramDimensions(const QuadraticProgram& qp) {
const int64_t var_lb_size = qp.variable_lower_bounds.size();
const int64_t con_lb_size = qp.constraint_lower_bounds.size();
if (var_lb_size != qp.variable_upper_bounds.size()) {
return absl::InvalidArgumentError(absl::StrCat(
"Inconsistent dimensions: variable lower bound vector has size ",
var_lb_size, " while variable upper bound vector has size ",
qp.variable_upper_bounds.size()));
}
if (var_lb_size != qp.objective_vector.size()) {
return absl::InvalidArgumentError(absl::StrCat(
"Inconsistent dimensions: variable lower bound vector has size ",
var_lb_size, " while objective vector has size ",
qp.objective_vector.size()));
}
if (var_lb_size != qp.constraint_matrix.cols()) {
return absl::InvalidArgumentError(absl::StrCat(
"Inconsistent dimensions: variable lower bound vector has size ",
var_lb_size, " while constraint matrix has ",
qp.constraint_matrix.cols(), " columns"));
}
if (qp.objective_matrix.has_value() &&
var_lb_size != qp.objective_matrix->rows()) {
return absl::InvalidArgumentError(absl::StrCat(
"Inconsistent dimensions: variable lower bound vector has size ",
var_lb_size, " while objective matrix has ",
qp.objective_matrix->rows(), " rows"));
}
if (con_lb_size != qp.constraint_upper_bounds.size()) {
return absl::InvalidArgumentError(absl::StrCat(
"Inconsistent dimensions: constraint lower bound vector has size ",
con_lb_size, " while constraint upper bound vector has size ",
qp.constraint_upper_bounds.size()));
}
if (con_lb_size != qp.constraint_matrix.rows()) {
return absl::InvalidArgumentError(absl::StrCat(
"Inconsistent dimensions: constraint lower bound vector has size ",
con_lb_size, " while constraint matrix has ",
qp.constraint_matrix.rows(), " rows "));
}
return absl::OkStatus();
}
absl::StatusOr<QuadraticProgram> QpFromMpModelProto(
const MPModelProto& proto, bool relax_integer_variables,
bool include_names) {
if (!proto.general_constraint().empty()) {
return absl::InvalidArgumentError("General constraints are not supported.");
}
const int primal_size = proto.variable_size();
const int dual_size = proto.constraint_size();
QuadraticProgram qp(primal_size, dual_size);
if (include_names) {
qp.problem_name = proto.name();
qp.variable_names = std::vector<std::string>(primal_size);
qp.constraint_names = std::vector<std::string>(dual_size);
}
for (int i = 0; i < primal_size; ++i) {
const auto& var = proto.variable(i);
qp.variable_lower_bounds[i] = var.lower_bound();
qp.variable_upper_bounds[i] = var.upper_bound();
qp.objective_vector[i] = var.objective_coefficient();
if (var.is_integer() && !relax_integer_variables) {
return absl::InvalidArgumentError(
"Integer variable encountered with relax_integer_variables == false");
}
if (include_names) {
(*qp.variable_names)[i] = var.name();
}
}
std::vector<int> nonzeros_by_column(primal_size);
for (int i = 0; i < dual_size; ++i) {
const auto& con = proto.constraint(i);
for (int j = 0; j < con.var_index_size(); ++j) {
if (con.var_index(j) < 0 || con.var_index(j) >= primal_size) {
return absl::InvalidArgumentError(absl::StrCat(
"Variable index of ", i, "th constraint's ", j, "th nonzero is ",
con.var_index(j), " which is not in the allowed range [0, ",
primal_size, ")"));
}
nonzeros_by_column[con.var_index(j)]++;
}
qp.constraint_lower_bounds[i] = con.lower_bound();
qp.constraint_upper_bounds[i] = con.upper_bound();
if (include_names) {
(*qp.constraint_names)[i] = con.name();
}
}
// To reduce peak RAM usage we construct the constraint matrix in-place.
// According to the documentation of `SparseMatrix::insert()` it's efficient
// to construct a matrix with insert()s as long as reserve() is called first
// and the non-zeros are inserted in increasing order of inner index.
// The non-zeros in each input constraint may not be sorted so this is only
// efficient with column major format.
static_assert(qp.constraint_matrix.IsRowMajor == 0, "See comment.");
qp.constraint_matrix.reserve(nonzeros_by_column);
for (int i = 0; i < dual_size; ++i) {
const auto& con = proto.constraint(i);
CHECK_EQ(con.var_index_size(), con.coefficient_size())
<< " in " << i << "th constraint";
if (con.var_index_size() != con.coefficient_size()) {
return absl::InvalidArgumentError(
absl::StrCat(i, "th constraint has ", con.coefficient_size(),
" coefficients, expected ", con.var_index_size()));
}
for (int j = 0; j < con.var_index_size(); ++j) {
qp.constraint_matrix.insert(i, con.var_index(j)) = con.coefficient(j);
}
}
if (qp.constraint_matrix.outerSize() > 0) {
qp.constraint_matrix.makeCompressed();
}
// We use triplets-based initialization for the objective matrix because the
// objective non-zeros may be in arbitrary order in the input.
std::vector<Eigen::Triplet<double, int64_t>> triplets;
const auto& quadratic = proto.quadratic_objective();
if (quadratic.qvar1_index_size() != quadratic.qvar2_index_size() ||
quadratic.qvar1_index_size() != quadratic.coefficient_size()) {
return absl::InvalidArgumentError(absl::StrCat(
"The quadratic objective has ", quadratic.qvar1_index_size(),
" qvar1_indices, ", quadratic.qvar2_index_size(),
" qvar2_indices, and ", quadratic.coefficient_size(),
" coefficients, expected equal numbers."));
}
if (quadratic.qvar1_index_size() > 0) {
qp.objective_matrix.emplace();
qp.objective_matrix->setZero(primal_size);
}
for (int i = 0; i < quadratic.qvar1_index_size(); ++i) {
const int index1 = quadratic.qvar1_index(i);
const int index2 = quadratic.qvar2_index(i);
if (index1 < 0 || index2 < 0 || index1 >= primal_size ||
index2 >= primal_size) {
return absl::InvalidArgumentError(absl::StrCat(
"The quadratic objective's ", i, "th nonzero has indices ", index1,
" and ", index2, ", which are not both in the expected range [0, ",
primal_size, ")"));
}
if (index1 != index2) {
return absl::InvalidArgumentError(absl::StrCat(
"The quadratic objective's ", i,
"th nonzero has off-diagonal element at (", index1, ", ", index2,
"). Only diagonal objective matrices are supported."));
}
// Note: `QuadraticProgram` has an implicit "1/2" in front of the quadratic
// term.
qp.objective_matrix->diagonal()[index1] = 2 * quadratic.coefficient(i);
}
qp.objective_offset = proto.objective_offset();
if (proto.maximize()) {
qp.objective_offset *= -1;
qp.objective_vector *= -1;
if (qp.objective_matrix.has_value()) {
qp.objective_matrix->diagonal() *= -1;
}
qp.objective_scaling_factor = -1;
}
return qp;
}
absl::Status CanFitInMpModelProto(const QuadraticProgram& qp) {
return internal::TestableCanFitInMpModelProto(
qp, std::numeric_limits<int32_t>::max());
}
namespace internal {
absl::Status TestableCanFitInMpModelProto(const QuadraticProgram& qp,
const int64_t largest_ok_size) {
const int64_t primal_size = qp.variable_lower_bounds.size();
const int64_t dual_size = qp.constraint_lower_bounds.size();
bool primal_too_big = primal_size > largest_ok_size;
if (primal_too_big) {
return absl::InvalidArgumentError(absl::StrCat(
"Too many variables (", primal_size, ") to index with an int32_t."));
}
bool dual_too_big = dual_size > largest_ok_size;
if (dual_too_big) {
return absl::InvalidArgumentError(absl::StrCat(
"Too many constraints (", dual_size, ") to index with an int32_t."));
}
return absl::OkStatus();
}
} // namespace internal
absl::StatusOr<MPModelProto> QpToMpModelProto(const QuadraticProgram& qp) {
RETURN_IF_ERROR(CanFitInMpModelProto(qp));
if (qp.objective_scaling_factor == 0) {
return absl::InvalidArgumentError(
"objective_scaling_factor cannot be zero.");
}
const int64_t primal_size = qp.variable_lower_bounds.size();
const int64_t dual_size = qp.constraint_lower_bounds.size();
MPModelProto proto;
if (qp.problem_name.has_value() && !qp.problem_name->empty()) {
proto.set_name(*qp.problem_name);
}
proto.set_objective_offset(qp.objective_scaling_factor * qp.objective_offset);
if (qp.objective_scaling_factor < 0) {
proto.set_maximize(true);
} else {
proto.set_maximize(false);
}
proto.mutable_variable()->Reserve(primal_size);
for (int64_t i = 0; i < primal_size; ++i) {
auto* var = proto.add_variable();
var->set_lower_bound(qp.variable_lower_bounds[i]);
var->set_upper_bound(qp.variable_upper_bounds[i]);
var->set_objective_coefficient(qp.objective_scaling_factor *
qp.objective_vector[i]);
if (qp.variable_names.has_value() && i < qp.variable_names->size()) {
const std::string& name = (*qp.variable_names)[i];
if (!name.empty()) {
var->set_name(name);
}
}
}
proto.mutable_constraint()->Reserve(dual_size);
for (int64_t i = 0; i < dual_size; ++i) {
auto* con = proto.add_constraint();
con->set_lower_bound(qp.constraint_lower_bounds[i]);
con->set_upper_bound(qp.constraint_upper_bounds[i]);
if (qp.constraint_names.has_value() && i < qp.constraint_names->size()) {
const std::string& name = (*qp.constraint_names)[i];
if (!name.empty()) {
con->set_name(name);
}
}
}
using InnerIterator =
::Eigen::SparseMatrix<double, Eigen::ColMajor, int64_t>::InnerIterator;
for (int64_t col = 0; col < qp.constraint_matrix.cols(); ++col) {
for (InnerIterator iter(qp.constraint_matrix, col); iter; ++iter) {
auto* con = proto.mutable_constraint(iter.row());
// To avoid reallocs during the inserts, we could count the nonzeros
// and `reserve()` before filling.
con->add_var_index(iter.col());
con->add_coefficient(iter.value());
}
}
// Some OR tools decide the objective is quadratic based on
// `has_quadratic_objective()` rather than on
// `quadratic_objective_size() == 0`, so don't create the quadratic objective
// for linear programs.
if (!IsLinearProgram(qp)) {
auto* quadratic_objective = proto.mutable_quadratic_objective();
const auto& diagonal = qp.objective_matrix->diagonal();
for (int64_t i = 0; i < diagonal.size(); ++i) {
if (diagonal[i] != 0.0) {
quadratic_objective->add_qvar1_index(i);
quadratic_objective->add_qvar2_index(i);
// Undo the implicit (1/2) term in `QuadraticProgram`'s objective.
quadratic_objective->add_coefficient(qp.objective_scaling_factor *
diagonal[i] / 2.0);
}
}
}
return proto;
}
void SetEigenMatrixFromTriplets(
std::vector<Eigen::Triplet<double, int64_t>> triplets,
Eigen::SparseMatrix<double, Eigen::ColMajor, int64_t>& matrix) {
using Triplet = Eigen::Triplet<double, int64_t>;
std::sort(triplets.begin(), triplets.end(),
[](const Triplet& lhs, const Triplet& rhs) {
return std::tie(lhs.col(), lhs.row()) <
std::tie(rhs.col(), rhs.row());
});
// The triplets are allowed to contain duplicate entries (and intentionally
// do for the diagonals of the objective matrix). For efficiency of insert and
// reserve, merge the duplicates first.
internal::CombineRepeatedTripletsInPlace(triplets);
std::vector<int64_t> num_column_entries(matrix.cols());
for (const Triplet& triplet : triplets) {
++num_column_entries[triplet.col()];
}
// NOTE: `reserve()` takes column counts because matrix is in column major
// order.
matrix.reserve(num_column_entries);
for (const Triplet& triplet : triplets) {
matrix.insert(triplet.row(), triplet.col()) = triplet.value();
}
if (matrix.outerSize() > 0) {
matrix.makeCompressed();
}
}
namespace internal {
void CombineRepeatedTripletsInPlace(
std::vector<Eigen::Triplet<double, int64_t>>& triplets) {
if (triplets.empty()) return;
auto output_iter = triplets.begin();
for (auto p = output_iter + 1; p != triplets.end(); ++p) {
if (output_iter->row() == p->row() && output_iter->col() == p->col()) {
*output_iter = {output_iter->row(), output_iter->col(),
output_iter->value() + p->value()};
} else {
++output_iter;
if (output_iter != p) { // Small optimization - skip no-op copies.
*output_iter = *p;
}
}
}
// `*output_iter` is the last output value, so erase everything after that.
triplets.erase(output_iter + 1, triplets.end());
}
} // namespace internal
} // namespace operations_research::pdlp