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