| 1 | // Example of using COIN-OR OSI |
| 2 | // Demonstrates some problem and solution query methods |
| 3 | // Also demonstrates some parameter setting |
| 4 | |
| 5 | #include <iostream> |
| 6 | #include OSIXXXhpp |
| 7 | |
| 8 | int |
| 9 | main(void) |
| 10 | { |
| 11 | // Create a problem pointer. We use the base class here. |
| 12 | OsiSolverInterface *si; |
| 13 | |
| 14 | // When we instantiate the object, we need a specific derived class. |
| 15 | si = new OSIXXX; |
| 16 | |
| 17 | // Read in an mps file. This one's from the MIPLIB library. |
| 18 | #if defined(SAMPLEDIR) |
| 19 | si->readMps(SAMPLEDIR "/p0033"); |
| 20 | #else |
| 21 | fprintf(stderr, "Do not know where to find sample MPS files.\n"); |
| 22 | exit(1); |
| 23 | #endif |
| 24 | |
| 25 | // Display some information about the instance |
| 26 | int nrows = si->getNumRows(); |
| 27 | int ncols = si->getNumCols(); |
| 28 | int nelem = si->getNumElements(); |
| 29 | std::cout << "This problem has " << nrows << " rows, " |
| 30 | << ncols << " columns, and " << nelem << " nonzeros." |
| 31 | << std::endl; |
| 32 | |
| 33 | double const * upper_bounds = si->getColUpper(); |
| 34 | std::cout << "The upper bound on the first column is " << upper_bounds[0] |
| 35 | << std::endl; |
| 36 | // All information about the instance is available with similar methods |
| 37 | |
| 38 | |
| 39 | // Before solving, indicate some parameters |
| 40 | si->setIntParam( OsiMaxNumIteration, 10); |
| 41 | si->setDblParam( OsiPrimalTolerance, 0.001 ); |
| 42 | |
| 43 | // Can also read parameters |
| 44 | std::string solver; |
| 45 | si->getStrParam( OsiSolverName, solver ); |
| 46 | std::cout << "About to solve with: " << solver << std::endl; |
| 47 | |
| 48 | // Solve the (relaxation of the) problem |
| 49 | si->initialSolve(); |
| 50 | |
| 51 | // Check the solution |
| 52 | if ( si->isProvenOptimal() ) { |
| 53 | std::cout << "Found optimal solution!" << std::endl; |
| 54 | std::cout << "Objective value is " << si->getObjValue() << std::endl; |
| 55 | |
| 56 | // Examine solution |
| 57 | int n = si->getNumCols(); |
| 58 | const double *solution; |
| 59 | solution = si->getColSolution(); |
| 60 | |
| 61 | std::cout << "Solution: "; |
| 62 | for (int i = 0; i < n; i++) |
| 63 | std::cout << solution[i] << " "; |
| 64 | std::cout << std::endl; |
| 65 | |
| 66 | std::cout << "It took " << si->getIterationCount() << " iterations" |
| 67 | << " to solve." << std::endl; |
| 68 | } else { |
| 69 | std::cout << "Didn't find optimal solution." << std::endl; |
| 70 | |
| 71 | // Check other status functions. What happened? |
| 72 | if (si->isProvenPrimalInfeasible()) |
| 73 | std::cout << "Problem is proven to be infeasible." << std::endl; |
| 74 | if (si->isProvenDualInfeasible()) |
| 75 | std::cout << "Problem is proven dual infeasible." << std::endl; |
| 76 | if (si->isIterationLimitReached()) |
| 77 | std::cout << "Reached iteration limit." << std::endl; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |