source: trunk/Cbc/examples/crew.cpp

Last change on this file was 1898, checked in by stefan, 5 years ago

fixup examples

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1// $Id: crew.cpp 1898 2013-04-09 18:06:04Z forrest $
2// Copyright (C) 2005, 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 <cassert>
7#include <iomanip>
8
9#include "CoinPragma.hpp"
10// For Branch and bound
11#include "OsiClpSolverInterface.hpp"
12#include "CbcModel.hpp"
13#include "CbcBranchActual.hpp"
14#include "CbcCompareUser.hpp"
15
16#include "CoinTime.hpp"
17
18/************************************************************************
19
20This main program reads in an integer model from an mps file.
21It expects it to be unit coefficients and unit rhs.
22
23Branching is follow-on branching plus simple binary branching on integer variables.
24
25*/
26int main (int argc, const char *argv[])
27{
28
29  // Define a Solver for long thin problems
30 
31  OsiClpSolverInterface solver1;
32
33  // Read in model using argv[1]
34  // and assert that it is a clean model
35  std::string mpsFileName;
36#if defined(SAMPLEDIR)
37  mpsFileName = SAMPLEDIR "/p0033.mps";
38#else
39  if (argc < 2) {
40    fprintf(stderr, "Do not know where to find sample MPS files.\n");
41    exit(1);
42  }
43#endif
44  if (argc>=2) mpsFileName = argv[1];
45  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
46  if( numMpsReadErrors != 0 )
47  {
48     printf("%d errors reading MPS file\n", numMpsReadErrors);
49     return numMpsReadErrors;
50  }
51  double time1 = CoinCpuTime();
52
53  solver1.initialSolve();
54  // Reduce printout
55  solver1.setHintParam(OsiDoReducePrint,true,OsiHintTry);
56
57  OsiSolverInterface * solver2=&solver1;
58  CbcModel model(*solver2);
59  // Point to solver
60  OsiSolverInterface * solver3 = model.solver();
61  assert (dynamic_cast< OsiClpSolverInterface*> (solver3));
62
63  // Definition of node choice
64  CbcCompareUser compare;
65  model.setNodeComparison(compare);
66
67
68  int iColumn;
69  int numberColumns = solver3->getNumCols();
70  /* We are going to add a single follow on object but we
71     want to give low priority to existing integers.
72     As the default priority is 1000 we don't actually need to give
73     integer priorities but it is here to show how.
74  */
75  // Normal integer priorities
76  int * priority = new int [numberColumns];
77  int numberIntegers=0;
78  for (iColumn=0;iColumn<numberColumns;iColumn++) {
79    if (solver3->isInteger(iColumn)) {
80      priority[numberIntegers++]= 100; // low priority
81    }
82  }
83  /* Second parameter is true if we are adding objects,
84     false if integers.  So this does integers */
85  model.passInPriorities(priority,false);
86  delete [] priority;
87  /* Add in objects before we can give priority.
88     In this case just one - but this shows general method
89  */
90  CbcObject ** objects = new CbcObject * [1];
91  objects[0]=new CbcFollowOn(&model);
92  model.addObjects(1,objects);
93  delete objects[0];
94  delete [] objects;
95  // High priority
96  int followPriority=1;
97  model.passInPriorities(&followPriority,true);
98
99  // Do initial solve to continuous
100  model.initialSolve();
101
102  // Do more strong branching if small
103  // Switch off strong branching if wanted
104  model.setNumberStrong(0);
105
106  model.solver()->setIntParam(OsiMaxNumIterationHotStart,100);
107
108  // Switch off most output
109  if (model.getNumCols()<3000) {
110    model.messageHandler()->setLogLevel(1);
111    //model.solver()->messageHandler()->setLogLevel(0);
112  } else {
113    model.messageHandler()->setLogLevel(2);
114    model.solver()->messageHandler()->setLogLevel(1);
115  }
116  //model.setPrintFrequency(50);
117
118  // Do complete search
119  try {
120    model.branchAndBound();
121  }
122  catch (CoinError e) {
123    e.print();
124    if (e.lineNumber()>=0)
125      std::cout<<"This was from a CoinAssert"<<std::endl;
126    exit(0);
127  }
128  //void printHowMany();
129  //printHowMany();
130  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
131           <<model.getNodeCount()<<" nodes with objective "
132           <<model.getObjValue()
133           <<(!model.status() ? " Finished" : " Not finished")
134           <<std::endl;
135
136  // Print solution if finished - we can't get names from Osi!
137
138  if (model.getMinimizationObjValue()<1.0e50) {
139    int numberColumns = model.solver()->getNumCols();
140   
141    const double * solution = model.solver()->getColSolution();
142   
143    int iColumn;
144    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
145   
146    std::cout<<"--------------------------------------"<<std::endl;
147    for (iColumn=0;iColumn<numberColumns;iColumn++) {
148      double value=solution[iColumn];
149      if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) 
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  return 0;
157}   
Note: See TracBrowser for help on using the repository browser.