Changeset 2469 for trunk/Cbc/examples/nway.cpp
 Timestamp:
 Jan 6, 2019 6:17:46 PM (10 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Cbc/examples/nway.cpp
r1898 r2469 6 6 #include <cassert> 7 7 #include <iomanip> 8 9 8 10 9 #include "CoinPragma.hpp" … … 17 16 #include "CoinTime.hpp" 18 17 19 20 18 /************************************************************************ 21 19 … … 25 23 26 24 *************************************************************************/ 27 int main 25 int main(int argc, const char *argv[]) 28 26 { 29 27 30 28 // Define your favorite OsiSolver 31 29 32 30 OsiClpSolverInterface solver1; 33 31 34 32 // Read in model using argv[1] 35 33 // and assert that it is a clean model … … 43 41 } 44 42 #endif 45 if (argc >=2) mpsFileName = argv[1];46 int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");47 i f( numMpsReadErrors != 0 )48 {49 50 43 if (argc >= 2) 44 mpsFileName = argv[1]; 45 int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), ""); 46 if (numMpsReadErrors != 0) { 47 printf("%d errors reading MPS file\n", numMpsReadErrors); 48 return numMpsReadErrors; 51 49 } 52 50 53 51 int iRow, iColumn; 54 52 int numberColumns = solver1.getNumCols(); 55 53 int numberRows = solver1.getNumRows(); 56 54 // get row copy 57 const CoinPackedMatrix * 58 const double * 59 const int * 60 const CoinBigIndex * 61 const int * 62 const double * 63 const double * 64 const double * 65 55 const CoinPackedMatrix *matrix = solver1.getMatrixByRow(); 56 const double *element = matrix>getElements(); 57 const int *column = matrix>getIndices(); 58 const CoinBigIndex *rowStart = matrix>getVectorStarts(); 59 const int *rowLength = matrix>getVectorLengths(); 60 const double *rowLower = solver1.getRowLower(); 61 const double *rowUpper = solver1.getRowUpper(); 62 const double *columnLower = solver1.getColLower(); 63 66 64 // Look for possible SOS 67 int numberSOS =0;68 int * 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++) {65 int numberSOS = 0; 66 int *mark = new int[numberColumns]; 67 CoinFillN(mark, numberColumns, 1); 68 for (iRow = 0; iRow < numberRows; iRow++) { 69 if (rowLower[iRow] == 1.0 && rowUpper[iRow] == 1.0) { 70 bool goodRow = true; 71 for (int j = rowStart[iRow]; j < rowStart[iRow] + rowLength[iRow]; j++) { 74 72 int iColumn = column[j]; 75 if (element[j] !=1.0!solver1.isInteger(iColumn)mark[iColumn]>=0columnLower[iColumn]) {76 goodRow =false;73 if (element[j] != 1.0  !solver1.isInteger(iColumn)  mark[iColumn] >= 0  columnLower[iColumn]) { 74 goodRow = false; 77 75 break; 78 76 } … … 80 78 if (goodRow) { 81 79 // mark all 82 for (int j =rowStart[iRow];j<rowStart[iRow]+rowLength[iRow];j++) {80 for (int j = rowStart[iRow]; j < rowStart[iRow] + rowLength[iRow]; j++) { 83 81 int iColumn = column[j]; 84 mark[iColumn] =numberSOS;82 mark[iColumn] = numberSOS; 85 83 } 86 84 numberSOS++; … … 88 86 } 89 87 } 90 std::cout <<numberSOS<<" SOS"<<std::endl;88 std::cout << numberSOS << " SOS" << std::endl; 91 89 if (!numberSOS) 92 90 return 0; 93 91 CbcModel model(solver1); 94 92 // Do sets and priorities 95 CbcObject ** objects = new CbcObject *[numberSOS];93 CbcObject **objects = new CbcObject *[numberSOS]; 96 94 int numberIntegers = model.numberIntegers(); 97 95 /* model may not have created objects 98 96 If none then create 99 97 */ 100 if (!numberIntegers !model.numberObjects()) {98 if (!numberIntegers  !model.numberObjects()) { 101 99 model.findIntegers(true); 102 100 numberIntegers = model.numberIntegers(); 103 101 } 104 int * 102 int *priority = new int[numberSOS]; 105 103 // Set SOS priorities high 106 CoinFillN(priority, numberSOS,1);104 CoinFillN(priority, numberSOS, 1); 107 105 // Set up SOS 108 int * 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;106 int *which = new int[numberColumns]; 107 for (int iSOS = 0; iSOS < numberSOS; iSOS++) { 108 int n = 0; 109 for (iColumn = 0; iColumn < numberColumns; iColumn++) { 110 if (mark[iColumn] == iSOS) 111 which[n++] = iColumn; 114 112 } 115 113 // NULL uses 0,1,2 .. as weights 116 objects[iSOS] = new CbcNWay(&model,n,which,iSOS);114 objects[iSOS] = new CbcNWay(&model, n, which, iSOS); 117 115 } 118 delete 119 delete 120 model.addObjects(numberSOS, objects);121 for (iColumn =0;iColumn<numberSOS;iColumn++)116 delete[] mark; 117 delete[] which; 118 model.addObjects(numberSOS, objects); 119 for (iColumn = 0; iColumn < numberSOS; iColumn++) 122 120 delete objects[iColumn]; 123 delete 124 model.passInPriorities(priority, true);125 delete 121 delete[] objects; 122 model.passInPriorities(priority, true); 123 delete[] priority; 126 124 127 125 // If time is given then stop after that number of minutes 128 if (argc >2) {126 if (argc > 2) { 129 127 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);128 std::cout << "Stopping after " << minutes << " minutes" << std::endl; 129 assert(minutes >= 0); 130 model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0 * minutes); 133 131 } 134 132 // Switch off most output 135 model.solver()>setHintParam(OsiDoReducePrint, true,OsiHintTry);136 if (model.getNumCols() <3000) {133 model.solver()>setHintParam(OsiDoReducePrint, true, OsiHintTry); 134 if (model.getNumCols() < 3000) { 137 135 model.messageHandler()>setLogLevel(1); 138 136 //model.solver()>messageHandler()>setLogLevel(0); … … 142 140 } 143 141 //model.messageHandler()>setLogLevel(1); 144 142 145 143 double time1 = CoinCpuTime(); 146 144 147 145 // Do complete search 148 146 149 147 model.branchAndBound(); 150 148 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;149 std::cout << mpsFileName << " took " << CoinCpuTime()  time1 << " seconds, " 150 << model.getNodeCount() << " nodes with objective " 151 << model.getObjValue() 152 << (!model.status() ? " Finished" : " Not finished") 153 << std::endl; 156 154 157 155 // Print solution  we can't get names from Osi! 158 156 159 if (model.getMinimizationObjValue() <1.0e50) {157 if (model.getMinimizationObjValue() < 1.0e50) { 160 158 int numberColumns = model.solver()>getNumCols(); 161 162 const double * 163 159 160 const double *solution = model.solver()>getColSolution(); 161 164 162 int iColumn; 165 std::cout <<std::setiosflags(std::ios::fixedstd::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.0e7&&model.solver()>isInteger(iColumn))171 std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;163 std::cout << std::setiosflags(std::ios::fixed  std::ios::showpoint) << std::setw(14); 164 165 std::cout << "" << std::endl; 166 for (iColumn = 0; iColumn < numberColumns; iColumn++) { 167 double value = solution[iColumn]; 168 if (fabs(value) > 1.0e7 && model.solver()>isInteger(iColumn)) 169 std::cout << std::setw(6) << iColumn << " " << value << std::endl; 172 170 } 173 std::cout <<""<<std::endl;174 175 std::cout <<std::resetiosflags(std::ios::fixedstd::ios::showpointstd::ios::scientific);171 std::cout << "" << std::endl; 172 173 std::cout << std::resetiosflags(std::ios::fixed  std::ios::showpoint  std::ios::scientific); 176 174 } 177 175 return 0; 178 } 176 }
Note: See TracChangeset
for help on using the changeset viewer.