source: html/ch03s03.html @ 2172

Last change on this file since 2172 was 561, checked in by ladanyi, 12 years ago

Cbc static pages moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
2<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Branching</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="ch03.html" title="Chapter 3. &#10;  Other Classes and Examples&#10;  "/><link rel="previous" href="ch03s02.html" title="CbcHeuristic - Heuristic Methods"/><link rel="next" href="ch03s04.html" title="Advanced use of solver"/></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Branching</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch03s02.html">Prev</a> </td><th width="60%" align="center">Chapter 3. 
3  Other Classes and Examples
4  </th><td width="20%" align="right"> <a accesskey="n" href="ch03s04.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="branching"/>Branching</h2></div></div><div/></div><p>
5If the user declares variables as integer but does no more, then Cbc will treat them
6as simple integer variables.  In many cases the user would like to do some more fine tuning.  This shows how to create integer variables with pseudo costs.  When pseudo costs are given then
7it is assumed that if a variable is at 1.3 then the cost of branching that variable down will be 0.3 times the down pseudo cost and the cost of branching up would be 0.7 times the up pseudo cost.  This can be used both for branching and for choosing a node.
8   The full code is in <tt class="filename">longthin.cpp</tt>
9  (this code can be found in the CBC Samples directory, see
10  <a href="ch05.html" title="Chapter 5. &#10;More Samples&#10;">Chapter 5, <i>
11More Samples
12</i></a>). 
13  The idea is simple for set covering problems.
14  Branching up gets us much closer to an integer solution so we want
15  to encourage up - so we will branch up if variable value &gt; 0.333333.
16  The expected cost of going up obviously depends on the cost of the
17  variable so we just choose pseudo costs to reflect that.
18  </p><div class="example"><a id="id3369250"/><p class="title"><b>Example 3.8. Pseudo costs</b></p><pre class="programlisting">
19   
20  int iColumn;
21  int numberColumns = solver3-&gt;getNumCols();
22  // do pseudo costs
23  CbcObject ** objects = new CbcObject * [numberColumns];
24  // Point to objective
25  const double * objective = model.getObjCoefficients();
26  int numberIntegers=0;
27  for (iColumn=0;iColumn&lt;numberColumns;iColumn++) {
28    if (solver3-&gt;isInteger(iColumn)) {
29      double cost = objective[iColumn];
30      CbcSimpleIntegerPseudoCost * newObject =
31        new CbcSimpleIntegerPseudoCost(&amp;model,numberIntegers,iColumn,
32                                       2.0*cost,cost);
33      newObject-&gt;setMethod(3);
34      objects[numberIntegers++]= newObject;
35    }
36  }
37  // Now add in objects (they will replace simple integers)
38  model.addObjects(numberIntegers,objects);
39  for (iColumn=0;iColumn&lt;numberIntegers;iColumn++)
40    delete objects[iColumn];
41  delete [] objects;
42     
43  </pre></div><p>
44The actual coding in the example also tries to give more importance to variables with more
45coefficients.  Whether this sort of thing is worthwhile should be the subject of experimentation.
46Here is another example which is for crew scheduling problems.  In this case the problem has
47few rows but many thousands of variables.  Branching a variable to 1 is very powerful as it
48fixes many other variables to zero, but branching to zero is very weak as thousands of variables
49can increase from zero.  But in crew scheduling each constraint is a flight leg e.g. JFK to DFW.
50From DFW (Dallas) there may be several flights the crew could take next - suppose one flight is
51the 9:30 flight from DFW to LAX (Los Angeles).  Then a binary branch is that the crew arriving
52at DFW either take the 9:30 flight to LAX or they don't.  This follow-on branching does not
53fix individual variables but instead divides all the variables with entries in the JFK-DFW
54constraint into two groups - those with entries in the DFW-LAX constraint and those without
55entries.
56   The full code is in <tt class="filename">crew.cpp</tt>
57  (this code can be found in the CBC Samples directory, see
58  <a href="ch05.html" title="Chapter 5. &#10;More Samples&#10;">Chapter 5, <i>
59More Samples
60</i></a>).  In this case we may as well leave the simple integer
61variables and we may have to if there are other sorts of constraints.  But we want to
62branch on the follow-on rules first so we use priorities to say that those are the
63important ones.
64</p><div class="example"><a id="id3369354"/><p class="title"><b>Example 3.9. Follow-on branching</b></p><pre class="programlisting">
65   
66  int iColumn;
67  int numberColumns = solver3-&gt;getNumCols();
68  /* We are going to add a single follow on object but we
69     want to give low priority to existing integers
70     As the default priority is 1000 we don't actually need to give
71     integer priorities but it is here to show how.
72  */
73  // Normal integer priorities
74  int * priority = new int [numberColumns];
75  int numberIntegers=0;
76  for (iColumn=0;iColumn&lt;numberColumns;iColumn++) {
77    if (solver3-&gt;isInteger(iColumn)) {
78      priority[numberIntegers++]= 100; // low priority
79    }
80  }
81  /* Second parameter is true if we are adding objects,
82     false if integers.  So this does integers */
83  model.passInPriorities(priority,false);
84  delete [] priority;
85  /* Add in objects before we can give priority.
86     In this case just one - but this shows general method
87  */
88  CbcObject ** objects = new CbcObject * [1];
89  objects[0]=new CbcFollowOn(&amp;model);
90  model.addObjects(1,objects);
91  delete objects[0];
92  delete [] objects;
93  // High priority
94  int followPriority=1;
95  model.passInPriorities(&amp;followPriority,true);
96     
97  </pre></div></div><div class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch03s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch03.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch03s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CbcHeuristic - Heuristic Methods </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Advanced use of solver</td></tr></table></div></body></html>
Note: See TracBrowser for help on using the repository browser.