source: trunk/Cbc/examples/driver.cpp

Last change on this file was 2469, checked in by unxusr, 4 months ago

formatting

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
RevLine 
[1574]1// $Id: driver.cpp 2469 2019-01-06 23:17:46Z forrest $
[100]2// Copyright (C) 2005, International Business Machines
3// Corporation and others.  All Rights Reserved.
[1574]4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
[100]6#include <cassert>
7#include <iomanip>
8
9// For Branch and bound
[1898]10#include "CoinPragma.hpp"
[100]11#include "CbcModel.hpp"
12#include "CbcStrategy.hpp"
13#include "OsiClpSolverInterface.hpp"
14// Preprocessing
15#include "CglPreProcess.hpp"
16
[1898]17#include "CoinTime.hpp"
[100]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
[2469]28int main(int argc, const char *argv[])
[100]29{
30
31  OsiClpSolverInterface solver1;
32
33  // Read in model using argv[1]
34  // and assert that it is a clean model
[1464]35  std::string mpsFileName;
[1468]36#if defined(SAMPLEDIR)
[1464]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
[2469]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;
[1898]50  }
[100]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  */
[2469]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;
[100]64      nGoodParam++;
[2469]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;
[100]70          iParam++; // skip time
71        }
72      }
73    }
74  }
[2469]75  if (nGoodParam == 0 && argc == 3 && isdigit(argv[2][0])) {
[100]76    // If time is given then stop after that number of minutes
77    minutes = atof(argv[2]);
[2469]78    if (minutes >= 0.0)
79      nGoodParam = 1;
[100]80  }
[2469]81  if (nGoodParam != argc - 2 && argc >= 2) {
[100]82    printf("Usage <file> [preprocess] [time <minutes>] or <file> <minutes>\n");
83    exit(1);
84  }
85  // See if we want preprocessing
[2469]86  OsiSolverInterface *solver2 = &solver1;
[100]87  CglPreProcess process;
88  if (preProcess) {
89    /* Do not try and produce equality cliques and
90       do up to 5 passes */
[2469]91    solver2 = process.preProcess(solver1, false, 5);
[100]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
[2469]100  if (minutes >= 0.0) {
101    std::cout << "Stopping after " << minutes << " minutes" << std::endl;
102    model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0 * minutes);
[100]103  }
104  // Set strategy - below is == CbcStrategyDefault()
[2469]105  CbcStrategyDefault strategy(true, 5, 0);
[100]106  model.setStrategy(strategy);
107  // Do complete search
[2469]108
[100]109  model.branchAndBound();
110
[2469]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;
[100]116
117  // Print solution if finished - we can't get names from Osi!
118
[2469]119  if (model.getMinimizationObjValue() < 1.0e50) {
[100]120    // post process
[2469]121    OsiSolverInterface *solver;
[100]122    if (preProcess) {
123      process.postProcess(*model.solver());
124      // Solution now back in solver1
[2469]125      solver = &solver1;
[100]126    } else {
127      solver = model.solver();
128    }
129    int numberColumns = solver->getNumCols();
[2469]130
131    const double *solution = solver->getColSolution();
132
[100]133    int iColumn;
[2469]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;
[100]141    }
[2469]142    std::cout << "--------------------------------------" << std::endl;
143
144    std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
[100]145  }
146  return 0;
[2469]147}
Note: See TracBrowser for help on using the repository browser.