Ignore:
Timestamp:
Jan 6, 2019 6:17:46 PM (4 months ago)
Author:
unxusr
Message:

formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/examples/sample2.cpp

    r1898 r2469  
    3939//#############################################################################
    4040
    41 
    4241/************************************************************************
    4342
     
    6362#define PREPROCESS 2
    6463
    65 int main (int argc, const char *argv[])
     64int main(int argc, const char *argv[])
    6665{
    6766
    6867  // Define your favorite OsiSolver
    69  
     68
    7069  OsiClpSolverInterface solver1;
    7170
    7271  // Read in model using argv[1]
    7372  // and assert that it is a clean model
    74   std::string dirsep(1,CoinFindDirSeparator());
     73  std::string dirsep(1, CoinFindDirSeparator());
    7574  std::string mpsFileName;
    76 # if defined(SAMPLEDIR)
    77   mpsFileName = SAMPLEDIR ;
    78   mpsFileName += dirsep+"p0033.mps";
    79 # else
     75#if defined(SAMPLEDIR)
     76  mpsFileName = SAMPLEDIR;
     77  mpsFileName += dirsep + "p0033.mps";
     78#else
    8079  if (argc < 2) {
    8180    fprintf(stderr, "Do not know where to find sample MPS files.\n");
    8281    exit(1);
    8382  }
    84 # endif
    85   if (argc>=2) mpsFileName = argv[1];
    86   int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
    87   if( numMpsReadErrors != 0 )
    88   {
    89      printf("%d errors reading MPS file\n", numMpsReadErrors);
    90      return numMpsReadErrors;
     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;
    9190  }
    9291  double time1 = CoinCpuTime();
     
    9796     if 2 parameters and numeric taken as time
    9897  */
    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;
    105104      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;
    111110          iParam++; // skip time
    112111        }
     
    114113    }
    115114  }
    116   if (nGoodParam==0&&argc==3&&isdigit(argv[2][0])) {
     115  if (nGoodParam == 0 && argc == 3 && isdigit(argv[2][0])) {
    117116    // If time is given then stop after that number of minutes
    118117    minutes = atof(argv[2]);
    119     if (minutes>=0.0)
    120       nGoodParam=1;
    121   }
    122   if (nGoodParam!=argc-2&&argc>=2) {
     118    if (minutes >= 0.0)
     119      nGoodParam = 1;
     120  }
     121  if (nGoodParam != argc - 2 && argc >= 2) {
    123122    printf("Usage <file> [preprocess] [time <minutes>] or <file> <minutes>\n");
    124123    exit(1);
     
    126125  solver1.initialSolve();
    127126  // Reduce printout
    128   solver1.setHintParam(OsiDoReducePrint,true,OsiHintTry);
     127  solver1.setHintParam(OsiDoReducePrint, true, OsiHintTry);
    129128  // See if we want preprocessing
    130   OsiSolverInterface * solver2=&solver1;
    131 #if PREPROCESS==1
     129  OsiSolverInterface *solver2 = &solver1;
     130#if PREPROCESS == 1
    132131  CglPreProcess process;
    133132  if (preProcess) {
    134133    /* Do not try and produce equality cliques and
    135134       do up to 5 passes */
    136     solver2 = process.preProcess(solver1,false,5);
     135    solver2 = process.preProcess(solver1, false, 5);
    137136    if (!solver2) {
    138137      printf("Pre-processing says infeasible\n");
     
    143142#endif
    144143  CbcModel model(*solver2);
    145   model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
     144  model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
    146145  // Set up some cut generators and defaults
    147146  // Probing first as gets tight bounds on continuous
     
    177176  CglMixedIntegerRounding2 mixedGen;
    178177  CglFlowCover flowGen;
    179  
     178
    180179  // Add in generators
    181180  // 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");
    185184  // 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");
    189188  // Say we want timings
    190189  int numberGenerators = model.numberCutGenerators();
    191190  int iGenerator;
    192   for (iGenerator=0;iGenerator<numberGenerators;iGenerator++) {
    193     CbcCutGenerator * generator = model.cutGenerator(iGenerator);
     191  for (iGenerator = 0; iGenerator < numberGenerators; iGenerator++) {
     192    CbcCutGenerator *generator = model.cutGenerator(iGenerator);
    194193    generator->setTiming(true);
    195194  }
    196   OsiClpSolverInterface * osiclp = dynamic_cast< OsiClpSolverInterface*> (model.solver());
     195  OsiClpSolverInterface *osiclp = dynamic_cast< OsiClpSolverInterface * >(model.solver());
    197196  // go faster stripes
    198197  if (osiclp) {
     
    200199    // Used to be automatically set
    201200    osiclp->setSpecialOptions(128);
    202     if(osiclp->getNumRows()<300&&osiclp->getNumCols()<500) {
     201    if (osiclp->getNumRows() < 300 && osiclp->getNumCols() < 500) {
    203202      //osiclp->setupForRepeatedUse(2,0);
    204       osiclp->setupForRepeatedUse(0,0);
    205     }
    206   } 
     203      osiclp->setupForRepeatedUse(0, 0);
     204    }
     205  }
    207206  // Uncommenting this should switch off all CBC messages
    208207  // model.messagesPointer()->setDetailMessages(10,10000,NULL);
     
    229228
    230229  // Could tune more
    231   double objValue = model.solver()->getObjSense()*model.solver()->getObjValue();
    232   double minimumDropA=CoinMin(1.0,fabs(objValue)*1.0e-3+1.0e-4);
    233   double minimumDrop= fabs(objValue)*1.0e-4+1.0e-4;
    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.0e-3 + 1.0e-4);
     232  double minimumDrop = fabs(objValue) * 1.0e-4 + 1.0e-4;
     233  printf("min drop %g (A %g)\n", minimumDrop, minimumDropA);
    235234  model.setMinimumDrop(minimumDrop);
    236235
    237   if (model.getNumCols()<500)
     236  if (model.getNumCols() < 500)
    238237    model.setMaximumCutPassesAtRoot(-100); // always do 100 if possible
    239   else if (model.getNumCols()<5000)
     238  else if (model.getNumCols() < 5000)
    240239    model.setMaximumCutPassesAtRoot(100); // use minimum drop
    241240  else
     
    247246  // model.setNumberStrong(0);
    248247  // Do more strong branching if small
    249   if (model.getNumCols()<5000)
     248  if (model.getNumCols() < 5000)
    250249    model.setNumberStrong(10);
    251250  model.setNumberStrong(20);
     
    253252  model.setNumberBeforeTrust(5);
    254253
    255   model.solver()->setIntParam(OsiMaxNumIterationHotStart,100);
     254  model.solver()->setIntParam(OsiMaxNumIterationHotStart, 100);
    256255
    257256  // 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);
    261260  }
    262261  // Switch off most output
    263   if (model.getNumCols()<3000) {
     262  if (model.getNumCols() < 3000) {
    264263    model.messageHandler()->setLogLevel(1);
    265264    //model.solver()->messageHandler()->setLogLevel(0);
     
    275274  // Set up debugger by name (only if no preprocesing)
    276275  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==2
     276    std::string problemName;
     277    model.solver()->getStrParam(OsiProbName, problemName);
     278    model.solver()->activateRowCutDebugger(problemName.c_str());
     279  }
     280#endif
     281#if PREPROCESS == 2
    283282  // Default strategy will leave cut generators as they exist already
    284283  // so cutsOnlyAtRoot (1) ignored
     
    286285  // numberBeforeTrust (3) is 5 (default is 0)
    287286  // printLevel (4) defaults (0)
    288   CbcStrategyDefault strategy(true,5,5);
     287  CbcStrategyDefault strategy(true, 5, 5);
    289288  // Set up pre-processing to find sos if wanted
    290289  if (preProcess)
     
    293292#endif
    294293  // Do complete search
    295  
     294
    296295  model.branchAndBound();
    297296
    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;
    303302
    304303  // 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 * generator = model.cutGenerator(iGenerator);
    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";
    314313    if (generator->timing())
    315       std::cout<<" ( "<<generator->timeInCutGenerator()<<" seconds)"<<std::endl;
     314      std::cout << " ( " << generator->timeInCutGenerator() << " seconds)" << std::endl;
    316315    else
    317       std::cout<<std::endl;
     316      std::cout << std::endl;
    318317  }
    319318  // Print solution if finished - we can't get names from Osi! - so get from OsiClp
    320319
    321   if (model.getMinimizationObjValue()<1.0e50) {
    322 #if PREPROCESS==1
     320  if (model.getMinimizationObjValue() < 1.0e50) {
     321#if PREPROCESS == 1
    323322    // post process
    324     OsiSolverInterface * solver;
     323    OsiSolverInterface *solver;
    325324    if (preProcess) {
    326325      process.postProcess(*model.solver());
    327326      // Solution now back in solver1
    328       solver = & solver1;
     327      solver = &solver1;
    329328    } else {
    330329      solver = model.solver();
    331330    }
    332331#else
    333     OsiSolverInterface * solver = model.solver();
     332    OsiSolverInterface *solver = model.solver();
    334333#endif
    335334    int numberColumns = solver->getNumCols();
    336    
    337     const double * solution = solver->getColSolution();
     335
     336    const double *solution = solver->getColSolution();
    338337
    339338    // 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
    342341    int iColumn;
    343     std::cout<<std::setiosflags(std::ios::fixed|std::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.0e-7&&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::fixed|std::ios::showpoint|std::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.0e-7 && 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);
    356355  }
    357356  return 0;
    358 }   
     357}
Note: See TracChangeset for help on using the changeset viewer.