source: trunk/Cbc/examples/lotsize.cpp

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

fixup examples

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