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

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

adding some examples

File size: 4.1 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/10teams";
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  bool ordinary = argc==2;
72  CoinIotaN(points,largestBound+1,0.0);
73 
74  numberLot=0;
75  for (iColumn=0;iColumn<numberColumns;iColumn++) {
76    if (mark[iColumn]) {
77      int iLo = (int) lower[iColumn];
78      int iUp = (int) upper[iColumn];
79      if (ordinary) {
80        objects[numberLot++]= new CbcLotsizeSimple(&model,iColumn,iUp-iLo+1,
81                                                   points+iLo);
82      } else {
83        // recreate points
84        int i;
85        int n=0;
86        if (((iUp-iLo)&1)==0) {
87          // every second
88          for (i=iLo;i<=iUp;i+=2) 
89            points[n++]=i;
90        } else {
91          // iLo,iLo+1 then every second
92          points[n++]=iLo;
93          for (i=iLo+1;i<=iUp;i+=2) 
94            points[n++]=i;
95        }
96        objects[numberLot++]= new CbcLotsizeSimple(&model,iColumn,n,
97                                                   points);
98      }
99    }
100  }
101  delete [] points;
102  delete [] mark;
103  model.addObjects(numberLot,objects);
104  for (iColumn=0;iColumn<numberLot;iColumn++)
105    delete objects[iColumn];
106  delete [] objects;
107
108  // Switch off most output
109  model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
110  model.messageHandler()->setLogLevel(1);
111 
112  double time1 = CoinCpuTime();
113
114  // Do complete search
115 
116  model.branchAndBound();
117
118  std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, "
119           <<model.getNodeCount()<<" nodes with objective "
120           <<model.getObjValue()
121           <<(!model.status() ? " Finished" : " Not finished")
122           <<std::endl;
123
124  // Print solution - we can't get names from Osi!
125
126  if (model.getMinimizationObjValue()<1.0e50) {
127    int numberColumns = model.solver()->getNumCols();
128   
129    const double * solution = model.solver()->getColSolution();
130   
131    int iColumn;
132    std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14);
133   
134    std::cout<<"--------------------------------------"<<std::endl;
135    for (iColumn=0;iColumn<numberColumns;iColumn++) {
136      double value=solution[iColumn];
137      if (fabs(value)>1.0e-7) 
138        std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl;
139    }
140    std::cout<<"--------------------------------------"<<std::endl;
141 
142    std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific);
143  }
144  return 0;
145}   
Note: See TracBrowser for help on using the repository browser.