File: simpleBAB.cpp

package info (click to toggle)
coinor-cbc 2.9.9%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,848 kB
  • ctags: 5,787
  • sloc: cpp: 104,337; sh: 8,921; xml: 2,950; makefile: 520; ansic: 491; awk: 197
file content (91 lines) | stat: -rw-r--r-- 2,724 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// $Id: simpleBAB.cpp 1898 2013-04-09 18:06:04Z stefan $
// Copyright (C) 2009, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#include <cassert>
#include <iomanip>


#include "CoinPragma.hpp"
#include "OsiClpSolverInterface.hpp"
#include "CoinPackedVector.hpp"
//#define USE_CBC
#ifdef USE_CBC
#include "CbcModel.hpp"
#endif

int main (int argc, const char *argv[])
{

  OsiClpSolverInterface model;
 
  int start[] = { 0, 1, 2};
  int index[] = { 0, 0};
  double values[] = {1.0, 2.0};
  double collb[] = {0.0, 0.0};
  double colub[] = {10.0, 10.0};
  double obj[] = { 1.0, 1.0};
  double rowlb[] = { 0.0};
  double rowub[]= { 3.9};
  
  // obj: Max x0 + x1
  //  st. x0 + 2 x1 <= 3.9
  //          0 <= x0 <= 10 and integer
  //          0 <= x1 <= 10
  model.loadProblem(2, 1, start, index, values, collb, colub, obj, rowlb, rowub);
  model.setInteger(0);
  model.setObjSense(-1.0);
  //bool optimal;
  
#ifndef USE_CBC
  // Save bounds - that and dual limit should be all that is needed
  // For this simple example we could just re-use collb and colub
  double saveLower[2];
  double saveUpper[2];
  int numberColumns = model.getNumCols();
  CoinCopyN(model.getColLower(),numberColumns,saveLower);
  CoinCopyN(model.getColUpper(),numberColumns,saveUpper);
  double objLimit;
  model.getDblParam(OsiDualObjectiveLimit,objLimit);
  model.branchAndBound();
  //optimal = model.isProvenOptimal();
  const double *val = model.getColSolution(); // x0 = 3, x1 = 0.45
  printf("Solution %g %g\n",val[0],val[1]);
  // Restore bounds and dual limit
  model.setColLower(saveLower);
  model.setColUpper(saveUpper);
  model.setDblParam(OsiDualObjectiveLimit,objLimit);
#else
  {
    CbcModel model2(model);
    model2.branchAndBound();
    //optimal = model2.isProvenOptimal();
    const double *val = model2.getColSolution(); // x0 = 3, x1 = 0.45
    printf("Solution %g %g\n",val[0],val[1]);
  }
#endif
  
  const int rowCols[] = {0};
  const double rowElements = { 1.0};
  
  // add x0 <= 2, and solve once again.
  CoinPackedVector v(1, rowCols, rowElements);
  model.addRow(v, 0.0, 2.0);
#ifndef USE_CBC
  model.branchAndBound();
  //optimal = model.isProvenOptimal(); // should be x0 = 2, x1 = 0.95
  // Address of solution will be same as only adding rows - but be safe
  val = model.getColSolution();
  printf("Solution %g %g\n",val[0],val[1]);
#else
  {
    CbcModel model2(model);
    model2.branchAndBound();
    //optimal = model2.isProvenOptimal(); // should be x0 = 2, x1 = 0.95
    const double *val = model2.getColSolution(); 
    printf("Solution %g %g\n",val[0],val[1]);
  }
#endif
  return 0;
}