Ignore:
Timestamp:
Oct 8, 2006 7:33:47 PM (13 years ago)
Author:
forrest
Message:

towards common use with other solvers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/devel/Cbc/src/CbcNode.cpp

    r427 r439  
    1717#define CUTS
    1818#include "OsiSolverInterface.hpp"
     19#include "OsiChooseVariable.hpp"
    1920#include "OsiAuxInfo.hpp"
    2021#include "OsiSolverBranch.hpp"
     
    905906  double integerTolerance =
    906907    model->getDblParam(CbcModel::CbcIntegerTolerance);
     908  // point to useful information
     909  OsiBranchingInformation usefulInfo = model->usefulInformation();
     910  // and modify
     911  usefulInfo.depth_=depth_;
    907912  int i;
    908913  bool beforeSolution = model->getSolutionCount()==0;
     
    937942  if (!decision||dynamicBranchingObject)
    938943    decision = new CbcBranchDefaultDecision();
    939 
     944  decision->initialize(model);
    940945  CbcStrongInfo * choice = new CbcStrongInfo[maximumStrong];
    941946  for (i=0;i<numberColumns;i++) {
     
    993998      bool canDoOneHot=false;
    994999      for (i=0;i<numberObjects;i++) {
    995         CbcObject * object = model->modifiableObject(i);
     1000        OsiObject * object = model->modifiableObject(i);
    9961001        int preferredWay;
    997         double infeasibility = object->infeasibility(preferredWay);
     1002        double infeasibility = object->infeasibility(&usefulInfo,preferredWay);
    9981003        int priorityLevel = object->priority();
    9991004        if (hotstartSolution) {
     
    10011006          const CbcSimpleInteger * thisOne = dynamic_cast <const CbcSimpleInteger *> (object);
    10021007          if (thisOne) {
    1003             int iColumn = thisOne->modelSequence();
     1008            int iColumn = thisOne->columnNumber();
    10041009            bool canDoThisHot=true;
    10051010            double targetValue = hotstartSolution[iColumn];
     
    10801085            choice[iSmallest].upMovement=infeasibility;
    10811086            delete choice[iSmallest].possibleBranch;
    1082             choice[iSmallest].possibleBranch=object->createBranch(preferredWay);
     1087            CbcSimpleInteger * obj =
     1088              dynamic_cast <CbcSimpleInteger *>(object) ;
     1089            if (obj) {
     1090              choice[iSmallest].possibleBranch=obj->createBranch(solver,&usefulInfo,preferredWay);
     1091            } else {
     1092              CbcObject * obj =
     1093                dynamic_cast <CbcObject *>(object) ;
     1094              assert (obj);
     1095              choice[iSmallest].possibleBranch=obj->createBranch(preferredWay);
     1096            }
    10831097            numberStrong = CoinMax(numberStrong,iSmallest+1);
    10841098            // Save which object it was
     
    12811295        for (i=0;i<numberStrong;i++) {
    12821296          int iObject = choice[i].objectNumber;
    1283           const CbcObject * object = model->object(iObject);
     1297          const OsiObject * object = model->object(iObject);
    12841298          const CbcSimpleInteger * simple = dynamic_cast <const CbcSimpleInteger *> (object);
    1285           int iSequence = simple->modelSequence();
     1299          int iSequence = simple->columnNumber();
    12861300          newLower[i]= ceil(saveSolution[iSequence]);
    12871301          newUpper[i]= floor(saveSolution[iSequence]);
     
    13161330          for (i=0;i<numberStrong;i++) {
    13171331            int iObject = choice[i].objectNumber;
    1318             const CbcObject * object = model->object(iObject);
     1332            const OsiObject * object = model->object(iObject);
    13191333            const CbcSimpleInteger * simple = dynamic_cast <const CbcSimpleInteger *> (object);
    1320             int iSequence = simple->modelSequence();
     1334            int iSequence = simple->columnNumber();
    13211335            which[i]=iSequence;
    13221336            double * sol = outputSolution[2*i];
     
    16981712            CbcStrongInfo thisChoice = choice[i];
    16991713            choice[i].possibleBranch=NULL;
    1700             const CbcObject * object = model->object(thisChoice.objectNumber);
     1714            const OsiObject * object = model->object(thisChoice.objectNumber);
    17011715            int preferredWay;
    1702             double infeasibility = object->infeasibility(preferredWay);
     1716            double infeasibility = object->infeasibility(&usefulInfo,preferredWay);
    17031717            if (!infeasibility) {
    17041718              // take out
     
    18941908    auxiliaryInfo = model->solverCharacteristics();
    18951909  }
     1910  // point to useful information
     1911  OsiBranchingInformation usefulInfo = model->usefulInformation();
     1912  // and modify
     1913  usefulInfo.depth_=depth_;
    18961914  assert (auxiliaryInfo);
    18971915  //assert(objectiveValue_ == solver->getObjSense()*solver->getObjValue());
     
    19731991    int i;
    19741992    for ( i=0;i<numberObjects;i++) {
    1975       CbcObject * object = model->modifiableObject(i);
     1993      OsiObject * object = model->modifiableObject(i);
    19761994      CbcSimpleIntegerDynamicPseudoCost * dynamicObject =
    19771995        dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(object) ;
     
    20422060      int i;
    20432061      for ( i=0;i<numberObjects;i++) {
    2044         CbcObject * object = model->modifiableObject(i);
     2062        OsiObject * object = model->modifiableObject(i);
    20452063        CbcSimpleIntegerDynamicPseudoCost * dynamicObject =
    20462064          dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(object) ;
     
    20642082        averageDown=1.0;
    20652083      for ( i=0;i<numberObjects;i++) {
    2066         CbcObject * object = model->modifiableObject(i);
     2084        OsiObject * object = model->modifiableObject(i);
    20672085        CbcSimpleIntegerDynamicPseudoCost * dynamicObject =
    20682086          dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(object) ;
     
    21662184#define PRINT_STUFF -1
    21672185      for (i=0;i<numberObjects;i++) {
    2168         CbcObject * object = model->modifiableObject(i);
     2186        OsiObject * object = model->modifiableObject(i);
    21692187        CbcSimpleIntegerDynamicPseudoCost * dynamicObject =
    21702188          dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(object) ;
    21712189        assert(dynamicObject);
    21722190        int preferredWay;
    2173         double infeasibility = object->infeasibility(preferredWay);
     2191        double infeasibility = object->infeasibility(&usefulInfo,preferredWay);
    21742192        int priorityLevel = object->priority();
    21752193#define ZERO_ONE 0
     
    24332451    if (hitMaxTime||numberPassesLeft<=0||(!numberNotTrusted&&false)||branchingMethod==11) {
    24342452      int iObject = whichObject[bestChoice];
    2435       CbcObject * object = model->modifiableObject(iObject);
     2453      OsiObject * object = model->modifiableObject(iObject);
    24362454      int preferredWay;
    2437       object->infeasibility(preferredWay);
    2438       branch_=object->createBranch(preferredWay);
    2439       branch_->way(preferredWay);
     2455      object->infeasibility(&usefulInfo,preferredWay);
     2456      CbcSimpleInteger * obj =
     2457        dynamic_cast <CbcSimpleInteger *>(object) ;
     2458      if (obj) {
     2459        branch_=obj->createBranch(solver,&usefulInfo,preferredWay);
     2460      } else {
     2461        CbcObject * obj =
     2462          dynamic_cast <CbcObject *>(object) ;
     2463        assert (obj);
     2464        branch_=obj->createBranch(preferredWay);
     2465      }
     2466      {
     2467        CbcBranchingObject * branchObj =
     2468          dynamic_cast <CbcBranchingObject *>(branch_) ;
     2469        assert (branchObj);
     2470        branchObj->way(preferredWay);
     2471      }
    24402472      delete ws;
    24412473      ws=NULL;
     
    24872519          int j = objectMark[i];
    24882520          int iObject = whichObject[j];
    2489           CbcObject * object = model->modifiableObject(iObject);
     2521          OsiObject * object = model->modifiableObject(iObject);
    24902522          CbcSimpleIntegerDynamicPseudoCost * dynamicObject =
    24912523            dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(object) ;
     
    26052637      }
    26062638#endif
    2607       if (depth_<10&&numberStrong) {
     2639      if (depth_<8&&numberStrong) {
    26082640        if (searchStrategy!=2) {
    26092641          doQuickly=false;
     
    27222754      }
    27232755      //printf("skipAll %c doQuickly %c numberTest %d numberTest2 %d numberNot %d\n",
    2724       //     skipAll ? 'Y' : 'N',doQuickly ? 'Y' : 'N',numberTest,numberTest2,numberNotTrusted);
     2756      //   skipAll ? 'Y' : 'N',doQuickly ? 'Y' : 'N',numberTest,numberTest2,numberNotTrusted);
    27252757      // See if we want mini tree
    27262758      bool wantMiniTree=false;
     
    27352767        CbcStrongInfo choice;
    27362768        int iObject = whichObject[iDo];
    2737         CbcObject * object = model->modifiableObject(iObject);
     2769        OsiObject * object = model->modifiableObject(iObject);
    27382770        CbcSimpleIntegerDynamicPseudoCost * dynamicObject =
    27392771          dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(object) ;
    27402772        int iColumn = dynamicObject->columnNumber();
    27412773        int preferredWay;
    2742         object->infeasibility(preferredWay);
    2743         choice.possibleBranch=object->createBranch(preferredWay);
     2774        object->infeasibility(&usefulInfo,preferredWay);
     2775        CbcSimpleInteger * obj =
     2776          dynamic_cast <CbcSimpleInteger *>(object) ;
     2777        if (obj) {
     2778          choice.possibleBranch=obj->createBranch(solver,&usefulInfo,preferredWay);
     2779        } else {
     2780          CbcObject * obj =
     2781            dynamic_cast <CbcObject *>(object) ;
     2782          assert (obj);
     2783          choice.possibleBranch=obj->createBranch(preferredWay);
     2784        }
    27442785        // Save which object it was
    27452786        choice.objectNumber=iObject;
     
    30803121            //assert(gap>0.0);
    30813122            double factor = 1.0; //changeFactor/CoinMin(gap,100.0);
    3082             int betterWay = decision->betterBranch(choice.possibleBranch,
    3083                                                    branch_,
    3084                                                    choice.upMovement*factor,
    3085                                                    choice.numIntInfeasUp ,
    3086                                                    choice.downMovement*factor,
    3087                                                    choice.numIntInfeasDown );
     3123            int betterWay;
     3124            {
     3125              CbcBranchingObject * branchObj =
     3126                dynamic_cast <CbcBranchingObject *>(branch_) ;
     3127              if (branch_)
     3128                assert (branchObj);
     3129              betterWay = decision->betterBranch(choice.possibleBranch,
     3130                                                     branchObj,
     3131                                                     choice.upMovement*factor,
     3132                                                     choice.numIntInfeasUp ,
     3133                                                     choice.downMovement*factor,
     3134                                                     choice.numIntInfeasDown );
     3135            }
    30883136            if (wantMiniTree) {
    30893137              double criterion = decision->getBestCriterion();
     
    31063154              branch_ = choice.possibleBranch;
    31073155              choice.possibleBranch=NULL;
    3108               branch_->way(betterWay);
     3156              {
     3157                CbcBranchingObject * branchObj =
     3158                  dynamic_cast <CbcBranchingObject *>(branch_) ;
     3159                assert (branchObj);
     3160                branchObj->way(preferredWay);
     3161              }
    31093162              if (couldChooseFirst)
    31103163                printf("choosing %d way %d\n",iDo,betterWay);
     
    32413294          for ( int jDo=iDo+1;jDo<numberToDo;jDo++) {
    32423295            int iObject = whichObject[iDo];
    3243             CbcObject * object = model->modifiableObject(iObject);
     3296            OsiObject * object = model->modifiableObject(iObject);
    32443297            CbcSimpleIntegerDynamicPseudoCost * dynamicObject =
    32453298              dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(object) ;
     
    33123365              // See if candidate still possible
    33133366              if (branch_) {
    3314                 const CbcObject * object = model->object(bestChoice);
     3367                const OsiObject * object = model->object(bestChoice);
    33153368                int preferredWay;
    3316                 double infeasibility = object->infeasibility(preferredWay);
     3369                double infeasibility = object->infeasibility(&usefulInfo,preferredWay);
    33173370                if (!infeasibility) {
    33183371                  // take out
     
    33203373                  branch_=NULL;
    33213374                } else {
    3322                   branch_->way(preferredWay);
     3375                  CbcBranchingObject * branchObj =
     3376                    dynamic_cast <CbcBranchingObject *>(branch_) ;
     3377                  assert (branchObj);
     3378                  branchObj->way(preferredWay);
    33233379                }
    33243380              }
     
    33853441    for (int iDo=0;iDo<numberMini;iDo++) {
    33863442      int iObject = whichObject[iDo];
    3387       CbcObject * object = model->modifiableObject(iObject);
    3388       OsiSolverBranch * oneBranch = object->solverBranch();
     3443      OsiObject * object = model->modifiableObject(iObject);
     3444      CbcSimpleInteger * obj =
     3445        dynamic_cast <CbcSimpleInteger *>(object) ;
     3446      OsiSolverBranch * oneBranch;
     3447      if (obj) {
     3448        oneBranch = obj->solverBranch(solver,&usefulInfo);
     3449      } else {
     3450        CbcObject * obj =
     3451          dynamic_cast <CbcObject *>(object) ;
     3452        assert (obj);
     3453        oneBranch = obj->solverBranch();
     3454      }
    33893455      branches[iDo]=*oneBranch;
    33903456      delete oneBranch;
     
    34653531  int numberToFix=0;
    34663532  int numberToDo=0;
     3533  double integerTolerance =
     3534    model->getDblParam(CbcModel::CbcIntegerTolerance);
     3535  // point to useful information
     3536  OsiBranchingInformation usefulInfo = model->usefulInformation();
     3537  // and modify
     3538  usefulInfo.depth_=depth_;
    34673539     
    34683540  // compute current state
     
    34963568  numberToDo=0;
    34973569  for (i=0;i<numberObjects;i++) {
    3498     CbcObject * object = model->modifiableObject(i);
     3570    OsiObject * object = model->modifiableObject(i);
    34993571    CbcSimpleIntegerDynamicPseudoCost * dynamicObject =
    35003572      dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(object) ;
     
    35023574      continue;
    35033575    int preferredWay;
    3504     double infeasibility = object->infeasibility(preferredWay);
     3576    double infeasibility = object->infeasibility(&usefulInfo,preferredWay);
    35053577    int iColumn = dynamicObject->columnNumber();
    35063578    if (saveUpper[iColumn]==saveLower[iColumn])
     
    35253597  //double distanceToCutoff=model->getCutoff()-objectiveValue_;
    35263598  double * currentSolution = model->currentSolution();
    3527   double integerTolerance =
    3528     model->getDblParam(CbcModel::CbcIntegerTolerance);
    35293599  double objMin = 1.0e50;
    35303600  double objMax = -1.0e50;
     
    35343604    CbcStrongInfo choice;
    35353605    int iObject = whichObject[iDo];
    3536     CbcObject * object = model->modifiableObject(iObject);
     3606    OsiObject * object = model->modifiableObject(iObject);
    35373607    CbcSimpleIntegerDynamicPseudoCost * dynamicObject =
    35383608      dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(object) ;
    35393609    int iColumn = dynamicObject->columnNumber();
    35403610    int preferredWay;
    3541     object->infeasibility(preferredWay);
     3611    object->infeasibility(&usefulInfo,preferredWay);
    35423612    double value = currentSolution[iColumn];
    35433613    double nearest = floor(value+0.5);
     
    35573627    }
    35583628    double upperValue = lowerValue+1.0;
    3559     choice.possibleBranch=object->createBranch(preferredWay);
     3629    CbcSimpleInteger * obj =
     3630      dynamic_cast <CbcSimpleInteger *>(object) ;
     3631    if (obj) {
     3632      choice.possibleBranch=obj->createBranch(solver,&usefulInfo,preferredWay);
     3633    } else {
     3634      CbcObject * obj =
     3635        dynamic_cast <CbcObject *>(object) ;
     3636      assert (obj);
     3637      choice.possibleBranch=obj->createBranch(preferredWay);
     3638    }
    35603639    currentSolution[iColumn]=value;
    35613640    // Save which object it was
     
    39013980CbcNode::branch()
    39023981{
    3903   double changeInGuessed=branch_->branch(true);
     3982  double changeInGuessed=branch_->branch();
    39043983  guessedObjectiveValue_+= changeInGuessed;
    39053984  //#define PRINTIT
     
    39093988  int parentNodeNumber = -1;
    39103989  //CbcBranchingObject * object1 = branch_->object_;
    3911   //CbcObject * object = object1->
     3990  //OsiObject * object = object1->
    39123991  //int sequence = object->columnNumber);
    39133992  int id=-1;
     
    39264005  return nodeInfo_->branchedOn();
    39274006}
     4007/* Active arm of the attached OsiBranchingObject.
     4008 
     4009   In the simplest instance, coded -1 for the down arm of the branch, +1 for
     4010   the up arm. But see OsiBranchingObject::way()
     4011     Use nodeInfo--.numberBranchesLeft_ to see how active
     4012*/
     4013int
     4014CbcNode::way() const
     4015{
     4016  if (branch_) {
     4017    CbcBranchingObject * obj =
     4018      dynamic_cast <CbcBranchingObject *>(branch_) ;
     4019    assert (obj);
     4020    return obj->way();
     4021  } else {
     4022    return 0;
     4023  }
     4024}
     4025/* Create a branching object for the node
     4026
     4027    The routine scans the object list of the model and selects a set of
     4028    unsatisfied objects as candidates for branching. The candidates are
     4029    evaluated, and an appropriate branch object is installed.
     4030
     4031    The numberPassesLeft is decremented to stop fixing one variable each time
     4032    and going on and on (e.g. for stock cutting, air crew scheduling)
     4033
     4034    If evaluation determines that an object is monotone or infeasible,
     4035    the routine returns immediately. In the case of a monotone object,
     4036    the branch object has already been called to modify the model.
     4037
     4038    Return value:
     4039    <ul>
     4040      <li>  0: A branching object has been installed
     4041      <li> -1: A monotone object was discovered
     4042      <li> -2: An infeasible object was discovered
     4043    </ul>
     4044    Branch state:
     4045    <ul>
     4046      <li> -1: start
     4047      <li> -1: A monotone object was discovered
     4048      <li> -2: An infeasible object was discovered
     4049    </ul>
     4050*/
     4051int
     4052CbcNode::chooseOsiBranch (CbcModel * model,
     4053                          CbcNode * lastNode,
     4054                          OsiBranchingInformation * usefulInfo,
     4055                          int branchState)
     4056{
     4057  int returnStatus=0;
     4058  if (lastNode)
     4059    depth_ = lastNode->depth_+1;
     4060  else
     4061    depth_ = 0;
     4062  objectiveValue_ = usefulInfo->solver_->getObjValue()*usefulInfo->solver_->getObjSense();
     4063  usefulInfo->objectiveValue_ = objectiveValue_;
     4064  usefulInfo->depth_ = depth_;
     4065  OsiChooseVariable * choose = model->branchingMethod()->chooseMethod();
     4066  int numberUnsatisfied=-1;
     4067  if (branchState<0) {
     4068    // initialize
     4069    numberUnsatisfied = choose->setupList(usefulInfo,true);
     4070    numberUnsatisfied_ = numberUnsatisfied;
     4071    branchState=0;
     4072  }
     4073  // unset best
     4074  int best=-1;
     4075  choose->setBestObject(-1);
     4076  if (numberUnsatisfied) {
     4077    if (branchState>0||!choose->numberOnList()) {
     4078      // we need to return at once - don't do strong branching or anything
     4079      if (choose->numberOnList()||!choose->numberStrong()) {
     4080        best = choose->candidates()[0];
     4081        choose->setBestObject(best);
     4082        choose->updateInformation(usefulInfo);
     4083      } else {
     4084        // nothing on list - need to try again - keep any solution
     4085        numberUnsatisfied = choose->setupList(usefulInfo, false);
     4086        numberUnsatisfied_ = numberUnsatisfied;
     4087        if (numberUnsatisfied) {
     4088          best = choose->candidates()[0];
     4089          choose->setBestObject(best);
     4090          choose->updateInformation(usefulInfo);
     4091        }
     4092      }
     4093    } else {
     4094      // carry on with strong branching or whatever
     4095      int returnCode = choose->chooseVariable(usefulInfo);
     4096      if (returnCode>1) {
     4097        // can fix
     4098        returnStatus=-1;
     4099      } else if (returnCode==-1) {
     4100        // infeasible
     4101        returnStatus=-2;
     4102      } else if (returnCode==0) {
     4103        // normal
     4104        returnStatus=0;
     4105        numberUnsatisfied=1;
     4106      } else {
     4107        // ones on list satisfied - double check
     4108        numberUnsatisfied = choose->setupList(usefulInfo, false);
     4109        numberUnsatisfied_ = numberUnsatisfied;
     4110        if (numberUnsatisfied) {
     4111          best = choose->candidates()[0];
     4112          choose->setBestObject(best);
     4113          choose->updateInformation( usefulInfo);
     4114        }
     4115      }
     4116    }
     4117  }
     4118  delete branch_;
     4119  branch_ = NULL;
     4120  guessedObjectiveValue_ = objectiveValue_; // for now
     4121  if (!returnStatus) {
     4122    if (numberUnsatisfied) {
     4123      // create branching object
     4124      const OsiObject * obj = model->object(choose->bestObject());
     4125      //const OsiSolverInterface * solver = usefulInfo->solver_;
     4126      branch_ = obj->createBranch(model->solver(),obj->whichWay());
     4127    }
     4128  }
     4129  return returnStatus;
     4130}
Note: See TracChangeset for help on using the changeset viewer.