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
/*++
Copyright (c) 2017 Microsoft Corporation
Module Name:
<name>
Abstract:
<abstract>
Author:
Lev Nachmanson (levnach)
Revision History:
--*/
#pragma once
#include "util/vector.h"
#include "math/lp/implied_bound.h"
#include "math/lp/lp_settings.h"
#include <functional>
// this class is for testing only
// 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
// In 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
// here in addition we assume that all coefficient in the row are positive
namespace lp {
class test_bound_analyzer {
linear_combination_iterator<mpq> & m_it;
std::function<impq (unsigned)>& m_lower_bounds;
std::function<impq (unsigned)>& m_upper_bounds;
std::function<column_type (unsigned)> m_column_types;
vector<implied_bound> & m_implied_bounds;
vector<mpq> m_coeffs;
int m_coeff_sign;
vector<unsigned> m_index;
unsigned m_row_or_term_index;
std::function<bool (unsigned, bool, mpq &, bool & strict)> & m_try_get_found_bound;
public :
// constructor
test_bound_analyzer(linear_combination_iterator<mpq> &it,
std::function<impq (unsigned) > & lower_bounds,
std::function<impq (unsigned)> & upper_bounds,
std::function<column_type (unsigned)> column_types,
vector<implied_bound> & evidence_vector,
unsigned row_or_term_index,
std::function<bool (unsigned, bool, mpq &, bool & strict)> & try_get_found_bound) :
m_it(it),
m_lower_bounds(lower_bounds),
m_upper_bounds(upper_bounds),
m_column_types(column_types),
m_implied_bounds(evidence_vector),
m_row_or_term_index(row_or_term_index),
m_try_get_found_bound(try_get_found_bound)
{
m_it.reset();
unsigned i;
mpq a;
while (m_it.next(a, i) ) {
m_coeffs.push_back(a);
m_index.push_back(i);
}
}
static int sign (const mpq & t) { return is_pos(t) ? 1: -1;}
void analyze() {
// We have the equality sum by j of row[j]*x[j] = m_rs
// We try to pin a var by pushing the total of the partial sum down, denoting the variable of this process by _u.
for (unsigned i = 0; i < m_index.size(); i++) {
analyze_i(i);
}
}
void analyze_i(unsigned i) {
// set the m_coeff_is_pos
m_coeff_sign = sign(m_coeffs[i]);
analyze_i_for_lower(i);
analyze_i_for_upper(i);
}
void analyze_i_for_upper(unsigned i) {
mpq l;
bool strict = false;
SASSERT(is_zero(l));
for (unsigned k = 0; k < m_index.size(); k++) {
if (k == i)
continue;
mpq lb;
bool str;
if (!upper_bound_of_monoid(k, lb, str)) {
return;
}
l += lb;
if (str)
strict = true;
}
l /= m_coeffs[i];
unsigned j = m_index[i];
switch(m_column_types(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::upper_bound:
{
const auto & lb = m_upper_bounds(j);
if (l > lb.x || (l == lb.x && !(is_zero(lb.y) && strict))) {
break; // no improvement on the existing upper bound
}
}
default:
m_implied_bounds.push_back(implied_bound(l, j, false, is_pos(m_coeffs[i]), m_row_or_term_index, strict));
}
}
bool lower_bound_of_monoid(unsigned k, mpq & lb, bool &strict) const {
int s = - m_coeff_sign * sign(m_coeffs[k]);
unsigned j = m_index[k];
if (s > 0) {
switch(m_column_types(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::lower_bound:
lb = -m_coeffs[k] * m_lower_bounds(j).x;
strict = !is_zero(m_lower_bounds(j).y);
return true;
default:
return false;
}
}
switch(m_column_types(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::upper_bound:
lb = -m_coeffs[k] * m_upper_bounds(j).x;
strict = !is_zero(m_upper_bounds(j).y);
return true;
default:
return false;
}
}
bool upper_bound_of_monoid(unsigned k, mpq & lb, bool & strict) const {
int s = - m_coeff_sign * sign(m_coeffs[k]);
unsigned j = m_index[k];
if (s > 0) {
switch(m_column_types(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::upper_bound:
lb = -m_coeffs[k] * m_upper_bounds(j).x;
strict = !is_zero(m_upper_bounds(j).y);
return true;
default:
return false;
}
}
switch(m_column_types(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::lower_bound:
lb = -m_coeffs[k] * m_lower_bounds(j).x;
strict = !is_zero(m_lower_bounds(j).y);
return true;
default:
return false;
}
}
void analyze_i_for_lower(unsigned i) {
mpq l;
SASSERT(is_zero(l));
bool strict = false;
for (unsigned k = 0; k < m_index.size(); k++) {
if (k == i)
continue;
mpq lb;
bool str;
if (!lower_bound_of_monoid(k, lb, str)) {
return;
}
if (str)
strict = true;
l += lb;
}
l /= m_coeffs[i];
unsigned j = m_index[i];
switch(m_column_types(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::lower_bound:
{
const auto & lb = m_lower_bounds(j);
if (l < lb.x || (l == lb.x && !(is_zero(lb.y) && strict))) {
break; // no improvement on the existing upper bound
}
}
default:
m_implied_bounds.push_back(implied_bound(l, j, true, is_pos(m_coeffs[i]), m_row_or_term_index, strict));
}
}
bool lower_bound_is_available(unsigned j) const {
switch(m_column_types(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::lower_bound:
return true;
default:
return false;
}
}
bool upper_bound_is_available(unsigned j) const {
switch(m_column_types(j)) {
case column_type::fixed:
case column_type::boxed:
case column_type::upper_bound:
return true;
default:
return false;
}
}
bool try_get_best_avail_bound(unsigned j, bool is_lower_bound, mpq & best_bound, bool & strict_of_best_bound) const {
if (m_try_get_found_bound(j, is_lower_bound, best_bound, strict_of_best_bound)) {
return true;
}
if (is_lower_bound) {
if (lower_bound_is_available(j)) {
best_bound = m_lower_bounds(j).x;
strict_of_best_bound = !is_zero(m_lower_bounds(j).y);
return true;
}
} else {
if (upper_bound_is_available(j)) {
best_bound = m_upper_bounds(j).x;
strict_of_best_bound = !is_zero(m_lower_bounds(j).y);
return true;
}
}
return false;
}
bool bound_is_new(unsigned j, const mpq& b, bool is_lower_bound, bool strict) const {
mpq best_bound;
bool strict_of_best_bound;
if (try_get_best_avail_bound(j, is_lower_bound, best_bound, strict_of_best_bound)) {
if (is_lower_bound) {
if (b > best_bound || ( b != best_bound && (strict && !strict_of_best_bound))) // the second clouse stands for strong inequality
return true;
} else {
if (b < best_bound || ( b == best_bound && (strict && !strict_of_best_bound)))
return true;
}
return false;
}
return true;
}
};
}