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

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

add $id

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