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

Last change on this file since 1898 was 1898, checked in by stefan, 6 years ago

fixup examples

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