The first sample program shows how to perform simple branch-and-bound with CBC. This program is short enough to present in full. Most of the remaining examples will take the form of small code fragments. The complete code for all the examples in this Guide can be found in the CBC Samples directory, COIN/Cbc/Samples.
Example 2.1. minimum.cpp
// Copyright (C) 2005, International Business Machines // Corporation and others. All Rights Reserved. #include "CbcModel.hpp" // Using CLP as the solver #include "OsiClpSolverInterface.hpp" int main (int argc, const char *argv[]) { OsiClpSolverInterface solver1; // Read in example model in MPS file format // and assert that it is a clean model int numMpsReadErrors = solver1.readMps("../../Mps/Sample/p0033.mps",""); assert(numMpsReadErrors==0); // Pass the solver with the problem to be solved to CbcModel CbcModel model(solver1); // Do complete search model.branchAndBound(); /* Print the solution. CbcModel clones the solver so we need to get current copy from the CbcModel */ int numberColumns = model.solver()->getNumCols(); const double * solution = model.bestSolution(); for (int iColumn=0;iColumn<numberColumns;iColumn++) { double value=solution[iColumn]; if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) printf("%d has value %g\n",iColumn,value); } return 0; }
The program in Example 2.1 creates a OsiClpSolverInterface solver interface (i.e., solver1), and reads an MPS file. If there are no errors, the program passes the problem to CbcModel which solves the problem using the branch-and-bound algorithm. The part of the program which solves the problem is very small (one line!) but before that one line, the LP solver (i.e., solver1) had to be created and populated with the problem. After that one line, the results were printed out.