1 | <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> |
---|
2 | <html xmlns="http://www.w3.org/1999/xhtml"><head><title> |
---|
3 | Simple Branch-and-Bound Example |
---|
4 | </title><meta name="generator" content="DocBook XSL Stylesheets V1.61.2"/><link rel="home" href="index.html" title="CBC User Guide"/><link rel="up" href="ch02.html" title="Chapter 2. The CBC Model Class "/><link rel="previous" href="ch02.html" title="Chapter 2. The CBC Model Class "/><link rel="next" href="ch02s03.html" title=" The Relationship Between OSI and CBC "/></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center"> |
---|
5 | Simple Branch-and-Bound Example |
---|
6 | </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><th width="60%" align="center">Chapter 2. |
---|
7 | The CBC Model Class |
---|
8 | </th><td width="20%" align="right"> <a accesskey="n" href="ch02s03.html">Next</a></td></tr></table><hr/></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="firstexample"/> |
---|
9 | Simple Branch-and-Bound Example |
---|
10 | </h2></div></div><div/></div><p> |
---|
11 | 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. |
---|
12 | The complete code for all the examples in this Guide can be found in the CBC Samples directory, <tt class="filename">COIN/Cbc/Samples</tt>. |
---|
13 | |
---|
14 | </p><div class="example"><a id="minimum.cpp"/><p class="title"><b>Example 2.1. minimum.cpp</b></p><pre class="programlisting"> |
---|
15 | |
---|
16 | // Copyright (C) 2005, International Business Machines |
---|
17 | // Corporation and others. All Rights Reserved. |
---|
18 | |
---|
19 | #include "CbcModel.hpp" |
---|
20 | |
---|
21 | // Using CLP as the solver |
---|
22 | #include "OsiClpSolverInterface.hpp" |
---|
23 | |
---|
24 | int main (int argc, const char *argv[]) |
---|
25 | { |
---|
26 | OsiClpSolverInterface solver1; |
---|
27 | |
---|
28 | // Read in example model in MPS file format |
---|
29 | // and assert that it is a clean model |
---|
30 | int numMpsReadErrors = solver1.readMps("../../Mps/Sample/p0033.mps",""); |
---|
31 | assert(numMpsReadErrors==0); |
---|
32 | |
---|
33 | // Pass the solver with the problem to be solved to CbcModel |
---|
34 | CbcModel model(solver1); |
---|
35 | |
---|
36 | // Do complete search |
---|
37 | model.branchAndBound(); |
---|
38 | |
---|
39 | /* Print the solution. CbcModel clones the solver so we |
---|
40 | need to get current copy from the CbcModel */ |
---|
41 | int numberColumns = model.solver()->getNumCols(); |
---|
42 | |
---|
43 | const double * solution = model.bestSolution(); |
---|
44 | |
---|
45 | for (int iColumn=0;iColumn<numberColumns;iColumn++) { |
---|
46 | double value=solution[iColumn]; |
---|
47 | if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) |
---|
48 | printf("%d has value %g\n",iColumn,value); |
---|
49 | } |
---|
50 | return 0; |
---|
51 | } |
---|
52 | |
---|
53 | </pre></div><p> |
---|
54 | The program in <a href="ch02s02.html#minimum.cpp" title="Example 2.1. minimum.cpp">Example 2.1</a> creates a <tt class="classname">OsiClpSolverInterface</tt> solver interface (i.e., <tt class="varname">solver1</tt>), and reads an MPS file. If there are no errors, the program passes the problem to <tt class="classname">CbcModel</tt> 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., <tt class="varname">solver1</tt>) had to be created and populated with the problem. After that one line, the results were printed out. |
---|
55 | </p></div><div class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch02.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch02s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 2. |
---|
56 | The CBC Model Class |
---|
57 | </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> |
---|
58 | The Relationship Between OSI and CBC |
---|
59 | </td></tr></table></div></body></html> |
---|