source: trunk/Cbc/examples/simpleBAB.cpp

Last change on this file was 2469, checked in by unxusr, 7 weeks ago

formatting

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