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

Last change on this file since 1565 was 333, checked in by andreasw, 13 years ago

finished examples subdir

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