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