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 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. This can be used both for branching and for choosing a node. The full code is in longthin.cpp (this code can be found in the CBC Samples directory, see Chapter 5, More Samples ). The idea is simple for set covering problems. Branching up gets us much closer to an integer solution so we want to encourage up - so we will branch up if variable value > 0.333333. The expected cost of going up obviously depends on the cost of the variable so we just choose pseudo costs to reflect that.
Example 3.8. Pseudo costs
int iColumn; int numberColumns = solver3->getNumCols(); // do pseudo costs CbcObject ** objects = new CbcObject * [numberColumns]; // Point to objective const double * objective = model.getObjCoefficients(); int numberIntegers=0; for (iColumn=0;iColumn<numberColumns;iColumn++) { if (solver3->isInteger(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<numberIntegers;iColumn++) delete objects[iColumn]; delete [] objects;
The actual coding in the example also tries to give more importance to variables with more coefficients. Whether this sort of thing is worthwhile should be the subject of experimentation. Here is another example which is for crew scheduling problems. In this case the problem has 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. But in crew scheduling each constraint is a flight leg e.g. JFK to DFW. From DFW (Dallas) there may be several flights the crew could take next - suppose one flight is the 9:30 flight from DFW to LAX (Los Angeles). Then 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 but instead 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 code is in crew.cpp (this code can be found in the CBC Samples directory, see Chapter 5, More Samples ). In this case we may as well leave the simple integer variables and we may have to if there are other sorts of constraints. But we want to branch on the follow-on rules first so we use priorities to say that those are the important ones.
Example 3.9. Follow-on branching
int iColumn; int numberColumns = solver3->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;iColumn<numberColumns;iColumn++) { if (solver3->isInteger(iColumn)) { priority[numberIntegers++]= 100; // low priority } } /* Second parameter is true if we are adding objects, false if integers. So this does integers */ model.passInPriorities(priority,false); delete [] priority; /* Add in objects before we can give priority. In this case just one - but this shows 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);