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