| 1 |
// Bare bones example of using COIN-OR OSI |
| 2 |
|
| 3 |
#include <iostream> |
| 4 |
#include OSIXXXhpp |
| 5 |
|
| 6 |
int |
| 7 |
main(void) |
| 8 |
{ |
| 9 |
// Create a problem pointer. We use the base class here. |
| 10 |
OsiSolverInterface *si; |
| 11 |
|
| 12 |
// When we instantiate the object, we need a specific derived class. |
| 13 |
si = new OSIXXX; |
| 14 |
|
| 15 |
// Read in an mps file. This one's from the MIPLIB library. |
| 16 |
#if defined(SAMPLEDIR) |
| 17 |
si->readMps(SAMPLEDIR "/p0033"); |
| 18 |
#else |
| 19 |
fprintf(stderr, "Do not know where to find sample MPS files.\n"); |
| 20 |
exit(1); |
| 21 |
#endif |
| 22 |
|
| 23 |
// Solve the (relaxation of the) problem |
| 24 |
si->initialSolve(); |
| 25 |
|
| 26 |
// Check the solution |
| 27 |
if ( si->isProvenOptimal() ) { |
| 28 |
std::cout << "Found optimal solution!" << std::endl; |
| 29 |
std::cout << "Objective value is " << si->getObjValue() << std::endl; |
| 30 |
|
| 31 |
int n = si->getNumCols(); |
| 32 |
const double* solution = si->getColSolution(); |
| 33 |
|
| 34 |
// We can then print the solution or could examine it. |
| 35 |
for( int i = 0; i < n && i < 5; ++i ) |
| 36 |
std::cout << si->getColName(i) << " = " << solution[i] << std::endl; |
| 37 |
if( 5 < n ) |
| 38 |
std::cout << "..." << std::endl; |
| 39 |
} else { |
| 40 |
std::cout << "Didn't find optimal solution." << std::endl; |
| 41 |
// Could then check other status functions. |
| 42 |
} |
| 43 |
|
| 44 |
return 0; |
| 45 |
} |