[1574] | 1 | // $Id: nway.cpp 1574 2011-01-05 01:13:55Z lou $ |
---|
[215] | 2 | // Copyright (C) 2005, International Business Machines |
---|
| 3 | // Corporation and others. All Rights Reserved. |
---|
[1574] | 4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
---|
| 5 | |
---|
[215] | 6 | #if defined(_MSC_VER) |
---|
| 7 | // Turn off compiler warning about long names |
---|
| 8 | # pragma warning(disable:4786) |
---|
| 9 | #endif |
---|
| 10 | |
---|
| 11 | #include <cassert> |
---|
| 12 | #include <iomanip> |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | // For Branch and bound |
---|
| 16 | #include "CbcModel.hpp" |
---|
| 17 | #include "CbcBranchActual.hpp" |
---|
| 18 | #include "OsiClpSolverInterface.hpp" |
---|
| 19 | |
---|
| 20 | // Time |
---|
| 21 | #include "CoinTime.hpp" |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | /************************************************************************ |
---|
| 25 | |
---|
| 26 | This main program reads in an integer model from an mps file. |
---|
| 27 | |
---|
| 28 | It then tries to find SOS structure and solves using N-way variables |
---|
| 29 | |
---|
| 30 | *************************************************************************/ |
---|
| 31 | int main (int argc, const char *argv[]) |
---|
| 32 | { |
---|
| 33 | |
---|
| 34 | // Define your favorite OsiSolver |
---|
| 35 | |
---|
| 36 | OsiClpSolverInterface solver1; |
---|
| 37 | |
---|
| 38 | // Read in model using argv[1] |
---|
| 39 | // and assert that it is a clean model |
---|
[1464] | 40 | std::string mpsFileName; |
---|
[1468] | 41 | #if defined(MIPLIB3DIR) |
---|
[1464] | 42 | mpsFileName = MIPLIB3DIR "/10teams"; |
---|
| 43 | #else |
---|
| 44 | if (argc < 2) { |
---|
| 45 | fprintf(stderr, "Do not know where to find miplib3 MPS files.\n"); |
---|
| 46 | exit(1); |
---|
| 47 | } |
---|
| 48 | #endif |
---|
[215] | 49 | if (argc>=2) mpsFileName = argv[1]; |
---|
| 50 | int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),""); |
---|
| 51 | assert(numMpsReadErrors==0); |
---|
| 52 | |
---|
| 53 | int iRow, iColumn; |
---|
| 54 | int numberColumns = solver1.getNumCols(); |
---|
| 55 | int numberRows = solver1.getNumRows(); |
---|
| 56 | // get row copy |
---|
| 57 | const CoinPackedMatrix * matrix = solver1.getMatrixByRow(); |
---|
| 58 | const double * element = matrix->getElements(); |
---|
| 59 | const int * column = matrix->getIndices(); |
---|
| 60 | const CoinBigIndex * rowStart = matrix->getVectorStarts(); |
---|
| 61 | const int * rowLength = matrix->getVectorLengths(); |
---|
| 62 | const double * rowLower = solver1.getRowLower(); |
---|
| 63 | const double * rowUpper = solver1.getRowUpper(); |
---|
| 64 | const double * columnLower = solver1.getColLower(); |
---|
| 65 | |
---|
| 66 | // Look for possible SOS |
---|
| 67 | int numberSOS=0; |
---|
| 68 | int * mark = new int[numberColumns]; |
---|
| 69 | CoinFillN(mark,numberColumns,-1); |
---|
| 70 | for (iRow=0;iRow<numberRows;iRow++) { |
---|
| 71 | if (rowLower[iRow]==1.0&&rowUpper[iRow]==1.0) { |
---|
| 72 | bool goodRow=true; |
---|
| 73 | for (int j=rowStart[iRow];j<rowStart[iRow]+rowLength[iRow];j++) { |
---|
| 74 | int iColumn = column[j]; |
---|
| 75 | if (element[j]!=1.0||!solver1.isInteger(iColumn)||mark[iColumn]>=0||columnLower[iColumn]) { |
---|
| 76 | goodRow=false; |
---|
| 77 | break; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | if (goodRow) { |
---|
| 81 | // mark all |
---|
| 82 | for (int j=rowStart[iRow];j<rowStart[iRow]+rowLength[iRow];j++) { |
---|
| 83 | int iColumn = column[j]; |
---|
| 84 | mark[iColumn]=numberSOS; |
---|
| 85 | } |
---|
| 86 | numberSOS++; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | std::cout<<numberSOS<<" SOS"<<std::endl; |
---|
| 91 | if (!numberSOS) |
---|
| 92 | return 0; |
---|
| 93 | CbcModel model(solver1); |
---|
| 94 | // Do sets and priorities |
---|
| 95 | CbcObject ** objects = new CbcObject * [numberSOS]; |
---|
| 96 | int numberIntegers = model.numberIntegers(); |
---|
| 97 | /* model may not have created objects |
---|
| 98 | If none then create |
---|
| 99 | */ |
---|
| 100 | if (!numberIntegers||!model.numberObjects()) { |
---|
| 101 | model.findIntegers(true); |
---|
| 102 | numberIntegers = model.numberIntegers(); |
---|
| 103 | } |
---|
| 104 | int * priority = new int[numberSOS]; |
---|
| 105 | // Set SOS priorities high |
---|
| 106 | CoinFillN(priority,numberSOS,1); |
---|
| 107 | // Set up SOS |
---|
| 108 | int * which = new int[numberColumns]; |
---|
| 109 | for (int iSOS =0;iSOS<numberSOS;iSOS++) { |
---|
| 110 | int n=0; |
---|
| 111 | for (iColumn=0;iColumn<numberColumns;iColumn++) { |
---|
| 112 | if (mark[iColumn]==iSOS) |
---|
| 113 | which[n++]=iColumn; |
---|
| 114 | } |
---|
| 115 | // NULL uses 0,1,2 .. as weights |
---|
| 116 | objects[iSOS]= new CbcNWay(&model,n,which,iSOS); |
---|
| 117 | } |
---|
| 118 | delete [] mark; |
---|
| 119 | delete [] which; |
---|
| 120 | model.addObjects(numberSOS,objects); |
---|
| 121 | for (iColumn=0;iColumn<numberSOS;iColumn++) |
---|
| 122 | delete objects[iColumn]; |
---|
| 123 | delete [] objects; |
---|
| 124 | model.passInPriorities(priority,true); |
---|
| 125 | delete [] priority; |
---|
| 126 | |
---|
| 127 | // If time is given then stop after that number of minutes |
---|
| 128 | if (argc>2) { |
---|
| 129 | int minutes = atoi(argv[2]); |
---|
| 130 | std::cout<<"Stopping after "<<minutes<<" minutes"<<std::endl; |
---|
| 131 | assert (minutes>=0); |
---|
| 132 | model.setDblParam(CbcModel::CbcMaximumSeconds,60.0*minutes); |
---|
| 133 | } |
---|
| 134 | // Switch off most output |
---|
| 135 | model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry); |
---|
| 136 | if (model.getNumCols()<3000) { |
---|
| 137 | model.messageHandler()->setLogLevel(1); |
---|
| 138 | //model.solver()->messageHandler()->setLogLevel(0); |
---|
| 139 | } else { |
---|
| 140 | model.messageHandler()->setLogLevel(2); |
---|
| 141 | //model.solver()->messageHandler()->setLogLevel(1); |
---|
| 142 | } |
---|
| 143 | //model.messageHandler()->setLogLevel(1); |
---|
| 144 | |
---|
| 145 | double time1 = CoinCpuTime(); |
---|
| 146 | |
---|
| 147 | // Do complete search |
---|
| 148 | |
---|
| 149 | model.branchAndBound(); |
---|
| 150 | |
---|
| 151 | std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, " |
---|
| 152 | <<model.getNodeCount()<<" nodes with objective " |
---|
| 153 | <<model.getObjValue() |
---|
| 154 | <<(!model.status() ? " Finished" : " Not finished") |
---|
| 155 | <<std::endl; |
---|
| 156 | |
---|
| 157 | // Print solution - we can't get names from Osi! |
---|
| 158 | |
---|
| 159 | if (model.getMinimizationObjValue()<1.0e50) { |
---|
| 160 | int numberColumns = model.solver()->getNumCols(); |
---|
| 161 | |
---|
| 162 | const double * solution = model.solver()->getColSolution(); |
---|
| 163 | |
---|
| 164 | int iColumn; |
---|
| 165 | std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14); |
---|
| 166 | |
---|
| 167 | std::cout<<"--------------------------------------"<<std::endl; |
---|
| 168 | for (iColumn=0;iColumn<numberColumns;iColumn++) { |
---|
| 169 | double value=solution[iColumn]; |
---|
| 170 | if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) |
---|
| 171 | std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl; |
---|
| 172 | } |
---|
| 173 | std::cout<<"--------------------------------------"<<std::endl; |
---|
| 174 | |
---|
| 175 | std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific); |
---|
| 176 | } |
---|
| 177 | return 0; |
---|
| 178 | } |
---|