| 1 | // Copyright (C) 2000, International Business Machines |
| 2 | // Corporation and others. All Rights Reserved. |
| 3 | // Test individual classes or groups of classes |
| 4 | // This file is licensed under the terms of Eclipse Public License (EPL). |
| 5 | // $Id: unitTest.cpp 1891 2013-02-04 21:22:58Z stefan $ |
| 6 | |
| 7 | #include "CoinPragma.hpp" |
| 8 | #include "OsiConfig.h" |
| 9 | |
| 10 | #include <iostream> |
| 11 | |
| 12 | #include "OsiUnitTests.hpp" |
| 13 | #include "OsiSolverInterface.hpp" |
| 14 | #include "OsiTestSolverInterface.hpp" |
| 15 | |
| 16 | using namespace OsiUnitTest; |
| 17 | |
| 18 | /* |
| 19 | Currently the Osi unit test is configured to exercise only the external |
| 20 | solvers. The Osi interfaces for Coin solvers have been moved out to the |
| 21 | project's repository and each has its own private Osi unit test. |
| 22 | |
| 23 | This unit test will include as many external solvers as are available. If |
| 24 | none of them are available, the OsiTestSolver (currently a clone of Vol) |
| 25 | will be used. If any other solver is available, its presence will disable |
| 26 | use of the test solver. You can disable it manually by undefining |
| 27 | USETESTSOLVER. |
| 28 | |
| 29 | You may want to use the Osi unit test to compare two or more Coin solvers. |
| 30 | In particular, OsiSolverInterfaceMpsUnitTest, which runs the Netlib problem |
| 31 | set, is set up for exactly this sort of comparison. To take advantage of |
| 32 | it, you'll need to edit this file and Makefile in order to get it to work. |
| 33 | */ |
| 34 | #define USETESTSOLVER |
| 35 | |
| 36 | /* |
| 37 | Some convenient undef's, to make it easy to isolate a particular solver. |
| 38 | Uncomment to disable a solver that's included in the build. Leave them |
| 39 | commented if you're happy with running the unitTest for all solvers in |
| 40 | the build. |
| 41 | */ |
| 42 | // #undef COIN_HAS_XPR |
| 43 | // #undef COIN_HAS_CPX |
| 44 | // #undef COIN_HAS_GLPK |
| 45 | // #undef COIN_HAS_MSK |
| 46 | // #undef COIN_HAS_GRB |
| 47 | // #undef COIN_HAS_SOPLEX |
| 48 | |
| 49 | #ifdef COIN_HAS_XPR |
| 50 | #include "OsiXprSolverInterface.hpp" |
| 51 | #ifdef USETESTSOLVER |
| 52 | #undef USETESTSOLVER |
| 53 | #endif |
| 54 | #endif |
| 55 | |
| 56 | #ifdef COIN_HAS_CPX |
| 57 | #include "OsiCpxSolverInterface.hpp" |
| 58 | #undef USETESTSOLVER |
| 59 | #ifdef USETESTSOLVER |
| 60 | #undef USETESTSOLVER |
| 61 | #endif |
| 62 | #endif |
| 63 | |
| 64 | #ifdef COIN_HAS_GLPK |
| 65 | #include "OsiGlpkSolverInterface.hpp" |
| 66 | #ifdef USETESTSOLVER |
| 67 | #undef USETESTSOLVER |
| 68 | #endif |
| 69 | #endif |
| 70 | |
| 71 | #ifdef COIN_HAS_MSK |
| 72 | #include "OsiMskSolverInterface.hpp" |
| 73 | #ifdef USETESTSOLVER |
| 74 | #undef USETESTSOLVER |
| 75 | #endif |
| 76 | #endif |
| 77 | |
| 78 | #ifdef COIN_HAS_GRB |
| 79 | #include "OsiGrbSolverInterface.hpp" |
| 80 | #ifdef USETESTSOLVER |
| 81 | #undef USETESTSOLVER |
| 82 | #endif |
| 83 | #endif |
| 84 | |
| 85 | #ifdef COIN_HAS_SOPLEX |
| 86 | #include "OsiSpxSolverInterface.hpp" |
| 87 | #ifdef USETESTSOLVER |
| 88 | #undef USETESTSOLVER |
| 89 | #endif |
| 90 | #endif |
| 91 | |
| 92 | #ifdef USETESTSOLVER |
| 93 | #include "OsiTestSolverInterface.hpp" |
| 94 | #endif |
| 95 | |
| 96 | |
| 97 | //---------------------------------------------------------------- |
| 98 | // to see parameter list, call unitTest -usage |
| 99 | //---------------------------------------------------------------- |
| 100 | |
| 101 | int main (int argc, const char *argv[]) |
| 102 | { |
| 103 | /* |
| 104 | Start off with various bits of initialisation that don't really belong |
| 105 | anywhere else. |
| 106 | |
| 107 | First off, synchronise C++ stream i/o with C stdio. This makes debugging |
| 108 | output a bit more comprehensible. It still suffers from interleave of cout |
| 109 | (stdout) and cerr (stderr), but -nobuf deals with that. |
| 110 | */ |
| 111 | std::ios::sync_with_stdio() ; |
| 112 | /* |
| 113 | Suppress an popup window that Windows shows in response to a crash. See |
| 114 | note at head of file. |
| 115 | */ |
| 116 | WindowsErrorPopupBlocker(); |
| 117 | /* |
| 118 | Might as well make use of this convenient Xpress feature. |
| 119 | */ |
| 120 | #ifdef COIN_HAS_XPR |
| 121 | OsiXprSolverInterface::setLogFileName("xprCallTrace.txt"); |
| 122 | #endif |
| 123 | |
| 124 | /* |
| 125 | Process command line parameters. |
| 126 | */ |
| 127 | std::map<std::string,std::string> parms ; |
| 128 | if (processParameters(argc,argv,parms) == false) |
| 129 | return 1; |
| 130 | |
| 131 | std::string mpsDir = parms["-mpsDir"] ; |
| 132 | std::string netlibDir = parms["-netlibDir"] ; |
| 133 | |
| 134 | /* |
| 135 | Test Osi{Row,Col}Cut routines. |
| 136 | */ |
| 137 | #ifdef COIN_HAS_XPR |
| 138 | { |
| 139 | OsiXprSolverInterface xprSi; |
| 140 | testingMessage( "Testing OsiRowCut with OsiXprSolverInterface\n" ); |
| 141 | OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&xprSi,mpsDir), {}, xprSi, "rowcut unittest"); |
| 142 | } |
| 143 | { |
| 144 | OsiXprSolverInterface xprSi; |
| 145 | testingMessage( "Testing OsiColCut with OsiXprSolverInterface\n" ); |
| 146 | OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&xprSi,mpsDir), {}, xprSi, "colcut unittest"); |
| 147 | } |
| 148 | { |
| 149 | OsiXprSolverInterface xprSi; |
| 150 | testingMessage( "Testing OsiRowCutDebugger with OsiXprSolverInterface\n" ); |
| 151 | OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&xprSi,mpsDir), {}, xprSi, "rowcut debugger unittest"); |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | #ifdef COIN_HAS_CPX |
| 156 | { |
| 157 | OsiCpxSolverInterface cpxSi; |
| 158 | testingMessage( "Testing OsiRowCut with OsiCpxSolverInterface\n" ); |
| 159 | OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&cpxSi,mpsDir), {}, cpxSi, "rowcut unittest"); |
| 160 | } |
| 161 | { |
| 162 | OsiCpxSolverInterface cpxSi; |
| 163 | testingMessage( "Testing OsiColCut with OsiCpxSolverInterface\n" ); |
| 164 | OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&cpxSi,mpsDir), {}, cpxSi, "colcut unittest"); |
| 165 | } |
| 166 | { |
| 167 | OsiCpxSolverInterface cpxSi; |
| 168 | testingMessage( "Testing OsiRowCutDebugger with OsiCpxSolverInterface\n" ); |
| 169 | OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&cpxSi,mpsDir), {}, cpxSi, "rowcut debugger unittest"); |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | #ifdef USETESTSOLVER |
| 174 | { |
| 175 | OsiTestSolverInterface testSi; |
| 176 | testingMessage( "Testing OsiRowCut with OsiTestSolverInterface\n" ); |
| 177 | OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&testSi,mpsDir), {}, testSi, "rowcut unittest"); |
| 178 | } |
| 179 | { |
| 180 | OsiTestSolverInterface testSi; |
| 181 | testingMessage( "Testing OsiColCut with OsiTestSolverInterface\n" ); |
| 182 | OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&testSi,mpsDir), {}, testSi, "colcut unittest"); |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | #ifdef COIN_HAS_GLPK |
| 187 | { |
| 188 | OsiGlpkSolverInterface glpkSi; |
| 189 | testingMessage( "Testing OsiRowCut with OsiGlpkSolverInterface\n" ); |
| 190 | OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&glpkSi,mpsDir), {}, glpkSi, "rowcut unittest"); |
| 191 | } |
| 192 | { |
| 193 | OsiGlpkSolverInterface glpkSi; |
| 194 | testingMessage( "Testing OsiColCut with OsiGlpkSolverInterface\n" ); |
| 195 | OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&glpkSi,mpsDir), {}, glpkSi, "colcut unittest"); |
| 196 | } |
| 197 | { |
| 198 | OsiGlpkSolverInterface glpkSi; |
| 199 | testingMessage( "Testing OsiRowCutDebugger with OsiGlpkSolverInterface\n" ); |
| 200 | OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&glpkSi,mpsDir), {}, glpkSi, "rowcut debugger unittest"); |
| 201 | } |
| 202 | #endif |
| 203 | |
| 204 | #ifdef COIN_HAS_MSK |
| 205 | { |
| 206 | OsiMskSolverInterface MskSi; |
| 207 | testingMessage( "Testing OsiRowCut with OsiMskSolverInterface\n" ); |
| 208 | OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&MskSi,mpsDir), {}, MskSi, "rowcut unittest"); |
| 209 | } |
| 210 | { |
| 211 | OsiMskSolverInterface MskSi; |
| 212 | testingMessage( "Testing OsiColCut with OsiMskSolverInterface\n" ); |
| 213 | OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&MskSi,mpsDir), {}, MskSi, "colcut unittest"); |
| 214 | } |
| 215 | { |
| 216 | OsiMskSolverInterface MskSi; |
| 217 | testingMessage( "Testing OsiRowCutDebugger with OsiMskSolverInterface\n" ); |
| 218 | OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&MskSi,mpsDir), {}, MskSi, "rowcut debugger unittest"); |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | #ifdef COIN_HAS_GRB |
| 223 | { |
| 224 | OsiGrbSolverInterface grbSi; |
| 225 | testingMessage( "Testing OsiRowCut with OsiGrbSolverInterface\n" ); |
| 226 | OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&grbSi,mpsDir), {}, grbSi, "rowcut unittest"); |
| 227 | } |
| 228 | { |
| 229 | OsiGrbSolverInterface grbSi; |
| 230 | testingMessage( "Testing OsiColCut with OsiGrbSolverInterface\n" ); |
| 231 | OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&grbSi,mpsDir), {}, grbSi, "colcut unittest"); |
| 232 | } |
| 233 | { |
| 234 | OsiGrbSolverInterface grbSi; |
| 235 | testingMessage( "Testing OsiRowCutDebugger with OsiGrbSolverInterface\n" ); |
| 236 | OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&grbSi,mpsDir), {}, grbSi, "rowcut debugger unittest"); |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | #ifdef COIN_HAS_SOPLEX |
| 241 | { |
| 242 | OsiSpxSolverInterface spxSi; |
| 243 | testingMessage( "Testing OsiRowCut with OsiSpxSolverInterface\n" ); |
| 244 | OSIUNITTEST_CATCH_ERROR(OsiRowCutUnitTest(&spxSi,mpsDir), {}, spxSi, "rowcut unittest"); |
| 245 | } |
| 246 | { |
| 247 | OsiSpxSolverInterface spxSi; |
| 248 | testingMessage( "Testing OsiColCut with OsiSpxSolverInterface\n" ); |
| 249 | OSIUNITTEST_CATCH_ERROR(OsiColCutUnitTest(&spxSi,mpsDir), {}, spxSi, "colcut unittest"); |
| 250 | } |
| 251 | { |
| 252 | OsiSpxSolverInterface spxSi; |
| 253 | testingMessage( "Testing OsiRowCutDebugger with OsiSpxSolverInterface\n" ); |
| 254 | OSIUNITTEST_CATCH_ERROR(OsiRowCutDebuggerUnitTest(&spxSi,mpsDir), {}, spxSi, "rowcut debugger unittest"); |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | testingMessage( "Testing OsiCuts\n" ); |
| 259 | OSIUNITTEST_CATCH_ERROR(OsiCutsUnitTest(), {}, "osi", "osicuts unittest"); |
| 260 | |
| 261 | /* |
| 262 | Testing OsiCuts only? A useful option when doing memory access and leak |
| 263 | checks. Keeps the run time to something reasonable. |
| 264 | */ |
| 265 | if (parms.find("-cutsOnly") != parms.end()) { |
| 266 | testingMessage( "Stopping after OsiCuts tests.\n" ); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | Run the OsiXXX class test for each solver. It's up to the solver implementor |
| 272 | to decide whether or not to run OsiSolverInterfaceCommonUnitTest. Arguably |
| 273 | this should be required. |
| 274 | */ |
| 275 | #ifdef COIN_HAS_XPR |
| 276 | testingMessage( "Testing OsiXprSolverInterface\n" ); |
| 277 | OSIUNITTEST_CATCH_ERROR(OsiXprSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "xpress", "osixpr unittest"); |
| 278 | #endif |
| 279 | |
| 280 | #ifdef COIN_HAS_CPX |
| 281 | testingMessage( "Testing OsiCpxSolverInterface\n" ); |
| 282 | OSIUNITTEST_CATCH_ERROR(OsiCpxSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "cplex", "osicpx unittest"); |
| 283 | #endif |
| 284 | |
| 285 | #ifdef USETESTSOLVER |
| 286 | testingMessage( "Testing OsiTestSolverInterface\n" ); |
| 287 | OSIUNITTEST_CATCH_ERROR(OsiTestSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "vol", "ositestsolver unittest"); |
| 288 | #endif |
| 289 | |
| 290 | #ifdef COIN_HAS_GLPK |
| 291 | testingMessage( "Testing OsiGlpkSolverInterface\n" ); |
| 292 | OSIUNITTEST_CATCH_ERROR(OsiGlpkSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "glpk", "osiglpk unittest"); |
| 293 | #endif |
| 294 | |
| 295 | #ifdef COIN_HAS_MSK |
| 296 | testingMessage( "Testing OsiMskSolverInterface\n" ); |
| 297 | OSIUNITTEST_CATCH_ERROR(OsiMskSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "mosek", "osimsk unittest"); |
| 298 | #endif |
| 299 | |
| 300 | #ifdef COIN_HAS_GRB |
| 301 | testingMessage( "Testing OsiGrbSolverInterface\n" ); |
| 302 | OSIUNITTEST_CATCH_ERROR(OsiGrbSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "gurobi", "osigrb unittest"); |
| 303 | #endif |
| 304 | |
| 305 | #ifdef COIN_HAS_SOPLEX |
| 306 | testingMessage( "Testing OsiSpxSolverInterface\n" ); |
| 307 | OSIUNITTEST_CATCH_ERROR(OsiSpxSolverInterfaceUnitTest(mpsDir,netlibDir), {}, "soplex", "osispx unittest"); |
| 308 | #endif |
| 309 | |
| 310 | /* |
| 311 | Each solver has run its specialised unit test. Check now to see if we need to |
| 312 | run through the Netlib problems. |
| 313 | */ |
| 314 | if (parms.find("-testOsiSolverInterface") != parms.end()) |
| 315 | { |
| 316 | // Create vector of solver interfaces |
| 317 | std::vector<OsiSolverInterface*> vecSi; |
| 318 | # if COIN_HAS_XPR |
| 319 | OsiSolverInterface * xprSi = new OsiXprSolverInterface; |
| 320 | vecSi.push_back(xprSi); |
| 321 | # endif |
| 322 | # if COIN_HAS_CPX |
| 323 | OsiSolverInterface * cpxSi = new OsiCpxSolverInterface; |
| 324 | vecSi.push_back(cpxSi); |
| 325 | # endif |
| 326 | # if COIN_HAS_GLPK |
| 327 | OsiSolverInterface * glpkSi = new OsiGlpkSolverInterface; |
| 328 | glpkSi->setHintParam(OsiDoPresolveInInitial,true,OsiHintTry) ; |
| 329 | glpkSi->setHintParam(OsiDoReducePrint,true,OsiHintDo) ; |
| 330 | vecSi.push_back(glpkSi); |
| 331 | # endif |
| 332 | # if COIN_HAS_MSK |
| 333 | OsiSolverInterface * MskSi = new OsiMskSolverInterface; |
| 334 | vecSi.push_back(MskSi); |
| 335 | # endif |
| 336 | # if COIN_HAS_GRB |
| 337 | OsiSolverInterface * grbSi = new OsiGrbSolverInterface; |
| 338 | vecSi.push_back(grbSi); |
| 339 | # endif |
| 340 | # if COIN_HAS_SOPLEX |
| 341 | OsiSolverInterface * spxSi = new OsiSpxSolverInterface; |
| 342 | vecSi.push_back(spxSi); |
| 343 | # endif |
| 344 | # ifdef USETESTSOLVER |
| 345 | /* |
| 346 | The test solver is normally Vol, which can't do any of the netlib problems. |
| 347 | So let's not try. |
| 348 | */ |
| 349 | { |
| 350 | std::string solverName ; |
| 351 | OsiSolverInterface * testSi = new OsiTestSolverInterface; |
| 352 | testSi->getStrParam(OsiSolverName,solverName) ; |
| 353 | if (solverName != "vol") |
| 354 | { vecSi.push_back(testSi); } |
| 355 | else |
| 356 | { testingMessage("Test solver vol cannot do Netlib. Skipping.\n") ; } |
| 357 | } |
| 358 | # endif |
| 359 | |
| 360 | if (vecSi.size() > 0) |
| 361 | { testingMessage( "Testing OsiSolverInterface on Netlib problems.\n" ); |
| 362 | OSIUNITTEST_CATCH_ERROR(OsiSolverInterfaceMpsUnitTest(vecSi,netlibDir), {}, "osi", "netlib unittest"); |
| 363 | } |
| 364 | |
| 365 | unsigned int i; |
| 366 | for (i=0; i<vecSi.size(); i++) |
| 367 | delete vecSi[i]; |
| 368 | } |
| 369 | else { |
| 370 | testingMessage( "***Skipped Testing of OsiSolverInterface on Netlib problems***\n" ); |
| 371 | testingMessage( "***use -testOsiSolverInterface to run them.***\n" ); |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | We're done. Report on the results. |
| 376 | */ |
| 377 | std::cout.flush(); |
| 378 | outcomes.print(); |
| 379 | |
| 380 | int nerrors; |
| 381 | int nerrors_expected; |
| 382 | outcomes.getCountBySeverity(TestOutcome::ERROR, nerrors, nerrors_expected); |
| 383 | |
| 384 | if (nerrors > nerrors_expected) |
| 385 | std::cerr << "Tests completed with " << nerrors - nerrors_expected << " unexpected errors." << std::endl ; |
| 386 | else |
| 387 | std::cerr << "All tests completed successfully\n"; |
| 388 | |
| 389 | return nerrors - nerrors_expected; |
| 390 | } |