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