Ignore:
Timestamp:
Nov 5, 2009 10:57:25 AM (10 years ago)
Author:
forrest
Message:

Creating new stable branch 2.4 from trunk (rev 1270)

Location:
stable/2.4
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • stable/2.4

    • Property svn:externals
      •  

        old new  
        77CoinUtils         https://projects.coin-or.org/svn/CoinUtils/stable/2.5/CoinUtils
        88Cgl               https://projects.coin-or.org/svn/Cgl/stable/0.54/Cgl
        9 Clp               https://projects.coin-or.org/svn/Clp/stable/1.10/Clp
         9Clp               https://projects.coin-or.org/svn/Clp/stable/1.11/Clp
        1010Osi               https://projects.coin-or.org/svn/Osi/stable/0.100/Osi
        1111Vol               https://projects.coin-or.org/svn/Vol/stable/1.1/Vol
  • stable/2.4/Cbc/src/CbcHeuristic.cpp

    r1226 r1271  
     1/* $Id$ */
    12// Copyright (C) 2002, International Business Machines
    23// Corporation and others.  All Rights Reserved.
     
    9192
    9293//==============================================================================
    93 
     94#define DEFAULT_WHERE ((255-2-16)*(1+256))
    9495// Default Constructor
    9596CbcHeuristic::CbcHeuristic() :
     
    103104  decayFactor_(0.0),
    104105  switches_(0),
     106  whereFrom_(DEFAULT_WHERE),
    105107  shallowDepth_(1),
    106108  howOftenShallow_(1),
     
    129131  decayFactor_(0.0),
    130132  switches_(0),
     133  whereFrom_(DEFAULT_WHERE),
    131134  shallowDepth_(1),
    132135  howOftenShallow_(1),
     
    153156  heuristicName_ = rhs.heuristicName_;
    154157  howOften_ = rhs.howOften_;
    155   decayFactor_ = rhs.howOften_;
     158  decayFactor_ = rhs.decayFactor_;
    156159  switches_ = rhs.switches_;
     160  whereFrom_ = rhs.whereFrom_;
    157161  shallowDepth_= rhs.shallowDepth_;
    158162  howOftenShallow_= rhs.howOftenShallow_;
     
    258262
    259263bool
    260 CbcHeuristic::shouldHeurRun()
    261 {
     264CbcHeuristic::shouldHeurRun(int whereFrom)
     265{
     266  assert (whereFrom>=0&&whereFrom<16);
     267  // take off 8 (code - likes new solution)
     268  whereFrom &= 7;
     269  if ((whereFrom_&(1<<whereFrom))==0)
     270    return false;
    262271  // No longer used for original purpose - so use for ever run at all JJF
    263272#if 1
     
    360369    double probability = numerator / denominator;
    361370    double randomNumber = randomNumberGenerator_.randomDouble();
    362     if (when_>2&&when_<8) {
     371    int when = when_%100;
     372    if (when>2&&when<8) {
    363373      /* JJF adjustments
    364374         3 only at root and if no solution
     
    368378         7 run up to 2 times if solution found 4 otherwise
    369379      */
    370       switch(when_) {
     380      switch(when) {
    371381      case 3:
    372382      default:
     
    451461  else
    452462    fprintf(fp,"4  %s.setNumberNodes(%d);\n",heuristic,numberNodes_);
     463  if (feasibilityPumpOptions_!=-1)
     464    fprintf(fp,"3  %s.setFeasibilityPumpOptions(%d);\n",heuristic,feasibilityPumpOptions_);
     465  else
     466    fprintf(fp,"4  %s.setFeasibilityPumpOptions(%d);\n",heuristic,feasibilityPumpOptions_);
    453467  if (fractionSmall_!=1.0)
    454468    fprintf(fp,"3  %s.setFractionSmall(%g);\n",heuristic,fractionSmall_);
     
    461475    fprintf(fp,"4  %s.setHeuristicName(\"%s\");\n",
    462476            heuristic,heuristicName_.c_str()) ;
     477  if (decayFactor_!=0.0)
     478    fprintf(fp,"3  %s.setDecayFactor(%g);\n",heuristic,decayFactor_);
     479  else
     480    fprintf(fp,"4  %s.setDecayFactor(%g);\n",heuristic,decayFactor_);
     481  if (switches_!=0)
     482    fprintf(fp,"3  %s.setSwitches(%d);\n",heuristic,switches_);
     483  else
     484    fprintf(fp,"4  %s.setSwitches(%d);\n",heuristic,switches_);
     485  if (whereFrom_!=DEFAULT_WHERE)
     486    fprintf(fp,"3  %s.setWhereFrom(%d);\n",heuristic,whereFrom_);
     487  else
     488    fprintf(fp,"4  %s.setWhereFrom(%d);\n",heuristic,whereFrom_);
     489  if (shallowDepth_!=1)
     490    fprintf(fp,"3  %s.setShallowDepth(%d);\n",heuristic,shallowDepth_);
     491  else
     492    fprintf(fp,"4  %s.setShallowDepth(%d);\n",heuristic,shallowDepth_);
     493  if (howOftenShallow_!=1)
     494    fprintf(fp,"3  %s.setHowOftenShallow(%d);\n",heuristic,howOftenShallow_);
     495  else
     496    fprintf(fp,"4  %s.setHowOftenShallow(%d);\n",heuristic,howOftenShallow_);
     497  if (minDistanceToRun_!=1)
     498    fprintf(fp,"3  %s.setMinDistanceToRun(%d);\n",heuristic,minDistanceToRun_);
     499  else
     500    fprintf(fp,"4  %s.setMinDistanceToRun(%d);\n",heuristic,minDistanceToRun_);
    463501}
    464502// Destructor
     
    472510{
    473511  model_ = model;
     512}
     513/* Clone but ..
     514   type 0 clone solver, 1 clone continuous solver
     515   Add 2 to say without integer variables which are at low priority
     516   Add 4 to say quite likely infeasible so give up easily.*/
     517OsiSolverInterface *
     518CbcHeuristic::cloneBut(int type)
     519{
     520  OsiSolverInterface * solver;
     521  if ((type&1)==0||!model_->continuousSolver())
     522    solver = model_->solver()->clone();
     523  else
     524    solver = model_->continuousSolver()->clone();
     525#ifdef COIN_HAS_CLP
     526  OsiClpSolverInterface * clpSolver
     527    = dynamic_cast<OsiClpSolverInterface *> (solver);
     528#endif
     529  if ((type&2)!=0) {
     530    int n=model_->numberObjects();
     531    int priority = model_->continuousPriority();
     532    if (priority<COIN_INT_MAX) {
     533      for (int i=0;i<n;i++) {
     534        const OsiObject * obj = model_->object(i);
     535        const CbcSimpleInteger * thisOne =
     536          dynamic_cast <const CbcSimpleInteger *> (obj);
     537        if (thisOne) {
     538          int iColumn=thisOne->columnNumber();
     539          if (thisOne->priority()>=priority)
     540            solver->setContinuous(iColumn);
     541        }
     542      }
     543    }
     544#ifdef COIN_HAS_CLP
     545    if (clpSolver) {
     546      for (int i=0;i<n;i++) {
     547        const OsiObject * obj = model_->object(i);
     548        const CbcSimpleInteger * thisOne =
     549          dynamic_cast <const CbcSimpleInteger *> (obj);
     550        if (thisOne) {
     551          int iColumn=thisOne->columnNumber();
     552          if (clpSolver->isOptionalInteger(iColumn))
     553            clpSolver->setContinuous(iColumn);
     554        }
     555      }
     556    }
     557#endif
     558  }
     559#ifdef COIN_HAS_CLP
     560  if ((type&4)!=0&&clpSolver) {
     561    int options = clpSolver->getModelPtr()->moreSpecialOptions();
     562    clpSolver->getModelPtr()->setMoreSpecialOptions(options|64);
     563  }
     564#endif
     565  return solver;
    474566}
    475567// Whether to exit at once on gap
     
    477569CbcHeuristic::exitNow(double bestObjective) const
    478570{
    479   if ((switches_&1)==0)
     571  if ((switches_&2048)!=0) {
     572    // exit may be forced - but unset for next time
     573    switches_ &= ~2048;
     574    if ((switches_&1024)!=0)
     575      return true;
     576  } else if ((switches_&1)==0) {
    480577    return false;
     578  }
    481579  // See if can stop on gap
    482580  OsiSolverInterface * solver = model_->solver();
    483581  double bestPossibleObjective = solver->getObjValue()*solver->getObjSense();
    484   double testGap = CoinMax(model_->getAllowableGap(),
     582  double absGap = CoinMax(model_->getAllowableGap(),
     583                          model_->getHeuristicGap());
     584  double fracGap = CoinMax(model_->getAllowableFractionGap(),
     585                          model_->getHeuristicFractionGap());
     586  double testGap = CoinMax(absGap,fracGap*
    485587                           CoinMax(fabs(bestObjective),
    486                                    fabs(bestPossibleObjective))
    487                            *model_->getAllowableFractionGap());
     588                                   fabs(bestPossibleObjective)));
     589
    488590  if (bestObjective-bestPossibleObjective < testGap
    489591      && model_->getCutoffIncrement()>=0.0) {
     
    782884        if (numberNodes>=0) {
    783885          // normal
    784           model_->setSpecialOptions(saveModelOptions|2048);
     886          model.setSpecialOptions(saveModelOptions|2048);
    785887          if (logLevel<=1)
    786888            model.setLogLevel(0);
     
    799901          model.setMaximumCutPasses(CoinMin(10,model_->getMaximumCutPasses()));
    800902        } else {
    801           model_->setSpecialOptions(saveModelOptions);
     903          model.setSpecialOptions(saveModelOptions);
    802904          model_->messageHandler()->message(CBC_RESTART,model_->messages())
    803905            <<solver2->getNumRows()<<solver2->getNumCols()
     
    9931095            //printf("added seren\n");
    9941096          } else {
     1097            double value = model_->getMinimizationObjValue();
     1098            model.setCutoff(value+1.0e-7*(1.0+fabs(value)));
    9951099#ifdef CLP_INVESTIGATE
    9961100            printf("NOT added seren\n");
     
    11111215  model_->setSpecialOptions(saveModelOptions);
    11121216  model_->setLogLevel(logLevel);
     1217  if (returnCode==1||returnCode==2) {
     1218    OsiSolverInterface * solverC = model_->continuousSolver();
     1219    if (false&&solverC) {
     1220      const double * lower = solver->getColLower();
     1221      const double * upper = solver->getColUpper();
     1222      const double * lowerC = solverC->getColLower();
     1223      const double * upperC = solverC->getColUpper();
     1224      bool good=true;
     1225      for (int iColumn=0;iColumn<numberColumns;iColumn++) {
     1226        if (solverC->isInteger(iColumn)) {
     1227          if (lower[iColumn]>lowerC[iColumn]&&
     1228              upper[iColumn]<upperC[iColumn]) {
     1229            good=false;
     1230            printf("CUT - can't add\n");
     1231            break;
     1232          }
     1233        }
     1234      }
     1235      if (good) {
     1236        double * cut = new double [numberColumns];
     1237        int * which = new int [numberColumns];
     1238        double rhs=-1.0;
     1239        int n=0;
     1240        for (int iColumn=0;iColumn<numberColumns;iColumn++) {
     1241          if (solverC->isInteger(iColumn)) {
     1242            if (lower[iColumn]==upperC[iColumn]) {
     1243              rhs += lower[iColumn];
     1244              cut[n]=1.0;
     1245              which[n++]=iColumn;
     1246            } else if (upper[iColumn]==lowerC[iColumn]) {
     1247              rhs -= upper[iColumn];
     1248              cut[n]=-1.0;
     1249              which[n++]=iColumn;
     1250            }
     1251          }
     1252        }
     1253        printf("CUT has %d entries\n",n);
     1254        OsiRowCut newCut;
     1255        newCut.setLb(-COIN_DBL_MAX);
     1256        newCut.setUb(rhs);
     1257        newCut.setRow(n,which,cut,false);
     1258        model_->makeGlobalCut(newCut);
     1259        delete [] cut;
     1260        delete [] which;
     1261      }
     1262    }
     1263#ifdef COIN_DEVELOP
     1264    if (status==1)
     1265      printf("heuristic could add cut because infeasible (%s)\n",heuristicName_.c_str());
     1266    else if (status==2)
     1267      printf("heuristic could add cut because optimal (%s)\n",heuristicName_.c_str());
     1268#endif
     1269  }
    11131270  if (reset) {
    11141271    for (int iColumn=0;iColumn<numberColumns;iColumn++) {
     
    11201277#ifdef HISTORY_STATISTICS
    11211278  getHistoryStatistics_=true;
    1122 #endif
    1123 #ifdef COIN_DEVELOP
    1124   if (returnCode==1||returnCode==2) {
    1125     if (status==1)
    1126       printf("heuristic could add cut because infeasible (%s)\n",heuristicName_.c_str());
    1127     else if (status==2)
    1128       printf("heuristic could add cut because optimal (%s)\n",heuristicName_.c_str());
    1129   }
    11301279#endif
    11311280  solver->setHintParam(OsiDoReducePrint,takeHint,strength);
     
    13811530{
    13821531  // matrix and row copy will automatically be empty
    1383   seed_=1;
     1532  seed_=7654321;
    13841533  down_ = NULL;
    13851534  up_ = NULL;
     
    13931542  // Get a copy of original matrix (and by row for rounding);
    13941543  assert(model.solver());
    1395   matrix_ = *model.solver()->getMatrixByCol();
    1396   matrixByRow_ = *model.solver()->getMatrixByRow();
    1397   validate();
     1544  if (model.solver()->getNumRows()) {
     1545    matrix_ = *model.solver()->getMatrixByCol();
     1546    matrixByRow_ = *model.solver()->getMatrixByRow();
     1547    validate();
     1548  }
    13981549  down_ = NULL;
    13991550  up_ = NULL;
    14001551  equal_ = NULL;
    1401   seed_=1;
     1552  seed_=7654321;
    14021553}
    14031554
     
    22442395  // Get a copy of original matrix (and by row for rounding);
    22452396  assert(model_->solver());
    2246   matrix_ = *model_->solver()->getMatrixByCol();
    2247   matrixByRow_ = *model_->solver()->getMatrixByRow();
    2248   // make sure model okay for heuristic
    2249   validate();
     2397  if (model_->solver()->getNumRows()) {
     2398    matrix_ = *model_->solver()->getMatrixByCol();
     2399    matrixByRow_ = *model_->solver()->getMatrixByRow();
     2400    // make sure model okay for heuristic
     2401    validate();
     2402  }
    22502403}
    22512404// Validate model i.e. sets when_ to 0 if necessary (may be NULL)
     
    22532406CbcRounding::validate()
    22542407{
    2255   if (model_&&when()<10) {
     2408  if (model_&&(when()%100)<10) {
    22562409    if (model_->numberIntegers()!=
    22572410        model_->numberObjects()&&(model_->numberObjects()||
    2258                                   (model_->specialOptions()&1024)==0))
    2259       setWhen(0);
     2411                                  (model_->specialOptions()&1024)==0)) {
     2412      int numberOdd=0;
     2413      for (int i=0;i<model_->numberObjects();i++) {
     2414        if (!model_->object(i)->canDoHeuristics())
     2415          numberOdd++;
     2416      }
     2417      if (numberOdd)
     2418        setWhen(0);
     2419    }
    22602420  }
    22612421#ifdef NEW_ROUNDING
     
    24602620CbcHeuristicPartial::validate()
    24612621{
    2462   if (model_&&when()<10) {
     2622  if (model_&&(when()%100)<10) {
    24632623    if (model_->numberIntegers()!=
    24642624        model_->numberObjects())
     
    24672627}
    24682628bool
    2469 CbcHeuristicPartial::shouldHeurRun()
     2629CbcHeuristicPartial::shouldHeurRun(int /*whereFrom*/)
    24702630{
    24712631  return true;
Note: See TracChangeset for help on using the changeset viewer.