# Changeset 2469

Ignore:
Timestamp:
Jan 6, 2019 6:17:46 PM (3 months ago)
Message:

formatting

Location:
trunk/Cbc
Files:
58 edited

Unmodified
Removed
• ## trunk/Cbc/MSVisualStudio/v10/CbcWithInstalledLibraries/main.cpp

 r1989 #include int main() { int main() { const int numcols = 2; const int numrows = 1; double obj[] = { 1.0, 1.0}; // obj: Max x0 + x1 double obj[] = { 1.0, 1.0 }; // obj: Max x0 + x1 // Column-major sparse "A" matrix: x0 + 2 x1 <= 3.9 int start[] = {0, 1, 2};      // where in index columns start (?) int index[] = {0, 0};        // row indexs for the columns double values[] = {1.0, 2.0}; // the values in the sparse matrix double rowlb[]  = {0.0}; double rowub[]  = {3.9}; int start[] = { 0, 1, 2 }; // where in index columns start (?) int index[] = { 0, 0 }; // row indexs for the columns double values[] = { 1.0, 2.0 }; // the values in the sparse matrix double rowlb[] = { 0.0 }; double rowub[] = { 3.9 }; //          0 <= x0 <= 10 and integer //          0 <= x1 <= 10 double collb[] = {0.0, 0.0}; double colub[] = {10.0, 10.0}; double collb[] = { 0.0, 0.0 }; double colub[] = { 10.0, 10.0 }; OsiClpSolverInterface model; model.loadProblem(numcols, numrows, start, index, values, collb, colub, obj, rowlb, rowub); model.loadProblem(numcols, numrows, start, index, values, collb, colub, obj, rowlb, rowub); model.setInteger(0); // Sets x0 to integer model.setObjSense(-1.0); // Maximise
