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 "OsiSolverInterface.hpp" |
---|
14 | #include "CbcModel.hpp" |
---|
15 | #include "CbcBranchActual.hpp" |
---|
16 | #include "CbcBranchUser.hpp" |
---|
17 | #include "CbcCompareUser.hpp" |
---|
18 | #include "CbcCutGenerator.hpp" |
---|
19 | #include "CbcHeuristicGreedy.hpp" |
---|
20 | #include "CbcSolver2.hpp" |
---|
21 | #include "CoinModel.hpp" |
---|
22 | |
---|
23 | // Cuts |
---|
24 | |
---|
25 | #include "CglProbing.hpp" |
---|
26 | |
---|
27 | #include "CoinTime.hpp" |
---|
28 | |
---|
29 | /************************************************************************ |
---|
30 | |
---|
31 | This main program reads in an integer model from an mps file. |
---|
32 | It expects it to be unit coefficients and unit rhs and long and thin |
---|
33 | |
---|
34 | Branching is simple binary branching on integer variables. |
---|
35 | |
---|
36 | */ |
---|
37 | int main (int argc, const char *argv[]) |
---|
38 | { |
---|
39 | |
---|
40 | // Define a Solver for long thin problems |
---|
41 | |
---|
42 | CbcSolver2 solver1; |
---|
43 | |
---|
44 | // Read in model using argv[1] |
---|
45 | // and assert that it is a clean model |
---|
46 | std::string mpsFileName; |
---|
47 | #if defined(SAMPLEDIR) |
---|
48 | mpsFileName = SAMPLEDIR "/p0033.mps"; |
---|
49 | #else |
---|
50 | if (argc < 2) { |
---|
51 | fprintf(stderr, "Do not know where to find sample MPS files.\n"); |
---|
52 | exit(1); |
---|
53 | } |
---|
54 | #endif |
---|
55 | if (argc>=2) mpsFileName = argv[1]; |
---|
56 | int numMpsReadErrors = solver1.readMps(mpsFileName.c_str(),""); |
---|
57 | assert(numMpsReadErrors==0); |
---|
58 | double time1 = CoinCpuTime(); |
---|
59 | |
---|
60 | solver1.initialSolve(); |
---|
61 | // Reduce printout |
---|
62 | solver1.setHintParam(OsiDoReducePrint,true,OsiHintTry); |
---|
63 | |
---|
64 | OsiSolverInterface * solver2=&solver1; |
---|
65 | CbcModel model(*solver2); |
---|
66 | // Point to solver |
---|
67 | OsiSolverInterface * solver3 = model.solver(); |
---|
68 | CbcSolver2 * osiclp = dynamic_cast< CbcSolver2*> (solver3); |
---|
69 | assert (osiclp); |
---|
70 | osiclp->initialize(&model,NULL); |
---|
71 | osiclp->setAlgorithm(2); |
---|
72 | osiclp->setMemory(1000); |
---|
73 | // Set up some cut generators and defaults |
---|
74 | // Probing first as gets tight bounds on continuous |
---|
75 | |
---|
76 | CglProbing generator1; |
---|
77 | generator1.setUsingObjective(true); |
---|
78 | generator1.setMaxPass(3); |
---|
79 | // Number of unsatisfied variables to look at |
---|
80 | generator1.setMaxProbe(10); |
---|
81 | // How far to follow the consequences |
---|
82 | generator1.setMaxLook(50); |
---|
83 | // Only look at rows with fewer than this number of elements |
---|
84 | generator1.setMaxElements(200); |
---|
85 | generator1.setRowCuts(3); |
---|
86 | |
---|
87 | |
---|
88 | // Add in generators |
---|
89 | // Experiment with -1 and -99 etc |
---|
90 | model.addCutGenerator(&generator1,-99,"Probing"); |
---|
91 | // Allow rounding heuristic |
---|
92 | |
---|
93 | CbcRounding heuristic1(model); |
---|
94 | model.addHeuristic(&heuristic1); |
---|
95 | |
---|
96 | // And Greedy heuristic |
---|
97 | |
---|
98 | CbcHeuristicGreedyCover heuristic2(model); |
---|
99 | // Use original upper and perturb more |
---|
100 | heuristic2.setAlgorithm(11); |
---|
101 | model.addHeuristic(&heuristic2); |
---|
102 | |
---|
103 | // Redundant definition of default branching (as Default == User) |
---|
104 | CbcBranchUserDecision branch; |
---|
105 | model.setBranchingMethod(&branch); |
---|
106 | |
---|
107 | // Definition of node choice |
---|
108 | CbcCompareUser compare; |
---|
109 | model.setNodeComparison(compare); |
---|
110 | |
---|
111 | int iColumn; |
---|
112 | int numberColumns = solver3->getNumCols(); |
---|
113 | // do pseudo costs |
---|
114 | CbcObject ** objects = new CbcObject * [numberColumns]; |
---|
115 | const CoinPackedMatrix * matrix = solver3->getMatrixByCol(); |
---|
116 | // Column copy |
---|
117 | const int * columnLength = matrix->getVectorLengths(); |
---|
118 | const double * objective = model.getObjCoefficients(); |
---|
119 | int numberIntegers=0; |
---|
120 | for (iColumn=0;iColumn<numberColumns;iColumn++) { |
---|
121 | if (solver3->isInteger(iColumn)) { |
---|
122 | /* Branching up gets us much closer to an integer solution so we want |
---|
123 | to encourage up - so we will branch up if variable value > 0.333333. |
---|
124 | The expected cost of going up obviously depends on the cost of the |
---|
125 | variable so we just choose pseudo costs to reflect that. We could also |
---|
126 | decide to try and use the pseudo costs to make it more likely to branch |
---|
127 | on a variable with many coefficients. This leads to the computation below. |
---|
128 | */ |
---|
129 | double cost = objective[iColumn]*(1.0 + 0.2*((double) columnLength[iColumn])); |
---|
130 | CbcSimpleIntegerPseudoCost * newObject = |
---|
131 | new CbcSimpleIntegerPseudoCost(&model,iColumn, |
---|
132 | 2.0*cost,cost); |
---|
133 | newObject->setMethod(3); |
---|
134 | objects[numberIntegers++]= newObject; |
---|
135 | } |
---|
136 | } |
---|
137 | model.addObjects(numberIntegers,objects); |
---|
138 | for (iColumn=0;iColumn<numberIntegers;iColumn++) |
---|
139 | delete objects[iColumn]; |
---|
140 | delete [] objects; |
---|
141 | |
---|
142 | // Do initial solve to continuous |
---|
143 | model.initialSolve(); |
---|
144 | |
---|
145 | // Do more strong branching if small |
---|
146 | // Switch off strong branching if wanted |
---|
147 | model.setNumberStrong(5); |
---|
148 | |
---|
149 | // say use resolve for strong branching |
---|
150 | osiclp->setSpecialOptions(16); |
---|
151 | // We had better allow a lot |
---|
152 | model.solver()->setIntParam(OsiMaxNumIterationHotStart,10000); |
---|
153 | // So use strategy to keep rows |
---|
154 | osiclp->setStrategy(1); |
---|
155 | |
---|
156 | // Switch off most output |
---|
157 | if (model.getNumCols()<3000) { |
---|
158 | model.messageHandler()->setLogLevel(1); |
---|
159 | //model.solver()->messageHandler()->setLogLevel(0); |
---|
160 | } else { |
---|
161 | model.messageHandler()->setLogLevel(2); |
---|
162 | model.solver()->messageHandler()->setLogLevel(1); |
---|
163 | } |
---|
164 | //model.setPrintFrequency(50); |
---|
165 | |
---|
166 | // Do complete search |
---|
167 | try { |
---|
168 | model.branchAndBound(); |
---|
169 | } |
---|
170 | catch (CoinError e) { |
---|
171 | e.print(); |
---|
172 | if (e.lineNumber()>=0) |
---|
173 | std::cout<<"This was from a CoinAssert"<<std::endl; |
---|
174 | exit(0); |
---|
175 | } |
---|
176 | //void printHowMany(); |
---|
177 | //printHowMany(); |
---|
178 | std::cout<<mpsFileName<<" took "<<CoinCpuTime()-time1<<" seconds, " |
---|
179 | <<model.getNodeCount()<<" nodes with objective " |
---|
180 | <<model.getObjValue() |
---|
181 | <<(!model.status() ? " Finished" : " Not finished") |
---|
182 | <<std::endl; |
---|
183 | |
---|
184 | // Print solution if finished - we can't get names from Osi! |
---|
185 | |
---|
186 | if (model.getMinimizationObjValue()<1.0e50) { |
---|
187 | int numberColumns = model.solver()->getNumCols(); |
---|
188 | |
---|
189 | const double * solution = model.solver()->getColSolution(); |
---|
190 | |
---|
191 | int iColumn; |
---|
192 | std::cout<<std::setiosflags(std::ios::fixed|std::ios::showpoint)<<std::setw(14); |
---|
193 | |
---|
194 | std::cout<<"--------------------------------------"<<std::endl; |
---|
195 | for (iColumn=0;iColumn<numberColumns;iColumn++) { |
---|
196 | double value=solution[iColumn]; |
---|
197 | if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn)) |
---|
198 | std::cout<<std::setw(6)<<iColumn<<" "<<value<<std::endl; |
---|
199 | } |
---|
200 | std::cout<<"--------------------------------------"<<std::endl; |
---|
201 | |
---|
202 | std::cout<<std::resetiosflags(std::ios::fixed|std::ios::showpoint|std::ios::scientific); |
---|
203 | } |
---|
204 | return 0; |
---|
205 | } |
---|