source: html/trunk/Cbc/ch02s02.html @ 557

Last change on this file since 557 was 557, checked in by rlh, 16 years ago

updated the user guide

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
1<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
2<html xmlns=""><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. &#10;   The CBC Model Class&#10;  "/><link rel="previous" href="ch02.html" title="Chapter 2. &#10;   The CBC Model Class&#10;  "/><link rel="next" href="ch02s03.html" title="&#10;The Relationship Between OSI and CBC&#10;"/></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>.
14  </p><div class="example"><a id="minimum.cpp"/><p class="title"><b>Example 2.1. minimum.cpp</b></p><pre class="programlisting">
16// Copyright (C) 2005, International Business Machines
17// Corporation and others.  All Rights Reserved.
19#include "CbcModel.hpp"
21// Using CLP as the solver
22#include "OsiClpSolverInterface.hpp"
24int main (int argc, const char *argv[])
26  OsiClpSolverInterface solver1;
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);
33  // Pass the solver with the problem to be solved to CbcModel
34  CbcModel model(solver1);
36  // Do complete search
37  model.branchAndBound();
39  /* Print the solution.  CbcModel clones the solver so we
40     need to get current copy from the CbcModel */
41  int numberColumns = model.solver()-&gt;getNumCols();
43  const double * solution = model.bestSolution();
45  for (int iColumn=0;iColumn&lt;numberColumns;iColumn++) {
46    double value=solution[iColumn];
47    if (fabs(value)&gt;1.0e-7&amp;&amp;model.solver()-&gt;isInteger(iColumn))
48      printf("%d has value %g\n",iColumn,value);
49   }
50  return 0;
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"> 
58The Relationship Between OSI and CBC
Note: See TracBrowser for help on using the repository browser.