• ## trunk/Cbc/examples/CbcBranchFollow2.cpp

 r1898 #include "CoinSort.hpp" #include "CoinError.hpp" // Default Constructor CbcFollowOn2::CbcFollowOn2 () : CbcObject(), rhs_(NULL), maximumRhs_(1) // Default Constructor CbcFollowOn2::CbcFollowOn2() : CbcObject() , rhs_(NULL) , maximumRhs_(1) { } // Useful constructor CbcFollowOn2::CbcFollowOn2 (CbcModel * model) CbcFollowOn2::CbcFollowOn2(CbcModel *model) : CbcObject(model) { assert (model); OsiSolverInterface * solver = model_->solver(); assert(model); OsiSolverInterface *solver = model_->solver(); matrix_ = *solver->getMatrixByCol(); matrix_.removeGaps(); matrixByRow_ = *solver->getMatrixByRow(); int numberRows = matrix_.getNumRows(); maximumRhs_ =1; maximumRhs_ = 1; rhs_ = new int[numberRows]; int i; const double * rowLower = solver->getRowLower(); const double * rowUpper = solver->getRowUpper(); const double *rowLower = solver->getRowLower(); const double *rowUpper = solver->getRowUpper(); // Row copy const double * elementByRow = matrixByRow_.getElements(); const int * column = matrixByRow_.getIndices(); const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts(); const int * rowLength = matrixByRow_.getVectorLengths(); for (i=0;i=1.0&&value<100.0) { // check elements bool good=true; for (int j=rowStart[i];jisInteger(iColumn)) good=false; double elValue = elementByRow[j]; if (floor(elValue)!=elValue||elValue<1.0) good=false; } if (good) rhs_[i]=(int) value; } } } } // Copy constructor CbcFollowOn2::CbcFollowOn2 ( const CbcFollowOn2 & rhs) :CbcObject(rhs), matrix_(rhs.matrix_), matrixByRow_(rhs.matrixByRow_), maximumRhs_(rhs.maximumRhs_) if (value == rowUpper[i]) { if (floor(value) == value && value >= 1.0 && value < 100.0) { // check elements bool good = true; for (int j = rowStart[i]; j < rowStart[i] + rowLength[i]; j++) { int iColumn = column[j]; if (!solver->isInteger(iColumn)) good = false; double elValue = elementByRow[j]; if (floor(elValue) != elValue || elValue < 1.0) good = false; } if (good) rhs_[i] = (int)value; } } } } // Copy constructor CbcFollowOn2::CbcFollowOn2(const CbcFollowOn2 &rhs) : CbcObject(rhs) , matrix_(rhs.matrix_) , matrixByRow_(rhs.matrixByRow_) , maximumRhs_(rhs.maximumRhs_) { int numberRows = matrix_.getNumRows(); rhs_= CoinCopyOfArray(rhs.rhs_,numberRows); rhs_ = CoinCopyOfArray(rhs.rhs_, numberRows); } } // Assignment operator CbcFollowOn2 & CbcFollowOn2::operator=( const CbcFollowOn2& rhs) { if (this!=&rhs) { // Assignment operator CbcFollowOn2 & CbcFollowOn2::operator=(const CbcFollowOn2 &rhs) { if (this != &rhs) { CbcObject::operator=(rhs); delete [] rhs_; delete[] rhs_; matrix_ = rhs.matrix_; matrixByRow_ = rhs.matrixByRow_; int numberRows = matrix_.getNumRows(); rhs_= CoinCopyOfArray(rhs.rhs_,numberRows); rhs_ = CoinCopyOfArray(rhs.rhs_, numberRows); maximumRhs_ = rhs.maximumRhs_; } } // Destructor CbcFollowOn2::~CbcFollowOn2 () { delete [] rhs_; // Destructor CbcFollowOn2::~CbcFollowOn2() { delete[] rhs_; } /* As some computation is needed in more than one place - returns row. Also returns other row and effective rhs (so we can know if cut) */ int CbcFollowOn2::gutsOfFollowOn2(int & otherRow, int & preferredWay, int & effectiveRhs) const { int whichRow=-1; otherRow=-1; int CbcFollowOn2::gutsOfFollowOn2(int &otherRow, int &preferredWay, int &effectiveRhs) const { int whichRow = -1; otherRow = -1; int numberRows = matrix_.getNumRows(); int i; // For sorting int * sort = new int [numberRows]; int * isort = new int [numberRows]; int *sort = new int[numberRows]; int *isort = new int[numberRows]; // Column copy //const double * element = matrix_.getElements(); const int * row = matrix_.getIndices(); const CoinBigIndex * columnStart = matrix_.getVectorStarts(); const int * columnLength = matrix_.getVectorLengths(); const int *row = matrix_.getIndices(); const CoinBigIndex *columnStart = matrix_.getVectorStarts(); const int *columnLength = matrix_.getVectorLengths(); // Row copy const double * elementByRow = matrixByRow_.getElements(); const int * column = matrixByRow_.getIndices(); const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts(); const int * rowLength = matrixByRow_.getVectorLengths(); OsiSolverInterface * solver = model_->solver(); const double * columnLower = solver->getColLower(); const double * columnUpper = solver->getColUpper(); const double * solution = solver->getColSolution(); const double *elementByRow = matrixByRow_.getElements(); const int *column = matrixByRow_.getIndices(); const CoinBigIndex *rowStart = matrixByRow_.getVectorStarts(); const int *rowLength = matrixByRow_.getVectorLengths(); OsiSolverInterface *solver = model_->solver(); const double *columnLower = solver->getColLower(); const double *columnUpper = solver->getColUpper(); const double *solution = solver->getColSolution(); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); int nSort=0; for (i=0;iintegerTolerance) numberUnsatisfied++; } else { rhsValue -= (int)(value*floor(solValue+0.5)); } } if (numberUnsatisfied>1) { if (smallest integerTolerance) numberUnsatisfied++; } else { rhsValue -= (int)(value * floor(solValue + 0.5)); } } if (numberUnsatisfied > 1) { if (smallest < largest) { #if 0 if  (largest>rhsValue) printf("could fix\n"); #endif if (rhsValue<=maximumRhs_&&0) { if (rhsValue <= maximumRhs_ && 0) { // will mean a cut but worth trying sort[nSort]=i; isort[nSort++]=100000-numberUnsatisfied; } } else if (largest==rhsValue) { sort[nSort]=i; isort[nSort++]=-numberUnsatisfied; } } } } if (nSort>1) { CoinSort_2(isort,isort+nSort,sort); assert (isort[1]<0); CoinZeroN(isort,numberRows); double * other = new double[numberRows]; CoinZeroN(other,numberRows); int * which = new int[numberRows]; sort[nSort] = i; isort[nSort++] = 100000 - numberUnsatisfied; } } else if (largest == rhsValue) { sort[nSort] = i; isort[nSort++] = -numberUnsatisfied; } } } } if (nSort > 1) { CoinSort_2(isort, isort + nSort, sort); assert(isort[1] < 0); CoinZeroN(isort, numberRows); double *other = new double[numberRows]; CoinZeroN(other, numberRows); int *which = new int[numberRows]; //#define COUNT #ifndef COUNT bool beforeSolution = model_->getSolutionCount()==0; #endif for (int k=0;kgetSolutionCount() == 0; #endif for (int k = 0; k < nSort - 1; k++) { i = sort[k]; int numberUnsatisfied = 0; int n=0; int n = 0; int j; for (j=rowStart[i];jintegerTolerance) { numberUnsatisfied++; for (int jj=columnStart[iColumn];jj integerTolerance) { numberUnsatisfied++; for (int jj = columnStart[iColumn]; jj < columnStart[iColumn] + columnLength[iColumn]; jj++) { int iRow = row[jj]; if (rhs_[iRow]) { other[iRow] += solValue; if (isort[iRow]) { isort[iRow]++; } else { isort[iRow] = 1; which[n++] = iRow; } } } } } } double total = 0.0; // Take out row double sumThis=other[i]; other[i]=0.0; assert (numberUnsatisfied==isort[i]); double sumThis = other[i]; other[i] = 0.0; assert(numberUnsatisfied == isort[i]); // find one nearest half if solution, one if before solution int iBest=-1; double dtarget=0.5*total; int iBest = -1; double dtarget = 0.5 * total; #ifdef COUNT int target = (numberUnsatisfied+1)>>1; int best=numberUnsatisfied; int target = (numberUnsatisfied + 1) >> 1; int best = numberUnsatisfied; #else double best; if (beforeSolution) best=dtarget; best = dtarget; else best=1.0e30; #endif for (j=0;jbest) { best = fabs(dvalue-dtarget); iBest=iRow; if (dvalue=0) { whichRow=i; otherRow=iBest; if (beforeSolution) { if (fabs(dvalue - dtarget) > best) { best = fabs(dvalue - dtarget); iBest = iRow; if (dvalue < dtarget) preferredWay = 1; else preferredWay = -1; } } else { if (fabs(dvalue - dtarget) < best) { best = fabs(dvalue - dtarget); iBest = iRow; if (dvalue < dtarget) preferredWay = 1; else preferredWay = -1; } } #endif } if (iBest >= 0) { whichRow = i; otherRow = iBest; //printf("Rows %d (%d) and %d (%d)\n",whichRow,rhs_[whichRow], //     otherRow,rhs_[otherRow]); break; } } delete [] which; delete [] other; } delete [] sort; delete [] isort; break; } } delete[] which; delete[] other; } delete[] sort; delete[] isort; return whichRow; } // Infeasibility - large is 0.5 double CbcFollowOn2::infeasibility(int & preferredWay) const { int otherRow=0; double CbcFollowOn2::infeasibility(int &preferredWay) const { int otherRow = 0; int effectiveRhs; int whichRow = gutsOfFollowOn2(otherRow,preferredWay,effectiveRhs); if (whichRow<0) { int whichRow = gutsOfFollowOn2(otherRow, preferredWay, effectiveRhs); if (whichRow < 0) { return 0.0; } else { assert (whichRow!=otherRow); return 2.0* model_->getDblParam(CbcModel::CbcIntegerTolerance); assert(whichRow != otherRow); return 2.0 * model_->getDblParam(CbcModel::CbcIntegerTolerance); } } // This looks at solution and sets bounds to contain solution void CbcFollowOn2::feasibleRegion() { } void CbcFollowOn2::feasibleRegion() { } // Creates a branching object CbcBranchingObject * CbcFollowOn2::createBranch(int way) { int otherRow=0; CbcBranchingObject * CbcFollowOn2::createBranch(int way) { int otherRow = 0; int preferredWay; int effectiveRhs; int whichRow = gutsOfFollowOn2(otherRow,preferredWay,effectiveRhs); assert(way==preferredWay); assert (whichRow>=0); int whichRow = gutsOfFollowOn2(otherRow, preferredWay, effectiveRhs); assert(way == preferredWay); assert(whichRow >= 0); int numberColumns = matrix_.getNumCols(); // Column copy //const double * element = matrix_.getElements(); const int * row = matrix_.getIndices(); const CoinBigIndex * columnStart = matrix_.getVectorStarts(); const int * columnLength = matrix_.getVectorLengths(); const int *row = matrix_.getIndices(); const CoinBigIndex *columnStart = matrix_.getVectorStarts(); const int *columnLength = matrix_.getVectorLengths(); // Row copy //const double * elementByRow = matrixByRow_.getElements(); const int * column = matrixByRow_.getIndices(); const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts(); const int * rowLength = matrixByRow_.getVectorLengths(); OsiSolverInterface * solver = model_->solver(); const double * columnLower = solver->getColLower(); const double * columnUpper = solver->getColUpper(); const int *column = matrixByRow_.getIndices(); const CoinBigIndex *rowStart = matrixByRow_.getVectorStarts(); const int *rowLength = matrixByRow_.getVectorLengths(); OsiSolverInterface *solver = model_->solver(); const double *columnLower = solver->getColLower(); const double *columnUpper = solver->getColUpper(); //const double * solution = solver->getColSolution(); #if 0 printf("%d free (but %d implicitly fixed of which %d nonzero), %d out of rhs\n",nFree,nImplicit,nFixedBut,nOut); #endif int nUp=0; int nDown=0; int * upList = new int[numberColumns]; int * downList = new int[numberColumns]; int nUp = 0; int nDown = 0; int *upList = new int[numberColumns]; int *downList = new int[numberColumns]; int j; for (j=rowStart[whichRow];j
