[2575] | 1 | // $Id: presolveBefore.cpp |
---|
| 2 | // Copyright (C) 2002, International Business Machines |
---|
| 3 | // Corporation and others. All Rights Reserved. |
---|
| 4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
---|
| 5 | |
---|
| 6 | #include <cassert> |
---|
| 7 | |
---|
| 8 | #include "CbcConfig.h" |
---|
| 9 | #include "CoinPragma.hpp" |
---|
| 10 | |
---|
| 11 | #include "CbcModel.hpp" |
---|
| 12 | |
---|
| 13 | #include "OsiClpSolverInterface.hpp" |
---|
| 14 | #include "ClpPresolve.hpp" |
---|
| 15 | #include "CoinTime.hpp" |
---|
| 16 | |
---|
| 17 | //############################################################################# |
---|
| 18 | |
---|
| 19 | #ifdef NDEBUG |
---|
| 20 | #undef NDEBUG |
---|
| 21 | #endif |
---|
| 22 | |
---|
| 23 | /************************************************************************ |
---|
| 24 | |
---|
| 25 | This main program reads in an integer model from an mps or lp file. |
---|
| 26 | |
---|
| 27 | It presolves problem (to file), passes that to standalone solver and then |
---|
| 28 | postsolves. |
---|
| 29 | |
---|
| 30 | ************************************************************************/ |
---|
| 31 | |
---|
| 32 | int main(int argc, const char *argv[]) |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | double time0 = CoinCpuTime(), time1, time2; |
---|
| 36 | // Define your favorite OsiSolver |
---|
| 37 | |
---|
| 38 | ClpSimplex * simplex = new ClpSimplex();; |
---|
| 39 | // Read in model using argv[1] |
---|
| 40 | // and assert that it is a clean model |
---|
| 41 | std::string mpsFileName; |
---|
| 42 | if (argc < 2) { |
---|
| 43 | fprintf(stderr, "Do not know where to find sample MPS files.\n"); |
---|
| 44 | exit(1); |
---|
| 45 | } |
---|
| 46 | if (argc >= 2) |
---|
| 47 | mpsFileName = argv[1]; |
---|
| 48 | int numMpsReadErrors; |
---|
| 49 | if (strstr(mpsFileName.c_str(), ".mps")) |
---|
| 50 | numMpsReadErrors = simplex->readMps(mpsFileName.c_str(), ""); |
---|
| 51 | else |
---|
| 52 | numMpsReadErrors = simplex->readLp(mpsFileName.c_str()); |
---|
| 53 | if (numMpsReadErrors != 0) { |
---|
| 54 | printf("%d errors reading MPS file\n", numMpsReadErrors); |
---|
| 55 | return numMpsReadErrors; |
---|
| 56 | } |
---|
| 57 | ClpPresolve pinfo; |
---|
| 58 | // dont allow some things |
---|
| 59 | pinfo.setDoDupcol(false); |
---|
| 60 | ClpSimplex *simplexA = pinfo.presolvedModel(*simplex, 1.0e-8); |
---|
| 61 | if (!simplexA) { |
---|
| 62 | std::cout << "Problem is not feasible" << std::endl; |
---|
| 63 | exit(77); |
---|
| 64 | } |
---|
| 65 | //#define SAVE_MEMORY |
---|
| 66 | #ifdef SAVE_MEMORY |
---|
| 67 | delete simplex; |
---|
| 68 | #endif |
---|
| 69 | OsiClpSolverInterface solver1(simplexA); |
---|
| 70 | // Do initial solve to continuous |
---|
| 71 | solver1.initialSolve(); |
---|
| 72 | time1 = CoinCpuTime(); |
---|
| 73 | std::cout << "Initialization " << time1 - time0 << " seconds" << std::endl; |
---|
| 74 | CbcModel model(solver1); |
---|
| 75 | // initialize |
---|
| 76 | CbcMain0(model); |
---|
| 77 | /* Now go into code for standalone solver |
---|
| 78 | Could copy arguments and add -quit at end to be safe |
---|
| 79 | but this will do |
---|
| 80 | */ |
---|
| 81 | if (argc > 2) { |
---|
| 82 | CbcMain1(argc - 1, argv + 1, model); |
---|
| 83 | } else { |
---|
| 84 | const char *argv2[] = { "presolveBefore", "-solve", "-quit" }; |
---|
| 85 | CbcMain1(3, argv2, model); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if (!simplexA->problemStatus()) { |
---|
| 89 | std::cout << "Objective value " << simplexA->objectiveValue() << std::endl; |
---|
| 90 | } else { |
---|
| 91 | std::cout << "Infeasible!" << std::endl; |
---|
| 92 | } |
---|
| 93 | OsiClpSolverInterface *clpSolver |
---|
| 94 | = dynamic_cast< OsiClpSolverInterface * >(model.solver()); |
---|
| 95 | assert(clpSolver); |
---|
| 96 | ClpSimplex *simplex2 = clpSolver->getModelPtr(); |
---|
| 97 | time1 = CoinCpuTime(); |
---|
| 98 | |
---|
| 99 | #ifdef SAVE_MEMORY |
---|
| 100 | simplex = new ClpSimplex(); |
---|
| 101 | if (strstr(mpsFileName.c_str(), ".mps")) |
---|
| 102 | simplex->readMps(mpsFileName.c_str(), ""); |
---|
| 103 | else |
---|
| 104 | simplex->readLp(mpsFileName.c_str()); |
---|
| 105 | pinfo.setOriginalModel(simplex); |
---|
| 106 | #endif |
---|
| 107 | pinfo.postsolve(true); |
---|
| 108 | // Fix all integers (if there are any) |
---|
| 109 | const char *info2 = simplex2->integerInformation(); |
---|
| 110 | if (info2) { |
---|
| 111 | const int *original = pinfo.originalColumns(); |
---|
| 112 | double *lower2 = simplex2->columnLower(); |
---|
| 113 | double *upper2 = simplex2->columnUpper(); |
---|
| 114 | double *lower = simplex->columnLower(); |
---|
| 115 | double *upper = simplex->columnUpper(); |
---|
| 116 | int i; |
---|
| 117 | for (i = 0; i < simplex2->numberColumns(); i++) { |
---|
| 118 | if (info2[i]) { |
---|
| 119 | int iSeq = original[i]; |
---|
| 120 | upper[iSeq] = upper2[i]; |
---|
| 121 | lower[iSeq] = lower2[i]; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | delete simplexA; |
---|
| 126 | simplex->initialSolve(); |
---|
| 127 | time2 = CoinCpuTime(); |
---|
| 128 | std::cout << "Cleanup took " << time2 - time1 << " seconds" << std::endl; |
---|
| 129 | std::cout << "Total time " << time2 - time0 << " seconds" << std::endl; |
---|
| 130 | delete simplex; |
---|
| 131 | return 0; |
---|
| 132 | } |
---|