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

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Cbc/examples/driver4.cpp
r2101 r2469 5 5 6 6 #include <cassert> 7 #include <iomanip> 8 7 #include <iomanip> 9 8 10 9 #include "CoinPragma.hpp" … … 22 21 23 22 //############################################################################# 24 25 23 26 24 /************************************************************************ … … 64 62 or you can check solver status. 65 63 */ 66 /* Return nonzero to return quickly */ 67 static int callBack(CbcModel * 68 { 69 int returnCode =0;64 /* Return nonzero to return quickly */ 65 static int callBack(CbcModel *model, int whereFrom) 66 { 67 int returnCode = 0; 70 68 switch (whereFrom) { 71 69 case 1: 72 70 case 2: 73 if (!model>status() &&model>secondaryStatus())74 returnCode =1;71 if (!model>status() && model>secondaryStatus()) 72 returnCode = 1; 75 73 break; 76 case 3: 77 { 78 // Add in some diving heuristics with different options 79 CbcHeuristicDiveCoefficient heuristicDC(*model); 80 heuristicDC.setHeuristicName("DiveCoefficient"); 81 // do if no solution 82 heuristicDC.setWhen(3); 83 // 150 passes and fix general integers 84 heuristicDC.setMaxIterations(151); 85 // make sure can do as many simplex iterations as wanted 86 heuristicDC.setMaxSimplexIterations(COIN_INT_MAX); 87 heuristicDC.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 88 model>addHeuristic(&heuristicDC) ; 89 CbcHeuristicDiveFractional heuristicDF(*model); 90 heuristicDF.setHeuristicName("DiveFractional"); 91 // do if no solution 92 heuristicDF.setWhen(3); 93 // 150 passes and don't fix general integers 94 heuristicDF.setMaxIterations(150); 95 // make sure can do as many simplex iterations as wanted 96 heuristicDF.setMaxSimplexIterations(COIN_INT_MAX); 97 heuristicDF.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 98 model>addHeuristic(&heuristicDF) ; 99 CbcHeuristicDiveGuided heuristicDG(*model); 100 heuristicDG.setHeuristicName("DiveGuided"); 101 // do if no solution 102 heuristicDG.setWhen(3); 103 // 200 passes and fix general integers 104 heuristicDG.setMaxIterations(201); 105 // make sure can do as many simplex iterations as wanted 106 heuristicDG.setMaxSimplexIterations(COIN_INT_MAX); 107 heuristicDG.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 108 model>addHeuristic(&heuristicDG) ; 109 CbcHeuristicDiveVectorLength heuristicDV(*model); 110 heuristicDV.setHeuristicName("DiveVectorLength"); 111 // do if no solution 112 heuristicDV.setWhen(3); 113 // 150 passes and fix general integers 114 heuristicDV.setMaxIterations(151); 115 // make sure can do as many simplex iterations as wanted 116 heuristicDV.setMaxSimplexIterations(COIN_INT_MAX); 117 heuristicDV.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 118 model>addHeuristic(&heuristicDV) ; 119 // Second version! 120 CbcHeuristicDiveVectorLength heuristicDV2(*model); 121 heuristicDV2.setHeuristicName("DiveVectorLength"); 122 // do if no solution 123 heuristicDV2.setWhen(3); 124 // 300 passes and don't fix general integers 125 heuristicDV2.setMaxIterations(300); 126 // fix fewer 127 heuristicDV2.setPercentageToFix(0.05); 128 // make sure can do as many simplex iterations as wanted 129 heuristicDV2.setMaxSimplexIterations(COIN_INT_MAX); 130 heuristicDV2.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 131 model>addHeuristic(&heuristicDV2) ; 132 CbcHeuristicDivePseudoCost heuristicDP(*model); 133 heuristicDP.setHeuristicName("DivePseudoCost"); 134 // do if no solution 135 heuristicDP.setWhen(3); 136 // 100 passes and don't fix general integers 137 heuristicDP.setMaxIterations(100); 138 // make sure can do as many simplex iterations as wanted 139 heuristicDP.setMaxSimplexIterations(COIN_INT_MAX); 140 heuristicDP.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 141 model>addHeuristic(&heuristicDP) ; 142 CbcHeuristicDiveLineSearch heuristicDL(*model); 143 heuristicDL.setHeuristicName("DiveLineSearch"); 144 // do if no solution 145 heuristicDL.setWhen(3); 146 // 150 passes and fix general integers 147 heuristicDL.setMaxIterations(151); 148 // make sure can do as many simplex iterations as wanted 149 heuristicDL.setMaxSimplexIterations(COIN_INT_MAX); 150 heuristicDL.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 151 model>addHeuristic(&heuristicDL) ; 152 //CbcCompareUser compare; 153 //model>setNodeComparison(compare); 154 } 155 break; 74 case 3: { 75 // Add in some diving heuristics with different options 76 CbcHeuristicDiveCoefficient heuristicDC(*model); 77 heuristicDC.setHeuristicName("DiveCoefficient"); 78 // do if no solution 79 heuristicDC.setWhen(3); 80 // 150 passes and fix general integers 81 heuristicDC.setMaxIterations(151); 82 // make sure can do as many simplex iterations as wanted 83 heuristicDC.setMaxSimplexIterations(COIN_INT_MAX); 84 heuristicDC.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 85 model>addHeuristic(&heuristicDC); 86 CbcHeuristicDiveFractional heuristicDF(*model); 87 heuristicDF.setHeuristicName("DiveFractional"); 88 // do if no solution 89 heuristicDF.setWhen(3); 90 // 150 passes and don't fix general integers 91 heuristicDF.setMaxIterations(150); 92 // make sure can do as many simplex iterations as wanted 93 heuristicDF.setMaxSimplexIterations(COIN_INT_MAX); 94 heuristicDF.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 95 model>addHeuristic(&heuristicDF); 96 CbcHeuristicDiveGuided heuristicDG(*model); 97 heuristicDG.setHeuristicName("DiveGuided"); 98 // do if no solution 99 heuristicDG.setWhen(3); 100 // 200 passes and fix general integers 101 heuristicDG.setMaxIterations(201); 102 // make sure can do as many simplex iterations as wanted 103 heuristicDG.setMaxSimplexIterations(COIN_INT_MAX); 104 heuristicDG.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 105 model>addHeuristic(&heuristicDG); 106 CbcHeuristicDiveVectorLength heuristicDV(*model); 107 heuristicDV.setHeuristicName("DiveVectorLength"); 108 // do if no solution 109 heuristicDV.setWhen(3); 110 // 150 passes and fix general integers 111 heuristicDV.setMaxIterations(151); 112 // make sure can do as many simplex iterations as wanted 113 heuristicDV.setMaxSimplexIterations(COIN_INT_MAX); 114 heuristicDV.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 115 model>addHeuristic(&heuristicDV); 116 // Second version! 117 CbcHeuristicDiveVectorLength heuristicDV2(*model); 118 heuristicDV2.setHeuristicName("DiveVectorLength"); 119 // do if no solution 120 heuristicDV2.setWhen(3); 121 // 300 passes and don't fix general integers 122 heuristicDV2.setMaxIterations(300); 123 // fix fewer 124 heuristicDV2.setPercentageToFix(0.05); 125 // make sure can do as many simplex iterations as wanted 126 heuristicDV2.setMaxSimplexIterations(COIN_INT_MAX); 127 heuristicDV2.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 128 model>addHeuristic(&heuristicDV2); 129 CbcHeuristicDivePseudoCost heuristicDP(*model); 130 heuristicDP.setHeuristicName("DivePseudoCost"); 131 // do if no solution 132 heuristicDP.setWhen(3); 133 // 100 passes and don't fix general integers 134 heuristicDP.setMaxIterations(100); 135 // make sure can do as many simplex iterations as wanted 136 heuristicDP.setMaxSimplexIterations(COIN_INT_MAX); 137 heuristicDP.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 138 model>addHeuristic(&heuristicDP); 139 CbcHeuristicDiveLineSearch heuristicDL(*model); 140 heuristicDL.setHeuristicName("DiveLineSearch"); 141 // do if no solution 142 heuristicDL.setWhen(3); 143 // 150 passes and fix general integers 144 heuristicDL.setMaxIterations(151); 145 // make sure can do as many simplex iterations as wanted 146 heuristicDL.setMaxSimplexIterations(COIN_INT_MAX); 147 heuristicDL.setMaxSimplexIterationsAtRoot(COIN_INT_MAX); 148 model>addHeuristic(&heuristicDL); 149 //CbcCompareUser compare; 150 //model>setNodeComparison(compare); 151 } break; 156 152 case 4: 157 153 // If not good enough could skip postprocessing 158 break; 154 break; 159 155 case 5: 160 156 break; … … 172 168 173 169 class MyEventHandler3 : public CbcEventHandler { 174 170 175 171 public: 176 172 /**@name Overrides */ … … 184 180 MyEventHandler3(); 185 181 /// Constructor with pointer to model (redundant as setEventHandler does) 186 MyEventHandler3(CbcModel * 182 MyEventHandler3(CbcModel *model); 187 183 /** Destructor */ 188 184 virtual ~MyEventHandler3(); 189 185 /** The copy constructor. */ 190 MyEventHandler3(const MyEventHandler3 & 186 MyEventHandler3(const MyEventHandler3 &rhs); 191 187 /// Assignment 192 MyEventHandler3 & operator=(const MyEventHandler3 &rhs);188 MyEventHandler3 &operator=(const MyEventHandler3 &rhs); 193 189 /// Clone 194 virtual CbcEventHandler * clone() const;190 virtual CbcEventHandler *clone() const; 195 191 //@} 196 197 192 198 193 protected: 199 194 // data goes here 200 195 }; 201 196 // 202 // Default Constructor 203 // 204 MyEventHandler3::MyEventHandler3 ()197 // Default Constructor 198 // 199 MyEventHandler3::MyEventHandler3() 205 200 : CbcEventHandler() 206 201 { … … 208 203 209 204 // 210 // Copy constructor 211 // 212 MyEventHandler3::MyEventHandler3 (const MyEventHandler3 & rhs)213 : CbcEventHandler(rhs)214 { 205 // Copy constructor 206 // 207 MyEventHandler3::MyEventHandler3(const MyEventHandler3 &rhs) 208 : CbcEventHandler(rhs) 209 { 215 210 } 216 211 217 212 // Constructor with pointer to model 218 MyEventHandler3::MyEventHandler3(CbcModel * 213 MyEventHandler3::MyEventHandler3(CbcModel *model) 219 214 : CbcEventHandler(model) 220 215 { … … 222 217 223 218 // 224 // Destructor 225 // 226 MyEventHandler3::~MyEventHandler3 219 // Destructor 220 // 221 MyEventHandler3::~MyEventHandler3() 227 222 { 228 223 } 229 224 230 225 // 231 // Assignment operator 226 // Assignment operator 232 227 // 233 228 MyEventHandler3 & 234 MyEventHandler3::operator=(const MyEventHandler3 &rhs)229 MyEventHandler3::operator=(const MyEventHandler3 &rhs) 235 230 { 236 231 if (this != &rhs) { … … 242 237 // Clone 243 238 // 244 CbcEventHandler * 239 CbcEventHandler *MyEventHandler3::clone() const 245 240 { 246 241 return new MyEventHandler3(*this); 247 242 } 248 243 249 CbcEventHandler::CbcAction 244 CbcEventHandler::CbcAction 250 245 MyEventHandler3::event(CbcEvent whichEvent) 251 246 { 252 247 // If in sub tree carry on 253 248 if (!model_>parentModel()) { 254 if (whichEvent ==solutionwhichEvent==heuristicSolution) {249 if (whichEvent == solution  whichEvent == heuristicSolution) { 255 250 #ifdef STOP_EARLY 256 251 return stop; // say finished … … 258 253 // If preprocessing was done solution will be to processed model 259 254 int numberColumns = model_>getNumCols(); 260 const double * 261 assert 262 printf("value of solution is %g\n", model_>getObjValue());263 for (int i =0;i<numberColumns;i++) {264 if (fabs(bestSolution[i])>1.0e8)265 printf("%d %g\n",i,bestSolution[i]);255 const double *bestSolution = model_>bestSolution(); 256 assert(bestSolution); 257 printf("value of solution is %g\n", model_>getObjValue()); 258 for (int i = 0; i < numberColumns; i++) { 259 if (fabs(bestSolution[i]) > 1.0e8) 260 printf("%d %g\n", i, bestSolution[i]); 266 261 } 267 262 return noAction; // carry on … … 271 266 } 272 267 } else { 273 274 } 275 } 276 277 int main 268 return noAction; // carry on 269 } 270 } 271 272 int main(int argc, const char *argv[]) 278 273 { 279 274 … … 282 277 #ifdef USE_OSI_NAMES 283 278 // Say we are keeping names (a bit slower this way) 284 solver1.setIntParam(OsiNameDiscipline, 1);279 solver1.setIntParam(OsiNameDiscipline, 1); 285 280 #endif 286 281 // Read in model using argv[1] … … 295 290 } 296 291 #endif 297 if (argc >=2) mpsFileName = argv[1];298 int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");299 i f( numMpsReadErrors != 0 )300 {301 302 292 if (argc >= 2) 293 mpsFileName = argv[1]; 294 int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(), ""); 295 if (numMpsReadErrors != 0) { 296 printf("%d errors reading MPS file\n", numMpsReadErrors); 297 return numMpsReadErrors; 303 298 } 304 299 // Tell solver to return fast if presolve or initial solve infeasible … … 309 304 */ 310 305 // Messy code below copied from CbcSolver.cpp 311 // Pass to Cbc initialize defaults 306 // Pass to Cbc initialize defaults 312 307 CbcModel modelA(solver1); 313 CbcModel * 308 CbcModel *model = &modelA; 314 309 CbcMain0(modelA); 315 310 // Event handler … … 320 315 but this will do 321 316 */ 322 if (argc >2) {323 CbcMain1(argc 1,argv+1,modelA,callBack);317 if (argc > 2) { 318 CbcMain1(argc  1, argv + 1, modelA, callBack); 324 319 } else { 325 const char * argv2[]={"driver4","solve","quit"};326 CbcMain1(3, argv2,modelA,callBack);320 const char *argv2[] = { "driver4", "solve", "quit" }; 321 CbcMain1(3, argv2, modelA, callBack); 327 322 } 328 323 // Solver was cloned so get current copy 329 OsiSolverInterface * 324 OsiSolverInterface *solver = model>solver(); 330 325 // Print solution if finished (could get from model>bestSolution() as well 331 326 332 327 if (model>bestSolution()) { 333 334 const double * 335 328 329 const double *solution = solver>getColSolution(); 330 336 331 int iColumn; 337 332 int numberColumns = solver>getNumCols(); 338 std::cout <<std::setiosflags(std::ios::fixedstd::ios::showpoint)<<std::setw(14);339 340 std::cout <<""<<std::endl;333 std::cout << std::setiosflags(std::ios::fixed  std::ios::showpoint) << std::setw(14); 334 335 std::cout << "" << std::endl; 341 336 #ifdef USE_OSI_NAMES 342 343 for (iColumn =0;iColumn<numberColumns;iColumn++) {344 double value =solution[iColumn];345 if (fabs(value) >1.0e7&&solver>isInteger(iColumn))346 std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver>getColName(iColumn)347 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;337 338 for (iColumn = 0; iColumn < numberColumns; iColumn++) { 339 double value = solution[iColumn]; 340 if (fabs(value) > 1.0e7 && solver>isInteger(iColumn)) 341 std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver>getColName(iColumn) 342 << resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl; 348 343 } 349 344 #else 350 345 // names may not be in current solver  use original 351 352 for (iColumn =0;iColumn<numberColumns;iColumn++) {353 double value =solution[iColumn];354 if (fabs(value) >1.0e7&&solver>isInteger(iColumn))355 std::cout<<std::setw(6)<<iColumn<<" "<<std::setw(8)<<setiosflags(std::ios::left)<<solver1.getModelPtr()>columnName(iColumn)356 <<resetiosflags(std::ios::adjustfield)<<std::setw(14)<<" "<<value<<std::endl;346 347 for (iColumn = 0; iColumn < numberColumns; iColumn++) { 348 double value = solution[iColumn]; 349 if (fabs(value) > 1.0e7 && solver>isInteger(iColumn)) 350 std::cout << std::setw(6) << iColumn << " " << std::setw(8) << setiosflags(std::ios::left) << solver1.getModelPtr()>columnName(iColumn) 351 << resetiosflags(std::ios::adjustfield) << std::setw(14) << " " << value << std::endl; 357 352 } 358 353 #endif 359 std::cout <<""<<std::endl;360 361 std::cout <<std::resetiosflags(std::ios::fixedstd::ios::showpointstd::ios::scientific);354 std::cout << "" << std::endl; 355 356 std::cout << std::resetiosflags(std::ios::fixed  std::ios::showpoint  std::ios::scientific); 362 357 } else { 363 std::cout <<" No solution!"<<std::endl;358 std::cout << " No solution!" << std::endl; 364 359 } 365 360 return 0; 366 } 361 }
Note: See TracChangeset
for help on using the changeset viewer.