Changeset 222 for trunk/CbcStrategy.cpp


Ignore:
Timestamp:
Dec 9, 2005 6:07:01 PM (14 years ago)
Author:
forrest
Message:

towards next version

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CbcStrategy.cpp

    r197 r222  
    1010
    1111#include "OsiSolverInterface.hpp"
     12#define COIN_USE_CLP
     13#ifdef COIN_USE_CLP
     14#include "OsiClpSolverInterface.hpp"
     15#endif
    1216#include "CbcModel.hpp"
    1317#include "CbcMessage.hpp"
    1418#include "CbcStrategy.hpp"
    1519#include "CbcCutGenerator.hpp"
     20#include "CbcBranchActual.hpp"
     21#include "CglPreProcess.hpp"
    1622// Cuts
    1723
     
    3036// Default Constructor
    3137CbcStrategy::CbcStrategy()
    32   :depth_(0)
     38  :depth_(0),
     39   preProcessState_(0),
     40   process_(NULL)
    3341{
    3442}
     
    3745CbcStrategy::~CbcStrategy ()
    3846{
     47  delete process_;
     48}
     49// Delete pre-processing object to save memory
     50void
     51CbcStrategy::deletePreProcess()
     52{
     53  delete process_;
     54  process_=NULL;
    3955}
    4056
     
    4864   numberStrong_(numberStrong),
    4965   numberBeforeTrust_(numberBeforeTrust),
    50    printLevel_(printLevel)
     66   printLevel_(printLevel),
     67   desiredPreProcess_(0),
     68   preProcessPasses_(0)
    5169{
    5270}
     
    7290  numberStrong_(rhs.numberStrong_),
    7391  numberBeforeTrust_(rhs.numberBeforeTrust_),
    74   printLevel_(rhs.printLevel_)
     92  printLevel_(rhs.printLevel_),
     93  desiredPreProcess_(rhs.desiredPreProcess_),
     94  preProcessPasses_(rhs.preProcessPasses_)
    7595{
    7696  setNested(rhs.getNested());
     
    243263CbcStrategyDefault::setupOther(CbcModel & model)
    244264{
     265  // See if preprocessing wanted
     266  if (desiredPreProcess_) {
     267    delete process_;
     268    // solver_ should have been cloned outside
     269    CglPreProcess * process = new CglPreProcess();
     270    OsiSolverInterface * solver = model.solver();
     271    int logLevel = model.messageHandler()->logLevel();
     272#ifdef COIN_USE_CLP
     273    OsiClpSolverInterface * clpSolver = dynamic_cast< OsiClpSolverInterface*> (solver);
     274    ClpSimplex * lpSolver=NULL;
     275    if (clpSolver) {
     276      if (clpSolver->messageHandler()->logLevel())
     277        clpSolver->messageHandler()->setLogLevel(1);
     278      if (logLevel>-1)
     279        clpSolver->messageHandler()->setLogLevel(CoinMin(logLevel,clpSolver->messageHandler()->logLevel()));
     280      lpSolver = clpSolver->getModelPtr();
     281      /// If user left factorization frequency then compute
     282      lpSolver->defaultFactorizationFrequency();
     283    }
     284#endif
     285    // Tell solver we are in Branch and Cut
     286    solver->setHintParam(OsiDoInBranchAndCut,true,OsiHintDo) ;
     287    // Default set of cut generators
     288    CglProbing generator1;
     289    generator1.setUsingObjective(true);
     290    generator1.setMaxPass(3);
     291    generator1.setMaxProbeRoot(solver->getNumCols());
     292    generator1.setMaxElements(100);
     293    generator1.setMaxLookRoot(50);
     294    generator1.setRowCuts(3);
     295    //generator1.messageHandler()->setLogLevel(logLevel);
     296    process->messageHandler()->setLogLevel(logLevel);
     297    // Add in generators
     298    process->addCutGenerator(&generator1);
     299    int translate[]={9999,0,2};
     300    OsiSolverInterface * solver2 =
     301      process->preProcessNonDefault(*solver,
     302                                    translate[desiredPreProcess_],preProcessPasses_);
     303    // Tell solver we are not in Branch and Cut
     304    solver->setHintParam(OsiDoInBranchAndCut,false,OsiHintDo) ;
     305    if (solver2)
     306      solver2->setHintParam(OsiDoInBranchAndCut,false,OsiHintDo) ;
     307    bool feasible=true;
     308    if (!solver2) {
     309      feasible = false;
     310    } else {
     311      // now tighten bounds
     312#ifdef COIN_USE_CLP
     313      if (clpSolver) {
     314        // model has changed
     315        solver = model.solver();
     316        OsiClpSolverInterface * clpSolver = dynamic_cast< OsiClpSolverInterface*> (solver);
     317        ClpSimplex * lpSolver = clpSolver->getModelPtr();
     318        if (lpSolver->tightenPrimalBounds()==0) {
     319          lpSolver->dual();
     320        } else {
     321          feasible = false;
     322        }
     323      }
     324#endif
     325      if (feasible) {
     326        preProcessState_=1;
     327        process_=process;
     328        /* Note that original solver will be kept (with false)
     329           and that final solver will also be kept.
     330           This is for post-processing
     331        */
     332        OsiSolverInterface * solver3 = solver2->clone();
     333        model.assignSolver(solver3,false);
     334        if (process_->numberSOS()) {
     335          int numberSOS = process_->numberSOS();
     336          int numberIntegers = model.numberIntegers();
     337          /* model may not have created objects
     338             If none then create
     339          */
     340          if (!numberIntegers||!model.numberObjects()) {
     341            model.findIntegers(true);
     342            numberIntegers = model.numberIntegers();
     343          }
     344          CbcObject ** oldObjects = model.objects();
     345          // Do sets and priorities
     346          CbcObject ** objects = new CbcObject * [numberSOS];
     347          // set old objects to have low priority
     348          int numberOldObjects = model.numberObjects();
     349          int numberColumns = model.getNumCols();
     350          for (int iObj = 0;iObj<numberOldObjects;iObj++) {
     351            int oldPriority = oldObjects[iObj]->priority();
     352            oldObjects[iObj]->setPriority(numberColumns+oldPriority);
     353          }
     354          const int * starts = process_->startSOS();
     355          const int * which = process_->whichSOS();
     356          const int * type = process_->typeSOS();
     357          const double * weight = process_->weightSOS();
     358          int iSOS;
     359          for (iSOS =0;iSOS<numberSOS;iSOS++) {
     360            int iStart = starts[iSOS];
     361            int n=starts[iSOS+1]-iStart;
     362            objects[iSOS] = new CbcSOS(&model,n,which+iStart,weight+iStart,
     363                                       iSOS,type[iSOS]);
     364            // branch on long sets first
     365            objects[iSOS]->setPriority(numberColumns-n);
     366          }
     367          model.addObjects(numberSOS,objects);
     368          for (iSOS=0;iSOS<numberSOS;iSOS++)
     369            delete objects[iSOS];
     370          delete [] objects;
     371        }
     372      } else {
     373        //printf("Pre-processing says infeasible\n");
     374        delete process;
     375        preProcessState_=-1;
     376        process_=NULL;
     377      }
     378    }
     379  }
    245380  model.setNumberStrong(numberStrong_);
    246381  model.setNumberBeforeTrust(numberBeforeTrust_);
Note: See TracChangeset for help on using the changeset viewer.