<?xml version="1.0" encoding="ISO-8859-1"?>
  <chapter id="cbcmodelclass">
  <title>
   The CBC Model Class
  </title>
  <section id="hierarchy">
  <title>
  Overview
  </title>
  <para>
  The main class in CBC is <classname>CbcModel</classname>.  The <classname>CbcModel</classname> class is where most
  of the parameter setting is done. The absolute minimum number of actions taken with <classname>CbcModel</classname> is two,
    <itemizedlist>
    <listitem>
    <simpara>
    <function>CbcModel(OsiSolverInterface &amp; linearSolver)</function> as constructor, and
    </simpara>
    </listitem>
    <listitem>
    <simpara>
    <function>branchAndBound()</function> for solving the problem.    
    </simpara>
    </listitem>
    </itemizedlist>
  </para>
  </section>

  <section id="firstexample">
  <title>
  Simple Branch-and-Bound Example
  </title>
  <para>
  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, <filename>COIN/Cbc/Samples</filename>.

  </para>
  <example id="minimum.cpp">
  <title>minimum.cpp</title>
  <programlisting>
  <![CDATA[  
// 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.solver()->getColSolution();
    
  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;
}    
  ]]>   
  </programlisting>
  </example>
  <!-- Clone comment  needs more explanation. What info is safe? When do you need to do a refresh?? -->
  <!-- printf vs cout  -->

  <para>
  The program in <xref linkend="minimum.cpp"/> creates a <classname>OsiClpSolverInterface</classname> solver interface (i.e., <varname>solver1</varname>), and reads an MPS file. If there are no errors, the program passes the problem to <classname>CbcModel</classname> 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., <varname>solver1</varname>) had to be created and populated with the problem. After that one line, the results were printed out. 
 </para>
</section>

<section id="osiAndCbc">
<title>
The Relationship Between OSI and CBC
</title>
<para> 
The program in <xref linkend="minimum.cpp"/> illustrates the dependency of CBC on  
  the <classname>OsiSolverInterface</classname> class. The constructor of <classname>CbcModel</classname> takes a pointer to an <classname>OsiSolverInterface</classname> (i.e., a solver). The <classname>CbcModel</classname> clones the solver, and uses its own instance of the solver. The <classname>CbcModel</classname>'s solver and the original solver (e.g., <varname>solver1</varname>) are not in sync unless the user synchronizes them. The user can always access the <classname>CbcModel</classname>'s solver through the <function>model()</function> method.  To synchronize the two solvers, explicitly refreshing the original, e.g.,  
 <programlisting>
  solver1 = model.solver();
</programlisting>
<classname>CbcModel</classname>'s method <function>solve()</function> returns a pointer to CBC's cloned solver.
</para>

<para>
For convenience, many of the OSI methods to access problem data have identical method names in  <classname>CbcModel</classname>. (It's just more convenient to type <function>model.getNumCols()</function> rather than <function>model.solver()->getNumCols()</function>). The <classname>CbcModel</classname> refreshes its solver at certain logical points during the algorithm. At these points, the information from the <classname>CbcModel</classname> <varname>model</varname> will match the information from the <function>model.solver()</function>. Elsewhere, the information may vary. For instance, the OSI method <function>getColSolution()</function> will contain the best solution so far, while the <classname>CbcModel</classname> method may not. In this case, it is safer to use <function>CbcModel::bestSolution()</function>. 
</para>
<para>
While all the OSI methods used in <filename>minimum.cpp</filename> have equivalent methods in <classname>CbcModel</classname>, there are some OSI methods which do not. For example, if  the program produced a lot of undesired output, one might add the line 
</para>
<programlisting>
  model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry); 
