source: trunk/Cbc/examples/driver.cpp @ 1898

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

fixup examples

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1// $Id: driver.cpp 1898 2013-04-09 18:06:04Z stefan $
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
10// For Branch and bound
11#include "CoinPragma.hpp"
12#include "CbcModel.hpp"
13#include "CbcStrategy.hpp"
14#include "OsiClpSolverInterface.hpp"
15// Preprocessing
16#include "CglPreProcess.hpp"
17
18#include "CoinTime.hpp"
19
20//#############################################################################
21
22
23/************************************************************************
24
25This main program reads in an integer model from an mps file.
26It then uses default strategy - just cuts at root node
27
28************************************************************************/
29
30int main (int argc, const char *argv[])
31{
32
33  OsiClpSolverInterface solver1;
34
35  // Read in model using argv[1]
36  // and assert that it is a clean model
37  std::string mpsFileName;
38#if defined(SAMPLEDIR)
39  mpsFileName = SAMPLEDIR "/p0033.mps";
40#else
41  if (argc < 2) {
42    fprintf(stderr, "Do not know where to find sample MPS files.\n");
43    exit(1);
44  }
45#endif
46  if (argc>=2) mpsFileName = argv[1];
47  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
48  if( numMpsReadErrors != 0 )
49  {
50     printf("%d errors reading MPS file\n", numMpsReadErrors);
51     return numMpsReadErrors;
52  }
53  double time1 = CoinCpuTime();
54
55  /* Options are:
56     preprocess to do preprocessing
57     time in minutes
58     if 2 parameters and numeric taken as time
59  */
60  bool preProcess=false;
61  double minutes=-1.0;
62  int nGoodParam=0;
63  for (int iParam=2; iParam<argc;iParam++) {
64    if (!strcmp(argv[iParam],"preprocess")) {
65      preProcess=true;
66      nGoodParam++;
67    } else if (!strcmp(argv[iParam],"time")) {
68      if (iParam+1<argc&&isdigit(argv[iParam+1][0])) {
69        minutes=atof(argv[iParam+1]);
70        if (minutes>=0.0) {
71          nGoodParam+=2;
72          iParam++; // skip time
73        }
74      }
75    }
76  }
77  if (nGoodParam==0&&argc==3&&isdigit(argv[2][0])) {
78    // If time is given then stop after that number of minutes
79    minutes = atof(argv[2]);
80    if (minutes>=0.0) 
81      nGoodParam=1;
82  }
83  if (nGoodParam!=argc-2&&argc>=2) {
84    printf("Usage <file> [preprocess] [time <minutes>] or <file> <minutes>\n");
85    exit(1);
86  }
87  // See if we want preprocessing
88  OsiSolverInterface * solver2=&solver1;
89  CglPreProcess process;
90  if (preProcess) {
91    /* Do not try and produce equality cliques and
92       do up to 5 passes */
93    solver2 = process.preProcess(solver1,false,5);
94    if (!solver2) {
95      printf("Pre-processing says infeasible\n");
96      exit(2);
97    }
98    solver2->resolve();
99  }
100  CbcModel model(*solver2);
101  // If time is given then stop after that number of minutes
102  if (minutes>=0.0) {
103    std::cout<<"Stopping after "<<minutes<<" minutes"<<std::endl;
104    model.setDblParam(CbcModel::CbcMaximumSeconds,60.0*minutes);
105  }
106  // Set strategy - below is == CbcStrategyDefault()
107  CbcStrategyDefault strategy(true,5,0);
108  model.setStrategy(strategy);
109  // Do complete search
110 
111  model.branchAndBound();
112
113  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
114           <<model.getNodeCount()<<" nodes with objective "
115           <<model.getObjValue()
116           <<(!model.status() ? " Finished" : " Not finished")
117           <<std::endl;
118
119  // Print solution if finished - we can't get names from Osi!
120
121  if (model.getMinimizationObjValue()<1.0e50) {
122    // post process
123    OsiSolverInterface * solver;
124    if (preProcess) {
125      process.postProcess(*model.solver());
126      // Solution now back in solver1
127      solver = & solver1;
128    } else {
129      solver = model.solver();
130    }
131    int numberColumns = solver->getNumCols();
132   
133    const double * solution = solver->getColSolution();
134   
135    int iColumn;
136    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
137   
138    std::cout<<"--------------------------------------"<<std::endl;
139    for (iColumn=0;iColumn<numberColumns;iColumn++) {
140      double value=solution[iColumn];
141      if (fabs(value)>1.0e-7&&solver->isInteger(iColumn)) 
142        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
143    }
144    std::cout<<"--------------------------------------"<<std::endl;
145 
146    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
147  }
148  return 0;
149}   
Note: See TracBrowser for help on using the repository browser.