source: branches/devel/Cbc/examples/driver.cpp @ 529

Last change on this file since 529 was 333, checked in by andreasw, 14 years ago

finished examples subdir

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