</programlisting>
<para> 
  <!-- model.solver() returns an OSISolverInterface? -->
  to reduce the output. There is no <function>setHintParam()</function> method in <classname>CbcModel</classname>.
  </para>
  </section>

  <section id="gettingsolution">
  <title>
  Getting Solution Information
  </title>
  <para>
  Optimality can be checked through a call to <function>model.isProvenOptimal()</function>.  Also
  available are <function>isProvenInfeasible()</function>,
  <function>isSolutionLimitReached()</function>,
  <function>isNodeLimitReached()</function> or the feared
  <function>isAbandoned()</function>. There is also
  <function>int&nbsp;status()</function> which returns 0 if finished (which includes the case when the algorithm is finished because it has been proved infeasible), 1 if stopped by user, and 2 if difficulties arose.
  </para>
  <para>
  In addition to these <classname>CbcModel</classname> methods, solution values can be accessed via OSI methods.  The OSI methods pick up the current solution in the <classname>CBCModel</classname>.  The current solution will match the best solution found so far if called after <function>branchAndBound()</function> and a solution was found. 
  </para>
  <table frame="none">
  <title>
  Methods for Getting Solution Information from OSI 
  </title>
  <tgroup cols="2">
  <thead>
    <row>
      <entry>
      Purpose
      </entry>
      <entry>
      Name
      </entry>
      <entry>
      Notes
      </entry>
    </row>
  </thead>
  <tbody>
    <row>
      <entry align="left" valign="top">
      Primal column solution
      </entry>
      <entry align="left" valign="top">
      <function>const double * getColSolution()</function>
      </entry>
      <entry align="left" valign="top">
      The OSI method will return the best solution found thus far, unless none has been found. It is safer to use <classname>CbcModel</classname> version, <function>CbcModel::bestSolution()</function>
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      Dual row solution
      </entry>
      <entry align="left" valign="top">
      <function>const double * getRowPrice()</function>
      </entry>
      <entry align="left" valign="top">
      Identical <classname>CbcModel</classname> version available, <function>CbcModel::getRowPrice()</function>.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      Primal row solution
      </entry>
      <entry align="left" valign="top">
      <function>const double * getRowActivity()</function>
      </entry>
      <entry align="left" valign="top">
      Identical <classname>CbcModel</classname> version available, <function>CbcModel::getRowActivity()</function>.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      Dual column solution
      </entry>
      <entry align="left" valign="top">
      <function>const double * getReducedCost()</function>
      </entry>
     <entry align="left" valign="top">
      Identical <classname>CbcModel</classname> version available, <function>CbcModel::gtReducedCost()</function>.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      Number of rows in model
      </entry>
      <entry align="left" valign="top">
      <function>int getNumRows()</function>
      </entry>
      <entry align="left" valign="top">
      Identical <classname>CbcModel</classname> version available, <function>CbcModel::getNumRows()</function>. Note: the number of rows can change due to cuts. 
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      Number of columns in model
      </entry>
      <entry align="left" valign="top">
      <function>int getNumCols()</function>
      </entry>
      <entry align="left" valign="top">
      Identical <classname>CbcModel</classname> version available, <function>CbcModel::getNumCols()</function>.
      </entry>
    </row>
  </tbody>
  </tgroup>
  </table>
  </section>

  <section id="setsandgets">
  <title>
   Useful Set and Get Methods in <classname>CbcModel</classname>
  </title>
