1 | // $Id: qmip.cpp 2469 2019-01-06 23:17:46Z forrest $ |
---|
2 | // Copyright (C) 2004, 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 | #include <cassert> |
---|
7 | #include <iomanip> |
---|
8 | |
---|
9 | #include "CoinPragma.hpp" |
---|
10 | |
---|
11 | // For Branch and bound |
---|
12 | #include "OsiSolverInterface.hpp" |
---|
13 | #include "CbcModel.hpp" |
---|
14 | #include "CbcBranchUser.hpp" |
---|
15 | #include "CbcCompareUser.hpp" |
---|
16 | #include "CbcCutGenerator.hpp" |
---|
17 | #include "CbcHeuristicLocal.hpp" |
---|
18 | #include "ClpQuadInterface.hpp" |
---|
19 | |
---|
20 | // Cuts - some may work but be very very careful |
---|
21 | |
---|
22 | #include "CglProbing.hpp" |
---|
23 | |
---|
24 | // Heuristics would need adapting |
---|
25 | |
---|
26 | #include "CbcHeuristic.hpp" |
---|
27 | |
---|
28 | // Time |
---|
29 | #include "CoinTime.hpp" |
---|
30 | |
---|
31 | /************************************************************************ |
---|
32 | |
---|
33 | This main program reads in a quadratic integer model from an mps file. |
---|
34 | |
---|
35 | It then sets up some Cgl cut generators and calls branch and cut. |
---|
36 | |
---|
37 | Branching is simple binary branching on integer variables. |
---|
38 | |
---|
39 | Node selection is depth first until first solution is found and then |
---|
40 | based on objective and number of unsatisfied integer variables. |
---|
41 | In this example the functionality is the same as default but it is |
---|
42 | a user comparison function. |
---|
43 | |
---|
44 | Variable branching selection is on maximum minimum-of-up-down change |
---|
45 | after strong branching on 5 variables closest to 0.5. |
---|
46 | |
---|
47 | A simple rounding heuristic could be used.but is switched off as needs work for quadratic |
---|
48 | |
---|
49 | This is NOT meant to be a serious MIQP code; it is to show how you can use |
---|
50 | ClpQuadInterface.hpp to solve a QP at each node. You could pick up data in that interface |
---|
51 | and use any QP solver (e.g. one that works) |
---|
52 | |
---|
53 | ************************************************************************/ |
---|
54 | |
---|
55 | int main(int argc, const char *argv[]) |
---|
56 | { |
---|
57 | |
---|
58 | // Define a Solver which inherits from OsiClpsolverInterface -> OsiSolverInterface |
---|
59 | |
---|
60 | ClpQuadInterface solver1; |
---|
61 | |
---|
62 | // Read in model using argv[1] |
---|
63 | if (argc <= 1) { |
---|
64 | printf("using %s <modelfile>\n", argv[0]); |
---|
65 | return 1; |
---|
66 | } |
---|
67 | // must use clp to get a quadratic model |
---|
68 | ClpSimplex *clp = solver1.getModelPtr(); |
---|
69 | int numMpsReadErrors = clp->readMps(argv[1]); |
---|
70 | // and assert that it is a clean model |
---|
71 | if (numMpsReadErrors != 0) { |
---|
72 | printf("%d errors reading MPS file\n", numMpsReadErrors); |
---|
73 | return numMpsReadErrors; |
---|
74 | } |
---|
75 | |
---|
76 | // This clones solver |
---|
77 | CbcModel model(solver1); |
---|
78 | // But now model doesn't know about integers! |
---|
79 | const char *integerInfo = clp->integerInformation(); |
---|
80 | int numberColumns = clp->numberColumns(); |
---|
81 | // and point to solver |
---|
82 | OsiSolverInterface *solver2 = model.solver(); |
---|
83 | for (int iColumn = 0; iColumn < numberColumns; iColumn++) { |
---|
84 | if (integerInfo[iColumn]) |
---|
85 | solver2->setInteger(iColumn); |
---|
86 | } |
---|
87 | // Okay - now we have a good MIQP solver |
---|
88 | // Within branch and cut it is better (at present) to switch off all objectives |
---|
89 | |
---|
90 | ClpQuadInterface *osiclp = dynamic_cast< ClpQuadInterface * >(solver2); |
---|
91 | assert(osiclp); |
---|
92 | // Set fake objective so Cbc not confused |
---|
93 | osiclp->initialize(); |
---|
94 | solver2->setHintParam(OsiDoReducePrint, true, OsiHintTry); |
---|
95 | |
---|
96 | // Set up some cut generators and defaults |
---|
97 | // Probing first as gets tight bounds on continuous |
---|
98 | |
---|
99 | CglProbing generator1; |
---|
100 | // can not use objective |
---|
101 | generator1.setUsingObjective(false); |
---|
102 | generator1.setMaxPass(3); |
---|
103 | generator1.setMaxProbe(100); |
---|
104 | generator1.setMaxLook(50); |
---|
105 | generator1.setRowCuts(3); |
---|
106 | |
---|
107 | // Add in generators |
---|
108 | // Only some generators work (and even then try without first) |
---|
109 | model.addCutGenerator(&generator1, 1, "Probing"); |
---|
110 | // Allow rounding heuristic |
---|
111 | |
---|
112 | CbcRounding heuristic1(model); |
---|
113 | // do not add yet as don't know how to deal with quadratic objective |
---|
114 | //model.addHeuristic(&heuristic1); |
---|
115 | |
---|
116 | // Redundant definition of default branching (as Default == User) |
---|
117 | CbcBranchUserDecision branch; |
---|
118 | model.setBranchingMethod(&branch); |
---|
119 | |
---|
120 | // Definition of node choice |
---|
121 | CbcCompareUser compare; |
---|
122 | // breadth first |
---|
123 | //compare.setWeight(0.0); |
---|
124 | model.setNodeComparison(compare); |
---|
125 | |
---|
126 | // Do initial solve to continuous |
---|
127 | model.initialSolve(); |
---|
128 | |
---|
129 | // Could tune more |
---|
130 | model.setMinimumDrop(CoinMin(1.0, |
---|
131 | fabs(model.getMinimizationObjValue()) * 1.0e-3 + 1.0e-4)); |
---|
132 | |
---|
133 | model.setMaximumCutPassesAtRoot(0); |
---|
134 | model.setMaximumCutPasses(0); |
---|
135 | |
---|
136 | // Switch off strong branching if wanted |
---|
137 | //model.setNumberStrong(5); |
---|
138 | |
---|
139 | model.solver()->setIntParam(OsiMaxNumIterationHotStart, 10000); |
---|
140 | |
---|
141 | // If time is given then stop after that number of minutes |
---|
142 | if (argc > 2) { |
---|
143 | int minutes = atoi(argv[2]); |
---|
144 | std::cout << "Stopping after " << minutes << " minutes" << std::endl; |
---|
145 | assert(minutes >= 0); |
---|
146 | model.setDblParam(CbcModel::CbcMaximumSeconds, 60.0 * minutes); |
---|
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 | double time1 = CoinCpuTime(); |
---|
159 | |
---|
160 | // Do complete search |
---|
161 | |
---|
162 | model.branchAndBound(); |
---|
163 | |
---|
164 | std::cout << argv[1] << " took " << CoinCpuTime() - time1 << " seconds, " |
---|
165 | << model.getNodeCount() << " nodes with objective " |
---|
166 | << model.getObjValue() |
---|
167 | << (!model.status() ? " Finished" : " Not finished") |
---|
168 | << std::endl; |
---|
169 | |
---|
170 | // Print more statistics |
---|
171 | std::cout << "Cuts at root node changed objective from " << model.getContinuousObjective() |
---|
172 | << " to " << model.rootObjectiveAfterCuts() << std::endl; |
---|
173 | |
---|
174 | int numberGenerators = model.numberCutGenerators(); |
---|
175 | for (int iGenerator = 0; iGenerator < numberGenerators; iGenerator++) { |
---|
176 | CbcCutGenerator *generator = model.cutGenerator(iGenerator); |
---|
177 | std::cout << generator->cutGeneratorName() << " was tried " |
---|
178 | << generator->numberTimesEntered() << " times and created " |
---|
179 | << generator->numberCutsInTotal() << " cuts of which " |
---|
180 | << generator->numberCutsActive() << " were active after adding rounds of cuts" |
---|
181 | << std::endl; |
---|
182 | } |
---|
183 | // Print solution if finished - we can't get names from Osi! |
---|
184 | |
---|
185 | if (!model.status() && model.getMinimizationObjValue() < 1.0e50) { |
---|
186 | int numberColumns = model.solver()->getNumCols(); |
---|
187 | |
---|
188 | const double *solution = model.solver()->getColSolution(); |
---|
189 | |
---|
190 | int iColumn; |
---|
191 | std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14); |
---|
192 | |
---|
193 | std::cout << "--------------------------------------" << std::endl; |
---|
194 | for (iColumn = 0; iColumn < numberColumns; iColumn++) { |
---|
195 | double value = solution[iColumn]; |
---|
196 | if (fabs(value) > 1.0e-7 && model.solver()->isInteger(iColumn)) |
---|
197 | std::cout << std::setw(6) << iColumn << " " << value << std::endl; |
---|
198 | } |
---|
199 | std::cout << "--------------------------------------" << std::endl; |
---|
200 | |
---|
201 | std::cout << std::resetiosflags(std::ios::fixed | std::ios::showpoint | std::ios::scientific); |
---|
202 | } |
---|
203 | return 0; |
---|
204 | } |
---|