Selecting the Next Node in the Search Tree
CbcCompare - Comparison Methods
The order in which the nodes of the search tree are explored can strongly influence the performance of branch-and-cut algorithms. CBC give users complete control over the search order. The search order is controlled via the CbcCompare... class. CBC provides an abstract base class, CbcCompareBase, and several commonly used instances which are described in .
Compare Classes Provided
Class name
Description

CbcCompareDepth
This will always choose the node deepest in tree. It gives minimum
tree size but may take a long time to find the best solution.

CbcCompareObjective
This will always choose the node with the best objective value. This may
give a very large tree. It is likely that the first solution found
will be the best and the search should finish soon after the first solution
is found.

CbcCompareDefault
This is designed to do a mostly depth-first search until a solution has
been found. It then use estimates that are designed to give a slightly better solution.
If a reasonable number of nodes have been explored (or a reasonable number of
solutions found), then this class will adopt a breadth-first search (i.e., making a comparison based strictly on objective function values) unless the tree is very large when it will revert to depth-first search. A better description of CbcCompareUser is given below.

CbcCompareEstimate
When pseudo costs are invoked, they can be used to guess a solution. This class uses the guessed solution.

It is relatively simple for an experienced user to create new compare class instances. The code in describes how to build a new comparison class and the reasoning behind it. The complete source can be found in CbcCompareUser.hpp and CbcCompareUser.cpp, located in the CBC Samples directory. See . The key method in CbcCompare is bool test(CbcNode* x, CbcNode* y)) which returns true if node y is preferred over node x. In the test() method, information from CbcNode can easily be used. list some commonly used methods to access information at a node.
Information Available from <classname>CbcNode</classname>
double objectiveValue() const
Value of objective at the node.

int numberUnsatisfied() const
Number of unsatisfied integers (assuming branching
object is an integer - otherwise it might be number of unsatisfied sets).

int depth() const
Depth of the node in the search tree.

double guessedObjectiveValue() const
If user was setting this (e.g., if using pseudo costs).

int way() const
The way which branching would next occur from this node
(for more advanced use).

int variable() const
The branching "variable" (associated with the CbcBranchingObject -- for more advanced use).

