1 | // $Id: sample1.cpp 2361 2018-01-30 15:49:34Z forrest $ |
---|
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 | if (!simplex2 ||!simplex2->integerInformation()) { |
---|
109 | std::cout<<"Please use a feasible problem which has integers after presolve"<<std::endl; |
---|
110 | exit(77); |
---|
111 | } |
---|
112 | OsiClpSolverInterface solver1(simplex2); |
---|
113 | solver1.writeMps("bad2"); |
---|
114 | // Do initial solve to continuous |
---|
115 | solver1.initialSolve(); |
---|
116 | time2 = cpuTime(); |
---|
117 | std::cout<<"Continuous solve took "<<time2-time1<<" seconds"<<std::endl;; |
---|
118 | time1 = time2; |
---|
119 | solver1.messageHandler()->setLogLevel(0); |
---|
120 | CbcModel model(solver1); |
---|
121 | // Definition of node choice |
---|
122 | CbcCompareUser compare; |
---|
123 | model.setNodeComparison(compare); |
---|
124 | |
---|
125 | // Maybe probing due to very large coefficients |
---|
126 | |
---|
127 | CglProbing generator1; |
---|
128 | generator1.setUsingObjective(true); |
---|
129 | generator1.setMaxPass(3); |
---|
130 | generator1.setMaxProbe(100); |
---|
131 | generator1.setMaxLook(50); |
---|
132 | generator1.setRowCuts(3); |
---|
133 | |
---|
134 | |
---|
135 | // Add in generators |
---|
136 | // model.addCutGenerator(&generator1,-1,"Probing"); |
---|
137 | // Switch off strong branching if wanted |
---|
138 | model.setNumberStrong(0); |
---|
139 | model.solver()->setIntParam(OsiMaxNumIterationHotStart,100); |
---|
140 | //model.solver()->setHintParam(OsiDoScale,false,OsiHintTry); |
---|
141 | // Switch off most output |
---|
142 | if (model.getNumCols()<3000) { |
---|
143 | model.messageHandler()->setLogLevel(1); |
---|
144 | model.solver()->messageHandler()->setLogLevel(0); |
---|
145 | } else { |
---|
146 | model.messageHandler()->setLogLevel(2); |
---|
147 | model.solver()->messageHandler()->setLogLevel(1); |
---|
148 | } |
---|
149 | |
---|
150 | // Do complete search |
---|
151 | |
---|
152 | model.branchAndBound(); |
---|
153 | time2 = cpuTime(); |
---|
154 | std::cout<<"Search took "<<time2-time1<<" seconds"<<std::endl; |
---|
155 | // as we made such a mess of presolve lets be safe |
---|
156 | OsiClpSolverInterface * clpSolver |
---|
157 | = dynamic_cast<OsiClpSolverInterface *> (model.solver()); |
---|
158 | assert (clpSolver); |
---|
159 | ClpSimplex * clp = clpSolver->getModelPtr(); |
---|
160 | *simplex2 = *clp; |
---|
161 | pinfo.postsolve(true); |
---|
162 | time1 = time2; |
---|
163 | // Fix all integers |
---|
164 | const int * original = pinfo.originalColumns(); |
---|
165 | double * lower2 = simplex2->columnLower(); |
---|
166 | double * upper2 = simplex2->columnUpper(); |
---|
167 | const char * info2 = simplex2->integerInformation(); |
---|
168 | double * lower = simplex.columnLower(); |
---|
169 | double * upper = simplex.columnUpper(); |
---|
170 | int i; |
---|
171 | for (i=0;i<simplex2->numberColumns();i++) { |
---|
172 | if (info2[i]) { |
---|
173 | int iSeq = original[i]; |
---|
174 | upper[iSeq]=upper2[i]; |
---|
175 | lower[iSeq]=lower2[i]; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | simplex.primal(); |
---|
180 | time2 = cpuTime(); |
---|
181 | std::cout<<"Cleanup took "<<time2-time1<<" seconds"<<std::endl;; |
---|
182 | std::cout<<"Total time "<<time2-time0<<" seconds"<<std::endl;; |
---|
183 | return 0; |
---|
184 | } |
---|