• ## trunk/Cbc/examples/CbcBranchFollow2.hpp

 r1574 #include "CbcBranchActual.hpp" #include "CoinPackedMatrix.hpp" /** Define a follow on class. public: // Default Constructor CbcFollowOn2 (); // Default Constructor CbcFollowOn2(); /** Useful constructor */ CbcFollowOn2 (CbcModel * model); // Copy constructor CbcFollowOn2 ( const CbcFollowOn2 &); CbcFollowOn2(CbcModel *model); // Copy constructor CbcFollowOn2(const CbcFollowOn2 &); /// Clone virtual CbcObject * clone() const; virtual CbcObject *clone() const; // Assignment operator CbcFollowOn2 & operator=( const CbcFollowOn2& rhs); // Assignment operator CbcFollowOn2 &operator=(const CbcFollowOn2 &rhs); // Destructor ~CbcFollowOn2 (); // Destructor ~CbcFollowOn2(); /// Infeasibility - large is 0.5 virtual double infeasibility(int & preferredWay) const; virtual double infeasibility(int &preferredWay) const; /// This looks at solution and sets bounds to contain solution virtual void feasibleRegion(); /// Creates a branching object virtual CbcBranchingObject * createBranch(int way) ; virtual CbcBranchingObject *createBranch(int way); /** As some computation is needed in more than one place - returns row. Also returns other row and effective rhs (so we can know if cut) */ virtual int gutsOfFollowOn2(int & otherRow, int & preferredWay, int & effectiveRhs) const; virtual int gutsOfFollowOn2(int &otherRow, int &preferredWay, int &effectiveRhs) const; /// get and set for maximum rhws (affects cuts as branch) inline int maximumRhs() const { return maximumRhs_;} { return maximumRhs_; } inline void setMaximumRhs(int value) { maximumRhs_=value;} { maximumRhs_ = value; } protected: /// data CoinPackedMatrix matrix_; /// Matrix by row CoinPackedMatrix matrixByRow_; CoinPackedMatrix matrixByRow_; /// Possible rhs (if 0 then not possible) int * rhs_; int *rhs_; /// If >1 then allow cuts if effective rhs <= this int maximumRhs_;

