Ignore:
Timestamp:
Dec 3, 2009 3:48:32 PM (10 years ago)
Author:
EdwinStraver
Message:

Combined CbcGeneralDepth?, CbcGeneralBranchingObject? and CbcOneGeneralBranchingObject?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/sandbox/Cbc/src/CbcGeneralDepth.cpp

    r1293 r1346  
    381381    return branch;
    382382}
     383
     384// Default Constructor
     385CbcGeneralBranchingObject::CbcGeneralBranchingObject()
     386        : CbcBranchingObject(),
     387        subProblems_(NULL),
     388        node_(NULL),
     389        numberSubProblems_(0),
     390        numberSubLeft_(0),
     391        whichNode_(-1),
     392        numberRows_(0)
     393{
     394    //  printf("CbcGeneral %x default constructor\n",this);
     395}
     396
     397// Useful constructor
     398CbcGeneralBranchingObject::CbcGeneralBranchingObject (CbcModel * model)
     399        : CbcBranchingObject(model, -1, -1, 0.5),
     400        subProblems_(NULL),
     401        node_(NULL),
     402        numberSubProblems_(0),
     403        numberSubLeft_(0),
     404        whichNode_(-1),
     405        numberRows_(0)
     406{
     407    //printf("CbcGeneral %x useful constructor\n",this);
     408}
     409
     410// Copy constructor
     411CbcGeneralBranchingObject::CbcGeneralBranchingObject ( const CbcGeneralBranchingObject & rhs)
     412        : CbcBranchingObject(rhs),
     413        subProblems_(NULL),
     414        node_(rhs.node_),
     415        numberSubProblems_(rhs.numberSubProblems_),
     416        numberSubLeft_(rhs.numberSubLeft_),
     417        whichNode_(rhs.whichNode_),
     418        numberRows_(rhs.numberRows_)
     419{
     420    abort();
     421    if (numberSubProblems_) {
     422        subProblems_ = new CbcSubProblem[numberSubProblems_];
     423        for (int i = 0; i < numberSubProblems_; i++)
     424            subProblems_[i] = rhs.subProblems_[i];
     425    }
     426}
     427
     428// Assignment operator
     429CbcGeneralBranchingObject &
     430CbcGeneralBranchingObject::operator=( const CbcGeneralBranchingObject & rhs)
     431{
     432    if (this != &rhs) {
     433        abort();
     434        CbcBranchingObject::operator=(rhs);
     435        delete [] subProblems_;
     436        numberSubProblems_ = rhs.numberSubProblems_;
     437        numberSubLeft_ = rhs.numberSubLeft_;
     438        whichNode_ = rhs.whichNode_;
     439        numberRows_ = rhs.numberRows_;
     440        if (numberSubProblems_) {
     441            subProblems_ = new CbcSubProblem[numberSubProblems_];
     442            for (int i = 0; i < numberSubProblems_; i++)
     443                subProblems_[i] = rhs.subProblems_[i];
     444        } else {
     445            subProblems_ = NULL;
     446        }
     447        node_ = rhs.node_;
     448    }
     449    return *this;
     450}
     451CbcBranchingObject *
     452CbcGeneralBranchingObject::clone() const
     453{
     454    return (new CbcGeneralBranchingObject(*this));
     455}
     456
     457
     458// Destructor
     459CbcGeneralBranchingObject::~CbcGeneralBranchingObject ()
     460{
     461    //printf("CbcGeneral %x destructor\n",this);
     462    delete [] subProblems_;
     463}
     464bool doingDoneBranch = false;
     465double
     466CbcGeneralBranchingObject::branch()
     467{
     468    double cutoff = model_->getCutoff();
     469    //printf("GenB %x whichNode %d numberLeft %d which %d\n",
     470    // this,whichNode_,numberBranchesLeft(),branchIndex());
     471    if (whichNode_ < 0) {
     472        assert (node_);
     473        bool applied = false;
     474        while (numberBranchesLeft()) {
     475            int which = branchIndex();
     476            decrementNumberBranchesLeft();
     477            CbcSubProblem * thisProb = subProblems_ + which;
     478            if (thisProb->objectiveValue_ < cutoff) {
     479                //printf("branch %x (sub %x) which now %d\n",this,
     480                //     subProblems_,which);
     481                OsiSolverInterface * solver = model_->solver();
     482                thisProb->apply(solver);
     483                OsiClpSolverInterface * clpSolver
     484                = dynamic_cast<OsiClpSolverInterface *> (solver);
     485                assert (clpSolver);
     486                // Move status to basis
     487                clpSolver->setWarmStart(NULL);
     488                //ClpSimplex * simplex = clpSolver->getModelPtr();
     489                node_->setObjectiveValue(thisProb->objectiveValue_);
     490                node_->setSumInfeasibilities(thisProb->sumInfeasibilities_);
     491                node_->setNumberUnsatisfied(thisProb->numberInfeasibilities_);
     492                applied = true;
     493                doingDoneBranch = true;
     494                break;
     495            } else if (numberBranchesLeft()) {
     496                node_->nodeInfo()->branchedOn() ;
     497            }
     498        }
     499        if (!applied) {
     500            // no good one
     501            node_->setObjectiveValue(cutoff + 1.0e20);
     502            node_->setSumInfeasibilities(1.0);
     503            node_->setNumberUnsatisfied(1);
     504            assert (whichNode_ < 0);
     505        }
     506    } else {
     507        decrementNumberBranchesLeft();
     508        CbcSubProblem * thisProb = subProblems_ + whichNode_;
     509        assert (thisProb->objectiveValue_ < cutoff);
     510        OsiSolverInterface * solver = model_->solver();
     511        thisProb->apply(solver);
     512        //OsiClpSolverInterface * clpSolver
     513        //= dynamic_cast<OsiClpSolverInterface *> (solver);
     514        //assert (clpSolver);
     515        // Move status to basis
     516        //clpSolver->setWarmStart(NULL);
     517    }
     518    return 0.0;
     519}
     520/* Double checks in case node can change its mind!
     521   Can change objective etc */
     522void
     523CbcGeneralBranchingObject::checkIsCutoff(double cutoff)
     524{
     525    assert (node_);
     526    int first = branchIndex();
     527    int last = first + numberBranchesLeft();
     528    for (int which = first; which < last; which++) {
     529        CbcSubProblem * thisProb = subProblems_ + which;
     530        if (thisProb->objectiveValue_ < cutoff) {
     531            node_->setObjectiveValue(thisProb->objectiveValue_);
     532            node_->setSumInfeasibilities(thisProb->sumInfeasibilities_);
     533            node_->setNumberUnsatisfied(thisProb->numberInfeasibilities_);
     534            break;
     535        }
     536    }
     537}
     538// Print what would happen
     539void
     540CbcGeneralBranchingObject::print()
     541{
     542    //printf("CbcGeneralObject has %d subproblems\n",numberSubProblems_);
     543}
     544// Fill in current objective etc
     545void
     546CbcGeneralBranchingObject::state(double & objectiveValue,
     547                                 double & sumInfeasibilities,
     548                                 int & numberUnsatisfied, int which) const
     549{
     550    assert (which >= 0 && which < numberSubProblems_);
     551    const CbcSubProblem * thisProb = subProblems_ + which;
     552    objectiveValue = thisProb->objectiveValue_;
     553    sumInfeasibilities = thisProb->sumInfeasibilities_;
     554    numberUnsatisfied = thisProb->numberInfeasibilities_;
     555}
     556/** Compare the original object of \c this with the original object of \c
     557    brObj. Assumes that there is an ordering of the original objects.
     558    This method should be invoked only if \c this and brObj are of the same
     559    type.
     560    Return negative/0/positive depending on whether \c this is
     561    smaller/same/larger than the argument.
     562*/
     563int
     564CbcGeneralBranchingObject::compareOriginalObject
     565(const CbcBranchingObject* /*brObj*/) const
     566{
     567    throw("must implement");
     568}
     569
     570/** Compare the \c this with \c brObj. \c this and \c brObj must be os the
     571    same type and must have the same original object, but they may have
     572    different feasible regions.
     573    Return the appropriate CbcRangeCompare value (first argument being the
     574    sub/superset if that's the case). In case of overlap (and if \c
     575    replaceIfOverlap is true) replace the current branching object with one
     576    whose feasible region is the overlap.
     577*/
     578CbcRangeCompare
     579CbcGeneralBranchingObject::compareBranchingObject
     580(const CbcBranchingObject* /*brObj*/, const bool /*replaceIfOverlap*/)
     581{
     582    throw("must implement");
     583}
     584
     585// Default Constructor
     586CbcOneGeneralBranchingObject::CbcOneGeneralBranchingObject()
     587        : CbcBranchingObject(),
     588        object_(NULL),
     589        whichOne_(-1)
     590{
     591    //printf("CbcOneGeneral %x default constructor\n",this);
     592}
     593
     594// Useful constructor
     595CbcOneGeneralBranchingObject::CbcOneGeneralBranchingObject (CbcModel * model,
     596        CbcGeneralBranchingObject * object,
     597        int whichOne)
     598        : CbcBranchingObject(model, -1, -1, 0.5),
     599        object_(object),
     600        whichOne_(whichOne)
     601{
     602    //printf("CbcOneGeneral %x useful constructor object %x %d left\n",this,
     603    //   object_,object_->numberSubLeft_);
     604    numberBranches_ = 1;
     605}
     606
     607// Copy constructor
     608CbcOneGeneralBranchingObject::CbcOneGeneralBranchingObject ( const CbcOneGeneralBranchingObject & rhs)
     609        : CbcBranchingObject(rhs),
     610        object_(rhs.object_),
     611        whichOne_(rhs.whichOne_)
     612{
     613}
     614
     615// Assignment operator
     616CbcOneGeneralBranchingObject &
     617CbcOneGeneralBranchingObject::operator=( const CbcOneGeneralBranchingObject & rhs)
     618{
     619    if (this != &rhs) {
     620        CbcBranchingObject::operator=(rhs);
     621        object_ = rhs.object_;
     622        whichOne_ = rhs.whichOne_;
     623    }
     624    return *this;
     625}
     626CbcBranchingObject *
     627CbcOneGeneralBranchingObject::clone() const
     628{
     629    return (new CbcOneGeneralBranchingObject(*this));
     630}
     631
     632
     633// Destructor
     634CbcOneGeneralBranchingObject::~CbcOneGeneralBranchingObject ()
     635{
     636    //printf("CbcOneGeneral %x destructor object %x %d left\n",this,
     637    // object_,object_->numberSubLeft_);
     638    assert (object_->numberSubLeft_ > 0 &&
     639            object_->numberSubLeft_ < 1000000);
     640    if (!object_->decrementNumberLeft()) {
     641        // printf("CbcGeneral %x yy destructor\n",object_);
     642        delete object_;
     643    }
     644}
     645double
     646CbcOneGeneralBranchingObject::branch()
     647{
     648    assert (numberBranchesLeft());
     649    decrementNumberBranchesLeft();
     650    assert (!numberBranchesLeft());
     651    object_->setWhichNode(whichOne_);
     652    object_->branch();
     653    return 0.0;
     654}
     655/* Double checks in case node can change its mind!
     656   Can change objective etc */
     657void
     658CbcOneGeneralBranchingObject::checkIsCutoff(double /*cutoff*/)
     659{
     660    assert (numberBranchesLeft());
     661}
     662// Print what would happen
     663void
     664CbcOneGeneralBranchingObject::print()
     665{
     666    //printf("CbcOneGeneralObject has 1 subproblem\n");
     667}
     668/** Compare the original object of \c this with the original object of \c
     669    brObj. Assumes that there is an ordering of the original objects.
     670    This method should be invoked only if \c this and brObj are of the same
     671    type.
     672    Return negative/0/positive depending on whether \c this is
     673    smaller/same/larger than the argument.
     674*/
     675int
     676CbcOneGeneralBranchingObject::compareOriginalObject
     677(const CbcBranchingObject* /*brObj*/) const
     678{
     679    throw("must implement");
     680}
     681
     682/** Compare the \c this with \c brObj. \c this and \c brObj must be os the
     683    same type and must have the same original object, but they may have
     684    different feasible regions.
     685    Return the appropriate CbcRangeCompare value (first argument being the
     686    sub/superset if that's the case). In case of overlap (and if \c
     687    replaceIfOverlap is true) replace the current branching object with one
     688    whose feasible region is the overlap.
     689*/
     690CbcRangeCompare
     691CbcOneGeneralBranchingObject::compareBranchingObject
     692(const CbcBranchingObject* /*brObj*/, const bool /*replaceIfOverlap*/)
     693{
     694    throw("must implement");
     695}
    383696#endif
Note: See TracChangeset for help on using the changeset viewer.