<para>
Most of the parameter setting in CBC is done through <classname>CbcModel</classname> methods. The most commonly used set and get methods are listed in <xref linkend="setGet"/>.
</para>
  <table frame="none" id="setGet">
  <title>Useful Set and Get Methods in <classname>CbcModel</classname></title>
    <tgroup cols="2">
    <thead>
    <row>
    <entry>
    Method(s)
    </entry>
    <entry>
    Description
    </entry>
    </row>
    </thead>
    <tbody>
    <row>
      <entry align="left" valign="top">
      <function>bool&nbsp;setMaximumNodes(int value)</function><sbr/>
      <function>int&nbsp;getMaximumNodes() const</function><sbr/>
      <function>bool&nbsp;setMaximumSeconds(double value)</function><sbr/>

      <function>double&nbsp;getMaximumSeconds()</function><sbr/>
      <function>bool&nbsp;setMaximumSolutions(double value)</function><sbr/>
      <function>double&nbsp;getMaximumSolutions() const</function>
      </entry>
      <entry align="left" valign="top">
      These set methods tell CBC to stop after a given number of nodes,
      seconds, or solutions is reached. The get methods return the corresponding values.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>bool&nbsp;setIntegerTolerance(double value) const</function><sbr/>
      <function>double&nbsp;getIntegerTolerance() const</function>
      </entry>
      <entry align="left" valign="top">
      An integer variable is deemed to be at an integral value if it is no further than this <parameter>value</parameter> (tolerance) away. 
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>bool&nbsp;setAllowableGap(double value)</function><sbr/>
      <function>double&nbsp;getAllowableGap() const</function><sbr/>
      <function>bool&nbsp;setAllowablePercentageGap(double value)</function><sbr/>
      <function>double&nbsp;getAllowablePercentageGap() const</function><sbr/>
      <function>bool&nbsp;setAllowableFractionGap(double value)</function><sbr/>
      <function>double&nbsp;getAllowableFractionGap() const</function><sbr/>
      </entry>
      <entry align="left" valign="top">
      <classname>CbcModel</classname> returns if the gap between the best known solution and the best
      possible solution is less than this <parameter>value</parameter>, or as a percentage, or a fraction.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>void&nbsp;setNumberStrong(double value) </function><sbr/>
      <function>int&nbsp;numberStrong() const </function> <!-- should be a "get" -->
      </entry>
      <entry align="left" valign="top">
      These methods set or get the maximum number of candidates at a node to
      be evaluated for strong branching.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>void&nbsp;setPrintFrequency(int value) </function><sbr/>
      <function>int&nbsp;printFrequency() const</function>
      </entry>
      <entry align="left" valign="top">
      Controls the number of nodes evaluated between status prints.
      Print frequency has a very slight overhead, if <parameter>value</parameter> is small.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>int&nbsp;getNodeCount() const</function>
      </entry>
      <entry align="left" valign="top">
      Returns number of nodes evaluated in the search.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>int&nbsp;numberRowsAtContinuous() const</function>
      </entry>
      <entry align="left" valign="top">
      Returns number of rows at continuous <!-- rlh: huh? -->
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>int &nbsp;numberIntegers() const</function><sbr/>
      <function>const int&nbsp;*&nbsp;integerVariable() const</function>
      </entry>
      <entry align="left" valign="top">
      Returns number of integer variables and an array specifying them. 
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>bool&nbsp;isBinary(int colIndex) const</function><sbr/>
      <function>bool&nbsp;isContinuous(int colIndex) const</function><sbr/>
      <function>bool&nbsp;isInteger(int colIndex) const</function>
      </entry>
      <entry align="left" valign="top">
      Returns information on variable <parameter>colIndex</parameter>. OSI methods
      can be used to set these attributes (before handing the model to <classname>CbcModel</classname>).
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>double&nbsp;getObjValue() const</function>
      </entry>
      <entry align="left" valign="top">
      This method returns the best objective value so far.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>double&nbsp;getCurrentObjValue() const</function>
      </entry>
      <entry align="left" valign="top">
      This method returns the current objective value.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>const&nbsp;double&nbsp;*&nbsp;getObjCoefficients() const</function><sbr/>
       </entry>
      <entry align="left" valign="top">
      This method return the objective coefficients.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>const&nbsp;double&nbsp;*&nbsp;getRowLower() const</function><sbr/>
       <function>const&nbsp;double&nbsp;*&nbsp;getRowUpper() const</function><sbr/>
       <function>const&nbsp;double&nbsp;*&nbsp;getColLower() const</function><sbr/>
       <function>const&nbsp;double&nbsp;*&nbsp;getColUpper() const</function><sbr/>
       </entry>
      <entry align="left" valign="top">
      These methods return the lower and upper bounds on row and column activities.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>const&nbsp;CoinPackMatrix&nbsp;*&nbsp;getMatrixByRow() const</function>
      </entry>
      <entry align="left" valign="top">
      This method returns a pointer to a row copy of matrix stored as a 
      <classname>CoinPackedMatrix</classname> which can be further examined.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>const CoinPackMatrix * getMatrixByCol() const</function>
      </entry>
      <entry align="left" valign="top">
      This method returns a pointer to a column copy of matrix stored as a
      <classname>CoinPackedMatrix</classname> which can be further examined.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
    <function>CoinBigIndex&nbsp;getNumElements() const</function> 
     <footnote> 
     <para> 
	<type>CoinBigIndex</type> is a <function>typedef</function> which in 
	most cases is the same as <type>int</type>. 
	</para> 
    </footnote> 
      </entry>
      <entry align="left" valign="top">
      Returns the number of nonzero elements in the problem matrix.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <function>void setObjSense(double&nbsp;value)</function><sbr/>
      <function>double getObjSense() const</function> 
      </entry>
      <entry align="left" valign="top">
      These methods set and get the objective sense.  The parameter
      <parameter>value</parameter> should be +1 to minimize and -1 to maximize.
      </entry>
    </row>
    </tbody>
  </tgroup>
  </table>
  </section>
  <section id="majormethods">
  <title>
  Impacting the Solution Process
  </title>
