1 | // $Id: sample1.cpp 1898 2013-04-09 18:06:04Z stefan $ |
---|
2 | // Copyright (C) 2002, 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 | |
---|
8 | #include "CbcConfig.h" |
---|
9 | #include "CoinPragma.hpp" |
---|
10 | |
---|
11 | // For Branch and bound |
---|
12 | #include "OsiSolverInterface.hpp" |
---|
13 | #include "CbcModel.hpp" |
---|
14 | |
---|
15 | #include "OsiClpSolverInterface.hpp" |
---|
16 | #include "ClpPresolve.hpp" |
---|
17 | #include "CbcCompareUser.hpp" |
---|
18 | #include "CglProbing.hpp" |
---|
19 | |
---|
20 | //############################################################################# |
---|
21 | |
---|
22 | #ifdef NDEBUG |
---|
23 | #undef NDEBUG |
---|
24 | #endif |
---|
25 | // Time |
---|
26 | |
---|
27 | #include <time.h> |
---|
28 | #if !defined(_MSC_VER) |
---|
29 | #include <sys/times.h> |
---|
30 | #include <sys/resource.h> |
---|
31 | #include <unistd.h> |
---|
32 | #endif |
---|
33 | static double cpuTime() |
---|
34 | { |
---|
35 | double cpu_temp; |
---|
36 | #if defined(_MSC_VER) |
---|
37 | unsigned int ticksnow; /* clock_t is same as int */ |
---|
38 | |
---|
39 | ticksnow = (unsigned int)clock(); |
---|
40 | |
---|
41 | cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC); |
---|
42 | #else |
---|
43 | struct rusage usage; |
---|
44 | getrusage(RUSAGE_SELF,&usage); |
---|
45 | cpu_temp = (double)usage.ru_utime.tv_sec; |
---|
46 | cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec); |
---|
47 | #endif |
---|
48 | return cpu_temp; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | /************************************************************************ |
---|
53 | |
---|
54 | This main program reads in an integer model from an mps file. |
---|
55 | |
---|
56 | It then sets up some Cgl cut generators and calls branch and cut. |
---|
57 | |
---|
58 | Branching is simple binary branching on integer variables. |
---|
59 | |
---|
60 | Node selection is depth first until first solution is found and then |
---|
61 | based on objective and number of unsatisfied integer variables. |
---|
62 | |
---|
63 | Variable branching selection is on maximum minimum-of-up-down change |
---|
64 | after strong branching on 5 variables closest to 0.5. |
---|
65 | |
---|
66 | A simple rounding heuristic is used. |
---|
67 | |
---|
68 | Any cut generators based on Cgl can be added in same way |
---|
69 | |
---|
70 | You may also wish to look at CbcModel.hpp |
---|
71 | |
---|
72 | |
---|
73 | ************************************************************************/ |
---|
74 | |
---|
75 | |
---|
76 | int main (int argc, const char *argv[]) |
---|
77 | { |
---|
78 | |
---|
79 | // Define your favorite OsiSolver |
---|
80 | |
---|
81 | ClpSimplex simplex; |
---|
82 | double time0 = cpuTime(); |
---|
83 | double time1 = time0; |
---|
84 | double time2; |
---|
85 | |
---|
86 | // Read in model using argv[1] |
---|
87 | // and assert that it is a clean model |
---|
88 | |
---|
89 | if (argc <= 1) { |
---|
90 | printf("using %s <modelfile>\n", argv[0]); |
---|
91 | return 1; |
---|
92 | } |
---|
93 | int numMpsReadErrors = simplex.readMps(argv[1],""); |
---|
94 | if( numMpsReadErrors != 0 ) |
---|
95 | { |
---|
96 | printf("%d errors reading MPS file\n", numMpsReadErrors); |
---|
97 | return numMpsReadErrors; |
---|
98 | } |
---|
99 | time2 = cpuTime(); |
---|
100 | std::cout<<"Input took "<<time2-time1<<" seconds"<<std::endl;; |
---|
101 | time1 = time2; |
---|
102 | // Should work with OsiPresolve but not sure - so this is complicated |
---|
103 | ClpPresolve pinfo; |
---|
104 | ClpSimplex * simplex2 = pinfo.presolvedModel(simplex,1.0e-8); |
---|
105 | time2 = cpuTime(); |
---|
106 | std::cout<<"Presolve took "<<time2-time1<<" seconds"<<std::endl;; |
---|
107 | time1 = time2; |
---|
108 | OsiClpSolverInterface solver1(simplex2); |
---|
109 | solver1.writeMps("bad2"); |
---|
110 | // Do initial solve to continuous |
---|
111 | solver1.initialSolve(); |
---|
112 | time2 = cpuTime(); |
---|
113 | std::cout<<"Continuous solve took "<<time2-time1<<" seconds"<<std::endl;; |
---|
114 | time1 = time2; |
---|
115 | solver1.messageHandler()->setLogLevel(0); |
---|
116 | CbcModel model(solver1); |
---|
117 | // Definition of node choice |
---|
118 | CbcCompareUser compare; |
---|
119 | model.setNodeComparison(compare); |
---|
120 | |
---|
121 | // Maybe probing due to very large coefficients |
---|
122 | |
---|
123 | CglProbing generator1; |
---|
124 | generator1.setUsingObjective(true); |
---|
125 | generator1.setMaxPass(3); |
---|
126 | generator1.setMaxProbe(100); |
---|
127 | generator1.setMaxLook(50); |
---|
128 | generator1.setRowCuts(3); |
---|
129 | |
---|
130 | |
---|
131 | // Add in generators |
---|
132 | // model.addCutGenerator(&generator1,-1,"Probing"); |
---|
133 | // Switch off strong branching if wanted |
---|
134 | model.setNumberStrong(0); |
---|
135 | model.solver()->setIntParam(OsiMaxNumIterationHotStart,100); |
---|
136 | //model.solver()->setHintParam(OsiDoScale,false,OsiHintTry); |
---|
137 | // Switch off most output |
---|
138 | if (model.getNumCols()<3000) { |
---|
139 | model.messageHandler()->setLogLevel(1); |
---|
140 | model.solver()->messageHandler()->setLogLevel(0); |
---|
141 | } else { |
---|
142 | model.messageHandler()->setLogLevel(2); |
---|
143 | model.solver()->messageHandler()->setLogLevel(1); |
---|
144 | } |
---|
145 | |
---|
146 | // Do complete search |
---|
147 | |
---|
148 | model.branchAndBound(); |
---|
149 | time2 = cpuTime(); |
---|
150 | std::cout<<"Search took "<<time2-time1<<" seconds"<<std::endl; |
---|
151 | // as we made such a mess of presolve lets be safe |
---|
152 | OsiClpSolverInterface * clpSolver |
---|
153 | = dynamic_cast<OsiClpSolverInterface *> (model.solver()); |
---|
154 | assert (clpSolver); |
---|
155 | ClpSimplex * clp = clpSolver->getModelPtr(); |
---|
156 | *simplex2 = *clp; |
---|
157 | pinfo.postsolve(true); |
---|
158 | time1 = time2; |
---|
159 | // Fix all integers |
---|
160 | const int * original = pinfo.originalColumns(); |
---|
161 | double * lower2 = simplex2->columnLower(); |
---|
162 | double * upper2 = simplex2->columnUpper(); |
---|
163 | const char * info2 = simplex2->integerInformation(); |
---|
164 | double * lower = simplex.columnLower(); |
---|
165 | double * upper = simplex.columnUpper(); |
---|
166 | int i; |
---|
167 | for (i=0;i<simplex2->numberColumns();i++) { |
---|
168 | if (info2[i]) { |
---|
169 | int iSeq = original[i]; |
---|
170 | upper[iSeq]=upper2[i]; |
---|
171 | lower[iSeq]=lower2[i]; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | simplex.primal(); |
---|
176 | time2 = cpuTime(); |
---|
177 | std::cout<<"Cleanup took "<<time2-time1<<" seconds"<<std::endl;; |
---|
178 | std::cout<<"Total time "<<time2-time0<<" seconds"<<std::endl;; |
---|
179 | return 0; |
---|
180 | } |
---|