source: trunk/Cbc/examples/simpleBAB.cpp @ 1140

Last change on this file since 1140 was 1140, checked in by forrest, 10 years ago

add example

File size: 2.6 KB
Line 
1// Copyright (C) 2009, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#if defined(_MSC_VER)
4// Turn off compiler warning about long names
5#  pragma warning(disable:4786)
6#endif
7
8#include <cassert>
9#include <iomanip>
10
11
12#include "OsiClpSolverInterface.hpp"
13#include "CoinPackedVector.hpp"
14//#define USE_CBC
15#ifdef USE_CBC
16#include "CbcModel.hpp"
17#endif
18
19int main (int argc, const char *argv[])
20{
21
22  OsiClpSolverInterface model;
23 
24  int start[] = { 0, 1, 2};
25  int index[] = { 0, 0};
26  double values[] = {1.0, 2.0};
27  double collb[] = {0.0, 0.0};
28  double colub[] = {10.0, 10.0};
29  double obj[] = { 1.0, 1.0};
30  double rowlb[] = { 0.0};
31  double rowub[]= { 3.9};
32 
33  // obj: Max x0 + x1
34  //  st. x0 + 2 x1 <= 3.9
35  //          0 <= x0 <= 10 and integer
36  //          0 <= x1 <= 10
37  model.loadProblem(2, 1, start, index, values, collb, colub, obj, rowlb, rowub);
38  model.setInteger(0);
39  model.setObjSense(-1.0);
40  bool optimal;
41 
42#ifndef USE_CBC
43  // Save bounds - that and dual limit should be all that is needed
44  // For this simple example we could just re-use collb and colub
45  double saveLower[2];
46  double saveUpper[2];
47  int numberColumns = model.getNumCols();
48  CoinCopyN(model.getColLower(),numberColumns,saveLower);
49  CoinCopyN(model.getColUpper(),numberColumns,saveUpper);
50  double objLimit;
51  model.getDblParam(OsiDualObjectiveLimit,objLimit);
52  model.branchAndBound();
53  optimal = model.isProvenOptimal(); 
54  const double *val = model.getColSolution(); // x0 = 3, x1 = 0.45
55  printf("Solution %g %g\n",val[0],val[1]);
56  // Restore bounds and dual limit
57  model.setColLower(saveLower);
58  model.setColUpper(saveUpper);
59  model.setDblParam(OsiDualObjectiveLimit,objLimit);
60#else
61  {
62    CbcModel model2(model);
63    model2.branchAndBound();
64    optimal = model2.isProvenOptimal(); 
65    const double *val = model2.getColSolution(); // x0 = 3, x1 = 0.45
66    printf("Solution %g %g\n",val[0],val[1]);
67  }
68#endif
69 
70  const int rowCols[] = {0};
71  const double rowElements = { 1.0};
72 
73  // add x0 <= 2, and solve once again.
74  CoinPackedVector v(1, rowCols, rowElements);
75  model.addRow(v, 0.0, 2.0);
76#ifndef USE_CBC
77  model.branchAndBound();
78  optimal = model.isProvenOptimal(); // should be x0 = 2, x1 = 0.95
79  // Address of solution will be same as only adding rows - but be safe
80  val = model.getColSolution();
81  printf("Solution %g %g\n",val[0],val[1]);
82#else
83  {
84    CbcModel model2(model);
85    model2.branchAndBound();
86    optimal = model2.isProvenOptimal(); // should be x0 = 2, x1 = 0.95
87    const double *val = model2.getColSolution(); 
88    printf("Solution %g %g\n",val[0],val[1]);
89  }
90#endif
91  return 0;
92}   
Note: See TracBrowser for help on using the repository browser.