Changeset 68


Ignore:
Timestamp:
Feb 25, 2005 3:58:02 PM (16 years ago)
Author:
forrest
Message:

adding pre-processing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Samples/sample2.cpp

    r58 r68  
    3333#include "CglFlowCover.hpp"
    3434#include "CglMixedIntegerRounding.hpp"
     35// Preprocessing
     36#include "CglPreProcess.hpp"
    3537
    3638// Heuristics
     
    7375#ifdef COIN_USE_CLP
    7476  OsiClpSolverInterface solver1;
    75   //solver1.messageHandler()->setLogLevel(0);
    76   CbcModel model(solver1);
    7777#elif COIN_USE_OSL
    7878  OsiOslSolverInterface solver1;
    79   //solver1.messageHandler()->setLogLevel(0);
    80   CbcModel model(solver1);
    81 #endif
    82   model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
     79#endif
    8380
    8481  // Read in model using argv[1]
     
    8683  std::string mpsFileName = "../../Mps/Sample/p0033.mps";
    8784  if (argc>=2) mpsFileName = argv[1];
    88   int numMpsReadErrors = model.solver()->readMps(mpsFileName.c_str(),"");
     85  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
    8986  assert(numMpsReadErrors==0);
    90 
     87  double time1 = CoinCpuTime();
     88
     89  /* Options are:
     90     preprocess to do preprocessing
     91     time in minutes
     92     if 2 parameters and numeric taken as time
     93  */
     94  bool preProcess=false;
     95  double minutes=-1.0;
     96  int nGoodParam=0;
     97  for (int iParam=2; iParam<argc;iParam++) {
     98    if (!strcmp(argv[iParam],"preprocess")) {
     99      preProcess=true;
     100      nGoodParam++;
     101    } else if (!strcmp(argv[iParam],"time")) {
     102      if (iParam+1<argc&&isdigit(argv[iParam+1][0])) {
     103        minutes=atof(argv[iParam+1]);
     104        if (minutes>=0.0) {
     105          nGoodParam+=2;
     106          iParam++; // skip time
     107        }
     108      }
     109    }
     110  }
     111  if (nGoodParam==0&&argc==3&&isdigit(argv[2][0])) {
     112    // If time is given then stop after that number of minutes
     113    minutes = atof(argv[2]);
     114    if (minutes>=0.0)
     115      nGoodParam=1;
     116  }
     117  if (nGoodParam!=argc-2) {
     118    printf("Usage <file> [preprocess] [time <minutes>] or <file> <minutes>\n");
     119    exit(1);
     120  }
     121  solver1.initialSolve();
     122  // Reduce printout
     123  solver1.setHintParam(OsiDoReducePrint,true,OsiHintTry);
     124  // See if we want preprocessing
     125  OsiSolverInterface * solver2=&solver1;
     126  CglPreProcess process;
     127  if (preProcess) {
     128    /* Do not try and produce equality cliques and
     129       do up to 5 passes */
     130    solver2 = process.preProcess(solver1,false,5);
     131    if (!solver2) {
     132      printf("Pre-processing says infeasible\n");
     133      exit(2);
     134    }
     135    solver2->resolve();
     136  }
     137  CbcModel model(*solver2);
     138  model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
    91139  // Set up some cut generators and defaults
    92140  // Probing first as gets tight bounds on continuous
     
    95143  generator1.setUsingObjective(true);
    96144  generator1.setMaxPass(3);
    97   generator1.setMaxProbe(100);
     145  // Number of unsatisfied variables to look at
     146  generator1.setMaxProbe(10);
     147  // How far to follow the consequences
    98148  generator1.setMaxLook(50);
     149  // Only look at rows with fewer than this number of elements
     150  generator1.setMaxElements(200);
    99151  generator1.setRowCuts(3);
    100   //  generator1.snapshot(*model.solver());
    101   //generator1.createCliques(*model.solver(),2,1000,true);
    102   //generator1.setMode(0);
    103152
    104153  CglGomory generator2;
     
    122171 
    123172  // Add in generators
     173  // Experiment with -1 and -99 etc
    124174  model.addCutGenerator(&generator1,-1,"Probing");
    125175  model.addCutGenerator(&generator2,-1,"Gomory");
     
    129179  model.addCutGenerator(&flowGen,-1,"FlowCover");
    130180  model.addCutGenerator(&mixedGen,-1,"MixedIntegerRounding");
    131 
     181  // Say we want timings
     182  int numberGenerators = model.numberCutGenerators();
     183  int iGenerator;
     184  for (iGenerator=0;iGenerator<numberGenerators;iGenerator++) {
     185    CbcCutGenerator * generator = model.cutGenerator(iGenerator);
     186    generator->setTiming(true);
     187  }
    132188#ifdef COIN_USE_CLP
    133189  OsiClpSolverInterface * osiclp = dynamic_cast< OsiClpSolverInterface*> (model.solver());
     
    182238
    183239  // If time is given then stop after that number of minutes
    184   if (argc>2) {
    185     int minutes = atoi(argv[2]);
     240  if (minutes>=0.0) {
    186241    std::cout<<"Stopping after "<<minutes<<" minutes"<<std::endl;
    187     assert (minutes>=0);
    188242    model.setDblParam(CbcModel::CbcMaximumSeconds,60.0*minutes);
    189243  }
    190244  // Switch off most output
    191   if (model.getNumCols()<3000) {
     245  if (model.getNumCols()<-3000) {
    192246    model.messageHandler()->setLogLevel(1);
    193247    //model.solver()->messageHandler()->setLogLevel(0);
     
    199253  //model.solver()->messageHandler()->setLogLevel(2);
    200254  //model.setPrintFrequency(50);
    201   //#define DEBUG_CUTS 2
    202 #if DEBUG_CUTS==1
    203   // Set up debugger by value
    204   {
    205     int numberColumns=model.getNumCols();
    206     double * solution = new double[numberColumns];
    207     memset(solution, 0,numberColumns*sizeof(double));
    208     // Set nonzero integers
    209     solution[1]=1.0;
    210     solution[18]=1.0;
    211     solution[33]=1.0;
    212     solution[59]=1.0;
    213     model.solver()->activateRowCutDebugger(solution);
    214     delete [] solution;
    215   }
    216 #elif DEBUG_CUTS==2
    217   // Set up debugger by name
    218   {
     255#ifdef DEBUG_CUTS
     256  // Set up debugger by name (only if no preprocesing)
     257  if (!preProcess) {
    219258    std::string problemName ;
    220259    model.solver()->getStrParam(OsiProbName,problemName) ;
     
    222261  }
    223262#endif
    224   double time1 = CoinCpuTime();
    225 
    226   if (0) {
    227     // integer presolve
    228     CbcModel * model2 = model.integerPresolve();
    229     if (model2) {
    230       // Do complete search
     263
     264  // Do complete search
    231265 
    232       model2->branchAndBound();
    233       // get back solution
    234       model.originalModel(model2,false);
    235     } else {
    236       // infeasible
    237       exit(1);
    238     }
    239   } else {
    240     // Do complete search
    241  
    242     model.branchAndBound();
    243   }
     266  model.branchAndBound();
    244267
    245268  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
     
    249272           <<std::endl;
    250273
    251   printf("Exact obj %18.18g\n",model.getObjValue());
    252274  // Print more statistics
    253275  std::cout<<"Cuts at root node changed objective from "<<model.getContinuousObjective()
    254276           <<" to "<<model.rootObjectiveAfterCuts()<<std::endl;
    255277
    256   int numberGenerators = model.numberCutGenerators();
    257   for (int iGenerator=0;iGenerator<numberGenerators;iGenerator++) {
     278  for (iGenerator=0;iGenerator<numberGenerators;iGenerator++) {
    258279    CbcCutGenerator * generator = model.cutGenerator(iGenerator);
    259280    std::cout<<generator->cutGeneratorName()<<" was tried "
    260281             <<generator->numberTimesEntered()<<" times and created "
    261282             <<generator->numberCutsInTotal()<<" cuts of which "
    262              <<generator->numberCutsActive()<<" were active after adding rounds of cuts"
    263              <<std::endl;
     283             <<generator->numberCutsActive()<<" were active after adding rounds of cuts";
     284    if (generator->timing())
     285      std::cout<<" ( "<<generator->timeInCutGenerator()<<" seconds)"<<std::endl;
     286    else
     287      std::cout<<std::endl;
    264288  }
    265289  // Print solution if finished - we can't get names from Osi!
    266290
    267291  if (model.getMinimizationObjValue()<1.0e50) {
     292    // post process
     293    if (preProcess)
     294      process.postProcess(*model.solver());
    268295    int numberColumns = model.solver()->getNumCols();
    269296   
Note: See TracChangeset for help on using the changeset viewer.