• ## trunk/Cbc/examples/CbcBranchUser.cpp

 r1898 #include "CoinSort.hpp" // Default Constructor // Default Constructor // Default Constructor // Default Constructor CbcBranchUserDecision::CbcBranchUserDecision() :CbcBranchDecision() { } // Copy constructor CbcBranchUserDecision::CbcBranchUserDecision ( const CbcBranchUserDecision & rhs) :CbcBranchDecision(rhs) : CbcBranchDecision() { } // Copy constructor CbcBranchUserDecision::CbcBranchUserDecision( const CbcBranchUserDecision &rhs) : CbcBranchDecision(rhs) { } // Clone CbcBranchDecision * CbcBranchDecision * CbcBranchUserDecision::clone() const { // Initialize i.e. before start of choosing at a node void CbcBranchUserDecision::initialize(CbcModel * model) { } void CbcBranchUserDecision::initialize(CbcModel *model) { } /* Returns nonzero if branching on first object is "better" than on */ int CbcBranchUserDecision::betterBranch(CbcBranchingObject * thisOne, CbcBranchingObject * bestSoFar, double changeUp, int numberInfeasibilitiesUp, double changeDown, int numberInfeasibilitiesDown) int CbcBranchUserDecision::betterBranch(CbcBranchingObject *thisOne, CbcBranchingObject *bestSoFar, double changeUp, int numberInfeasibilitiesUp, double changeDown, int numberInfeasibilitiesDown) { printf("Now obsolete CbcBranchUserDecision::betterBranch\n"); */ int CbcBranchUserDecision::bestBranch (CbcBranchingObject ** objects, int numberObjects, int numberUnsatisfied, double * changeUp, int * numberInfeasibilitiesUp, double * changeDown, int * numberInfeasibilitiesDown, double objectiveValue) { int bestWay=0; int CbcBranchUserDecision::bestBranch(CbcBranchingObject **objects, int numberObjects, int numberUnsatisfied, double *changeUp, int *numberInfeasibilitiesUp, double *changeDown, int *numberInfeasibilitiesDown, double objectiveValue) { int bestWay = 0; int whichObject = -1; if (numberObjects) { CbcModel * model = objects[0]->model(); CbcModel *model = objects[0]->model(); // at continuous //double continuousObjective = model->getContinuousObjective(); //int continuousInfeasibilities = model->getContinuousInfeasibilities(); // average cost to get rid of infeasibility //double averageCostPerInfeasibility = //double averageCostPerInfeasibility = //(objectiveValue-continuousObjective)/ //(double) (abs(continuousInfeasibilities-numberUnsatisfied)+1); int numberSolutions = model->getSolutionCount(); double cutoff = model->getCutoff(); int method=0; int method = 0; int i; if (numberSolutions) { int numberHeuristic = model->getNumberHeuristicSolutions(); if (numberHeuristicnumberInfeasibilitiesDown[i]) { if (numberInfeasibilitiesDown[i] numberInfeasibilitiesDown[i]) { if (numberInfeasibilitiesDown[i] < bestNumber) { betterWay = -1; } else { if (changeDown[i] < bestCriterion) betterWay = -1; } } else { // up and down have same number bool better = false; if (numberInfeasibilitiesUp[i] < bestNumber) { better = true; } else if (numberInfeasibilitiesUp[i] == bestNumber) { if (CoinMin(changeUp[i], changeDown[i]) < bestCriterion) better = true; ; } if (better) { // see which way if (changeUp[i] <= changeDown[i]) betterWay = 1; else betterWay = -1; } } if (betterWay) { bestCriterion = CoinMin(changeUp[i], changeDown[i]); bestNumber = thisNumber; whichObject = i; bestWay = betterWay; } } } break; case 1: for ( i = 0 ; i < numberObjects ; i++) { int betterWay=0; if (changeUp[i]<=changeDown[i]) { if (changeUp[i]>bestCriterion) betterWay=1; } else { if (changeDown[i]>bestCriterion) betterWay=-1; } if (betterWay) { bestCriterion = CoinMin(changeUp[i],changeDown[i]); whichObject = i; bestWay = betterWay; } for (i = 0; i < numberObjects; i++) { int betterWay = 0; if (changeUp[i] <= changeDown[i]) { if (changeUp[i] > bestCriterion) betterWay = 1; } else { if (changeDown[i] > bestCriterion) betterWay = -1; } if (betterWay) { bestCriterion = CoinMin(changeUp[i], changeDown[i]); whichObject = i; bestWay = betterWay; } } break; case 2: for ( i = 0 ; i < numberObjects ; i++) { double change = CoinMin(changeUp[i],changeDown[i]); double sum = changeUp[i] + changeDown[i]; bool take=false; if (change>1.1*bestCriterion) take=true; else if (change>0.9*bestCriterion&&sum+change>bestCriterion+alternativeCriterion) take=true; if (take) { if (changeUp[i]<=changeDown[i]) { if (changeUp[i]>bestCriterion) bestWay=1; } else { if (changeDown[i]>bestCriterion) bestWay=-1; } bestCriterion = change; alternativeCriterion = sum; whichObject = i; } for (i = 0; i < numberObjects; i++) { double change = CoinMin(changeUp[i], changeDown[i]); double sum = changeUp[i] + changeDown[i]; bool take = false; if (change > 1.1 * bestCriterion) take = true; else if (change > 0.9 * bestCriterion && sum + change > bestCriterion + alternativeCriterion) take = true; if (take) { if (changeUp[i] <= changeDown[i]) { if (changeUp[i] > bestCriterion) bestWay = 1; } else { if (changeDown[i] > bestCriterion) bestWay = -1; } bestCriterion = change; alternativeCriterion = sum; whichObject = i; } } break; case 3: for ( i = 0 ; i < numberObjects ; i++) { int numberNext = numberInfeasibilitiesUp[i]; if (numberNextnumberInfeasibilitiesDown[i]) { if (numberInfeasibilitiesDown[i] numberInfeasibilitiesDown[i]) { if (numberInfeasibilitiesDown[i] < bestNumber) { betterWay = -1; } else { if (changeDown[i] < bestCriterion) betterWay = -1; } } else { // up and down have same number bool better = false; if (numberInfeasibilitiesUp[i] < bestNumber) { better = true; } else if (numberInfeasibilitiesUp[i] == bestNumber) { if (CoinMin(changeUp[i], changeDown[i]) < bestCriterion) better = true; ; } if (better) { // see which way if (changeUp[i] <= changeDown[i]) betterWay = 1; else betterWay = -1; } } if (betterWay) { bestCriterion = CoinMin(changeUp[i], changeDown[i]); bestNumber = thisNumber; whichObject = i; bestWay = betterWay; } } } bestCriterion = 1.0e50; for (i = 0; i < numberObjects; i++) { int thisNumber = numberInfeasibilitiesUp[i]; if (thisNumber == bestNumber && changeUp) { if (changeUp[i] < bestCriterion) { bestCriterion = changeUp[i]; whichObject = i; bestWay = 1; } } } break; } // set way in best if (whichObject>=0) if (whichObject >= 0) objects[whichObject]->way(bestWay); } Equivalent to an unspecified binary variable. */ CbcSimpleIntegerFixed::CbcSimpleIntegerFixed () CbcSimpleIntegerFixed::CbcSimpleIntegerFixed() : CbcSimpleInteger() { Loads actual upper & lower bounds for the specified variable. */ CbcSimpleIntegerFixed::CbcSimpleIntegerFixed (CbcModel * model, int iColumn, double breakEven) : CbcSimpleInteger(model,iColumn,breakEven) CbcSimpleIntegerFixed::CbcSimpleIntegerFixed(CbcModel *model, int iColumn, double breakEven) : CbcSimpleInteger(model, iColumn, breakEven) { } // Constructor from simple CbcSimpleIntegerFixed::CbcSimpleIntegerFixed (const CbcSimpleInteger & rhs) CbcSimpleIntegerFixed::CbcSimpleIntegerFixed(const CbcSimpleInteger &rhs) : CbcSimpleInteger(rhs) { } // Copy constructor CbcSimpleIntegerFixed::CbcSimpleIntegerFixed ( const CbcSimpleIntegerFixed & rhs) :CbcSimpleInteger(rhs) // Copy constructor CbcSimpleIntegerFixed::CbcSimpleIntegerFixed(const CbcSimpleIntegerFixed &rhs) : CbcSimpleInteger(rhs) { } // Assignment operator CbcSimpleIntegerFixed & CbcSimpleIntegerFixed::operator=( const CbcSimpleIntegerFixed& rhs) { if (this!=&rhs) { // Assignment operator CbcSimpleIntegerFixed & CbcSimpleIntegerFixed::operator=(const CbcSimpleIntegerFixed &rhs) { if (this != &rhs) { CbcSimpleInteger::operator=(rhs); } } // Destructor CbcSimpleIntegerFixed::~CbcSimpleIntegerFixed () // Destructor CbcSimpleIntegerFixed::~CbcSimpleIntegerFixed() { } // Infeasibility - large is 0.5 double CbcSimpleIntegerFixed::infeasibility(int & preferredWay) const { OsiSolverInterface * solver = model_->solver(); const double * solution = model_->testSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); double CbcSimpleIntegerFixed::infeasibility(int &preferredWay) const { OsiSolverInterface *solver = model_->solver(); const double *solution = model_->testSolution(); const double *lower = solver->getColLower(); const double *upper = solver->getColUpper(); double value = solution[columnNumber_]; value = CoinMax(value, lower[columnNumber_]); /*printf("%d %g %g %g %g\n",columnNumber_,value,lower[columnNumber_], solution[columnNumber_],upper[columnNumber_]);*/ double nearest = floor(value+(1.0-breakEven_)); assert (breakEven_>0.0&&breakEven_<1.0); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); if (nearest>value) preferredWay=1; double nearest = floor(value + (1.0 - breakEven_)); assert(breakEven_ > 0.0 && breakEven_ < 1.0); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); if (nearest > value) preferredWay = 1; else preferredWay=-1; preferredWay = -1; if (preferredWay_) preferredWay=preferredWay_; double weight = fabs(value-nearest); preferredWay = preferredWay_; double weight = fabs(value - nearest); // normalize so weight is 0.5 at break even if (nearesttestSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); CbcBranchingObject * CbcSimpleIntegerFixed::createBranch(OsiSolverInterface *solver, const OsiBranchingInformation *info, int way) { const double *solution = model_->testSolution(); const double *lower = solver->getColLower(); const double *upper = solver->getColUpper(); double value = solution[columnNumber_]; value = CoinMax(value, lower[columnNumber_]); value = CoinMin(value, upper[columnNumber_]); assert (upper[columnNumber_]>lower[columnNumber_]); assert(upper[columnNumber_] > lower[columnNumber_]); if (!model_->hotstartSolution()) { double nearest = floor(value+0.5); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); if (fabs(value-nearest)getDblParam(CbcModel::CbcIntegerTolerance); if (fabs(value - nearest) < integerTolerance) { // adjust value if (nearest!=upper[columnNumber_]) value = nearest+2.0*integerTolerance; if (nearest != upper[columnNumber_]) value = nearest + 2.0 * integerTolerance; else value = nearest-2.0*integerTolerance; value = nearest - 2.0 * integerTolerance; } } else { const double * hotstartSolution = model_->hotstartSolution(); const double *hotstartSolution = model_->hotstartSolution(); double targetValue = hotstartSolution[columnNumber_]; if (way>0) value = targetValue-0.1; if (way > 0) value = targetValue - 0.1; else value = targetValue+0.1; value = targetValue + 0.1; } CbcBranchingObject * branch = new CbcIntegerBranchingObject(model_,columnNumber_,way, value); CbcBranchingObject *branch = new CbcIntegerBranchingObject(model_, columnNumber_, way, value); branch->setOriginalObject(this); return branch;
