source: trunk/Cbc/examples/lotsize.cpp @ 1468

Last change on this file since 1468 was 1468, checked in by stefan, 11 years ago

do not require CbcConfig?.h in example to decide whether sample or miplib3 is present - do this in makefile

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