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

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

fix svn keywords property

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