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

Last change on this file since 1574 was 1574, checked in by lou, 8 years ago

Change to EPL license notice.

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