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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*++
Copyright (c) 2017 Microsoft Corporation
Module Name:
<name>
Abstract:
We have an equality : sum by j of row[j]*x[j] = rs
We try to pin a var by pushing the total by using the variable bounds
on a loop we drive the partial sum down, denoting the variables of this process by _u.
In the same loop trying to pin variables by pushing the partial sum up, denoting the variable related to it by _l
Author:
Lev Nachmanson (levnach)
Nikolaj Bjorner (nbjorner)
Revision History:
--*/
#pragma once
#include "util/vector.h"
#include "math/lp/implied_bound.h"
#include "math/lp/test_bound_analyzer.h"
namespace lp {
template <typename C, typename B> // C plays a role of a container, B - lp_bound_propagator
class bound_analyzer_on_row {
const C& m_row;
B & m_bp;
int m_column_of_u; // index of an unlimited from above monoid
// -1 means that such a value is not found, -2 means that at least two of such monoids were found
int m_column_of_l; // index of an unlimited from below monoid
const impq& m_rs;
// lazily computed: 1 if some column of the row can receive an interesting bound, 0 if none can, -1 if unknown
int m_some_column_is_interesting = -1;
public :
// constructor
bound_analyzer_on_row(
const C & it,
const numeric_pair<mpq>& rs,
B & bp)
:
m_row(it),
m_bp(bp),
m_column_of_u(-1),
m_column_of_l(-1),
m_rs(rs)
{}
static unsigned analyze_row(const C & row,
const numeric_pair<mpq>& rs,
B & bp) {
bound_analyzer_on_row a(row, rs, bp);
return a.analyze();
}
private:
unsigned analyze() {
unsigned num_prop = 0;
for (const auto & c : m_row) {
if ((m_column_of_l == -2) && (m_column_of_u == -2))
return 0;
if (c.coeff().is_big()) // bounds derived with big coefficients are not worth their cost
return 0;
analyze_bound_on_var_on_coeff(c.var(), c.coeff());
}
// Bounds are only derived for columns that can consume them: computing
// a bound costs rational arithmetic and, when it is reported, closures
// for its explanation, so the cheap relevance test comes first.
if (m_column_of_u >= 0) {
if (m_bp.column_is_interesting(m_column_of_u)) {
limit_monoid_u_from_below();
++num_prop;
}
}
else if (m_column_of_u == -1 && some_column_is_interesting()) {
limit_all_monoids_from_below();
++num_prop;
}
if (m_column_of_l >= 0) {
if (m_bp.column_is_interesting(m_column_of_l)) {
limit_monoid_l_from_above();
++num_prop;
}
}
else if (m_column_of_l == -1 && some_column_is_interesting()) {
limit_all_monoids_from_above();
++num_prop;
}
return num_prop;
}
bool some_column_is_interesting() {
if (m_some_column_is_interesting < 0) {
m_some_column_is_interesting = 0;
for (const auto& c : m_row) {
if (m_bp.column_is_interesting(c.var())) {
m_some_column_is_interesting = 1;
break;
}
}
}
return m_some_column_is_interesting == 1;
}
bool bound_is_available(unsigned j, bool lower_bound) {
return (lower_bound && m_bp.lower_bound_is_available(j)) ||
(!lower_bound && m_bp.upper_bound_is_available(j));
}
const impq & ub(unsigned j) const {
SASSERT(upper_bound_is_available(j));
return get_upper_bound(j);
}
const impq & lb(unsigned j) const {
SASSERT(lower_bound_is_available(j));
return get_lower_bound(j);
}
const mpq & monoid_max_no_mult(bool a_is_pos, unsigned j, bool & strict) const {
if (a_is_pos) {
strict = !is_zero(ub(j).y);
return ub(j).x;
}
strict = !is_zero(lb(j).y);
return lb(j).x;
}
mpq monoid_max(const mpq & a, unsigned j) const {
return a * (is_pos(a) ? ub(j).x : lb(j).x);
}
mpq monoid_max(const mpq & a, unsigned j, bool & strict) const {
if (is_pos(a)) {
strict = !is_zero(ub(j).y);
return a * ub(j).x;
}
strict = !is_zero(lb(j).y);
return a * lb(j).x;
}
const mpq & monoid_min_no_mult(bool a_is_pos, unsigned j, bool & strict) const {
if (!a_is_pos) {
strict = !is_zero(ub(j).y);
return ub(j).x;
}
strict = !is_zero(lb(j).y);
return lb(j).x;
}
mpq monoid_min(const mpq & a, unsigned j, bool& strict) const {
if (is_neg(a)) {
strict = !is_zero(ub(j).y);
return a * ub(j).x;
}
strict = !is_zero(lb(j).y);
return a * lb(j).x;
}
mpq monoid_min(const mpq & a, unsigned j) const {
return a * (is_neg(a) ? ub(j).x : lb(j).x);
}
// t -= monoid_max(a, j, strict), without a temporary
void submul_monoid_max(mpq& t, const mpq & a, unsigned j, bool & strict) const {
if (is_pos(a)) {
strict = !is_zero(ub(j).y);
t.submul(a, ub(j).x);
}
else {
strict = !is_zero(lb(j).y);
t.submul(a, lb(j).x);
}
}
// t -= monoid_min(a, j, strict), without a temporary
void submul_monoid_min(mpq& t, const mpq & a, unsigned j, bool & strict) const {
if (is_neg(a)) {
strict = !is_zero(ub(j).y);
t.submul(a, ub(j).x);
}
else {
strict = !is_zero(lb(j).y);
t.submul(a, lb(j).x);
}
}
mpq m_total, m_bound;
void limit_all_monoids_from_above() {
int strict = 0;
m_total = m_rs.x;
for (const auto& p : m_row) {
bool str;
submul_monoid_min(m_total, p.coeff(), p.var(), str);
if (str)
strict++;
}
for (const auto &p : m_row) {
if (!m_bp.column_is_interesting(p.var()))
continue;
bool str;
bool a_is_pos = is_pos(p.coeff());
m_bound = m_total;
m_bound /= p.coeff();
m_bound += monoid_min_no_mult(a_is_pos, p.var(), str);
if (a_is_pos) {
limit_j(p.var(), m_bound, true, false, strict - static_cast<int>(str) > 0);
}
else {
limit_j(p.var(), m_bound, false, true, strict - static_cast<int>(str) > 0);
}
}
}
// total := - sum_{a_j > 0} a_j * ub(x_j) - sum_{a_j < 0} a_j * lb(x_j)
//
// for each a_j* x_j in row,
// bound := total / a_j + ub(x_j) = (total + (a_j * ub(x_j))) / a_j if a_j > 0
// bound := total / a_j + lb(x_j) = (total + (a_j * lb(x_j))) / a_j if a_j < 0
// example
// 3x + 2y + z = 0
// x <= 1, y <= 2, z <= 1
// total = - (3*1 + 2*2 + 1) = -8
// bound := -8/1 + 1 = -7
// => z >= -7
// If all variables are integer (or if x_j is integer).
// let K = lcm(a_j)
//
// totalK = K*total
// sum without x_j: bound := totalK + K * a_j * ub(x_j)
// K*a_j x >= bound
// x >= ceil(bound / K*a_j) = ceil ((total + a_j * ub(x_j)) / a_j) = ceil ((total / a_j) + ub(x_j))
//
void limit_all_monoids_from_below() {
int strict = 0;
m_total = m_rs.x;
for (const auto &p : m_row) {
bool str;
submul_monoid_max(m_total, p.coeff(), p.var(), str);
if (str)
strict++;
}
for (const auto& p : m_row) {
if (!m_bp.column_is_interesting(p.var()))
continue;
bool str;
bool a_is_pos = is_pos(p.coeff());
m_bound = m_total;
m_bound /= p.coeff();
m_bound += monoid_max_no_mult(a_is_pos, p.var(), str);
bool astrict = strict - static_cast<int>(str) > 0;
if (a_is_pos) {
limit_j(p.var(), m_bound, true, true, astrict);
}
else {
limit_j(p.var(), m_bound, false, false, astrict);
}
}
}
void limit_monoid_u_from_below() {
// we are going to limit from below the monoid m_column_of_u,
// every other monoid is impossible to limit from below
mpq u_coeff;
unsigned j;
m_bound = m_rs.x;
bool strict = false;
for (const auto& p : m_row) {
j = p.var();
if (j == static_cast<unsigned>(m_column_of_u)) {
u_coeff = p.coeff();
continue;
}
bool str;
submul_monoid_max(m_bound, p.coeff(), j, str);
if (str)
strict = true;
}
m_bound /= u_coeff;
if (u_coeff.is_pos()) {
limit_j(m_column_of_u, m_bound, true, true, strict);
} else {
limit_j(m_column_of_u, m_bound, false, false, strict);
}
}
void limit_monoid_l_from_above() {
// we are going to limit from above the monoid m_column_of_l,
// every other monoid is impossible to limit from above
mpq l_coeff;
unsigned j;
m_bound = m_rs.x;
bool strict = false;
for (const auto &p : m_row) {
j = p.var();
if (j == static_cast<unsigned>(m_column_of_l)) {
l_coeff = p.coeff();
continue;
}
bool str;
submul_monoid_min(m_bound, p.coeff(), j, str);
if (str)
strict = true;
}
m_bound /= l_coeff;
if (is_pos(l_coeff)) {
limit_j(m_column_of_l, m_bound, true, false, strict);
} else {
limit_j(m_column_of_l, m_bound, false, true, strict);
}
}
// // it is the coefficient before the bounded column
// void provide_evidence(bool coeff_is_pos) {
// /*
// auto & be = m_ibounds.back();
// bool lower_bound = be.m_lower_bound;
// if (!coeff_is_pos)
// lower_bound = !lower_bound;
// auto it = m_row.clone();
// mpq a; unsigned j;
// while (it->next(a, j)) {
// if (be.m_j == j) continue;
// SASSERT(bound_is_available(j, is_neg(a) ? lower_bound : !lower_bound));
// be.m_vector_of_bound_signatures.emplace_back(a, j, numeric_traits<impq>::
// is_neg(a)? lower_bound: !lower_bound);
// }
// delete it;
// */
// }
void limit_j(unsigned bound_j, const mpq& u, bool coeff_before_j_is_pos, bool is_lower_bound, bool strict) {
auto* lar = &m_bp.lp();
auto* row = &this->m_row;
++lar->stats().m_bp_bound_candidates;
if (!m_bp.bound_is_interesting(bound_j, is_lower_bound, strict, u))
return;
++lar->stats().m_bp_bounds_interesting;
// The bound on bound_j follows from the row sum_k a_k x_k = 0 and the bounds of the
// other columns: x_j = -sum_{k != j} (a_k / a_j) x_k. The bound of column k enters
// the derivation with Farkas coefficient |a_k / a_j|.
auto explain_weighted = [row, bound_j, coeff_before_j_is_pos, is_lower_bound, strict, lar]
(implied_bound::weighted_consumer const& consume) {
(void) strict;
TRACE(bound_analyzer, tout << "explain_bound_on_var_on_coeff, bound_j = " << bound_j << ", coeff_before_j_is_pos = " << coeff_before_j_is_pos << ", is_lower_bound = " << is_lower_bound << ", strict = " << strict << "\n";);
int bound_sign = (is_lower_bound ? 1 : -1);
int j_sign = (coeff_before_j_is_pos ? 1 : -1) * bound_sign;
mpq a_j;
for (auto const& r : *row)
if (r.var() == bound_j) {
a_j = abs(r.coeff());
break;
}
for (auto const& r : *row) {
unsigned j = r.var();
if (j == bound_j)
continue;
mpq const& a = r.coeff();
int a_sign = is_pos(a) ? 1 : -1;
int sign = j_sign * a_sign;
u_dependency* witness = sign > 0 ? lar->get_column_upper_bound_witness(j) : lar->get_column_lower_bound_witness(j);
consume(abs(a) / a_j, witness);
}
};
auto explain = [explain_weighted, lar]() {
u_dependency* ret = nullptr;
explain_weighted([&](mpq const&, u_dependency* witness) { ret = lar->join_deps(ret, witness); });
return ret;
};
if (lar->dep_manager().proofs_enabled()) {
u_dependency* support = nullptr;
lp::evidence_manager::recipe r;
r.kind = lp::evidence_manager::rule::linear;
r.conclusion.lhs.push_back({mpq(1), bound_j});
r.conclusion.kind = is_lower_bound ? (strict ? GT : GE) : (strict ? LT : LE);
r.conclusion.rhs = impq(u);
explain_weighted([&](mpq const& c, u_dependency* p) {
support = lar->join_deps(support, p);
r.premises.push_back({p, c});
});
auto* proof = lar->dep_manager().derive(support, [&] { return std::move(r); });
auto* recipe = lp::evidence_manager::get_recipe(proof);
m_bp.add_bound(u, bound_j, is_lower_bound, strict, [proof]() { return proof; },
[recipe](implied_bound::weighted_consumer const& consume) {
for (auto const& p : recipe->premises)
consume(p.coefficient, p.evidence);
});
}
else {
m_bp.add_bound(u, bound_j, is_lower_bound, strict, explain, explain_weighted);
}
}
void advance_u(unsigned j) {
m_column_of_u = (m_column_of_u == -1) ? j : -2;
}
void advance_l(unsigned j) {
m_column_of_l = (m_column_of_l == -1) ? j : -2;
}
void analyze_bound_on_var_on_coeff(int j, const mpq &a) {
switch (get_column_type(j)) {
case column_type::lower_bound:
if (numeric_traits<mpq>::is_pos(a))
advance_u(j);
else
advance_l(j);
break;
case column_type::upper_bound:
if (numeric_traits<mpq>::is_neg(a))
advance_u(j);
else
advance_l(j);
break;
case column_type::free_column:
advance_u(j);
advance_l(j);
break;
default:
break;
}
}
const impq& get_upper_bound(unsigned j) const {
return lp().get_upper_bound(j);
}
const impq& get_lower_bound(unsigned j) const {
return lp().get_lower_bound(j);
}
column_type get_column_type(unsigned j) const {
return (lp().get_column_types())[j];
}
const auto& lp() const { return m_bp.lp(); }
auto& lp() { return m_bp.lp(); }
bool upper_bound_is_available(unsigned j) const {
switch (get_column_type(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::upper_bound:
return true;
default:
return false;
}
}
bool lower_bound_is_available(unsigned j) const {
switch (get_column_type(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::lower_bound:
return true;
default:
return false;
}
}
};
}