1 //===- Simplex.cpp - MLIR Simplex Class -----------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "mlir/Analysis/Presburger/Simplex.h" 10 #include "mlir/Analysis/Presburger/Matrix.h" 11 #include "mlir/Support/MathExtras.h" 12 13 namespace mlir { 14 using Direction = Simplex::Direction; 15 16 const int nullIndex = std::numeric_limits<int>::max(); 17 18 /// Construct a Simplex object with `nVar` variables. 19 Simplex::Simplex(unsigned nVar) 20 : nRow(0), nCol(2), tableau(0, 2 + nVar), empty(false) { 21 colUnknown.push_back(nullIndex); 22 colUnknown.push_back(nullIndex); 23 for (unsigned i = 0; i < nVar; ++i) { 24 var.emplace_back(Orientation::Column, /*restricted=*/false, /*pos=*/nCol); 25 colUnknown.push_back(i); 26 nCol++; 27 } 28 } 29 30 Simplex::Simplex(const FlatAffineConstraints &constraints) 31 : Simplex(constraints.getNumIds()) { 32 for (unsigned i = 0, numIneqs = constraints.getNumInequalities(); 33 i < numIneqs; ++i) 34 addInequality(constraints.getInequality(i)); 35 for (unsigned i = 0, numEqs = constraints.getNumEqualities(); i < numEqs; ++i) 36 addEquality(constraints.getEquality(i)); 37 } 38 39 const Simplex::Unknown &Simplex::unknownFromIndex(int index) const { 40 assert(index != nullIndex && "nullIndex passed to unknownFromIndex"); 41 return index >= 0 ? var[index] : con[~index]; 42 } 43 44 const Simplex::Unknown &Simplex::unknownFromColumn(unsigned col) const { 45 assert(col < nCol && "Invalid column"); 46 return unknownFromIndex(colUnknown[col]); 47 } 48 49 const Simplex::Unknown &Simplex::unknownFromRow(unsigned row) const { 50 assert(row < nRow && "Invalid row"); 51 return unknownFromIndex(rowUnknown[row]); 52 } 53 54 Simplex::Unknown &Simplex::unknownFromIndex(int index) { 55 assert(index != nullIndex && "nullIndex passed to unknownFromIndex"); 56 return index >= 0 ? var[index] : con[~index]; 57 } 58 59 Simplex::Unknown &Simplex::unknownFromColumn(unsigned col) { 60 assert(col < nCol && "Invalid column"); 61 return unknownFromIndex(colUnknown[col]); 62 } 63 64 Simplex::Unknown &Simplex::unknownFromRow(unsigned row) { 65 assert(row < nRow && "Invalid row"); 66 return unknownFromIndex(rowUnknown[row]); 67 } 68 69 /// Add a new row to the tableau corresponding to the given constant term and 70 /// list of coefficients. The coefficients are specified as a vector of 71 /// (variable index, coefficient) pairs. 72 unsigned Simplex::addRow(ArrayRef<int64_t> coeffs) { 73 assert(coeffs.size() == 1 + var.size() && 74 "Incorrect number of coefficients!"); 75 76 ++nRow; 77 // If the tableau is not big enough to accomodate the extra row, we extend it. 78 if (nRow >= tableau.getNumRows()) 79 tableau.resizeVertically(nRow); 80 rowUnknown.push_back(~con.size()); 81 con.emplace_back(Orientation::Row, false, nRow - 1); 82 83 tableau(nRow - 1, 0) = 1; 84 tableau(nRow - 1, 1) = coeffs.back(); 85 for (unsigned col = 2; col < nCol; ++col) 86 tableau(nRow - 1, col) = 0; 87 88 // Process each given variable coefficient. 89 for (unsigned i = 0; i < var.size(); ++i) { 90 unsigned pos = var[i].pos; 91 if (coeffs[i] == 0) 92 continue; 93 94 if (var[i].orientation == Orientation::Column) { 95 // If a variable is in column position at column col, then we just add the 96 // coefficient for that variable (scaled by the common row denominator) to 97 // the corresponding entry in the new row. 98 tableau(nRow - 1, pos) += coeffs[i] * tableau(nRow - 1, 0); 99 continue; 100 } 101 102 // If the variable is in row position, we need to add that row to the new 103 // row, scaled by the coefficient for the variable, accounting for the two 104 // rows potentially having different denominators. The new denominator is 105 // the lcm of the two. 106 int64_t lcm = mlir::lcm(tableau(nRow - 1, 0), tableau(pos, 0)); 107 int64_t nRowCoeff = lcm / tableau(nRow - 1, 0); 108 int64_t idxRowCoeff = coeffs[i] * (lcm / tableau(pos, 0)); 109 tableau(nRow - 1, 0) = lcm; 110 for (unsigned col = 1; col < nCol; ++col) 111 tableau(nRow - 1, col) = 112 nRowCoeff * tableau(nRow - 1, col) + idxRowCoeff * tableau(pos, col); 113 } 114 115 normalizeRow(nRow - 1); 116 // Push to undo log along with the index of the new constraint. 117 undoLog.push_back(UndoLogEntry::RemoveLastConstraint); 118 return con.size() - 1; 119 } 120 121 /// Normalize the row by removing factors that are common between the 122 /// denominator and all the numerator coefficients. 123 void Simplex::normalizeRow(unsigned row) { 124 int64_t gcd = 0; 125 for (unsigned col = 0; col < nCol; ++col) { 126 if (gcd == 1) 127 break; 128 gcd = llvm::greatestCommonDivisor(gcd, std::abs(tableau(row, col))); 129 } 130 for (unsigned col = 0; col < nCol; ++col) 131 tableau(row, col) /= gcd; 132 } 133 134 namespace { 135 bool signMatchesDirection(int64_t elem, Direction direction) { 136 assert(elem != 0 && "elem should not be 0"); 137 return direction == Direction::Up ? elem > 0 : elem < 0; 138 } 139 140 Direction flippedDirection(Direction direction) { 141 return direction == Direction::Up ? Direction::Down : Simplex::Direction::Up; 142 } 143 } // anonymous namespace 144 145 /// Find a pivot to change the sample value of the row in the specified 146 /// direction. The returned pivot row will involve `row` if and only if the 147 /// unknown is unbounded in the specified direction. 148 /// 149 /// To increase (resp. decrease) the value of a row, we need to find a live 150 /// column with a non-zero coefficient. If the coefficient is positive, we need 151 /// to increase (decrease) the value of the column, and if the coefficient is 152 /// negative, we need to decrease (increase) the value of the column. Also, 153 /// we cannot decrease the sample value of restricted columns. 154 /// 155 /// If multiple columns are valid, we break ties by considering a lexicographic 156 /// ordering where we prefer unknowns with lower index. 157 Optional<Simplex::Pivot> Simplex::findPivot(int row, 158 Direction direction) const { 159 Optional<unsigned> col; 160 for (unsigned j = 2; j < nCol; ++j) { 161 int64_t elem = tableau(row, j); 162 if (elem == 0) 163 continue; 164 165 if (unknownFromColumn(j).restricted && 166 !signMatchesDirection(elem, direction)) 167 continue; 168 if (!col || colUnknown[j] < colUnknown[*col]) 169 col = j; 170 } 171 172 if (!col) 173 return {}; 174 175 Direction newDirection = 176 tableau(row, *col) < 0 ? flippedDirection(direction) : direction; 177 Optional<unsigned> maybePivotRow = findPivotRow(row, newDirection, *col); 178 return Pivot{maybePivotRow.getValueOr(row), *col}; 179 } 180 181 /// Swap the associated unknowns for the row and the column. 182 /// 183 /// First we swap the index associated with the row and column. Then we update 184 /// the unknowns to reflect their new position and orientation. 185 void Simplex::swapRowWithCol(unsigned row, unsigned col) { 186 std::swap(rowUnknown[row], colUnknown[col]); 187 Unknown &uCol = unknownFromColumn(col); 188 Unknown &uRow = unknownFromRow(row); 189 uCol.orientation = Orientation::Column; 190 uRow.orientation = Orientation::Row; 191 uCol.pos = col; 192 uRow.pos = row; 193 } 194 195 void Simplex::pivot(Pivot pair) { pivot(pair.row, pair.column); } 196 197 /// Pivot pivotRow and pivotCol. 198 /// 199 /// Let R be the pivot row unknown and let C be the pivot col unknown. 200 /// Since initially R = a*C + sum b_i * X_i 201 /// (where the sum is over the other column's unknowns, x_i) 202 /// C = (R - (sum b_i * X_i))/a 203 /// 204 /// Let u be some other row unknown. 205 /// u = c*C + sum d_i * X_i 206 /// So u = c*(R - sum b_i * X_i)/a + sum d_i * X_i 207 /// 208 /// This results in the following transform: 209 /// pivot col other col pivot col other col 210 /// pivot row a b -> pivot row 1/a -b/a 211 /// other row c d other row c/a d - bc/a 212 /// 213 /// Taking into account the common denominators p and q: 214 /// 215 /// pivot col other col pivot col other col 216 /// pivot row a/p b/p -> pivot row p/a -b/a 217 /// other row c/q d/q other row cp/aq (da - bc)/aq 218 /// 219 /// The pivot row transform is accomplished be swapping a with the pivot row's 220 /// common denominator and negating the pivot row except for the pivot column 221 /// element. 222 void Simplex::pivot(unsigned pivotRow, unsigned pivotCol) { 223 assert(pivotCol >= 2 && "Refusing to pivot invalid column"); 224 225 swapRowWithCol(pivotRow, pivotCol); 226 std::swap(tableau(pivotRow, 0), tableau(pivotRow, pivotCol)); 227 // We need to negate the whole pivot row except for the pivot column. 228 if (tableau(pivotRow, 0) < 0) { 229 // If the denominator is negative, we negate the row by simply negating the 230 // denominator. 231 tableau(pivotRow, 0) = -tableau(pivotRow, 0); 232 tableau(pivotRow, pivotCol) = -tableau(pivotRow, pivotCol); 233 } else { 234 for (unsigned col = 1; col < nCol; ++col) { 235 if (col == pivotCol) 236 continue; 237 tableau(pivotRow, col) = -tableau(pivotRow, col); 238 } 239 } 240 normalizeRow(pivotRow); 241 242 for (unsigned row = 0; row < nRow; ++row) { 243 if (row == pivotRow) 244 continue; 245 if (tableau(row, pivotCol) == 0) // Nothing to do. 246 continue; 247 tableau(row, 0) *= tableau(pivotRow, 0); 248 for (unsigned j = 1; j < nCol; ++j) { 249 if (j == pivotCol) 250 continue; 251 // Add rather than subtract because the pivot row has been negated. 252 tableau(row, j) = tableau(row, j) * tableau(pivotRow, 0) + 253 tableau(row, pivotCol) * tableau(pivotRow, j); 254 } 255 tableau(row, pivotCol) *= tableau(pivotRow, pivotCol); 256 normalizeRow(row); 257 } 258 } 259 260 /// Perform pivots until the unknown has a non-negative sample value or until 261 /// no more upward pivots can be performed. Return the sign of the final sample 262 /// value. 263 LogicalResult Simplex::restoreRow(Unknown &u) { 264 assert(u.orientation == Orientation::Row && 265 "unknown should be in row position"); 266 267 while (tableau(u.pos, 1) < 0) { 268 Optional<Pivot> maybePivot = findPivot(u.pos, Direction::Up); 269 if (!maybePivot) 270 break; 271 272 pivot(*maybePivot); 273 if (u.orientation == Orientation::Column) 274 return LogicalResult::Success; // the unknown is unbounded above. 275 } 276 return success(tableau(u.pos, 1) >= 0); 277 } 278 279 /// Find a row that can be used to pivot the column in the specified direction. 280 /// This returns an empty optional if and only if the column is unbounded in the 281 /// specified direction (ignoring skipRow, if skipRow is set). 282 /// 283 /// If skipRow is set, this row is not considered, and (if it is restricted) its 284 /// restriction may be violated by the returned pivot. Usually, skipRow is set 285 /// because we don't want to move it to column position unless it is unbounded, 286 /// and we are either trying to increase the value of skipRow or explicitly 287 /// trying to make skipRow negative, so we are not concerned about this. 288 /// 289 /// If the direction is up (resp. down) and a restricted row has a negative 290 /// (positive) coefficient for the column, then this row imposes a bound on how 291 /// much the sample value of the column can change. Such a row with constant 292 /// term c and coefficient f for the column imposes a bound of c/|f| on the 293 /// change in sample value (in the specified direction). (note that c is 294 /// non-negative here since the row is restricted and the tableau is consistent) 295 /// 296 /// We iterate through the rows and pick the row which imposes the most 297 /// stringent bound, since pivoting with a row changes the row's sample value to 298 /// 0 and hence saturates the bound it imposes. We break ties between rows that 299 /// impose the same bound by considering a lexicographic ordering where we 300 /// prefer unknowns with lower index value. 301 Optional<unsigned> Simplex::findPivotRow(Optional<unsigned> skipRow, 302 Direction direction, 303 unsigned col) const { 304 Optional<unsigned> retRow; 305 int64_t retElem, retConst; 306 for (unsigned row = 0; row < nRow; ++row) { 307 if (skipRow && row == *skipRow) 308 continue; 309 int64_t elem = tableau(row, col); 310 if (elem == 0) 311 continue; 312 if (!unknownFromRow(row).restricted) 313 continue; 314 if (signMatchesDirection(elem, direction)) 315 continue; 316 int64_t constTerm = tableau(row, 1); 317 318 if (!retRow) { 319 retRow = row; 320 retElem = elem; 321 retConst = constTerm; 322 continue; 323 } 324 325 int64_t diff = retConst * elem - constTerm * retElem; 326 if ((diff == 0 && rowUnknown[row] < rowUnknown[*retRow]) || 327 (diff != 0 && !signMatchesDirection(diff, direction))) { 328 retRow = row; 329 retElem = elem; 330 retConst = constTerm; 331 } 332 } 333 return retRow; 334 } 335 336 bool Simplex::isEmpty() const { return empty; } 337 338 void Simplex::swapRows(unsigned i, unsigned j) { 339 if (i == j) 340 return; 341 tableau.swapRows(i, j); 342 std::swap(rowUnknown[i], rowUnknown[j]); 343 unknownFromRow(i).pos = i; 344 unknownFromRow(j).pos = j; 345 } 346 347 /// Mark this tableau empty and push an entry to the undo stack. 348 void Simplex::markEmpty() { 349 undoLog.push_back(UndoLogEntry::UnmarkEmpty); 350 empty = true; 351 } 352 353 /// Add an inequality to the tableau. If coeffs is c_0, c_1, ... c_n, where n 354 /// is the curent number of variables, then the corresponding inequality is 355 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} >= 0. 356 /// 357 /// We add the inequality and mark it as restricted. We then try to make its 358 /// sample value non-negative. If this is not possible, the tableau has become 359 /// empty and we mark it as such. 360 void Simplex::addInequality(ArrayRef<int64_t> coeffs) { 361 unsigned conIndex = addRow(coeffs); 362 Unknown &u = con[conIndex]; 363 u.restricted = true; 364 LogicalResult result = restoreRow(u); 365 if (failed(result)) 366 markEmpty(); 367 } 368 369 /// Add an equality to the tableau. If coeffs is c_0, c_1, ... c_n, where n 370 /// is the curent number of variables, then the corresponding equality is 371 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} == 0. 372 /// 373 /// We simply add two opposing inequalities, which force the expression to 374 /// be zero. 375 void Simplex::addEquality(ArrayRef<int64_t> coeffs) { 376 addInequality(coeffs); 377 SmallVector<int64_t, 8> negatedCoeffs; 378 for (int64_t coeff : coeffs) 379 negatedCoeffs.emplace_back(-coeff); 380 addInequality(negatedCoeffs); 381 } 382 383 unsigned Simplex::numVariables() const { return var.size(); } 384 unsigned Simplex::numConstraints() const { return con.size(); } 385 386 /// Return a snapshot of the curent state. This is just the current size of the 387 /// undo log. 388 unsigned Simplex::getSnapshot() const { return undoLog.size(); } 389 390 void Simplex::undo(UndoLogEntry entry) { 391 if (entry == UndoLogEntry::RemoveLastConstraint) { 392 Unknown &constraint = con.back(); 393 if (constraint.orientation == Orientation::Column) { 394 unsigned column = constraint.pos; 395 Optional<unsigned> row; 396 397 // Try to find any pivot row for this column that preserves tableau 398 // consistency (except possibly the column itself, which is going to be 399 // deallocated anyway). 400 // 401 // If no pivot row is found in either direction, then the unknown is 402 // unbounded in both directions and we are free to 403 // perform any pivot at all. To do this, we just need to find any row with 404 // a non-zero coefficient for the column. 405 if (Optional<unsigned> maybeRow = 406 findPivotRow({}, Direction::Up, column)) { 407 row = *maybeRow; 408 } else if (Optional<unsigned> maybeRow = 409 findPivotRow({}, Direction::Down, column)) { 410 row = *maybeRow; 411 } else { 412 // The loop doesn't find a pivot row only if the column has zero 413 // coefficients for every row. But the unknown is a constraint, 414 // so it was added initially as a row. Such a row could never have been 415 // pivoted to a column. So a pivot row will always be found. 416 for (unsigned i = 0; i < nRow; ++i) { 417 if (tableau(i, column) != 0) { 418 row = i; 419 break; 420 } 421 } 422 } 423 assert(row.hasValue() && "No pivot row found!"); 424 pivot(*row, column); 425 } 426 427 // Move this unknown to the last row and remove the last row from the 428 // tableau. 429 swapRows(constraint.pos, nRow - 1); 430 // It is not strictly necessary to shrink the tableau, but for now we 431 // maintain the invariant that the tableau has exactly nRow rows. 432 tableau.resizeVertically(nRow - 1); 433 nRow--; 434 rowUnknown.pop_back(); 435 con.pop_back(); 436 } else if (entry == UndoLogEntry::UnmarkEmpty) { 437 empty = false; 438 } 439 } 440 441 /// Rollback to the specified snapshot. 442 /// 443 /// We undo all the log entries until the log size when the snapshot was taken 444 /// is reached. 445 void Simplex::rollback(unsigned snapshot) { 446 while (undoLog.size() > snapshot) { 447 undo(undoLog.back()); 448 undoLog.pop_back(); 449 } 450 } 451 452 Optional<Fraction> Simplex::computeRowOptimum(Direction direction, 453 unsigned row) { 454 // Keep trying to find a pivot for the row in the specified direction. 455 while (Optional<Pivot> maybePivot = findPivot(row, direction)) { 456 // If findPivot returns a pivot involving the row itself, then the optimum 457 // is unbounded, so we return None. 458 if (maybePivot->row == row) 459 return {}; 460 pivot(*maybePivot); 461 } 462 463 // The row has reached its optimal sample value, which we return. 464 // The sample value is the entry in the constant column divided by the common 465 // denominator for this row. 466 return Fraction(tableau(row, 1), tableau(row, 0)); 467 } 468 469 /// Compute the optimum of the specified expression in the specified direction, 470 /// or None if it is unbounded. 471 Optional<Fraction> Simplex::computeOptimum(Direction direction, 472 ArrayRef<int64_t> coeffs) { 473 assert(!empty && "Tableau should not be empty"); 474 475 unsigned snapshot = getSnapshot(); 476 unsigned conIndex = addRow(coeffs); 477 unsigned row = con[conIndex].pos; 478 Optional<Fraction> optimum = computeRowOptimum(direction, row); 479 rollback(snapshot); 480 return optimum; 481 } 482 483 bool Simplex::isUnbounded() { 484 if (empty) 485 return false; 486 487 SmallVector<int64_t, 8> dir(var.size() + 1); 488 for (unsigned i = 0; i < var.size(); ++i) { 489 dir[i] = 1; 490 491 Optional<Fraction> maybeMax = computeOptimum(Direction::Up, dir); 492 if (!maybeMax) 493 return true; 494 495 Optional<Fraction> maybeMin = computeOptimum(Direction::Down, dir); 496 if (!maybeMin) 497 return true; 498 499 dir[i] = 0; 500 } 501 return false; 502 } 503 504 /// Make a tableau to represent a pair of points in the original tableau. 505 /// 506 /// The product constraints and variables are stored as: first A's, then B's. 507 /// 508 /// The product tableau has row layout: 509 /// A's rows, B's rows. 510 /// 511 /// It has column layout: 512 /// denominator, constant, A's columns, B's columns. 513 Simplex Simplex::makeProduct(const Simplex &a, const Simplex &b) { 514 unsigned numVar = a.numVariables() + b.numVariables(); 515 unsigned numCon = a.numConstraints() + b.numConstraints(); 516 Simplex result(numVar); 517 518 result.tableau.resizeVertically(numCon); 519 result.empty = a.empty || b.empty; 520 521 auto concat = [](ArrayRef<Unknown> v, ArrayRef<Unknown> w) { 522 SmallVector<Unknown, 8> result; 523 result.reserve(v.size() + w.size()); 524 result.insert(result.end(), v.begin(), v.end()); 525 result.insert(result.end(), w.begin(), w.end()); 526 return result; 527 }; 528 result.con = concat(a.con, b.con); 529 result.var = concat(a.var, b.var); 530 531 auto indexFromBIndex = [&](int index) { 532 return index >= 0 ? a.numVariables() + index 533 : ~(a.numConstraints() + ~index); 534 }; 535 536 result.colUnknown.assign(2, nullIndex); 537 for (unsigned i = 2; i < a.nCol; ++i) { 538 result.colUnknown.push_back(a.colUnknown[i]); 539 result.unknownFromIndex(result.colUnknown.back()).pos = 540 result.colUnknown.size() - 1; 541 } 542 for (unsigned i = 2; i < b.nCol; ++i) { 543 result.colUnknown.push_back(indexFromBIndex(b.colUnknown[i])); 544 result.unknownFromIndex(result.colUnknown.back()).pos = 545 result.colUnknown.size() - 1; 546 } 547 548 auto appendRowFromA = [&](unsigned row) { 549 for (unsigned col = 0; col < a.nCol; ++col) 550 result.tableau(result.nRow, col) = a.tableau(row, col); 551 result.rowUnknown.push_back(a.rowUnknown[row]); 552 result.unknownFromIndex(result.rowUnknown.back()).pos = 553 result.rowUnknown.size() - 1; 554 result.nRow++; 555 }; 556 557 // Also fixes the corresponding entry in rowUnknown and var/con (as the case 558 // may be). 559 auto appendRowFromB = [&](unsigned row) { 560 result.tableau(result.nRow, 0) = b.tableau(row, 0); 561 result.tableau(result.nRow, 1) = b.tableau(row, 1); 562 563 unsigned offset = a.nCol - 2; 564 for (unsigned col = 2; col < b.nCol; ++col) 565 result.tableau(result.nRow, offset + col) = b.tableau(row, col); 566 result.rowUnknown.push_back(indexFromBIndex(b.rowUnknown[row])); 567 result.unknownFromIndex(result.rowUnknown.back()).pos = 568 result.rowUnknown.size() - 1; 569 result.nRow++; 570 }; 571 572 for (unsigned row = 0; row < a.nRow; ++row) 573 appendRowFromA(row); 574 for (unsigned row = 0; row < b.nRow; ++row) 575 appendRowFromB(row); 576 577 return result; 578 } 579 580 Optional<SmallVector<int64_t, 8>> Simplex::getSamplePointIfIntegral() const { 581 // The tableau is empty, so no sample point exists. 582 if (empty) 583 return {}; 584 585 SmallVector<int64_t, 8> sample; 586 // Push the sample value for each variable into the vector. 587 for (const Unknown &u : var) { 588 if (u.orientation == Orientation::Column) { 589 // If the variable is in column position, its sample value is zero. 590 sample.push_back(0); 591 } else { 592 // If the variable is in row position, its sample value is the entry in 593 // the constant column divided by the entry in the common denominator 594 // column. If this is not an integer, then the sample point is not 595 // integral so we return None. 596 if (tableau(u.pos, 1) % tableau(u.pos, 0) != 0) 597 return {}; 598 sample.push_back(tableau(u.pos, 1) / tableau(u.pos, 0)); 599 } 600 } 601 return sample; 602 } 603 604 /// Given a simplex for a polytope, construct a new simplex whose variables are 605 /// identified with a pair of points (x, y) in the original polytope. Supports 606 /// some operations needed for generalized basis reduction. In what follows, 607 /// dotProduct(x, y) = x_1 * y_1 + x_2 * y_2 + ... x_n * y_n where n is the 608 /// dimension of the original polytope. 609 /// 610 /// This supports adding equality constraints dotProduct(dir, x - y) == 0. It 611 /// also supports rolling back this addition, by maintaining a snapshot stack 612 /// that contains a snapshot of the Simplex's state for each equality, just 613 /// before that equality was added. 614 class GBRSimplex { 615 using Orientation = Simplex::Orientation; 616 617 public: 618 GBRSimplex(const Simplex &originalSimplex) 619 : simplex(Simplex::makeProduct(originalSimplex, originalSimplex)), 620 simplexConstraintOffset(simplex.numConstraints()) {} 621 622 /// Add an equality dotProduct(dir, x - y) == 0. 623 /// First pushes a snapshot for the current simplex state to the stack so 624 /// that this can be rolled back later. 625 void addEqualityForDirection(ArrayRef<int64_t> dir) { 626 assert( 627 std::any_of(dir.begin(), dir.end(), [](int64_t x) { return x != 0; }) && 628 "Direction passed is the zero vector!"); 629 snapshotStack.push_back(simplex.getSnapshot()); 630 simplex.addEquality(getCoeffsForDirection(dir)); 631 } 632 633 /// Compute max(dotProduct(dir, x - y)) and save the dual variables for only 634 /// the direction equalities to `dual`. 635 Fraction computeWidthAndDuals(ArrayRef<int64_t> dir, 636 SmallVectorImpl<int64_t> &dual, 637 int64_t &dualDenom) { 638 unsigned snap = simplex.getSnapshot(); 639 unsigned conIndex = simplex.addRow(getCoeffsForDirection(dir)); 640 unsigned row = simplex.con[conIndex].pos; 641 Optional<Fraction> maybeWidth = 642 simplex.computeRowOptimum(Simplex::Direction::Up, row); 643 assert(maybeWidth.hasValue() && "Width should not be unbounded!"); 644 dualDenom = simplex.tableau(row, 0); 645 dual.clear(); 646 // The increment is i += 2 because equalities are added as two inequalities, 647 // one positive and one negative. Each iteration processes one equality. 648 for (unsigned i = simplexConstraintOffset; i < conIndex; i += 2) { 649 // The dual variable is the negative of the coefficient of the new row 650 // in the column of the constraint, if the constraint is in a column. 651 // Note that the second inequality for the equality is negated. 652 // 653 // We want the dual for the original equality. If the positive inequality 654 // is in column position, the negative of its row coefficient is the 655 // desired dual. If the negative inequality is in column position, its row 656 // coefficient is the desired dual. (its coefficients are already the 657 // negated coefficients of the original equality, so we don't need to 658 // negate it now.) 659 // 660 // If neither are in column position, we move the negated inequality to 661 // column position. Since the inequality must have sample value zero 662 // (since it corresponds to an equality), we are free to pivot with 663 // any column. Since both the unknowns have sample value before and after 664 // pivoting, no other sample values will change and the tableau will 665 // remain consistent. To pivot, we just need to find a column that has a 666 // non-zero coefficient in this row. There must be one since otherwise the 667 // equality would be 0 == 0, which should never be passed to 668 // addEqualityForDirection. 669 // 670 // After finding a column, we pivot with the column, after which we can 671 // get the dual from the inequality in column position as explained above. 672 if (simplex.con[i].orientation == Orientation::Column) { 673 dual.push_back(-simplex.tableau(row, simplex.con[i].pos)); 674 } else { 675 if (simplex.con[i + 1].orientation == Orientation::Row) { 676 unsigned ineqRow = simplex.con[i + 1].pos; 677 // Since it is an equality, the sample value must be zero. 678 assert(simplex.tableau(ineqRow, 1) == 0 && 679 "Equality's sample value must be zero."); 680 for (unsigned col = 2; col < simplex.nCol; ++col) { 681 if (simplex.tableau(ineqRow, col) != 0) { 682 simplex.pivot(ineqRow, col); 683 break; 684 } 685 } 686 assert(simplex.con[i + 1].orientation == Orientation::Column && 687 "No pivot found. Equality has all-zeros row in tableau!"); 688 } 689 dual.push_back(simplex.tableau(row, simplex.con[i + 1].pos)); 690 } 691 } 692 simplex.rollback(snap); 693 return *maybeWidth; 694 } 695 696 /// Remove the last equality that was added through addEqualityForDirection. 697 /// 698 /// We do this by rolling back to the snapshot at the top of the stack, which 699 /// should be a snapshot taken just before the last equality was added. 700 void removeLastEquality() { 701 assert(!snapshotStack.empty() && "Snapshot stack is empty!"); 702 simplex.rollback(snapshotStack.back()); 703 snapshotStack.pop_back(); 704 } 705 706 private: 707 /// Returns coefficients of the expression 'dot_product(dir, x - y)', 708 /// i.e., dir_1 * x_1 + dir_2 * x_2 + ... + dir_n * x_n 709 /// - dir_1 * y_1 - dir_2 * y_2 - ... - dir_n * y_n, 710 /// where n is the dimension of the original polytope. 711 SmallVector<int64_t, 8> getCoeffsForDirection(ArrayRef<int64_t> dir) { 712 assert(2 * dir.size() == simplex.numVariables() && 713 "Direction vector has wrong dimensionality"); 714 SmallVector<int64_t, 8> coeffs(dir.begin(), dir.end()); 715 coeffs.reserve(2 * dir.size()); 716 for (int64_t coeff : dir) 717 coeffs.push_back(-coeff); 718 coeffs.push_back(0); // constant term 719 return coeffs; 720 } 721 722 Simplex simplex; 723 /// The first index of the equality constraints, the index immediately after 724 /// the last constraint in the initial product simplex. 725 unsigned simplexConstraintOffset; 726 /// A stack of snapshots, used for rolling back. 727 SmallVector<unsigned, 8> snapshotStack; 728 }; 729 730 /// Reduce the basis to try and find a direction in which the polytope is 731 /// "thin". This only works for bounded polytopes. 732 /// 733 /// This is an implementation of the algorithm described in the paper 734 /// "An Implementation of Generalized Basis Reduction for Integer Programming" 735 /// by W. Cook, T. Rutherford, H. E. Scarf, D. Shallcross. 736 /// 737 /// Let b_{level}, b_{level + 1}, ... b_n be the current basis. 738 /// Let width_i(v) = max <v, x - y> where x and y are points in the original 739 /// polytope such that <b_j, x - y> = 0 is satisfied for all level <= j < i. 740 /// 741 /// In every iteration, we first replace b_{i+1} with b_{i+1} + u*b_i, where u 742 /// is the integer such that width_i(b_{i+1} + u*b_i) is minimized. Let dual_i 743 /// be the dual variable associated with the constraint <b_i, x - y> = 0 when 744 /// computing width_{i+1}(b_{i+1}). It can be shown that dual_i is the 745 /// minimizing value of u, if it were allowed to be fractional. Due to 746 /// convexity, the minimizing integer value is either floor(dual_i) or 747 /// ceil(dual_i), so we just need to check which of these gives a lower 748 /// width_{i+1} value. If dual_i turned out to be an integer, then u = dual_i. 749 /// 750 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and (the new) 751 /// b_{i + 1} and decrement i (unless i = level, in which case we stay at the 752 /// same i). Otherwise, we increment i. 753 /// 754 /// We keep f values and duals cached and invalidate them when necessary. 755 /// Whenever possible, we use them instead of recomputing them. We implement the 756 /// algorithm as follows. 757 /// 758 /// In an iteration at i we need to compute: 759 /// a) width_i(b_{i + 1}) 760 /// b) width_i(b_i) 761 /// c) the integer u that minimizes width_i(b_{i + 1} + u*b_i) 762 /// 763 /// If width_i(b_i) is not already cached, we compute it. 764 /// 765 /// If the duals are not already cached, we compute width_{i+1}(b_{i+1}) and 766 /// store the duals from this computation. 767 /// 768 /// We call updateBasisWithUAndGetFCandidate, which finds the minimizing value 769 /// of u as explained before, caches the duals from this computation, sets 770 /// b_{i+1} to b_{i+1} + u*b_i, and returns the new value of width_i(b_{i+1}). 771 /// 772 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and b_{i+1} and 773 /// decrement i, resulting in the basis 774 /// ... b_{i - 1}, b_{i + 1} + u*b_i, b_i, b_{i+2}, ... 775 /// with corresponding f values 776 /// ... width_{i-1}(b_{i-1}), width_i(b_{i+1} + u*b_i), width_{i+1}(b_i), ... 777 /// The values up to i - 1 remain unchanged. We have just gotten the middle 778 /// value from updateBasisWithUAndGetFCandidate, so we can update that in the 779 /// cache. The value at width_{i+1}(b_i) is unknown, so we evict this value from 780 /// the cache. The iteration after decrementing needs exactly the duals from the 781 /// computation of width_i(b_{i + 1} + u*b_i), so we keep these in the cache. 782 /// 783 /// When incrementing i, no cached f values get invalidated. However, the cached 784 /// duals do get invalidated as the duals for the higher levels are different. 785 void Simplex::reduceBasis(Matrix &basis, unsigned level) { 786 const Fraction epsilon(3, 4); 787 788 if (level == basis.getNumRows() - 1) 789 return; 790 791 GBRSimplex gbrSimplex(*this); 792 SmallVector<Fraction, 8> width; 793 SmallVector<int64_t, 8> dual; 794 int64_t dualDenom; 795 796 // Finds the value of u that minimizes width_i(b_{i+1} + u*b_i), caches the 797 // duals from this computation, sets b_{i+1} to b_{i+1} + u*b_i, and returns 798 // the new value of width_i(b_{i+1}). 799 // 800 // If dual_i is not an integer, the minimizing value must be either 801 // floor(dual_i) or ceil(dual_i). We compute the expression for both and 802 // choose the minimizing value. 803 // 804 // If dual_i is an integer, we don't need to perform these computations. We 805 // know that in this case, 806 // a) u = dual_i. 807 // b) one can show that dual_j for j < i are the same duals we would have 808 // gotten from computing width_i(b_{i + 1} + u*b_i), so the correct duals 809 // are the ones already in the cache. 810 // c) width_i(b_{i+1} + u*b_i) = min_{alpha} width_i(b_{i+1} + alpha * b_i), 811 // which 812 // one can show is equal to width_{i+1}(b_{i+1}). The latter value must 813 // be in the cache, so we get it from there and return it. 814 auto updateBasisWithUAndGetFCandidate = [&](unsigned i) -> Fraction { 815 assert(i < level + dual.size() && "dual_i is not known!"); 816 817 int64_t u = floorDiv(dual[i - level], dualDenom); 818 basis.addToRow(i, i + 1, u); 819 if (dual[i - level] % dualDenom != 0) { 820 SmallVector<int64_t, 8> candidateDual[2]; 821 int64_t candidateDualDenom[2]; 822 Fraction widthI[2]; 823 824 // Initially u is floor(dual) and basis reflects this. 825 widthI[0] = gbrSimplex.computeWidthAndDuals( 826 basis.getRow(i + 1), candidateDual[0], candidateDualDenom[0]); 827 828 // Now try ceil(dual), i.e. floor(dual) + 1. 829 ++u; 830 basis.addToRow(i, i + 1, 1); 831 widthI[1] = gbrSimplex.computeWidthAndDuals( 832 basis.getRow(i + 1), candidateDual[1], candidateDualDenom[1]); 833 834 unsigned j = widthI[0] < widthI[1] ? 0 : 1; 835 if (j == 0) 836 // Subtract 1 to go from u = ceil(dual) back to floor(dual). 837 basis.addToRow(i, i + 1, -1); 838 dual = std::move(candidateDual[j]); 839 dualDenom = candidateDualDenom[j]; 840 return widthI[j]; 841 } 842 assert(i + 1 - level < width.size() && "width_{i+1} wasn't saved"); 843 // When dual minimizes f_i(b_{i+1} + dual*b_i), this is equal to 844 // width_{i+1}(b_{i+1}). 845 return width[i + 1 - level]; 846 }; 847 848 // In the ith iteration of the loop, gbrSimplex has constraints for directions 849 // from `level` to i - 1. 850 unsigned i = level; 851 while (i < basis.getNumRows() - 1) { 852 if (i >= level + width.size()) { 853 // We don't even know the value of f_i(b_i), so let's find that first. 854 // We have to do this first since later we assume that width already 855 // contains values up to and including i. 856 857 assert((i == 0 || i - 1 < level + width.size()) && 858 "We are at level i but we don't know the value of width_{i-1}"); 859 860 // We don't actually use these duals at all, but it doesn't matter 861 // because this case should only occur when i is level, and there are no 862 // duals in that case anyway. 863 assert(i == level && "This case should only occur when i == level"); 864 width.push_back( 865 gbrSimplex.computeWidthAndDuals(basis.getRow(i), dual, dualDenom)); 866 } 867 868 if (i >= level + dual.size()) { 869 assert(i + 1 >= level + width.size() && 870 "We don't know dual_i but we know width_{i+1}"); 871 // We don't know dual for our level, so let's find it. 872 gbrSimplex.addEqualityForDirection(basis.getRow(i)); 873 width.push_back(gbrSimplex.computeWidthAndDuals(basis.getRow(i + 1), dual, 874 dualDenom)); 875 gbrSimplex.removeLastEquality(); 876 } 877 878 // This variable stores width_i(b_{i+1} + u*b_i). 879 Fraction widthICandidate = updateBasisWithUAndGetFCandidate(i); 880 if (widthICandidate < epsilon * width[i - level]) { 881 basis.swapRows(i, i + 1); 882 width[i - level] = widthICandidate; 883 // The values of width_{i+1}(b_{i+1}) and higher may change after the 884 // swap, so we remove the cached values here. 885 width.resize(i - level + 1); 886 if (i == level) { 887 dual.clear(); 888 continue; 889 } 890 891 gbrSimplex.removeLastEquality(); 892 i--; 893 continue; 894 } 895 896 // Invalidate duals since the higher level needs to recompute its own duals. 897 dual.clear(); 898 gbrSimplex.addEqualityForDirection(basis.getRow(i)); 899 i++; 900 } 901 } 902 903 /// Search for an integer sample point using a branch and bound algorithm. 904 /// 905 /// Each row in the basis matrix is a vector, and the set of basis vectors 906 /// should span the space. Initially this is the identity matrix, 907 /// i.e., the basis vectors are just the variables. 908 /// 909 /// In every level, a value is assigned to the level-th basis vector, as 910 /// follows. Compute the minimum and maximum rational values of this direction. 911 /// If only one integer point lies in this range, constrain the variable to 912 /// have this value and recurse to the next variable. 913 /// 914 /// If the range has multiple values, perform generalized basis reduction via 915 /// reduceBasis and then compute the bounds again. Now we try constraining 916 /// this direction in the first value in this range and "recurse" to the next 917 /// level. If we fail to find a sample, we try assigning the direction the next 918 /// value in this range, and so on. 919 /// 920 /// If no integer sample is found from any of the assignments, or if the range 921 /// contains no integer value, then of course the polytope is empty for the 922 /// current assignment of the values in previous levels, so we return to 923 /// the previous level. 924 /// 925 /// If we reach the last level where all the variables have been assigned values 926 /// already, then we simply return the current sample point if it is integral, 927 /// and go back to the previous level otherwise. 928 /// 929 /// To avoid potentially arbitrarily large recursion depths leading to stack 930 /// overflows, this algorithm is implemented iteratively. 931 Optional<SmallVector<int64_t, 8>> Simplex::findIntegerSample() { 932 if (empty) 933 return {}; 934 935 unsigned nDims = var.size(); 936 Matrix basis = Matrix::identity(nDims); 937 938 unsigned level = 0; 939 // The snapshot just before constraining a direction to a value at each level. 940 SmallVector<unsigned, 8> snapshotStack; 941 // The maximum value in the range of the direction for each level. 942 SmallVector<int64_t, 8> upperBoundStack; 943 // The next value to try constraining the basis vector to at each level. 944 SmallVector<int64_t, 8> nextValueStack; 945 946 snapshotStack.reserve(basis.getNumRows()); 947 upperBoundStack.reserve(basis.getNumRows()); 948 nextValueStack.reserve(basis.getNumRows()); 949 while (level != -1u) { 950 if (level == basis.getNumRows()) { 951 // We've assigned values to all variables. Return if we have a sample, 952 // or go back up to the previous level otherwise. 953 if (auto maybeSample = getSamplePointIfIntegral()) 954 return maybeSample; 955 level--; 956 continue; 957 } 958 959 if (level >= upperBoundStack.size()) { 960 // We haven't populated the stack values for this level yet, so we have 961 // just come down a level ("recursed"). Find the lower and upper bounds. 962 // If there is more than one integer point in the range, perform 963 // generalized basis reduction. 964 SmallVector<int64_t, 8> basisCoeffs = 965 llvm::to_vector<8>(basis.getRow(level)); 966 basisCoeffs.push_back(0); 967 968 int64_t minRoundedUp, maxRoundedDown; 969 std::tie(minRoundedUp, maxRoundedDown) = 970 computeIntegerBounds(basisCoeffs); 971 972 // Heuristic: if the sample point is integral at this point, just return 973 // it. 974 if (auto maybeSample = getSamplePointIfIntegral()) 975 return *maybeSample; 976 977 if (minRoundedUp < maxRoundedDown) { 978 reduceBasis(basis, level); 979 basisCoeffs = llvm::to_vector<8>(basis.getRow(level)); 980 basisCoeffs.push_back(0); 981 std::tie(minRoundedUp, maxRoundedDown) = 982 computeIntegerBounds(basisCoeffs); 983 } 984 985 snapshotStack.push_back(getSnapshot()); 986 // The smallest value in the range is the next value to try. 987 nextValueStack.push_back(minRoundedUp); 988 upperBoundStack.push_back(maxRoundedDown); 989 } 990 991 assert((snapshotStack.size() - 1 == level && 992 nextValueStack.size() - 1 == level && 993 upperBoundStack.size() - 1 == level) && 994 "Mismatched variable stack sizes!"); 995 996 // Whether we "recursed" or "returned" from a lower level, we rollback 997 // to the snapshot of the starting state at this level. (in the "recursed" 998 // case this has no effect) 999 rollback(snapshotStack.back()); 1000 int64_t nextValue = nextValueStack.back(); 1001 nextValueStack.back()++; 1002 if (nextValue > upperBoundStack.back()) { 1003 // We have exhausted the range and found no solution. Pop the stack and 1004 // return up a level. 1005 snapshotStack.pop_back(); 1006 nextValueStack.pop_back(); 1007 upperBoundStack.pop_back(); 1008 level--; 1009 continue; 1010 } 1011 1012 // Try the next value in the range and "recurse" into the next level. 1013 SmallVector<int64_t, 8> basisCoeffs(basis.getRow(level).begin(), 1014 basis.getRow(level).end()); 1015 basisCoeffs.push_back(-nextValue); 1016 addEquality(basisCoeffs); 1017 level++; 1018 } 1019 1020 return {}; 1021 } 1022 1023 /// Compute the minimum and maximum integer values the expression can take. We 1024 /// compute each separately. 1025 std::pair<int64_t, int64_t> 1026 Simplex::computeIntegerBounds(ArrayRef<int64_t> coeffs) { 1027 int64_t minRoundedUp; 1028 if (Optional<Fraction> maybeMin = 1029 computeOptimum(Simplex::Direction::Down, coeffs)) 1030 minRoundedUp = ceil(*maybeMin); 1031 else 1032 llvm_unreachable("Tableau should not be unbounded"); 1033 1034 int64_t maxRoundedDown; 1035 if (Optional<Fraction> maybeMax = 1036 computeOptimum(Simplex::Direction::Up, coeffs)) 1037 maxRoundedDown = floor(*maybeMax); 1038 else 1039 llvm_unreachable("Tableau should not be unbounded"); 1040 1041 return {minRoundedUp, maxRoundedDown}; 1042 } 1043 1044 void Simplex::print(raw_ostream &os) const { 1045 os << "rows = " << nRow << ", columns = " << nCol << "\n"; 1046 if (empty) 1047 os << "Simplex marked empty!\n"; 1048 os << "var: "; 1049 for (unsigned i = 0; i < var.size(); ++i) { 1050 if (i > 0) 1051 os << ", "; 1052 var[i].print(os); 1053 } 1054 os << "\ncon: "; 1055 for (unsigned i = 0; i < con.size(); ++i) { 1056 if (i > 0) 1057 os << ", "; 1058 con[i].print(os); 1059 } 1060 os << '\n'; 1061 for (unsigned row = 0; row < nRow; ++row) { 1062 if (row > 0) 1063 os << ", "; 1064 os << "r" << row << ": " << rowUnknown[row]; 1065 } 1066 os << '\n'; 1067 os << "c0: denom, c1: const"; 1068 for (unsigned col = 2; col < nCol; ++col) 1069 os << ", c" << col << ": " << colUnknown[col]; 1070 os << '\n'; 1071 for (unsigned row = 0; row < nRow; ++row) { 1072 for (unsigned col = 0; col < nCol; ++col) 1073 os << tableau(row, col) << '\t'; 1074 os << '\n'; 1075 } 1076 os << '\n'; 1077 } 1078 1079 void Simplex::dump() const { print(llvm::errs()); } 1080 1081 } // namespace mlir 1082