32 shifted_lp_values_.clear();
33 bound_parity_.clear();
35 col_to_rows_.resize(size);
36 tmp_marked_.resize(size);
40 const std::vector<double>& lp_values,
43 Reset(lp_values.size());
46 lp_values_ = lp_values;
47 for (
int i = 0; i < lp_values.size(); ++i) {
50 if (lb_dist < ub_dist) {
51 shifted_lp_values_.push_back(lb_dist);
54 shifted_lp_values_.push_back(ub_dist);
63 for (
const int col : binary_row.
cols) {
64 col_to_rows_[
col].push_back(rows_.size());
66 rows_.push_back(binary_row);
70 const glop::RowIndex
row,
71 const std::vector<std::pair<glop::ColIndex, IntegerValue>>& terms,
72 IntegerValue lb, IntegerValue ub) {
73 if (terms.size() > kMaxInputConstraintSize)
return;
75 double activity = 0.0;
76 IntegerValue magnitude(0);
79 for (
const auto& term : terms) {
80 const int col = term.first.value();
81 activity +=
ToDouble(term.second) * lp_values_[
col];
85 if ((term.second.value() & 1) == 0)
continue;
88 if (shifted_lp_values_[
col] > 1e-2) {
93 rhs_adjust ^= bound_parity_[
col];
99 if (magnitude > kMaxInputConstraintMagnitude)
return;
107 const double tighteness_threshold = 1e-2;
108 if (
ToDouble(ub) - activity < tighteness_threshold) {
111 binary_row.
rhs_parity = (ub.value() & 1) ^ rhs_adjust;
114 if (activity -
ToDouble(lb) < tighteness_threshold) {
117 binary_row.
rhs_parity = (lb.value() & 1) ^ rhs_adjust;
123 std::function<
bool(
int)> extra_condition,
const std::vector<int>&
a,
124 std::vector<int>*
b) {
125 for (
const int v : *
b) tmp_marked_[v] =
true;
126 for (
const int v :
a) {
127 if (tmp_marked_[v]) {
128 tmp_marked_[v] =
false;
130 tmp_marked_[v] =
true;
139 for (
const int v : *
b) {
140 if (tmp_marked_[v]) {
141 if (extra_condition(v)) {
142 (*b)[new_size++] = v;
144 tmp_marked_[v] =
false;
150void ZeroHalfCutHelper::ProcessSingletonColumns() {
151 for (
const int singleton_col : singleton_cols_) {
152 if (col_to_rows_[singleton_col].empty())
continue;
153 CHECK_EQ(col_to_rows_[singleton_col].size(), 1);
154 const int row = col_to_rows_[singleton_col][0];
156 auto& mutable_cols = rows_[
row].cols;
157 for (
const int col : mutable_cols) {
158 if (
col == singleton_col)
continue;
159 mutable_cols[new_size++] =
col;
161 CHECK_LT(new_size, mutable_cols.size());
162 mutable_cols.resize(new_size);
163 col_to_rows_[singleton_col].clear();
164 rows_[
row].slack += shifted_lp_values_[singleton_col];
166 singleton_cols_.clear();
171 int eliminated_row) {
172 CHECK_LE(rows_[eliminated_row].slack, 1e-6);
173 CHECK(!rows_[eliminated_row].cols.empty());
176 tmp_marked_.resize(
std::max(col_to_rows_.size(), rows_.size()));
177 DCHECK(std::all_of(tmp_marked_.begin(), tmp_marked_.end(),
178 [](
bool b) { return !b; }));
180 for (
const int other_row : col_to_rows_[eliminated_col]) {
181 if (other_row == eliminated_row)
continue;
182 col_to_rows_[eliminated_col][new_size++] = other_row;
185 &rows_[other_row].cols);
188 rows_[other_row].rhs_parity ^= rows_[eliminated_row].rhs_parity;
189 rows_[other_row].slack += rows_[eliminated_row].slack;
193 auto& mutable_multipliers = rows_[other_row].multipliers;
194 mutable_multipliers.insert(mutable_multipliers.end(),
195 rows_[eliminated_row].multipliers.begin(),
196 rows_[eliminated_row].multipliers.end());
197 std::sort(mutable_multipliers.begin(), mutable_multipliers.end());
199 for (
const auto& entry : mutable_multipliers) {
200 if (new_size > 0 && entry == mutable_multipliers[new_size - 1]) {
204 mutable_multipliers[new_size++] = entry;
207 mutable_multipliers.resize(new_size);
210 col_to_rows_[eliminated_col].resize(new_size);
218 for (
const int other_col : rows_[eliminated_row].cols) {
219 if (other_col == eliminated_col)
continue;
220 const int old_size = col_to_rows_[other_col].size();
221 rows_[eliminated_row].cols[new_size++] = other_col;
223 [
this](
int i) {
return rows_[i].slack < kSlackThreshold; },
224 col_to_rows_[eliminated_col], &col_to_rows_[other_col]);
225 if (old_size != 1 && col_to_rows_[other_col].size() == 1) {
226 singleton_cols_.push_back(other_col);
229 rows_[eliminated_row].cols.resize(new_size);
233 col_to_rows_[eliminated_col].clear();
234 rows_[eliminated_row].slack += shifted_lp_values_[eliminated_col];
237std::vector<std::vector<std::pair<glop::RowIndex, IntegerValue>>>
239 std::vector<std::vector<std::pair<glop::RowIndex, IntegerValue>>> result;
242 singleton_cols_.clear();
243 for (
int col = 0;
col < col_to_rows_.size(); ++
col) {
244 if (col_to_rows_[
col].size() == 1) singleton_cols_.push_back(
col);
248 std::vector<int> to_process;
249 for (
int row = 0;
row < rows_.size(); ++
row) to_process.push_back(
row);
250 std::shuffle(to_process.begin(), to_process.end(), *random);
251 std::stable_sort(to_process.begin(), to_process.end(), [
this](
int a,
int b) {
252 return rows_[a].cols.size() < rows_[b].cols.size();
255 for (
const int row : to_process) {
256 ProcessSingletonColumns();
258 if (rows_[
row].cols.empty())
continue;
259 if (rows_[
row].slack > 1e-6)
continue;
260 if (rows_[
row].multipliers.size() > kMaxAggregationSize)
continue;
263 int eliminated_col = -1;
264 double max_lp_value = 0.0;
265 for (
const int col : rows_[
row].cols) {
266 if (shifted_lp_values_[
col] > max_lp_value) {
267 max_lp_value = shifted_lp_values_[
col];
268 eliminated_col =
col;
271 if (eliminated_col == -1)
continue;
278 for (
const auto&
row : rows_) {
279 if (
row.cols.empty() &&
row.rhs_parity &&
row.slack < kSlackThreshold) {
280 result.push_back(
row.multipliers);
283 VLOG(1) <<
"#candidates: " << result.size() <<
" / " << rows_.size();
#define CHECK_LT(val1, val2)
#define CHECK_EQ(val1, val2)
#define DCHECK(condition)
#define CHECK_LE(val1, val2)
#define VLOG(verboselevel)
void EliminateVarUsingRow(int col, int row)
void AddBinaryRow(const CombinationOfRows &binary_row)
void SymmetricDifference(std::function< bool(int)> extra_condition, const std::vector< int > &a, std::vector< int > *b)
void ProcessVariables(const std::vector< double > &lp_values, const std::vector< IntegerValue > &lower_bounds, const std::vector< IntegerValue > &upper_bounds)
void AddOneConstraint(glop::RowIndex, const std::vector< std::pair< glop::ColIndex, IntegerValue > > &terms, IntegerValue lb, IntegerValue ub)
std::vector< std::vector< std::pair< glop::RowIndex, IntegerValue > > > InterestingCandidates(ModelRandomGenerator *random)
IntType IntTypeAbs(IntType t)
double ToDouble(IntegerValue value)
Collection of objects used to extend the Constraint Solver library.
std::vector< double > lower_bounds
std::vector< double > upper_bounds
std::vector< std::pair< glop::RowIndex, IntegerValue > > multipliers