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

Last change on this file since 1900 was 1900, checked in by forrest, 6 years ago

fix some examples and a bug on repeated use of same CbcModel?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1// $Id: sample4.cpp 1900 2013-04-10 13:30:07Z forrest $
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 SOS model (rgn) 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 rgn.mps
49  std::string mpsFileName;
50#if defined(MIPLIB3DIR)
51  mpsFileName = MIPLIB3DIR "/rgn";
52#else
53  if (argc < 2) {
54    fprintf(stderr, "Do not know where to find miplib3 MPS files.\n");
55    exit(1);
56  }
57#endif
58  if (argc>=2) mpsFileName = argv[1];
59  int numMpsReadErrors = model.solver()->readMps(mpsFileName.c_str(),"");
60  if( numMpsReadErrors != 0 )
61  {
62     printf("%d errors reading MPS file\n", numMpsReadErrors);
63     return numMpsReadErrors;
64  }
65
66  // Definition of node choice
67  CbcCompareUser compare;
68  compare.setWeight(0.0);
69  model.setNodeComparison(compare);
70  // Reduce output
71  model.messageHandler()->setLogLevel(1);
72  // Get branching messages
73  model.messageHandler()->setLogLevel(3);
74
75  // Do initial solve to continuous
76  model.initialSolve();
77
78  // Save model
79  CbcModel model2 = model;
80  int numberColumns=model.getNumCols();
81  int numberIntegers = 0;
82  int * integerVariable = new int[numberColumns];
83  int i;
84  for ( i=0;i<numberColumns;i++) {
85    if (model.isInteger(i)) {
86      integerVariable[numberIntegers++]=i;
87    }
88  }
89
90  if (numberColumns!=180 || numberIntegers!=100) {
91    printf("Incorrect model for example\n");
92    exit(1);
93  }
94
95  double time1 = CoinCpuTime() ;
96
97  model.branchAndBound();
98
99  std::cout<<"rgn.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
100           <<model.getNodeCount()<<" nodes with objective "
101           <<model.getObjValue()
102           <<(!model.status() ? " Finished" : " Not finished")
103           <<std::endl;
104
105  const double * solution = model.solver()->getColSolution();
106 
107  std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
108 
109  std::cout<<"--------------------------------------"<<std::endl;
110  for (i=0;i<numberIntegers;i++) {
111    int iColumn = integerVariable[i];
112    double value=solution[iColumn];
113    if (fabs(value)>1.0e-7) 
114      std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
115  }
116  std::cout<<"--------------------------------------"<<std::endl;
117 
118  std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
119
120  // Restore model
121  model = model2;
122
123  // Convert slacks to variables
124  CoinBigIndex start[5]={0,1,2,3,4};
125  int row[4]={0,1,2,3};
126  double element[4]={1.0,1.0,1.0,1.0};
127  double up[4]={1.0,1.0,1.0,1.0};
128  model.solver()->addCols(4,start,row,element,NULL,up,NULL);
129  // Now use SOS1
130  int numberSets=4;
131  int which[104];
132  double weights[104];
133  int starts[5];
134  // load
135  int n=0;
136  starts[0]=0;
137  for (int iSet=0;iSet<4;iSet++) {
138    for (int i=0;i<25;i++) {
139      weights[n]=i+1.0;
140      which[n]=iSet*25+i;
141      n++;
142    }
143    // slack - make sure first branch is on slack
144    weights[n]=1000.0;
145    which[n]=180+iSet;
146    n++;
147    starts[iSet+1]=n;
148  }
149  for (i=0;i<numberIntegers;i++) {
150    int iColumn = integerVariable[i];
151    // Stop being integer
152    model.solver()->setContinuous(iColumn);
153  }
154  // save model in this state
155  CbcModel modelSOS = model;
156  CbcObject ** objects = new CbcObject * [numberSets];
157  for (i=0;i<numberSets;i++) {
158    objects[i]= new CbcSOS(&model,starts[i+1]-starts[i],which+starts[i],
159                           weights,i);
160  }
161  model.addObjects(numberSets,objects);
162  for (i=0;i<numberSets;i++)
163    delete objects[i];
164  delete [] objects;
165
166  time1 = CoinCpuTime() ;
167
168  model.branchAndBound();
169
170  std::cout<<"rgn.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
171           <<model.getNodeCount()<<" nodes with objective "
172           <<model.getObjValue()
173           <<(!model.status() ? " Finished" : " Not finished")
174           <<std::endl;
175
176  solution = model.solver()->getColSolution();
177 
178  std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
179 
180  std::cout<<"--------------------------------------"<<std::endl;
181  for (i=0;i<numberIntegers;i++) {
182    int iColumn = integerVariable[i];
183    double value=solution[iColumn];
184    if (fabs(value)>1.0e-7) 
185      std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
186  }
187  std::cout<<"--------------------------------------"<<std::endl;
188 
189  std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
190
191
192  // Restore SOS model
193  model = modelSOS;
194
195// Now use SOS2
196  objects = new CbcObject * [numberSets];
197  for (i=0;i<numberSets;i++) {
198    objects[i]= new CbcSOS(&model,starts[i+1]-starts[i],which+starts[i],
199                           weights,i,2);
200  }
201  model.addObjects(numberSets,objects);
202  for (i=0;i<numberSets;i++)
203    delete objects[i];
204  delete [] objects;
205
206  time1 = CoinCpuTime() ;
207
208  model.branchAndBound();
209
210  std::cout<<"rgn.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
211           <<model.getNodeCount()<<" nodes with objective "
212           <<model.getObjValue()
213           <<(!model.status() ? " Finished" : " Not finished")
214           <<std::endl;
215
216  solution = model.solver()->getColSolution();
217 
218  std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
219 
220  std::cout<<"--------------------------------------"<<std::endl;
221
222  for (i=0;i<numberIntegers;i++) {
223    int iColumn = integerVariable[i];
224    double value=solution[iColumn];
225    if (fabs(value)>1.0e-7) 
226      std::cout<<std::setw(6)<<iColumn<<" "<<value
227               <<std::endl;
228  }
229  std::cout<<"--------------------------------------"<<std::endl;
230 
231  std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
232
233
234  delete [] integerVariable;
235  return 0;
236}   
Note: See TracBrowser for help on using the repository browser.