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