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

Last change on this file since 2496 was 2469, checked in by unxusr, 8 months ago

formatting

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