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

Last change on this file since 1660 was 1660, checked in by stefan, 8 years ago

change to new way of using and installing configuration header files

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