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