The node desired in the tree is often a function of the how the search is progressing. In the design of CBC, there is no information on the state of the tree. The CBC is designed so that the method
newSolution() is called whenever a solution is found and the method every1000Nodes() is called every 1000 nodes. When these methods are called, the user has the opportunity to modify the
behavior of test() by adjusting their common variables (e.g., weight_). Because CbcNode has a pointer to the model, the user can also influence the search through actions such as changing the maximum time CBC is allowed, once a solution has been found (e.g., CbcModel::setMaximumSeconds(double value)). In CbcCompareUser.cpp of the COIN/Cbc/Samples directory, four items of data are used.
1) The number of solutions found so far
2) The size of the tree (defined to be the number of active nodes)
3) A weight, weight_, which is initialized to -1.0
4) A saved value of weight, saveWeight_ (for when weight is set back to -1.0 for special reason)
The full code for the CbcCompareUser::test() method is given in .
<function>CbcCompareUser::test()</function>
numberUnsatisfied() > y->numberUnsatisfied())
return true;
else if (x->numberUnsatisfied() < y->numberUnsatisfied())
return false;
else
return x->depth() < y->depth();
} else {
// after solution.
// note: if weight_=0, comparison is based
// solely on objective value
double weight = CoinMax(weight_,0.0);
return x->objectiveValue()+ weight*x->numberUnsatisfied() >
y->objectiveValue() + weight*y->numberUnsatisfied();
}
}
]]>
Initially, weight_ is -1.0 and the search is biased towards depth first. In
fact, test() prefers y if y has fewer unsatisfied variables. In the case of a tie, test() prefers the node with the greater depth in tree. Once a solution is found, newSolution() is called. The method newSolution() interacts with test() by means of the variable weight_. If the solution was achieved by branching, a calculation is made to determine the cost per unsatisfied integer variable to go from the continuous solution to an integer solution. The variable weight_ is then set to aim at a slightly better solution. From then on, test() returns true if it seems that y will lead to a better solution than x. This source for newSolution() in given in .
<function>CbcCompareUser::newSolution()</function>
getSolutionCount()==model->getNumberHeuristicSolutions())
return; // solution was found by rounding so ignore it.
// set weight_ to get close to this solution
double costPerInteger =
(model->getObjValue()-objectiveAtContinuous)/
((double) numberInfeasibilitiesAtContinuous);
weight_ = 0.98*costPerInteger;
saveWeight_=weight_;
numberSolutions_++;
if (numberSolutions_>5)
weight_ =0.0; // comparison in test() will be
// based strictly on objective value.
}
]]>
As the search progresses, the comparison can be modified. If many nodes (or many solutions) have been genereated, then weight_ is set to 0.0 leading to a breadth-first search. Breadth-first search can lead to an enormous tree. If the tree size is exceeds 10000, it may be desirable to return to a search biased towards depth first. Changing the behavior in this manner is done by the method every1000Nodes shown in .
<function>CbcCompareUser::every1000Nodes()</function>
10000)
weight_ =0.0; // compare nodes based on objective value
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) {
// set weight to reduce size most of time
if (treeSize_>20000)
weight_=-1.0;
else if ((numberNodes%4000)!=0)
weight_=-1.0;
else
weight_=saveWeight_;
}
return numberNodes==11000; // resort if first time
}
]]>
Getting Good Bounds in CBC
CbcHeuristic - Heuristic Methods
In practice, it is very useful to get a good solution reasonably fast. Any MIP-feasible solution produces an upper bound, and a good bound will greatly reduce the run time. Good solutions can satisfy the user
on very large problems where a complete search is impossible. Obviously, heuristics are
problem dependent, although some do have more general use.
At present there is only one heuristic in CBC itself, CbcRounding. Hopefully, the number will grow. Other heuristics are in the COIN/Cbc/Samples
directory. A heuristic tries to obtain a solution to the original
problem so it only needs to consider the original rows and does not have to use the
current bounds. CBC provides an abstract base class CbcHeuristic and a rounding heuristic in CBC.
This chapter describes how to build a greedy heuristic for a set covering problem, e.g., the miplib problem fast0507. A more general (and efficient) version of the heuristic is in CbcHeuristicGreedy.hpp and CbcHeuristicGreedy.cpp located in the COIN/Cbc/Samples directory, see .
The greedy heuristic will leave all variables taking value one at this node of the
tree at value one, and will initially set all other variable to value zero.
All variables are then sorted in order of their cost
divided by the number of entries in rows which are not yet covered. (We may randomize that
value a bit so that ties will be broken in different ways on different runs of the heuristic.)
The best one is choosen, and set to one. The process is repeated. Because this is
a set covering problem (i.e., all constraints are ≥), the heuristic is guaranteed to find a solution (but not necessarily an improved solution). The speed of the heuristic could be improved by just redoing those affected, but for illustrative purposes we will keep it simple.(The speed could also be improved if all elements are 1.0).
The key CbcHeuristic method is int solution(double & solutionValue,
double * betterSolution).
The solution() method returns 0 if no solution found, and returns 1 if a solution is found, in which case it fills in the objective value and primal solution. The code in CbcHeuristicGreedy.cpp is a little more complicated than this following example. For instance, the code here assumes all variables are integer. The important bit of data is a copy of the matrix (stored by column) before any cuts have been made. The data used are bounds, objective and the matrix plus two work arrays.
Data
solver(); // Get solver from CbcModel
const double * columnLower = solver->getColLower(); // Column Bounds
const double * columnUpper = solver->getColUpper();
const double * rowLower = solver->getRowLower(); // We know we only need lower bounds
const double * solution = solver->getColSolution();
const double * objective = solver->getObjCoefficients(); // In code we also use min/max
double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
double primalTolerance;
solver->getDblParam(OsiPrimalTolerance,primalTolerance);
int numberRows = originalNumberRows_; // This is number of rows when matrix was passed in
// Column copy of matrix (before cuts)
const double * element = matrix_.getElements();
const int * row = matrix_.getIndices();
const CoinBigIndex * columnStart = matrix_.getVectorStarts();
const int * columnLength = matrix_.getVectorLengths();
// Get solution array for heuristic solution
int numberColumns = solver->getNumCols();
double * newSolution = new double [numberColumns];
// And to sum row activities
double * rowActivity = new double[numberRows];
]]>
The newSolution is then initialized to the rounded down solution.
Initialize <varname>newSolution</varname>
At this point some row activities may be below their lower bound. To correct this infeasibility, the variable which is cheapest in reducing the sum of infeasibilities is found and updated, and the process repeats. This is a finite process. (Theimplementation could be faster, but is kept simple for illustrative purposes.)
Create Feasible <varname>newSolution</varname> from Initial <varname>newSolution</varname>
1.0e-7) {
sum += CoinMin(element[j],gap);
if (element[j]+rowActivity[iRow]0.0) {
double ratio = (cost/sum)*(1.0+0.1*CoinDrand48());
if (ratio
A solution value of newSolution is compared to the best solution value. If newSolution is an improvement, its feasibility is validated.
Check Solution Quality of <varname>newSolution</varname>
Branching
Pseudo Cost Branching
If the user declares variables as integer but does no more, then CBC will treat them
as simple integer variables. In many cases the user would like to do some more fine tuning. This section shows how to create integer variables with pseudo costs. When pseudo costs are given then
it is assumed that if a variable is at 1.3 then the cost of branching that variable down will be 0.3 times the down pseudo cost and the cost of branching up would be 0.7 times the up pseudo cost. Pseudo costs can be used both for branching and for choosing a node.
The full code is in longthin.cpp located in the CBC Samples directory, see
.
The idea is simple for set covering problems.
Branching up gets us much closer to an integer solution so we will encourage that direction by branch up if variable value > 0.333333.
The expected cost of going up obviously depends on the cost of the
variable. The pseudo costs are choosen to reflect that fact.
<classname>CbcSimpleIntegerPseudoCosts</classname>
getNumCols();
// do pseudo costs
CbcObject ** objects = new CbcObject * [numberColumns];
// Point to objective
const double * objective = model.getObjCoefficients();
int numberIntegers=0;
for (iColumn=0;iColumnisInteger(iColumn)) {
double cost = objective[iColumn];
CbcSimpleIntegerPseudoCost * newObject =
new CbcSimpleIntegerPseudoCost(&model,numberIntegers,iColumn,
2.0*cost,cost);
newObject->setMethod(3);
objects[numberIntegers++]= newObject;
}
}
// Now add in objects (they will replace simple integers)
model.addObjects(numberIntegers,objects);
for (iColumn=0;iColumn
The code in also tries to give more importance to variables with more
coefficients. Whether this sort of thing is worthwhile should be the subject of experimentation.
Follow-On Branching
In crew scheduling, the problems are long and thin. A problem may have a few rows but many thousands of variables. Branching a variable to 1 is very powerful as it fixes many other variables to zero, but branching to zero is very weak as thousands of variables can increase from zero. In crew scheduling problems, each constraint is a flight leg, e.g., JFK airport to DFW airport.
From DFW there may be several flights the crew could take next - suppose one flight is
the 9:30 flight from DFW to LAX airport. A binary branch is that the crew arriving
at DFW either take the 9:30 flight to LAX or they don't. This "follow-on" branching does not
fix individual variables. Instead this branching divides all the variables with entries in the JFK-DFW
constraint into two groups - those with entries in the DFW-LAX constraint and those without
entries.
The full sample code for follow-on brancing is in crew.cpp
located in the CBC Samples directory, see
). In this case, the simple integer
variables are left which may be necessary if other sorts of constraints exist. Follow-on branching rules are to be considered first, so the priorities are set to indicated the follow-on rules take precedence. Priority 1 is the highest priority.
<classname>CbcFollowOn</classname>
getNumCols();
/* We are going to add a single follow-on object but we
want to give low priority to existing integers
As the default priority is 1000 we don't actually need to give
integer priorities but it is here to show how.
*/
// Normal integer priorities
int * priority = new int [numberColumns];
int numberIntegers=0;
for (iColumn=0;iColumnisInteger(iColumn)) {
priority[numberIntegers++]= 100; // low priority
}
}
/* Second parameter is set to true for objects,
and false for integers. This indicates integers */
model.passInPriorities(priority,false);
delete [] priority;
/* Add in objects before we can give them a priority.
In this case just one object
- but it shows the general method
*/
CbcObject ** objects = new CbcObject * [1];
objects[0]=new CbcFollowOn(&model);
model.addObjects(1,objects);
delete objects[0];
delete [] objects;
// High priority
int followPriority=1;
model.passInPriorities(&followPriority,true);
]]>
Advance Solver Uses
Creating a Solver via Inheritance
CBC uses a generic OsiSolverInterface and its resolve capability.
This does not give much flexibility so advanced users can inherit from their interface
of choice. This section illustrates how to implement such a solver for a long thin problem, e.g., fast0507 again. As with the other examples in the Guide, the sample code is not guaranteed to be the fastest way to solve the problem. The main purpose of the example is to illustrate techniques. The full source is in CbcSolver2.hpp and CbcSolver2.cpp located in the CBC Samples directory, see
.
The method initialSolve is called a few times in CBC, and provides a convenient starting point. The modelPtr_ derives from OsiClpSolverInterface.
<function>initialSolve()</function>
setLogLevel(1); // switch on a bit of printout
modelPtr_->scaling(0); // We don't want scaling for fast0507
setBasis(basis_,modelPtr_); // Put basis into ClpSimplex
// Do long thin by sprint
ClpSolve options;
options.setSolveType(ClpSolve::usePrimalorSprint);
options.setPresolveType(ClpSolve::presolveOff);
options.setSpecialOption(1,3,15); // Do 15 sprint iterations
modelPtr_->initialSolve(options); // solve problem
basis_ = getBasis(modelPtr_); // save basis
modelPtr_->setLogLevel(0); // switch off printout
]]>
The resolve() method is more complicated than initialSolve(). The main pieces of data are a counter count_ which is incremented each solve and an integer array node_ which stores the last time
a variable was active in a solution. For the first few times, the normal Dual Simplex is called and
node_ array is updated.
First Few Solves
status()==0) {
count_++; // feasible - save any nonzero or basic
const double * solution = modelPtr_->primalColumnSolution();
for (int i=0;i1.0e-6||modelPtr_->getStatus(i)==ClpSimplex::basic) {
node_[i]=CoinMax(count_,node_[i]);
howMany_[i]++;
}
}
} else {
printf("infeasible early on\n");
}
}
]]>
After the first few solves, only those variables which took part in a solution in the last so many
solves are used. As fast0507 is a set covering problem, any rows which are already covered can be taken out.
Create Small Problem
columnLower();
const double * upper = modelPtr_->columnUpper();
setBasis(basis_,modelPtr_); // Set basis
int nNewCol=0; // Number of columns in small model
// Column copy of matrix
const double * element = modelPtr_->matrix()->getElements();
const int * row = modelPtr_->matrix()->getIndices();
const CoinBigIndex * columnStart = modelPtr_->matrix()->getVectorStarts();
const int * columnLength = modelPtr_->matrix()->getVectorLengths();
int * rowActivity = new int[numberRows]; // Number of columns with entries in each row
memset(rowActivity,0,numberRows*sizeof(int));
int * rowActivity2 = new int[numberRows]; // Lower bound on row activity for each row
memset(rowActivity2,0,numberRows*sizeof(int));
char * mark = (char *) modelPtr_->dualColumnSolution(); // Get some space to mark columns
memset(mark,0,numberColumns);
for (i=0;icount_-memory_&&node_[i]>0); // Choose if used recently
// Take if used recently or active in some sense
if ((choose&&upper[i])
||(modelPtr_->getStatus(i)!=ClpSimplex::atLowerBound&&
modelPtr_->getStatus(i)!=ClpSimplex::isFixed)
||lower[i]>0.0) {
mark[i]=1; // mark as used
whichColumn[nNewCol++]=i; // add to list
CoinBigIndex j;
double value = upper[i];
if (value) {
for (j=columnStart[i];
j0.0) {
for (j=columnStart[i];
jsetRowStatus(i,ClpSimplex::basic); // make slack basic
}
if (nOKsetProblemStatus(1);
delete [] whichRow;
delete [] whichColumn;
printf("infeasible by inspection\n");
return;
}
// Now make up a small model with the right rows and columns
ClpSimplex * temp = new ClpSimplex(modelPtr_,nNewRow,whichRow,nNewCol,whichColumn);
]]>
If the variables cover the rows, then the problem is feasible (no cuts are being used). If the rows
were equality constraints, then this might not be the case. More work would be needed. After the solution, the reduct costs are checked. If any reduced costs are negative, the code goes back to the full problem and cleans up with Primal Simplex.
Check Optimal Solution
setDualObjectiveLimit(1.0e50); // Switch off dual cutoff as problem is restricted
temp->dual(); // solve
double * solution = modelPtr_->primalColumnSolution(); // put back solution
const double * solution2 = temp->primalColumnSolution();
memset(solution,0,numberColumns*sizeof(double));
for (i=0;isetStatus(iColumn,temp->getStatus(i));
}
double * rowSolution = modelPtr_->primalRowSolution();
const double * rowSolution2 = temp->primalRowSolution();
double * dual = modelPtr_->dualRowSolution();
const double * dual2 = temp->dualRowSolution();
memset(dual,0,numberRows*sizeof(double));
for (i=0;isetRowStatus(iRow,temp->getRowStatus(i));
rowSolution[iRow]=rowSolution2[i];
dual[iRow]=dual2[i];
}
// See if optimal
double * dj = modelPtr_->dualColumnSolution();
// get reduced cost for large problem
// this assumes minimization
memcpy(dj,modelPtr_->objective(),numberColumns*sizeof(double));
modelPtr_->transposeTimes(-1.0,dual,dj);
modelPtr_->setObjectiveValue(temp->objectiveValue());
modelPtr_->setProblemStatus(0);
int nBad=0;
for (i=0;igetStatus(i)==ClpSimplex::atLowerBound
&&upper[i]>lower[i]&&dj[i]<-1.0e-5)
nBad++;
}
// If necessary clean up with primal (and save some statistics)
if (nBad) {
timesBad_++;
modelPtr_->primal(1);
iterationsBad_ += modelPtr_->numberIterations();
}
]]>
The array node_ is updated, as for the first few solves. To give some idea of the effect of this tactic, the problem fast0507 has 63,009 variables but the small problem never has more than 4,000 variables. In only about ten percent of solves was it necessary to resolve, and then the average number of iterations
on full problem was less than 20.
Quadratic MIP
To give another example - again only for illustrative purposes -- it is possible to do quadratic
MIP with CBC. In this case, we make resolve the same as
initialSolve.
The full code is in ClpQuadInterface.hpp and
ClpQuadInterface.cpp located in the CBC Samples directory, see
).
Solving a Quadratic MIP
dualObjectiveLimit();
modelPtr_->setDualObjectiveLimit(1.0e50);
modelPtr_->scaling(0);
modelPtr_->setLogLevel(0);
// solve with no objective to get feasible solution
setBasis(basis_,modelPtr_);
modelPtr_->dual();
basis_ = getBasis(modelPtr_);
modelPtr_->setDualObjectiveLimit(cutoff);
if (modelPtr_->problemStatus())
return; // problem was infeasible
// Now pass in quadratic objective
ClpObjective * saveObjective = modelPtr_->objectiveAsObject();
modelPtr_->setObjectivePointer(quadraticObjective_);
modelPtr_->primal();
modelPtr_->setDualObjectiveLimit(cutoff);
if (modelPtr_->objectiveValue()>cutoff)
modelPtr_->setProblemStatus(1);
modelPtr_->setObjectivePointer(saveObjective);
]]>