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

Last change on this file since 554 was 554, checked in by rlh, 15 years ago

initial import of Cbc documentation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
2  First Example
3  </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. 
4  Basic Model Classes
5  "><link rel="previous" href="ch02.html" title="Chapter 2. 
6  Basic Model Classes
7  "><link rel="next" href="ch02s03.html" title="
8  Getting at the Solution (CbcModel methods)
9  "></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">
10  First Example
11  </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><th width="60%" align="center">Chapter 2. 
12  Basic Model Classes
13  </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 name="firstexample"></a>
14  First Example
15  </h2></div></div><div></div></div><p>
16  Below is our first CBC sample program.  It is short enough to present in full
17  (this code can be found in the CBC Samples directory, see
18  <a href="ch05.html" title="Chapter 5. 
19More Samples
20">Chapter 5, <i>
21More Samples
22</i></a>).  Most of the remaining examples in this Guide
23  will take the form of small code fragments.
24  </p><div class="example"><a name="id2898102"></a><p class="title"><b>Example 2.1. minimum.cpp</b></p><pre class="programlisting">
25   
26// Copyright (C) 2005, International Business Machines
27// Corporation and others.  All Rights Reserved.
28
29#include "CbcModel.hpp"
30
31// Using as solver
32#include "OsiClpSolverInterface.hpp"
33
34int main (int argc, const char *argv[])
35{
36  OsiClpSolverInterface solver1;
37  // Read in example model
38  // and assert that it is a clean model
39  int numMpsReadErrors = solver1.readMps("../../Mps/Sample/p0033.mps","");
40  assert(numMpsReadErrors==0);
41
42  // Pass data and solver to CbcModel
43  CbcModel model(solver1);
44
45  // Do complete search
46  model.branchAndBound();
47  /* Print solution.  CbcModel clones solver so we
48     need to get current copy */
49  int numberColumns = model.solver()-&gt;getNumCols();
50   
51  const double * solution = model.solver()-&gt;getColSolution();
52   
53  for (int iColumn=0;iColumn&lt;numberColumns;iColumn++) {
54    double value=solution[iColumn];
55    if (fabs(value)&gt;1.0e-7&amp;&amp;model.solver()-&gt;isInteger(iColumn))
56      printf("%d has value %g\n",iColumn,value);
57  }
58  return 0;
59}   
60     
61  </pre></div><p>
62  This sample program creates a  <tt class="classname">OsiClpSolverInterface</tt> solver,
63  reads an MPS file, and if there are no errors, passes it to <tt class="classname">CbcModel</tt>
64  which solves it
65  using the Branch and Bound algorithm.  The part of the program which solves the program
66  is very small but before that the linear solver had to be created with data and
67  after that the results were printed out.  So the user can see that often knowledge
68  of the <tt class="classname">OsiSolverInterface</tt> methods will be necessary.
69  In this case CbcModel has the identical methods so we could have used those but not
70  always.  For instance the program produces a lot of output so one might add
71   
72  model.solver()-&gt;setHintParam(OsiDoReducePrint,true,OsiHintTry);
73     
74  to reduce the amount. That improves things a lot but we still get one message per node
75  so we could add
76   
77    model.setLogLevel(1);
78     
79  The following section gives many of the ways of getting information from the
80  model or underlying solver.
81  </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. 
82  Basic Model Classes
83   </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 
84  Getting at the Solution (CbcModel methods)
85  </td></tr></table></div></body></html>
Note: See TracBrowser for help on using the repository browser.