| 1 | // Example of using COIN-OR OSI, building the instance internally |
| 2 | // with sparse matrix object |
| 3 | |
| 4 | #include <iostream> |
| 5 | #include OSIXXXhpp |
| 6 | #include "CoinPackedMatrix.hpp" |
| 7 | #include "CoinPackedVector.hpp" |
| 8 | |
| 9 | int |
| 10 | main(void) |
| 11 | { |
| 12 | // Create a problem pointer. We use the base class here. |
| 13 | OsiSolverInterface *si; |
| 14 | |
| 15 | // When we instantiate the object, we need a specific derived class. |
| 16 | si = new OSIXXX; |
| 17 | |
| 18 | // Build our own instance from scratch |
| 19 | |
| 20 | /* |
| 21 | * This section adapted from Matt Galati's example |
| 22 | * on the COIN-OR Tutorial website. |
| 23 | * |
| 24 | * Problem from Bertsimas, Tsitsiklis page 21 |
| 25 | * |
| 26 | * optimal solution: x* = (1,1) |
| 27 | * |
| 28 | * minimize -1 x0 - 1 x1 |
| 29 | * s.t 1 x0 + 2 x1 <= 3 |
| 30 | * 2 x0 + 1 x1 <= 3 |
| 31 | * x0 >= 0 |
| 32 | * x1 >= 0 |
| 33 | */ |
| 34 | |
| 35 | int n_cols = 2; |
| 36 | double *objective = new double[n_cols];//the objective coefficients |
| 37 | double *col_lb = new double[n_cols];//the column lower bounds |
| 38 | double *col_ub = new double[n_cols];//the column upper bounds |
| 39 | |
| 40 | //Define the objective coefficients. |
| 41 | //minimize -1 x0 - 1 x1 |
| 42 | objective[0] = -1.0; |
| 43 | objective[1] = -1.0; |
| 44 | |
| 45 | //Define the variable lower/upper bounds. |
| 46 | // x0 >= 0 => 0 <= x0 <= infinity |
| 47 | // x1 >= 0 => 0 <= x1 <= infinity |
| 48 | col_lb[0] = 0.0; |
| 49 | col_lb[1] = 0.0; |
| 50 | col_ub[0] = si->getInfinity(); |
| 51 | col_ub[1] = si->getInfinity(); |
| 52 | |
| 53 | int n_rows = 2; |
| 54 | double *row_lb = new double[n_rows]; //the row lower bounds |
| 55 | double *row_ub = new double[n_rows]; //the row upper bounds |
| 56 | |
| 57 | //Define the constraint matrix. |
| 58 | CoinPackedMatrix *matrix = new CoinPackedMatrix(false,0,0); |
| 59 | matrix->setDimensions(0, n_cols); |
| 60 | |
| 61 | //1 x0 + 2 x1 <= 3 => -infinity <= 1 x0 + 2 x2 <= 3 |
| 62 | CoinPackedVector row1; |
| 63 | row1.insert(0, 1.0); |
| 64 | row1.insert(1, 2.0); |
| 65 | row_lb[0] = -1.0 * si->getInfinity(); |
| 66 | row_ub[0] = 3.0; |
| 67 | matrix->appendRow(row1); |
| 68 | |
| 69 | //2 x0 + 1 x1 <= 3 => -infinity <= 2 x0 + 1 x1 <= 3 |
| 70 | CoinPackedVector row2; |
| 71 | row2.insert(0, 2.0); |
| 72 | row2.insert(1, 1.0); |
| 73 | row_lb[1] = -1.0 * si->getInfinity(); |
| 74 | row_ub[1] = 3.0; |
| 75 | matrix->appendRow(row2); |
| 76 | |
| 77 | //load the problem to OSI |
| 78 | si->loadProblem(*matrix, col_lb, col_ub, objective, row_lb, row_ub); |
| 79 | |
| 80 | //write the MPS file to a file called example.mps |
| 81 | si->writeMps("example"); |
| 82 | |
| 83 | |
| 84 | |
| 85 | // Solve the (relaxation of the) problem |
| 86 | si->initialSolve(); |
| 87 | |
| 88 | // Check the solution |
| 89 | if ( si->isProvenOptimal() ) { |
| 90 | std::cout << "Found optimal solution!" << std::endl; |
| 91 | std::cout << "Objective value is " << si->getObjValue() << std::endl; |
| 92 | |
| 93 | int n = si->getNumCols(); |
| 94 | const double* solution = si->getColSolution(); |
| 95 | |
| 96 | // We can then print the solution or could examine it. |
| 97 | for( int i = 0; i < n; ++i ) |
| 98 | std::cout << si->getColName(i) << " = " << solution[i] << std::endl; |
| 99 | |
| 100 | } else { |
| 101 | std::cout << "Didn't find optimal solution." << std::endl; |
| 102 | // Could then check other status functions. |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |