source: trunk/Cbc/examples/sample4.cpp @ 310

Last change on this file since 310 was 2, checked in by ladanyi, 16 years ago

Import of Coin Branch-and-Cut (formerly known as Sbb)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1// Copyright (C) 2004, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#if defined(_MSC_VER)
4// Turn off compiler warning about long names
5#  pragma warning(disable:4786)
6#endif
7
8#include <cassert>
9#include <iomanip>
10
11
12// For Branch and bound
13#include "OsiSolverInterface.hpp"
14#include "CbcModel.hpp"
15#include "CbcBranchUser.hpp"
16#include "CbcBranchActual.hpp"
17#include "CbcCompareUser.hpp"
18#include "CoinTime.hpp"
19#ifdef COIN_USE_CLP
20#include "OsiClpSolverInterface.hpp"
21#endif
22
23//#############################################################################
24
25
26/************************************************************************
27
28This main program reads in an SOSr model (ltw) from an mps file.
29
30It then solves it three ways :-
31
32a) As normal
33b) SOS 1
34c) SOS 2(so answer will be different)
35
36************************************************************************/
37
38int main (int argc, const char *argv[])
39{
40
41  // Define your favorite OsiSolver
42 
43#ifdef COIN_USE_CLP
44  OsiClpSolverInterface solver1;
45  //solver1.messageHandler()->setLogLevel(0);
46  CbcModel model(solver1);
47#endif
48  model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
49
50  // Read in ltw.mps
51  // and assert that it is a clean model
52  int numMpsReadErrors = model.solver()->readMps("./ltw.mps","");
53  assert(numMpsReadErrors==0);
54
55  // Definition of node choice
56  CbcCompareUser compare;
57  compare.setWeight(0.0);
58  model.setNodeComparison(compare);
59  // Reduce output
60  model.messageHandler()->setLogLevel(1);
61  // Get branching messages
62  model.messageHandler()->setLogLevel(3);
63
64  // Do initial solve to continuous
65  model.initialSolve();
66
67  // Save model
68  CbcModel model2 = model;
69  int numberColumns=model.getNumCols();
70  int numberIntegers = 0;
71  int * integerVariable = new int[numberColumns];
72  int i;
73  for ( i=0;i<numberColumns;i++) {
74    if (model.isInteger(i)) {
75      integerVariable[numberIntegers++]=i;
76    }
77  }
78
79 
80  double time1 = CoinCpuTime() ;
81
82  model.branchAndBound();
83
84  std::cout<<"ltw.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
85           <<model.getNodeCount()<<" nodes with objective "
86           <<model.getObjValue()
87           <<(!model.status() ? " Finished" : " Not finished")
88           <<std::endl;
89
90  const double * solution = model.solver()->getColSolution();
91 
92  std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
93 
94  std::cout<<"--------------------------------------"<<std::endl;
95  for (i=0;i<numberIntegers;i++) {
96    int iColumn = integerVariable[i];
97    double value=solution[iColumn];
98    if (fabs(value)>1.0e-7) 
99      std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
100  }
101  std::cout<<"--------------------------------------"<<std::endl;
102 
103  std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
104
105  // Restore model
106  model = model2;
107
108  // Now use SOS1
109  int numberSets=8;
110  int which[28]={20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,
111                 39,40,41,42,43,44,45,46,47};
112  double weights[]={1.0,2.0,3.0,4.0,5.0};
113  int starts[]={0,2,4,6,8,13,18,23,28};
114  CbcObject ** objects = new CbcObject * [numberSets];
115  for (i=0;i<numberIntegers;i++) {
116    int iColumn = integerVariable[i];
117    // Stop being integer
118    model.solver()->setContinuous(iColumn);
119  }
120  for (i=0;i<numberSets;i++) {
121    objects[i]= new CbcSOS(&model,starts[i+1]-starts[i],which+starts[i],
122                           weights,i);
123  }
124  model.addObjects(numberSets,objects);
125  for (i=0;i<numberSets;i++)
126    delete objects[i];
127  delete [] objects;
128
129  time1 = CoinCpuTime() ;
130
131  model.branchAndBound();
132
133  std::cout<<"ltw.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
134           <<model.getNodeCount()<<" nodes with objective "
135           <<model.getObjValue()
136           <<(!model.status() ? " Finished" : " Not finished")
137           <<std::endl;
138
139  solution = model.solver()->getColSolution();
140 
141  std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
142 
143  std::cout<<"--------------------------------------"<<std::endl;
144  for (i=0;i<numberIntegers;i++) {
145    int iColumn = integerVariable[i];
146    double value=solution[iColumn];
147    if (fabs(value)>1.0e-7) 
148      std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
149  }
150  std::cout<<"--------------------------------------"<<std::endl;
151 
152  std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
153
154
155  // Restore model
156  model = model2;
157
158// Now use SOS2
159  objects = new CbcObject * [numberSets];
160  for (i=0;i<numberIntegers;i++) {
161    int iColumn = integerVariable[i];
162    // Stop being integer
163    model.solver()->setContinuous(iColumn);
164  }
165  for (i=0;i<numberSets;i++) {
166    objects[i]= new CbcSOS(&model,starts[i+1]-starts[i],which+starts[i],
167                           weights,i,2);
168  }
169  model.addObjects(numberSets,objects);
170  for (i=0;i<numberSets;i++)
171    delete objects[i];
172  delete [] objects;
173
174  time1 = CoinCpuTime() ;
175
176  model.branchAndBound();
177
178  std::cout<<"ltw.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
179           <<model.getNodeCount()<<" nodes with objective "
180           <<model.getObjValue()
181           <<(!model.status() ? " Finished" : " Not finished")
182           <<std::endl;
183
184  solution = model.solver()->getColSolution();
185 
186  std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
187 
188  std::cout<<"--------------------------------------"<<std::endl;
189
190  for (i=0;i<numberIntegers;i++) {
191    int iColumn = integerVariable[i];
192    double value=solution[iColumn];
193    if (fabs(value)>1.0e-7) 
194      std::cout<<std::setw(6)<<iColumn<<" "<<value
195               <<std::endl;
196  }
197  std::cout<<"--------------------------------------"<<std::endl;
198 
199  std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
200
201
202  delete [] integerVariable;
203  return 0;
204}   
Note: See TracBrowser for help on using the repository browser.