-
Notifications
You must be signed in to change notification settings - Fork 67
/
nodebipartite.cpp
400 lines (351 loc) · 13 KB
/
nodebipartite.cpp
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <array>
#include <cmath>
#include <cstddef>
#include <limits>
#include <type_traits>
#include <scip/scip.h>
#include <scip/struct_lp.h>
#include <xtensor/xview.hpp>
#include "ecole/observation/nodebipartite.hpp"
#include "ecole/scip/model.hpp"
#include "ecole/scip/row.hpp"
namespace ecole::observation {
namespace {
/*********************
* Common helpers *
*********************/
using xmatrix = decltype(NodeBipartiteObs::column_features);
using value_type = xmatrix::value_type;
using ColumnFeatures = NodeBipartiteObs::ColumnFeatures;
using RowFeatures = NodeBipartiteObs::RowFeatures;
value_type constexpr cste = 5.;
value_type constexpr nan = std::numeric_limits<value_type>::quiet_NaN();
SCIP_Real obj_l2_norm(SCIP* const scip) noexcept {
auto const norm = SCIPgetObjNorm(scip);
return norm > 0 ? norm : 1.;
}
/******************************************
* Column features extraction functions *
******************************************/
std::optional<SCIP_Real> upper_bound(SCIP* const scip, SCIP_COL* const col) noexcept {
auto const ub_val = SCIPcolGetUb(col);
if (SCIPisInfinity(scip, std::abs(ub_val))) {
return {};
}
return ub_val;
}
std::optional<SCIP_Real> lower_bound(SCIP* const scip, SCIP_COL* const col) noexcept {
auto const lb_val = SCIPcolGetLb(col);
if (SCIPisInfinity(scip, std::abs(lb_val))) {
return {};
}
return lb_val;
}
bool is_prim_sol_at_lb(SCIP* const scip, SCIP_COL* const col) noexcept {
auto const lb_val = lower_bound(scip, col);
if (lb_val) {
return SCIPisEQ(scip, SCIPcolGetPrimsol(col), lb_val.value());
}
return false;
}
bool is_prim_sol_at_ub(SCIP* const scip, SCIP_COL* const col) noexcept {
auto const ub_val = upper_bound(scip, col);
if (ub_val) {
return SCIPisEQ(scip, SCIPcolGetPrimsol(col), ub_val.value());
}
return false;
}
std::optional<SCIP_Real> best_sol_val(SCIP* const scip, SCIP_VAR* const var) noexcept {
auto* const sol = SCIPgetBestSol(scip);
if (sol != nullptr) {
return SCIPgetSolVal(scip, sol, var);
}
return {};
}
std::optional<SCIP_Real> avg_sol(SCIP* const scip, SCIP_VAR* const var) noexcept {
if (SCIPgetBestSol(scip) != nullptr) {
return SCIPvarGetAvgSol(var);
}
return {};
}
std::optional<SCIP_Real> feas_frac(SCIP* const scip, SCIP_VAR* const var, SCIP_COL* const col) noexcept {
if (SCIPvarGetType(var) == SCIP_VARTYPE_CONTINUOUS) {
return {};
}
return SCIPfeasFrac(scip, SCIPcolGetPrimsol(col));
}
/** Convert an enum to its underlying index. */
template <typename E> constexpr auto idx(E e) {
return static_cast<std::underlying_type_t<E>>(e);
}
template <typename Features>
void set_static_features_for_col(Features&& out, SCIP_VAR* const var, SCIP_COL* const col, value_type obj_norm) {
out[idx(ColumnFeatures::objective)] = SCIPcolGetObj(col) / obj_norm;
// On-hot enconding of varaible type
out[idx(ColumnFeatures::is_type_binary)] = 0.;
out[idx(ColumnFeatures::is_type_integer)] = 0.;
out[idx(ColumnFeatures::is_type_implicit_integer)] = 0.;
out[idx(ColumnFeatures::is_type_continuous)] = 0.;
switch (SCIPvarGetType(var)) {
case SCIP_VARTYPE_BINARY:
out[idx(ColumnFeatures::is_type_binary)] = 1.;
break;
case SCIP_VARTYPE_INTEGER:
out[idx(ColumnFeatures::is_type_integer)] = 1.;
break;
case SCIP_VARTYPE_IMPLINT:
out[idx(ColumnFeatures::is_type_implicit_integer)] = 1.;
break;
case SCIP_VARTYPE_CONTINUOUS:
out[idx(ColumnFeatures::is_type_continuous)] = 1.;
break;
default:
assert(false); // All enum cases must be handled
}
}
template <typename Features>
void set_dynamic_features_for_col(
Features&& out,
SCIP* const scip,
SCIP_VAR* const var,
SCIP_COL* const col,
value_type obj_norm,
value_type n_lps) {
out[idx(ColumnFeatures::has_lower_bound)] = static_cast<value_type>(lower_bound(scip, col).has_value());
out[idx(ColumnFeatures::has_upper_bound)] = static_cast<value_type>(upper_bound(scip, col).has_value());
out[idx(ColumnFeatures::normed_reduced_cost)] = SCIPgetColRedcost(scip, col) / obj_norm;
out[idx(ColumnFeatures::solution_value)] = SCIPcolGetPrimsol(col);
out[idx(ColumnFeatures::solution_frac)] = feas_frac(scip, var, col).value_or(0.);
out[idx(ColumnFeatures::is_solution_at_lower_bound)] = static_cast<value_type>(is_prim_sol_at_lb(scip, col));
out[idx(ColumnFeatures::is_solution_at_upper_bound)] = static_cast<value_type>(is_prim_sol_at_ub(scip, col));
out[idx(ColumnFeatures::scaled_age)] = static_cast<value_type>(SCIPcolGetAge(col)) / (n_lps + cste);
out[idx(ColumnFeatures::incumbent_value)] = best_sol_val(scip, var).value_or(nan);
out[idx(ColumnFeatures::average_incumbent_value)] = avg_sol(scip, var).value_or(nan);
// On-hot encoding
out[idx(ColumnFeatures::is_basis_lower)] = 0.;
out[idx(ColumnFeatures::is_basis_basic)] = 0.;
out[idx(ColumnFeatures::is_basis_upper)] = 0.;
out[idx(ColumnFeatures::is_basis_zero)] = 0.;
switch (SCIPcolGetBasisStatus(col)) {
case SCIP_BASESTAT_LOWER:
out[idx(ColumnFeatures::is_basis_lower)] = 1.;
break;
case SCIP_BASESTAT_BASIC:
out[idx(ColumnFeatures::is_basis_basic)] = 1.;
break;
case SCIP_BASESTAT_UPPER:
out[idx(ColumnFeatures::is_basis_upper)] = 1.;
break;
case SCIP_BASESTAT_ZERO:
out[idx(ColumnFeatures::is_basis_zero)] = 1.;
break;
default:
assert(false); // All enum cases must be handled
}
}
void set_features_for_all_cols(xmatrix& out, scip::Model& model, bool const update_static) {
auto* const scip = model.get_scip_ptr();
// Contant reused in every iterations
auto const n_lps = static_cast<value_type>(SCIPgetNLPs(scip));
auto const obj_norm = obj_l2_norm(scip);
auto const columns = model.lp_columns();
auto const n_columns = columns.size();
for (std::size_t col_idx = 0; col_idx < n_columns; ++col_idx) {
auto* const col = columns[col_idx];
auto* const var = SCIPcolGetVar(col);
auto features = xt::row(out, static_cast<std::ptrdiff_t>(col_idx));
if (update_static) {
set_static_features_for_col(features, var, col, obj_norm);
}
set_dynamic_features_for_col(features, scip, var, col, obj_norm, n_lps);
}
}
/***************************************
* Row features extraction functions *
***************************************/
SCIP_Real row_l2_norm(SCIP_ROW* const row) noexcept {
auto const norm = SCIProwGetNorm(row);
return norm > 0 ? norm : 1.;
}
SCIP_Real obj_cos_sim(SCIP* const scip, SCIP_ROW* const row) noexcept {
auto const norm_prod = SCIProwGetNorm(row) * SCIPgetObjNorm(scip);
if (SCIPisPositive(scip, norm_prod)) {
return row->objprod / norm_prod;
}
return 0.;
}
/**
* Number of inequality rows.
*
* Row are counted once per right hand side and once per left hand side.
*/
std::size_t n_ineq_rows(scip::Model& model) {
auto* const scip = model.get_scip_ptr();
std::size_t count = 0;
for (auto* row : model.lp_rows()) {
count += static_cast<std::size_t>(scip::get_unshifted_lhs(scip, row).has_value());
count += static_cast<std::size_t>(scip::get_unshifted_rhs(scip, row).has_value());
}
return count;
}
template <typename Features>
void set_static_features_for_lhs_row(Features&& out, SCIP* const scip, SCIP_ROW* const row, value_type row_norm) {
out[idx(RowFeatures::bias)] = -1. * scip::get_unshifted_lhs(scip, row).value() / row_norm;
out[idx(RowFeatures::objective_cosine_similarity)] = -1 * obj_cos_sim(scip, row);
}
template <typename Features>
void set_static_features_for_rhs_row(Features&& out, SCIP* const scip, SCIP_ROW* const row, value_type row_norm) {
out[idx(RowFeatures::bias)] = scip::get_unshifted_rhs(scip, row).value() / row_norm;
out[idx(RowFeatures::objective_cosine_similarity)] = obj_cos_sim(scip, row);
}
template <typename Features>
void set_dynamic_features_for_lhs_row(
Features&& out,
SCIP* const scip,
SCIP_ROW* const row,
value_type row_norm,
value_type obj_norm,
value_type n_lps) {
out[idx(RowFeatures::is_tight)] = static_cast<value_type>(scip::is_at_lhs(scip, row));
out[idx(RowFeatures::dual_solution_value)] = -1. * SCIProwGetDualsol(row) / (row_norm * obj_norm);
out[idx(RowFeatures::scaled_age)] = static_cast<value_type>(SCIProwGetAge(row)) / (n_lps + cste);
}
template <typename Features>
void set_dynamic_features_for_rhs_row(
Features&& out,
SCIP* const scip,
SCIP_ROW* const row,
value_type row_norm,
value_type obj_norm,
value_type n_lps) {
out[idx(RowFeatures::is_tight)] = static_cast<value_type>(scip::is_at_rhs(scip, row));
out[idx(RowFeatures::dual_solution_value)] = SCIProwGetDualsol(row) / (row_norm * obj_norm);
out[idx(RowFeatures::scaled_age)] = static_cast<value_type>(SCIProwGetAge(row)) / (n_lps + cste);
}
auto set_features_for_all_rows(xmatrix& out, scip::Model& model, bool const update_static) {
auto* const scip = model.get_scip_ptr();
auto const n_lps = static_cast<value_type>(SCIPgetNLPs(scip));
value_type const obj_norm = obj_l2_norm(scip);
auto feat_row_idx = std::size_t{0};
for (auto* const row : model.lp_rows()) {
auto const row_norm = static_cast<value_type>(row_l2_norm(row));
// Rows are counted once per rhs and once per lhs
if (scip::get_unshifted_lhs(scip, row).has_value()) {
auto features = xt::row(out, static_cast<std::ptrdiff_t>(feat_row_idx));
if (update_static) {
set_static_features_for_lhs_row(features, scip, row, row_norm);
}
set_dynamic_features_for_lhs_row(features, scip, row, row_norm, obj_norm, n_lps);
feat_row_idx++;
}
if (scip::get_unshifted_rhs(scip, row).has_value()) {
auto features = xt::row(out, static_cast<std::ptrdiff_t>(feat_row_idx));
if (update_static) {
set_static_features_for_rhs_row(features, scip, row, row_norm);
}
set_dynamic_features_for_rhs_row(features, scip, row, row_norm, obj_norm, n_lps);
feat_row_idx++;
}
}
assert(feat_row_idx == n_ineq_rows(model));
}
/****************************************
* Edge features extraction functions *
****************************************/
/**
* Number of non zero element in the constraint matrix.
*
* Row are counted once per right hand side and once per left hand side.
*/
auto matrix_nnz(scip::Model& model) {
auto* const scip = model.get_scip_ptr();
std::size_t nnz = 0;
for (auto* row : model.lp_rows()) {
auto const row_size = static_cast<std::size_t>(SCIProwGetNLPNonz(row));
if (scip::get_unshifted_lhs(scip, row).has_value()) {
nnz += row_size;
}
if (scip::get_unshifted_rhs(scip, row).has_value()) {
nnz += row_size;
}
}
return nnz;
}
utility::coo_matrix<value_type> extract_edge_features(scip::Model& model) {
auto* const scip = model.get_scip_ptr();
using coo_matrix = utility::coo_matrix<value_type>;
auto const nnz = matrix_nnz(model);
auto values = decltype(coo_matrix::values)::from_shape({nnz});
auto indices = decltype(coo_matrix::indices)::from_shape({2, nnz});
std::size_t i = 0;
std::size_t j = 0;
for (auto* const row : model.lp_rows()) {
auto* const row_cols = SCIProwGetCols(row);
auto const* const row_vals = SCIProwGetVals(row);
auto const row_nnz = static_cast<std::size_t>(SCIProwGetNLPNonz(row));
if (scip::get_unshifted_lhs(scip, row).has_value()) {
for (std::size_t k = 0; k < row_nnz; ++k) {
indices(0, j + k) = i;
indices(1, j + k) = static_cast<std::size_t>(SCIPcolGetLPPos(row_cols[k]));
values[j + k] = -row_vals[k];
}
j += row_nnz;
i++;
}
if (scip::get_unshifted_rhs(scip, row).has_value()) {
for (std::size_t k = 0; k < row_nnz; ++k) {
indices(0, j + k) = i;
indices(1, j + k) = static_cast<std::size_t>(SCIPcolGetLPPos(row_cols[k]));
values[j + k] = row_vals[k];
}
j += row_nnz;
i++;
}
}
auto const n_rows = n_ineq_rows(model);
auto const n_cols = static_cast<std::size_t>(SCIPgetNLPCols(scip));
return {values, indices, {n_rows, n_cols}};
}
auto is_on_root_node(scip::Model& model) -> bool {
auto* const scip = model.get_scip_ptr();
return SCIPgetCurrentNode(scip) == SCIPgetRootNode(scip);
}
auto extract_observation_fully(scip::Model& model) -> NodeBipartiteObs {
auto obs = NodeBipartiteObs{
xmatrix::from_shape({model.lp_columns().size(), NodeBipartiteObs::n_column_features}),
xmatrix::from_shape({n_ineq_rows(model), NodeBipartiteObs::n_row_features}),
extract_edge_features(model),
};
set_features_for_all_cols(obs.column_features, model, true);
set_features_for_all_rows(obs.row_features, model, true);
return obs;
}
auto extract_observation_from_cache(scip::Model& model, NodeBipartiteObs obs) -> NodeBipartiteObs {
set_features_for_all_cols(obs.column_features, model, false);
set_features_for_all_rows(obs.row_features, model, false);
return obs;
}
} // namespace
/*************************************
* Observation extracting function *
*************************************/
auto NodeBipartite::before_reset(scip::Model& /* model */) -> void {
cache_computed = false;
}
auto NodeBipartite::extract(scip::Model& model, bool /* done */) -> std::optional<NodeBipartiteObs> {
if (model.stage() == SCIP_STAGE_SOLVING) {
if (use_cache) {
if (is_on_root_node(model)) {
the_cache = extract_observation_fully(model);
cache_computed = true;
return the_cache;
}
if (cache_computed) {
return extract_observation_from_cache(model, the_cache);
}
}
return extract_observation_fully(model);
}
return {};
}
} // namespace ecole::observation