• ## trunk/Cbc/examples/CbcBranchUser.hpp

 r1574 class CbcBranchUserDecision : public CbcBranchDecision { public: // Default Constructor CbcBranchUserDecision (); // Default Constructor CbcBranchUserDecision(); // Copy constructor CbcBranchUserDecision ( const CbcBranchUserDecision &); // Copy constructor CbcBranchUserDecision(const CbcBranchUserDecision &); virtual ~CbcBranchUserDecision(); /// Clone virtual CbcBranchDecision * clone() const; /// Clone virtual CbcBranchDecision *clone() const; /// Initialize i.e. before start of choosing at a node virtual void initialize(CbcModel * model); /// Initialize i.e. before start of choosing at a node virtual void initialize(CbcModel *model); /** Returns nonzero if branching on first object is "better" than on */ virtual int betterBranch(CbcBranchingObject * thisOne, CbcBranchingObject * bestSoFar, double changeUp, int numberInfeasibilitiesUp, double changeDown, int numberInfeasibilitiesDown); virtual int betterBranch(CbcBranchingObject *thisOne, CbcBranchingObject *bestSoFar, double changeUp, int numberInfeasibilitiesUp, double changeDown, int numberInfeasibilitiesDown); /** \brief Compare N branching objects. Return index of best virtual int bestBranch (CbcBranchingObject ** objects, int numberObjects, int numberUnsatisfied, double * changeUp, int * numberInfeasibilitiesUp, double * changeDown, int * numberInfeasibilitiesDown, double objectiveValue) ; bestBranch(CbcBranchingObject **objects, int numberObjects, int numberUnsatisfied, double *changeUp, int *numberInfeasibilitiesUp, double *changeDown, int *numberInfeasibilitiesDown, double objectiveValue); private: /// Illegal Assignment operator CbcBranchUserDecision & operator=(const CbcBranchUserDecision& rhs); /// Illegal Assignment operator CbcBranchUserDecision &operator=(const CbcBranchUserDecision &rhs); }; /// Define a single integer class where branching is forced until fixed class CbcSimpleIntegerFixed : public CbcSimpleInteger { public: // Default Constructor CbcSimpleIntegerFixed (); // Default Constructor CbcSimpleIntegerFixed(); // Useful constructor - passed integer index and model index CbcSimpleIntegerFixed (CbcModel * model, int iColumn, double breakEven=0.5); CbcSimpleIntegerFixed(CbcModel *model, int iColumn, double breakEven = 0.5); // Constructor from simple CbcSimpleIntegerFixed (const CbcSimpleInteger & simple); // Copy constructor CbcSimpleIntegerFixed ( const CbcSimpleIntegerFixed &); CbcSimpleIntegerFixed(const CbcSimpleInteger &simple); // Copy constructor CbcSimpleIntegerFixed(const CbcSimpleIntegerFixed &); /// Clone virtual CbcObject * clone() const; virtual CbcObject *clone() const; // Assignment operator CbcSimpleIntegerFixed & operator=( const CbcSimpleIntegerFixed& rhs); // Assignment operator CbcSimpleIntegerFixed &operator=(const CbcSimpleIntegerFixed &rhs); // Destructor ~CbcSimpleIntegerFixed (); // Destructor ~CbcSimpleIntegerFixed(); /// Infeasibility - large is 0.5 virtual double infeasibility(int & preferredWay) const; virtual double infeasibility(int &preferredWay) const; /** Creates a branching object variables, etc.) */ virtual CbcBranchingObject * createBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) ; virtual CbcBranchingObject *createBranch(OsiSolverInterface *solver, const OsiBranchingInformation *info, int way); protected:
• ## trunk/Cbc/examples/CbcCompareUser.cpp

 r1898 */ CbcCompareUser::CbcCompareUser () : CbcCompareBase(), weight_(-1.0), saveWeight_(0.0), numberSolutions_(0), count_(0), treeSize_(0) { test_=this; CbcCompareUser::CbcCompareUser() : CbcCompareBase() , weight_(-1.0) , saveWeight_(0.0) , numberSolutions_(0) , count_(0) , treeSize_(0) { test_ = this; } // Constructor with weight CbcCompareUser::CbcCompareUser (double weight) : CbcCompareBase(), weight_(weight) , saveWeight_(0.0), numberSolutions_(0), count_(0), treeSize_(0) { test_=this; } // Copy constructor CbcCompareUser::CbcCompareUser ( const CbcCompareUser & rhs) :CbcCompareBase(rhs) { weight_=rhs.weight_; CbcCompareUser::CbcCompareUser(double weight) : CbcCompareBase() , weight_(weight) , saveWeight_(0.0) , numberSolutions_(0) , count_(0) , treeSize_(0) { test_ = this; } // Copy constructor CbcCompareUser::CbcCompareUser(const CbcCompareUser &rhs) : CbcCompareBase(rhs) { weight_ = rhs.weight_; saveWeight_ = rhs.saveWeight_; numberSolutions_=rhs.numberSolutions_; numberSolutions_ = rhs.numberSolutions_; count_ = rhs.count_; treeSize_ = rhs.treeSize_; } // Assignment operator CbcCompareUser & CbcCompareUser::operator=( const CbcCompareUser& rhs) { if (this!=&rhs) { // Assignment operator CbcCompareUser & CbcCompareUser::operator=(const CbcCompareUser &rhs) { if (this != &rhs) { CbcCompareBase::operator=(rhs); weight_=rhs.weight_; weight_ = rhs.weight_; saveWeight_ = rhs.saveWeight_; numberSolutions_=rhs.numberSolutions_; numberSolutions_ = rhs.numberSolutions_; count_ = rhs.count_; treeSize_ = rhs.treeSize_; } // Destructor CbcCompareUser::~CbcCompareUser () // Destructor CbcCompareUser::~CbcCompareUser() { } // Returns true if y better than x bool CbcCompareUser::test (CbcNode * x, CbcNode * y) { if (weight_==-1.0&&(y->depth()>7||x->depth()>7)) { bool CbcCompareUser::test(CbcNode *x, CbcNode *y) { if (weight_ == -1.0 && (y->depth() > 7 || x->depth() > 7)) { // before solution /* printf("x %d %d %g, y %d %d %g\n", int testX = x->depth(); int testY = y->depth(); if (testX!=testY) return testX < testY; if (testX != testY) return testX < testY; else return equalityTest(x,y); // so ties will be broken in consistent manner return equalityTest(x, y); // so ties will be broken in consistent manner } } else { // after solution double weight = CoinMax(weight_,0.0); double testX =  x->objectiveValue()+ weight*x->numberUnsatisfied(); double testY = y->objectiveValue() + weight*y->numberUnsatisfied(); if (testX!=testY) double weight = CoinMax(weight_, 0.0); double testX = x->objectiveValue() + weight * x->numberUnsatisfied(); double testY = y->objectiveValue() + weight * y->numberUnsatisfied(); if (testX != testY) return testX > testY; else return equalityTest(x,y); // so ties will be broken in consistent manner return equalityTest(x, y); // so ties will be broken in consistent manner } } // This allows method to change behavior as it is called // after each solution bool CbcCompareUser::newSolution(CbcModel * model, double objectiveAtContinuous, int numberInfeasibilitiesAtContinuous) { if (model->getSolutionCount()==model->getNumberHeuristicSolutions()&& model->getSolutionCount()<5&&model->getNodeCount()<500) return (false) ; // solution was got by rounding bool CbcCompareUser::newSolution(CbcModel *model, double objectiveAtContinuous, int numberInfeasibilitiesAtContinuous) { if (model->getSolutionCount() == model->getNumberHeuristicSolutions() && model->getSolutionCount() < 5 && model->getNodeCount() < 500) return (false); // solution was got by rounding // set to get close to this solution double costPerInteger = (model->getObjValue()-objectiveAtContinuous)/ ((double) numberInfeasibilitiesAtContinuous); weight_ = 0.95*costPerInteger; saveWeight_ = 0.95*weight_; double costPerInteger = (model->getObjValue() - objectiveAtContinuous) / ((double)numberInfeasibilitiesAtContinuous); weight_ = 0.95 * costPerInteger; saveWeight_ = 0.95 * weight_; numberSolutions_++; if (numberSolutions_>5) weight_ =0.0; // this searches on objective return (true) ; } // This allows method to change behavior bool CbcCompareUser::every1000Nodes(CbcModel * model, int numberNodes) { double saveWeight=weight_; int numberNodes1000 = numberNodes/1000; if (numberNodes>10000) { weight_ =0.0; // this searches on objective if (numberSolutions_ > 5) weight_ = 0.0; // this searches on objective return (true); } // This allows method to change behavior bool CbcCompareUser::every1000Nodes(CbcModel *model, int numberNodes) { double saveWeight = weight_; int numberNodes1000 = numberNodes / 1000; if (numberNodes > 10000) { weight_ = 0.0; // this searches on objective // but try a bit of other stuff if ((numberNodes1000%4)==1) weight_=saveWeight_; } else if (numberNodes==1000&&weight_==-2.0) { weight_=-1.0; // Go to depth first if ((numberNodes1000 % 4) == 1) weight_ = saveWeight_; } else if (numberNodes == 1000 && weight_ == -2.0) { weight_ = -1.0; // Go to depth first } // get size of tree treeSize_ = model->tree()->size(); if (treeSize_>10000) { int n1 = model->solver()->getNumRows()+model->solver()->getNumCols(); if (treeSize_ > 10000) { int n1 = model->solver()->getNumRows() + model->solver()->getNumCols(); int n2 = model->numberObjects(); double size = n1*0.1 + n2*2.0; double size = n1 * 0.1 + n2 * 2.0; // set weight to reduce size most of time if (treeSize_*size>5.0e7) weight_=-1.0; else if ((numberNodes1000%4)==0&&treeSize_*size>1.0e6) weight_=-1.0; else if ((numberNodes1000%4)==1) weight_=0.0; if (treeSize_ * size > 5.0e7) weight_ = -1.0; else if ((numberNodes1000 % 4) == 0 && treeSize_ * size > 1.0e6) weight_ = -1.0; else if ((numberNodes1000 % 4) == 1) weight_ = 0.0; else weight_=saveWeight_; } return (weight_!=saveWeight); weight_ = saveWeight_; } return (weight_ != saveWeight); } // Returns true if wants code to do scan with alternate criterion bool CbcCompareUser::fullScan() const bool CbcCompareUser::fullScan() const { return false; } // This is alternate test function bool CbcCompareUser::alternateTest (CbcNode * x, CbcNode * y) bool CbcCompareUser::alternateTest(CbcNode *x, CbcNode *y) { // not used
• ## trunk/Cbc/examples/CbcCompareUser.hpp

 r1574 less 2% */ class CbcCompareUser  : public CbcCompareBase { class CbcCompareUser : public CbcCompareBase { public: // Default Constructor CbcCompareUser () ; // Default Constructor CbcCompareUser(); // Constructor with weight CbcCompareUser (double weight); CbcCompareUser(double weight); // Copy constructor CbcCompareUser ( const CbcCompareUser &rhs); // Assignment operator CbcCompareUser & operator=( const CbcCompareUser& rhs); // Copy constructor CbcCompareUser(const CbcCompareUser &rhs); // Assignment operator CbcCompareUser &operator=(const CbcCompareUser &rhs); /// Clone virtual CbcCompareBase * clone() const; virtual CbcCompareBase *clone() const; ~CbcCompareUser() ; ~CbcCompareUser(); /* This returns true if weighted value of node y is less than weighted value of node x */ virtual bool test (CbcNode * x, CbcNode * y) ; virtual bool test(CbcNode *x, CbcNode *y); /// This is alternate test function virtual bool alternateTest (CbcNode * x, CbcNode * y); virtual bool alternateTest(CbcNode *x, CbcNode *y); // This allows method to change behavior as it is called // after each solution virtual bool newSolution(CbcModel * model, double objectiveAtContinuous, int numberInfeasibilitiesAtContinuous) ; virtual bool newSolution(CbcModel *model, double objectiveAtContinuous, int numberInfeasibilitiesAtContinuous); /// Returns true if wants code to do scan with alternate criterion virtual bool fullScan() const; // This allows method to change behavior // This allows method to change behavior // Return true if want tree re-sorted virtual bool every1000Nodes(CbcModel * model,int numberNodes); virtual bool every1000Nodes(CbcModel *model, int numberNodes); /* if weight == -1.0 then depth first (before solution) */ inline double getWeight() const { return weight_;} { return weight_; } inline void setWeight(double weight) { weight_ = weight;} { weight_ = weight; } protected: // Weight for each infeasibility