forked from NVIDIA/cuopt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_model_view.cpp
More file actions
363 lines (310 loc) · 11.8 KB
/
data_model_view.cpp
File metadata and controls
363 lines (310 loc) · 11.8 KB
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
356
357
358
359
360
361
362
363
/* clang-format off */
/*
* SPDX-FileCopyrightText: Copyright (c) 2023-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* clang-format on */
#include <mps_parser/data_model_view.hpp>
#include <mps_parser/utilities/span.hpp>
#include <utilities/error.hpp>
namespace cuopt::mps_parser {
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_maximize(bool maximize)
{
maximize_ = maximize;
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_csr_constraint_matrix(const f_t* A_values,
i_t size_values,
const i_t* A_indices,
i_t size_indices,
const i_t* A_offsets,
i_t size_offsets)
{
if (size_values != 0) {
mps_parser_expects(
A_values != nullptr, error_type_t::ValidationError, "A_values cannot be null");
}
A_ = span<f_t const>(A_values, size_values);
if (size_indices != 0) {
mps_parser_expects(
A_indices != nullptr, error_type_t::ValidationError, "A_indices cannot be null");
}
A_indices_ = span<i_t const>(A_indices, size_indices);
mps_parser_expects(
A_offsets != nullptr, error_type_t::ValidationError, "A_offsets cannot be null");
mps_parser_expects(
size_offsets > 0, error_type_t::ValidationError, "size_offsets cannot be empty");
A_offsets_ = span<i_t const>(A_offsets, size_offsets);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_constraint_bounds(const f_t* b, i_t size)
{
if (size != 0) {
mps_parser_expects(b != nullptr, error_type_t::ValidationError, "b cannot be null");
}
b_ = span<f_t const>(b, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_objective_coefficients(const f_t* c, i_t size)
{
if (size != 0) {
mps_parser_expects(c != nullptr, error_type_t::ValidationError, "c cannot be null");
}
c_ = span<f_t const>(c, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_objective_scaling_factor(f_t objective_scaling_factor)
{
objective_scaling_factor_ = objective_scaling_factor;
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_objective_offset(f_t objective_offset)
{
objective_offset_ = objective_offset;
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_variable_lower_bounds(const f_t* variable_lower_bounds,
i_t size)
{
mps_parser_expects(variable_lower_bounds != nullptr,
error_type_t::ValidationError,
"data model variable_lower_bounds cannot be null");
variable_lower_bounds_ = span<f_t const>(variable_lower_bounds, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_variable_upper_bounds(const f_t* variable_upper_bounds,
i_t size)
{
mps_parser_expects(variable_upper_bounds != nullptr,
error_type_t::ValidationError,
"variable_upper_bounds cannot be null");
variable_upper_bounds_ = span<f_t const>(variable_upper_bounds, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_variable_types(const char* variable_types, i_t size)
{
mps_parser_expects(
variable_types != nullptr, error_type_t::ValidationError, "variable_types cannot be null");
variable_types_ = span<char const>(variable_types, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_constraint_lower_bounds(const f_t* constraint_lower_bounds,
i_t size)
{
mps_parser_expects(constraint_lower_bounds != nullptr,
error_type_t::ValidationError,
"constraint_lower_bounds cannot be null");
constraint_lower_bounds_ = span<f_t const>(constraint_lower_bounds, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_constraint_upper_bounds(const f_t* constraint_upper_bounds,
i_t size)
{
mps_parser_expects(constraint_upper_bounds != nullptr,
error_type_t::ValidationError,
"constraint_upper_bounds cannot be null");
constraint_upper_bounds_ = span<f_t const>(constraint_upper_bounds, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_initial_primal_solution(const f_t* initial_primal_solution,
i_t size)
{
mps_parser_expects(initial_primal_solution != nullptr,
error_type_t::ValidationError,
"initial_primal_solution cannot be null");
initial_primal_solution_ = span<f_t const>(initial_primal_solution, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_initial_dual_solution(const f_t* initial_dual_solution,
i_t size)
{
mps_parser_expects(initial_dual_solution != nullptr,
error_type_t::ValidationError,
"initial_dual_solution cannot be null");
initial_dual_solution_ = span<f_t const>(initial_dual_solution, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_quadratic_objective_matrix(const f_t* Q_values,
i_t size_values,
const i_t* Q_indices,
i_t size_indices,
const i_t* Q_offsets,
i_t size_offsets,
const bool is_symmetrized)
{
if (size_values != 0) {
mps_parser_expects(
Q_values != nullptr, error_type_t::ValidationError, "Q_values cannot be null");
}
Q_objective_ = span<f_t const>(Q_values, size_values);
if (size_indices != 0) {
mps_parser_expects(
Q_indices != nullptr, error_type_t::ValidationError, "Q_indices cannot be null");
}
Q_objective_indices_ = span<i_t const>(Q_indices, size_indices);
mps_parser_expects(
Q_offsets != nullptr, error_type_t::ValidationError, "Q_offsets cannot be null");
mps_parser_expects(
size_offsets > 0, error_type_t::ValidationError, "size_offsets cannot be empty");
Q_objective_offsets_ = span<i_t const>(Q_offsets, size_offsets);
is_Q_symmetrized_ = is_symmetrized;
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_row_types(const char* row_types, i_t size)
{
mps_parser_expects(
row_types != nullptr, error_type_t::ValidationError, "row_types cannot be null");
row_types_ = span<char const>(row_types, size);
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_objective_name(const std::string& objective_name)
{
objective_name_ = objective_name;
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_problem_name(const std::string& problem_name)
{
problem_name_ = problem_name;
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_variable_names(
const std::vector<std::string>& variables_names)
{
variable_names_ = variables_names;
}
template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_row_names(const std::vector<std::string>& row_names)
{
row_names_ = row_names;
}
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_constraint_matrix_values() const noexcept
{
return A_;
}
template <typename i_t, typename f_t>
span<const i_t> data_model_view_t<i_t, f_t>::get_constraint_matrix_indices() const noexcept
{
return A_indices_;
}
template <typename i_t, typename f_t>
span<const i_t> data_model_view_t<i_t, f_t>::get_constraint_matrix_offsets() const noexcept
{
return A_offsets_;
}
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_constraint_bounds() const noexcept
{
return b_;
}
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_objective_coefficients() const noexcept
{
return c_;
}
template <typename i_t, typename f_t>
f_t data_model_view_t<i_t, f_t>::get_objective_scaling_factor() const noexcept
{
return objective_scaling_factor_;
}
template <typename i_t, typename f_t>
f_t data_model_view_t<i_t, f_t>::get_objective_offset() const noexcept
{
return objective_offset_;
}
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_variable_lower_bounds() const noexcept
{
return variable_lower_bounds_;
}
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_variable_upper_bounds() const noexcept
{
return variable_upper_bounds_;
}
template <typename i_t, typename f_t>
span<const char> data_model_view_t<i_t, f_t>::get_variable_types() const noexcept
{
return variable_types_;
}
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_constraint_lower_bounds() const noexcept
{
return constraint_lower_bounds_;
}
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_constraint_upper_bounds() const noexcept
{
return constraint_upper_bounds_;
}
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_initial_primal_solution() const noexcept
{
return initial_primal_solution_;
}
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_initial_dual_solution() const noexcept
{
return initial_dual_solution_;
}
template <typename i_t, typename f_t>
span<const char> data_model_view_t<i_t, f_t>::get_row_types() const noexcept
{
return row_types_;
}
template <typename i_t, typename f_t>
std::string data_model_view_t<i_t, f_t>::get_objective_name() const noexcept
{
return objective_name_;
}
template <typename i_t, typename f_t>
std::string data_model_view_t<i_t, f_t>::get_problem_name() const noexcept
{
return problem_name_;
}
template <typename i_t, typename f_t>
bool data_model_view_t<i_t, f_t>::get_sense() const noexcept
{
return maximize_;
}
template <typename i_t, typename f_t>
const std::vector<std::string>& data_model_view_t<i_t, f_t>::get_variable_names() const noexcept
{
return variable_names_;
}
template <typename i_t, typename f_t>
const std::vector<std::string>& data_model_view_t<i_t, f_t>::get_row_names() const noexcept
{
return row_names_;
}
// QPS-specific getter implementations
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_quadratic_objective_values() const noexcept
{
return Q_objective_;
}
template <typename i_t, typename f_t>
span<const i_t> data_model_view_t<i_t, f_t>::get_quadratic_objective_indices() const noexcept
{
return Q_objective_indices_;
}
template <typename i_t, typename f_t>
span<const i_t> data_model_view_t<i_t, f_t>::get_quadratic_objective_offsets() const noexcept
{
return Q_objective_offsets_;
}
template <typename i_t, typename f_t>
bool data_model_view_t<i_t, f_t>::has_quadratic_objective() const noexcept
{
return Q_objective_.size() > 0;
}
template <typename i_t, typename f_t>
bool data_model_view_t<i_t, f_t>::is_Q_symmetrized() const noexcept
{
return is_Q_symmetrized_;
}
// NOTE: Explicitly instantiate all types here in order to avoid linker error
template class data_model_view_t<int, float>;
template class data_model_view_t<int, double>;
} // namespace cuopt::mps_parser