source: branches/devel/Cbc/examples/lotsizeSimple.cpp @ 425

Last change on this file since 425 was 413, checked in by forrest, 13 years ago

example

File size: 4.4 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 "CbcBranchLotsizeSimple.hpp"
15#include "OsiClpSolverInterface.hpp"
16#include "CoinHelperFunctions.hpp"
17
18// Time
19#include "CoinTime.hpp"
20
21
22/************************************************************************
23
24This main program reads in an integer model from an mps file.
25
26It then replaces all integer variables by lotsizing variables
27which can take integral value
28
29OR
30
31it uses 0,2,4... or 0,1,3,
32
33*************************************************************************/
34int main (int argc, const char *argv[])
35{
36 
37  // Define your favorite OsiSolver
38 
39  OsiClpSolverInterface solver1;
40 
41  // Read in model using argv[1]
42  // and assert that it is a clean model
43  std::string mpsFileName = "miplib3/bell3a";
44  if (argc>=2) mpsFileName = argv[1];
45  int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),"");
46  assert(numMpsReadErrors==0);
47 
48  int iColumn;
49  int numberColumns = solver1.getNumCols();
50  int numberLot=0;
51  char * mark = new char[numberColumns];
52  int largestBound=0;
53  // take off integers but find where they are
54  const double * upper = solver1.getColUpper();
55  const double * lower = solver1.getColLower();
56  for (iColumn=0;iColumn<numberColumns;iColumn++) {
57    if (solver1.isInteger(iColumn)) {
58      solver1.setContinuous(iColumn);
59      mark[iColumn]=1;
60      numberLot++;
61      assert (lower[iColumn]>=0.0);
62      largestBound = CoinMax(largestBound,(int) upper[iColumn]);
63    } else {
64      mark[iColumn]=0;
65    }
66  }
67  CbcModel model(solver1);
68  // Do lotsizing
69  CbcObject ** objects = new CbcObject * [numberLot];
70  double * points = new double [largestBound+1];
71  // If extra parameter not numeric then every second
72  bool ordinary = argc==2;
73  int increment=2;
74  if (!ordinary&&argc>2) {
75    int value = atoi(argv[2]);
76    if (value>2) {
77      printf("Using increment of %d\n",value);
78      increment=value;
79    }
80  } 
81  CoinIotaN(points,largestBound+1,0.0);
82 
83  numberLot=0;
84  for (iColumn=0;iColumn<numberColumns;iColumn++) {
85    if (mark[iColumn]) {
86      int iLo = (int) lower[iColumn];
87      int iUp = (int) upper[iColumn];
88      if (ordinary) {
89        objects[numberLot++]= new CbcLotsizeSimple(&model,iColumn,iUp-iLo+1,
90                                                   points+iLo);
91      } else {
92        // recreate points
93        int i;
94        int n=0;
95        if (increment==2) {
96          if (((iUp-iLo)&1)==0) {
97            // every second
98            for (i=iLo;i<=iUp;i+=2) 
99              points[n++]=i;
100          } else {
101            // iLo,iLo+1 then every second
102            points[n++]=iLo;
103            for (i=iLo+1;i<=iUp;i+=2) 
104              points[n++]=i;
105          }
106        } else {
107          for (i=iLo;i<=iUp;i+=increment) 
108            points[n++]=i;
109          if (i-increment!=iUp)
110            points[n++]=iUp; // add in last
111        }
112        objects[numberLot++]= new CbcLotsizeSimple(&model,iColumn,n,
113                                                   points);
114      }
115    }
116  }
117  delete [] points;
118  delete [] mark;
119  model.addObjects(numberLot,objects);
120  for (iColumn=0;iColumn<numberLot;iColumn++)
121    delete objects[iColumn];
122  delete [] objects;
123
124  // Switch off most output
125  model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
126  model.messageHandler()->setLogLevel(1);
127 
128  double time1 = CoinCpuTime();
129
130  // Do complete search
131 
132  model.branchAndBound();
133
134  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
135           <<model.getNodeCount()<<" nodes with objective "
136           <<model.getObjValue()
137           <<(!model.status() ? " Finished" : " Not finished")
138           <<std::endl;
139
140  // Print solution - we can't get names from Osi!
141
142  if (model.getMinimizationObjValue()<1.0e50) {
143    int numberColumns = model.solver()->getNumCols();
144   
145    const double * solution = model.solver()->getColSolution();
146   
147    int iColumn;
148    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
149   
150    std::cout<<"--------------------------------------"<<std::endl;
151    for (iColumn=0;iColumn<numberColumns;iColumn++) {
152      double value=solution[iColumn];
153      if (fabs(value)>1.0e-7) 
154        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
155    }
156    std::cout<<"--------------------------------------"<<std::endl;
157 
158    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
159  }
160  return 0;
161}   
Note: See TracBrowser for help on using the repository browser.