source: trunk/Cbc/examples/lotsize.cpp

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

formatting

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1// $Id: lotsize.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#include "CoinPragma.hpp"
10// For Branch and bound
11#include "CbcModel.hpp"
12#include "CbcBranchLotsize.hpp"
13#include "OsiClpSolverInterface.hpp"
14
15// Time
16#include "CoinTime.hpp"
17
18/************************************************************************
19
20This main program reads in an integer model from an mps file.
21
22It then replaces all 0-1 variables by lotsizing variables
23which can take values 0.0,0.45-0.55 or 1.0
24
25*************************************************************************/
26int main(int argc, const char *argv[])
27{
28
29  // Define your favorite OsiSolver
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(MIPLIB3DIR)
37  mpsFileName = MIPLIB3DIR "/10teams";
38#else
39  if (argc < 2) {
40    fprintf(stderr, "Do not know where to find miplib3 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
52  int iColumn;
53  int numberColumns = solver1.getNumCols();
54  int numberLot = 0;
55  char *mark = new char[numberColumns];
56  // take off integers but find where they are
57  for (iColumn = 0; iColumn < numberColumns; iColumn++) {
58    if (solver1.isBinary(iColumn)) {
59      solver1.setContinuous(iColumn);
60      mark[iColumn] = 1;
61      numberLot++;
62    } else {
63      mark[iColumn] = 0;
64    }
65  }
66  CbcModel model(solver1);
67  // Do lotsizing
68  CbcObject **objects = new CbcObject *[numberLot];
69  numberLot = 0;
70  /* For semi-continuous variables numberRanges is 2
71     and ranges[]={0.0,0.0,K,COIN_DBL_MAX};
72  */
73  // valid ranges are 0.0 to 0.0, 0.45 to 0.55, 1.0 to 1.0
74  double ranges[] = { 0.0, 0.0, 0.45, 0.55, 1.0, 1.0 };
75  for (iColumn = 0; iColumn < numberColumns; iColumn++) {
76    if (mark[iColumn])
77      objects[numberLot++] = new CbcLotsize(&model, iColumn, 3, ranges, true);
78  }
79  delete[] mark;
80  model.addObjects(numberLot, objects);
81  for (iColumn = 0; iColumn < numberLot; iColumn++)
82    delete objects[iColumn];
83  delete[] objects;
84
85  // If time is given then stop after that number of minutes
86  if (argc > 2) {
87    int minutes = atoi(argv[2]);
88    std::cout << "Stopping after " << minutes << " minutes" << std::endl;
89    assert(minutes >= 0);
90    model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0 * minutes);
91  }
92  // Switch off most output
93  model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
94  if (model.getNumCols() < 3000) {
95    model.messageHandler()->setLogLevel(1);
96    //model.solver()->messageHandler()->setLogLevel(0);
97  } else {
98    model.messageHandler()->setLogLevel(2);
99    model.solver()->messageHandler()->setLogLevel(1);
100  }
101  model.messageHandler()->setLogLevel(1);
102
103  double time1 = CoinCpuTime();
104
105  // Do complete search
106
107  model.branchAndBound();
108
109  std::cout << mpsFileName << " took " << CoinCpuTime() - time1 << " seconds, "
110            << model.getNodeCount() << " nodes with objective "
111            << model.getObjValue()
112            << (!model.status() ? " Finished" : " Not finished")
113            << std::endl;
114
115  // Print solution - we can't get names from Osi!
116
117  if (model.getMinimizationObjValue() < 1.0e50) {
118    int numberColumns = model.solver()->getNumCols();
119
120    const double *solution = model.solver()->getColSolution();
121
122    int iColumn;
123    std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14);
124
125    std::cout << "--------------------------------------" << std::endl;
126    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
127      double value = solution[iColumn];
128      if (fabs(value) > 1.0e-7)
129        std::cout << std::setw(6) << iColumn << " " << value << std::endl;
130    }
131    std::cout << "--------------------------------------" << std::endl;
132
133    std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific);
134  }
135  return 0;
136}
Note: See TracBrowser for help on using the repository browser.