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

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Cbc/examples/qmip.cpp
r1898 r2469 6 6 #include <cassert> 7 7 #include <iomanip> 8 9 8 10 9 #include "CoinPragma.hpp" … … 27 26 #include "CbcHeuristic.hpp" 28 27 29 30 28 // Time 31 29 #include "CoinTime.hpp" 32 33 30 34 31 /************************************************************************ … … 56 53 ************************************************************************/ 57 54 58 int main 55 int main(int argc, const char *argv[]) 59 56 { 60 57 61 58 // Define a Solver which inherits from OsiClpsolverInterface > OsiSolverInterface 62 59 63 60 ClpQuadInterface solver1; 64 61 65 62 // Read in model using argv[1] 66 63 if (argc <= 1) { 67 68 64 printf("using %s <modelfile>\n", argv[0]); 65 return 1; 69 66 } 70 67 // must use clp to get a quadratic model 71 ClpSimplex * 68 ClpSimplex *clp = solver1.getModelPtr(); 72 69 int numMpsReadErrors = clp>readMps(argv[1]); 73 70 // and assert that it is a clean model 74 if( numMpsReadErrors != 0 ) 75 { 76 printf("%d errors reading MPS file\n", numMpsReadErrors); 77 return numMpsReadErrors; 78 } 79 80 // This clones solver 71 if (numMpsReadErrors != 0) { 72 printf("%d errors reading MPS file\n", numMpsReadErrors); 73 return numMpsReadErrors; 74 } 75 76 // This clones solver 81 77 CbcModel model(solver1); 82 78 // But now model doesn't know about integers! 83 const char * 79 const char *integerInfo = clp>integerInformation(); 84 80 int numberColumns = clp>numberColumns(); 85 81 // and point to solver 86 OsiSolverInterface * 87 for (int iColumn =0;iColumn<numberColumns;iColumn++) {88 if (integerInfo[iColumn]) 82 OsiSolverInterface *solver2 = model.solver(); 83 for (int iColumn = 0; iColumn < numberColumns; iColumn++) { 84 if (integerInfo[iColumn]) 89 85 solver2>setInteger(iColumn); 90 86 } … … 92 88 // Within branch and cut it is better (at present) to switch off all objectives 93 89 94 ClpQuadInterface * osiclp = dynamic_cast< ClpQuadInterface*>(solver2);95 assert 90 ClpQuadInterface *osiclp = dynamic_cast< ClpQuadInterface * >(solver2); 91 assert(osiclp); 96 92 // Set fake objective so Cbc not confused 97 93 osiclp>initialize(); 98 solver2>setHintParam(OsiDoReducePrint, true,OsiHintTry);94 solver2>setHintParam(OsiDoReducePrint, true, OsiHintTry); 99 95 100 96 // Set up some cut generators and defaults … … 111 107 // Add in generators 112 108 // Only some generators work (and even then try without first) 113 model.addCutGenerator(&generator1, 1,"Probing");109 model.addCutGenerator(&generator1, 1, "Probing"); 114 110 // Allow rounding heuristic 115 111 … … 117 113 // do not add yet as don't know how to deal with quadratic objective 118 114 //model.addHeuristic(&heuristic1); 119 120 115 121 116 // Redundant definition of default branching (as Default == User) … … 129 124 model.setNodeComparison(compare); 130 125 131 132 126 // Do initial solve to continuous 133 127 model.initialSolve(); … … 135 129 // Could tune more 136 130 model.setMinimumDrop(CoinMin(1.0, 137 fabs(model.getMinimizationObjValue())*1.0e3+1.0e4));131 fabs(model.getMinimizationObjValue()) * 1.0e3 + 1.0e4)); 138 132 139 133 model.setMaximumCutPassesAtRoot(0); … … 142 136 // Switch off strong branching if wanted 143 137 //model.setNumberStrong(5); 144 145 model.solver()>setIntParam(OsiMaxNumIterationHotStart, 10000);138 139 model.solver()>setIntParam(OsiMaxNumIterationHotStart, 10000); 146 140 147 141 // If time is given then stop after that number of minutes 148 if (argc >2) {142 if (argc > 2) { 149 143 int minutes = atoi(argv[2]); 150 std::cout <<"Stopping after "<<minutes<<" minutes"<<std::endl;151 assert (minutes>=0);152 model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0*minutes);144 std::cout << "Stopping after " << minutes << " minutes" << std::endl; 145 assert(minutes >= 0); 146 model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0 * minutes); 153 147 } 154 148 // Switch off most output 155 if (model.getNumCols() <3000) {149 if (model.getNumCols() < 3000) { 156 150 model.messageHandler()>setLogLevel(1); 157 151 //model.solver()>messageHandler()>setLogLevel(0); … … 161 155 } 162 156 model.setPrintFrequency(50); 163 157 164 158 double time1 = CoinCpuTime(); 165 159 166 160 // Do complete search 167 161 168 162 model.branchAndBound(); 169 163 170 std::cout <<argv[1]<<" took "<<CoinCpuTime()time1<<" seconds, "171 <<model.getNodeCount()<<" nodes with objective "172 <<model.getObjValue()173 <<(!model.status() ? " Finished" : " Not finished")174 <<std::endl;164 std::cout << argv[1] << " took " << CoinCpuTime()  time1 << " seconds, " 165 << model.getNodeCount() << " nodes with objective " 166 << model.getObjValue() 167 << (!model.status() ? " Finished" : " Not finished") 168 << std::endl; 175 169 176 170 // Print more statistics 177 std::cout <<"Cuts at root node changed objective from "<<model.getContinuousObjective()178 <<" to "<<model.rootObjectiveAfterCuts()<<std::endl;171 std::cout << "Cuts at root node changed objective from " << model.getContinuousObjective() 172 << " to " << model.rootObjectiveAfterCuts() << std::endl; 179 173 180 174 int numberGenerators = model.numberCutGenerators(); 181 for (int iGenerator =0;iGenerator<numberGenerators;iGenerator++) {182 CbcCutGenerator * 183 std::cout <<generator>cutGeneratorName()<<" was tried "184 <<generator>numberTimesEntered()<<" times and created "185 <<generator>numberCutsInTotal()<<" cuts of which "186 <<generator>numberCutsActive()<<" were active after adding rounds of cuts"187 <<std::endl;175 for (int iGenerator = 0; iGenerator < numberGenerators; iGenerator++) { 176 CbcCutGenerator *generator = model.cutGenerator(iGenerator); 177 std::cout << generator>cutGeneratorName() << " was tried " 178 << generator>numberTimesEntered() << " times and created " 179 << generator>numberCutsInTotal() << " cuts of which " 180 << generator>numberCutsActive() << " were active after adding rounds of cuts" 181 << std::endl; 188 182 } 189 183 // Print solution if finished  we can't get names from Osi! 190 184 191 if (!model.status() &&model.getMinimizationObjValue()<1.0e50) {185 if (!model.status() && model.getMinimizationObjValue() < 1.0e50) { 192 186 int numberColumns = model.solver()>getNumCols(); 193 194 const double * 195 187 188 const double *solution = model.solver()>getColSolution(); 189 196 190 int iColumn; 197 std::cout <<std::setiosflags(std::ios::fixedstd::ios::showpoint)<<std::setw(14);198 199 std::cout <<""<<std::endl;200 for (iColumn =0;iColumn<numberColumns;iColumn++) {201 double value =solution[iColumn];202 if (fabs(value) >1.0e7&&model.solver()>isInteger(iColumn))203 std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;191 std::cout << std::setiosflags(std::ios::fixed  std::ios::showpoint) << std::setw(14); 192 193 std::cout << "" << std::endl; 194 for (iColumn = 0; iColumn < numberColumns; iColumn++) { 195 double value = solution[iColumn]; 196 if (fabs(value) > 1.0e7 && model.solver()>isInteger(iColumn)) 197 std::cout << std::setw(6) << iColumn << " " << value << std::endl; 204 198 } 205 std::cout <<""<<std::endl;206 207 std::cout <<std::resetiosflags(std::ios::fixedstd::ios::showpointstd::ios::scientific);199 std::cout << "" << std::endl; 200 201 std::cout << std::resetiosflags(std::ios::fixed  std::ios::showpoint  std::ios::scientific); 208 202 } 209 203 return 0; 210 } 204 }
Note: See TracChangeset
for help on using the changeset viewer.