source: branches/devel/Cbc/examples/lotsize.cpp @ 467

Last change on this file since 467 was 333, checked in by andreasw, 14 years ago

finished examples subdir

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