| 1 | /* |
| 2 | Copyright (C) 2000 -- 2010, Lou Hafer, International Business Machines, |
| 3 | and others. All Rights Reserved. |
| 4 | |
| 5 | This code is licensed under the terms of the Eclipse Public License (EPL). |
| 6 | */ |
| 7 | |
| 8 | #include "CoinPragma.hpp" |
| 9 | |
| 10 | #include "OsiUnitTests.hpp" |
| 11 | |
| 12 | #include "OsiConfig.h" |
| 13 | |
| 14 | /* |
| 15 | #include "CoinTime.hpp" |
| 16 | #include <cstdlib> |
| 17 | #include <cassert> |
| 18 | #include <vector> |
| 19 | #include <iostream> |
| 20 | #include <iomanip> |
| 21 | #include <sstream> |
| 22 | #include <cstdio> |
| 23 | */ |
| 24 | |
| 25 | #include "CoinHelperFunctions.hpp" |
| 26 | #include "CoinFinite.hpp" |
| 27 | #include "CoinFloatEqual.hpp" |
| 28 | #include "CoinPackedVector.hpp" |
| 29 | #include "CoinPackedMatrix.hpp" |
| 30 | #include "OsiSolverInterface.hpp" |
| 31 | |
| 32 | using namespace OsiUnitTest; |
| 33 | |
| 34 | /* |
| 35 | Helper methods for the OSI simplex API test. |
| 36 | */ |
| 37 | namespace { |
| 38 | |
| 39 | /* |
| 40 | Test that the given vector is a unit vector with a 1 in the specified index |
| 41 | position |
| 42 | */ |
| 43 | bool isUnitVector(int /* ndx */, int len, double *vec) |
| 44 | { |
| 45 | bool retval = false; |
| 46 | |
| 47 | CoinAbsFltEq fltEq; |
| 48 | |
| 49 | int nzCount = 0; |
| 50 | int oneCount = 0; |
| 51 | int onePosn = -1; |
| 52 | |
| 53 | for (int j = 0; j < len; j++) { |
| 54 | if (!fltEq(vec[j], 0.0)) { |
| 55 | nzCount++; |
| 56 | if (fltEq(vec[j], 1.0)) { |
| 57 | oneCount++; |
| 58 | onePosn = j; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | if (nzCount == 1 && oneCount == 1 && onePosn >= 0) { |
| 63 | retval = true; |
| 64 | } |
| 65 | |
| 66 | if (OsiUnitTest::verbosity >= 2 && !retval) { |
| 67 | if (nzCount > oneCount) { |
| 68 | std::cout |
| 69 | << " Vector contains " << nzCount - oneCount |
| 70 | << " elements that are neither 1.0 or 0.0." << std::endl; |
| 71 | } |
| 72 | if (oneCount > 1) { |
| 73 | std::cout |
| 74 | << " Vector contains " << oneCount |
| 75 | << " elements that are 1.0." << std::endl; |
| 76 | } |
| 77 | if (oneCount < 1) { |
| 78 | std::cout |
| 79 | << " Vector contains no elements that are 1.0." << std::endl; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return (retval); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | Build a constraint system in basis order. Assumes that enableFactorization |
| 88 | has been called. Indirect test of getBasics, because if the basis columns are |
| 89 | loaded incorrectly, checks that B inv(B) = I will surely fail. |
| 90 | */ |
| 91 | CoinPackedMatrix *buildBasisMatrix(const OsiSolverInterface *si) |
| 92 | { |
| 93 | std::string solverName; |
| 94 | si->getStrParam(OsiSolverName, solverName); |
| 95 | |
| 96 | CoinPackedMatrix *basisMtx = new CoinPackedMatrix(); |
| 97 | const CoinPackedMatrix *mtx = si->getMatrixByCol(); |
| 98 | |
| 99 | int m = si->getNumRows(); |
| 100 | int n = si->getNumCols(); |
| 101 | int *basicIndices = new int[m]; |
| 102 | si->getBasics(basicIndices); |
| 103 | |
| 104 | for (int i = 0; i < m; i++) { |
| 105 | int j = basicIndices[i]; |
| 106 | if (j < n) { |
| 107 | if (OsiUnitTest::verbosity >= 2) { |
| 108 | std::cout |
| 109 | << " Retrieving column " << j << " for basis pos'n " << i << "." |
| 110 | << std::endl; |
| 111 | } |
| 112 | CoinShallowPackedVector col = mtx->getVector(j); |
| 113 | basisMtx->appendCol(col); |
| 114 | } else { |
| 115 | j -= n; |
| 116 | if (OsiUnitTest::verbosity >= 2) { |
| 117 | std::cout |
| 118 | << " Fabricating e<" << j << "> for basis pos'n " << i << "." |
| 119 | << std::endl; |
| 120 | } |
| 121 | CoinPackedVector ei = CoinPackedVector(1, &j, 1.0); |
| 122 | basisMtx->appendCol(ei); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return (basisMtx); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | Test columns beta<k> = inv(B)e<k> of the basis inverse by calculating |
| 131 | B beta<k> and checking that the result is the appropriate unit vector. |
| 132 | Also tests getBasics, because we use it to build the basis matrix. |
| 133 | |
| 134 | It's assumed that the si passed as a parameter is ready for tableau |
| 135 | queries: a problem has been loaded and solved to optimality and |
| 136 | enableFactorization has been called. |
| 137 | */ |
| 138 | void testBInvCol(const OsiSolverInterface *si) |
| 139 | { |
| 140 | std::string solverName; |
| 141 | si->getStrParam(OsiSolverName, solverName); |
| 142 | |
| 143 | int m = si->getNumRows(); |
| 144 | |
| 145 | std::cout << " Testing getBInvCol ... " << std::endl; |
| 146 | |
| 147 | CoinPackedMatrix *basisMtx = buildBasisMatrix(si); |
| 148 | /* |
| 149 | Fetch beta<k>, calculate B beta<k>, k = 0, ..., m-1, and check that we have |
| 150 | the appropriate unit vector. |
| 151 | */ |
| 152 | double *betak = new double[m]; |
| 153 | double *ek = new double[m]; |
| 154 | for (int k = 0; k < m; k++) { |
| 155 | CoinFillN(betak, m, COIN_DBL_MAX); |
| 156 | CoinFillN(ek, m, COIN_DBL_MAX); |
| 157 | OSIUNITTEST_CATCH_ERROR(si->getBInvCol(k, betak), {}, solverName, "testBInvCol"); |
| 158 | |
| 159 | basisMtx->times(betak, ek); |
| 160 | OSIUNITTEST_ASSERT_ERROR(isUnitVector(k, m, ek), |
| 161 | if (OsiUnitTest::verbosity >= 1) { std::cout << " " |
| 162 | << "B beta<" << k << "> != e<" << k << ">." << std::endl; }, |
| 163 | solverName, "testBInvCol"); |
| 164 | } |
| 165 | delete[] betak; |
| 166 | delete[] ek; |
| 167 | delete basisMtx; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | Test rows beta<i> = e<i>inv(B) of the basis inverse by calculating beta<i>B |
| 172 | and checking that the result is the appropriate unit vector. Also tests |
| 173 | getBasics, because we need it to build the basis matrix. |
| 174 | |
| 175 | It's assumed that the si passed as a parameter is ready for tableau queries: |
| 176 | a problem has been loaded and solved to optimality and enableFactorization |
| 177 | has been called. |
| 178 | */ |
| 179 | void testBInvRow(const OsiSolverInterface *si) |
| 180 | { |
| 181 | std::string solverName; |
| 182 | si->getStrParam(OsiSolverName, solverName); |
| 183 | |
| 184 | int m = si->getNumRows(); |
| 185 | |
| 186 | std::cout << " Testing getBInvRow ... " << std::endl; |
| 187 | |
| 188 | /* |
| 189 | Should construct in row-major form for transposeTimes, but efficiency is |
| 190 | not all that big an issue here. |
| 191 | */ |
| 192 | CoinPackedMatrix *basisMtx = buildBasisMatrix(si); |
| 193 | /* |
| 194 | Fetch beta<i>, calculate beta<i> B, i = 0, ..., m-1, and check that we have |
| 195 | the appropriate unit vector. |
| 196 | */ |
| 197 | double *betai = new double[m]; |
| 198 | double *ei = new double[m]; |
| 199 | for (int i = 0; i < m; i++) { |
| 200 | CoinFillN(betai, m, COIN_DBL_MAX); |
| 201 | CoinFillN(ei, m, COIN_DBL_MAX); |
| 202 | OSIUNITTEST_CATCH_ERROR(si->getBInvRow(i, betai), {}, solverName, "testBInvRow"); |
| 203 | |
| 204 | basisMtx->transposeTimes(betai, ei); |
| 205 | OSIUNITTEST_ASSERT_ERROR(isUnitVector(i, m, ei), |
| 206 | if (OsiUnitTest::verbosity >= 1) { std::cout << " " |
| 207 | << "beta<" << i << ">B != e<" << i << ">." << std::endl; }, |
| 208 | solverName, "testBInvRow"); |
| 209 | } |
| 210 | delete[] betai; |
| 211 | delete[] ei; |
| 212 | delete basisMtx; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | Test columns abar<j> = inv(B) a<j> by checking that B abar<j> = a<j>. |
| 217 | |
| 218 | It's assumed that the si passed as a parameter is ready for tableau |
| 219 | queries: a problem has been loaded and solved to optimality and |
| 220 | enableFactorization has been called. |
| 221 | */ |
| 222 | void testBInvACol(const OsiSolverInterface *si) |
| 223 | { |
| 224 | std::string solverName; |
| 225 | si->getStrParam(OsiSolverName, solverName); |
| 226 | |
| 227 | int n = si->getNumCols(); |
| 228 | int m = si->getNumRows(); |
| 229 | |
| 230 | std::cout << " Testing getBInvACol ... " << std::endl; |
| 231 | |
| 232 | CoinPackedMatrix *basisMtx = buildBasisMatrix(si); |
| 233 | const CoinPackedMatrix *mtx = si->getMatrixByCol(); |
| 234 | /* |
| 235 | Fetch abar<j>, calculate B abar<j>, k = 0, ..., n-1, and check that the |
| 236 | result is a<j>. |
| 237 | */ |
| 238 | double *abarj = new double[m]; |
| 239 | double *aj = new double[m]; |
| 240 | for (int j = 0; j < n; j++) { |
| 241 | CoinFillN(abarj, m, COIN_DBL_MAX); |
| 242 | CoinFillN(aj, m, COIN_DBL_MAX); |
| 243 | OSIUNITTEST_CATCH_ERROR(si->getBInvACol(j, abarj), {}, solverName, "testBInvACol"); |
| 244 | |
| 245 | basisMtx->times(abarj, aj); |
| 246 | const CoinShallowPackedVector pv = mtx->getVector(j); |
| 247 | OSIUNITTEST_ASSERT_ERROR(isEquivalent(pv, m, aj), |
| 248 | if (OsiUnitTest::verbosity >= 1) { std::cout << " " |
| 249 | << "B abar<" << j << "> != a<" << j << ">." << std::endl; }, |
| 250 | solverName, "testBInvACol"); |
| 251 | } |
| 252 | delete[] abarj; |
| 253 | delete[] aj; |
| 254 | delete basisMtx; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | Test rows abar<i> = e<i>(inv(B)(A I)). This is an awkward thing to check, |
| 259 | because there's no analog to the column identity B abar<j> = a<j>. Go with |
| 260 | brute force: Build inv(B)A row by row with getBInvARow and check it against |
| 261 | inv(B)A built column by column with getBInvACol. (Clearly, testBInvACol |
| 262 | should run first.) |
| 263 | |
| 264 | e<i>(inv(B)I) is of course beta<i> = e<i>inv(B), so we can just check that |
| 265 | beta<i>B = e<i>. |
| 266 | |
| 267 | It's assumed that the si passed as a parameter is ready for tableau |
| 268 | queries: a problem has been loaded and solved to optimality and |
| 269 | enableFactorization has been called. |
| 270 | */ |
| 271 | void testBInvARow(const OsiSolverInterface *si) |
| 272 | { |
| 273 | std::string solverName; |
| 274 | si->getStrParam(OsiSolverName, solverName); |
| 275 | |
| 276 | int n = si->getNumCols(); |
| 277 | int m = si->getNumRows(); |
| 278 | |
| 279 | std::cout << " Testing getBInvARow ... " << std::endl; |
| 280 | |
| 281 | CoinPackedMatrix *basisMtx = buildBasisMatrix(si); |
| 282 | /* |
| 283 | Construct inv(B)A by column, then change over to row-major ordering so we |
| 284 | can compare with the version build from tableau rows. |
| 285 | |
| 286 | Interesting quirk here: Turns out p0033's tableau has no nonzeros in rows |
| 287 | with index 6 or 15. Because of that, when abarj is converted to row-major, |
| 288 | it has only 15 rows, and that causes isEquivalent2 to fail. So force the |
| 289 | full size. |
| 290 | */ |
| 291 | CoinPackedMatrix abarjMtx; |
| 292 | double *abarj = new double[m]; |
| 293 | for (int j = 0; j < n; j++) { |
| 294 | si->getBInvACol(j, abarj); |
| 295 | CoinPackedVector pkv; |
| 296 | pkv.setFullNonZero(m, abarj); |
| 297 | abarjMtx.appendCol(pkv); |
| 298 | } |
| 299 | delete[] abarj; |
| 300 | abarjMtx.reverseOrdering(); |
| 301 | abarjMtx.setDimensions(m, n); |
| 302 | if (OsiUnitTest::verbosity >= 1) { |
| 303 | std::cout |
| 304 | << " Col-major tableau is " << abarjMtx.getNumRows() << " x " |
| 305 | << abarjMtx.getNumCols() << " with " << abarjMtx.getNumElements() |
| 306 | << " elements." << std::endl; |
| 307 | } |
| 308 | /* |
| 309 | Construct inv(B)A by row. Check the vectors returned for inv(B)I = beta<i> |
| 310 | as we go. |
| 311 | */ |
| 312 | CoinPackedMatrix abariMtx; |
| 313 | abariMtx.reverseOrdering(); |
| 314 | double *abari = new double[n]; |
| 315 | double *betai = new double[m]; |
| 316 | double *ei = new double[m]; |
| 317 | for (int i = 0; i < m; i++) { |
| 318 | CoinFillN(abari, n, COIN_DBL_MAX); |
| 319 | CoinFillN(betai, m, COIN_DBL_MAX); |
| 320 | OSIUNITTEST_CATCH_ERROR(si->getBInvARow(i, abari, betai), {}, solverName, "testBInvARow"); |
| 321 | CoinPackedVector pkv; |
| 322 | pkv.setFullNonZero(n, abari); |
| 323 | if (OsiUnitTest::verbosity >= 2) { |
| 324 | std::cout << " Adding"; |
| 325 | const int *indices = pkv.getIndices(); |
| 326 | for (int v = 0; v < pkv.getNumElements(); v++) { |
| 327 | std::cout << " (" << i << "," << indices[v] << ")"; |
| 328 | } |
| 329 | std::cout << std::endl; |
| 330 | if (!isEquivalent(pkv, n, abari)) |
| 331 | std::cout << " !! packed abari != full abari !!" << std::endl; |
| 332 | } |
| 333 | abariMtx.appendRow(pkv); |
| 334 | basisMtx->transposeTimes(betai, ei); |
| 335 | OSIUNITTEST_ASSERT_ERROR(isUnitVector(i, m, ei), |
| 336 | if (OsiUnitTest::verbosity >= 1) { std::cout << " " |
| 337 | << "beta<" << i << ">B != e<" << i << ">." << std::endl; }, |
| 338 | solverName, "testBInvARow"); |
| 339 | } |
| 340 | abariMtx.setDimensions(m, n); |
| 341 | if (OsiUnitTest::verbosity >= 2) { |
| 342 | std::cout |
| 343 | << " Row-major tableau is " << abariMtx.getNumRows() << " x " |
| 344 | << abariMtx.getNumCols() << " with " << abariMtx.getNumElements() |
| 345 | << " elements." << std::endl; |
| 346 | } |
| 347 | delete[] abari; |
| 348 | delete[] betai; |
| 349 | delete[] ei; |
| 350 | delete basisMtx; |
| 351 | /* |
| 352 | Check that the two matrices are equivalent. isEquivalent2 will report |
| 353 | differences, but we won't get a good count. But then, one error is all we |
| 354 | need to report. |
| 355 | */ |
| 356 | OSIUNITTEST_ASSERT_ERROR(abariMtx.isEquivalent2(abarjMtx), {}, solverName, "testBInvARow: tableaus built by rows and columns match"); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | Test the row and column duals returned by getReducedGradient. |
| 361 | |
| 362 | ** This method requires that the solver have an optimal solution in hand. ** |
| 363 | |
| 364 | The method checks the values returned by getReducedGradient against the |
| 365 | values held in the solver (getRowPrice, getReducedCost) and tests that |
| 366 | the sign of the reduced costs matches the status of the architectural |
| 367 | variables. None of these are guaranteed unless the problem has been solved |
| 368 | to optimality. The validity of the test hinges on the assumption that an |
| 369 | implementor will just do the calculations for the given c rather than try |
| 370 | to determine if the answer held in the solver is valid for the given c. |
| 371 | |
| 372 | * For row duals, test that y = c<B>inv(B), using getBInvCol (already |
| 373 | tested) to obtain the columns of the basis inverse. Also check that |
| 374 | we agree with getRowPrice. |
| 375 | * For column duals (aka reduced costs), test that cbar = c<N> - yN, |
| 376 | using the duals we've just checked. Also check that we agree with |
| 377 | getReducedCost. Cross-check the sign of the reduced costs against the |
| 378 | status vector returned by getBasisStatus. |
| 379 | |
| 380 | It's assumed that the si passed as a parameter is ready for tableau |
| 381 | queries: a problem has been loaded and solved to optimality and |
| 382 | enableFactorization has been called. |
| 383 | */ |
| 384 | void testReducedGradient(const OsiSolverInterface *si) |
| 385 | { |
| 386 | std::string solverName; |
| 387 | si->getStrParam(OsiSolverName, solverName); |
| 388 | |
| 389 | int n = si->getNumCols(); |
| 390 | int m = si->getNumRows(); |
| 391 | |
| 392 | double objSense = si->getObjSense(); |
| 393 | std::cout |
| 394 | << " Testing getReducedGradient ... "; |
| 395 | /* |
| 396 | Acquire the solver's notion of current duals and reduced costs before we do |
| 397 | anything else. |
| 398 | */ |
| 399 | const double *yGold = si->getRowPrice(); |
| 400 | const double *cbarGold = si->getReducedCost(); |
| 401 | /* |
| 402 | Acquire the basis and build c<B>, the vector of basic cost coefficients. For |
| 403 | logicals (basicIndices[k] >= n) assume a zero coefficient. |
| 404 | */ |
| 405 | int *basicIndices = new int[m]; |
| 406 | si->getBasics(basicIndices); |
| 407 | const double *c = si->getObjCoefficients(); |
| 408 | double *cB = new double[m]; |
| 409 | for (int k = 0; k < m; k++) { |
| 410 | int j = basicIndices[k]; |
| 411 | if (j < n) { |
| 412 | if (OsiUnitTest::verbosity >= 2) { |
| 413 | std::cout |
| 414 | << " Retrieving c<" << j << "> = " << c[j] |
| 415 | << " for basis pos'n " << k << "." << std::endl; |
| 416 | } |
| 417 | cB[k] = c[j]; |
| 418 | } else { |
| 419 | if (OsiUnitTest::verbosity >= 2) { |
| 420 | std::cout |
| 421 | << " Assuming c<n+" << n - j << "> = " << 0.0 |
| 422 | << " for basis pos'n " << k << "." << std::endl; |
| 423 | } |
| 424 | cB[k] = 0.0; |
| 425 | } |
| 426 | } |
| 427 | delete[] basicIndices; |
| 428 | /* |
| 429 | Call getReducedGradient to get duals and reduced costs. |
| 430 | */ |
| 431 | double *cbar = new double[n]; |
| 432 | double *y = new double[m]; |
| 433 | OSIUNITTEST_CATCH_SEVERITY_EXPECTED(si->getReducedGradient(cbar, y, c), |
| 434 | delete[] cbar; |
| 435 | delete[] y; |
| 436 | return, |
| 437 | solverName, "testReducedGradient", TestOutcome::ERROR, solverName == "cplex"); |
| 438 | |
| 439 | /* |
| 440 | Run through the columns of the basis. Retrieve beta<j>, calculate |
| 441 | dot(c<B>,beta<j>) and check that all three sources of y<k> agree. |
| 442 | */ |
| 443 | double *betaj = new double[m]; |
| 444 | CoinRelFltEq eq; |
| 445 | for (int k = 0; k < m; k++) { |
| 446 | double yk = 0.0; |
| 447 | si->getBInvCol(k, betaj); |
| 448 | for (int i = 0; i < m; i++) { |
| 449 | yk += cB[i] * betaj[i]; |
| 450 | } |
| 451 | OSIUNITTEST_ASSERT_ERROR(eq(y[k], yGold[k]), |
| 452 | if (OsiUnitTest::verbosity >= 1) |
| 453 | std::cout |
| 454 | << " " << y[k] << " = y<" << k << "> != yGold<" << k << "> = " << yGold[k] << ", diff = " << y[k] - yGold[k] << "." << std::endl, |
| 455 | solverName, "testReducedGradient"); |
| 456 | OSIUNITTEST_ASSERT_ERROR(eq(y[k], yk), |
| 457 | if (OsiUnitTest::verbosity >= 1) |
| 458 | std::cout |
| 459 | << " " << y[k] << " = y<" << k << "> != c<B>beta<" << k << "> = " << yk << ", diff = " << y[k] - yk << "." << std::endl, |
| 460 | solverName, "testReducedGradient"); |
| 461 | } |
| 462 | delete[] cB; |
| 463 | delete[] betaj; |
| 464 | /* |
| 465 | Now that we're confident the duals are correct, use them to calculate cbar |
| 466 | as c-yN and check that all sources for cbar agree. Also check that the sign |
| 467 | is correct given the status of the variable. There's no need to |
| 468 | differentiate basic and nonbasic columns here. |
| 469 | */ |
| 470 | int *archStatus = new int[n]; |
| 471 | int *logStatus = new int[m]; |
| 472 | si->getBasisStatus(archStatus, logStatus); |
| 473 | double dualTol; |
| 474 | si->getDblParam(OsiDualTolerance, dualTol); |
| 475 | |
| 476 | const int OsiSimplex_isFree = 0; |
| 477 | const int OsiSimplex_basic = 1; |
| 478 | const int OsiSimplex_nbub = 2; |
| 479 | const int OsiSimplex_nblb = 3; |
| 480 | std::string statNames[] = { "NBFR", "B", "NBUB", "NBLB" }; |
| 481 | |
| 482 | const CoinPackedMatrix *mtx = si->getMatrixByCol(); |
| 483 | double *cbarCalc = new double[n]; |
| 484 | mtx->transposeTimes(y, cbarCalc); |
| 485 | std::transform(c, c + n, cbarCalc, cbarCalc, std::minus< double >()); |
| 486 | |
| 487 | for (int j = 1; j < n; j++) { |
| 488 | double cbarj = cbar[j]; |
| 489 | int statj = archStatus[j]; |
| 490 | if (OsiUnitTest::verbosity >= 2) |
| 491 | std::cout << " x<" << j << "> " << statNames[statj] << ", cbar<" << j << "> = " << cbarj << "." << std::endl; |
| 492 | OSIUNITTEST_ASSERT_ERROR(eq(cbarj, cbarGold[j]), |
| 493 | if (OsiUnitTest::verbosity >= 1) |
| 494 | std::cout |
| 495 | << " " << cbarj << " = cbar<" << j << "> != cbarGold<" << j << "> = " << cbarGold[j] << ", diff = " << cbarj - cbarGold[j] << "." << std::endl, |
| 496 | solverName, "testReducedGradient"); |
| 497 | OSIUNITTEST_ASSERT_ERROR(eq(cbarj, cbarCalc[j]), |
| 498 | if (OsiUnitTest::verbosity >= 1) |
| 499 | std::cout |
| 500 | << " " << cbarj << " = cbar<" << j << "> != c<" << j << "> - ya<" << j << "> = " << cbarCalc[j] << ", diff = " << cbarj - cbarCalc[j] << "." << std::endl, |
| 501 | solverName, "testReducedGradient"); |
| 502 | double testcbarj = objSense * cbarj; |
| 503 | switch (statj) { |
| 504 | case OsiSimplex_nbub: { |
| 505 | OSIUNITTEST_ASSERT_ERROR(testcbarj <= dualTol, |
| 506 | if (OsiUnitTest::verbosity >= 1) |
| 507 | std::cout |
| 508 | << " cbar<" << j << "> = " << cbarj << " has the wrong sign for a NBUB variable." << std::endl, |
| 509 | solverName, "testReducedGradient"); |
| 510 | break; |
| 511 | } |
| 512 | case OsiSimplex_nblb: { |
| 513 | OSIUNITTEST_ASSERT_ERROR(testcbarj >= -dualTol, |
| 514 | if (OsiUnitTest::verbosity >= 1) |
| 515 | std::cout |
| 516 | << " cbar<" << j << "> = " << cbarj << " has the wrong sign for a NBLB variable." << std::endl, |
| 517 | solverName, "testReducedGradient"); |
| 518 | break; |
| 519 | } |
| 520 | case OsiSimplex_isFree: { |
| 521 | OSIUNITTEST_ASSERT_ERROR(CoinAbs(testcbarj) <= dualTol, |
| 522 | if (OsiUnitTest::verbosity >= 1) |
| 523 | std::cout |
| 524 | << " cbar<" << j << "> = " << cbarj << " should be zero for a NBFR variable." << std::endl, |
| 525 | solverName, "testReducedGradient"); |
| 526 | break; |
| 527 | } |
| 528 | case OsiSimplex_basic: { |
| 529 | OSIUNITTEST_ASSERT_ERROR(CoinAbs(testcbarj) <= dualTol, |
| 530 | if (OsiUnitTest::verbosity >= 1) |
| 531 | std::cout |
| 532 | << " cbar<" << j << "> = " << cbarj << " should be zero for a basic variable." << std::endl, |
| 533 | solverName, "testReducedGradient"); |
| 534 | break; |
| 535 | } |
| 536 | default: { |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | delete[] y; |
| 543 | delete[] cbar; |
| 544 | delete[] cbarCalc; |
| 545 | delete[] archStatus; |
| 546 | delete[] logStatus; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | Test the mode 2 portion of the simplex API. |
| 551 | Solve an lp by hand |
| 552 | */ |
| 553 | void testSimplexMode2(const OsiSolverInterface *emptySi, std::string sampleDir) |
| 554 | { |
| 555 | OsiSolverInterface *si = emptySi->clone(); |
| 556 | std::string solverName; |
| 557 | si->getStrParam(OsiSolverName, solverName); |
| 558 | std::string fn = sampleDir + "p0033"; |
| 559 | si->readMps(fn.c_str(), "mps"); |
| 560 | si->setObjSense(-1.0); |
| 561 | si->initialSolve(); |
| 562 | si->setObjSense(1.0); |
| 563 | // enable special mode |
| 564 | si->enableSimplexInterface(true); |
| 565 | // we happen to know that variables are 0-1 and rows are L |
| 566 | int numberIterations = 0; |
| 567 | int numberColumns = si->getNumCols(); |
| 568 | int numberRows = si->getNumRows(); |
| 569 | double *fakeCost = new double[numberColumns]; |
| 570 | double *duals = new double[numberRows]; |
| 571 | double *djs = new double[numberColumns]; |
| 572 | const double *solution = si->getColSolution(); |
| 573 | memcpy(fakeCost, si->getObjCoefficients(), numberColumns * sizeof(double)); |
| 574 | while (1) { |
| 575 | const double *dj; |
| 576 | const double *dual; |
| 577 | if ((numberIterations & 1) == 0) { |
| 578 | // use given ones |
| 579 | dj = si->getReducedCost(); |
| 580 | dual = si->getRowPrice(); |
| 581 | } else { |
| 582 | // create |
| 583 | dj = djs; |
| 584 | dual = duals; |
| 585 | si->getReducedGradient(djs, duals, fakeCost); |
| 586 | } |
| 587 | int i; |
| 588 | int colIn = 9999; |
| 589 | int direction = 1; |
| 590 | double best = 1.0e-6; |
| 591 | // find most negative reduced cost |
| 592 | // Should check basic - but should be okay on this problem |
| 593 | for (i = 0; i < numberRows; i++) { |
| 594 | double value = dual[i]; |
| 595 | if (value > best) { |
| 596 | direction = -1; |
| 597 | best = value; |
| 598 | colIn = -i - 1; |
| 599 | } |
| 600 | } |
| 601 | for (i = 0; i < numberColumns; i++) { |
| 602 | double value = dj[i]; |
| 603 | if (value < -best && solution[i] < 1.0e-6) { |
| 604 | direction = 1; |
| 605 | best = -value; |
| 606 | colIn = i; |
| 607 | } else if (value > best && solution[i] > 1.0 - 1.0e-6) { |
| 608 | direction = -1; |
| 609 | best = value; |
| 610 | colIn = i; |
| 611 | } |
| 612 | } |
| 613 | if (colIn == 9999) |
| 614 | break; // should be optimal |
| 615 | int colOut; |
| 616 | int outStatus; |
| 617 | double theta; |
| 618 | OSIUNITTEST_ASSERT_ERROR(!si->primalPivotResult(colIn, direction, colOut, outStatus, theta, NULL), break, solverName, "testSimplexMode2"); |
| 619 | printf("out %d, direction %d theta %g\n", |
| 620 | colOut, outStatus, theta); |
| 621 | numberIterations++; |
| 622 | } |
| 623 | delete[] fakeCost; |
| 624 | delete[] duals; |
| 625 | delete[] djs; |
| 626 | // exit special mode |
| 627 | si->disableSimplexInterface(); |
| 628 | si->resolve(); |
| 629 | OSIUNITTEST_ASSERT_ERROR(!si->getIterationCount(), {}, solverName, "testSimplexMode2: resolve after disable simplex interface"); |
| 630 | si->setObjSense(-1.0); |
| 631 | si->initialSolve(); |
| 632 | std::cout << solverName << " passed OsiSimplexInterface test" << std::endl; |
| 633 | delete si; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | Test Simplex API mode 1 (tableau access) methods. |
| 638 | */ |
| 639 | void testSimplexMode1(const OsiSolverInterface *emptySi, std::string sampleDir) |
| 640 | { |
| 641 | OsiSolverInterface *si = emptySi->clone(); |
| 642 | std::string solverName; |
| 643 | si->getStrParam(OsiSolverName, solverName); |
| 644 | si->setHintParam(OsiDoReducePrint, true, OsiHintDo); |
| 645 | /* |
| 646 | Read p0033 and check that there's no optimal basis prior to solving. |
| 647 | */ |
| 648 | std::string fn = sampleDir + "p0033"; |
| 649 | si->readMps(fn.c_str(), "mps"); |
| 650 | |
| 651 | OSIUNITTEST_ASSERT_ERROR(!si->basisIsAvailable(), |
| 652 | if (OsiUnitTest::verbosity >= 1) |
| 653 | std::cout |
| 654 | << " " << solverName << " shows no optimal basis before initial solve." << std::endl, |
| 655 | *si, "testSimplexMode1: basis before solve"); |
| 656 | /* |
| 657 | Solve as minimisation problem. |
| 658 | */ |
| 659 | si->setObjSense(1.0); |
| 660 | si->initialSolve(); |
| 661 | OSIUNITTEST_ASSERT_ERROR(si->isProvenOptimal(), return, *si, "testSimplexMode1: solve p0033"); |
| 662 | if (OsiUnitTest::verbosity >= 1) { |
| 663 | std::cout |
| 664 | << " " << solverName << " solved p0033 z = " << si->getObjValue() |
| 665 | << "." << std::endl; |
| 666 | } |
| 667 | /* |
| 668 | Now get tough. Resolve, first as maximisation, then minimisation. Enable the |
| 669 | tableau interface and check the methods. |
| 670 | */ |
| 671 | double minmax[] = { -1.0, 1.0 }; |
| 672 | for (int ndx = 0; ndx < 2; ndx++) { |
| 673 | si->setObjSense(minmax[ndx]); |
| 674 | std::cout |
| 675 | << " " << ((minmax[ndx] < 0) ? "maximisation ..." : "minimisation") |
| 676 | << " ..." << std::endl; |
| 677 | si->resolve(); |
| 678 | OSIUNITTEST_ASSERT_ERROR(si->isProvenOptimal(), return, *si, "testSimplexMode1: resolve p0033"); |
| 679 | if (OsiUnitTest::verbosity >= 1) { |
| 680 | std::cout << " " << solverName << ((si->getObjSense() < 0) ? " maximised" : " minimised") << " p0033 z = " << si->getObjValue() << "." << std::endl; |
| 681 | } |
| 682 | OSIUNITTEST_ASSERT_ERROR(si->basisIsAvailable(), {}, *si, "testSimplexMode1: basis available after resolve"); |
| 683 | if (OsiUnitTest::verbosity >= 1 && si->basisIsAvailable()) { |
| 684 | std::cout << " " << solverName << " shows optimal basis after resolve." << std::endl; |
| 685 | } |
| 686 | /* |
| 687 | Enable simplex mode 1. |
| 688 | */ |
| 689 | si->enableFactorization(); |
| 690 | /* |
| 691 | Test the various methods. |
| 692 | */ |
| 693 | testBInvCol(si); |
| 694 | testBInvRow(si); |
| 695 | testBInvACol(si); |
| 696 | testBInvARow(si); |
| 697 | testReducedGradient(si); |
| 698 | /* |
| 699 | Disable simplex mode 1. |
| 700 | */ |
| 701 | si->disableFactorization(); |
| 702 | } |
| 703 | /* |
| 704 | Trash this solver and we're finished. |
| 705 | */ |
| 706 | delete si; |
| 707 | } |
| 708 | |
| 709 | } // end file-local namespace |
| 710 | |
| 711 | namespace OsiUnitTest { |
| 712 | |
| 713 | /* |
| 714 | Test a solver's implementation of the OSI simplex API. |
| 715 | */ |
| 716 | |
| 717 | void testSimplexAPI(const OsiSolverInterface *emptySi, const std::string &sampleDir) |
| 718 | { |
| 719 | OsiSolverInterface *si = emptySi->clone(); |
| 720 | std::string solverName; |
| 721 | si->getStrParam(OsiSolverName, solverName); |
| 722 | /* |
| 723 | Do the tests only if the solver implements the simplex API. |
| 724 | */ |
| 725 | if (si->canDoSimplexInterface() == 0) { |
| 726 | OSIUNITTEST_ADD_OUTCOME(solverName, "testSimplexAPI", "skipped test", OsiUnitTest::TestOutcome::NOTE, true); |
| 727 | std::cout << solverName << " has no OsiSimplex API." << std::endl; |
| 728 | return; |
| 729 | } |
| 730 | /* |
| 731 | Test the mode 1 (tableau access) portion of the API. |
| 732 | */ |
| 733 | if (si->canDoSimplexInterface() >= 1) { |
| 734 | std::cout << "Testing Simplex API mode 1 for " << solverName << " ... " << std::endl; |
| 735 | testSimplexMode1(emptySi, sampleDir); |
| 736 | } |
| 737 | /* |
| 738 | Test the mode 2 (pivot-by-pivot control) portion of the API. |
| 739 | */ |
| 740 | if (si->canDoSimplexInterface() >= 2) { |
| 741 | std::cout << "Testing Simplex API mode 2 for " << solverName << " ... " << std::endl; |
| 742 | testSimplexMode2(emptySi, sampleDir); |
| 743 | } else { |
| 744 | OSIUNITTEST_ADD_OUTCOME(solverName, "testSimplexAPI mode 2", "skipped test", OsiUnitTest::TestOutcome::NOTE, true); |
| 745 | std::cout << solverName << " does not implement Simplex API mode 2." << std::endl; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | } /* namespace OsiUnitTest */ |
| 750 | |
| 751 | /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2 |
| 752 | */ |