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

Last change on this file since 1574 was 1574, checked in by lou, 8 years ago

Change to EPL license notice.

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