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 #include "llvm/ADT/Optional.h" 13 #include "llvm/Support/Compiler.h" 14 15 using namespace mlir; 16 using namespace presburger; 17 18 using Direction = Simplex::Direction; 19 20 const int nullIndex = std::numeric_limits<int>::max(); 21 22 // Return a + scale*b; 23 LLVM_ATTRIBUTE_UNUSED 24 static SmallVector<int64_t, 8> 25 scaleAndAddForAssert(ArrayRef<int64_t> a, int64_t scale, ArrayRef<int64_t> b) { 26 assert(a.size() == b.size()); 27 SmallVector<int64_t, 8> res; 28 res.reserve(a.size()); 29 for (unsigned i = 0, e = a.size(); i < e; ++i) 30 res.push_back(a[i] + scale * b[i]); 31 return res; 32 } 33 34 SimplexBase::SimplexBase(unsigned nVar, bool mustUseBigM) 35 : usingBigM(mustUseBigM), nRedundant(0), nSymbol(0), 36 tableau(0, getNumFixedCols() + nVar), empty(false) { 37 colUnknown.insert(colUnknown.begin(), getNumFixedCols(), nullIndex); 38 for (unsigned i = 0; i < nVar; ++i) { 39 var.emplace_back(Orientation::Column, /*restricted=*/false, 40 /*pos=*/getNumFixedCols() + i); 41 colUnknown.push_back(i); 42 } 43 } 44 45 SimplexBase::SimplexBase(unsigned nVar, bool mustUseBigM, 46 const llvm::SmallBitVector &isSymbol) 47 : SimplexBase(nVar, mustUseBigM) { 48 assert(isSymbol.size() == nVar && "invalid bitmask!"); 49 // Invariant: nSymbol is the number of symbols that have been marked 50 // already and these occupy the columns 51 // [getNumFixedCols(), getNumFixedCols() + nSymbol). 52 for (unsigned symbolIdx : isSymbol.set_bits()) { 53 var[symbolIdx].isSymbol = true; 54 swapColumns(var[symbolIdx].pos, getNumFixedCols() + nSymbol); 55 ++nSymbol; 56 } 57 } 58 59 const Simplex::Unknown &SimplexBase::unknownFromIndex(int index) const { 60 assert(index != nullIndex && "nullIndex passed to unknownFromIndex"); 61 return index >= 0 ? var[index] : con[~index]; 62 } 63 64 const Simplex::Unknown &SimplexBase::unknownFromColumn(unsigned col) const { 65 assert(col < getNumColumns() && "Invalid column"); 66 return unknownFromIndex(colUnknown[col]); 67 } 68 69 const Simplex::Unknown &SimplexBase::unknownFromRow(unsigned row) const { 70 assert(row < getNumRows() && "Invalid row"); 71 return unknownFromIndex(rowUnknown[row]); 72 } 73 74 Simplex::Unknown &SimplexBase::unknownFromIndex(int index) { 75 assert(index != nullIndex && "nullIndex passed to unknownFromIndex"); 76 return index >= 0 ? var[index] : con[~index]; 77 } 78 79 Simplex::Unknown &SimplexBase::unknownFromColumn(unsigned col) { 80 assert(col < getNumColumns() && "Invalid column"); 81 return unknownFromIndex(colUnknown[col]); 82 } 83 84 Simplex::Unknown &SimplexBase::unknownFromRow(unsigned row) { 85 assert(row < getNumRows() && "Invalid row"); 86 return unknownFromIndex(rowUnknown[row]); 87 } 88 89 unsigned SimplexBase::addZeroRow(bool makeRestricted) { 90 // Resize the tableau to accommodate the extra row. 91 unsigned newRow = tableau.appendExtraRow(); 92 assert(getNumRows() == getNumRows() && "Inconsistent tableau size"); 93 rowUnknown.push_back(~con.size()); 94 con.emplace_back(Orientation::Row, makeRestricted, newRow); 95 undoLog.push_back(UndoLogEntry::RemoveLastConstraint); 96 tableau(newRow, 0) = 1; 97 return newRow; 98 } 99 100 /// Add a new row to the tableau corresponding to the given constant term and 101 /// list of coefficients. The coefficients are specified as a vector of 102 /// (variable index, coefficient) pairs. 103 unsigned SimplexBase::addRow(ArrayRef<int64_t> coeffs, bool makeRestricted) { 104 assert(coeffs.size() == var.size() + 1 && 105 "Incorrect number of coefficients!"); 106 assert(var.size() + getNumFixedCols() == getNumColumns() && 107 "inconsistent column count!"); 108 109 unsigned newRow = addZeroRow(makeRestricted); 110 tableau(newRow, 1) = coeffs.back(); 111 if (usingBigM) { 112 // When the lexicographic pivot rule is used, instead of the variables 113 // 114 // x, y, z ... 115 // 116 // we internally use the variables 117 // 118 // M, M + x, M + y, M + z, ... 119 // 120 // where M is the big M parameter. As such, when the user tries to add 121 // a row ax + by + cz + d, we express it in terms of our internal variables 122 // as -(a + b + c)M + a(M + x) + b(M + y) + c(M + z) + d. 123 // 124 // Symbols don't use the big M parameter since they do not get lex 125 // optimized. 126 int64_t bigMCoeff = 0; 127 for (unsigned i = 0; i < coeffs.size() - 1; ++i) 128 if (!var[i].isSymbol) 129 bigMCoeff -= coeffs[i]; 130 // The coefficient to the big M parameter is stored in column 2. 131 tableau(newRow, 2) = bigMCoeff; 132 } 133 134 // Process each given variable coefficient. 135 for (unsigned i = 0; i < var.size(); ++i) { 136 unsigned pos = var[i].pos; 137 if (coeffs[i] == 0) 138 continue; 139 140 if (var[i].orientation == Orientation::Column) { 141 // If a variable is in column position at column col, then we just add the 142 // coefficient for that variable (scaled by the common row denominator) to 143 // the corresponding entry in the new row. 144 tableau(newRow, pos) += coeffs[i] * tableau(newRow, 0); 145 continue; 146 } 147 148 // If the variable is in row position, we need to add that row to the new 149 // row, scaled by the coefficient for the variable, accounting for the two 150 // rows potentially having different denominators. The new denominator is 151 // the lcm of the two. 152 int64_t lcm = mlir::lcm(tableau(newRow, 0), tableau(pos, 0)); 153 int64_t nRowCoeff = lcm / tableau(newRow, 0); 154 int64_t idxRowCoeff = coeffs[i] * (lcm / tableau(pos, 0)); 155 tableau(newRow, 0) = lcm; 156 for (unsigned col = 1, e = getNumColumns(); col < e; ++col) 157 tableau(newRow, col) = 158 nRowCoeff * tableau(newRow, col) + idxRowCoeff * tableau(pos, col); 159 } 160 161 tableau.normalizeRow(newRow); 162 // Push to undo log along with the index of the new constraint. 163 return con.size() - 1; 164 } 165 166 namespace { 167 bool signMatchesDirection(int64_t elem, Direction direction) { 168 assert(elem != 0 && "elem should not be 0"); 169 return direction == Direction::Up ? elem > 0 : elem < 0; 170 } 171 172 Direction flippedDirection(Direction direction) { 173 return direction == Direction::Up ? Direction::Down : Simplex::Direction::Up; 174 } 175 } // namespace 176 177 /// We simply make the tableau consistent while maintaining a lexicopositive 178 /// basis transform, and then return the sample value. If the tableau becomes 179 /// empty, we return empty. 180 /// 181 /// Let the variables be x = (x_1, ... x_n). 182 /// Let the basis unknowns be y = (y_1, ... y_n). 183 /// We have that x = A*y + b for some n x n matrix A and n x 1 column vector b. 184 /// 185 /// As we will show below, A*y is either zero or lexicopositive. 186 /// Adding a lexicopositive vector to b will make it lexicographically 187 /// greater, so A*y + b is always equal to or lexicographically greater than b. 188 /// Thus, since we can attain x = b, that is the lexicographic minimum. 189 /// 190 /// We have that that every column in A is lexicopositive, i.e., has at least 191 /// one non-zero element, with the first such element being positive. Since for 192 /// the tableau to be consistent we must have non-negative sample values not 193 /// only for the constraints but also for the variables, we also have x >= 0 and 194 /// y >= 0, by which we mean every element in these vectors is non-negative. 195 /// 196 /// Proof that if every column in A is lexicopositive, and y >= 0, then 197 /// A*y is zero or lexicopositive. Begin by considering A_1, the first row of A. 198 /// If this row is all zeros, then (A*y)_1 = (A_1)*y = 0; proceed to the next 199 /// row. If we run out of rows, A*y is zero and we are done; otherwise, we 200 /// encounter some row A_i that has a non-zero element. Every column is 201 /// lexicopositive and so has some positive element before any negative elements 202 /// occur, so the element in this row for any column, if non-zero, must be 203 /// positive. Consider (A*y)_i = (A_i)*y. All the elements in both vectors are 204 /// non-negative, so if this is non-zero then it must be positive. Then the 205 /// first non-zero element of A*y is positive so A*y is lexicopositive. 206 /// 207 /// Otherwise, if (A_i)*y is zero, then for every column j that had a non-zero 208 /// element in A_i, y_j is zero. Thus these columns have no contribution to A*y 209 /// and we can completely ignore these columns of A. We now continue downwards, 210 /// looking for rows of A that have a non-zero element other than in the ignored 211 /// columns. If we find one, say A_k, once again these elements must be positive 212 /// since they are the first non-zero element in each of these columns, so if 213 /// (A_k)*y is not zero then we have that A*y is lexicopositive and if not we 214 /// add these to the set of ignored columns and continue to the next row. If we 215 /// run out of rows, then A*y is zero and we are done. 216 MaybeOptimum<SmallVector<Fraction, 8>> LexSimplex::findRationalLexMin() { 217 if (restoreRationalConsistency().failed()) { 218 markEmpty(); 219 return OptimumKind::Empty; 220 } 221 return getRationalSample(); 222 } 223 224 /// Given a row that has a non-integer sample value, add an inequality such 225 /// that this fractional sample value is cut away from the polytope. The added 226 /// inequality will be such that no integer points are removed. i.e., the 227 /// integer lexmin, if it exists, is the same with and without this constraint. 228 /// 229 /// Let the row be 230 /// (c + coeffM*M + a_1*s_1 + ... + a_m*s_m + b_1*y_1 + ... + b_n*y_n)/d, 231 /// where s_1, ... s_m are the symbols and 232 /// y_1, ... y_n are the other basis unknowns. 233 /// 234 /// For this to be an integer, we want 235 /// coeffM*M + a_1*s_1 + ... + a_m*s_m + b_1*y_1 + ... + b_n*y_n = -c (mod d) 236 /// Note that this constraint must always hold, independent of the basis, 237 /// becuse the row unknown's value always equals this expression, even if *we* 238 /// later compute the sample value from a different expression based on a 239 /// different basis. 240 /// 241 /// Let us assume that M has a factor of d in it. Imposing this constraint on M 242 /// does not in any way hinder us from finding a value of M that is big enough. 243 /// Moreover, this function is only called when the symbolic part of the sample, 244 /// a_1*s_1 + ... + a_m*s_m, is known to be an integer. 245 /// 246 /// Also, we can safely reduce the coefficients modulo d, so we have: 247 /// 248 /// (b_1%d)y_1 + ... + (b_n%d)y_n = (-c%d) + k*d for some integer `k` 249 /// 250 /// Note that all coefficient modulos here are non-negative. Also, all the 251 /// unknowns are non-negative here as both constraints and variables are 252 /// non-negative in LexSimplexBase. (We used the big M trick to make the 253 /// variables non-negative). Therefore, the LHS here is non-negative. 254 /// Since 0 <= (-c%d) < d, k is the quotient of dividing the LHS by d and 255 /// is therefore non-negative as well. 256 /// 257 /// So we have 258 /// ((b_1%d)y_1 + ... + (b_n%d)y_n - (-c%d))/d >= 0. 259 /// 260 /// The constraint is violated when added (it would be useless otherwise) 261 /// so we immediately try to move it to a column. 262 LogicalResult LexSimplexBase::addCut(unsigned row) { 263 int64_t d = tableau(row, 0); 264 unsigned cutRow = addZeroRow(/*makeRestricted=*/true); 265 tableau(cutRow, 0) = d; 266 tableau(cutRow, 1) = -mod(-tableau(row, 1), d); // -c%d. 267 tableau(cutRow, 2) = 0; 268 for (unsigned col = 3 + nSymbol, e = getNumColumns(); col < e; ++col) 269 tableau(cutRow, col) = mod(tableau(row, col), d); // b_i%d. 270 return moveRowUnknownToColumn(cutRow); 271 } 272 273 Optional<unsigned> LexSimplex::maybeGetNonIntegralVarRow() const { 274 for (const Unknown &u : var) { 275 if (u.orientation == Orientation::Column) 276 continue; 277 // If the sample value is of the form (a/d)M + b/d, we need b to be 278 // divisible by d. We assume M contains all possible 279 // factors and is divisible by everything. 280 unsigned row = u.pos; 281 if (tableau(row, 1) % tableau(row, 0) != 0) 282 return row; 283 } 284 return {}; 285 } 286 287 MaybeOptimum<SmallVector<int64_t, 8>> LexSimplex::findIntegerLexMin() { 288 // We first try to make the tableau consistent. 289 if (restoreRationalConsistency().failed()) 290 return OptimumKind::Empty; 291 292 // Then, if the sample value is integral, we are done. 293 while (Optional<unsigned> maybeRow = maybeGetNonIntegralVarRow()) { 294 // Otherwise, for the variable whose row has a non-integral sample value, 295 // we add a cut, a constraint that remove this rational point 296 // while preserving all integer points, thus keeping the lexmin the same. 297 // We then again try to make the tableau with the new constraint 298 // consistent. This continues until the tableau becomes empty, in which 299 // case there is no integer point, or until there are no variables with 300 // non-integral sample values. 301 // 302 // Failure indicates that the tableau became empty, which occurs when the 303 // polytope is integer empty. 304 if (addCut(*maybeRow).failed()) 305 return OptimumKind::Empty; 306 if (restoreRationalConsistency().failed()) 307 return OptimumKind::Empty; 308 } 309 310 MaybeOptimum<SmallVector<Fraction, 8>> sample = getRationalSample(); 311 assert(!sample.isEmpty() && "If we reached here the sample should exist!"); 312 if (sample.isUnbounded()) 313 return OptimumKind::Unbounded; 314 return llvm::to_vector<8>( 315 llvm::map_range(*sample, std::mem_fn(&Fraction::getAsInteger))); 316 } 317 318 bool LexSimplex::isSeparateInequality(ArrayRef<int64_t> coeffs) { 319 SimplexRollbackScopeExit scopeExit(*this); 320 addInequality(coeffs); 321 return findIntegerLexMin().isEmpty(); 322 } 323 324 bool LexSimplex::isRedundantInequality(ArrayRef<int64_t> coeffs) { 325 return isSeparateInequality(getComplementIneq(coeffs)); 326 } 327 328 SmallVector<int64_t, 8> 329 SymbolicLexSimplex::getSymbolicSampleNumerator(unsigned row) const { 330 SmallVector<int64_t, 8> sample; 331 sample.reserve(nSymbol + 1); 332 for (unsigned col = 3; col < 3 + nSymbol; ++col) 333 sample.push_back(tableau(row, col)); 334 sample.push_back(tableau(row, 1)); 335 return sample; 336 } 337 338 SmallVector<int64_t, 8> 339 SymbolicLexSimplex::getSymbolicSampleIneq(unsigned row) const { 340 SmallVector<int64_t, 8> sample = getSymbolicSampleNumerator(row); 341 // The inequality is equivalent to the GCD-normalized one. 342 normalizeRange(sample); 343 return sample; 344 } 345 346 void LexSimplexBase::appendSymbol() { 347 appendVariable(); 348 swapColumns(3 + nSymbol, getNumColumns() - 1); 349 var.back().isSymbol = true; 350 nSymbol++; 351 } 352 353 static bool isRangeDivisibleBy(ArrayRef<int64_t> range, int64_t divisor) { 354 assert(divisor > 0 && "divisor must be positive!"); 355 return llvm::all_of(range, [divisor](int64_t x) { return x % divisor == 0; }); 356 } 357 358 bool SymbolicLexSimplex::isSymbolicSampleIntegral(unsigned row) const { 359 int64_t denom = tableau(row, 0); 360 return tableau(row, 1) % denom == 0 && 361 isRangeDivisibleBy(tableau.getRow(row).slice(3, nSymbol), denom); 362 } 363 364 /// This proceeds similarly to LexSimplexBase::addCut(). We are given a row that 365 /// has a symbolic sample value with fractional coefficients. 366 /// 367 /// Let the row be 368 /// (c + coeffM*M + sum_i a_i*s_i + sum_j b_j*y_j)/d, 369 /// where s_1, ... s_m are the symbols and 370 /// y_1, ... y_n are the other basis unknowns. 371 /// 372 /// As in LexSimplex::addCut, for this to be an integer, we want 373 /// 374 /// coeffM*M + sum_j b_j*y_j = -c + sum_i (-a_i*s_i) (mod d) 375 /// 376 /// This time, a_1*s_1 + ... + a_m*s_m may not be an integer. We find that 377 /// 378 /// sum_i (b_i%d)y_i = ((-c%d) + sum_i (-a_i%d)s_i)%d + k*d for some integer k 379 /// 380 /// where we take a modulo of the whole symbolic expression on the right to 381 /// bring it into the range [0, d - 1]. Therefore, as in addCut(), 382 /// k is the quotient on dividing the LHS by d, and since LHS >= 0, we have 383 /// k >= 0 as well. If all the a_i are divisible by d, then we can add the 384 /// constraint directly. Otherwise, we realize the modulo of the symbolic 385 /// expression by adding a division variable 386 /// 387 /// q = ((-c%d) + sum_i (-a_i%d)s_i)/d 388 /// 389 /// to the symbol domain, so the equality becomes 390 /// 391 /// sum_i (b_i%d)y_i = (-c%d) + sum_i (-a_i%d)s_i - q*d + k*d for some integer k 392 /// 393 /// So the cut is 394 /// (sum_i (b_i%d)y_i - (-c%d) - sum_i (-a_i%d)s_i + q*d)/d >= 0 395 /// This constraint is violated when added so we immediately try to move it to a 396 /// column. 397 LogicalResult SymbolicLexSimplex::addSymbolicCut(unsigned row) { 398 int64_t d = tableau(row, 0); 399 if (isRangeDivisibleBy(tableau.getRow(row).slice(3, nSymbol), d)) { 400 // The coefficients of symbols in the symbol numerator are divisible 401 // by the denominator, so we can add the constraint directly, 402 // i.e., ignore the symbols and add a regular cut as in addCut(). 403 return addCut(row); 404 } 405 406 // Construct the division variable `q = ((-c%d) + sum_i (-a_i%d)s_i)/d`. 407 SmallVector<int64_t, 8> divCoeffs; 408 divCoeffs.reserve(nSymbol + 1); 409 int64_t divDenom = d; 410 for (unsigned col = 3; col < 3 + nSymbol; ++col) 411 divCoeffs.push_back(mod(-tableau(row, col), divDenom)); // (-a_i%d)s_i 412 divCoeffs.push_back(mod(-tableau(row, 1), divDenom)); // -c%d. 413 normalizeDiv(divCoeffs, divDenom); 414 415 domainSimplex.addDivisionVariable(divCoeffs, divDenom); 416 domainPoly.addLocalFloorDiv(divCoeffs, divDenom); 417 418 // Update `this` to account for the additional symbol we just added. 419 appendSymbol(); 420 421 // Add the cut (sum_i (b_i%d)y_i - (-c%d) + sum_i -(-a_i%d)s_i + q*d)/d >= 0. 422 unsigned cutRow = addZeroRow(/*makeRestricted=*/true); 423 tableau(cutRow, 0) = d; 424 tableau(cutRow, 2) = 0; 425 426 tableau(cutRow, 1) = -mod(-tableau(row, 1), d); // -(-c%d). 427 for (unsigned col = 3; col < 3 + nSymbol - 1; ++col) 428 tableau(cutRow, col) = -mod(-tableau(row, col), d); // -(-a_i%d)s_i. 429 tableau(cutRow, 3 + nSymbol - 1) = d; // q*d. 430 431 for (unsigned col = 3 + nSymbol, e = getNumColumns(); col < e; ++col) 432 tableau(cutRow, col) = mod(tableau(row, col), d); // (b_i%d)y_i. 433 return moveRowUnknownToColumn(cutRow); 434 } 435 436 void SymbolicLexSimplex::recordOutput(SymbolicLexMin &result) const { 437 Matrix output(0, domainPoly.getNumVars() + 1); 438 output.reserveRows(result.lexmin.getNumOutputs()); 439 for (const Unknown &u : var) { 440 if (u.isSymbol) 441 continue; 442 443 if (u.orientation == Orientation::Column) { 444 // M + u has a sample value of zero so u has a sample value of -M, i.e, 445 // unbounded. 446 result.unboundedDomain.unionInPlace(domainPoly); 447 return; 448 } 449 450 int64_t denom = tableau(u.pos, 0); 451 if (tableau(u.pos, 2) < denom) { 452 // M + u has a sample value of fM + something, where f < 1, so 453 // u = (f - 1)M + something, which has a negative coefficient for M, 454 // and so is unbounded. 455 result.unboundedDomain.unionInPlace(domainPoly); 456 return; 457 } 458 assert(tableau(u.pos, 2) == denom && 459 "Coefficient of M should not be greater than 1!"); 460 461 SmallVector<int64_t, 8> sample = getSymbolicSampleNumerator(u.pos); 462 for (int64_t &elem : sample) { 463 assert(elem % denom == 0 && "coefficients must be integral!"); 464 elem /= denom; 465 } 466 output.appendExtraRow(sample); 467 } 468 result.lexmin.addPiece(domainPoly, output); 469 } 470 471 Optional<unsigned> SymbolicLexSimplex::maybeGetAlwaysViolatedRow() { 472 // First look for rows that are clearly violated just from the big M 473 // coefficient, without needing to perform any simplex queries on the domain. 474 for (unsigned row = 0, e = getNumRows(); row < e; ++row) 475 if (tableau(row, 2) < 0) 476 return row; 477 478 for (unsigned row = 0, e = getNumRows(); row < e; ++row) { 479 if (tableau(row, 2) > 0) 480 continue; 481 if (domainSimplex.isSeparateInequality(getSymbolicSampleIneq(row))) { 482 // Sample numerator always takes negative values in the symbol domain. 483 return row; 484 } 485 } 486 return {}; 487 } 488 489 Optional<unsigned> SymbolicLexSimplex::maybeGetNonIntegralVarRow() { 490 for (const Unknown &u : var) { 491 if (u.orientation == Orientation::Column) 492 continue; 493 assert(!u.isSymbol && "Symbol should not be in row orientation!"); 494 if (!isSymbolicSampleIntegral(u.pos)) 495 return u.pos; 496 } 497 return {}; 498 } 499 500 /// The non-branching pivots are just the ones moving the rows 501 /// that are always violated in the symbol domain. 502 LogicalResult SymbolicLexSimplex::doNonBranchingPivots() { 503 while (Optional<unsigned> row = maybeGetAlwaysViolatedRow()) 504 if (moveRowUnknownToColumn(*row).failed()) 505 return failure(); 506 return success(); 507 } 508 509 SymbolicLexMin SymbolicLexSimplex::computeSymbolicIntegerLexMin() { 510 SymbolicLexMin result(domainPoly.getSpace(), var.size() - nSymbol); 511 512 /// The algorithm is more naturally expressed recursively, but we implement 513 /// it iteratively here to avoid potential issues with stack overflows in the 514 /// compiler. We explicitly maintain the stack frames in a vector. 515 /// 516 /// To "recurse", we store the current "stack frame", i.e., state variables 517 /// that we will need when we "return", into `stack`, increment `level`, and 518 /// `continue`. To "tail recurse", we just `continue`. 519 /// To "return", we decrement `level` and `continue`. 520 /// 521 /// When there is no stack frame for the current `level`, this indicates that 522 /// we have just "recursed" or "tail recursed". When there does exist one, 523 /// this indicates that we have just "returned" from recursing. There is only 524 /// one point at which non-tail calls occur so we always "return" there. 525 unsigned level = 1; 526 struct StackFrame { 527 int splitIndex; 528 unsigned snapshot; 529 unsigned domainSnapshot; 530 IntegerRelation::CountsSnapshot domainPolyCounts; 531 }; 532 SmallVector<StackFrame, 8> stack; 533 534 while (level > 0) { 535 assert(level >= stack.size()); 536 if (level > stack.size()) { 537 if (empty || domainSimplex.findIntegerLexMin().isEmpty()) { 538 // No integer points; return. 539 --level; 540 continue; 541 } 542 543 if (doNonBranchingPivots().failed()) { 544 // Could not find pivots for violated constraints; return. 545 --level; 546 continue; 547 } 548 549 SmallVector<int64_t, 8> symbolicSample; 550 unsigned splitRow = 0; 551 for (unsigned e = getNumRows(); splitRow < e; ++splitRow) { 552 if (tableau(splitRow, 2) > 0) 553 continue; 554 assert(tableau(splitRow, 2) == 0 && 555 "Non-branching pivots should have been handled already!"); 556 557 symbolicSample = getSymbolicSampleIneq(splitRow); 558 if (domainSimplex.isRedundantInequality(symbolicSample)) 559 continue; 560 561 // It's neither redundant nor separate, so it takes both positive and 562 // negative values, and hence constitutes a row for which we need to 563 // split the domain and separately run each case. 564 assert(!domainSimplex.isSeparateInequality(symbolicSample) && 565 "Non-branching pivots should have been handled already!"); 566 break; 567 } 568 569 if (splitRow < getNumRows()) { 570 unsigned domainSnapshot = domainSimplex.getSnapshot(); 571 IntegerRelation::CountsSnapshot domainPolyCounts = 572 domainPoly.getCounts(); 573 574 // First, we consider the part of the domain where the row is not 575 // violated. We don't have to do any pivots for the row in this case, 576 // but we record the additional constraint that defines this part of 577 // the domain. 578 domainSimplex.addInequality(symbolicSample); 579 domainPoly.addInequality(symbolicSample); 580 581 // Recurse. 582 // 583 // On return, the basis as a set is preserved but not the internal 584 // ordering within rows or columns. Thus, we take note of the index of 585 // the Unknown that caused the split, which may be in a different 586 // row when we come back from recursing. We will need this to recurse 587 // on the other part of the split domain, where the row is violated. 588 // 589 // Note that we have to capture the index above and not a reference to 590 // the Unknown itself, since the array it lives in might get 591 // reallocated. 592 int splitIndex = rowUnknown[splitRow]; 593 unsigned snapshot = getSnapshot(); 594 stack.push_back( 595 {splitIndex, snapshot, domainSnapshot, domainPolyCounts}); 596 ++level; 597 continue; 598 } 599 600 // The tableau is rationally consistent for the current domain. 601 // Now we look for non-integral sample values and add cuts for them. 602 if (Optional<unsigned> row = maybeGetNonIntegralVarRow()) { 603 if (addSymbolicCut(*row).failed()) { 604 // No integral points; return. 605 --level; 606 continue; 607 } 608 609 // Rerun this level with the added cut constraint (tail recurse). 610 continue; 611 } 612 613 // Record output and return. 614 recordOutput(result); 615 --level; 616 continue; 617 } 618 619 if (level == stack.size()) { 620 // We have "returned" from "recursing". 621 const StackFrame &frame = stack.back(); 622 domainPoly.truncate(frame.domainPolyCounts); 623 domainSimplex.rollback(frame.domainSnapshot); 624 rollback(frame.snapshot); 625 const Unknown &u = unknownFromIndex(frame.splitIndex); 626 627 // Drop the frame. We don't need it anymore. 628 stack.pop_back(); 629 630 // Now we consider the part of the domain where the unknown `splitIndex` 631 // was negative. 632 assert(u.orientation == Orientation::Row && 633 "The split row should have been returned to row orientation!"); 634 SmallVector<int64_t, 8> splitIneq = 635 getComplementIneq(getSymbolicSampleIneq(u.pos)); 636 normalizeRange(splitIneq); 637 if (moveRowUnknownToColumn(u.pos).failed()) { 638 // The unknown can't be made non-negative; return. 639 --level; 640 continue; 641 } 642 643 // The unknown can be made negative; recurse with the corresponding domain 644 // constraints. 645 domainSimplex.addInequality(splitIneq); 646 domainPoly.addInequality(splitIneq); 647 648 // We are now taking care of the second half of the domain and we don't 649 // need to do anything else here after returning, so it's a tail recurse. 650 continue; 651 } 652 } 653 654 return result; 655 } 656 657 bool LexSimplex::rowIsViolated(unsigned row) const { 658 if (tableau(row, 2) < 0) 659 return true; 660 if (tableau(row, 2) == 0 && tableau(row, 1) < 0) 661 return true; 662 return false; 663 } 664 665 Optional<unsigned> LexSimplex::maybeGetViolatedRow() const { 666 for (unsigned row = 0, e = getNumRows(); row < e; ++row) 667 if (rowIsViolated(row)) 668 return row; 669 return {}; 670 } 671 672 /// We simply look for violated rows and keep trying to move them to column 673 /// orientation, which always succeeds unless the constraints have no solution 674 /// in which case we just give up and return. 675 LogicalResult LexSimplex::restoreRationalConsistency() { 676 if (empty) 677 return failure(); 678 while (Optional<unsigned> maybeViolatedRow = maybeGetViolatedRow()) 679 if (moveRowUnknownToColumn(*maybeViolatedRow).failed()) 680 return failure(); 681 return success(); 682 } 683 684 // Move the row unknown to column orientation while preserving lexicopositivity 685 // of the basis transform. The sample value of the row must be non-positive. 686 // 687 // We only consider pivots where the pivot element is positive. Suppose no such 688 // pivot exists, i.e., some violated row has no positive coefficient for any 689 // basis unknown. The row can be represented as (s + c_1*u_1 + ... + c_n*u_n)/d, 690 // where d is the denominator, s is the sample value and the c_i are the basis 691 // coefficients. If s != 0, then since any feasible assignment of the basis 692 // satisfies u_i >= 0 for all i, and we have s < 0 as well as c_i < 0 for all i, 693 // any feasible assignment would violate this row and therefore the constraints 694 // have no solution. 695 // 696 // We can preserve lexicopositivity by picking the pivot column with positive 697 // pivot element that makes the lexicographically smallest change to the sample 698 // point. 699 // 700 // Proof. Let 701 // x = (x_1, ... x_n) be the variables, 702 // z = (z_1, ... z_m) be the constraints, 703 // y = (y_1, ... y_n) be the current basis, and 704 // define w = (x_1, ... x_n, z_1, ... z_m) = B*y + s. 705 // B is basically the simplex tableau of our implementation except that instead 706 // of only describing the transform to get back the non-basis unknowns, it 707 // defines the values of all the unknowns in terms of the basis unknowns. 708 // Similarly, s is the column for the sample value. 709 // 710 // Our goal is to show that each column in B, restricted to the first n 711 // rows, is lexicopositive after the pivot if it is so before. This is 712 // equivalent to saying the columns in the whole matrix are lexicopositive; 713 // there must be some non-zero element in every column in the first n rows since 714 // the n variables cannot be spanned without using all the n basis unknowns. 715 // 716 // Consider a pivot where z_i replaces y_j in the basis. Recall the pivot 717 // transform for the tableau derived for SimplexBase::pivot: 718 // 719 // pivot col other col pivot col other col 720 // pivot row a b -> pivot row 1/a -b/a 721 // other row c d other row c/a d - bc/a 722 // 723 // Similarly, a pivot results in B changing to B' and c to c'; the difference 724 // between the tableau and these matrices B and B' is that there is no special 725 // case for the pivot row, since it continues to represent the same unknown. The 726 // same formula applies for all rows: 727 // 728 // B'.col(j) = B.col(j) / B(i,j) 729 // B'.col(k) = B.col(k) - B(i,k) * B.col(j) / B(i,j) for k != j 730 // and similarly, s' = s - s_i * B.col(j) / B(i,j). 731 // 732 // If s_i == 0, then the sample value remains unchanged. Otherwise, if s_i < 0, 733 // the change in sample value when pivoting with column a is lexicographically 734 // smaller than that when pivoting with column b iff B.col(a) / B(i, a) is 735 // lexicographically smaller than B.col(b) / B(i, b). 736 // 737 // Since B(i, j) > 0, column j remains lexicopositive. 738 // 739 // For the other columns, suppose C.col(k) is not lexicopositive. 740 // This means that for some p, for all t < p, 741 // C(t,k) = 0 => B(t,k) = B(t,j) * B(i,k) / B(i,j) and 742 // C(t,k) < 0 => B(p,k) < B(t,j) * B(i,k) / B(i,j), 743 // which is in contradiction to the fact that B.col(j) / B(i,j) must be 744 // lexicographically smaller than B.col(k) / B(i,k), since it lexicographically 745 // minimizes the change in sample value. 746 LogicalResult LexSimplexBase::moveRowUnknownToColumn(unsigned row) { 747 Optional<unsigned> maybeColumn; 748 for (unsigned col = 3 + nSymbol, e = getNumColumns(); col < e; ++col) { 749 if (tableau(row, col) <= 0) 750 continue; 751 maybeColumn = 752 !maybeColumn ? col : getLexMinPivotColumn(row, *maybeColumn, col); 753 } 754 755 if (!maybeColumn) 756 return failure(); 757 758 pivot(row, *maybeColumn); 759 return success(); 760 } 761 762 unsigned LexSimplexBase::getLexMinPivotColumn(unsigned row, unsigned colA, 763 unsigned colB) const { 764 // First, let's consider the non-symbolic case. 765 // A pivot causes the following change. (in the diagram the matrix elements 766 // are shown as rationals and there is no common denominator used) 767 // 768 // pivot col big M col const col 769 // pivot row a p b 770 // other row c q d 771 // | 772 // v 773 // 774 // pivot col big M col const col 775 // pivot row 1/a -p/a -b/a 776 // other row c/a q - pc/a d - bc/a 777 // 778 // Let the sample value of the pivot row be s = pM + b before the pivot. Since 779 // the pivot row represents a violated constraint we know that s < 0. 780 // 781 // If the variable is a non-pivot column, its sample value is zero before and 782 // after the pivot. 783 // 784 // If the variable is the pivot column, then its sample value goes from 0 to 785 // (-p/a)M + (-b/a), i.e. 0 to -(pM + b)/a. Thus the change in the sample 786 // value is -s/a. 787 // 788 // If the variable is the pivot row, its sample value goes from s to 0, for a 789 // change of -s. 790 // 791 // If the variable is a non-pivot row, its sample value changes from 792 // qM + d to qM + d + (-pc/a)M + (-bc/a). Thus the change in sample value 793 // is -(pM + b)(c/a) = -sc/a. 794 // 795 // Thus the change in sample value is either 0, -s/a, -s, or -sc/a. Here -s is 796 // fixed for all calls to this function since the row and tableau are fixed. 797 // The callee just wants to compare the return values with the return value of 798 // other invocations of the same function. So the -s is common for all 799 // comparisons involved and can be ignored, since -s is strictly positive. 800 // 801 // Thus we take away this common factor and just return 0, 1/a, 1, or c/a as 802 // appropriate. This allows us to run the entire algorithm treating M 803 // symbolically, as the pivot to be performed does not depend on the value 804 // of M, so long as the sample value s is negative. Note that this is not 805 // because of any special feature of M; by the same argument, we ignore the 806 // symbols too. The caller ensure that the sample value s is negative for 807 // all possible values of the symbols. 808 auto getSampleChangeCoeffForVar = [this, row](unsigned col, 809 const Unknown &u) -> Fraction { 810 int64_t a = tableau(row, col); 811 if (u.orientation == Orientation::Column) { 812 // Pivot column case. 813 if (u.pos == col) 814 return {1, a}; 815 816 // Non-pivot column case. 817 return {0, 1}; 818 } 819 820 // Pivot row case. 821 if (u.pos == row) 822 return {1, 1}; 823 824 // Non-pivot row case. 825 int64_t c = tableau(u.pos, col); 826 return {c, a}; 827 }; 828 829 for (const Unknown &u : var) { 830 Fraction changeA = getSampleChangeCoeffForVar(colA, u); 831 Fraction changeB = getSampleChangeCoeffForVar(colB, u); 832 if (changeA < changeB) 833 return colA; 834 if (changeA > changeB) 835 return colB; 836 } 837 838 // If we reached here, both result in exactly the same changes, so it 839 // doesn't matter which we return. 840 return colA; 841 } 842 843 /// Find a pivot to change the sample value of the row in the specified 844 /// direction. The returned pivot row will involve `row` if and only if the 845 /// unknown is unbounded in the specified direction. 846 /// 847 /// To increase (resp. decrease) the value of a row, we need to find a live 848 /// column with a non-zero coefficient. If the coefficient is positive, we need 849 /// to increase (decrease) the value of the column, and if the coefficient is 850 /// negative, we need to decrease (increase) the value of the column. Also, 851 /// we cannot decrease the sample value of restricted columns. 852 /// 853 /// If multiple columns are valid, we break ties by considering a lexicographic 854 /// ordering where we prefer unknowns with lower index. 855 Optional<SimplexBase::Pivot> Simplex::findPivot(int row, 856 Direction direction) const { 857 Optional<unsigned> col; 858 for (unsigned j = 2, e = getNumColumns(); j < e; ++j) { 859 int64_t elem = tableau(row, j); 860 if (elem == 0) 861 continue; 862 863 if (unknownFromColumn(j).restricted && 864 !signMatchesDirection(elem, direction)) 865 continue; 866 if (!col || colUnknown[j] < colUnknown[*col]) 867 col = j; 868 } 869 870 if (!col) 871 return {}; 872 873 Direction newDirection = 874 tableau(row, *col) < 0 ? flippedDirection(direction) : direction; 875 Optional<unsigned> maybePivotRow = findPivotRow(row, newDirection, *col); 876 return Pivot{maybePivotRow.value_or(row), *col}; 877 } 878 879 /// Swap the associated unknowns for the row and the column. 880 /// 881 /// First we swap the index associated with the row and column. Then we update 882 /// the unknowns to reflect their new position and orientation. 883 void SimplexBase::swapRowWithCol(unsigned row, unsigned col) { 884 std::swap(rowUnknown[row], colUnknown[col]); 885 Unknown &uCol = unknownFromColumn(col); 886 Unknown &uRow = unknownFromRow(row); 887 uCol.orientation = Orientation::Column; 888 uRow.orientation = Orientation::Row; 889 uCol.pos = col; 890 uRow.pos = row; 891 } 892 893 void SimplexBase::pivot(Pivot pair) { pivot(pair.row, pair.column); } 894 895 /// Pivot pivotRow and pivotCol. 896 /// 897 /// Let R be the pivot row unknown and let C be the pivot col unknown. 898 /// Since initially R = a*C + sum b_i * X_i 899 /// (where the sum is over the other column's unknowns, x_i) 900 /// C = (R - (sum b_i * X_i))/a 901 /// 902 /// Let u be some other row unknown. 903 /// u = c*C + sum d_i * X_i 904 /// So u = c*(R - sum b_i * X_i)/a + sum d_i * X_i 905 /// 906 /// This results in the following transform: 907 /// pivot col other col pivot col other col 908 /// pivot row a b -> pivot row 1/a -b/a 909 /// other row c d other row c/a d - bc/a 910 /// 911 /// Taking into account the common denominators p and q: 912 /// 913 /// pivot col other col pivot col other col 914 /// pivot row a/p b/p -> pivot row p/a -b/a 915 /// other row c/q d/q other row cp/aq (da - bc)/aq 916 /// 917 /// The pivot row transform is accomplished be swapping a with the pivot row's 918 /// common denominator and negating the pivot row except for the pivot column 919 /// element. 920 void SimplexBase::pivot(unsigned pivotRow, unsigned pivotCol) { 921 assert(pivotCol >= getNumFixedCols() && "Refusing to pivot invalid column"); 922 assert(!unknownFromColumn(pivotCol).isSymbol); 923 924 swapRowWithCol(pivotRow, pivotCol); 925 std::swap(tableau(pivotRow, 0), tableau(pivotRow, pivotCol)); 926 // We need to negate the whole pivot row except for the pivot column. 927 if (tableau(pivotRow, 0) < 0) { 928 // If the denominator is negative, we negate the row by simply negating the 929 // denominator. 930 tableau(pivotRow, 0) = -tableau(pivotRow, 0); 931 tableau(pivotRow, pivotCol) = -tableau(pivotRow, pivotCol); 932 } else { 933 for (unsigned col = 1, e = getNumColumns(); col < e; ++col) { 934 if (col == pivotCol) 935 continue; 936 tableau(pivotRow, col) = -tableau(pivotRow, col); 937 } 938 } 939 tableau.normalizeRow(pivotRow); 940 941 for (unsigned row = 0, numRows = getNumRows(); row < numRows; ++row) { 942 if (row == pivotRow) 943 continue; 944 if (tableau(row, pivotCol) == 0) // Nothing to do. 945 continue; 946 tableau(row, 0) *= tableau(pivotRow, 0); 947 for (unsigned col = 1, numCols = getNumColumns(); col < numCols; ++col) { 948 if (col == pivotCol) 949 continue; 950 // Add rather than subtract because the pivot row has been negated. 951 tableau(row, col) = tableau(row, col) * tableau(pivotRow, 0) + 952 tableau(row, pivotCol) * tableau(pivotRow, col); 953 } 954 tableau(row, pivotCol) *= tableau(pivotRow, pivotCol); 955 tableau.normalizeRow(row); 956 } 957 } 958 959 /// Perform pivots until the unknown has a non-negative sample value or until 960 /// no more upward pivots can be performed. Return success if we were able to 961 /// bring the row to a non-negative sample value, and failure otherwise. 962 LogicalResult Simplex::restoreRow(Unknown &u) { 963 assert(u.orientation == Orientation::Row && 964 "unknown should be in row position"); 965 966 while (tableau(u.pos, 1) < 0) { 967 Optional<Pivot> maybePivot = findPivot(u.pos, Direction::Up); 968 if (!maybePivot) 969 break; 970 971 pivot(*maybePivot); 972 if (u.orientation == Orientation::Column) 973 return success(); // the unknown is unbounded above. 974 } 975 return success(tableau(u.pos, 1) >= 0); 976 } 977 978 /// Find a row that can be used to pivot the column in the specified direction. 979 /// This returns an empty optional if and only if the column is unbounded in the 980 /// specified direction (ignoring skipRow, if skipRow is set). 981 /// 982 /// If skipRow is set, this row is not considered, and (if it is restricted) its 983 /// restriction may be violated by the returned pivot. Usually, skipRow is set 984 /// because we don't want to move it to column position unless it is unbounded, 985 /// and we are either trying to increase the value of skipRow or explicitly 986 /// trying to make skipRow negative, so we are not concerned about this. 987 /// 988 /// If the direction is up (resp. down) and a restricted row has a negative 989 /// (positive) coefficient for the column, then this row imposes a bound on how 990 /// much the sample value of the column can change. Such a row with constant 991 /// term c and coefficient f for the column imposes a bound of c/|f| on the 992 /// change in sample value (in the specified direction). (note that c is 993 /// non-negative here since the row is restricted and the tableau is consistent) 994 /// 995 /// We iterate through the rows and pick the row which imposes the most 996 /// stringent bound, since pivoting with a row changes the row's sample value to 997 /// 0 and hence saturates the bound it imposes. We break ties between rows that 998 /// impose the same bound by considering a lexicographic ordering where we 999 /// prefer unknowns with lower index value. 1000 Optional<unsigned> Simplex::findPivotRow(Optional<unsigned> skipRow, 1001 Direction direction, 1002 unsigned col) const { 1003 Optional<unsigned> retRow; 1004 // Initialize these to zero in order to silence a warning about retElem and 1005 // retConst being used uninitialized in the initialization of `diff` below. In 1006 // reality, these are always initialized when that line is reached since these 1007 // are set whenever retRow is set. 1008 int64_t retElem = 0, retConst = 0; 1009 for (unsigned row = nRedundant, e = getNumRows(); row < e; ++row) { 1010 if (skipRow && row == *skipRow) 1011 continue; 1012 int64_t elem = tableau(row, col); 1013 if (elem == 0) 1014 continue; 1015 if (!unknownFromRow(row).restricted) 1016 continue; 1017 if (signMatchesDirection(elem, direction)) 1018 continue; 1019 int64_t constTerm = tableau(row, 1); 1020 1021 if (!retRow) { 1022 retRow = row; 1023 retElem = elem; 1024 retConst = constTerm; 1025 continue; 1026 } 1027 1028 int64_t diff = retConst * elem - constTerm * retElem; 1029 if ((diff == 0 && rowUnknown[row] < rowUnknown[*retRow]) || 1030 (diff != 0 && !signMatchesDirection(diff, direction))) { 1031 retRow = row; 1032 retElem = elem; 1033 retConst = constTerm; 1034 } 1035 } 1036 return retRow; 1037 } 1038 1039 bool SimplexBase::isEmpty() const { return empty; } 1040 1041 void SimplexBase::swapRows(unsigned i, unsigned j) { 1042 if (i == j) 1043 return; 1044 tableau.swapRows(i, j); 1045 std::swap(rowUnknown[i], rowUnknown[j]); 1046 unknownFromRow(i).pos = i; 1047 unknownFromRow(j).pos = j; 1048 } 1049 1050 void SimplexBase::swapColumns(unsigned i, unsigned j) { 1051 assert(i < getNumColumns() && j < getNumColumns() && 1052 "Invalid columns provided!"); 1053 if (i == j) 1054 return; 1055 tableau.swapColumns(i, j); 1056 std::swap(colUnknown[i], colUnknown[j]); 1057 unknownFromColumn(i).pos = i; 1058 unknownFromColumn(j).pos = j; 1059 } 1060 1061 /// Mark this tableau empty and push an entry to the undo stack. 1062 void SimplexBase::markEmpty() { 1063 // If the set is already empty, then we shouldn't add another UnmarkEmpty log 1064 // entry, since in that case the Simplex will be erroneously marked as 1065 // non-empty when rolling back past this point. 1066 if (empty) 1067 return; 1068 undoLog.push_back(UndoLogEntry::UnmarkEmpty); 1069 empty = true; 1070 } 1071 1072 /// Add an inequality to the tableau. If coeffs is c_0, c_1, ... c_n, where n 1073 /// is the current number of variables, then the corresponding inequality is 1074 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} >= 0. 1075 /// 1076 /// We add the inequality and mark it as restricted. We then try to make its 1077 /// sample value non-negative. If this is not possible, the tableau has become 1078 /// empty and we mark it as such. 1079 void Simplex::addInequality(ArrayRef<int64_t> coeffs) { 1080 unsigned conIndex = addRow(coeffs, /*makeRestricted=*/true); 1081 LogicalResult result = restoreRow(con[conIndex]); 1082 if (failed(result)) 1083 markEmpty(); 1084 } 1085 1086 /// Add an equality to the tableau. If coeffs is c_0, c_1, ... c_n, where n 1087 /// is the current number of variables, then the corresponding equality is 1088 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} == 0. 1089 /// 1090 /// We simply add two opposing inequalities, which force the expression to 1091 /// be zero. 1092 void SimplexBase::addEquality(ArrayRef<int64_t> coeffs) { 1093 addInequality(coeffs); 1094 SmallVector<int64_t, 8> negatedCoeffs; 1095 for (int64_t coeff : coeffs) 1096 negatedCoeffs.emplace_back(-coeff); 1097 addInequality(negatedCoeffs); 1098 } 1099 1100 unsigned SimplexBase::getNumVariables() const { return var.size(); } 1101 unsigned SimplexBase::getNumConstraints() const { return con.size(); } 1102 1103 /// Return a snapshot of the current state. This is just the current size of the 1104 /// undo log. 1105 unsigned SimplexBase::getSnapshot() const { return undoLog.size(); } 1106 1107 unsigned SimplexBase::getSnapshotBasis() { 1108 SmallVector<int, 8> basis; 1109 for (int index : colUnknown) { 1110 if (index != nullIndex) 1111 basis.push_back(index); 1112 } 1113 savedBases.push_back(std::move(basis)); 1114 1115 undoLog.emplace_back(UndoLogEntry::RestoreBasis); 1116 return undoLog.size() - 1; 1117 } 1118 1119 void SimplexBase::removeLastConstraintRowOrientation() { 1120 assert(con.back().orientation == Orientation::Row); 1121 1122 // Move this unknown to the last row and remove the last row from the 1123 // tableau. 1124 swapRows(con.back().pos, getNumRows() - 1); 1125 // It is not strictly necessary to shrink the tableau, but for now we 1126 // maintain the invariant that the tableau has exactly getNumRows() 1127 // rows. 1128 tableau.resizeVertically(getNumRows() - 1); 1129 rowUnknown.pop_back(); 1130 con.pop_back(); 1131 } 1132 1133 // This doesn't find a pivot row only if the column has zero 1134 // coefficients for every row. 1135 // 1136 // If the unknown is a constraint, this can't happen, since it was added 1137 // initially as a row. Such a row could never have been pivoted to a column. So 1138 // a pivot row will always be found if we have a constraint. 1139 // 1140 // If we have a variable, then the column has zero coefficients for every row 1141 // iff no constraints have been added with a non-zero coefficient for this row. 1142 Optional<unsigned> SimplexBase::findAnyPivotRow(unsigned col) { 1143 for (unsigned row = nRedundant, e = getNumRows(); row < e; ++row) 1144 if (tableau(row, col) != 0) 1145 return row; 1146 return {}; 1147 } 1148 1149 // It's not valid to remove the constraint by deleting the column since this 1150 // would result in an invalid basis. 1151 void Simplex::undoLastConstraint() { 1152 if (con.back().orientation == Orientation::Column) { 1153 // We try to find any pivot row for this column that preserves tableau 1154 // consistency (except possibly the column itself, which is going to be 1155 // deallocated anyway). 1156 // 1157 // If no pivot row is found in either direction, then the unknown is 1158 // unbounded in both directions and we are free to perform any pivot at 1159 // all. To do this, we just need to find any row with a non-zero 1160 // coefficient for the column. findAnyPivotRow will always be able to 1161 // find such a row for a constraint. 1162 unsigned column = con.back().pos; 1163 if (Optional<unsigned> maybeRow = findPivotRow({}, Direction::Up, column)) { 1164 pivot(*maybeRow, column); 1165 } else if (Optional<unsigned> maybeRow = 1166 findPivotRow({}, Direction::Down, column)) { 1167 pivot(*maybeRow, column); 1168 } else { 1169 Optional<unsigned> row = findAnyPivotRow(column); 1170 assert(row && "Pivot should always exist for a constraint!"); 1171 pivot(*row, column); 1172 } 1173 } 1174 removeLastConstraintRowOrientation(); 1175 } 1176 1177 // It's not valid to remove the constraint by deleting the column since this 1178 // would result in an invalid basis. 1179 void LexSimplexBase::undoLastConstraint() { 1180 if (con.back().orientation == Orientation::Column) { 1181 // When removing the last constraint during a rollback, we just need to find 1182 // any pivot at all, i.e., any row with non-zero coefficient for the 1183 // column, because when rolling back a lexicographic simplex, we always 1184 // end by restoring the exact basis that was present at the time of the 1185 // snapshot, so what pivots we perform while undoing doesn't matter as 1186 // long as we get the unknown to row orientation and remove it. 1187 unsigned column = con.back().pos; 1188 Optional<unsigned> row = findAnyPivotRow(column); 1189 assert(row && "Pivot should always exist for a constraint!"); 1190 pivot(*row, column); 1191 } 1192 removeLastConstraintRowOrientation(); 1193 } 1194 1195 void SimplexBase::undo(UndoLogEntry entry) { 1196 if (entry == UndoLogEntry::RemoveLastConstraint) { 1197 // Simplex and LexSimplex handle this differently, so we call out to a 1198 // virtual function to handle this. 1199 undoLastConstraint(); 1200 } else if (entry == UndoLogEntry::RemoveLastVariable) { 1201 // Whenever we are rolling back the addition of a variable, it is guaranteed 1202 // that the variable will be in column position. 1203 // 1204 // We can see this as follows: any constraint that depends on this variable 1205 // was added after this variable was added, so the addition of such 1206 // constraints should already have been rolled back by the time we get to 1207 // rolling back the addition of the variable. Therefore, no constraint 1208 // currently has a component along the variable, so the variable itself must 1209 // be part of the basis. 1210 assert(var.back().orientation == Orientation::Column && 1211 "Variable to be removed must be in column orientation!"); 1212 1213 if (var.back().isSymbol) 1214 nSymbol--; 1215 1216 // Move this variable to the last column and remove the column from the 1217 // tableau. 1218 swapColumns(var.back().pos, getNumColumns() - 1); 1219 tableau.resizeHorizontally(getNumColumns() - 1); 1220 var.pop_back(); 1221 colUnknown.pop_back(); 1222 } else if (entry == UndoLogEntry::UnmarkEmpty) { 1223 empty = false; 1224 } else if (entry == UndoLogEntry::UnmarkLastRedundant) { 1225 nRedundant--; 1226 } else if (entry == UndoLogEntry::RestoreBasis) { 1227 assert(!savedBases.empty() && "No bases saved!"); 1228 1229 SmallVector<int, 8> basis = std::move(savedBases.back()); 1230 savedBases.pop_back(); 1231 1232 for (int index : basis) { 1233 Unknown &u = unknownFromIndex(index); 1234 if (u.orientation == Orientation::Column) 1235 continue; 1236 for (unsigned col = getNumFixedCols(), e = getNumColumns(); col < e; 1237 col++) { 1238 assert(colUnknown[col] != nullIndex && 1239 "Column should not be a fixed column!"); 1240 if (std::find(basis.begin(), basis.end(), colUnknown[col]) != 1241 basis.end()) 1242 continue; 1243 if (tableau(u.pos, col) == 0) 1244 continue; 1245 pivot(u.pos, col); 1246 break; 1247 } 1248 1249 assert(u.orientation == Orientation::Column && "No pivot found!"); 1250 } 1251 } 1252 } 1253 1254 /// Rollback to the specified snapshot. 1255 /// 1256 /// We undo all the log entries until the log size when the snapshot was taken 1257 /// is reached. 1258 void SimplexBase::rollback(unsigned snapshot) { 1259 while (undoLog.size() > snapshot) { 1260 undo(undoLog.back()); 1261 undoLog.pop_back(); 1262 } 1263 } 1264 1265 /// We add the usual floor division constraints: 1266 /// `0 <= coeffs - denom*q <= denom - 1`, where `q` is the new division 1267 /// variable. 1268 /// 1269 /// This constrains the remainder `coeffs - denom*q` to be in the 1270 /// range `[0, denom - 1]`, which fixes the integer value of the quotient `q`. 1271 void SimplexBase::addDivisionVariable(ArrayRef<int64_t> coeffs, int64_t denom) { 1272 assert(denom != 0 && "Cannot divide by zero!\n"); 1273 appendVariable(); 1274 1275 SmallVector<int64_t, 8> ineq(coeffs.begin(), coeffs.end()); 1276 int64_t constTerm = ineq.back(); 1277 ineq.back() = -denom; 1278 ineq.push_back(constTerm); 1279 addInequality(ineq); 1280 1281 for (int64_t &coeff : ineq) 1282 coeff = -coeff; 1283 ineq.back() += denom - 1; 1284 addInequality(ineq); 1285 } 1286 1287 void SimplexBase::appendVariable(unsigned count) { 1288 if (count == 0) 1289 return; 1290 var.reserve(var.size() + count); 1291 colUnknown.reserve(colUnknown.size() + count); 1292 for (unsigned i = 0; i < count; ++i) { 1293 var.emplace_back(Orientation::Column, /*restricted=*/false, 1294 /*pos=*/getNumColumns() + i); 1295 colUnknown.push_back(var.size() - 1); 1296 } 1297 tableau.resizeHorizontally(getNumColumns() + count); 1298 undoLog.insert(undoLog.end(), count, UndoLogEntry::RemoveLastVariable); 1299 } 1300 1301 /// Add all the constraints from the given IntegerRelation. 1302 void SimplexBase::intersectIntegerRelation(const IntegerRelation &rel) { 1303 assert(rel.getNumVars() == getNumVariables() && 1304 "IntegerRelation must have same dimensionality as simplex"); 1305 for (unsigned i = 0, e = rel.getNumInequalities(); i < e; ++i) 1306 addInequality(rel.getInequality(i)); 1307 for (unsigned i = 0, e = rel.getNumEqualities(); i < e; ++i) 1308 addEquality(rel.getEquality(i)); 1309 } 1310 1311 MaybeOptimum<Fraction> Simplex::computeRowOptimum(Direction direction, 1312 unsigned row) { 1313 // Keep trying to find a pivot for the row in the specified direction. 1314 while (Optional<Pivot> maybePivot = findPivot(row, direction)) { 1315 // If findPivot returns a pivot involving the row itself, then the optimum 1316 // is unbounded, so we return None. 1317 if (maybePivot->row == row) 1318 return OptimumKind::Unbounded; 1319 pivot(*maybePivot); 1320 } 1321 1322 // The row has reached its optimal sample value, which we return. 1323 // The sample value is the entry in the constant column divided by the common 1324 // denominator for this row. 1325 return Fraction(tableau(row, 1), tableau(row, 0)); 1326 } 1327 1328 /// Compute the optimum of the specified expression in the specified direction, 1329 /// or None if it is unbounded. 1330 MaybeOptimum<Fraction> Simplex::computeOptimum(Direction direction, 1331 ArrayRef<int64_t> coeffs) { 1332 if (empty) 1333 return OptimumKind::Empty; 1334 1335 SimplexRollbackScopeExit scopeExit(*this); 1336 unsigned conIndex = addRow(coeffs); 1337 unsigned row = con[conIndex].pos; 1338 return computeRowOptimum(direction, row); 1339 } 1340 1341 MaybeOptimum<Fraction> Simplex::computeOptimum(Direction direction, 1342 Unknown &u) { 1343 if (empty) 1344 return OptimumKind::Empty; 1345 if (u.orientation == Orientation::Column) { 1346 unsigned column = u.pos; 1347 Optional<unsigned> pivotRow = findPivotRow({}, direction, column); 1348 // If no pivot is returned, the constraint is unbounded in the specified 1349 // direction. 1350 if (!pivotRow) 1351 return OptimumKind::Unbounded; 1352 pivot(*pivotRow, column); 1353 } 1354 1355 unsigned row = u.pos; 1356 MaybeOptimum<Fraction> optimum = computeRowOptimum(direction, row); 1357 if (u.restricted && direction == Direction::Down && 1358 (optimum.isUnbounded() || *optimum < Fraction(0, 1))) { 1359 if (failed(restoreRow(u))) 1360 llvm_unreachable("Could not restore row!"); 1361 } 1362 return optimum; 1363 } 1364 1365 bool Simplex::isBoundedAlongConstraint(unsigned constraintIndex) { 1366 assert(!empty && "It is not meaningful to ask whether a direction is bounded " 1367 "in an empty set."); 1368 // The constraint's perpendicular is already bounded below, since it is a 1369 // constraint. If it is also bounded above, we can return true. 1370 return computeOptimum(Direction::Up, con[constraintIndex]).isBounded(); 1371 } 1372 1373 /// Redundant constraints are those that are in row orientation and lie in 1374 /// rows 0 to nRedundant - 1. 1375 bool Simplex::isMarkedRedundant(unsigned constraintIndex) const { 1376 const Unknown &u = con[constraintIndex]; 1377 return u.orientation == Orientation::Row && u.pos < nRedundant; 1378 } 1379 1380 /// Mark the specified row redundant. 1381 /// 1382 /// This is done by moving the unknown to the end of the block of redundant 1383 /// rows (namely, to row nRedundant) and incrementing nRedundant to 1384 /// accomodate the new redundant row. 1385 void Simplex::markRowRedundant(Unknown &u) { 1386 assert(u.orientation == Orientation::Row && 1387 "Unknown should be in row position!"); 1388 assert(u.pos >= nRedundant && "Unknown is already marked redundant!"); 1389 swapRows(u.pos, nRedundant); 1390 ++nRedundant; 1391 undoLog.emplace_back(UndoLogEntry::UnmarkLastRedundant); 1392 } 1393 1394 /// Find a subset of constraints that is redundant and mark them redundant. 1395 void Simplex::detectRedundant(unsigned offset, unsigned count) { 1396 assert(offset + count <= con.size() && "invalid range!"); 1397 // It is not meaningful to talk about redundancy for empty sets. 1398 if (empty) 1399 return; 1400 1401 // Iterate through the constraints and check for each one if it can attain 1402 // negative sample values. If it can, it's not redundant. Otherwise, it is. 1403 // We mark redundant constraints redundant. 1404 // 1405 // Constraints that get marked redundant in one iteration are not respected 1406 // when checking constraints in later iterations. This prevents, for example, 1407 // two identical constraints both being marked redundant since each is 1408 // redundant given the other one. In this example, only the first of the 1409 // constraints that is processed will get marked redundant, as it should be. 1410 for (unsigned i = 0; i < count; ++i) { 1411 Unknown &u = con[offset + i]; 1412 if (u.orientation == Orientation::Column) { 1413 unsigned column = u.pos; 1414 Optional<unsigned> pivotRow = findPivotRow({}, Direction::Down, column); 1415 // If no downward pivot is returned, the constraint is unbounded below 1416 // and hence not redundant. 1417 if (!pivotRow) 1418 continue; 1419 pivot(*pivotRow, column); 1420 } 1421 1422 unsigned row = u.pos; 1423 MaybeOptimum<Fraction> minimum = computeRowOptimum(Direction::Down, row); 1424 if (minimum.isUnbounded() || *minimum < Fraction(0, 1)) { 1425 // Constraint is unbounded below or can attain negative sample values and 1426 // hence is not redundant. 1427 if (failed(restoreRow(u))) 1428 llvm_unreachable("Could not restore non-redundant row!"); 1429 continue; 1430 } 1431 1432 markRowRedundant(u); 1433 } 1434 } 1435 1436 bool Simplex::isUnbounded() { 1437 if (empty) 1438 return false; 1439 1440 SmallVector<int64_t, 8> dir(var.size() + 1); 1441 for (unsigned i = 0; i < var.size(); ++i) { 1442 dir[i] = 1; 1443 1444 if (computeOptimum(Direction::Up, dir).isUnbounded()) 1445 return true; 1446 1447 if (computeOptimum(Direction::Down, dir).isUnbounded()) 1448 return true; 1449 1450 dir[i] = 0; 1451 } 1452 return false; 1453 } 1454 1455 /// Make a tableau to represent a pair of points in the original tableau. 1456 /// 1457 /// The product constraints and variables are stored as: first A's, then B's. 1458 /// 1459 /// The product tableau has row layout: 1460 /// A's redundant rows, B's redundant rows, A's other rows, B's other rows. 1461 /// 1462 /// It has column layout: 1463 /// denominator, constant, A's columns, B's columns. 1464 Simplex Simplex::makeProduct(const Simplex &a, const Simplex &b) { 1465 unsigned numVar = a.getNumVariables() + b.getNumVariables(); 1466 unsigned numCon = a.getNumConstraints() + b.getNumConstraints(); 1467 Simplex result(numVar); 1468 1469 result.tableau.reserveRows(numCon); 1470 result.empty = a.empty || b.empty; 1471 1472 auto concat = [](ArrayRef<Unknown> v, ArrayRef<Unknown> w) { 1473 SmallVector<Unknown, 8> result; 1474 result.reserve(v.size() + w.size()); 1475 result.insert(result.end(), v.begin(), v.end()); 1476 result.insert(result.end(), w.begin(), w.end()); 1477 return result; 1478 }; 1479 result.con = concat(a.con, b.con); 1480 result.var = concat(a.var, b.var); 1481 1482 auto indexFromBIndex = [&](int index) { 1483 return index >= 0 ? a.getNumVariables() + index 1484 : ~(a.getNumConstraints() + ~index); 1485 }; 1486 1487 result.colUnknown.assign(2, nullIndex); 1488 for (unsigned i = 2, e = a.getNumColumns(); i < e; ++i) { 1489 result.colUnknown.push_back(a.colUnknown[i]); 1490 result.unknownFromIndex(result.colUnknown.back()).pos = 1491 result.colUnknown.size() - 1; 1492 } 1493 for (unsigned i = 2, e = b.getNumColumns(); i < e; ++i) { 1494 result.colUnknown.push_back(indexFromBIndex(b.colUnknown[i])); 1495 result.unknownFromIndex(result.colUnknown.back()).pos = 1496 result.colUnknown.size() - 1; 1497 } 1498 1499 auto appendRowFromA = [&](unsigned row) { 1500 unsigned resultRow = result.tableau.appendExtraRow(); 1501 for (unsigned col = 0, e = a.getNumColumns(); col < e; ++col) 1502 result.tableau(resultRow, col) = a.tableau(row, col); 1503 result.rowUnknown.push_back(a.rowUnknown[row]); 1504 result.unknownFromIndex(result.rowUnknown.back()).pos = 1505 result.rowUnknown.size() - 1; 1506 }; 1507 1508 // Also fixes the corresponding entry in rowUnknown and var/con (as the case 1509 // may be). 1510 auto appendRowFromB = [&](unsigned row) { 1511 unsigned resultRow = result.tableau.appendExtraRow(); 1512 result.tableau(resultRow, 0) = b.tableau(row, 0); 1513 result.tableau(resultRow, 1) = b.tableau(row, 1); 1514 1515 unsigned offset = a.getNumColumns() - 2; 1516 for (unsigned col = 2, e = b.getNumColumns(); col < e; ++col) 1517 result.tableau(resultRow, offset + col) = b.tableau(row, col); 1518 result.rowUnknown.push_back(indexFromBIndex(b.rowUnknown[row])); 1519 result.unknownFromIndex(result.rowUnknown.back()).pos = 1520 result.rowUnknown.size() - 1; 1521 }; 1522 1523 result.nRedundant = a.nRedundant + b.nRedundant; 1524 for (unsigned row = 0; row < a.nRedundant; ++row) 1525 appendRowFromA(row); 1526 for (unsigned row = 0; row < b.nRedundant; ++row) 1527 appendRowFromB(row); 1528 for (unsigned row = a.nRedundant, e = a.getNumRows(); row < e; ++row) 1529 appendRowFromA(row); 1530 for (unsigned row = b.nRedundant, e = b.getNumRows(); row < e; ++row) 1531 appendRowFromB(row); 1532 1533 return result; 1534 } 1535 1536 Optional<SmallVector<Fraction, 8>> Simplex::getRationalSample() const { 1537 if (empty) 1538 return {}; 1539 1540 SmallVector<Fraction, 8> sample; 1541 sample.reserve(var.size()); 1542 // Push the sample value for each variable into the vector. 1543 for (const Unknown &u : var) { 1544 if (u.orientation == Orientation::Column) { 1545 // If the variable is in column position, its sample value is zero. 1546 sample.emplace_back(0, 1); 1547 } else { 1548 // If the variable is in row position, its sample value is the 1549 // entry in the constant column divided by the denominator. 1550 int64_t denom = tableau(u.pos, 0); 1551 sample.emplace_back(tableau(u.pos, 1), denom); 1552 } 1553 } 1554 return sample; 1555 } 1556 1557 void LexSimplexBase::addInequality(ArrayRef<int64_t> coeffs) { 1558 addRow(coeffs, /*makeRestricted=*/true); 1559 } 1560 1561 MaybeOptimum<SmallVector<Fraction, 8>> LexSimplex::getRationalSample() const { 1562 if (empty) 1563 return OptimumKind::Empty; 1564 1565 SmallVector<Fraction, 8> sample; 1566 sample.reserve(var.size()); 1567 // Push the sample value for each variable into the vector. 1568 for (const Unknown &u : var) { 1569 // When the big M parameter is being used, each variable x is represented 1570 // as M + x, so its sample value is finite if and only if it is of the 1571 // form 1*M + c. If the coefficient of M is not one then the sample value 1572 // is infinite, and we return an empty optional. 1573 1574 if (u.orientation == Orientation::Column) { 1575 // If the variable is in column position, the sample value of M + x is 1576 // zero, so x = -M which is unbounded. 1577 return OptimumKind::Unbounded; 1578 } 1579 1580 // If the variable is in row position, its sample value is the 1581 // entry in the constant column divided by the denominator. 1582 int64_t denom = tableau(u.pos, 0); 1583 if (usingBigM) 1584 if (tableau(u.pos, 2) != denom) 1585 return OptimumKind::Unbounded; 1586 sample.emplace_back(tableau(u.pos, 1), denom); 1587 } 1588 return sample; 1589 } 1590 1591 Optional<SmallVector<int64_t, 8>> Simplex::getSamplePointIfIntegral() const { 1592 // If the tableau is empty, no sample point exists. 1593 if (empty) 1594 return {}; 1595 1596 // The value will always exist since the Simplex is non-empty. 1597 SmallVector<Fraction, 8> rationalSample = *getRationalSample(); 1598 SmallVector<int64_t, 8> integerSample; 1599 integerSample.reserve(var.size()); 1600 for (const Fraction &coord : rationalSample) { 1601 // If the sample is non-integral, return None. 1602 if (coord.num % coord.den != 0) 1603 return {}; 1604 integerSample.push_back(coord.num / coord.den); 1605 } 1606 return integerSample; 1607 } 1608 1609 /// Given a simplex for a polytope, construct a new simplex whose variables are 1610 /// identified with a pair of points (x, y) in the original polytope. Supports 1611 /// some operations needed for generalized basis reduction. In what follows, 1612 /// dotProduct(x, y) = x_1 * y_1 + x_2 * y_2 + ... x_n * y_n where n is the 1613 /// dimension of the original polytope. 1614 /// 1615 /// This supports adding equality constraints dotProduct(dir, x - y) == 0. It 1616 /// also supports rolling back this addition, by maintaining a snapshot stack 1617 /// that contains a snapshot of the Simplex's state for each equality, just 1618 /// before that equality was added. 1619 class presburger::GBRSimplex { 1620 using Orientation = Simplex::Orientation; 1621 1622 public: 1623 GBRSimplex(const Simplex &originalSimplex) 1624 : simplex(Simplex::makeProduct(originalSimplex, originalSimplex)), 1625 simplexConstraintOffset(simplex.getNumConstraints()) {} 1626 1627 /// Add an equality dotProduct(dir, x - y) == 0. 1628 /// First pushes a snapshot for the current simplex state to the stack so 1629 /// that this can be rolled back later. 1630 void addEqualityForDirection(ArrayRef<int64_t> dir) { 1631 assert(llvm::any_of(dir, [](int64_t x) { return x != 0; }) && 1632 "Direction passed is the zero vector!"); 1633 snapshotStack.push_back(simplex.getSnapshot()); 1634 simplex.addEquality(getCoeffsForDirection(dir)); 1635 } 1636 /// Compute max(dotProduct(dir, x - y)). 1637 Fraction computeWidth(ArrayRef<int64_t> dir) { 1638 MaybeOptimum<Fraction> maybeWidth = 1639 simplex.computeOptimum(Direction::Up, getCoeffsForDirection(dir)); 1640 assert(maybeWidth.isBounded() && "Width should be bounded!"); 1641 return *maybeWidth; 1642 } 1643 1644 /// Compute max(dotProduct(dir, x - y)) and save the dual variables for only 1645 /// the direction equalities to `dual`. 1646 Fraction computeWidthAndDuals(ArrayRef<int64_t> dir, 1647 SmallVectorImpl<int64_t> &dual, 1648 int64_t &dualDenom) { 1649 // We can't just call into computeWidth or computeOptimum since we need to 1650 // access the state of the tableau after computing the optimum, and these 1651 // functions rollback the insertion of the objective function into the 1652 // tableau before returning. We instead add a row for the objective function 1653 // ourselves, call into computeOptimum, compute the duals from the tableau 1654 // state, and finally rollback the addition of the row before returning. 1655 SimplexRollbackScopeExit scopeExit(simplex); 1656 unsigned conIndex = simplex.addRow(getCoeffsForDirection(dir)); 1657 unsigned row = simplex.con[conIndex].pos; 1658 MaybeOptimum<Fraction> maybeWidth = 1659 simplex.computeRowOptimum(Simplex::Direction::Up, row); 1660 assert(maybeWidth.isBounded() && "Width should be bounded!"); 1661 dualDenom = simplex.tableau(row, 0); 1662 dual.clear(); 1663 1664 // The increment is i += 2 because equalities are added as two inequalities, 1665 // one positive and one negative. Each iteration processes one equality. 1666 for (unsigned i = simplexConstraintOffset; i < conIndex; i += 2) { 1667 // The dual variable for an inequality in column orientation is the 1668 // negative of its coefficient at the objective row. If the inequality is 1669 // in row orientation, the corresponding dual variable is zero. 1670 // 1671 // We want the dual for the original equality, which corresponds to two 1672 // inequalities: a positive inequality, which has the same coefficients as 1673 // the equality, and a negative equality, which has negated coefficients. 1674 // 1675 // Note that at most one of these inequalities can be in column 1676 // orientation because the column unknowns should form a basis and hence 1677 // must be linearly independent. If the positive inequality is in column 1678 // position, its dual is the dual corresponding to the equality. If the 1679 // negative inequality is in column position, the negation of its dual is 1680 // the dual corresponding to the equality. If neither is in column 1681 // position, then that means that this equality is redundant, and its dual 1682 // is zero. 1683 // 1684 // Note that it is NOT valid to perform pivots during the computation of 1685 // the duals. This entire dual computation must be performed on the same 1686 // tableau configuration. 1687 assert(!(simplex.con[i].orientation == Orientation::Column && 1688 simplex.con[i + 1].orientation == Orientation::Column) && 1689 "Both inequalities for the equality cannot be in column " 1690 "orientation!"); 1691 if (simplex.con[i].orientation == Orientation::Column) 1692 dual.push_back(-simplex.tableau(row, simplex.con[i].pos)); 1693 else if (simplex.con[i + 1].orientation == Orientation::Column) 1694 dual.push_back(simplex.tableau(row, simplex.con[i + 1].pos)); 1695 else 1696 dual.emplace_back(0); 1697 } 1698 return *maybeWidth; 1699 } 1700 1701 /// Remove the last equality that was added through addEqualityForDirection. 1702 /// 1703 /// We do this by rolling back to the snapshot at the top of the stack, which 1704 /// should be a snapshot taken just before the last equality was added. 1705 void removeLastEquality() { 1706 assert(!snapshotStack.empty() && "Snapshot stack is empty!"); 1707 simplex.rollback(snapshotStack.back()); 1708 snapshotStack.pop_back(); 1709 } 1710 1711 private: 1712 /// Returns coefficients of the expression 'dot_product(dir, x - y)', 1713 /// i.e., dir_1 * x_1 + dir_2 * x_2 + ... + dir_n * x_n 1714 /// - dir_1 * y_1 - dir_2 * y_2 - ... - dir_n * y_n, 1715 /// where n is the dimension of the original polytope. 1716 SmallVector<int64_t, 8> getCoeffsForDirection(ArrayRef<int64_t> dir) { 1717 assert(2 * dir.size() == simplex.getNumVariables() && 1718 "Direction vector has wrong dimensionality"); 1719 SmallVector<int64_t, 8> coeffs(dir.begin(), dir.end()); 1720 coeffs.reserve(2 * dir.size()); 1721 for (int64_t coeff : dir) 1722 coeffs.push_back(-coeff); 1723 coeffs.emplace_back(0); // constant term 1724 return coeffs; 1725 } 1726 1727 Simplex simplex; 1728 /// The first index of the equality constraints, the index immediately after 1729 /// the last constraint in the initial product simplex. 1730 unsigned simplexConstraintOffset; 1731 /// A stack of snapshots, used for rolling back. 1732 SmallVector<unsigned, 8> snapshotStack; 1733 }; 1734 1735 /// Reduce the basis to try and find a direction in which the polytope is 1736 /// "thin". This only works for bounded polytopes. 1737 /// 1738 /// This is an implementation of the algorithm described in the paper 1739 /// "An Implementation of Generalized Basis Reduction for Integer Programming" 1740 /// by W. Cook, T. Rutherford, H. E. Scarf, D. Shallcross. 1741 /// 1742 /// Let b_{level}, b_{level + 1}, ... b_n be the current basis. 1743 /// Let width_i(v) = max <v, x - y> where x and y are points in the original 1744 /// polytope such that <b_j, x - y> = 0 is satisfied for all level <= j < i. 1745 /// 1746 /// In every iteration, we first replace b_{i+1} with b_{i+1} + u*b_i, where u 1747 /// is the integer such that width_i(b_{i+1} + u*b_i) is minimized. Let dual_i 1748 /// be the dual variable associated with the constraint <b_i, x - y> = 0 when 1749 /// computing width_{i+1}(b_{i+1}). It can be shown that dual_i is the 1750 /// minimizing value of u, if it were allowed to be fractional. Due to 1751 /// convexity, the minimizing integer value is either floor(dual_i) or 1752 /// ceil(dual_i), so we just need to check which of these gives a lower 1753 /// width_{i+1} value. If dual_i turned out to be an integer, then u = dual_i. 1754 /// 1755 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and (the new) 1756 /// b_{i + 1} and decrement i (unless i = level, in which case we stay at the 1757 /// same i). Otherwise, we increment i. 1758 /// 1759 /// We keep f values and duals cached and invalidate them when necessary. 1760 /// Whenever possible, we use them instead of recomputing them. We implement the 1761 /// algorithm as follows. 1762 /// 1763 /// In an iteration at i we need to compute: 1764 /// a) width_i(b_{i + 1}) 1765 /// b) width_i(b_i) 1766 /// c) the integer u that minimizes width_i(b_{i + 1} + u*b_i) 1767 /// 1768 /// If width_i(b_i) is not already cached, we compute it. 1769 /// 1770 /// If the duals are not already cached, we compute width_{i+1}(b_{i+1}) and 1771 /// store the duals from this computation. 1772 /// 1773 /// We call updateBasisWithUAndGetFCandidate, which finds the minimizing value 1774 /// of u as explained before, caches the duals from this computation, sets 1775 /// b_{i+1} to b_{i+1} + u*b_i, and returns the new value of width_i(b_{i+1}). 1776 /// 1777 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and b_{i+1} and 1778 /// decrement i, resulting in the basis 1779 /// ... b_{i - 1}, b_{i + 1} + u*b_i, b_i, b_{i+2}, ... 1780 /// with corresponding f values 1781 /// ... width_{i-1}(b_{i-1}), width_i(b_{i+1} + u*b_i), width_{i+1}(b_i), ... 1782 /// The values up to i - 1 remain unchanged. We have just gotten the middle 1783 /// value from updateBasisWithUAndGetFCandidate, so we can update that in the 1784 /// cache. The value at width_{i+1}(b_i) is unknown, so we evict this value from 1785 /// the cache. The iteration after decrementing needs exactly the duals from the 1786 /// computation of width_i(b_{i + 1} + u*b_i), so we keep these in the cache. 1787 /// 1788 /// When incrementing i, no cached f values get invalidated. However, the cached 1789 /// duals do get invalidated as the duals for the higher levels are different. 1790 void Simplex::reduceBasis(Matrix &basis, unsigned level) { 1791 const Fraction epsilon(3, 4); 1792 1793 if (level == basis.getNumRows() - 1) 1794 return; 1795 1796 GBRSimplex gbrSimplex(*this); 1797 SmallVector<Fraction, 8> width; 1798 SmallVector<int64_t, 8> dual; 1799 int64_t dualDenom; 1800 1801 // Finds the value of u that minimizes width_i(b_{i+1} + u*b_i), caches the 1802 // duals from this computation, sets b_{i+1} to b_{i+1} + u*b_i, and returns 1803 // the new value of width_i(b_{i+1}). 1804 // 1805 // If dual_i is not an integer, the minimizing value must be either 1806 // floor(dual_i) or ceil(dual_i). We compute the expression for both and 1807 // choose the minimizing value. 1808 // 1809 // If dual_i is an integer, we don't need to perform these computations. We 1810 // know that in this case, 1811 // a) u = dual_i. 1812 // b) one can show that dual_j for j < i are the same duals we would have 1813 // gotten from computing width_i(b_{i + 1} + u*b_i), so the correct duals 1814 // are the ones already in the cache. 1815 // c) width_i(b_{i+1} + u*b_i) = min_{alpha} width_i(b_{i+1} + alpha * b_i), 1816 // which 1817 // one can show is equal to width_{i+1}(b_{i+1}). The latter value must 1818 // be in the cache, so we get it from there and return it. 1819 auto updateBasisWithUAndGetFCandidate = [&](unsigned i) -> Fraction { 1820 assert(i < level + dual.size() && "dual_i is not known!"); 1821 1822 int64_t u = floorDiv(dual[i - level], dualDenom); 1823 basis.addToRow(i, i + 1, u); 1824 if (dual[i - level] % dualDenom != 0) { 1825 SmallVector<int64_t, 8> candidateDual[2]; 1826 int64_t candidateDualDenom[2]; 1827 Fraction widthI[2]; 1828 1829 // Initially u is floor(dual) and basis reflects this. 1830 widthI[0] = gbrSimplex.computeWidthAndDuals( 1831 basis.getRow(i + 1), candidateDual[0], candidateDualDenom[0]); 1832 1833 // Now try ceil(dual), i.e. floor(dual) + 1. 1834 ++u; 1835 basis.addToRow(i, i + 1, 1); 1836 widthI[1] = gbrSimplex.computeWidthAndDuals( 1837 basis.getRow(i + 1), candidateDual[1], candidateDualDenom[1]); 1838 1839 unsigned j = widthI[0] < widthI[1] ? 0 : 1; 1840 if (j == 0) 1841 // Subtract 1 to go from u = ceil(dual) back to floor(dual). 1842 basis.addToRow(i, i + 1, -1); 1843 1844 // width_i(b{i+1} + u*b_i) should be minimized at our value of u. 1845 // We assert that this holds by checking that the values of width_i at 1846 // u - 1 and u + 1 are greater than or equal to the value at u. If the 1847 // width is lesser at either of the adjacent values, then our computed 1848 // value of u is clearly not the minimizer. Otherwise by convexity the 1849 // computed value of u is really the minimizer. 1850 1851 // Check the value at u - 1. 1852 assert(gbrSimplex.computeWidth(scaleAndAddForAssert( 1853 basis.getRow(i + 1), -1, basis.getRow(i))) >= widthI[j] && 1854 "Computed u value does not minimize the width!"); 1855 // Check the value at u + 1. 1856 assert(gbrSimplex.computeWidth(scaleAndAddForAssert( 1857 basis.getRow(i + 1), +1, basis.getRow(i))) >= widthI[j] && 1858 "Computed u value does not minimize the width!"); 1859 1860 dual = std::move(candidateDual[j]); 1861 dualDenom = candidateDualDenom[j]; 1862 return widthI[j]; 1863 } 1864 1865 assert(i + 1 - level < width.size() && "width_{i+1} wasn't saved"); 1866 // f_i(b_{i+1} + dual*b_i) == width_{i+1}(b_{i+1}) when `dual` minimizes the 1867 // LHS. (note: the basis has already been updated, so b_{i+1} + dual*b_i in 1868 // the above expression is equal to basis.getRow(i+1) below.) 1869 assert(gbrSimplex.computeWidth(basis.getRow(i + 1)) == 1870 width[i + 1 - level]); 1871 return width[i + 1 - level]; 1872 }; 1873 1874 // In the ith iteration of the loop, gbrSimplex has constraints for directions 1875 // from `level` to i - 1. 1876 unsigned i = level; 1877 while (i < basis.getNumRows() - 1) { 1878 if (i >= level + width.size()) { 1879 // We don't even know the value of f_i(b_i), so let's find that first. 1880 // We have to do this first since later we assume that width already 1881 // contains values up to and including i. 1882 1883 assert((i == 0 || i - 1 < level + width.size()) && 1884 "We are at level i but we don't know the value of width_{i-1}"); 1885 1886 // We don't actually use these duals at all, but it doesn't matter 1887 // because this case should only occur when i is level, and there are no 1888 // duals in that case anyway. 1889 assert(i == level && "This case should only occur when i == level"); 1890 width.push_back( 1891 gbrSimplex.computeWidthAndDuals(basis.getRow(i), dual, dualDenom)); 1892 } 1893 1894 if (i >= level + dual.size()) { 1895 assert(i + 1 >= level + width.size() && 1896 "We don't know dual_i but we know width_{i+1}"); 1897 // We don't know dual for our level, so let's find it. 1898 gbrSimplex.addEqualityForDirection(basis.getRow(i)); 1899 width.push_back(gbrSimplex.computeWidthAndDuals(basis.getRow(i + 1), dual, 1900 dualDenom)); 1901 gbrSimplex.removeLastEquality(); 1902 } 1903 1904 // This variable stores width_i(b_{i+1} + u*b_i). 1905 Fraction widthICandidate = updateBasisWithUAndGetFCandidate(i); 1906 if (widthICandidate < epsilon * width[i - level]) { 1907 basis.swapRows(i, i + 1); 1908 width[i - level] = widthICandidate; 1909 // The values of width_{i+1}(b_{i+1}) and higher may change after the 1910 // swap, so we remove the cached values here. 1911 width.resize(i - level + 1); 1912 if (i == level) { 1913 dual.clear(); 1914 continue; 1915 } 1916 1917 gbrSimplex.removeLastEquality(); 1918 i--; 1919 continue; 1920 } 1921 1922 // Invalidate duals since the higher level needs to recompute its own duals. 1923 dual.clear(); 1924 gbrSimplex.addEqualityForDirection(basis.getRow(i)); 1925 i++; 1926 } 1927 } 1928 1929 /// Search for an integer sample point using a branch and bound algorithm. 1930 /// 1931 /// Each row in the basis matrix is a vector, and the set of basis vectors 1932 /// should span the space. Initially this is the identity matrix, 1933 /// i.e., the basis vectors are just the variables. 1934 /// 1935 /// In every level, a value is assigned to the level-th basis vector, as 1936 /// follows. Compute the minimum and maximum rational values of this direction. 1937 /// If only one integer point lies in this range, constrain the variable to 1938 /// have this value and recurse to the next variable. 1939 /// 1940 /// If the range has multiple values, perform generalized basis reduction via 1941 /// reduceBasis and then compute the bounds again. Now we try constraining 1942 /// this direction in the first value in this range and "recurse" to the next 1943 /// level. If we fail to find a sample, we try assigning the direction the next 1944 /// value in this range, and so on. 1945 /// 1946 /// If no integer sample is found from any of the assignments, or if the range 1947 /// contains no integer value, then of course the polytope is empty for the 1948 /// current assignment of the values in previous levels, so we return to 1949 /// the previous level. 1950 /// 1951 /// If we reach the last level where all the variables have been assigned values 1952 /// already, then we simply return the current sample point if it is integral, 1953 /// and go back to the previous level otherwise. 1954 /// 1955 /// To avoid potentially arbitrarily large recursion depths leading to stack 1956 /// overflows, this algorithm is implemented iteratively. 1957 Optional<SmallVector<int64_t, 8>> Simplex::findIntegerSample() { 1958 if (empty) 1959 return {}; 1960 1961 unsigned nDims = var.size(); 1962 Matrix basis = Matrix::identity(nDims); 1963 1964 unsigned level = 0; 1965 // The snapshot just before constraining a direction to a value at each level. 1966 SmallVector<unsigned, 8> snapshotStack; 1967 // The maximum value in the range of the direction for each level. 1968 SmallVector<int64_t, 8> upperBoundStack; 1969 // The next value to try constraining the basis vector to at each level. 1970 SmallVector<int64_t, 8> nextValueStack; 1971 1972 snapshotStack.reserve(basis.getNumRows()); 1973 upperBoundStack.reserve(basis.getNumRows()); 1974 nextValueStack.reserve(basis.getNumRows()); 1975 while (level != -1u) { 1976 if (level == basis.getNumRows()) { 1977 // We've assigned values to all variables. Return if we have a sample, 1978 // or go back up to the previous level otherwise. 1979 if (auto maybeSample = getSamplePointIfIntegral()) 1980 return maybeSample; 1981 level--; 1982 continue; 1983 } 1984 1985 if (level >= upperBoundStack.size()) { 1986 // We haven't populated the stack values for this level yet, so we have 1987 // just come down a level ("recursed"). Find the lower and upper bounds. 1988 // If there is more than one integer point in the range, perform 1989 // generalized basis reduction. 1990 SmallVector<int64_t, 8> basisCoeffs = 1991 llvm::to_vector<8>(basis.getRow(level)); 1992 basisCoeffs.emplace_back(0); 1993 1994 MaybeOptimum<int64_t> minRoundedUp, maxRoundedDown; 1995 std::tie(minRoundedUp, maxRoundedDown) = 1996 computeIntegerBounds(basisCoeffs); 1997 1998 // We don't have any integer values in the range. 1999 // Pop the stack and return up a level. 2000 if (minRoundedUp.isEmpty() || maxRoundedDown.isEmpty()) { 2001 assert((minRoundedUp.isEmpty() && maxRoundedDown.isEmpty()) && 2002 "If one bound is empty, both should be."); 2003 snapshotStack.pop_back(); 2004 nextValueStack.pop_back(); 2005 upperBoundStack.pop_back(); 2006 level--; 2007 continue; 2008 } 2009 2010 // We already checked the empty case above. 2011 assert((minRoundedUp.isBounded() && maxRoundedDown.isBounded()) && 2012 "Polyhedron should be bounded!"); 2013 2014 // Heuristic: if the sample point is integral at this point, just return 2015 // it. 2016 if (auto maybeSample = getSamplePointIfIntegral()) 2017 return *maybeSample; 2018 2019 if (*minRoundedUp < *maxRoundedDown) { 2020 reduceBasis(basis, level); 2021 basisCoeffs = llvm::to_vector<8>(basis.getRow(level)); 2022 basisCoeffs.emplace_back(0); 2023 std::tie(minRoundedUp, maxRoundedDown) = 2024 computeIntegerBounds(basisCoeffs); 2025 } 2026 2027 snapshotStack.push_back(getSnapshot()); 2028 // The smallest value in the range is the next value to try. 2029 // The values in the optionals are guaranteed to exist since we know the 2030 // polytope is bounded. 2031 nextValueStack.push_back(*minRoundedUp); 2032 upperBoundStack.push_back(*maxRoundedDown); 2033 } 2034 2035 assert((snapshotStack.size() - 1 == level && 2036 nextValueStack.size() - 1 == level && 2037 upperBoundStack.size() - 1 == level) && 2038 "Mismatched variable stack sizes!"); 2039 2040 // Whether we "recursed" or "returned" from a lower level, we rollback 2041 // to the snapshot of the starting state at this level. (in the "recursed" 2042 // case this has no effect) 2043 rollback(snapshotStack.back()); 2044 int64_t nextValue = nextValueStack.back(); 2045 ++nextValueStack.back(); 2046 if (nextValue > upperBoundStack.back()) { 2047 // We have exhausted the range and found no solution. Pop the stack and 2048 // return up a level. 2049 snapshotStack.pop_back(); 2050 nextValueStack.pop_back(); 2051 upperBoundStack.pop_back(); 2052 level--; 2053 continue; 2054 } 2055 2056 // Try the next value in the range and "recurse" into the next level. 2057 SmallVector<int64_t, 8> basisCoeffs(basis.getRow(level).begin(), 2058 basis.getRow(level).end()); 2059 basisCoeffs.push_back(-nextValue); 2060 addEquality(basisCoeffs); 2061 level++; 2062 } 2063 2064 return {}; 2065 } 2066 2067 /// Compute the minimum and maximum integer values the expression can take. We 2068 /// compute each separately. 2069 std::pair<MaybeOptimum<int64_t>, MaybeOptimum<int64_t>> 2070 Simplex::computeIntegerBounds(ArrayRef<int64_t> coeffs) { 2071 MaybeOptimum<int64_t> minRoundedUp( 2072 computeOptimum(Simplex::Direction::Down, coeffs).map(ceil)); 2073 MaybeOptimum<int64_t> maxRoundedDown( 2074 computeOptimum(Simplex::Direction::Up, coeffs).map(floor)); 2075 return {minRoundedUp, maxRoundedDown}; 2076 } 2077 2078 void SimplexBase::print(raw_ostream &os) const { 2079 os << "rows = " << getNumRows() << ", columns = " << getNumColumns() << "\n"; 2080 if (empty) 2081 os << "Simplex marked empty!\n"; 2082 os << "var: "; 2083 for (unsigned i = 0; i < var.size(); ++i) { 2084 if (i > 0) 2085 os << ", "; 2086 var[i].print(os); 2087 } 2088 os << "\ncon: "; 2089 for (unsigned i = 0; i < con.size(); ++i) { 2090 if (i > 0) 2091 os << ", "; 2092 con[i].print(os); 2093 } 2094 os << '\n'; 2095 for (unsigned row = 0, e = getNumRows(); row < e; ++row) { 2096 if (row > 0) 2097 os << ", "; 2098 os << "r" << row << ": " << rowUnknown[row]; 2099 } 2100 os << '\n'; 2101 os << "c0: denom, c1: const"; 2102 for (unsigned col = 2, e = getNumColumns(); col < e; ++col) 2103 os << ", c" << col << ": " << colUnknown[col]; 2104 os << '\n'; 2105 for (unsigned row = 0, numRows = getNumRows(); row < numRows; ++row) { 2106 for (unsigned col = 0, numCols = getNumColumns(); col < numCols; ++col) 2107 os << tableau(row, col) << '\t'; 2108 os << '\n'; 2109 } 2110 os << '\n'; 2111 } 2112 2113 void SimplexBase::dump() const { print(llvm::errs()); } 2114 2115 bool Simplex::isRationalSubsetOf(const IntegerRelation &rel) { 2116 if (isEmpty()) 2117 return true; 2118 2119 for (unsigned i = 0, e = rel.getNumInequalities(); i < e; ++i) 2120 if (findIneqType(rel.getInequality(i)) != IneqType::Redundant) 2121 return false; 2122 2123 for (unsigned i = 0, e = rel.getNumEqualities(); i < e; ++i) 2124 if (!isRedundantEquality(rel.getEquality(i))) 2125 return false; 2126 2127 return true; 2128 } 2129 2130 /// Returns the type of the inequality with coefficients `coeffs`. 2131 /// Possible types are: 2132 /// Redundant The inequality is satisfied by all points in the polytope 2133 /// Cut The inequality is satisfied by some points, but not by others 2134 /// Separate The inequality is not satisfied by any point 2135 /// 2136 /// Internally, this computes the minimum and the maximum the inequality with 2137 /// coefficients `coeffs` can take. If the minimum is >= 0, the inequality holds 2138 /// for all points in the polytope, so it is redundant. If the minimum is <= 0 2139 /// and the maximum is >= 0, the points in between the minimum and the 2140 /// inequality do not satisfy it, the points in between the inequality and the 2141 /// maximum satisfy it. Hence, it is a cut inequality. If both are < 0, no 2142 /// points of the polytope satisfy the inequality, which means it is a separate 2143 /// inequality. 2144 Simplex::IneqType Simplex::findIneqType(ArrayRef<int64_t> coeffs) { 2145 MaybeOptimum<Fraction> minimum = computeOptimum(Direction::Down, coeffs); 2146 if (minimum.isBounded() && *minimum >= Fraction(0, 1)) { 2147 return IneqType::Redundant; 2148 } 2149 MaybeOptimum<Fraction> maximum = computeOptimum(Direction::Up, coeffs); 2150 if ((!minimum.isBounded() || *minimum <= Fraction(0, 1)) && 2151 (!maximum.isBounded() || *maximum >= Fraction(0, 1))) { 2152 return IneqType::Cut; 2153 } 2154 return IneqType::Separate; 2155 } 2156 2157 /// Checks whether the type of the inequality with coefficients `coeffs` 2158 /// is Redundant. 2159 bool Simplex::isRedundantInequality(ArrayRef<int64_t> coeffs) { 2160 assert(!empty && 2161 "It is not meaningful to ask about redundancy in an empty set!"); 2162 return findIneqType(coeffs) == IneqType::Redundant; 2163 } 2164 2165 /// Check whether the equality given by `coeffs == 0` is redundant given 2166 /// the existing constraints. This is redundant when `coeffs` is already 2167 /// always zero under the existing constraints. `coeffs` is always zero 2168 /// when the minimum and maximum value that `coeffs` can take are both zero. 2169 bool Simplex::isRedundantEquality(ArrayRef<int64_t> coeffs) { 2170 assert(!empty && 2171 "It is not meaningful to ask about redundancy in an empty set!"); 2172 MaybeOptimum<Fraction> minimum = computeOptimum(Direction::Down, coeffs); 2173 MaybeOptimum<Fraction> maximum = computeOptimum(Direction::Up, coeffs); 2174 assert((!minimum.isEmpty() && !maximum.isEmpty()) && 2175 "Optima should be non-empty for a non-empty set"); 2176 return minimum.isBounded() && maximum.isBounded() && 2177 *maximum == Fraction(0, 1) && *minimum == Fraction(0, 1); 2178 } 2179