<para>
<classname>CbcModel</classname> is extremely flexible and customizable. The class structure of CBC is designed to make the most commonly desired customizations of branch-and-cut possible. These include: 
    <itemizedlist>
    <listitem>
    <simpara>
     selecting the next node to consider in the search tree,
    </simpara>
    </listitem>
    <listitem>
    <simpara>
    determining which variable to branch on,
    </simpara>
    </listitem>
   <listitem>
    <simpara>
    using heuristics to generate MIP-feasible solutions quickly,
    </simpara>
    </listitem>
   <listitem>
    <simpara>
    including cut generation when solving the LP-relaxations, and 
    </simpara>
    </listitem>
  <listitem>
    <simpara>
    invoking customized subproblem solvers.
    </simpara>
    </listitem>
    </itemizedlist>


To enable this flexibility,  <classname>CbcModel</classname> uses other classes in CBC (some of which are virtual and may have multiple instances). Not all classes are created equal. The two tables below list in alphabetical order the classes used by <classname>CbcModel</classname> that are of most interest and of least interest. 
</para>
    <table frame="none">
  <title>Classes Used by CbcModel - Most Useful</title>
    <tgroup cols="3">
    <thead>
    <row>
    <entry>
    Class name 
    </entry>
    <entry>
    Description
    </entry>
    <entry>
    Notes
    </entry>
    </row>
    </thead>
    <tbody>
    <row>
      <entry align="left" valign="top">
      <classname>CbcCompareBase</classname>
      </entry>
      <entry align="left" valign="top">
      Controls which node on the tree is selected.
      </entry>
      <entry align="left" valign="top">
      The default is <classname>CbcCompareDefault</classname>. Other comparison classes in <filename>CbcCompareActual.hpp</filename> include <classname>CbcCompareDepth</classname> and <classname>CbcCompareObjective</classname>. Experimenting with these classes and creating new compare classes is easy.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>CbcCutGenerator</classname>
      </entry>
      <entry align="left" valign="top">
      A wrapper for <classname>CglCutGenerator</classname> with additional data to control when the cut generator is invoked during the tree search.
      </entry>
      <entry align="left" valign="top">
      Other than knowing how to add a cut generator to <classname>CbcModel</classname>, there is not much the average user needs to know about this class. However, sophisticated users can implement their own cut generators. <!-- what's the default? -->
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>CbcHeuristic</classname>
      </entry>
      <entry align="left" valign="top">
      Heuristic that attempts to generate valid MIP-solutions leading to good upper bounds.
      </entry>
      <entry align="left" valign="top">
      Specialized heuristics can dramatically improve branch-and-cut performance. As many different heuristics as desired can be used in CBC. Advanced users should consider implementing custom heuristics when tackling difficult problems. <!-- What's the default? -->
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>CbcObject</classname>
      </entry>
      <entry align="left" valign="top">
      Defines what it means for a variable to be satisfied. Used in branching.
      </entry>
      <entry align="left" valign="top">
      Virtual class. CBC's concept of branching is based on the idea of an "object". An object has (i) a feasible region, (ii) can be evaluated for infeasibility, (iii) can be branched on, e.g., a method of generating a branching object, which defines an up branch and a down branch, and (iv) allows comparsion of the effect of branching. Instances of objects include <classname>CbcSimpleInteger</classname>, <classname>CbcSimpleIntegerPseudoCosts</classname>, <classname>CbcClique</classname>, <classname>CbcSOS</classname> (type 1 and 2), <classname>CbcFollowOn</classname>, and <classname>CbcLotsize</classname>.
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>OsiSolverInterface</classname>
      </entry>
      <entry align="left" valign="top">
      Defines the LP solver being used and the LP model. Normally
      a pointer to the desired <classname>OsiSolverInteface</classname> is passed to <classname>CbcModel</classname> before branch and cut.
      </entry>
      <entry align="left" valign="top">
      Virtual class. The user instantiates the solver interface of their choice, e.g., 
      <classname>OsiClpSolverInterface</classname>.
      </entry>
    </row>
    </tbody>
  </tgroup>
  </table>
