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

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Cbc/examples/sample2.cpp
r1898 r2469 39 39 //############################################################################# 40 40 41 42 41 /************************************************************************ 43 42 … … 63 62 #define PREPROCESS 2 64 63 65 int main 64 int main(int argc, const char *argv[]) 66 65 { 67 66 68 67 // Define your favorite OsiSolver 69 68 70 69 OsiClpSolverInterface solver1; 71 70 72 71 // Read in model using argv[1] 73 72 // and assert that it is a clean model 74 std::string dirsep(1, CoinFindDirSeparator());73 std::string dirsep(1, CoinFindDirSeparator()); 75 74 std::string mpsFileName; 76 # 77 mpsFileName = SAMPLEDIR 78 mpsFileName += dirsep +"p0033.mps";79 # 75 #if defined(SAMPLEDIR) 76 mpsFileName = SAMPLEDIR; 77 mpsFileName += dirsep + "p0033.mps"; 78 #else 80 79 if (argc < 2) { 81 80 fprintf(stderr, "Do not know where to find sample MPS files.\n"); 82 81 exit(1); 83 82 } 84 # 85 if (argc >=2) mpsFileName = argv[1];86 int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");87 i f( numMpsReadErrors != 0 )88 {89 90 83 #endif 84 if (argc >= 2) 85 mpsFileName = argv[1]; 86 int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), ""); 87 if (numMpsReadErrors != 0) { 88 printf("%d errors reading MPS file\n", numMpsReadErrors); 89 return numMpsReadErrors; 91 90 } 92 91 double time1 = CoinCpuTime(); … … 97 96 if 2 parameters and numeric taken as time 98 97 */ 99 bool preProcess =false;100 double minutes =1.0;101 int nGoodParam =0;102 for (int iParam =2; iParam<argc;iParam++) {103 if (!strcmp(argv[iParam], "preprocess")) {104 preProcess =true;98 bool preProcess = false; 99 double minutes = 1.0; 100 int nGoodParam = 0; 101 for (int iParam = 2; iParam < argc; iParam++) { 102 if (!strcmp(argv[iParam], "preprocess")) { 103 preProcess = true; 105 104 nGoodParam++; 106 } else if (!strcmp(argv[iParam], "time")) {107 if (iParam +1<argc&&isdigit(argv[iParam+1][0])) {108 minutes =atof(argv[iParam+1]);109 if (minutes >=0.0) {110 nGoodParam +=2;105 } else if (!strcmp(argv[iParam], "time")) { 106 if (iParam + 1 < argc && isdigit(argv[iParam + 1][0])) { 107 minutes = atof(argv[iParam + 1]); 108 if (minutes >= 0.0) { 109 nGoodParam += 2; 111 110 iParam++; // skip time 112 111 } … … 114 113 } 115 114 } 116 if (nGoodParam ==0&&argc==3&&isdigit(argv[2][0])) {115 if (nGoodParam == 0 && argc == 3 && isdigit(argv[2][0])) { 117 116 // If time is given then stop after that number of minutes 118 117 minutes = atof(argv[2]); 119 if (minutes >=0.0)120 nGoodParam =1;121 } 122 if (nGoodParam !=argc2&&argc>=2) {118 if (minutes >= 0.0) 119 nGoodParam = 1; 120 } 121 if (nGoodParam != argc  2 && argc >= 2) { 123 122 printf("Usage <file> [preprocess] [time <minutes>] or <file> <minutes>\n"); 124 123 exit(1); … … 126 125 solver1.initialSolve(); 127 126 // Reduce printout 128 solver1.setHintParam(OsiDoReducePrint, true,OsiHintTry);127 solver1.setHintParam(OsiDoReducePrint, true, OsiHintTry); 129 128 // See if we want preprocessing 130 OsiSolverInterface * solver2=&solver1;131 #if PREPROCESS ==1129 OsiSolverInterface *solver2 = &solver1; 130 #if PREPROCESS == 1 132 131 CglPreProcess process; 133 132 if (preProcess) { 134 133 /* Do not try and produce equality cliques and 135 134 do up to 5 passes */ 136 solver2 = process.preProcess(solver1, false,5);135 solver2 = process.preProcess(solver1, false, 5); 137 136 if (!solver2) { 138 137 printf("Preprocessing says infeasible\n"); … … 143 142 #endif 144 143 CbcModel model(*solver2); 145 model.solver()>setHintParam(OsiDoReducePrint, true,OsiHintTry);144 model.solver()>setHintParam(OsiDoReducePrint, true, OsiHintTry); 146 145 // Set up some cut generators and defaults 147 146 // Probing first as gets tight bounds on continuous … … 177 176 CglMixedIntegerRounding2 mixedGen; 178 177 CglFlowCover flowGen; 179 178 180 179 // Add in generators 181 180 // Experiment with 1 and 99 etc 182 model.addCutGenerator(&generator1, 1,"Probing");183 model.addCutGenerator(&generator2, 1,"Gomory");184 model.addCutGenerator(&generator3, 1,"Knapsack");181 model.addCutGenerator(&generator1, 1, "Probing"); 182 model.addCutGenerator(&generator2, 1, "Gomory"); 183 model.addCutGenerator(&generator3, 1, "Knapsack"); 185 184 // model.addCutGenerator(&generator4,1,"RedSplit"); 186 model.addCutGenerator(&generator5, 1,"Clique");187 model.addCutGenerator(&flowGen, 1,"FlowCover");188 model.addCutGenerator(&mixedGen, 1,"MixedIntegerRounding");185 model.addCutGenerator(&generator5, 1, "Clique"); 186 model.addCutGenerator(&flowGen, 1, "FlowCover"); 187 model.addCutGenerator(&mixedGen, 1, "MixedIntegerRounding"); 189 188 // Say we want timings 190 189 int numberGenerators = model.numberCutGenerators(); 191 190 int iGenerator; 192 for (iGenerator =0;iGenerator<numberGenerators;iGenerator++) {193 CbcCutGenerator * 191 for (iGenerator = 0; iGenerator < numberGenerators; iGenerator++) { 192 CbcCutGenerator *generator = model.cutGenerator(iGenerator); 194 193 generator>setTiming(true); 195 194 } 196 OsiClpSolverInterface * osiclp = dynamic_cast< OsiClpSolverInterface*>(model.solver());195 OsiClpSolverInterface *osiclp = dynamic_cast< OsiClpSolverInterface * >(model.solver()); 197 196 // go faster stripes 198 197 if (osiclp) { … … 200 199 // Used to be automatically set 201 200 osiclp>setSpecialOptions(128); 202 if (osiclp>getNumRows()<300&&osiclp>getNumCols()<500) {201 if (osiclp>getNumRows() < 300 && osiclp>getNumCols() < 500) { 203 202 //osiclp>setupForRepeatedUse(2,0); 204 osiclp>setupForRepeatedUse(0, 0);205 } 206 } 203 osiclp>setupForRepeatedUse(0, 0); 204 } 205 } 207 206 // Uncommenting this should switch off all CBC messages 208 207 // model.messagesPointer()>setDetailMessages(10,10000,NULL); … … 229 228 230 229 // Could tune more 231 double objValue = model.solver()>getObjSense() *model.solver()>getObjValue();232 double minimumDropA =CoinMin(1.0,fabs(objValue)*1.0e3+1.0e4);233 double minimumDrop = fabs(objValue)*1.0e4+1.0e4;234 printf("min drop %g (A %g)\n", minimumDrop,minimumDropA);230 double objValue = model.solver()>getObjSense() * model.solver()>getObjValue(); 231 double minimumDropA = CoinMin(1.0, fabs(objValue) * 1.0e3 + 1.0e4); 232 double minimumDrop = fabs(objValue) * 1.0e4 + 1.0e4; 233 printf("min drop %g (A %g)\n", minimumDrop, minimumDropA); 235 234 model.setMinimumDrop(minimumDrop); 236 235 237 if (model.getNumCols() <500)236 if (model.getNumCols() < 500) 238 237 model.setMaximumCutPassesAtRoot(100); // always do 100 if possible 239 else if (model.getNumCols() <5000)238 else if (model.getNumCols() < 5000) 240 239 model.setMaximumCutPassesAtRoot(100); // use minimum drop 241 240 else … … 247 246 // model.setNumberStrong(0); 248 247 // Do more strong branching if small 249 if (model.getNumCols() <5000)248 if (model.getNumCols() < 5000) 250 249 model.setNumberStrong(10); 251 250 model.setNumberStrong(20); … … 253 252 model.setNumberBeforeTrust(5); 254 253 255 model.solver()>setIntParam(OsiMaxNumIterationHotStart, 100);254 model.solver()>setIntParam(OsiMaxNumIterationHotStart, 100); 256 255 257 256 // If time is given then stop after that number of minutes 258 if (minutes >=0.0) {259 std::cout <<"Stopping after "<<minutes<<" minutes"<<std::endl;260 model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0*minutes);257 if (minutes >= 0.0) { 258 std::cout << "Stopping after " << minutes << " minutes" << std::endl; 259 model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0 * minutes); 261 260 } 262 261 // Switch off most output 263 if (model.getNumCols() <3000) {262 if (model.getNumCols() < 3000) { 264 263 model.messageHandler()>setLogLevel(1); 265 264 //model.solver()>messageHandler()>setLogLevel(0); … … 275 274 // Set up debugger by name (only if no preprocesing) 276 275 if (!preProcess) { 277 std::string problemName 278 model.solver()>getStrParam(OsiProbName, problemName);279 model.solver()>activateRowCutDebugger(problemName.c_str()) 280 } 281 #endif 282 #if PREPROCESS ==2276 std::string problemName; 277 model.solver()>getStrParam(OsiProbName, problemName); 278 model.solver()>activateRowCutDebugger(problemName.c_str()); 279 } 280 #endif 281 #if PREPROCESS == 2 283 282 // Default strategy will leave cut generators as they exist already 284 283 // so cutsOnlyAtRoot (1) ignored … … 286 285 // numberBeforeTrust (3) is 5 (default is 0) 287 286 // printLevel (4) defaults (0) 288 CbcStrategyDefault strategy(true, 5,5);287 CbcStrategyDefault strategy(true, 5, 5); 289 288 // Set up preprocessing to find sos if wanted 290 289 if (preProcess) … … 293 292 #endif 294 293 // Do complete search 295 294 296 295 model.branchAndBound(); 297 296 298 std::cout <<mpsFileName<<" took "<<CoinCpuTime()time1<<" seconds, "299 <<model.getNodeCount()<<" nodes with objective "300 <<model.getObjValue()301 <<(!model.status() ? " Finished" : " Not finished")302 <<std::endl;297 std::cout << mpsFileName << " took " << CoinCpuTime()  time1 << " seconds, " 298 << model.getNodeCount() << " nodes with objective " 299 << model.getObjValue() 300 << (!model.status() ? " Finished" : " Not finished") 301 << std::endl; 303 302 304 303 // Print more statistics 305 std::cout <<"Cuts at root node changed objective from "<<model.getContinuousObjective()306 <<" to "<<model.rootObjectiveAfterCuts()<<std::endl;307 308 for (iGenerator =0;iGenerator<numberGenerators;iGenerator++) {309 CbcCutGenerator * 310 std::cout <<generator>cutGeneratorName()<<" was tried "311 <<generator>numberTimesEntered()<<" times and created "312 <<generator>numberCutsInTotal()<<" cuts of which "313 <<generator>numberCutsActive()<<" were active after adding rounds of cuts";304 std::cout << "Cuts at root node changed objective from " << model.getContinuousObjective() 305 << " to " << model.rootObjectiveAfterCuts() << std::endl; 306 307 for (iGenerator = 0; iGenerator < numberGenerators; iGenerator++) { 308 CbcCutGenerator *generator = model.cutGenerator(iGenerator); 309 std::cout << generator>cutGeneratorName() << " was tried " 310 << generator>numberTimesEntered() << " times and created " 311 << generator>numberCutsInTotal() << " cuts of which " 312 << generator>numberCutsActive() << " were active after adding rounds of cuts"; 314 313 if (generator>timing()) 315 std::cout <<" ( "<<generator>timeInCutGenerator()<<" seconds)"<<std::endl;314 std::cout << " ( " << generator>timeInCutGenerator() << " seconds)" << std::endl; 316 315 else 317 std::cout <<std::endl;316 std::cout << std::endl; 318 317 } 319 318 // Print solution if finished  we can't get names from Osi!  so get from OsiClp 320 319 321 if (model.getMinimizationObjValue() <1.0e50) {322 #if PREPROCESS ==1320 if (model.getMinimizationObjValue() < 1.0e50) { 321 #if PREPROCESS == 1 323 322 // post process 324 OsiSolverInterface * 323 OsiSolverInterface *solver; 325 324 if (preProcess) { 326 325 process.postProcess(*model.solver()); 327 326 // Solution now back in solver1 328 solver = & 327 solver = &solver1; 329 328 } else { 330 329 solver = model.solver(); 331 330 } 332 331 #else 333 OsiSolverInterface * 332 OsiSolverInterface *solver = model.solver(); 334 333 #endif 335 334 int numberColumns = solver>getNumCols(); 336 337 const double * 335 336 const double *solution = solver>getColSolution(); 338 337 339 338 // Get names from solver1 (as OsiSolverInterface may lose) 340 std::vector< std::string> columnNames = *solver1.getModelPtr()>columnNames();341 339 std::vector< std::string > columnNames = *solver1.getModelPtr()>columnNames(); 340 342 341 int iColumn; 343 std::cout <<std::setiosflags(std::ios::fixedstd::ios::showpoint)<<std::setw(14);344 345 std::cout <<""<<std::endl;346 for (iColumn =0;iColumn<numberColumns;iColumn++) {347 double value =solution[iColumn];348 if (fabs(value) >1.0e7&&solver>isInteger(iColumn))349 std::cout<<std::setw(6)<<iColumn<<" "350 <<columnNames[iColumn]<<" "351 <<value<<std::endl;352 } 353 std::cout <<""<<std::endl;354 355 std::cout <<std::resetiosflags(std::ios::fixedstd::ios::showpointstd::ios::scientific);342 std::cout << std::setiosflags(std::ios::fixed  std::ios::showpoint) << std::setw(14); 343 344 std::cout << "" << std::endl; 345 for (iColumn = 0; iColumn < numberColumns; iColumn++) { 346 double value = solution[iColumn]; 347 if (fabs(value) > 1.0e7 && solver>isInteger(iColumn)) 348 std::cout << std::setw(6) << iColumn << " " 349 << columnNames[iColumn] << " " 350 << value << std::endl; 351 } 352 std::cout << "" << std::endl; 353 354 std::cout << std::resetiosflags(std::ios::fixed  std::ios::showpoint  std::ios::scientific); 356 355 } 357 356 return 0; 358 } 357 }
Note: See TracChangeset
for help on using the changeset viewer.