1 | // $Id: crew.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 "OsiClpSolverInterface.hpp" |
---|
17 | #include "CbcModel.hpp" |
---|
18 | #include "CbcBranchActual.hpp" |
---|
19 | #include "CbcCompareUser.hpp" |
---|
20 | |
---|
21 | #include "CoinTime.hpp" |
---|
22 | |
---|
23 | /************************************************************************ |
---|
24 | |
---|
25 | This main program reads in an integer model from an mps file. |
---|
26 | It expects it to be unit coefficients and unit rhs. |
---|
27 | |
---|
28 | Branching is follow-on branching plus simple binary branching on integer variables. |
---|
29 | |
---|
30 | */ |
---|
31 | int main (int argc, const char *argv[]) |
---|
32 | { |
---|
33 | |
---|
34 | // Define a Solver for long thin problems |
---|
35 | |
---|
36 | OsiClpSolverInterface solver1; |
---|
37 | |
---|
38 | // Read in model using argv[1] |
---|
39 | // and assert that it is a clean model |
---|
40 | std::string mpsFileName; |
---|
41 | #if defined(SAMPLEDIR) |
---|
42 | mpsFileName = SAMPLEDIR "/p0033.mps"; |
---|
43 | #else |
---|
44 | if (argc < 2) { |
---|
45 | fprintf(stderr, "Do not know where to find sample MPS files.\n"); |
---|
46 | exit(1); |
---|
47 | } |
---|
48 | #endif |
---|
49 | if (argc>=2) mpsFileName = argv[1]; |
---|
50 | int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),""); |
---|
51 | assert(numMpsReadErrors==0); |
---|
52 | double time1 = CoinCpuTime(); |
---|
53 | |
---|
54 | solver1.initialSolve(); |
---|
55 | // Reduce printout |
---|
56 | solver1.setHintParam(OsiDoReducePrint,true,OsiHintTry); |
---|
57 | |
---|
58 | OsiSolverInterface * solver2=&solver1; |
---|
59 | CbcModel model(*solver2); |
---|
60 | // Point to solver |
---|
61 | OsiSolverInterface * solver3 = model.solver(); |
---|
62 | OsiClpSolverInterface * osiclp = dynamic_cast< OsiClpSolverInterface*> (solver3); |
---|
63 | assert (osiclp); |
---|
64 | |
---|
65 | // Definition of node choice |
---|
66 | CbcCompareUser compare; |
---|
67 | model.setNodeComparison(compare); |
---|
68 | |
---|
69 | |
---|
70 | int iColumn; |
---|
71 | int numberColumns = solver3->getNumCols(); |
---|
72 | /* We are going to add a single follow on object but we |
---|
73 | want to give low priority to existing integers. |
---|
74 | As the default priority is 1000 we don't actually need to give |
---|
75 | integer priorities but it is here to show how. |
---|
76 | */ |
---|
77 | // Normal integer priorities |
---|
78 | int * priority = new int [numberColumns]; |
---|
79 | int numberIntegers=0; |
---|
80 | for (iColumn=0;iColumn<numberColumns;iColumn++) { |
---|
81 | if (solver3->isInteger(iColumn)) { |
---|
82 | priority[numberIntegers++]= 100; // low priority |
---|
83 | } |
---|
84 | } |
---|
85 | /* Second parameter is true if we are adding objects, |
---|
86 | false if integers. So this does integers */ |
---|
87 | model.passInPriorities(priority,false); |
---|
88 | delete [] priority; |
---|
89 | /* Add in objects before we can give priority. |
---|
90 | In this case just one - but this shows general method |
---|
91 | */ |
---|
92 | CbcObject ** objects = new CbcObject * [1]; |
---|
93 | objects[0]=new CbcFollowOn(&model); |
---|
94 | model.addObjects(1,objects); |
---|
95 | delete objects[0]; |
---|
96 | delete [] objects; |
---|
97 | // High priority |
---|
98 | int followPriority=1; |
---|
99 | model.passInPriorities(&followPriority,true); |
---|
100 | |
---|
101 | // Do initial solve to continuous |
---|
102 | model.initialSolve(); |
---|
103 | |
---|
104 | // Do more strong branching if small |
---|
105 | // Switch off strong branching if wanted |
---|
106 | model.setNumberStrong(0); |
---|
107 | |
---|
108 | model.solver()->setIntParam(OsiMaxNumIterationHotStart,100); |
---|
109 | |
---|
110 | // Switch off most output |
---|
111 | if (model.getNumCols()<3000) { |
---|
112 | model.messageHandler()->setLogLevel(1); |
---|
113 | //model.solver()->messageHandler()->setLogLevel(0); |
---|
114 | } else { |
---|
115 | model.messageHandler()->setLogLevel(2); |
---|
116 | model.solver()->messageHandler()->setLogLevel(1); |
---|
117 | } |
---|
118 | //model.setPrintFrequency(50); |
---|
119 | |
---|
120 | // Do complete search |
---|
121 | try { |
---|
122 | model.branchAndBound(); |
---|
123 | } |
---|
124 | catch (CoinError e) { |
---|
125 | e.print(); |
---|
126 | if (e.lineNumber()>=0) |
---|
127 | std::cout<<"This was from a CoinAssert"<<std::endl; |
---|
128 | exit(0); |
---|
129 | } |
---|
130 | //void printHowMany(); |
---|
131 | //printHowMany(); |
---|
132 | std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, " |
---|
133 | <<model.getNodeCount()<<" nodes with objective " |
---|
134 | <<model.getObjValue() |
---|
135 | <<(!model.status() ? " Finished" : " Not finished") |
---|
136 | <<std::endl; |
---|
137 | |
---|
138 | // Print solution if finished - we can't get names from Osi! |
---|
139 | |
---|
140 | if (model.getMinimizationObjValue()<1.0e50) { |
---|
141 | int numberColumns = model.solver()->getNumCols(); |
---|
142 | |
---|
143 | const double * solution = model.solver()->getColSolution(); |
---|
144 | |
---|
145 | int iColumn; |
---|
146 | std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14); |
---|
147 | |
---|
148 | std::cout<<"--------------------------------------"<<std::endl; |
---|
149 | for (iColumn=0;iColumn<numberColumns;iColumn++) { |
---|
150 | double value=solution[iColumn]; |
---|
151 | if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) |
---|
152 | std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl; |
---|
153 | } |
---|
154 | std::cout<<"--------------------------------------"<<std::endl; |
---|
155 | |
---|
156 | std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific); |
---|
157 | } |
---|
158 | return 0; |
---|
159 | } |
---|