1 | /* $Id: CbcHeuristicGreedy.hpp 2464 2019-01-03 19:03:23Z unxusr $ */ |
---|
2 | // Copyright (C) 2005, 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 | #ifndef CbcHeuristicGreedy_H |
---|
7 | #define CbcHeuristicGreedy_H |
---|
8 | |
---|
9 | #include "CbcHeuristic.hpp" |
---|
10 | /** Greedy heuristic classes |
---|
11 | */ |
---|
12 | |
---|
13 | class CbcHeuristicGreedyCover : public CbcHeuristic { |
---|
14 | public: |
---|
15 | // Default Constructor |
---|
16 | CbcHeuristicGreedyCover(); |
---|
17 | |
---|
18 | /* Constructor with model - assumed before cuts |
---|
19 | Initial version does not do Lps |
---|
20 | */ |
---|
21 | CbcHeuristicGreedyCover(CbcModel &model); |
---|
22 | |
---|
23 | // Copy constructor |
---|
24 | CbcHeuristicGreedyCover(const CbcHeuristicGreedyCover &); |
---|
25 | |
---|
26 | // Destructor |
---|
27 | ~CbcHeuristicGreedyCover(); |
---|
28 | |
---|
29 | /// Clone |
---|
30 | virtual CbcHeuristic *clone() const; |
---|
31 | /// Assignment operator |
---|
32 | CbcHeuristicGreedyCover &operator=(const CbcHeuristicGreedyCover &rhs); |
---|
33 | /// Create C++ lines to get to current state |
---|
34 | virtual void generateCpp(FILE *fp); |
---|
35 | |
---|
36 | /// update model (This is needed if cliques update matrix etc) |
---|
37 | virtual void setModel(CbcModel *model); |
---|
38 | |
---|
39 | using CbcHeuristic::solution; |
---|
40 | /** returns 0 if no solution, 1 if valid solution. |
---|
41 | Sets solution values if good, sets objective value (only if good) |
---|
42 | We leave all variables which are at one at this node of the |
---|
43 | tree to that value and will |
---|
44 | initially set all others to zero. We then sort all variables in order of their cost |
---|
45 | divided by the number of entries in rows which are not yet covered. We randomize that |
---|
46 | value a bit so that ties will be broken in different ways on different runs of the heuristic. |
---|
47 | We then choose the best one and set it to one and repeat the exercise. |
---|
48 | |
---|
49 | */ |
---|
50 | virtual int solution(double &objectiveValue, |
---|
51 | double *newSolution); |
---|
52 | /// Validate model i.e. sets when_ to 0 if necessary (may be NULL) |
---|
53 | virtual void validate(); |
---|
54 | /// Resets stuff if model changes |
---|
55 | virtual void resetModel(CbcModel *model); |
---|
56 | /* Algorithm |
---|
57 | 0 - use current upper bounds |
---|
58 | 1 - use original upper bounds |
---|
59 | If 10 added perturb ratios more |
---|
60 | if 100 added round up all >=0.5 |
---|
61 | */ |
---|
62 | inline int algorithm() const |
---|
63 | { |
---|
64 | return algorithm_; |
---|
65 | } |
---|
66 | inline void setAlgorithm(int value) |
---|
67 | { |
---|
68 | algorithm_ = value; |
---|
69 | } |
---|
70 | // Only do this many times |
---|
71 | inline int numberTimes() const |
---|
72 | { |
---|
73 | return numberTimes_; |
---|
74 | } |
---|
75 | inline void setNumberTimes(int value) |
---|
76 | { |
---|
77 | numberTimes_ = value; |
---|
78 | } |
---|
79 | |
---|
80 | protected: |
---|
81 | /// Guts of constructor from a CbcModel |
---|
82 | void gutsOfConstructor(CbcModel *model); |
---|
83 | // Data |
---|
84 | |
---|
85 | // Original matrix by column |
---|
86 | CoinPackedMatrix matrix_; |
---|
87 | // original number of rows |
---|
88 | int originalNumberRows_; |
---|
89 | /* Algorithm |
---|
90 | 0 - use current upper bounds |
---|
91 | 1 - use original upper bounds |
---|
92 | If 10 added perturb ratios more |
---|
93 | */ |
---|
94 | int algorithm_; |
---|
95 | /// Do this many times |
---|
96 | int numberTimes_; |
---|
97 | }; |
---|
98 | |
---|
99 | class CbcHeuristicGreedyEquality : public CbcHeuristic { |
---|
100 | public: |
---|
101 | // Default Constructor |
---|
102 | CbcHeuristicGreedyEquality(); |
---|
103 | |
---|
104 | /* Constructor with model - assumed before cuts |
---|
105 | Initial version does not do Lps |
---|
106 | */ |
---|
107 | CbcHeuristicGreedyEquality(CbcModel &model); |
---|
108 | |
---|
109 | // Copy constructor |
---|
110 | CbcHeuristicGreedyEquality(const CbcHeuristicGreedyEquality &); |
---|
111 | |
---|
112 | // Destructor |
---|
113 | ~CbcHeuristicGreedyEquality(); |
---|
114 | |
---|
115 | /// Clone |
---|
116 | virtual CbcHeuristic *clone() const; |
---|
117 | /// Assignment operator |
---|
118 | CbcHeuristicGreedyEquality &operator=(const CbcHeuristicGreedyEquality &rhs); |
---|
119 | /// Create C++ lines to get to current state |
---|
120 | virtual void generateCpp(FILE *fp); |
---|
121 | |
---|
122 | /// update model (This is needed if cliques update matrix etc) |
---|
123 | virtual void setModel(CbcModel *model); |
---|
124 | |
---|
125 | using CbcHeuristic::solution; |
---|
126 | /** returns 0 if no solution, 1 if valid solution. |
---|
127 | Sets solution values if good, sets objective value (only if good) |
---|
128 | We leave all variables which are at one at this node of the |
---|
129 | tree to that value and will |
---|
130 | initially set all others to zero. We then sort all variables in order of their cost |
---|
131 | divided by the number of entries in rows which are not yet covered. We randomize that |
---|
132 | value a bit so that ties will be broken in different ways on different runs of the heuristic. |
---|
133 | We then choose the best one and set it to one and repeat the exercise. |
---|
134 | |
---|
135 | */ |
---|
136 | virtual int solution(double &objectiveValue, |
---|
137 | double *newSolution); |
---|
138 | /// Validate model i.e. sets when_ to 0 if necessary (may be NULL) |
---|
139 | virtual void validate(); |
---|
140 | /// Resets stuff if model changes |
---|
141 | virtual void resetModel(CbcModel *model); |
---|
142 | /* Algorithm |
---|
143 | 0 - use current upper bounds |
---|
144 | 1 - use original upper bounds |
---|
145 | If 10 added perturb ratios more |
---|
146 | if 100 added round up all >=0.5 |
---|
147 | */ |
---|
148 | inline int algorithm() const |
---|
149 | { |
---|
150 | return algorithm_; |
---|
151 | } |
---|
152 | inline void setAlgorithm(int value) |
---|
153 | { |
---|
154 | algorithm_ = value; |
---|
155 | } |
---|
156 | // Fraction of rhs to cover before branch and cut |
---|
157 | inline void setFraction(double value) |
---|
158 | { |
---|
159 | fraction_ = value; |
---|
160 | } |
---|
161 | inline double fraction() const |
---|
162 | { |
---|
163 | return fraction_; |
---|
164 | } |
---|
165 | // Only do this many times |
---|
166 | inline int numberTimes() const |
---|
167 | { |
---|
168 | return numberTimes_; |
---|
169 | } |
---|
170 | inline void setNumberTimes(int value) |
---|
171 | { |
---|
172 | numberTimes_ = value; |
---|
173 | } |
---|
174 | |
---|
175 | protected: |
---|
176 | /// Guts of constructor from a CbcModel |
---|
177 | void gutsOfConstructor(CbcModel *model); |
---|
178 | // Data |
---|
179 | |
---|
180 | // Original matrix by column |
---|
181 | CoinPackedMatrix matrix_; |
---|
182 | // Fraction of rhs to cover before branch and cut |
---|
183 | double fraction_; |
---|
184 | // original number of rows |
---|
185 | int originalNumberRows_; |
---|
186 | /* Algorithm |
---|
187 | 0 - use current upper bounds |
---|
188 | 1 - use original upper bounds |
---|
189 | If 10 added perturb ratios more |
---|
190 | */ |
---|
191 | int algorithm_; |
---|
192 | /// Do this many times |
---|
193 | int numberTimes_; |
---|
194 | }; |
---|
195 | |
---|
196 | /** Greedy heuristic for SOS and L rows (and positive elements) |
---|
197 | */ |
---|
198 | |
---|
199 | class CbcHeuristicGreedySOS : public CbcHeuristic { |
---|
200 | public: |
---|
201 | // Default Constructor |
---|
202 | CbcHeuristicGreedySOS(); |
---|
203 | |
---|
204 | /* Constructor with model - assumed before cuts |
---|
205 | Initial version does not do Lps |
---|
206 | */ |
---|
207 | CbcHeuristicGreedySOS(CbcModel &model); |
---|
208 | |
---|
209 | // Copy constructor |
---|
210 | CbcHeuristicGreedySOS(const CbcHeuristicGreedySOS &); |
---|
211 | |
---|
212 | // Destructor |
---|
213 | ~CbcHeuristicGreedySOS(); |
---|
214 | |
---|
215 | /// Clone |
---|
216 | virtual CbcHeuristic *clone() const; |
---|
217 | /// Assignment operator |
---|
218 | CbcHeuristicGreedySOS &operator=(const CbcHeuristicGreedySOS &rhs); |
---|
219 | /// Create C++ lines to get to current state |
---|
220 | virtual void generateCpp(FILE *fp); |
---|
221 | |
---|
222 | /// update model (This is needed if cliques update matrix etc) |
---|
223 | virtual void setModel(CbcModel *model); |
---|
224 | |
---|
225 | using CbcHeuristic::solution; |
---|
226 | /** returns 0 if no solution, 1 if valid solution. |
---|
227 | Sets solution values if good, sets objective value (only if good) |
---|
228 | We leave all variables which are at one at this node of the |
---|
229 | tree to that value and will |
---|
230 | initially set all others to zero. We then sort all variables in order of their cost |
---|
231 | divided by the number of entries in rows which are not yet covered. We randomize that |
---|
232 | value a bit so that ties will be broken in different ways on different runs of the heuristic. |
---|
233 | We then choose the best one and set it to one and repeat the exercise. |
---|
234 | |
---|
235 | */ |
---|
236 | virtual int solution(double &objectiveValue, |
---|
237 | double *newSolution); |
---|
238 | /// Validate model i.e. sets when_ to 0 if necessary (may be NULL) |
---|
239 | virtual void validate(); |
---|
240 | /// Resets stuff if model changes |
---|
241 | virtual void resetModel(CbcModel *model); |
---|
242 | /* Algorithm |
---|
243 | Bits |
---|
244 | 1 bit - use current model, otherwise original |
---|
245 | 2 - use current solution as starting point, otherwise pure greedy |
---|
246 | 4 - as 2 but use merit not merit/size |
---|
247 | 8 - use duals to modify greedy |
---|
248 | 16 - use duals on GUB/SOS in special way |
---|
249 | */ |
---|
250 | inline int algorithm() const |
---|
251 | { |
---|
252 | return algorithm_; |
---|
253 | } |
---|
254 | inline void setAlgorithm(int value) |
---|
255 | { |
---|
256 | algorithm_ = value; |
---|
257 | } |
---|
258 | // Only do this many times |
---|
259 | inline int numberTimes() const |
---|
260 | { |
---|
261 | return numberTimes_; |
---|
262 | } |
---|
263 | inline void setNumberTimes(int value) |
---|
264 | { |
---|
265 | numberTimes_ = value; |
---|
266 | } |
---|
267 | |
---|
268 | protected: |
---|
269 | /// Guts of constructor from a CbcModel |
---|
270 | void gutsOfConstructor(CbcModel *model); |
---|
271 | // Data |
---|
272 | |
---|
273 | // Original RHS - if -1.0 then SOS otherwise <= value |
---|
274 | double *originalRhs_; |
---|
275 | // Original matrix by column |
---|
276 | CoinPackedMatrix matrix_; |
---|
277 | // original number of rows |
---|
278 | int originalNumberRows_; |
---|
279 | /* Algorithm |
---|
280 | */ |
---|
281 | int algorithm_; |
---|
282 | /// Do this many times |
---|
283 | int numberTimes_; |
---|
284 | }; |
---|
285 | |
---|
286 | #endif |
---|