<para>
There is not much about the classes listed in <xref linkend="least"/> that the average user needs to know about. 
</para>
    <table frame="none" id="least">
  <title>Classes Used by CbcModel - Least Useful</title>
    <tgroup cols="3">
    <thead>
    <row>
    <entry>
    Class name 
    </entry>
    <entry>
    Description
    </entry>
    <entry>
    Notes
    </entry>
    </row>
    </thead>
    <tbody>
    <row>
      <entry align="left" valign="top">
      <classname>CbcBranchDecision</classname>
      </entry>
      <entry align="left" valign="top">
      Used in choosing which variable to branch on, however, most of 
      the work is done by the definitions in <classname>CbcObject</classname>.
      </entry>
      <entry align="left" valign="top">
      Defaults to <classname>CbcBranchDefaultDecision</classname>. 
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>CbcCountRowCut</classname>
      </entry>
      <entry align="left" valign="top">
      Interface to <classname>OsiRowCut</classname>. It counts the usage so cuts can gracefully vanish.
      </entry>
      <entry align="left" valign="top">
      See <classname>OsiRowCut</classname> for more details. <!-- Default? -->
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>CbcNode</classname>
      </entry>
      <entry align="left" valign="top">
      Controls which variable/entity is selected to be branch on.
      </entry>
      <entry align="left" valign="top">
      Controlled via <classname>CbcModel</classname> parameters. Information from <classname>CbcNode</classname> can be useful in creating customized node selection rules.  <!-- Default? -->
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>CbcNodeInfo</classname>
      </entry>
      <entry align="left" valign="top">
      Contains data on bounds, basis, etc. for one node of the search tree.
      </entry>
      <entry align="left" valign="top">
      Header is located in <filename>CbcNode.hpp</filename>. <!-- Defaults? -->
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>CbcTree</classname>
      </entry>
      <entry align="left" valign="top">
      Defines how the search tree is stored.
      </entry>
      <entry align="left" valign="top">
      This class can be changed but it is not likely to be modified.<!-- Defaults? -->
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>CoinMessageHandler</classname>
      </entry>
      <entry align="left" valign="top">
      Deals with message handling
      </entry>
      <entry align="left" valign="top">
      The user can inherit from <classname>CoinMessageHandler</classname> to specialize message handling.  
      <!-- Defaults? -->
      </entry>
    </row>
    <row>
      <entry align="left" valign="top">
      <classname>CoinWarmStartBasis</classname>
      </entry>
      <entry align="left" valign="top">
      Basis representation to be used by solver
      </entry>
      <entry align="left" valign="top">
      <!-- Defaults? -->
      </entry>
    </row>
    </tbody>
  </tgroup>
  </table>
</section>

  </chapter>
